home *** CD-ROM | disk | FTP | other *** search
/ High Voltage Shareware / high1.zip / high1 / DIR3 / KA9Q212.ZIP / TTYLINK.C < prev    next >
C/C++ Source or Header  |  1993-07-16  |  3KB  |  116 lines

  1. /* Internet TTY "link" (keyboard chat) server
  2.  * Copyright 1991 Phil Karn, KA9Q
  3.  */
  4.  
  5. /****************************************************************************
  6. *    $Id: ttylink.c 1.2 93/07/16 11:51:53 ROOT_DOS Exp $
  7. *    08 May 93    1.2        GT    Fix warnings.                                    *
  8. ****************************************************************************/
  9.  
  10.  
  11. #include <stdio.h>
  12. #include "global.h"
  13. #include "mbuf.h"
  14. #include "socket.h"
  15. #include "telnet.h"
  16. #include "session.h"
  17. #include "proc.h"
  18. #include "tty.h"
  19. #include "mailbox.h"
  20. #include "commands.h"
  21.  
  22. static int Sttylink = -1;    /* Protoype socket for service */
  23.  
  24. int
  25. ttylstart(argc,argv,p)
  26. int argc;
  27. char *argv[];
  28. void *p;
  29. {
  30.     struct sockaddr_in lsocket;
  31.     int s,type;
  32.  
  33.     if(Sttylink != -1){
  34.         return 0;
  35.     }
  36.     psignal(Curproc,0);    /* Don't keep the parser waiting */
  37.     chname(Curproc,"TTYlink listener");
  38.  
  39.     lsocket.sin_family = AF_INET;
  40.     lsocket.sin_addr.s_addr = INADDR_ANY;
  41.     if(argc < 2)
  42.         lsocket.sin_port = IPPORT_TTYLINK;
  43.     else
  44.         lsocket.sin_port = atoi(argv[1]);
  45.  
  46.     Sttylink = socket(AF_INET,SOCK_STREAM,0);
  47.     bind(Sttylink,(char *)&lsocket,sizeof(lsocket));
  48.     listen(Sttylink,1);
  49.     for(;;){
  50.         if((s = accept(Sttylink,NULLCHAR,(int *)NULL)) == -1)
  51.             break;    /* Service is shutting down */
  52.         
  53.         if(availmem() < Memthresh){
  54.             usprintf(s,"System is overloaded; try again later\n");
  55.             shutdown(s,1);
  56.         } else {
  57.             type = TELNET;
  58.             newproc("chat",2048,ttylhandle,s,(void *)&type,NULL,0);
  59.         }
  60.     }
  61.     return 0;
  62. }
  63. /* This function handles all incoming "chat" sessions, be they TCP,
  64.  * NET/ROM or AX.25
  65.  */
  66. void
  67. ttylhandle(s,t,p)
  68. int s;
  69. void *t;
  70. void *p;
  71. {
  72.     int type,index;
  73.     struct session *sp;
  74.     char addr[MAXSOCKSIZE];
  75.     int len = MAXSOCKSIZE;
  76.     struct telnet tn;
  77.  
  78.     type = * (int *)t;
  79.     sockmode(s,SOCK_ASCII);
  80.     sockowner(s,Curproc);    /* We own it now */
  81.     log(s,"open %s",Sestypes[type]);
  82.  
  83.     /* Allocate a session descriptor */
  84.     if((sp = newsession(NULLCHAR,type)) == NULLSESSION){
  85.         usprintf(s,"Too many sessions\n");
  86.         close_s(s);
  87.         return;
  88.     }
  89.     index = (int) (sp - Sessions);
  90.  
  91.     /* Initialize a Telnet protocol descriptor */
  92.     memset((char *)&tn,0,sizeof(tn));
  93.     tn.session = sp;    /* Upward pointer */
  94.     sp->cb.telnet = &tn;    /* Downward pointer */
  95.     sp->s = s;
  96.     sp->proc = Curproc;
  97.  
  98.     getpeername(s,addr,&len);
  99.     tprintf("\007Incoming %s session %u from %s\007\n",
  100.      Sestypes[type],index,psocket(addr));
  101.  
  102.     tnrecv(&tn);
  103. }
  104.  
  105. /* Shut down Ttylink server */
  106. int
  107. ttyl0(argc,argv,p)
  108. int argc;
  109. char *argv[];
  110. void *p;
  111. {
  112.     close_s(Sttylink);
  113.     Sttylink = -1;
  114.     return 0;
  115. }
  116.