home *** CD-ROM | disk | FTP | other *** search
/ Media Share 9 / MEDIASHARE_09.ISO / hamradio / s920603.zip / TTYLINK.C < prev    next >
C/C++ Source or Header  |  1992-05-19  |  2KB  |  111 lines

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