home *** CD-ROM | disk | FTP | other *** search
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include <time.h>
- #include "global.h"
- #include "mbuf.h"
- #include "timer.h"
- #include "icmp.h"
- #include "netuser.h"
- #include "tcp.h"
- #include "telnet.h"
- #include "session.h"
- #include "misc.h"
- #include "vterm.h"
-
- extern char bnrtelnetat[];
- extern char bnrtelnetun[];
- extern int do_banner(char *file, char *pfx, struct tcb *c);
-
- static void tnet_state(struct tcb *, char, char);
- static void sndmsg(struct tcb *, char *);
-
- struct tcb *tnet_tcb;
- int tn1(int argc, char **argv)
- {
- struct socket lsocket;
- extern int32 ip_addr;
-
- /* Incoming Telnet */
- lsocket.address = ip_addr;
- if(argc < 2)
- lsocket.port = TELNET_PORT;
- else
- lsocket.port = atoi(argv[1]);
- tnet_tcb = open_tcp(&lsocket, NULLSOCK, TCP_SERVER, 0, (void(*)())rcv_char, NULLVFP, (void(*)())tnet_state, 0, (char *)NULL);
- return(0);
- }
- /* Handle incoming Telnet connect requests by creating a Telnet session,
- then change upcall vector so it behaves like an ordinary Telnet session. */
- static void tnet_state(struct tcb *tcb, char old, char new)
- {
- struct telnet *tn;
- struct session *s;
- char *a, buf[80];
- extern char hostname[];
- extern int attended;
- char *cp;
- time_t t;
-
- old = old;
-
- time(&t);
- cp = ctime(&t);
- rip(cp);
-
- switch(new)
- {
- case ESTABLISHED:
- set_server_vars("telnet",tcb);
- log_event(tcb, "open Telnet");
- /* Allocate a session descriptor */
- if((s = newsession()) == NULLSESSION)
- {
- cwprintf(NULL, "%s : Incoming Telnet call from %s refused; too many sessions\r\n",
- cp,psocket(&tcb->conn.remote));
- sndmsg(tcb,"Call rejected; too many sessions on remote system\n");
- close_tcp(tcb);
- return;
- }
- a = inet_ntoa(tcb->conn.remote.address);
- if((s->name = malloc((unsigned)strlen(a)+1)) != NULLCHAR)
- strcpy(s->name,a);
- s->type = TELNET;
- s->parse = (void (*)())send_tel;
- /* Create and initialize a Telnet protocol descriptor */
- if((tn = (struct telnet *)calloc(1,sizeof(struct telnet))) == NULLTN)
- {
- cwprintf(NULL, "%s : Incoming Telnet call refused; no space\r\n",cp);
- sndmsg(tcb,"Call rejected; no space on remote system\n");
- close_tcp(tcb);
- s->type = FREE;
- return;
- }
- sprintf(buf, "Telnet from %s", psocket(&tcb->conn.remote));
- tn->window = Window_Open(s, buf, term_SIXTEEN);
- if (tn->window == NULL)
- {
- current = s;
- }
- else
- {
- vterm_setflags(tn->window->vt, VTSW_CHAT, VTSW_CHAT);
- }
- tn->session = s; /* Upward pointer */
- tn->state = TS_DATA;
- s->cb.telnet = tn; /* Downward pointer */
- s->window = tn->window;
- tn->session->echo = TRUE;
-
- tcb->user = (char *)tn; /* Upward pointer */
- tn->tcb = tcb; /* Downward pointer */
- cwprintf(NULL, "%s : Incoming Telnet session %lu from %s\r\n",
- cp,(long)(s - sessions),psocket(&tcb->conn.remote));
-
- if (attended)
- {
- if (!do_banner(bnrtelnetat, "", tcb))
- tprintf(tcb,"Welcome to the %s system's TELNET mode.\n",hostname);
- }
- else
- {
- if (!do_banner(bnrtelnetun, "", tcb))
- tprintf(tcb,"Sorry, the %s system is UNATTENDED.\n",hostname);
- }
- tcb->s_upcall = (void (*)())t_state;
- return;
-
- case CLOSED:
- /* This will only happen if the connection closed before
- the session was set up, e.g., if we refused it because
- there were too many sessions, or if the server is being
- shut down. */
- if(tcb == tnet_tcb)
- tnet_tcb = NULLTCB;
- del_tcp(tcb);
- break;
- }
- }
-
- /* Shut down Telnet server */
- int tn0(int argc, char **argv)
- {
- struct tcb *tp;
- struct connection conn;
- /*
- * Have to find the poxy server!
- */
- conn.local.address = ip_addr;
- if(argc < 2)
- conn.local.port = TELNET_PORT;
- else
- conn.local.port = atoi(argv[1]);
-
- /*
- * Passive remote (I hope!)
- */
- conn.remote.address = 0;
- conn.remote.port = 0;
-
- if (tp = lookup_tcb(&conn), tp!=NULLTCB)
- close_tcp(tp);
- else
- cwprintf(NULL, "No such server enabled\r\n");
-
- return(0);
- }
-
- static void sndmsg(struct tcb *tcb, char *msg)
- {
- struct mbuf *bp;
-
- bp = qdata(msg,(int16)strlen(msg));
- send_tcp(tcb,bp);
- }
-
-