home *** CD-ROM | disk | FTP | other *** search
- /* Session control */
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include <time.h>
- #include "global.h"
- #include "config.h"
- #include "mbuf.h"
- #include "netuser.h"
- #include "timer.h"
- #include "tcp.h"
- #include "ax25.h"
- #include "lapb.h"
- #include "ftp.h"
- #include "telnet.h"
- #include "finger.h"
- #include "dns.h"
- #include "netrom.h"
- #include "nr4.h"
- #include "session.h"
- #include "cmdparse.h"
- #include "misc.h"
-
- static struct session *sessptr(char *);
-
- struct session *sessions;
- struct session *current;
- char notval[] = "Not a valid control block\r\n";
- char badsess[] = "Invalid session\r\n";
-
- /* Convert a character string containing a decimal session index number
- * into a pointer. If the arg is NULLCHAR, use the current default session.
- * If the index is out of range or unused, return NULLSESSION.
- */
- static struct session *sessptr(char *cp)
- {
- register struct session *s;
- unsigned int i;
-
- if(cp == NULLCHAR)
- {
- s = current;
- }
- else
- {
- if ((i = atoi(cp)) >= nsessions)
- return NULLSESSION;
- s = &sessions[i];
- }
- if(s == NULLSESSION || s->type == FREE)
- return NULLSESSION;
-
- return s;
- }
-
- /* Select and display sessions */
- int dosession(int argc, char **argv)
- {
- struct session *s;
- extern char *tcpstates[];
- extern char *ax25states[];
-
- if(argc > 1)
- {
- if((current = sessptr(argv[1])) != NULLSESSION)
- {
- go(current);
- }
- else
- {
- cwprintf(NULL, "Session %s not active\r\n", argv[1]);
- }
- return 0;
- }
- cwprintf(NULL, " # &CB Type Rcv-Q State Remote socket\r\n");
- for(s=sessions; s < &sessions[nsessions];s++)
- {
- switch(s->type)
- {
- case TELNET:
- cwprintf(NULL, "%c%-3d%8lx Telnet %4d %-13s%-s:%s\r\n",
- (current == s)? '*':' ',
- (int)(s - sessions),
- (long)s->cb.telnet->tcb,
- s->cb.telnet->tcb->rcvcnt,
- tcpstates[s->cb.telnet->tcb->state],
- s->name,
- tcp_port(s->cb.telnet->tcb->conn.remote.port));
- break;
- case DNSQUERY:
- cwprintf(NULL, "%c%-3d%8lx DNS ? %4d %-13s%-s:%s\r\n",
- (current == s)? '*':' ',
- (int)(s - sessions),
- (long)s->cb.dns->tcb,
- s->cb.dns->tcb->rcvcnt,
- tcpstates[s->cb.dns->tcb->state],
- s->name,
- tcp_port(s->cb.dns->tcb->conn.remote.port));
- break;
- case FTP:
- cwprintf(NULL, "%c%-3d%8lx FTP %4d %-13s%-s:%s\r\n",
- (current == s)? '*':' ',
- (int)(s - sessions),
- (long)s->cb.ftp->control,
- s->cb.ftp->control->rcvcnt,
- tcpstates[s->cb.ftp->control->state],
- s->name,
- tcp_port(s->cb.ftp->control->conn.remote.port));
- break;
- case AX25TNC:
- cwprintf(NULL, "%c%-3d%8lx AX25 %4d %-13s%-s\r\n",
- (current == s)? '*':' ',
- (int)(s - sessions),
- (long)s->cb.ax25_cb,
- len_mbuf(s->cb.ax25_cb->rxq),
- ax25states[s->cb.ax25_cb->state],
- s->name);
- break;
- case FINGER:
- cwprintf(NULL, "%c%-3d%8lx Finger %4d %-13s%-s:%s\r\n",
- (current == s)? '*':' ',
- (int)(s - sessions),
- (long)s->cb.finger->tcb,
- s->cb.finger->tcb->rcvcnt,
- tcpstates[s->cb.finger->tcb->state],
- s->name,
- tcp_port(s->cb.finger->tcb->conn.remote.port));
- break;
- case NRSESSION:
- cwprintf(NULL, "%c%-3d%8lx NET/ROM %4d %-13s%-s\r\n",
- (current == s)? '*':' ',
- (int)(s - sessions),
- (long)s->cb.nr4_cb,
- len_mbuf(s->cb.nr4_cb->rxq),
- Nr4states[s->cb.nr4_cb->state],
- s->name);
- break;
- default:
- continue;
- }
- if(s->rfile != NULLCHAR)
- cwprintf(NULL, " Record: %s\r\n",s->rfile);
- if(s->ufile != NULLCHAR)
- cwprintf(NULL, " Upload: %s\r\n",s->ufile);
- }
- return 0;
- }
-
- /* Enter conversational mode with current session */
- int go(struct session *active)
- {
- if (active == NULL)
- active = current;
-
- if(active == NULLSESSION || active->type == FREE)
- return 0;
-
- mode = CONV_MODE;
- switch(active->type)
- {
- case TELNET:
- cwtitle(active->window, "Telnet - %s", active->name);
- if (active->cb.telnet->remote[TN_ECHO])
- {
- raw(); /* Re-establish raw mode if it was set */
- active->raw = TRUE;
- }
- rcv_char(active->cb.telnet->tcb,0); /* Get any pending input */
- break;
- case FTP:
- cwtitle(active->window, "FTP - %s", active->name);
- ftpccr(active->cb.ftp->control, 0);
- break;
- case AX25TNC:
- cwtitle(active->window, "AX25 - %s", active->name);
- ax_rx(active->cb.ax25_cb, 0);
- break;
- case FINGER:
- cwtitle(active->window, "Finger - %s", active->name);
- fingcli_rcv(active->cb.finger->tcb, 0) ;
- break ;
- case DNSQUERY:
- cwtitle(active->window, "DNS Query - %s", active->name);
- fingcli_rcv(active->cb.dns->tcb, 0) ;
- break ;
- case NRSESSION:
- cwtitle(active->window, "NET/ROM - %s", active->name);
- nr4_rx(active->cb.nr4_cb,0) ;
- break ;
- }
- return 0;
- }
-
- void close_sess(struct session *s)
- {
- if (s==current)
- current = NULLSESSION;
-
- switch(s->type)
- {
- case TELNET:
- close_tcp(s->cb.telnet->tcb);
- break;
- case DNSQUERY:
- close_tcp(s->cb.dns->tcb);
- break;
- case FTP:
- close_tcp(s->cb.ftp->control);
- break;
- case AX25TNC:
- disc_ax25(s->cb.ax25_cb);
- break;
- case FINGER:
- close_tcp(s->cb.finger->tcb);
- break;
- case NRSESSION:
- disc_nr4(s->cb.nr4_cb) ;
- break ;
- }
- }
-
- int doclose(int argc, char **argv)
- {
- struct session *s;
-
- if((s = sessptr(argc > 1 ? argv[1] : NULLCHAR)) == NULLSESSION)
- {
- cwprintf(NULL, badsess);
- return -1;
- }
- close_sess(s);
- return 0;
- }
-
- int doreset(int argc, char **argv)
- {
- struct session *s;
-
- if((s = sessptr(argc > 1 ? argv[1] : NULLCHAR)) == NULLSESSION){
- cwprintf(NULL, badsess);
- return -1;
- }
- switch(s->type)
- {
- case TELNET:
- reset_tcp(s->cb.telnet->tcb);
- break;
- case DNSQUERY:
- reset_tcp(s->cb.dns->tcb);
- break;
- case FTP:
- if(s->cb.ftp->data != NULLTCB)
- {
- reset_tcp(s->cb.ftp->data);
- s->cb.ftp->data = NULLTCB;
- }
- reset_tcp(s->cb.ftp->control);
- break;
- case AX25TNC:
- reset_ax25(s->cb.ax25_cb);
- break;
- case FINGER:
- reset_tcp(s->cb.finger->tcb);
- break;
- case NRSESSION:
- reset_nr4(s->cb.nr4_cb) ;
- break ;
- }
- return 0;
- }
-
- int dokick(int argc, char **argv)
- {
- struct session *s;
-
- if((s = sessptr(argc > 1 ? argv[1] : NULLCHAR)) == NULLSESSION){
- cwprintf(NULL, badsess);
- return -1;
- }
- switch(s->type)
- {
- case TELNET:
- if(kick_tcp(s->cb.telnet->tcb) == -1)
- {
- cwprintf(NULL, notval);
- return 1;
- }
- break;
- case DNSQUERY:
- if(kick_tcp(s->cb.dns->tcb) == -1)
- {
- cwprintf(NULL, notval);
- return 1;
- }
- break;
- case FTP:
- if(kick_tcp(s->cb.ftp->control) == -1)
- {
- cwprintf(NULL, notval);
- return 1;
- }
- if(s->cb.ftp->data != NULLTCB)
- kick_tcp(s->cb.ftp->data);
- break;
- case AX25TNC:
- if(kick_ax25(s->cb.ax25_cb) == -1){
- cwprintf(NULL, notval);
- return 1;
- }
- return 1;
- case FINGER:
- if(kick_tcp(s->cb.finger->tcb) == -1)
- {
- cwprintf(NULL, notval);
- return 1;
- }
- break;
- case NRSESSION:
- if(kick_nr4(s->cb.nr4_cb) == -1) {
- cwprintf(NULL, notval) ;
- return 1 ;
- }
- break ;
- }
- return 0;
- }
-
- struct session *newsession(void)
- {
- register int i;
-
- for (i = 0; i < nsessions; i++)
- {
- if (sessions[i].type == FREE)
- {
- sessions[i].record = NULL;
- return &sessions[i];
- }
- }
-
- return NULLSESSION;
- }
-
- void freesession(struct session *s)
- {
- if(s == NULLSESSION)
- return;
- if(s->record != NULLFILE)
- {
- fclose(s->record);
- s->record = NULLFILE;
- }
- if(s->rfile != NULLCHAR)
- {
- free(s->rfile);
- s->rfile = NULLCHAR;
- }
- if(s->upload != NULLFILE)
- {
- fclose(s->upload);
- s->upload = NULLFILE;
- }
- if(s->ufile != NULLCHAR)
- {
- free(s->ufile);
- s->ufile = NULLCHAR;
- }
- if(s->name != NULLCHAR)
- {
- free(s->name);
- s->name = NULLCHAR;
- }
- s->type = FREE;
- }
-
- /* Control session recording */
- int dorecord(int argc, char **argv)
- {
- if(current == NULLSESSION)
- {
- cwprintf(NULL, "No current session\r\n");
- return 1;
- }
- if(argc > 1)
- {
- if(current->rfile != NULLCHAR)
- {
- fclose(current->record);
- free(current->rfile);
- current->record = NULLFILE;
- current->rfile = NULLCHAR;
- }
- /* Open new record file, unless file name is "off", which means
- * disable recording
- */
- if (strcmp(argv[1], "off") != 0)
- {
- if ((current->record = fopen(argv[1],"a")) == NULLFILE) {
- cwprintf(NULL, "Can't open %s\r\n", argv[1]);
- }
- else
- {
- current->rfile = malloc((unsigned)strlen(argv[1])+1);
- strcpy(current->rfile,argv[1]);
- }
- }
- }
- if(current->rfile != NULLCHAR)
- cwprintf(NULL, "Recording into %s\r\n",current->rfile);
- else
- cwprintf(NULL, "Recording off\r\n");
- return 0;
- }
-
- /* Control file transmission */
- int doupload(int argc, char **argv)
- {
- struct tcb *tcb;
- struct ax25_cb *axp;
- struct nr4cb *cb ;
-
- if(current == NULLSESSION){
- cwprintf(NULL, "No current session\r\n");
- return 1;
- }
- if(argc > 1){
- switch(current->type){
- case TELNET:
- tcb = current->cb.telnet->tcb;
- break;
- case AX25TNC:
- axp = current->cb.ax25_cb;
- break;
- case NRSESSION:
- cb = current->cb.nr4_cb ;
- break ;
- case FTP:
- cwprintf(NULL, "Uploading on FTP control channel not supported\r\n");
- return 1;
- }
- if(strcmp(argv[1],"stop") == 0 && current->upload != NULLFILE){
- /* Abort upload */
- fclose(current->upload);
- current->upload = NULLFILE;
- if(current->ufile != NULLCHAR){
- free(current->ufile);
- current->ufile = NULLCHAR;
- }
- }
- /* Open upload file */
- if((current->upload = fopen(argv[1],"r")) == NULLFILE){
- cwprintf(NULL, "Can't read %s\r\n",argv[1]);
- return 1;
- }
- current->ufile = malloc((unsigned)strlen(argv[1])+1);
- strcpy(current->ufile,argv[1]);
- /* All set, kick transmit upcall to get things rolling */
- switch(current->type){
- case AX25TNC:
- (*axp->t_upcall)(axp,axp->paclen * axp->maxframe);
- break;
- case NRSESSION:
- (*cb->t_upcall)(cb, NR4MAXINFO) ;
- break ;
- case TELNET:
- (*tcb->t_upcall)(tcb,tcb->snd.wnd - tcb->sndcnt);
- break;
- }
- }
- if(current->ufile != NULLCHAR)
- cwprintf(NULL, "Uploading %s\r\n",current->ufile);
- else
- cwprintf(NULL, "Uploading off\r\n");
- return 0;
- }
-
- int session_number(struct session *s)
- {
- return (int)(s - sessions);
- }
-