/*
 *      $Filename: TnServ.c $
 *      $Revision: 2.0 $
 *      $Date: 1995/04/13 19:53:48 $
 *
 *      Copyright (C) 1993,94 by Steve Holland <sdh4@cornell.edu>
 *      Copyright (C) 1995 by Peter Simons <simons@peti.rhein.de>
 *
 *      This program is free software; you can redistribute it and/or
 *      modify it under the terms of the GNU General Public License as
 *      published by the Free Software Foundation; either version 2 of
 *      the License, or (at your option) any later version.
 *
 *      This program is distributed in the hope that it will be useful,
 *      but WITHOUT ANY WARRANTY; without even the implied warranty of
 *      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 *      General Public License for more details.
 *
 *      You should have received a copy of the GNU General Public License
 *      along with this program; if not, write to the Free Software
 *      Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 *
 *      $Id: TnServ.c 2.0 1995/04/13 19:53:48 simons Exp $
 *
 */

/************************************* includes ***********/
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <proto/socket.h>
#include <netdb.h>
#include <errno.h>
#include <dos/dos.h>
#include <dos/dostags.h>
#include <proto/dos.h>
#include <proto/exec.h>
#include <clib/netlib_protos.h>

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

#include "TnServ_rev.h"

/************************************* defines ************/

/************************************* global variables ***/
static const char __DOSVer[] = VERSTAG " written by Peter Simons <simons@peti.rhein.de>";

int main(void)
{
        struct servent *sp;
        struct sockaddr_in ServAddr;
        LONG Socket, SubSock, SubSockID;
        int WaitSig;
        static char ArgStr[200];
        BPTR TelNetProg;
        long Len;

        if (!(sp = getservbyname("telnet", "tcp")))
                return 21;

        bzero(&ServAddr, sizeof(ServAddr));
        ServAddr.sin_port = sp->s_port;
        ServAddr.sin_addr.s_addr = htonl(INADDR_ANY);
        ServAddr.sin_family = AF_INET;
        ServAddr.sin_len = sizeof(ServAddr);
        Socket = socket(AF_INET, SOCK_STREAM, 0);
        if (Socket < 0)
                return 22;
        bind(Socket, (struct sockaddr *) &ServAddr, sizeof(ServAddr));
        listen(Socket, 5);
        WaitSig = AllocSignal(-1);
        if (WaitSig == -1)
                return 23;

        for (;;) {
                Len = sizeof(ServAddr);
                SubSock = accept(Socket, (struct sockaddr *) &ServAddr, &Len);
                if (SubSock < 0) {
                        if (Errno() == EINTR) {
                                CloseSocket(Socket);
                                return 0;
                        }
                        continue;
                }
                SetSignal(0, (1 << WaitSig));   /* unset WaitSig signal */
                SubSockID = ReleaseSocket(SubSock, UNIQUE_ID);
                sprintf(ArgStr, "%u %u %u", (unsigned long) SubSockID, (unsigned long) WaitSig, (unsigned long) FindTask(NULL));
                if (TelNetProg = LoadSeg("AmiTCP:serv/telnetd")) {
                        if (CreateNewProcTags(NP_Seglist, TelNetProg,
                                                NP_Name, "telnet login",
                                                NP_Arguments, ArgStr,
                                                NP_StackSize, 12000,
                                                NP_FreeSeglist, TRUE,
                                                TAG_END)) {

                                /* CreateProc("telnet login",0,TelNetProg,12000); */
                                Wait(1 << WaitSig);     /* Wait for new proc to start up */
                        }
                }
        }

}


/************************************* subroutines ********/
