home *** CD-ROM | disk | FTP | other *** search
/ Columbia Kermit / kermit.zip / old / ckermit5a188 / ckcnet.c < prev    next >
C/C++ Source or Header  |  2020-01-01  |  48KB  |  1,704 lines

  1. char *cknetv = "Network support, 5A(016) 12 Dec 92";
  2.  
  3. /*  C K C N E T  --  Network support  */
  4. /*
  5.   Authors:
  6.  
  7.   Frank da Cruz (fdc@columbia.edu, FDCCU@CUVMA.BITNET),
  8.     Columbia University Center for Computing Activities.
  9.   netopen() routine for TCP/IP originally by Ken Yap, Rochester University
  10.     (ken@cs.rochester.edu) (no longer at that address).
  11.   Missing pieces for Excelan sockets library from William Bader, Moravian
  12.     College <bader@moravian.edu>.
  13.   TELNET protocol by Frank da Cruz.
  14.   TGV MultiNet code by Frank da Cruz.
  15.   MultiNet code adapted to WIN/TCP by Ray Hunter of TWG.
  16.   MultiNet code adapted to DEC TCP/IP by Lee Tibbert of DEC and Frank da Cruz.
  17.   SunLink X.25 support by Marcello Frutig, Catholic University,
  18.     Rio de Janeiro, Brazil (frutig@rnp.impa.br) with fixes from
  19.     Stefaan Eeckels, Eurokom, Luxembourg.
  20.   Other contributions as indicated below.
  21.  
  22.   Copyright (C) 1985, 1992, Trustees of Columbia University in the City of New
  23.   York.  Permission is granted to any individual or institution to use this
  24.   software as long as it is not sold for profit.  This copyright notice must be
  25.   retained.  This software may not be included in commercial products without
  26.   written permission of Columbia University.
  27. */
  28.  
  29. #include "ckcdeb.h"
  30. #include "ckcker.h"
  31. #ifdef I386IX                /* Has to come before ckcnet.h in */
  32. #include <errno.h>            /* this version, but after in others */
  33. #endif /* I386IX */
  34. #include "ckcnet.h"
  35.  
  36. #ifdef NETCONN
  37. /* Don't need these if there is no network support. */
  38.  
  39. #ifdef WINTCP
  40.  
  41. #include <errno.h>
  42. #include <setjmp.h>
  43. #include <signal.h>
  44. /*
  45.  * The WIN/TCP code path is the same as that for Multinet.  Only the routine
  46.  * names have changed ...
  47.  */
  48. #define socket_errno     errno
  49. #define socket_read     netread
  50. #define socket_ioctl    ioctl
  51. #define socket_write     netwrite
  52. #define socket_perror   win$perror
  53. #define socket_close     netclose
  54.  
  55. #else /* Not WINTCP */
  56. #ifndef I386IX
  57. #include <errno.h>
  58. #endif /* I386IX */
  59. #include <signal.h>
  60. #ifndef ZILOG
  61. #include <setjmp.h>
  62. #else
  63. #include <setret.h>
  64. #endif /* ZILOG */
  65. #endif /* WINTCP */
  66.  
  67. #ifdef datageneral            /* Data General AOS/VS */
  68. #include <:usr:include:vs_tcp_errno.h>
  69. #include <:usr:include:sys:vs_tcp_types.h>
  70. #include <:usr:include:sys:socket.h>
  71. #include <:usr:include:netinet:in.h>
  72. #include <:usr:include:netdb.h>
  73. #endif /* datageneral */
  74.  
  75. extern SIGTYP (*saval)();        /* For saving alarm handler */
  76.  
  77. _PROTOTYP( VOID bgchk, (void) );
  78.  
  79. extern int                /* External variables */
  80.   duplex, debses, seslog, ttyfd, quiet, msgflg;
  81.  
  82. #ifdef SVR4
  83. /*
  84.   These suggested by Rob Healey, rhealey@kas.helios.mn.org, to avoid
  85.   bugs in Berkeley compatibility library on Sys V R4 systems, but untested
  86.   by me (fdc).  Remove this bit if it gives you trouble.
  87.   (Later corrected by Marc Boucher <mboucher@iro.umontreal.ca> because
  88.   bzero/bcopy are not argument-compatible with memset/memcpy|memmove.)
  89. */
  90. #define bzero(s,n) memset(s,0,n)
  91. #define bcopy(h,a,l) memmove(a,h,l)
  92. #else
  93. #ifdef PTX                /* Sequent DYNIX PTX 1.3 */
  94. #define bzero(s,n) memset(s,0,n)
  95. #define bcopy(h,a,l) memcpy(a,h,l)
  96. #endif /* PTX */
  97. #endif /* SVR4 */
  98.  
  99. #define NAMECPYL 100            /* Local copy of hostname */
  100. static char namecopy[NAMECPYL];
  101.  
  102. char ipaddr[20] = { '\0' };        /* Global copy of IP address */
  103.  
  104. /*
  105.   VMSTCPIP means "DEC_TCPIP or MULTINET or WINTCP" (defined in ckcnet.h).
  106. */
  107. #ifdef VMSTCPIP
  108. /*
  109.   General global variables, but so far used only by MultiNet and WIN/TCP.
  110.   Kept within #ifdef MULTINET..#endif to keep strict compilers (and lint)
  111.   from complaining about unused variables.
  112. */
  113. static jmp_buf njbuf;            /* For timeout longjumps */
  114. #endif /* VMSTCPIP */
  115.  
  116. #endif /* NETCONN */
  117.  
  118. int ttnet = NET_NONE;            /* Network type */
  119. int ttnproto = NP_NONE;            /* Network virtual terminal protocol */
  120. int tn_init = 0;            /* Telnet protocol initialized flag */
  121. int tn_duplex = 1;            /* Initial echo status */
  122. char *tn_term = NULL;            /* Terminal type override */
  123. int tn_nlm = 1;                /* Telnet CR -> CR LF mode */
  124.  
  125. #ifndef NETCONN
  126. /*
  127.   Network support not defined.
  128.   Dummy functions here in case #ifdef's forgotten elsewhere.
  129. */
  130. int                    /* Open network connection */
  131. netopen(name, lcl, nett) char *name; int *lcl, nett; {
  132.     return(-1);
  133. }
  134. int                    /* Close network connection */
  135. netclos() {
  136.     return(-1);
  137. }
  138. int                    /* Check network input buffer */
  139. nettchk() {
  140.     return(-1);
  141. }
  142. int                    /* Flush network input buffer */
  143. netflui() {
  144.     return(-1);
  145. }
  146. int                    /* Send network BREAK */
  147. netbreak() {
  148.     return(-1);
  149. }
  150. int                    /* Input character from network */
  151. netinc(timo) int timo; {
  152.     return(-1);
  153. }
  154. int                    /* Output character to network */
  155. #ifdef CK_ANSIC
  156. nettoc(char c)
  157. #else
  158. nettoc(c) char c;
  159. #endif /* CK_ANSIC */
  160. /* nettoc */ {
  161.     return(-1);
  162. }
  163. int
  164. nettol(s,n) char *s; int n; {
  165.     return(-1);
  166. }
  167.  
  168. #else /* NETCONN is defined (rest of this module...) */
  169.  
  170. #ifdef VMSTCPIP
  171.  
  172. /* For buffered network reads... */
  173. /*
  174.   If the buffering code is written right, it shouldn't matter how long this
  175.   buffer is -- it could even be shorter than a Kermit packet.
  176. */
  177. #define TTIBUFL 8192            /* Maybe 8K?... */
  178.  
  179. CHAR     ttibuf[TTIBUFL+1];
  180. int     ttibp = 0, ttibn = 0;
  181. /*
  182.   Read bytes from network into internal buffer ttibuf[].
  183.   To be called when input buffer is empty, i.e. when ttibn == 0.
  184.  
  185.   Other network reading routines, like ttinc, ttinl, ttxin, should check the
  186.   internal buffer first, and call this routine for a refill if necessary.
  187.  
  188.   Returns -1 on error, 0 if nothing happens.  When data is read successfully,
  189.   returns number of bytes read, and sets global ttibn to that number and
  190.   ttibp (the buffer pointer) to zero.
  191. */
  192. int
  193. ttbufr() {                /* TT Buffer Read */
  194.     int count;
  195.  
  196.     if (ttnet != NET_TCPB) {        /* First make sure current net is */
  197.     return(-1);            /* TCP/IP; if not, do nothing. */
  198.     } else {
  199.     if (ttibn > 0)            /* Out internal buffer is not empty, */
  200.       return(ttibn);        /* so keep using it. */
  201. #ifdef WINTCP
  202.     count = 512;            /* This works for WIN/TCP */
  203. #else                    /* Not WINTCP, i.e it's Multinet */
  204. #ifdef DEC_TCPIP
  205.     count = 512;            /* This works for WIN/TCP */
  206. #else                    /* Not WINTCP, i.e it's Multinet */
  207.     count = nettchk();        /* Check network input buffer, */
  208.     if (ttibn > 0) return(ttibn);    /* which can put a char there! */
  209.     if (count < 0)            /* Read error */
  210.       return(-1);
  211.     else if (count > TTIBUFL)    /* Too many to read */
  212.       count = TTIBUFL;
  213.     else if (count == 0)        /* None, so force blocking read */
  214.       count = 1;
  215. #endif /* DEC_TCPIP */
  216. #endif /* WINTCP */
  217.     debug(F101,"ttbufr count 1","",count);
  218.  
  219. #ifdef COMMENT
  220. /*
  221.  This is for nonblocking reads, which we don't do any more.  This code didn't
  222.  work anyway, in the sense that a broken connection was never sensed.
  223. */
  224.     if ((count = socket_read(ttyfd,ttibuf,count)) < 1) {
  225.         if (count == -1 && socket_errno == EWOULDBLOCK) {
  226.         debug(F100,"ttbufr finds nothing","",0);
  227.         return(0);
  228.         } else if (count == 0) {
  229.         debug(F100,"ttbufr socket eof","",0);        
  230.         return(-1);
  231.         } else {
  232.         debug(F101,"ttbufr socket_read error","",socket_errno);
  233.         return(-1);
  234.         }
  235.     }
  236. #else
  237. /* This is for blocking reads */
  238.     if ((count = socket_read(ttyfd,ttibuf,count)) < 1) {
  239.         debug(F101,"ttbufr socket_read","",count);
  240.         debug(F101,"ttbufr socket_errno","",socket_errno);
  241.         return(-1);
  242.     }
  243. #endif /* COMMENT */
  244.     ttibp = 0;            /* Reset buffer pointer. */
  245.     ttibn = count;
  246. #ifdef DEBUG
  247.     debug(F101,"ttbufr count 2","",count); /* Got some bytes. */
  248.     if (count > 0) ttibuf[count] = '\0';
  249.     debug(F111,"ttbufr ttibuf",ttibuf,ttibp);
  250. #endif /* DEBUG */
  251.     return(ttibn);            /* Return buffer count. */
  252.     }
  253. }
  254. #endif /* VMSTCPIP */
  255.  
  256. /*
  257.   C-Kermit network open/close functions for BSD-sockets.
  258.   Much of this code shared by SunLink X.25, which also uses the socket library.
  259. */
  260.  
  261. /*  N E T O P E N  --  Open a network connection.  */
  262.  
  263. /*  Returns 0 on success, -1 on failure.  */
  264.  
  265. #define    TELNET_PORT    23       /* Should do lookup, but it won't change */
  266.  
  267. /* This symbol is not known to, e.g., Ultrix 2.0 */
  268. #ifndef TELOPT_TTYPE
  269. #define TELOPT_TTYPE 24
  270. #endif /* TELOPT_TTYPE */
  271.  
  272. /*  N E T O P N  --  Open a network connection.  */
  273. /*
  274.   Call with:
  275.     name of host (or host:service),
  276.     lcl - local-mode flag to be set if this function succeeds,
  277.     network type - value defined in ckunet.h.
  278. */
  279.  
  280. #ifdef EXCELAN
  281. /*
  282.   Most other BSD sockets implementations define these in header files
  283.   and libraries.
  284. */
  285. struct servent {
  286.     unsigned short s_port;
  287. };
  288.  
  289. struct hostent {
  290.     short h_addrtype;
  291.     struct in_addr h_addr;
  292.     int h_length;
  293. };
  294.  
  295. struct servent *
  296. getservbyname(service, connection) char *service,*connection; {
  297.     static struct servent servrec;
  298.     int port;
  299.  
  300.     port = 0;
  301.     if (strcmp(service, "telnet") == 0) port = 23;
  302.     else if (strcmp(service, "smtp") == 0) port = 25;
  303.     else port = atoi(service);
  304.  
  305.     debug(F101,"getservbyname return port ","",port);
  306.  
  307.     if (port > 0) {
  308.         servrec.s_port = htons(port);
  309.         return( &servrec );
  310.     }
  311.     return( (struct servent *) NULL );
  312. }
  313.  
  314. struct hostent *
  315. gethostbyname(hostname) char *hostname; {
  316.     return( (struct hostent *) NULL );
  317. }
  318.  
  319. unsigned long
  320. inet_addr(name) char *name; {
  321.     unsigned long addr;
  322.  
  323.     addr = rhost(&name);
  324.     debug(F111,"inet_addr ",name,(int)addr);
  325.     return(addr);
  326. }
  327.  
  328. char *
  329. inet_ntoa(in) struct in_addr in; {
  330.     static char name[80];
  331.     sprintf(name, "%d.%d.%d.%d", in.s_net, in.s_host, in.s_lh, in.s_impno);
  332.     return(name);
  333. }
  334. #endif /* EXCELAN */
  335.  
  336. /*  N E T O P E N  --  Open a network connection  */
  337. /*
  338.   Calling conventions same as ttopen(), except third argument is network
  339.   type rather than modem type.  Designed to be called from within ttopen.
  340. */
  341. int
  342. netopen(name, lcl, nett) char *name; int *lcl, nett; {
  343.     char *p;
  344. #ifdef SO_OOBINLINE
  345.     int on = 1;
  346. #endif /* SO_OOBINLINE */
  347.     int i, x;
  348.     struct servent *service, servrec;
  349.     struct hostent *host;
  350.     struct sockaddr_in saddr;
  351. #ifdef EXCELAN
  352.     struct sockaddr_in send_socket;
  353. #endif /* EXCELAN */
  354.  
  355. #ifdef SUNX25                /* Code for SunLink X.25 support */
  356. #define X29PID 1            /* X.29 Protocol ID */
  357.     VOID x25oobh();
  358.     CONN_DB x25host;
  359.     FACILITY_DB x25facil;
  360.     static int needh = 1;
  361.     PID_T pid;
  362.     extern int linkid, lcn, x25ver;
  363.     extern int revcall, closgr, cudata;
  364.     extern char udata[MAXCUDATA];
  365. #endif /* SUNX25 */
  366.  
  367.     debug(F101,"netopen nett","",nett);
  368.     *ipaddr = '\0';            /* Initialize IP address string */
  369.  
  370. #ifdef SUNX25
  371.     if (nett == NET_SX25) {        /* If network type is X.25 */
  372.         netclos();            /* Close any previous net connection */
  373.         ttnproto = NP_NONE;        /* No protocol selected yet */
  374.  
  375.         /* Set up host structure */
  376.         bzero ((char *)&x25host,sizeof(x25host));
  377.         if ((x25host.hostlen = pkx121 (name,x25host.host)) < 0) {
  378.             fprintf (stderr,"Invalid X.121 host address %s\n",name);
  379.             errno = 0;
  380.             return (-1);
  381.         }
  382.         x25host.datalen = X29PIDLEN;
  383.         x25host.data[0] = X29PID;
  384.  
  385.     /* Set call user data if specified */
  386.         if (cudata) {
  387.             strncpy(x25host.data+X29PIDLEN,udata,(int)strlen(udata));
  388.             x25host.datalen += (int)strlen(udata);
  389.         }
  390.  
  391.         /* Open SunLink X.25 socket */
  392.         if ((ttyfd = socket (AF_X25, SOCK_STREAM, 0)) < 0) {
  393.         debug(F101,"netopen socket error","",errno);
  394.             perror ("X.25 connect socket error");
  395.             return (-1);
  396.         }
  397.  
  398.         /* Setting X.25 out-of-band data handler */
  399.         pid = getpid();
  400.         if (ioctl(ttyfd,SIOCSPGRP,&pid)) {
  401.             perror("Setting process group id");
  402.             return(-1);
  403.         }
  404.         (VOID) signal(SIGURG,x25oobh);
  405.  
  406.         /* Set reverse charge call and closed user group if requested */
  407.         bzero ((char *)&x25facil,sizeof(x25facil));
  408.         if (revcall) x25facil.reverse_charge = revcall;
  409.         if (closgr > -1) {
  410.             x25facil.cug_req = 1;
  411.             x25facil.cug_index = closgr;
  412.         }
  413.         if (ioctl(ttyfd,X25_WR_FACILITY,&x25facil) < 0) {
  414.             perror ("Setting X.25 facilities");
  415.             return (-1);
  416.         }
  417.  
  418.         /*  Need X.25 header with bits Q and M */
  419.         if (ioctl (ttyfd,X25_HEADER,&needh) < 0) {
  420.             perror ("Setting X.25 header");
  421.             return (-1);
  422.         }
  423.  
  424.         /* Connects to remote host via SunLink X.25 */
  425.         if (connect(ttyfd,&x25host,sizeof(x25host)) < 0) {
  426.             debug(F101,"netopen connect errno","",errno);
  427.             i = errno;
  428.         if (errno) {
  429.                 perror("netopen");
  430.                 x25diag();
  431.             }
  432.             (VOID) close (ttyfd);
  433.             ttyfd = -1;
  434.             errno = i;
  435.             return (-1);
  436.         }
  437.  
  438.         /* Get X.25 link identification used for the connection */
  439.         if (ioctl(ttyfd,X25_GET_LINK,&linkid) < 0) {
  440.             perror ("Getting X.25 link id");
  441.             return (-1);
  442.         }
  443.  
  444.         /* Get X.25 logical channel number used for the connection */
  445.         if (ioctl(ttyfd,X25_RD_LCGN,&lcn) < 0) {
  446.             perror ("Getting X.25 lcn");
  447.             return (-1);
  448.         }
  449.  
  450.         /* Get SunLink X.25 version */
  451.         if (ioctl(ttyfd,X25_VERSION,&x25ver) < 0) {
  452.             perror ("Getting SunLink X.25 version");
  453.             return (-1);
  454.         }
  455.         ttnet = nett;                   /* Sunlink X.25 network */
  456.         ttnproto = NP_X3;               /* PAD X.3, X.28, X.29 protocol */
  457.         if (*lcl < 0) *lcl = 1;         /* Local mode */
  458.         return(0);
  459.     } else /* Note that SUNX25 support can coexist with TCP/IP support. */
  460. #endif /* SUNX25 */
  461. /*
  462.   Add support for other networks here.
  463. */
  464.     if (nett != NET_TCPB) return(-1);    /* BSD socket support */
  465.  
  466.     netclos();                /* Close any previous connection. */
  467.     strncpy(namecopy, name, NAMECPYL);    /* Copy the hostname. */
  468.     ttnproto = NP_NONE;            /* No protocol selected yet. */
  469.     debug(F110,"netopen namecopy",namecopy,0);
  470.  
  471.     p = namecopy;            /* Was a service requested? */
  472.     while (*p != '\0' && *p != ':') p++; /* Look for colon */
  473.     if (*p == ':') {            /* Have a colon */
  474.     *p++ = '\0';            /* Get service name or number */
  475.     } else {                /* Otherwise use telnet */
  476.     p = "telnet";
  477.     }
  478.     debug(F110,"netopen service requested",p,0);
  479.     if (isdigit(*p)) {            /* Use socket number without lookup */
  480.     service = &servrec;
  481.     service->s_port = htons((unsigned short)atoi(p));
  482.     } else {                /* Otherwise lookup the service name */
  483.     service = getservbyname(p, "tcp");
  484.     }
  485.     if (!service) {
  486.     fprintf(stderr, "Cannot find port for service %s\n", p);
  487. #ifdef MULTINET
  488.     debug(F101,"netopen can't get service","",socket_errno);
  489. #else
  490.     debug(F101,"netopen can't get service","",errno);
  491. #endif /* MULTINET */
  492.     errno = 0;            /* rather than mislead */
  493.     return(-1);
  494.     }
  495.     /* Set up socket structure and get host address */
  496.  
  497.     bzero((char *)&saddr, sizeof(saddr));
  498.     if ((host = gethostbyname(namecopy)) != NULL) {
  499.     saddr.sin_family = host->h_addrtype;
  500.     bcopy(host->h_addr, (caddr_t)&saddr.sin_addr, host->h_length);
  501.     } else {
  502. #ifdef INADDRX
  503. /* inet_addr() is of type struct in_addr */
  504.     struct in_addr ina;
  505.     unsigned long uu;
  506. #ifdef datageneral
  507.         extern struct in_addr inet_addr();
  508. #endif /* datageneral */
  509.     ina = inet_addr(namecopy);
  510.     uu = *(unsigned long *)&ina;
  511. #else /* Not INADDRX */
  512. /* inet_addr() is unsigned long */
  513.     unsigned long uu;
  514.     uu = inet_addr(namecopy);
  515. #endif /* INADDRX */
  516.     if ((saddr.sin_addr.s_addr = uu) != ((unsigned long)-1))
  517.       saddr.sin_family = AF_INET;
  518.     else {
  519.       fprintf(stderr, "Can't get address for %s\n", namecopy);
  520. #ifdef MULTINET
  521.       debug(F101,"netopen can't get address","",socket_errno);
  522. #else
  523.       debug(F101,"netopen can't get address","",errno);
  524. #endif /* MULTINET */
  525.       errno = 0;            /* rather than mislead */
  526.       return(-1);
  527.       }
  528.     }
  529.  
  530.     /* Get a file descriptor for the connection. */
  531.  
  532.     saddr.sin_port = service->s_port;
  533.     sprintf(ipaddr,"%s", inet_ntoa(saddr.sin_addr));
  534.     if (!quiet && *ipaddr) printf(" Trying %s...\n", ipaddr);
  535.  
  536. #ifdef EXCELAN
  537.     send_socket.sin_family = AF_INET;
  538.     send_socket.sin_addr.s_addr = 0;
  539.     send_socket.sin_port = 0;
  540.     if ((ttyfd = socket(SOCK_STREAM, (struct sockproto *)0,
  541.         &send_socket, SO_REUSEADDR)) < 0)
  542. #else
  543.     if ((ttyfd = socket(AF_INET, SOCK_STREAM, 0)) < 0)
  544. #endif /* EXCELAN */
  545.       {
  546. #ifdef EXCELAN
  547.     experror("TCP socket error");
  548. #else
  549. #ifdef MULTINET
  550.      socket_perror("TCP socket error");
  551.     debug(F101,"netopen socket error","",socket_errno);
  552. #else
  553.      perror("TCP socket error");
  554.     debug(F101,"netopen socket error","",errno);
  555. #endif /* MULTINET */
  556. #endif /* EXCELAN */
  557.     return (-1);
  558.     }
  559.     errno = 0;
  560.  
  561.     /* Now connect to the socket on the other end. */
  562.  
  563. #ifdef EXCELAN
  564.     if (connect(ttyfd, &saddr) < 0)
  565. #else
  566.     if (connect(ttyfd, (struct sockaddr *)&saddr, sizeof(saddr)) < 0)
  567. #endif /* EXCELAN */
  568.       {
  569.     i = errno;            /* save error code */
  570.     close(ttyfd);
  571.     ttyfd = -1;
  572.     errno = i;            /* and report this error */
  573. #ifdef EXCELAN
  574.     if (errno) experror("netopen connect");
  575. #else
  576. #ifdef MULTINET
  577.     debug(F101,"netopen connect error","",socket_errno);
  578.     if (errno) socket_perror("netopen connect");
  579. #else
  580.     debug(F101,"netopen connect errno","",errno);
  581. #ifdef    WINTCP
  582.     perror("netopen connect");
  583. #endif    /* WINTCP */
  584. #ifdef DEC_TCPIP
  585.     perror("netopen connect");
  586. #endif /* DEC_TCPIP */
  587. #endif /* MULTINET */
  588. #endif /* EXCELAN */
  589.     return(-1);
  590.     }
  591. #ifdef SO_OOBINLINE
  592. /*
  593.   The symbol SO_OOBINLINE is not known to Ultrix 2.0.
  594.   It means "leave out of band data inline".  The normal value is 0x0100,
  595.   but don't try this on systems where the symbol is undefined.
  596. */
  597. #ifdef datageneral
  598.     setsockopt(ttyfd, SOL_SOCKET, SO_OOBINLINE,(char *) &on, sizeof on);
  599. #else
  600. #ifdef BSD43
  601.     setsockopt(ttyfd, SOL_SOCKET, SO_OOBINLINE,(char *) &on, sizeof on);
  602. #else
  603. #ifdef OSF1
  604.     setsockopt(ttyfd, SOL_SOCKET, SO_OOBINLINE,(char *) &on, sizeof on);
  605. #else
  606. #ifdef POSIX
  607.     setsockopt(ttyfd, SOL_SOCKET, SO_OOBINLINE,(char *) &on, sizeof on);
  608. #else
  609. #ifdef SOLARIS
  610. /*
  611.   Maybe this applies to all SVR4 versions, but the other (else) way has been
  612.   compiling and working fine on all the others, so best not to change it.
  613. */
  614.     setsockopt(ttyfd, SOL_SOCKET, SO_OOBINLINE,(char *) &on, sizeof on);
  615. #else
  616.     setsockopt(ttyfd, SOL_SOCKET, SO_OOBINLINE, &on, sizeof on);
  617. #endif /* SOLARIS */
  618. #endif /* POSIX */
  619. #endif /* BSD43 */
  620. #endif /* OSF1 */
  621. #endif /* datageneral */
  622. #endif /* SO_OOBINLINE */
  623.  
  624.     /* See if the service is TELNET. */
  625.     if ((x = ntohs((unsigned short)service->s_port)) == TELNET_PORT)
  626.       ttnproto = NP_TELNET;        /* Yes, set global flag. */
  627.     debug(F101,"netopen service","",x);
  628.     ttnet = nett;            /* TCP/IP (sockets) network */
  629.     tn_init = 0;            /* Telnet protocol not init'd yet */
  630.     if (*lcl < 0) *lcl = 1;        /* Local mode. */
  631.     return(0);                /* Done. */
  632. }
  633.  
  634. /*  N E T C L O S  --  Close current network connection.  */
  635.  
  636. int
  637. netclos() {
  638.     int x = 0;
  639.     if (ttyfd < 0)            /* Was open? */
  640.       return(0);            /* Wasn't. */
  641.     if (ttyfd > -1)            /* Was. */
  642. #ifdef VMSTCPIP
  643.       x = socket_close(ttyfd);        /* Close it. */
  644. #else
  645.       x = close(ttyfd);
  646. #endif /* VMSTCPIP */
  647.     ttyfd = -1;                /* Mark it as closed. */
  648.     tn_init = 0;            /* Remember about telnet protocol... */
  649.     *ipaddr = '\0';            /* Zero the IP address string */
  650.     return(x);
  651. }
  652.  
  653. /*  N E T T C H K  --  Check if network up, and how many bytes can be read */
  654. /*
  655.   Returns number of bytes waiting, or -1 if connection has been dropped.
  656. */
  657. int                    /* Check how many bytes are ready */
  658. nettchk() {                /* for reading from network */
  659. #ifdef VMSTCPIP
  660.     unsigned int count;
  661.     int x, y;
  662.     char c;
  663.  
  664.     debug(F101,"nettchk entry ttibn","",ttibn);
  665.     debug(F101,"nettchk entry ttibp","",ttibp);
  666.     socket_errno = 0;
  667. /*
  668.   Note: this socket_ioctl() call does NOT return an error if the
  669.   connection has been broken.  (At least not in Multinet.)
  670. */
  671.     if (socket_ioctl(ttyfd,FIONREAD,&count) < 0) {
  672.     debug(F101,"nettchk socket_ioctl error","",socket_errno);
  673.     if (ttibn < 1) return(-1);
  674.     else return(ttibn);
  675.     }
  676.     debug(F101,"nettchk count","",count);
  677.  
  678. #ifndef DEC_TCPIP
  679. /*
  680.   Let's see if we can skip this for UCX, since it seems to cause trouble.
  681. */
  682.     if (count == 0) {
  683. /*
  684.   Here we need to tell the difference between a 0 count on an active
  685.   connection, and a 0 count because the remote end of the socket broke the
  686.   connection.  There is no mechanism in TGV MultiNet (or WIN/TCP?) to query
  687.   the status of the connection, so we have to do a read.  -1 means there was
  688.   no data available (socket_errno == EWOULDBLOCK), 0 means the connection is
  689.   down.  But if, by chance, we actually get a character, we have to put it
  690.   where it won't be lost.
  691. */
  692.     y = 1;                /* Turn on nonblocking reads */
  693.     debug(F101,"nettchk before FIONBIO","",x);
  694.     x = socket_ioctl(ttyfd,FIONBIO,&y);
  695.     debug(F101,"nettchk FIONBIO","",x);
  696.     x = socket_read(ttyfd,&c,1);    /* Returns -1 if no data */
  697.     debug(F101,"nettchk socket_read","",x);
  698.     y = 0;                /* Turn them back off */
  699.     socket_ioctl(ttyfd,FIONBIO,&y);
  700.     if (x == 0) return(-1);        /* Connection is broken. */
  701.     if (x == 1) {            /* Oops, actually got a byte? */
  702.         debug(F101,"nettchk socket_read char","",c);
  703.         debug(F101,"nettchk ttibp","",ttibp);
  704.         debug(F101,"nettchk ttibn","",ttibn);
  705. /*
  706.   So put the byte we got into the buffer at the current position.
  707.   Increment the buffer count, but DON'T increment the buffer pointer.
  708. */
  709.         ttibuf[ttibp+ttibn] = c;
  710.         ttibn++;
  711. #ifdef DEBUG
  712.         ttibuf[ttibp+ttibn] = '\0';
  713.         debug(F111,"nettchk ttibn",ttibuf,ttibn);
  714. #endif /* DEBUG */
  715.     }
  716.     }
  717. #endif /* DEC_TCPIP */
  718.     debug(F101,"nettchk returns","",count+ttibn);
  719.     return(count + ttibn);
  720.  
  721. #else /* Not VMSTCPIP */
  722. /*
  723.   UNIX just uses ttchk(), in which the ioctl() calls on the file descriptor
  724.   seem to work OK.
  725. */
  726.     return(0);
  727. #endif /* VMSTCPIP */
  728. /*
  729.   But what about X.25?
  730. */
  731. }
  732.  
  733. /*  N E T I N C --  Input character from network */
  734.  
  735. int            
  736. netinc(timo) int timo; {
  737. #ifdef VMSTCPIP
  738.     int x; unsigned char c;        /* The locals. */
  739.  
  740.     if (ttibn > 0) {            /* Something in internal buffer? */
  741.     debug(F100,"netinc char in buf","",0); /* Yes. */
  742.     x = 0;                /* Success. */
  743.     } else {                /* Else must read from network. */
  744.     x = -1;                /* Assume failure. */
  745. #ifdef DEBUG
  746.     debug(F101,"netinc goes to net, timo","",timo);
  747.     ttibuf[ttibp+1] = '\0';
  748.     debug(F111,"netinc ttibuf",ttibuf,ttibp);
  749. #endif /* DEBUG */
  750.     if (timo <= 0) {        /* Untimed case. */
  751.         while (1) {            /* Wait forever if necessary. */
  752.         if (ttbufr() < 0)    /* Refill buffer. */
  753.           break;        /* Error, fail. */
  754.         if (ttibn > 0) {    /* Success. */
  755.             x = 0;
  756.             break;
  757.         }
  758.         }
  759.     } else {            /* Timed case... */
  760.         saval = signal(SIGALRM,ttimoff); /* Enable timer interrupt */
  761.         alarm(timo);        /* for requested interval. */
  762.         if (setjmp(njbuf)) {    /* Timer went off? */
  763.         x = -1;            /* Yes, fail. */
  764.         } else {
  765.         while (1) {
  766.             if (ttbufr() < 0)    /* Keep trying to refill it. */
  767.               break;        /* Till we get an error. */
  768.             if (ttibn > 0) {    /* Or we get a character. */
  769.             x = 0;
  770.             break;
  771.             }
  772.         }
  773.         }
  774.         ttimoff();            /* Timer off. */
  775.     }
  776.     }
  777.     if (x < 0) {            /* Return -1 if we failed. */
  778.     debug(F100,"netinc timed out","",0);
  779.     return(-1);
  780.     } else {                /* Otherwise */
  781.     ttibn--;            /* Return what we got. */
  782.     c = ttibuf[ttibp++];
  783.     debug(F101,"netinc returning","",c);
  784.     return((c & 0xff));
  785.     }
  786. #else /* Not MULTINET or WINTCP */
  787.     return(-1);
  788. #endif /* VMSTCPIP */
  789. }
  790.  
  791. /*  N E T T O L  --  Output a string of bytes to the network  */
  792. /*
  793.   Call with s = pointer to string, n = length.
  794.   Returns number of bytes actually written on success, or
  795.   -1 on i/o error, -2 if called improperly.
  796. */
  797. int
  798. nettol(s,n) char *s; int n; {
  799. #ifdef VMSTCPIP
  800.     int count;
  801.     if (ttnet == NET_TCPB) {
  802.     if ((count = socket_write(ttyfd,s,n)) < 1) {
  803.         debug(F101,"nettol socket_write error","",socket_errno);
  804.         return(-1);
  805.     }
  806.     debug(F111,"nettol socket_write",s,count);
  807.     return(count);
  808.     } else return(-2);
  809. #else
  810.     debug(F100,"nettol VMSTCPIP not defined","",0);
  811.     return(-2);
  812. #endif /* VMSTCPIP */
  813. }
  814.  
  815. /*  N E T T O C  --   Output character to network */
  816. /*
  817.   Call with character to be transmitted.
  818.   Returns 0 if transmission was successful, or
  819.   -1 upon i/o error, or -2 if called improperly.
  820. */
  821. int            
  822. #ifdef CK_ANSIC
  823. nettoc(char c)
  824. #else
  825. nettoc(c) char c;
  826. #endif /* CK_ANSIC */
  827. /* nettoc */ {
  828. #ifdef VMSTCPIP
  829.     unsigned char cc;
  830.     cc = c;
  831.     if (ttnet == NET_TCPB) {
  832.     debug(F101,"nettoc cc","",cc);
  833.     if (socket_write(ttyfd,&cc,1) < 1) {
  834.         debug(F101,"nettoc socket_write error","",socket_errno);
  835.         return(-1);
  836.     }
  837.     debug(F101,"nettoc socket_write","", cc);
  838.     return(0);
  839.     } else return(-2);
  840. #else
  841.     return(-2);
  842. #endif /* MULTINET */
  843. }
  844.  
  845. /*  N E T F L U I  --  Flush network input buffer  */
  846.  
  847. int
  848. netflui() {
  849.     int n;
  850. #ifdef VMSTCPIP
  851.     ttibuf[ttibp+1] = '\0';
  852.     debug(F111,"netflui 1",ttibuf,ttibn);
  853.     ttibn = ttibp = 0;            /* Flush internal buffer *FIRST* */
  854.     if ((n = nettchk()) > 0) {        /* Now see what's waiting on the net */
  855.     if (n > TTIBUFL) n = TTIBUFL;    /* and sponge it up */
  856.     debug(F101,"netflui 2","",n);    /* ... */
  857.     n = socket_read(ttyfd,ttibuf,n) ; /* into our buffer */
  858.     if (n >= 0) ttibuf[n] = '\0';
  859.     debug(F111,"netflui 3",ttibuf,n);
  860.     ttibuf[0] = '\0';
  861.     }
  862. #else
  863. /*
  864.   It seems the UNIX ioctl()s don't do the trick, so we have to read the
  865.   stuff ourselves.  This should be pretty much portable, if not elegant.
  866. */
  867.     if ((n = ttchk()) > 0) {
  868.     debug(F101,"netflui","",n);
  869.     while ((n--) && ttinc(0) > -1) ; /* Don't worry, it's buffered. */
  870.     }
  871. #endif /* VMSTCPIP */
  872.     return(0);
  873. }
  874.  
  875. #ifdef TNCODE                /* Compile in telnet support code */
  876.  
  877. /* TCP/IP TELNET protocol negotiation support code */
  878.  
  879. static int sgaflg = 0;            /* SUPRRESS GO-AHEAD state */
  880. static int dosga  = 0;            /* 1 if I sent DO SGA from tn_ini() */
  881. static int wttflg = 0;            /* ditto for WILL TERMINAL TYPE */
  882.  
  883. #ifndef TELCMDS
  884. char *telcmds[] = {
  885.     "SE", "NOP", "DMARK", "BRK",  "IP",   "AO", "AYT",  "EC",
  886.     "EL", "GA",  "SB",    "WILL", "WONT", "DO", "DONT", "IAC",
  887. };
  888. int ntelcmds = sizeof(telcmds) / sizeof(char *);
  889. #endif /* TELCMDS */
  890.  
  891. #ifndef TELOPTS
  892. char *telopts[] = {
  893.     "BINARY", "ECHO", "RCP", "SUPPRESS GO AHEAD", "NAME",
  894.     "STATUS", "TIMING MARK", "RCTE", "NAOL", "NAOP",
  895.     "NAOCRD", "NAOHTS", "NAOHTD", "NAOFFD", "NAOVTS",
  896.     "NAOVTD", "NAOLFD", "EXTEND ASCII", "LOGOUT", "BYTE MACRO",
  897.     "DATA ENTRY TERMINAL", "SUPDUP", "SUPDUP OUTPUT",
  898.     "SEND LOCATION", "TERMINAL TYPE", "END OF RECORD"
  899. #ifdef TELOPT_TUID
  900.     ,"TACACS UID"
  901. #ifdef TELOPT_OUTMRK
  902.     ,"OUTPUT MARKING"
  903. #ifdef TELOPT_TTYLOC
  904.     ,"TTYLOC"
  905. #ifdef TELOPT_3270REGIME
  906.     ,"3270 REGIME"
  907. #ifdef TELOPT_X3PAD
  908.     ,"X.3 PAD"
  909. #ifdef TELOPT_NAWS
  910.     ,"NAWS"
  911. #ifdef TELOPT_TSPEED
  912.     ,"TSPEED"
  913. #ifdef TELOPT_LFLOW
  914.     ,"LFLOW"
  915. #ifdef TELOPT_LINEMODE
  916.     ,"LINEMODE"
  917. #endif
  918. #endif
  919. #endif
  920. #endif
  921. #endif
  922. #endif
  923. #endif
  924. #endif
  925. #endif
  926. };
  927. #endif /* TELOPTS */
  928.  
  929. int ntelopts = sizeof(telopts) / sizeof(char *);
  930. #endif /* TNCODE */
  931.  
  932. /* Send network BREAK */
  933. /*
  934.   Returns -1 on error, 0 if nothing happens, 1 if BREAK sent successfully.
  935. */
  936. int
  937. netbreak() {
  938.     CHAR buf[3];
  939.     if (ttnet == NET_TCPB) {
  940.     if (ttnproto == NP_TELNET) {
  941. #ifdef TNCODE
  942.         buf[0] = (CHAR) IAC; buf[1] = (CHAR) BREAK;
  943.         if (ttol(buf,2) < 2) return(-1);
  944.         debug(F101,"telnet BREAK ok","",BREAK);
  945.         return(1);
  946. #else
  947.         debug(F100,"netbreak no TNCODE","",0);
  948.         return(0);
  949. #endif /* TNCODE */
  950.     }
  951.     /* Insert other TCP/IP protocols here */
  952.     }
  953.     /* Insert other networks here */
  954.     return(0);
  955. }
  956.  
  957. /* Send a telnet option, avoid loops. */
  958. /* Returns 1 if command was sent, 0 if not, -1 on error */
  959.  
  960. int
  961. tn_sopt(cmd,opt) int cmd, opt; {    /* TELNET SEND OPTION */
  962.     CHAR buf[4];
  963.     int n;
  964.     if (ttnet != NET_TCPB) return(0);    /* Must be TCP/IP */
  965.     if (ttnproto != NP_TELNET) return(0); /* Must be telnet protocol */
  966.     n = cmd - SE;
  967.     if (n < 0 || n > ntelcmds) return(0);
  968. #ifdef TNCODE
  969.     buf[0] = (CHAR) IAC;
  970.     buf[1] = (CHAR) cmd & 0xff;
  971.     buf[2] = (CHAR) opt & 0xff;
  972.     if (ttol(buf,3) < 3)
  973.       return(-1);
  974.     debug(F111,"telnet cmd >",telcmds[n],cmd);
  975.     debug(F111,"telnet opt >",
  976.       (opt < ntelopts) ? telopts[opt] : "UNKNOWN", opt );
  977.     if (debses && cmd != SB)
  978.       printf("[%s %s]",telcmds[n],
  979.          (opt < ntelopts) ? telopts[opt] : "UNKNOWN");
  980.     return(1);
  981. #else
  982.     debug(F100,"tn_sopt no TNCODE","",0);
  983.     return(0);
  984. #endif /* TNCODE */
  985. }
  986.  
  987. /* Initialize a telnet connection. */
  988. /* Returns -1 on error, 0 if nothing happens, 1 if init msgs sent ok */
  989.  
  990. int
  991. tn_ini() {
  992. #ifndef TNCODE
  993.     debug(F100,"tn_ini no TNCODE","",0);
  994.     return(0);
  995. #else /* TELNET protocol support */
  996.     debug(F101,"tn_ini ttnproto","",ttnproto);
  997.     debug(F101,"tn_ini tn_init","",tn_init);
  998.  
  999.     if (ttnet != NET_TCPB)        /* Make sure connection is TCP/IP. */
  1000.       return(0);
  1001.     if (tn_init)            /* Have we done this already? */
  1002.       return(0);            /* Don't do it again. */
  1003.     debug(F101,"tn_ini tn_duplex","",tn_duplex);
  1004.     duplex = tn_duplex;            /* Assume local echo. */
  1005.     sgaflg = 0;                /* Assume Go-Ahead suppressed. */
  1006.     wttflg = 0;                /* Did not send WILL TERM TYPE yet. */
  1007.     dosga  = 0;                /* Did not send DO SGA yet. */
  1008.     if (ttnproto == NP_NONE) {        /* If not talking to a telnet port, */
  1009.     ttnproto = NP_TELNET;        /* pretend it's telnet anyway, */
  1010.     tn_init = 1;            /* but don't send initial options. */
  1011.     debug(F100,"tn_ini skipping telnet negotiations","",0);
  1012.     return(0);
  1013.     }
  1014.     /* Talking to telnet port, so send WILL TERMINAL TYPE and DO SGA */
  1015.  
  1016.     if (tn_sopt(WILL,TELOPT_TTYPE) < 0)    /* Will send terminal type. */
  1017.       return(-1);
  1018.     wttflg = 1;                /* Remember I said I would. */
  1019.     if (tn_sopt(DO,TELOPT_SGA) < 0)    /* Please suppress go-ahead. */
  1020.       return(-1);
  1021.     dosga = 1;                /* Remember I send DO SGA */
  1022.     tn_init = 1;            /* Set telnet-initialized flag. */
  1023.  
  1024.     /* Don't send anthing else! */
  1025.  
  1026.     debug(F101,"tn_ini duplex","",duplex);
  1027.     return(1);
  1028. #endif /* TNCODE */
  1029. }
  1030.  
  1031. /*
  1032.   Process in-band Telnet negotiation characters from the remote host.
  1033.   Call with the telnet IAC character and the current duplex setting
  1034.   (0 = remote echo, 1 = local echo).
  1035.   Returns:
  1036.     3 if server has sent us a quoted IAC
  1037.     2 if local echo must be changed to remote
  1038.     1 if remote echo must be changed to local
  1039.     0 if nothing happens or no action necessary
  1040.    -1 on failure (= internal or i/o error)
  1041. */
  1042.  
  1043. #define TSBUFSIZ 41
  1044. char sb[TSBUFSIZ];            /* Buffer for subnegotiations */
  1045.  
  1046. int
  1047. #ifdef CK_ANSIC                /* TELNET DO OPTION */
  1048. tn_doop( CHAR z, int echo, int (*fn)(int) )
  1049. #else
  1050. tn_doop(z, echo, fn) CHAR z; int echo; int (*fn)();
  1051. #endif /* CK_ANSIC */
  1052. /* tn_doop */ {
  1053.     int c, x, y, n, m, flag;
  1054.  
  1055. #ifndef TNCODE
  1056.     debug(F100,"tn_doop no TNCODE","",0);
  1057.     return(0);
  1058. #else
  1059.     if (z != (CHAR) IAC) {
  1060.     debug(F101,"tn_doop bad call","",z);
  1061.     return(-1);
  1062.     }
  1063.     if (ttnet != NET_TCPB) return(0);
  1064.     if (ttnproto != NP_TELNET) return(0);     /* Check protocol */
  1065.  
  1066. /* Have IAC, read command character. */
  1067.  
  1068.     c = (*fn)(0) & 0xff;        /* Read command character */
  1069.     m = c - SE;                /* Check validity */
  1070.     if (m < 0 || m > ntelcmds) {
  1071.     debug(F101,"tn_doop bad cmd","",c);
  1072.     return(0);
  1073.     }
  1074.     if (seslog) {            /* Copy to session log, if any. */
  1075.     if (zchout(ZSFILE, (char) z) < 0) seslog = 0; /* Log IAC. */
  1076.     else if (zchout(ZSFILE, (char) c) < 0) seslog = 0; /* Log command */
  1077.     }
  1078.     debug(F111,"telnet cmd <",telcmds[m],c); /* Debug log. */
  1079.  
  1080.     if (c == (CHAR) IAC) return(3);    /* Quoted IAC */
  1081.     if (c < SB) return(0);        /* Other command with no arguments. */
  1082.  
  1083. /* SB, WILL, WONT, DO, or DONT need more bytes... */
  1084.  
  1085.     if ((x = (*fn)(0)) < 0) return(-1);    /* Get the option. */
  1086.     x &= 0xff;                /* Trim to 8 bits. */
  1087.  
  1088.     debug(F111,"telnet opt <",
  1089.       (x < ntelopts) ? telopts[x] : "UNKNOWN", x );
  1090.     if (seslog)                /* Session log */
  1091.       if (zchout(ZSFILE, (char) x) < 0) seslog = 0;
  1092.  
  1093.     /* Now handle the command */
  1094.  
  1095.     if (debses && c != SB)         /* Debug to screen. */
  1096.       printf("<%s %s>",telcmds[m],
  1097.          (x < ntelopts) ? telopts[x] : "UNKNOWN" );
  1098.     switch (x) {
  1099.       case TELOPT_ECHO:            /* ECHO negotiation. */
  1100.     switch (c) {            /* Command */
  1101.       case WILL:            /* Host says it will echo. */
  1102.         if (echo)            /* Only reply if change required. */
  1103.           return((tn_sopt(DO,x) < 0) ? -1 : 2); /* Please do. */
  1104.         else return(0);        /* Otherwise no change. */
  1105.       case WONT:            /* Host says it won't echo. */
  1106.         if (!echo)            /* If I'm full duplex */
  1107.           return ((tn_sopt(DONT,x) < 0) ? -1 : 1); /* Switch to half */
  1108.         else return(0);        /* Otherwise, no change.  */
  1109.       case DO:            /* Host wants me to echo */
  1110.         if (tn_sopt(WONT,x) < 0)    /* but the client never echoes */
  1111.           return(-1);        /* back to the server. */
  1112.       default:            /* Don't reply to anything else */
  1113.         return(0);
  1114.     }
  1115.  
  1116.       case TELOPT_SGA:            /* Suppress Go-Ahead */
  1117.     switch (c) {            /* Command... */
  1118.       case WONT:            /* Host says it won't. */
  1119.         if (!sgaflg) {
  1120.         sgaflg = 1;        /* Remember. */
  1121.         if (tn_sopt(DONT,x) < 0) /* acknowledge, */
  1122.           return(-1);
  1123.         }
  1124.         return(echo ? 0 : 1);    /* Switch to half duplex */
  1125.       case WILL:            /* Server says it will SGA */
  1126.         if (sgaflg || !dosga) {    /* ACK only if necessary */
  1127.         if (tn_sopt(DO,x) < 0)
  1128.           return(-1);
  1129.         sgaflg = 0;        /* Remember new SGA state. */
  1130.         }
  1131.         return(0);            /* But don't change echo state. */
  1132.     }
  1133.  
  1134. #ifdef TELOPT_TTYPE
  1135.       case TELOPT_TTYPE:        /* Terminal Type */
  1136.     switch (c) {
  1137.       case DO:            /* DO terminal type. */
  1138.         if (wttflg == 0) {        /* If I haven't said so before, */
  1139.         if (tn_sopt((CHAR)WILL,x) < 0) /* say I'll send it if asked. */
  1140.           return(-1);
  1141.         wttflg++;
  1142.         }
  1143.         return(0);
  1144.       case SB:
  1145.         debug(F100,"TELNET subnegotiation:","",0);
  1146.         n = flag = 0;        /* Flag for when done reading SB */
  1147.         while (n < TSBUFSIZ) {    /* Loop looking for IAC SE */
  1148.         if ((y = (*fn)(0)) < 0)    /* Read a byte */
  1149.           return(-1);
  1150.         y &= 0xff;        /* Make sure it's just 8 bits. */
  1151.         sb[n++] = y;        /* Deposit in buffer. */
  1152.         if (seslog)        /* Take care of session log */
  1153.           if (zchout(ZSFILE, (char) y) < 0)
  1154.             seslog = 0;
  1155.         if (y == IAC) {        /* If this is an IAC */
  1156.             if (flag) {        /* If previous char was IAC */
  1157.             n--;        /* it's quoted, keep one IAC */
  1158.             flag = 0;    /* and turn off the flag. */
  1159.             } else flag = 1;    /* Otherwise set the flag. */
  1160.         } else if (flag) {     /* Something else following IAC */
  1161.             if (y != SE)    /* If not SE, it's a protocol error */
  1162.               flag = 0;
  1163.             break;
  1164.         }
  1165.         }
  1166.         if (!flag) {        /* Make sure we got a valid SB */
  1167.         debug(F100, "TELNET Subnegotian prematurely broken", "",0);
  1168.         return(-1);
  1169.         }
  1170.         if (debses) {        /* Debug to screen. */
  1171.         int i;
  1172.         printf("<SB %s ",telopts[TELOPT_TTYPE]);
  1173.         for (i = 0; i < n-2; i++) printf("%02x",(unsigned int) sb[i]);
  1174.         printf(" IAC SE>");
  1175.         }
  1176.         debug(F101,"TELNET suboption<","",sb[0]);
  1177.         if (sb[0] == 1) {        /* SEND terminal type? */
  1178.         if (tn_sttyp() < 0)    /* Yes, so send it. */
  1179.           return(-1);
  1180.         }
  1181.       default:            /* Others, ignore */
  1182.         return(0);
  1183.     }
  1184. #endif /* TELOPT_TTYPE */
  1185.  
  1186.       default:                /* All others: refuse */
  1187.     switch(c) {
  1188.       case WILL:            /* You will? */
  1189.         if (tn_sopt(DONT,x) < 0)    /* Please don't. */
  1190.           return(-1);        /* (Could this cause a loop?) */
  1191.         break;
  1192.       case DO:            /* You want me to? */
  1193.         if (tn_sopt(WONT,x) < 0)    /* I won't. */
  1194.           return(-1);
  1195.         break;
  1196.       case DONT:            /* You don't want me to? */
  1197.         if (tn_sopt(WONT,x) < 0)    /* I won't. */
  1198.           return(-1);        /* (Could this cause a loop?) */
  1199.       case WONT:            /* You won't? */
  1200.         break;            /* I didn't want you to. */
  1201.     }                /* Anything else, treat as user data */
  1202.     return(0);
  1203.     }
  1204. #endif /* TNCODE */
  1205. }
  1206.  
  1207. /* Telnet send terminal type */
  1208. /* Returns -1 on error, 0 if nothing happens, 1 if type sent successfully */
  1209.  
  1210. int
  1211. tn_sttyp() {                /* Send telnet terminal type. */
  1212. #ifndef TNCODE
  1213.     debug(F100,"tn_sttyp no TNCODE","",0);
  1214.     return(0);
  1215. #else
  1216.     char *ttn; int ttl, i;        /* Name & length of terminal type. */
  1217.  
  1218.     if (ttnet != NET_TCPB) return(0);
  1219.     if (ttnproto != NP_TELNET) return(0);
  1220.  
  1221.     ttn = NULL;
  1222.  
  1223.     if (tn_term) {            /* Terminal type override? */
  1224.     debug(F110,"tn_sttyp",tn_term,0);
  1225.     if (*tn_term) ttn = tn_term;
  1226.     } else debug(F100,"tn_sttyp no term override","",0);
  1227. #ifndef datageneral
  1228.     if (!ttn)                /* If no override, */
  1229.       ttn = getenv("TERM");        /* get it from the environment. */
  1230. #endif /* datageneral */
  1231.     if ((ttn == ((char *)0)) || ((ttl = (int)strlen(ttn)) >= TSBUFSIZ)) {
  1232.     ttn = "UNKNOWN";
  1233.     ttl = 7;
  1234.     }
  1235.     sb[0] = IAC;            /* I Am a Command */
  1236.     sb[1] = SB;                /* Subnegotiation */
  1237.     sb[2] = TELOPT_TTYPE;        /* Terminal Type */
  1238.     sb[3] = (CHAR) 0;            /* Is... */
  1239.     for (i = 4; *ttn; ttn++,i++)    /* Copy and uppercase it */
  1240.       sb[i] = (islower(*ttn)) ? toupper(*ttn) : *ttn;
  1241.     ttn = sb;                /* Point back to beginning */
  1242.     sb[i++] = IAC;            /* End of Subnegotiation */
  1243.     sb[i++] = SE;            /* marked by IAC SE */
  1244.     if (ttol((CHAR *)sb,i) < 0)        /* Send it. */
  1245.       return(-1);
  1246. #ifdef DEBUG
  1247.     sb[i-2] = '\0';            /* For debugging */
  1248.     debug(F111,"telnet SB sent ttype",sb+4,ttl);
  1249. #endif /* DEBUG */
  1250.     if (debses)                /* Debug to screen. */
  1251.       printf("[SB TERMINAL TYPE 00 %s IAC SE]",sb+4);
  1252.     return(1);
  1253. #endif /* TNCODE */
  1254. }
  1255.  
  1256. #ifdef SUNX25
  1257. /*
  1258.   SunLink X.25 support by Marcello Frutig, Catholic University,
  1259.   Rio de Janeiro, Brazil, 1990.
  1260. */
  1261.  
  1262. /* PAD X.3, X.28 and X.29 support */
  1263.  
  1264. static CHAR x29err [MAXPADPARMS+3] = { X29_ERROR, INVALID_PAD_PARM, '\0' };
  1265.  
  1266.  
  1267. /* Initialize PAD */
  1268.  
  1269. extern CHAR padparms[MAXPADPARMS+1];
  1270.  
  1271. VOID
  1272. initpad() {
  1273.   padparms[PAD_BREAK_CHARACTER]        = 0;  /* Break character */
  1274.   padparms[PAD_ESCAPE]                 = 1;  /* Escape permitted */
  1275.   padparms[PAD_ECHO]                   = 1;  /* Kermit PAD does echo */
  1276.   padparms[PAD_DATA_FORWARD_CHAR]      = 2;  /* forward character CR */
  1277.   padparms[PAD_DATA_FORWARD_TIMEOUT]   = 0;  /* no timeout forward condition */
  1278.   padparms[PAD_FLOW_CONTROL_BY_PAD]    = 0;  /* not used */
  1279.   padparms[PAD_SUPPRESSION_OF_SIGNALS] = 1;  /* allow PAD service signals */
  1280.   padparms[PAD_BREAK_ACTION]           = 21; /* brk action: INT pk + brk ind*/
  1281.   padparms[PAD_SUPPRESSION_OF_DATA]    = 0;  /* no supression of user data */
  1282.   padparms[PAD_PADDING_AFTER_CR]       = 0;  /* no padding after CR */
  1283.   padparms[PAD_LINE_FOLDING]           = 0;  /* no line fold */
  1284.   padparms[PAD_LINE_SPEED]             = 0;  /* line speed - don't care */
  1285.   padparms[PAD_FLOW_CONTROL_BY_USER]   = 0;  /* flow cont of PAD - not used */
  1286.   padparms[PAD_LF_AFTER_CR]            = 0;  /* no LF insertion after CR */
  1287.   padparms[PAD_PADDING_AFTER_LF]       = 0;  /* no padding after LF */
  1288.   padparms[PAD_EDITING]                = 1;  /* can edit */
  1289.   padparms[PAD_CHAR_DELETE_CHAR]       = 8;  /* character delete character */
  1290.   padparms[PAD_BUFFER_DELETE_CHAR]     = 21; /* buffer delete character */
  1291.   padparms[PAD_BUFFER_DISPLAY_CHAR]    = 18; /* buffer display character */
  1292. }
  1293.  
  1294.  
  1295. /* Set PAD parameters */
  1296.  
  1297. VOID
  1298. setpad(s,n) CHAR *s; int n; {
  1299.     int i;
  1300.     CHAR *ps = s;
  1301.  
  1302.     for (i = 0; i < n; i++) {
  1303.         if (*ps > MAXPADPARMS)
  1304.       x29err[i+2] = *ps;
  1305.         else
  1306.       padparms[*ps] = *(ps+1);
  1307.         ps += 2;
  1308.     }
  1309. }
  1310.  
  1311. /* Read PAD parameters */
  1312.  
  1313. VOID
  1314. readpad(s,n,r) CHAR *s; int n; CHAR *r; {
  1315.     int i;
  1316.     CHAR *ps = s;
  1317.     CHAR *pr = r;
  1318.  
  1319.     *pr++ = X29_PARAMETER_INDICATION;
  1320.     for (i = 0; i < n; i++, ps++) {
  1321.          if (*ps > MAXPADPARMS) {
  1322.              x29err[i+2] = *ps++;
  1323.          } else {
  1324.              *pr++ = *ps;
  1325.              *pr++ = padparms[*ps++];
  1326.          }
  1327.     }
  1328. }
  1329.  
  1330. int
  1331. qbitpkt(s,n) CHAR *s; int n; {
  1332.     CHAR *ps = s;
  1333.     int x29cmd = *ps;
  1334.     CHAR *psa = s+1;
  1335.     CHAR x29resp[(MAXPADPARMS*2)+1];
  1336.  
  1337.     switch (x29cmd) {
  1338.  
  1339.         case X29_SET_PARMS:
  1340.             setpad (ps+1,n/2);
  1341.             if ((int)strlen(x29err) > 2) {
  1342.                 ttol (x29err,(int)strlen(x29err));
  1343.                 x29err[2] = '\0';
  1344.             }
  1345.             return (-2);
  1346.         case X29_READ_PARMS:
  1347.             readpad (ps+1,n/2,x29resp);
  1348.             setqbit ();
  1349.             ttol (x29resp,n+1);
  1350.             if ((int)strlen(x29err) > 2) {
  1351.                 ttol (x29err,(int)strlen(x29err));
  1352.                 x29err[2] = '\0';
  1353.             }
  1354.             resetqbit();
  1355.             break;
  1356.         case X29_SET_AND_READ_PARMS:
  1357.             setpad (ps+1,n/2);
  1358.             readpad (ps+1,n/2,x29resp);
  1359.             setqbit();
  1360.             ttol (x29resp,n+1);
  1361.             if ((int)strlen(x29err) > 2) {
  1362.                 ttol (x29err,(int)strlen(x29err));
  1363.                 x29err [2] = '\0';
  1364.             }
  1365.             resetqbit();
  1366.             return (-2);
  1367.         case X29_INVITATION_TO_CLEAR:
  1368.             (VOID) x25clear();
  1369.             return (-1) ;
  1370.         case X29_INDICATION_OF_BREAK:
  1371.         break;
  1372.     }
  1373.     return (0);
  1374. }
  1375.  
  1376. /* PAD break action processor */
  1377.  
  1378. VOID
  1379. breakact() {
  1380.     extern char x25obuf[MAXOX25];
  1381.     extern int obufl;
  1382.     extern int active;
  1383.     extern unsigned char tosend;
  1384.     static CHAR indbrk[3] = {
  1385.     X29_INDICATION_OF_BREAK,
  1386.     PAD_SUPPRESSION_OF_DATA,
  1387.     1
  1388.     };
  1389.     CHAR intudat, cause, diag;
  1390.  
  1391.     if (x25stat() < 0) return(0);   /* Ignore if no virtual call established */
  1392.  
  1393.     if (padparms[PAD_BREAK_ACTION] != 0) /* Forward condition */
  1394.         if (ttol(x25obuf,obufl) < 0) {
  1395.             perror ("\r\nCan't send characters");
  1396.             active = 0;
  1397.         } else {
  1398.             bzero (x25obuf,sizeof(x25obuf));
  1399.             obufl = 0;
  1400.             tosend = 0;
  1401.         };
  1402.  
  1403.     switch (padparms[PAD_BREAK_ACTION]) {
  1404.  
  1405.        case 0 : break;            /* do nothing */
  1406.        case 1 : /* send interrupt packet with interrupt user data field = 1 */
  1407.             intudat = 1;
  1408.                 x25intr (intudat);
  1409.                 break;
  1410.        case 2 : /* send reset packet with cause and diag = 0 */
  1411.         cause = diag = 0;
  1412.                 x25reset (cause,diag);
  1413.                 break;
  1414.        case 5 : /* send interrupt packet with interrupt user data field = 0 */
  1415.         intudat = 0;
  1416.                 x25intr (intudat) ;
  1417.                 setqbit ();
  1418.             /* send indication of break without a parameter field */
  1419.                 ttoc(X29_INDICATION_OF_BREAK);
  1420.                 resetqbit ();
  1421.                 break;
  1422.        case 8 : active = 0;        /* leave data transfer */
  1423.                 conol ("\r\n");
  1424.                 break;
  1425.        case 21: /* send interrupt packet with interrupt user data field = 0 */
  1426.         intudat = 0;
  1427.                 x25intr (intudat);
  1428.                 setpad (indbrk+1,2);    /* set pad to discard input */
  1429.                 setqbit ();
  1430.         /* send indication of break with parameter field */
  1431.                 ttol (indbrk,sizeof(indbrk));
  1432.                 resetqbit ();
  1433.                 break;
  1434.      }
  1435. }
  1436.  
  1437. /* X.25 support functions */
  1438.  
  1439. X25_CAUSE_DIAG diag;
  1440.  
  1441. /*
  1442.   Convert a null-terminated string representing an X.121 address
  1443.   to a packed BCD form.
  1444. */
  1445.  
  1446. int
  1447. pkx121(str,bcd) char *str; CHAR *bcd; {
  1448.     int i, j;
  1449.     u_char c;
  1450.  
  1451.     i = j = 0;
  1452.     while (str[i]) {
  1453.         if ( i >= 15 || str [i] < '0' || str [i] > '9' )
  1454.       return (-1);
  1455.         c = str [i] - '0';
  1456.         if ( i & 1 )
  1457.       bcd [j++] |= c;
  1458.         else
  1459.       bcd [j] = c << 4;
  1460.         i++;
  1461.     }
  1462.     return (i);
  1463. }
  1464.  
  1465. /* Reads and prints X.25 diagnostic */
  1466.  
  1467. int
  1468. x25diag () {
  1469.     int i;
  1470.  
  1471.     bzero ((char *)&diag,sizeof(diag));
  1472.     if (ioctl(ttyfd,X25_RD_CAUSE_DIAG,&diag)) {
  1473.         perror ("Reading X.25 diagnostic");
  1474.         return(-1);
  1475.     }
  1476.     if (diag.datalen > 0) {
  1477.         printf ("X.25 Diagnostic :");
  1478.         for (i = 0; i < diag.datalen; i++) printf (" %02x",diag.data[i]);
  1479.         printf ("\r\n");
  1480.     }
  1481.     return(0);
  1482. }
  1483.  
  1484. /* X.25 Out-of-Band Signal Handler */
  1485.  
  1486. VOID
  1487. x25oobh() {
  1488.     int oobtype;
  1489.     u_char oobdata;
  1490.  
  1491.     (VOID) signal(SIGURG,x25oobh);
  1492.     do {
  1493.         if (ioctl(ttyfd,X25_OOB_TYPE,&oobtype)) {
  1494.             perror ("Getting signal type");
  1495.             return;
  1496.         }
  1497.         switch (oobtype) {
  1498.       case INT_DATA:
  1499.         if (recv(ttyfd,oobdata,1,MSG_OOB) < 0) {
  1500.         perror ("Receiving X.25 interrupt data");
  1501.         return;
  1502.         }
  1503.         printf ("\r\nInterrupt received, data = %d\r\n", oobdata);
  1504.         break;
  1505.       case VC_RESET:
  1506.         printf ("\r\nVirtual circuit reset\r\n");
  1507.         x25diag ();
  1508.         break;
  1509.       case N_RESETS:
  1510.         printf ("\r\nReset timeout\r\n");
  1511.         break;
  1512.       case N_CLEARS:
  1513.         printf ("\r\nClear timeout\r\n");
  1514.         break;
  1515.       case MSG_TOO_LONG:
  1516.         printf ("\r\nMessage discarded, too long\r\n");
  1517.         break;
  1518.       default:
  1519.         if (oobtype) printf("\r\nUnknown oob type %d\r\n",oobtype);
  1520.         break;
  1521.     }
  1522.     } while (oobtype);
  1523. }
  1524.  
  1525. /* Send a X.25 interrupt packet */
  1526.  
  1527. int
  1528. #ifdef CK_ANSIC
  1529. x25intr(char intr)
  1530. #else
  1531. x25intr(intr) char intr;
  1532. #endif /* CK_ANSIC */
  1533. /* x25intr */ {
  1534.     if (send(ttyfd,&intr,1,MSG_OOB) < 0) return(-1);
  1535.     debug(F100,"X.25 intr","",0);
  1536.     return(0);
  1537. }
  1538.  
  1539. /* Reset X.25 virtual circuit */
  1540. int
  1541. #ifdef CK_ANSIC
  1542. x25reset(char cause, char diagn)
  1543. #else
  1544. x25reset(cause, diagn) char cause; char diagn;
  1545. #endif /* CK_ANSIC */
  1546. /* x25reset */ {
  1547.     bzero ((char *)&diag,sizeof(diag));
  1548.     diag.flags   = 0;
  1549.     diag.datalen = 2;
  1550.     diag.data[0] = cause;
  1551.     diag.data[1] = diagn;
  1552.     if (ioctl(ttyfd,X25_WR_CAUSE_DIAG,&diag) < 0)
  1553.       return(-1);
  1554.     debug(F100,"X.25 reset","",0);
  1555.     return(0);
  1556. }
  1557.  
  1558. /* Clear X.25 virtual circuit */
  1559. int
  1560. x25clear() {
  1561.     int i;
  1562.     debug(F100,"X.25 clear","",0);
  1563.     bzero ((char *)&diag,sizeof(diag));
  1564.     diag.flags = (1 << DIAG_TYPE);
  1565.     diag.datalen = 2;
  1566.     diag.data[0] = 0;
  1567.     diag.data[1] = 0;
  1568.     ioctl (ttyfd,X25_WR_CAUSE_DIAG,&diag); /* Send Clear Request */
  1569.     return(ttclos(0));            /* Close socket */
  1570. }
  1571.  
  1572. /* X.25 status */
  1573. int
  1574. x25stat() {
  1575.     if (ttyfd < 0) return (-1);
  1576.     return(0);
  1577. }
  1578.  
  1579. /* Set Q_BIT on */
  1580. VOID
  1581. setqbit() {
  1582.     static int qbiton = 1 << Q_BIT;
  1583.     ioctl (ttyfd,X25_SEND_TYPE,&qbiton);
  1584. }
  1585.  
  1586. /* Set Q_BIT off */
  1587. VOID
  1588. resetqbit() {
  1589.     static int qbitoff = 0;
  1590.     ioctl (ttyfd,X25_SEND_TYPE,&qbitoff);
  1591. }
  1592.  
  1593. /* Read n characters from X.25 circuit into buf */
  1594.  
  1595. int
  1596. x25xin(n,buf) int n; CHAR *buf; {
  1597.     register int x, c;
  1598.     int qpkt;
  1599.  
  1600.     do {
  1601.     x = read(ttyfd,buf,n);
  1602.     if (buf[0] & (1 << Q_BIT)) { /* If Q_BIT packet, process it */
  1603.         /* If return -1 : invitation to clear; -2 : PAD changes */
  1604.         if ((c=qbitpkt(buf+1,x-2)) < 0) return(c);
  1605.         qpkt = 1;
  1606.     } else qpkt = 0;
  1607.     } while (qpkt);
  1608.     if (x > 0) buf[x] = '\0';
  1609.     if (x < 1) x = -1;
  1610.     debug(F101,"x25xin x","",x);
  1611.  
  1612.     return(x);
  1613. }
  1614.  
  1615. #ifdef COMMENT /* NO LONGER NEEDED! */
  1616. /* X.25 read a line */
  1617.  
  1618. int
  1619. #ifdef PARSENSE
  1620. #ifdef CK_ANSIC
  1621. x25inl(CHAR *dest, int max,int timo, CHAR eol, CHAR start)
  1622. #else
  1623. x25inl(dest,max,timo,eol,start) int max,timo; CHAR *dest, eol, start;
  1624. #endif /* CK_ANSIC */
  1625. #else /* not PARSENSE */
  1626. #ifdef CK_ANSIC
  1627. x25inl(CHAR *dest, int max,int timo, CHAR eol)
  1628. #else
  1629. x25inl(dest,max,timo,eol) int max,timo; CHAR *dest, eol;
  1630. #endif /* __SDTC__ */
  1631. #endif /* PARSENSE */
  1632. /* x25inl */ {
  1633.     CHAR *pdest;
  1634.     int pktype, goteol, rest, n;
  1635.     int i, flag = 0;
  1636.     extern int ttprty, ttpflg;
  1637.     int ttpmsk;
  1638.  
  1639.     ttpmsk = (ttprty) ? 0177 : 0377;    /* Set parity stripping mask */
  1640.  
  1641.     debug(F101,"x25inl max","",max);
  1642.     debug(F101,"x25inl eol","",eol);
  1643.     pdest  = dest;
  1644.     rest   = max;
  1645.     goteol = 0;
  1646.     do {
  1647.     n = read(ttyfd,pdest,rest);
  1648.     n--;
  1649.     pktype = *pdest & 0x7f;
  1650.     switch (pktype) {
  1651.       case 1 << Q_BIT:
  1652.         if (qbitpkt(pdest+1,--n) < 0) return(-2);
  1653.         break;
  1654.       default:
  1655.         if (flag == 0) { /* if not in packet, search start */
  1656.         for (i = 1; (i < n) &&
  1657.              !(flag = ((dest[i] & 0x7f) == start));
  1658.              i++);
  1659.         if (flag == 0) { /* not found, discard junk */
  1660.             debug(F101,"x25inl skipping","",n);
  1661.             continue;
  1662.         } else {        /* found, discard junk before start */
  1663.             int k;
  1664.             n = n - i + 1;
  1665.             for (k = 1; k <= n; k++, i++) dest[k] = dest[i];
  1666.         }
  1667.         }
  1668.         for (i = 0; (i < n) && /* search for eol */
  1669.          !(goteol=(((*pdest = *(pdest+1)&ttpmsk)&0x7f)== eol));
  1670.          i++,pdest++);
  1671.         *pdest = '\0';
  1672.         rest -= n;
  1673.     }
  1674.     } while ( (rest > 0) && (!goteol) );
  1675.  
  1676.     if (goteol) {
  1677.     n = max - rest;
  1678.     debug (F111,"x25inl X.25 got",(char *) dest,n);
  1679.     if (timo) ttimoff();
  1680.     if (ttpflg++ == 0 && ttprty == 0) {
  1681.         if ((ttprty = parchk(dest,start,n)) > 0) {
  1682.         int j;
  1683.         debug(F101,"x25inl senses parity","",ttprty);
  1684.         debug(F110,"x25inl packet before",(char *)dest,0);
  1685.         ttpmsk = 0x7f;
  1686.         for (j = 0; j < n; j++)
  1687.           dest[j] &= 0x7f; /* Strip parity from packet */
  1688.         debug(F110,"x25inl packet after ",dest,0);
  1689.         } else {
  1690.         debug(F101,"parchk","",ttprty);
  1691.         if (ttprty < 0) { ttprty = 0; n = -1; }
  1692.         }
  1693.     }
  1694.     ttimoff();
  1695.     return(n);
  1696.     }
  1697.     ttimoff();
  1698.     return(-1);
  1699. }
  1700. #endif /* COMMENT */
  1701. #endif /* SUNX25 */
  1702.  
  1703. #endif /* NETCONN */
  1704.