home *** CD-ROM | disk | FTP | other *** search
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include "global.h"
- #include "mbuf.h"
- #include "domain.h"
- #include "timer.h"
- #include "icmp.h"
- #include "netuser.h"
- #include "tcp.h"
- #include "telnet.h"
- #include "session.h"
- #include "misc.h"
- #include "Terminal.h"
- #include "vterm.h"
-
- extern cwnputs(Terminal *Window, char *s, int n);
-
- static void free_telnet(struct telnet *);
- static void willopt(struct telnet *, char);
- static void wontopt(struct telnet *, char);
- static void doopt(struct telnet *, char);
- static void dontopt(struct telnet *, char);
- static void answer(struct telnet *, int, int);
-
- #define CTLZ 26
-
- extern char nospace[];
- extern char badhost[];
- int refuse_echo = 0;
- int unix_line_mode = 0; /* if true turn <cr> to <nl> when in line mode */
-
- #ifdef DEBUG
- char *t_options[] = {
- "Transmit Binary",
- "Echo",
- "",
- "Suppress Go Ahead",
- "",
- "Status",
- "Timing Mark"
- };
- #endif
-
- /* Execute user telnet command */
- int dotelnet(int argc, char **argv)
- {
- int used_args;
- struct session *s;
- struct telnet *tn;
- struct tcb *tcb;
- struct socket lsocket,fsocket;
-
- lsocket.address = ip_addr;
- lsocket.port = lport++;
- if((fsocket.address = resolve(argv[1])) == 0)
- {
- cwprintf(NULL, badhost,argv[1]);
- return 1;
- }
-
- if (argc<3)
- {
- fsocket.port = TELNET_PORT;
- used_args = 2;
- }
- else
- {
- if (*argv[2]!='\\')
- {
- fsocket.port = atoi(argv[2]);
- used_args = 3;
- }
- else
- {
- fsocket.port = TELNET_PORT;
- used_args = 2;
- }
- }
- /* Allocate a session descriptor */
- if((s = newsession()) == NULLSESSION)
- {
- cwprintf(NULL, "Too many sessions\r\n");
- return 1;
- }
- if((s->name = malloc((unsigned)strlen(argv[1]) + 1)) != NULLCHAR)
- strcpy(s->name, argv[1]);
- s->type = TELNET;
- if ((refuse_echo == 0) && (unix_line_mode != 0))
- {
- s->parse = (void (*)())unix_send_tel;
- }
- else
- {
- s->parse = (void (*)())send_tel;
- }
-
- /* Create and initialize a Telnet protocol descriptor */
- if ((tn = (struct telnet *)calloc(1, sizeof(struct telnet))) == NULLTN)
- {
- cwprintf(NULL, nospace);
- s->type = FREE;
- return 1;
- }
- tn->window = Window_Open(s, "Telnet", term_SIXTEEN | term_CARET);
- vterm_parse(tn->window->vt, argc-used_args, &argv[used_args]);
- current = s;
- tn->session = s; /* Upward pointer */
- tn->state = TS_DATA;
- s->cb.telnet = tn; /* Downward pointer */
- s->window = tn->window;
- tn->session->echo = TRUE;
-
- tcb = open_tcp(&lsocket, &fsocket, TCP_ACTIVE, 0,
- (void (*)())rcv_char, (void(*)())tn_tx, (void(*)())t_state, 0, (char *)tn);
-
- tn->tcb = tcb; /* Downward pointer */
- go(s);
- return 0;
- }
-
- /* Process typed characters */
- void unix_send_tel(struct session *window, char *buf, int16 n)
- {
- int i;
-
- for (i=0; (i<n) && (buf[i] != '\r'); i++)
- ;
- if (buf[i] == '\r')
- {
- buf[i] = '\n';
- n = i+1;
- }
- send_tel(window, buf, n);
- }
-
- void send_tel(struct session *active, char *buf, int16 n)
- {
- extern int ttyflow;
- struct mbuf *bp;
-
- if (active == NULL)
- active = current;
-
- if (active->window == NULL && (ttyflow == 0 || current == NULLSESSION || current->cb.telnet == NULLTN || current->cb.telnet->tcb == NULLTCB))
- return;
- /* If we're doing our own echoing and recording is enabled, record it */
- if (!active->cb.telnet->remote[TN_ECHO] && active->record != NULLFILE)
- fwrite(buf, 1, n, active->record);
- bp = qdata(buf, n);
- send_tcp(active->cb.telnet->tcb, bp);
- }
-
- /* Process incoming TELNET characters */
- void tel_input(register struct telnet *tn, struct mbuf *bp)
- {
- char c;
- FILE *record;
- register char *s;
- char *line;
-
- if ((line = s = malloc(len_mbuf(bp) + 1)) == NULL)
- {
- cwprintf(tn->window, "Out of memory in TELNET\r\n");
- free_p(bp);
- return;
- }
-
- /* Optimization for very common special case -- no special chars */
- if(tn->state == TS_DATA)
- {
- while(bp != NULLBUF && memchr(bp->data,IAC,bp->cnt) == NULLCHAR)
- {
- while(bp->cnt-- != 0)
- {
- *s++ = *bp->data;
- bp->data++;
- }
- bp = free_mbuf(bp);
- }
- }
- while (pullone(&bp,&c) == 1)
- {
- switch(tn->state)
- {
- case TS_DATA:
- if(uchar(c) == IAC)
- {
- tn->state = TS_IAC;
- }
- else
- {
- if(!tn->remote[TN_TRANSMIT_BINARY] && c != 0)
- *s++ = c & 0x7f;
- }
- break;
- case TS_IAC:
- switch(uchar(c))
- {
- case WILL:
- tn->state = TS_WILL;
- break;
- case WONT:
- tn->state = TS_WONT;
- break;
- case DO:
- tn->state = TS_DO;
- break;
- case DONT:
- tn->state = TS_DONT;
- break;
- case IAC:
- cwputchar(tn->window, c);
- tn->state = TS_DATA;
- break;
- default:
- tn->state = TS_DATA;
- break;
- }
- break;
- case TS_WILL:
- willopt(tn,c);
- tn->state = TS_DATA;
- break;
- case TS_WONT:
- wontopt(tn,c);
- tn->state = TS_DATA;
- break;
- case TS_DO:
- doopt(tn,c);
- tn->state = TS_DATA;
- break;
- case TS_DONT:
- dontopt(tn,c);
- tn->state = TS_DATA;
- break;
- }
- }
- if ((record = tn->session->record) != NULLFILE)
- {
- fwrite(line, 1, s - line, record);
- fflush(record);
- }
- *s = '\0';
- cwnputs(tn->window, line, s - line);
- free(line);
- }
-
- /* Telnet receiver upcall routine */
- void rcv_char(register struct tcb *tcb, int16 cnt)
- {
- extern int ttyflow;
- struct mbuf *bp;
- struct telnet *tn;
- FILE *record;
-
- if((tn = (struct telnet *)tcb->user) == NULLTN)
- {
- /* Unknown connection; ignore it */
- return;
- }
- /* Hold output if we're not the current session */
- if (tn->window == NULL && (mode != CONV_MODE || current == NULLSESSION || ttyflow == 0 || current->type != TELNET || current->cb.telnet != tn))
- return;
-
- if(recv_tcp(tcb, &bp, cnt) > 0)
- tel_input(tn, bp);
-
- if((record = tn->session->record) != NULLFILE)
- fflush(record);
- }
-
- /* Handle transmit upcalls. Used only for file uploading */
- void tn_tx(struct tcb *tcb, int16 cnt)
- {
- struct telnet *tn;
- struct session *s;
- struct mbuf *bp;
- int size;
-
- if((tn = (struct telnet *)tcb->user) == NULLTN
- || (s = tn->session) == NULLSESSION
- || s->upload == NULLFILE)
- return;
- if((bp = alloc_mbuf(cnt)) == NULLBUF)
- return;
- if((size = fread(bp->data,1,cnt,s->upload)) > 0)
- {
- bp->cnt = (int16)size;
- send_tcp(tcb,bp);
- }
- else
- {
- free_p(bp);
- }
- if(size != cnt)
- {
- /* Error or end-of-file */
- fclose(s->upload);
- s->upload = NULLFILE;
- free(s->ufile);
- s->ufile = NULLCHAR;
- }
- }
-
- /* State change upcall routine */
- void t_state(register struct tcb *tcb, char old, char new)
- {
- extern int ttyflow;
- struct telnet *tn;
- char notify = 0;
- extern char *tcpstates[];
- extern char *reasons[];
- extern char *unreach[];
- extern char *exceed[];
-
- old = old;
-
- /* Can't add a check for unknown connection here, it would loop
- on a close upcall! We're just careful later on. */
- tn = (struct telnet *)tcb->user;
-
- if(tn->window != NULL || (ttyflow && current != NULLSESSION && current->type == TELNET && current->cb.telnet == tn))
- notify = 1;
-
- switch(new)
- {
- case CLOSE_WAIT:
- if(notify)
- cwprintf(tn->window, "%s\r\n",tcpstates[new]);
- close_tcp(tcb);
- break;
- case CLOSED: /* court adjourned */
- if(notify)
- {
- cwprintf(tn->window, "%s (%s",tcpstates[new],reasons[tcb->reason]);
- if(tcb->reason == NETWORK)
- {
- switch(tcb->type)
- {
- case DEST_UNREACH:
- cwprintf(tn->window, ": %s unreachable",unreach[tcb->code]);
- break;
- case TIME_EXCEED:
- cwprintf(tn->window, ": %s time exceeded",exceed[tcb->code]);
- break;
- }
- }
- cwprintf(tn->window, ")\r\n");
- cmdmode();
- }
- del_tcp(tcb);
- if(tn != NULLTN)
- free_telnet(tn);
- break;
- default:
- if(notify)
- cwprintf(tn->window, "%s\r\n",tcpstates[new]);
- break;
- }
- }
- /* Delete telnet structure */
- static void free_telnet(struct telnet *tn)
- {
- if(tn->session != NULLSESSION)
- freesession(tn->session);
- if (tn->window)
- {
- tn->window->Attr = ATTR_REVERSE;
- tn->window->Flags.flags.dont_destroy = FALSE;
- Window_CloseDown(tn->window);
- }
-
- if(tn != NULLTN)
- free((char *)tn);
- }
-
- /* The guts of the actual Telnet protocol: negotiating options */
- static void willopt(struct telnet *tn, char opt)
- {
- int ack;
-
- #ifdef DEBUG
- cwprintf(tn->window, "recv: will ");
- if(uchar(opt) <= NOPTIONS)
- cwprintf(tn->window, "%s\r\n",t_options[opt]);
- else
- cwprintf(tn->window, "%u\r\n",opt);
- #endif
-
- switch(uchar(opt))
- {
- case TN_TRANSMIT_BINARY:
- case TN_ECHO:
- case TN_SUPPRESS_GA:
- if(tn->remote[uchar(opt)] == 1)
- return; /* Already set, ignore to prevent loop */
- if(uchar(opt) == TN_ECHO)
- {
- if(refuse_echo)
- {
- /* User doesn't want to accept */
- ack = DONT;
- break;
- }
- else
- {
- raw(); /* Put tty into raw mode */
- vterm_setflags(tn->window->vt,
- /* Clear */ VTSW_CHAT|VTSW_LINE|VTSW_NEWLINE|VTSW_WRAP|VTSW_ECHO,
- /* Set */ 0 );
-
- tn->session->raw = TRUE;
- }
- }
- tn->remote[uchar(opt)] = 1;
- ack = DO;
- break;
- default:
- ack = DONT; /* We don't know what he's offering; refuse */
- }
- answer(tn,ack,opt);
- }
- static void wontopt(struct telnet *tn, char opt)
- {
- #ifdef DEBUG
- cwprintf(tn->window, "recv: wont ");
- if(uchar(opt) <= NOPTIONS)
- cwprintf(tn->window, "%s\r\n",t_options[uchar(opt)]);
- else
- cwprintf(tn->window, "%u\r\n",uchar(opt));
- #endif
- if(uchar(opt) <= NOPTIONS)
- {
- if(tn->remote[uchar(opt)] == 0)
- return; /* Already clear, ignore to prevent loop */
- tn->remote[uchar(opt)] = 0;
- if(uchar(opt) == TN_ECHO)
- {
- cooked(); /* Put tty into cooked mode (old code) */
- tn->session->raw = FALSE;
- vterm_setflags(tn->window->vt,
- /* Clear */ VTSW_CHAT|VTSW_LINE|VTSW_NEWLINE|VTSW_WRAP|VTSW_ECHO,
- /* Set */ VTSW_LINE|VTSW_NEWLINE|VTSW_WRAP|VTSW_ECHO );
- }
- }
- answer(tn,DONT,opt); /* Must always accept */
- }
- static void doopt(struct telnet *tn, char opt)
- {
- int ack;
-
- #ifdef DEBUG
- cwprintf(tn->window, "recv: do ");
- if(uchar(opt) <= NOPTIONS)
- cwprintf(tn->window, "%s\r\n",t_options[uchar(opt)]);
- else
- cwprintf(tn->window, "%u\r\n",uchar(opt));
- #endif
- switch(uchar(opt))
- {
- #ifdef FUTURE /* Use when local options are implemented */
- if(tn->local[uchar(opt)] == 1)
- return; /* Already set, ignore to prevent loop */
- tn->local[uchar(opt)] = 1;
- ack = WILL;
- break;
- #endif
- default:
- ack = WONT; /* Don't know what it is */
- }
- answer(tn,ack,opt);
- }
- static void dontopt(struct telnet *tn, char opt)
- {
- #ifdef DEBUG
- cwprintf(tn->window, "recv: dont ");
- if(uchar(opt) <= NOPTIONS)
- cwprintf(tn->window, "%s\r\n",t_options[uchar(opt)]);
- else
- cwprintf(tn->window, "%u\r\n",uchar(opt));
- #endif
- if(uchar(opt) <= NOPTIONS)
- {
- if(tn->local[uchar(opt)] == 0)
- {
- /* Already clear, ignore to prevent loop */
- return;
- }
- tn->local[uchar(opt)] = 0;
- }
- answer(tn,WONT,opt);
- }
- static void answer(struct telnet *tn, int r1, int r2)
- {
- struct mbuf *bp;
- char s[3];
-
- #ifdef DEBUG
- switch(r1)
- {
- case WILL:
- cwprintf(tn->window, "sent: will ");
- break;
- case WONT:
- cwprintf(tn->window, "sent: wont ");
- break;
- case DO:
- cwprintf(tn->window, "sent: do ");
- break;
- case DONT:
- cwprintf(tn->window, "sent: dont ");
- break;
- }
- if(r2 <= 6)
- cwprintf(tn->window, "%s\r\n",t_options[r2]);
- else
- cwprintf(tn->window, "%u\r\n",r2);
- #endif
-
- s[0] = IAC;
- s[1] = r1;
- s[2] = r2;
- bp = qdata(s,(int16)3);
- send_tcp(tn->tcb,bp);
- }
-