home *** CD-ROM | disk | FTP | other *** search
/ The Datafile PD-CD 3 / PDCD_3.iso / internet / tcpipsrc / h / if / NetMisc / c / session < prev    next >
Encoding:
Text File  |  1994-09-26  |  10.4 KB  |  481 lines

  1. /* Session control */
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <string.h>
  5. #include <time.h>
  6. #include "global.h"
  7. #include "config.h"
  8. #include "mbuf.h"
  9. #include "netuser.h"
  10. #include "timer.h"
  11. #include "tcp.h"
  12. #include "ax25.h"
  13. #include "lapb.h"
  14. #include "ftp.h"
  15. #include "telnet.h"
  16. #include "finger.h"
  17. #include "dns.h"
  18. #include "netrom.h"
  19. #include "nr4.h"
  20. #include "session.h"
  21. #include "cmdparse.h"
  22. #include "misc.h"
  23.  
  24. static struct session *sessptr(char *);
  25.  
  26. struct session *sessions;
  27. struct session *current;
  28. char notval[] = "Not a valid control block\r\n";
  29. char badsess[] = "Invalid session\r\n";
  30.  
  31. /* Convert a character string containing a decimal session index number
  32.  * into a pointer. If the arg is NULLCHAR, use the current default session.
  33.  * If the index is out of range or unused, return NULLSESSION.
  34.  */
  35. static struct session *sessptr(char *cp)
  36. {
  37.   register struct session *s;
  38.   unsigned int i;
  39.  
  40.   if(cp == NULLCHAR)
  41.   {
  42.     s = current;
  43.   }
  44.   else
  45.   {
  46.     if ((i = atoi(cp)) >= nsessions)
  47.       return NULLSESSION;
  48.     s = &sessions[i];
  49.   }
  50.   if(s == NULLSESSION || s->type == FREE)
  51.     return NULLSESSION;
  52.  
  53.   return s;
  54. }
  55.  
  56. /* Select and display sessions */
  57. int dosession(int argc, char **argv)
  58. {
  59.   struct session *s;
  60.   extern char *tcpstates[];
  61.   extern char *ax25states[];
  62.  
  63.   if(argc > 1)
  64.   {
  65.     if((current = sessptr(argv[1])) != NULLSESSION)
  66.     {
  67.       go(current);
  68.     }
  69.     else
  70.     {
  71.       cwprintf(NULL, "Session %s not active\r\n", argv[1]);
  72.     }
  73.     return 0;
  74.   }
  75.   cwprintf(NULL, " #       &CB Type   Rcv-Q  State        Remote socket\r\n");
  76.   for(s=sessions; s < &sessions[nsessions];s++)
  77.   {
  78.     switch(s->type)
  79.     {
  80.     case TELNET:
  81.       cwprintf(NULL, "%c%-3d%8lx Telnet  %4d  %-13s%-s:%s\r\n",
  82.       (current == s)? '*':' ',
  83.       (int)(s - sessions),
  84.       (long)s->cb.telnet->tcb,
  85.       s->cb.telnet->tcb->rcvcnt,
  86.       tcpstates[s->cb.telnet->tcb->state],
  87.       s->name,
  88.       tcp_port(s->cb.telnet->tcb->conn.remote.port));
  89.       break;
  90.     case DNSQUERY:
  91.       cwprintf(NULL, "%c%-3d%8lx DNS ?   %4d  %-13s%-s:%s\r\n",
  92.       (current == s)? '*':' ',
  93.       (int)(s - sessions),
  94.       (long)s->cb.dns->tcb,
  95.       s->cb.dns->tcb->rcvcnt,
  96.       tcpstates[s->cb.dns->tcb->state],
  97.       s->name,
  98.       tcp_port(s->cb.dns->tcb->conn.remote.port));
  99.       break;
  100.     case FTP:
  101.       cwprintf(NULL, "%c%-3d%8lx FTP     %4d  %-13s%-s:%s\r\n",
  102.       (current == s)? '*':' ',
  103.       (int)(s - sessions),
  104.       (long)s->cb.ftp->control,
  105.       s->cb.ftp->control->rcvcnt,
  106.       tcpstates[s->cb.ftp->control->state],
  107.       s->name,
  108.       tcp_port(s->cb.ftp->control->conn.remote.port));
  109.       break;
  110.     case AX25TNC:
  111.       cwprintf(NULL, "%c%-3d%8lx AX25    %4d  %-13s%-s\r\n",
  112.       (current == s)? '*':' ',
  113.       (int)(s - sessions),
  114.       (long)s->cb.ax25_cb,
  115.       len_mbuf(s->cb.ax25_cb->rxq),
  116.       ax25states[s->cb.ax25_cb->state],
  117.       s->name);
  118.       break;
  119.     case FINGER:
  120.       cwprintf(NULL, "%c%-3d%8lx Finger  %4d  %-13s%-s:%s\r\n",
  121.       (current == s)? '*':' ',
  122.       (int)(s - sessions),
  123.       (long)s->cb.finger->tcb,
  124.       s->cb.finger->tcb->rcvcnt,
  125.       tcpstates[s->cb.finger->tcb->state],
  126.       s->name,
  127.       tcp_port(s->cb.finger->tcb->conn.remote.port));
  128.       break;
  129.     case NRSESSION:
  130.       cwprintf(NULL, "%c%-3d%8lx NET/ROM %4d  %-13s%-s\r\n",
  131.       (current == s)? '*':' ',
  132.       (int)(s - sessions),
  133.       (long)s->cb.nr4_cb,
  134.       len_mbuf(s->cb.nr4_cb->rxq),
  135.       Nr4states[s->cb.nr4_cb->state],
  136.       s->name);
  137.       break;
  138.     default:
  139.       continue;
  140.     }
  141.     if(s->rfile != NULLCHAR)
  142.       cwprintf(NULL, "        Record: %s\r\n",s->rfile);
  143.     if(s->ufile != NULLCHAR)
  144.       cwprintf(NULL, "        Upload: %s\r\n",s->ufile);
  145.   }
  146.   return 0;
  147. }
  148.  
  149. /* Enter conversational mode with current session */
  150. int go(struct session *active)
  151. {
  152.   if (active == NULL)
  153.     active = current;
  154.  
  155.   if(active == NULLSESSION || active->type == FREE)
  156.     return 0;
  157.  
  158.   mode = CONV_MODE;
  159.   switch(active->type)
  160.   {
  161.   case TELNET:
  162.     cwtitle(active->window, "Telnet - %s", active->name);
  163.     if (active->cb.telnet->remote[TN_ECHO])
  164.     {
  165.       raw();  /* Re-establish raw mode if it was set */
  166.       active->raw = TRUE;
  167.     }
  168.     rcv_char(active->cb.telnet->tcb,0); /* Get any pending input */
  169.     break;
  170.   case FTP:
  171.     cwtitle(active->window, "FTP - %s", active->name);
  172.     ftpccr(active->cb.ftp->control, 0);
  173.     break;
  174.   case AX25TNC:
  175.     cwtitle(active->window, "AX25 - %s", active->name);
  176.     ax_rx(active->cb.ax25_cb, 0);
  177.     break;
  178.   case FINGER:
  179.     cwtitle(active->window, "Finger - %s", active->name);
  180.     fingcli_rcv(active->cb.finger->tcb, 0) ;
  181.     break ;
  182.   case DNSQUERY:
  183.     cwtitle(active->window, "DNS Query - %s", active->name);
  184.     fingcli_rcv(active->cb.dns->tcb, 0) ;
  185.     break ;
  186.   case NRSESSION:
  187.     cwtitle(active->window, "NET/ROM - %s", active->name);
  188.     nr4_rx(active->cb.nr4_cb,0) ;
  189.     break ;
  190.   }
  191.   return 0;
  192. }
  193.  
  194. void close_sess(struct session *s)
  195. {
  196.   if (s==current)
  197.     current = NULLSESSION;
  198.  
  199.   switch(s->type)
  200.   {
  201.   case TELNET:
  202.     close_tcp(s->cb.telnet->tcb);
  203.     break;
  204.   case DNSQUERY:
  205.     close_tcp(s->cb.dns->tcb);
  206.     break;
  207.   case FTP:
  208.     close_tcp(s->cb.ftp->control);
  209.     break;
  210.   case AX25TNC:
  211.     disc_ax25(s->cb.ax25_cb);
  212.     break;
  213.   case FINGER:
  214.     close_tcp(s->cb.finger->tcb);
  215.     break;
  216.   case NRSESSION:
  217.     disc_nr4(s->cb.nr4_cb) ;
  218.     break ;
  219.   }
  220. }
  221.  
  222. int doclose(int argc, char **argv)
  223. {
  224.   struct session *s;
  225.  
  226.   if((s = sessptr(argc > 1 ? argv[1] : NULLCHAR)) == NULLSESSION)
  227.   {
  228.     cwprintf(NULL, badsess);
  229.     return -1;
  230.   }
  231.   close_sess(s);
  232.   return 0;
  233. }
  234.  
  235. int doreset(int argc, char **argv)
  236. {
  237.   struct session *s;
  238.  
  239.   if((s = sessptr(argc > 1 ? argv[1] : NULLCHAR)) == NULLSESSION){
  240.     cwprintf(NULL, badsess);
  241.     return -1;
  242.   }
  243.   switch(s->type)
  244.   {
  245.   case TELNET:
  246.     reset_tcp(s->cb.telnet->tcb);
  247.     break;
  248.   case DNSQUERY:
  249.     reset_tcp(s->cb.dns->tcb);
  250.     break;
  251.   case FTP:
  252.     if(s->cb.ftp->data != NULLTCB)
  253.     {
  254.       reset_tcp(s->cb.ftp->data);
  255.       s->cb.ftp->data = NULLTCB;
  256.     }
  257.     reset_tcp(s->cb.ftp->control);
  258.     break;
  259.   case AX25TNC:
  260.     reset_ax25(s->cb.ax25_cb);
  261.     break;
  262.   case FINGER:
  263.     reset_tcp(s->cb.finger->tcb);
  264.     break;
  265.   case NRSESSION:
  266.     reset_nr4(s->cb.nr4_cb) ;
  267.     break ;
  268.   }
  269.   return 0;
  270. }
  271.  
  272. int dokick(int argc, char **argv)
  273. {
  274.   struct session *s;
  275.  
  276.   if((s = sessptr(argc > 1 ? argv[1] : NULLCHAR)) == NULLSESSION){
  277.     cwprintf(NULL, badsess);
  278.     return -1;
  279.   }
  280.   switch(s->type)
  281.   {
  282.   case TELNET:
  283.     if(kick_tcp(s->cb.telnet->tcb) == -1)
  284.     {
  285.       cwprintf(NULL, notval);
  286.       return 1;
  287.     }
  288.     break;
  289.   case DNSQUERY:
  290.     if(kick_tcp(s->cb.dns->tcb) == -1)
  291.     {
  292.       cwprintf(NULL, notval);
  293.       return 1;
  294.     }
  295.     break;
  296.   case FTP:
  297.     if(kick_tcp(s->cb.ftp->control) == -1)
  298.     {
  299.       cwprintf(NULL, notval);
  300.       return 1;
  301.     }
  302.     if(s->cb.ftp->data != NULLTCB)
  303.       kick_tcp(s->cb.ftp->data);
  304.     break;
  305.   case AX25TNC:
  306.     if(kick_ax25(s->cb.ax25_cb) == -1){
  307.       cwprintf(NULL, notval);
  308.       return 1;
  309.     }
  310.     return 1;
  311.   case FINGER:
  312.     if(kick_tcp(s->cb.finger->tcb) == -1)
  313.     {
  314.       cwprintf(NULL, notval);
  315.       return 1;
  316.     }
  317.     break;
  318.   case NRSESSION:
  319.     if(kick_nr4(s->cb.nr4_cb) == -1) {
  320.       cwprintf(NULL, notval) ;
  321.       return 1 ;
  322.     }
  323.     break ;
  324.   }
  325.   return 0;
  326. }
  327.  
  328. struct session *newsession(void)
  329. {
  330.   register int i;
  331.  
  332.   for (i = 0; i < nsessions; i++)
  333.   {
  334.     if (sessions[i].type == FREE)
  335.     {
  336.       sessions[i].record = NULL;
  337.       return &sessions[i];
  338.     }
  339.   }
  340.  
  341.   return NULLSESSION;
  342. }
  343.  
  344. void freesession(struct session *s)
  345. {
  346.   if(s == NULLSESSION)
  347.     return;
  348.   if(s->record != NULLFILE)
  349.   {
  350.     fclose(s->record);
  351.     s->record = NULLFILE;
  352.   }
  353.   if(s->rfile != NULLCHAR)
  354.   {
  355.     free(s->rfile);
  356.     s->rfile = NULLCHAR;
  357.   }
  358.   if(s->upload != NULLFILE)
  359.   {
  360.     fclose(s->upload);
  361.     s->upload = NULLFILE;
  362.   }
  363.   if(s->ufile != NULLCHAR)
  364.   {
  365.     free(s->ufile);
  366.     s->ufile = NULLCHAR;
  367.   }
  368.   if(s->name != NULLCHAR)
  369.   {
  370.     free(s->name);
  371.     s->name = NULLCHAR;
  372.   }
  373.   s->type = FREE;
  374. }
  375.  
  376. /* Control session recording */
  377. int dorecord(int argc, char **argv)
  378. {
  379.   if(current == NULLSESSION)
  380.   {
  381.     cwprintf(NULL, "No current session\r\n");
  382.     return 1;
  383.   }
  384.   if(argc > 1)
  385.   {
  386.     if(current->rfile != NULLCHAR)
  387.     {
  388.       fclose(current->record);
  389.       free(current->rfile);
  390.       current->record = NULLFILE;
  391.       current->rfile = NULLCHAR;
  392.     }
  393.     /* Open new record file, unless file name is "off", which means
  394.                      * disable recording
  395.                      */
  396.     if (strcmp(argv[1], "off") != 0)
  397.     {
  398.       if ((current->record = fopen(argv[1],"a")) == NULLFILE) {
  399.         cwprintf(NULL, "Can't open %s\r\n", argv[1]);
  400.       }
  401.       else
  402.           {
  403.         current->rfile = malloc((unsigned)strlen(argv[1])+1);
  404.         strcpy(current->rfile,argv[1]);
  405.       }
  406.     }
  407.   }
  408.   if(current->rfile != NULLCHAR)
  409.     cwprintf(NULL, "Recording into %s\r\n",current->rfile);
  410.   else
  411.       cwprintf(NULL, "Recording off\r\n");
  412.   return 0;
  413. }
  414.  
  415. /* Control file transmission */
  416. int doupload(int argc, char **argv)
  417. {
  418.   struct tcb *tcb;
  419.   struct ax25_cb *axp;
  420.   struct nr4cb *cb ;
  421.  
  422.   if(current == NULLSESSION){
  423.     cwprintf(NULL, "No current session\r\n");
  424.     return 1;
  425.   }
  426.   if(argc > 1){
  427.     switch(current->type){
  428.     case TELNET:
  429.       tcb = current->cb.telnet->tcb;
  430.       break;
  431.     case AX25TNC:
  432.       axp = current->cb.ax25_cb;
  433.       break;
  434.     case NRSESSION:
  435.       cb = current->cb.nr4_cb ;
  436.       break ;
  437.     case FTP:
  438.       cwprintf(NULL, "Uploading on FTP control channel not supported\r\n");
  439.       return 1;
  440.     }
  441.     if(strcmp(argv[1],"stop") == 0 && current->upload != NULLFILE){
  442.       /* Abort upload */
  443.       fclose(current->upload);
  444.       current->upload = NULLFILE;
  445.       if(current->ufile != NULLCHAR){
  446.         free(current->ufile);
  447.         current->ufile = NULLCHAR;
  448.       }
  449.     }
  450.     /* Open upload file */
  451.     if((current->upload = fopen(argv[1],"r")) == NULLFILE){
  452.       cwprintf(NULL, "Can't read %s\r\n",argv[1]);
  453.       return 1;
  454.     }
  455.     current->ufile = malloc((unsigned)strlen(argv[1])+1);
  456.     strcpy(current->ufile,argv[1]);
  457.     /* All set, kick transmit upcall to get things rolling */
  458.     switch(current->type){
  459.     case AX25TNC:
  460.       (*axp->t_upcall)(axp,axp->paclen * axp->maxframe);
  461.       break;
  462.     case NRSESSION:
  463.       (*cb->t_upcall)(cb, NR4MAXINFO) ;
  464.       break ;
  465.     case TELNET:
  466.       (*tcb->t_upcall)(tcb,tcb->snd.wnd - tcb->sndcnt);
  467.       break;
  468.     }
  469.   }
  470.   if(current->ufile != NULLCHAR)
  471.     cwprintf(NULL, "Uploading %s\r\n",current->ufile);
  472.   else
  473.       cwprintf(NULL, "Uploading off\r\n");
  474.   return 0;
  475. }
  476.  
  477. int session_number(struct session *s)
  478. {
  479.   return (int)(s - sessions);
  480. }
  481.