home *** CD-ROM | disk | FTP | other *** search
/ ARM Club 3 / TheARMClub_PDCD3.iso / hensa / internet / tcpip / src205 / TCPIP_Src / Telnet / c / tnserv < prev   
Encoding:
Text File  |  1995-04-18  |  4.1 KB  |  166 lines

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <time.h>
  5. #include "global.h"
  6. #include "mbuf.h"
  7. #include "timer.h"
  8. #include "icmp.h"
  9. #include "netuser.h"
  10. #include "tcp.h"
  11. #include "telnet.h"
  12. #include "session.h"
  13. #include "misc.h"
  14. #include "vterm.h"
  15.  
  16. extern char bnrtelnetat[];
  17. extern char bnrtelnetun[];
  18. extern int do_banner(char *file, char *pfx, struct tcb *c);
  19.  
  20. static void tnet_state(struct tcb *, char, char);
  21. static void sndmsg(struct tcb *, char *);
  22.  
  23. struct tcb *tnet_tcb;
  24. int tn1(int argc, char **argv)
  25. {
  26.   struct socket lsocket;
  27.   extern int32 ip_addr;
  28.  
  29.   /* Incoming Telnet */
  30.   lsocket.address = ip_addr;
  31.   if(argc < 2)
  32.     lsocket.port = TELNET_PORT;
  33.   else
  34.     lsocket.port = atoi(argv[1]);
  35.   tnet_tcb = open_tcp(&lsocket, NULLSOCK, TCP_SERVER, 0, (void(*)())rcv_char, NULLVFP, (void(*)())tnet_state, 0, (char *)NULL);
  36.   return(0);
  37. }
  38. /* Handle incoming Telnet connect requests by creating a Telnet session,
  39.    then change upcall vector so it behaves like an ordinary Telnet session. */
  40. static void tnet_state(struct tcb *tcb, char old, char new)
  41. {
  42.   struct telnet *tn;
  43.   struct session *s;
  44.   char *a, buf[80];
  45.   extern char hostname[];
  46.   extern int attended;
  47.   char *cp;
  48.   time_t t;
  49.  
  50.   old = old;
  51.  
  52.   time(&t);
  53.   cp = ctime(&t);
  54.   rip(cp);
  55.  
  56.   switch(new)
  57.   {
  58.   case ESTABLISHED:
  59.     set_server_vars("telnet",tcb);
  60.     log_event(tcb, "open Telnet");
  61.     /* Allocate a session descriptor */
  62.     if((s = newsession()) == NULLSESSION)
  63.     {
  64.       cwprintf(NULL, "%s : Incoming Telnet call from %s refused; too many sessions\r\n",
  65.       cp,psocket(&tcb->conn.remote));
  66.       sndmsg(tcb,"Call rejected; too many sessions on remote system\n");
  67.       close_tcp(tcb);
  68.       return;
  69.     }
  70.     a = inet_ntoa(tcb->conn.remote.address);
  71.     if((s->name = malloc((unsigned)strlen(a)+1)) != NULLCHAR)
  72.       strcpy(s->name,a);
  73.     s->type = TELNET;
  74.     s->parse = (void (*)())send_tel;
  75.     /* Create and initialize a Telnet protocol descriptor */
  76.     if((tn = (struct telnet *)calloc(1,sizeof(struct telnet))) == NULLTN)
  77.     {
  78.       cwprintf(NULL, "%s : Incoming Telnet call refused; no space\r\n",cp);
  79.       sndmsg(tcb,"Call rejected; no space on remote system\n");
  80.       close_tcp(tcb);
  81.       s->type = FREE;
  82.       return;
  83.     }
  84.     sprintf(buf, "Telnet from %s", psocket(&tcb->conn.remote));
  85.     tn->window = Window_Open(s, buf, term_SIXTEEN);
  86.     if (tn->window == NULL)
  87.     {
  88.       current = s;
  89.     }
  90.     else
  91.     {
  92.       vterm_setflags(tn->window->vt, VTSW_CHAT, VTSW_CHAT);
  93.     }
  94.     tn->session  = s;        /* Upward pointer */
  95.     tn->state    = TS_DATA;
  96.     s->cb.telnet = tn;       /* Downward pointer */
  97.     s->window    = tn->window;
  98.     tn->session->echo = TRUE;
  99.  
  100.     tcb->user = (char *)tn; /* Upward pointer */
  101.     tn->tcb = tcb;          /* Downward pointer */
  102.     cwprintf(NULL, "%s : Incoming Telnet session %lu from %s\r\n",
  103.     cp,(long)(s - sessions),psocket(&tcb->conn.remote));
  104.  
  105.     if (attended)
  106.     {
  107.       if (!do_banner(bnrtelnetat, "", tcb))
  108.         tprintf(tcb,"Welcome to the %s system's TELNET mode.\n",hostname);
  109.     }
  110.     else
  111.     {
  112.       if (!do_banner(bnrtelnetun, "", tcb))
  113.         tprintf(tcb,"Sorry, the %s system is UNATTENDED.\n",hostname);
  114.     }
  115.     tcb->s_upcall = (void (*)())t_state;
  116.     return;
  117.  
  118.   case CLOSED:
  119.     /* This will only happen if the connection closed before
  120.        the session was set up, e.g., if we refused it because
  121.        there were too many sessions, or if the server is being
  122.        shut down. */
  123.     if(tcb == tnet_tcb)
  124.       tnet_tcb = NULLTCB;
  125.     del_tcp(tcb);
  126.     break;
  127.   }
  128. }
  129.  
  130. /* Shut down Telnet server */
  131. int tn0(int argc, char **argv)
  132. {
  133.   struct tcb *tp;
  134.   struct connection conn;
  135.   /*
  136.    * Have to find the poxy server!
  137.    */
  138.   conn.local.address = ip_addr;
  139.   if(argc < 2)
  140.     conn.local.port = TELNET_PORT;
  141.   else
  142.     conn.local.port = atoi(argv[1]);
  143.  
  144.   /*
  145.    * Passive remote (I hope!)
  146.    */
  147.   conn.remote.address = 0;
  148.   conn.remote.port = 0;
  149.  
  150.   if (tp = lookup_tcb(&conn), tp!=NULLTCB)
  151.     close_tcp(tp);
  152.   else
  153.     cwprintf(NULL, "No such server enabled\r\n");
  154.  
  155.   return(0);
  156. }
  157.  
  158. static void sndmsg(struct tcb *tcb, char *msg)
  159. {
  160.   struct mbuf *bp;
  161.  
  162.   bp = qdata(msg,(int16)strlen(msg));
  163.   send_tcp(tcb,bp);
  164. }
  165.  
  166.