home *** CD-ROM | disk | FTP | other *** search
/ Columbia Kermit / kermit.zip / k95source / ckcnet.c < prev    next >
C/C++ Source or Header  |  2020-01-01  |  446KB  |  14,338 lines

  1. char *cknetv = "Network support, 8.0.290, 11 Jan 2007";
  2.  
  3. /*  C K C N E T  --  Network support  */
  4.  
  5. /*
  6.   Copyright (C) 1985, 2005,
  7.     Trustees of Columbia University in the City of New York.
  8.     All rights reserved.  See the C-Kermit COPYING.TXT file or the
  9.     copyright text in the ckcmai.c module for disclaimer and permissions.
  10.  
  11.   Copyright (C) 2006, 2007  Secure Endpoints Inc.
  12.  
  13. */
  14.  
  15. /*
  16.   REMINDER: Any changes made to this file that other modules depend must
  17.   also be made to cklnet.c (for VOS) until such time as cklnet.c and this
  18.   module are merged back together.
  19.  
  20.   NOTE TO CONTRIBUTORS: This file, and all the other shared (ckc and cku)
  21.   C-Kermit source files, must be compatible with C preprocessors that support
  22.   only #ifdef, #else, #endif, #define, and #undef.  Please do not use #if,
  23.   logical operators, or other preprocessor features in this module.  Also,
  24.   don't use any ANSI C constructs except within #ifdef CK_ANSIC..#endif.
  25.  
  26.   Authors:
  27.  
  28.   Frank da Cruz (fdc@columbia.edu),
  29.     Columbia University Academic Information Systems, New York City.
  30.   Jeffrey E Altman (jaltman@secure-endpoints.com) -- Primary 
  31.     maintainer/developer since about 1996.
  32.   netopen() routine for TCP/IP originally by Ken Yap, Rochester University
  33.     (ken@cs.rochester.edu) (no longer at that address).
  34.   Missing pieces for Excelan sockets library from William Bader.
  35.   Telnet protocol by Frank da Cruz and Jeffrey Altman.
  36.   Rlogin protocol by Jeffrey E Altman.
  37.   SSL support adapted by Jeffrey E Altman from work done by
  38.     Tim Hudson <tjh@cryptosoft.com> +61 7 32781581
  39.   TLS support by Jeffrey E Altman.
  40.   HTTP support by Jeffrey E Altman.
  41.   TGV MultiNet code by Frank da Cruz.
  42.   MultiNet code adapted to WIN/TCP by Ray Hunter of TWG.
  43.   MultiNet code adapted to DEC TCP/IP by Lee Tibbert of DEC and Frank da Cruz.
  44.   TCP/IP support adapted to IBM TCP/IP 1.2.1,2.0 for OS/2 by Kai Uwe Rommel.
  45.   CMU-OpenVMS/IP modifications by Mike O'Malley, Digital (DEC).
  46.   X.25 support by Marcello Frutig, Catholic University,
  47.     Rio de Janeiro, Brazil (frutig@rnp.impa.br) with fixes from
  48.     Stefaan Eeckels, Eurokom, Luxembourg.
  49.     David Lane added support for Stratus VOS X.25 1996.
  50.     Stephen Riehm added support for IBM AIX X.25 in April 1998.
  51.   Other contributions as indicated in the code.
  52. */
  53. #define CKCNET_C
  54. #include "ckcsym.h"
  55. #include "ckcdeb.h"
  56. #include "ckcker.h"
  57. #include "ckcasc.h"
  58. #ifdef I386IX                           /* Has to come before ckcnet.h in */
  59. #include <errno.h>                      /* this version, but after in others */
  60. #endif /* I386IX */
  61. #include "ckcnet.h"                     /* which includes ckctel.h */
  62. #ifdef CK_SSL
  63. #include "ck_ssl.h"
  64. #endif /* CK_SSL */
  65.  
  66. #ifdef CK_DNS_SRV
  67. #ifdef OS2
  68. #ifdef NT
  69. #include <wshelper.h>
  70. #else /* NT */
  71. /* !Error OS/2 does not support DNS Service Records. */
  72. #endif /* NT */
  73. #else /* OS2 */
  74. #include <arpa/inet.h>
  75. #ifdef USE_NAMESER_COMPAT
  76. #include <arpa/nameser_compat.h>
  77. #endif    /* USE_NAMESER_COMPAT */
  78.  
  79. #ifdef MINIX3
  80. #include <net/gen/resolv.h>
  81. #include <net/gen/nameser.h>
  82. #else
  83. #include <arpa/nameser.h>
  84. #include <resolv.h>
  85. #endif /* MINIX 3 */
  86.  
  87. #ifndef PS2AIX10
  88. #ifndef BSD4
  89. #ifndef I386IX
  90. #ifndef RTAIX
  91. #include <netdb.h>
  92. #endif /* RTAIX */
  93. #endif /* I386IX */
  94. #endif /* BSD4 */
  95. #endif /* PS2AIX10 */
  96. #endif /* OS2 */
  97. #ifndef T_SRV
  98. #define T_SRV 33
  99. #endif /* T_SRV */
  100. #ifndef T_TXT
  101. #define T_TXT 16
  102. #endif /* T_TXT */
  103.  
  104. /* for old Unixes and friends ... */
  105. #ifndef MAXHOSTNAMELEN
  106. #define MAXHOSTNAMELEN 64
  107. #endif /* MAXHOSTNAMELEN */
  108.  
  109. #define MAX_DNS_NAMELEN (15*(MAXHOSTNAMELEN + 1)+1)
  110. #endif /* CK_DNS_SRV */
  111.  
  112. #ifdef NONET
  113. #ifdef TCPIPLIB
  114. #undef TCPIPLIB
  115. #endif /* TCPIPLIB */
  116. #endif /* NONET */
  117.  
  118. #ifndef NOMHHOST
  119. #ifdef datageneral
  120. #define NOMHHOST
  121. #else
  122. #ifdef HPUX5WINTCP
  123. #define NOMHHOST
  124. #endif /* HPUX5WINTCP */
  125. #endif /* datageneral */
  126. #endif /* NOMHHOST */
  127.  
  128. #ifdef INADDRX
  129.   struct in_addr inaddrx;
  130. #endif /* INADDRX */
  131.  
  132. int ttnet = NET_NONE;                   /* Network type */
  133. int ttnproto = NP_DEFAULT;              /* Network virtual terminal protocol */
  134.  
  135. /* 0 = don't lowercase username for Rlogin/Telnet protocol */
  136. /* nonzero = do lowercase it.  Add a SET command if necessary... */
  137. #ifdef VMS
  138. int ck_lcname = 1;
  139. #else
  140. int ck_lcname = 0;
  141. #endif /* VMS */
  142.  
  143. extern int                              /* External variables */
  144.   duplex, debses, seslog, sessft, wasclosed,
  145.   ttyfd, quiet, msgflg, what, nettype, ttmdm;
  146. #ifdef IKSD
  147. extern int inserver;
  148. #endif /* IKSD */
  149.  
  150. char myipaddr[20] = { '\0' };           /* Global copy of my IP address */
  151.  
  152. #ifdef NETCONN
  153. /* Don't need any of this if there is no network support. */
  154.  
  155. #ifndef OS2
  156. /* Current fd-swapping hack is not thread-safe */
  157. #define HTTP_BUFFERING
  158. #endif    /* OS2 */
  159.  
  160. #ifdef HTTP_BUFFERING
  161. #define HTTP_INBUFLEN 8192
  162. static char http_inbuf[HTTP_INBUFLEN];
  163. static int http_bufp = 0, http_count;
  164. #endif    /* HTTP_BUFFERING */
  165.  
  166. /*
  167.   NETLEBUF is (must be) defined for those platforms that call this
  168.   module to do network i/o (e.g. netinc(), nettchk(), etc) rather
  169.   than doing it themselves (ttinc(), ttchk(), etc).  In this case
  170.   the Telnet local-echo buffers and routines are defined and referenced
  171.   here, rather than in the ck?tio.c module.
  172. */
  173. #ifdef NETLEBUF
  174. #define LEBUFSIZ 4096
  175. int ttpush = -1, le_data = 0;           /* These are seen from outside */
  176. static CHAR le_buf[LEBUFSIZ];           /* These are used internally */
  177. static int le_start = 0, le_end = 0;
  178. int tt_push_inited = 0;
  179. #endif /* NETLEBUF */
  180.  
  181. #ifdef CK_SOCKS                         /* SOCKS Internet relay package */
  182. #ifdef CK_SOCKS5                        /* SOCKS 5 */
  183. #define accept  SOCKSaccept
  184. #define bind    SOCKSbind
  185. #define connect SOCKSconnect
  186. #define getsockname SOCKSgetsockname
  187. #define listen SOCKSlisten
  188. #else  /* Not SOCKS 5 */
  189. #define accept  Raccept
  190. #define bind    Rbind
  191. #define connect Rconnect
  192. #define getsockname Rgetsockname
  193. #define listen Rlisten
  194. #endif /* CK_SOCKS5 */
  195. #endif /* CK_SOCKS */
  196.  
  197. #ifdef DEC_TCPIP
  198. #include <time.h>
  199. #include <inet.h>
  200. #endif /* DEC_TCPIP */
  201.  
  202. /* Also see ckcnet.h -- hmmm, why don't we always include inet.h? */
  203.  
  204. #ifdef HPUX
  205. #ifndef HPUX7                           /* HPUX 7.00 doesn't have it */
  206. #include <arpa/inet.h>                  /* For inet_ntoa() prototype */
  207. #endif /* HPUX7 */
  208. #endif /* HPUX */
  209.  
  210. #ifdef CMU_TCPIP
  211. #include <time.h>
  212. #endif /* CMU_TCPIP */
  213.  
  214. #ifndef NODCLTIMEVAL
  215. #ifdef DCLTIMEVAL                       /* UnixWare 7 */
  216. struct timeval {                        /* And define these ourselves. */
  217.     long tv_sec;                        /* (see comments in ckutio.c) */
  218.     long tv_usec;
  219. };
  220. struct timezone {
  221.     int tz_minuteswest;
  222.     int tz_dsttime;
  223. };
  224. #endif /* DCLTIMEVAL */
  225. #endif /* NODCLTIMEVAL */
  226.  
  227. #ifdef WINTCP
  228.  
  229. #include <setjmp.h>
  230. #include <signal.h>
  231. #include <sys/time.h>
  232. /*
  233.   The WIN/TCP code path is the same as that for MultiNet.
  234.   Only the routine names have changed ...
  235. */
  236. #define socket_read     netread
  237. #define socket_ioctl    ioctl
  238. #define socket_write    netwrite
  239. #define socket_close    netclose
  240.  
  241. #ifdef OLD_TWG                         /* some routines have evolved */
  242.         extern int vmserrno, uerrno;
  243. #define socket_errno    uerrno
  244. #define socket_perror   perror         /* which uses errno, not uerrno! */
  245. #else
  246. #define socket_errno    errno
  247. #define socket_perror   win$perror
  248. #endif /* OLD_TWG */
  249.  
  250. #else /* Not WINTCP */
  251.  
  252. #ifdef OSF13
  253. #ifdef CK_ANSIC
  254. #ifdef _NO_PROTO
  255. #undef _NO_PROTO
  256. #endif /* _NO_PROTO */
  257. #endif /* CK_ANSIC */
  258. #endif /* OSF13 */
  259.  
  260. #ifndef I386IX
  261. #include <errno.h>                      /* Already included above */
  262. #endif /* I386IX */
  263.  
  264. #include <signal.h>                     /* Everybody needs this */
  265.  
  266. #ifdef ZILOG                            /* Zilog has different name for this */
  267. #include <setret.h>
  268. #else
  269. #include <setjmp.h>
  270. #endif /* ZILOG */
  271.  
  272. #endif /* WINTCP */
  273.  
  274. #ifdef datageneral                      /* Data General AOS/VS */
  275. #include <:usr:include:vs_tcp_errno.h>
  276. #include <:usr:include:sys:vs_tcp_types.h>
  277. #ifdef SELECT
  278. /*
  279.   NOTE: This can be compiled and linked OK with SELECT defined
  280.   but it doesn't work at all.  Anybody who cares and knows how
  281.   to fix it, feel free.
  282. */
  283. #include <:usr:include:sys:vs_tcp_time.h>
  284. #endif /* SELECT */
  285. #include <:usr:include:sys:socket.h>
  286. #include <:usr:include:netinet:in.h>
  287. #include <:usr:include:netdb.h>
  288. #endif /* datageneral */
  289.  
  290. #ifndef socket_errno
  291. #define socket_errno errno
  292. #endif /* socket_errno */
  293.  
  294. #ifdef TNCODE
  295. extern int tn_deb;
  296. #endif /* TNCODE */
  297.  
  298. int tcp_rdns =                          /* Reverse DNS lookup */
  299. #ifdef DEC_TCPIP_OLD
  300.     SET_OFF                             /* Doesn't seem to work in UCX */
  301. #else
  302.      SET_AUTO
  303. #endif /* DEC_TCPIP */
  304.       ;
  305. #ifdef CK_DNS_SRV
  306. int tcp_dns_srv = SET_OFF;
  307. #endif /* CK_DNS_SRV */
  308.  
  309. _PROTOTYP( char * cmcvtdate, (char *, int) );
  310.  
  311. #ifdef RLOGCODE
  312. _PROTOTYP( int rlog_ctrl, (CHAR *, int) );
  313. _PROTOTYP( static int rlog_oob, (CHAR *, int) );
  314. #ifndef TCPIPLIB
  315. _PROTOTYP( static SIGTYP rlogoobh, ( int ) );
  316. #endif /* TCPIPLIB */
  317. _PROTOTYP( static int rlog_ini, (CHAR *, int,
  318.                                  struct sockaddr_in *,
  319.                                  struct sockaddr_in *) );
  320. int rlog_mode = RL_COOKED;
  321. int rlog_stopped = 0;
  322. int rlog_inband = 0;
  323. #endif /* RLOGCODE */
  324.  
  325. #ifndef NOICP
  326. extern int doconx;                      /* CONNECT-class command active */
  327. #endif /* NOICP */
  328.  
  329. #ifdef IBMX25
  330. /* This variable should probably be generalised for true client/server
  331.  * support - ie: the fd of the listening server, accepted calls should
  332.  * be forked or at least handled via a second fd (for IBM's X.25 -
  333.  * ttyfd always holds the active fd - ie the server becomes inactive
  334.  * as long as a client is connected, and becomes active again when the
  335.  * connection is closed)
  336.  */
  337. int x25serverfd = 0;            /* extern in ckcnet.h */
  338. int x25seqno = 0;               /* Connection sequence number */
  339. int x25lastmsg = -1;            /* A cheapskate's state table */
  340.  
  341. #define X25_CLOSED      0       /* Default state: no connection, no STREAM */
  342. #define X25_SETUP       1       /* X.25 has been set up (no connection) */
  343. #define X25_CONNECTED   2       /* X.25 connection has been established */
  344. int x25_state = X25_CLOSED;     /* Default state */
  345. #endif /* IBMX25 */
  346.  
  347. #ifndef DEBUG
  348. #define deblog 0
  349. #endif /* DEBUG */
  350.  
  351. #ifdef CK_NAWS                          /* Negotiate About Window Size */
  352. #ifdef RLOGCODE
  353. _PROTOTYP( int rlog_naws, (void) );
  354. #endif /* RLOGCODE */
  355. #endif /* CK_NAWS */
  356.  
  357. #ifdef OS2                              /* For terminal type name string */
  358. #include "ckuusr.h"
  359. #ifndef NT
  360. #include <os2.h>
  361. #undef COMMENT
  362. #endif /* NT */
  363. #include "ckocon.h"
  364. extern int tt_type, max_tt;
  365. extern struct tt_info_rec tt_info[];
  366. extern char ttname[];
  367. #else
  368. #ifdef CK_AUTHENTICATION
  369. #include "ckuusr.h"
  370. #endif /* CK_AUTHENTICATION */
  371. #endif /* OS2 */
  372.  
  373. #ifdef NT
  374. extern int winsock_version;
  375. #endif /* NT */
  376.  
  377. #ifdef CK_AUTHENTICATION
  378. #include "ckuath.h"
  379. #endif /* CK_AUTHENTICATION */
  380.  
  381. #include "ckcsig.h"
  382.  
  383. #ifndef OS2                             /* For timeout longjumps */
  384. static ckjmpbuf njbuf;
  385. #endif /* OS2 */
  386.  
  387. #define NAMECPYL 1024                   /* Local copy of hostname */
  388. char namecopy[NAMECPYL];                /* Referenced by ckctel.c */
  389. char namecopy2[NAMECPYL];        /* Referenced by ckctel.c */
  390. #ifndef NOHTTP
  391. char http_host_port[NAMECPYL];          /* orig host/port necessary for http */
  392. char http_ip[20] = { '\0' };            /* ip address of host */
  393. char http_port = 0;
  394. int  http_ssl = 0;
  395. char * http_agent = 0;
  396. int  httpfd = -1;                       /* socket for http connections */
  397. int  http_code = 0;
  398. #define HTTPBUFLEN  1024
  399. char http_reply_str[HTTPBUFLEN] = "";
  400. #endif /* NOHTTP */
  401.  
  402. char ipaddr[20] = { '\0' };             /* Global copy of IP address */
  403. unsigned long myxipaddr = 0L;           /* Ditto as a number */
  404. #endif /* NETCONN */
  405.  
  406. char *tcp_address = NULL;               /* Preferred IP Address */
  407. extern char uidbuf[];                   /* User ID buffer */
  408. extern char pwbuf[];                    /* Password buffer */
  409.  
  410. #ifndef NOHTTP
  411. char * tcp_http_proxy = NULL;           /* Name[:port] of http proxy server */
  412. int    tcp_http_proxy_errno = 0;
  413. char * tcp_http_proxy_user = NULL;
  414. char * tcp_http_proxy_pwd  = NULL;
  415. char * tcp_http_proxy_agent = NULL;
  416. #define HTTPCPYL 1024
  417. static char proxycopy[HTTPCPYL];
  418. #endif /* NOHTTP */
  419.  
  420. #ifdef OS2
  421. extern int tt_rows[], tt_cols[];
  422. extern int tt_status[VNUM];
  423. #else /* OS2 */
  424. extern int tt_rows, tt_cols;            /* Everybody has this */
  425. #endif /* OS2 */
  426.  
  427. extern int cmd_cols, cmd_rows;
  428.  
  429. #ifdef STREAMING                        /* Use blocking writes for streaming */
  430. extern int streaming;
  431. #endif /* STREAMING */
  432.  
  433. #ifdef NT
  434. extern int WSASafeToCancel;
  435. int win95selectbug = 0;                 /* For TCP/IP stacks whose select() */
  436. /* always fails on write requests such as Cisco and Quarterdeck */
  437. #define stricmp _stricmp
  438. #endif /* NT */
  439.  
  440. #ifndef NOTCPOPTS
  441.  
  442. /* Skip all this if NOTCPOPTS specified. */
  443.  
  444. #ifdef SOL_SOCKET
  445.  
  446. #ifdef TCP_NODELAY
  447. int tcp_nodelay = 0;                    /* Nagle algorithm TCP_NODELAY */
  448. #endif /* TCP_NODELAY */
  449.  
  450. #ifdef SO_DONTROUTE
  451. int tcp_dontroute = 0;
  452. #endif /* SO_DONTROUTE */
  453.  
  454. #ifdef SO_LINGER
  455. int tcp_linger  = 0;                    /* SO_LINGER */
  456. int tcp_linger_tmo = 0;                 /* SO_LINGER timeout */
  457. #endif /* SO_LINGER */
  458.  
  459. #ifdef HPUX                             /* But the data structures */
  460. #ifndef HPUX8                           /* needed for linger are not */
  461. #ifndef HPUX9                           /* defined in HP-UX versions */
  462. #ifndef HPUX10                          /* prior to 8.00. */
  463. #ifdef SO_LINGER
  464. #undef SO_LINGER
  465. #endif /* SO_LINGER */
  466. #endif /* HPUX10 */
  467. #endif /* HPUX9 */
  468. #endif /* HPUX8 */
  469. #endif /* HPUX */
  470.  
  471. #ifndef SO_OOBINLINE                    /* Hopefully only HP-UX 7.0 */
  472. #define SO_OOBINLINE 0x0100
  473. #endif /* SO_OOBINLINE */
  474.  
  475. #ifndef TCPSNDBUFSIZ
  476. #ifdef VMS
  477. #ifdef __alpha
  478. #define TCPSNDBUFSIZ 16384
  479. #endif /* __alpha */
  480. #endif /* VMS */
  481. #endif /* TCPSNDBUFSIZ */
  482.  
  483. #ifndef TCPSNDBUFSIZ
  484. #define TCPSNDBUFSIZ -1
  485. #endif /* TCPSNDBUFSIZ */
  486.  
  487. #ifdef SO_SNDBUF
  488. int tcp_sendbuf = TCPSNDBUFSIZ;
  489. #endif /* SO_SNDBUF */
  490.  
  491. #ifdef SO_RCVBUF
  492. int tcp_recvbuf = -1;
  493. #endif /* SO_RCVBUF */
  494.  
  495. #ifdef SO_KEEPALIVE
  496. int tcp_keepalive = 1;
  497. #endif /* SO_KEEPALIVE */
  498.  
  499. #endif /* SOL_SOCKET */
  500. #endif /* NOTCPOPTS */
  501.  
  502. #ifndef NETCONN
  503. /*
  504.   Network support not defined.
  505.   Dummy functions here in case #ifdef's forgotten elsewhere.
  506. */
  507. int                                     /* Open network connection */
  508. netopen(name, lcl, nett) char *name; int *lcl, nett; {
  509.     return(-1);
  510. }
  511. int                                     /* Close network connection */
  512. netclos() {
  513.     return(-1);
  514. }
  515. int                                     /* Check network input buffer */
  516. nettchk() {
  517.     return(-1);
  518. }
  519. int                                     /* Flush network input buffer */
  520. netflui() {
  521.     return(-1);
  522. }
  523. int                                     /* Send network BREAK */
  524. netbreak() {
  525.     return(-1);
  526. }
  527. int                                     /* Input character from network */
  528. netinc(timo) int timo; {
  529.     return(-1);
  530. }
  531. int                                     /* Output character to network */
  532. #ifdef CK_ANSIC
  533. nettoc(CHAR c)
  534. #else
  535. nettoc(c) CHAR c;
  536. #endif /* CK_ANSIC */
  537. /* nettoc */ {
  538.     return(-1);
  539. }
  540. int
  541. nettol(s,n) CHAR *s; int n; {
  542.     return(-1);
  543. }
  544.  
  545. #else /* NETCONN is defined (much of this module...) */
  546.  
  547. #ifdef NETLEBUF
  548. VOID
  549. le_init() {                             /* LocalEchoInit() */
  550.     int i;
  551.     for (i = 0; i < LEBUFSIZ; i++)
  552.       le_buf[i] = '\0';
  553.     le_start = 0;
  554.     le_end = 0;
  555.     le_data = 0;
  556.     tt_push_inited = 1;
  557. }
  558.  
  559. VOID
  560. le_clean() {                            /* LocalEchoCleanup() */
  561.     le_init();
  562.     return;
  563. }
  564.  
  565. int
  566. le_inbuf() {
  567.     int rc = 0;
  568.     if (le_start != le_end) {
  569.         rc = (le_end -
  570.               le_start +
  571.               LEBUFSIZ) % LEBUFSIZ;
  572.     }
  573.     return(rc);
  574. }
  575.  
  576. int
  577. #ifdef CK_ANSIC
  578. le_putchar(CHAR ch)
  579. #else
  580. le_putchar(ch) CHAR ch;
  581. #endif /* CK_ANSIC */
  582. /* le_putchar */ {
  583.     if ((le_start - le_end + LEBUFSIZ)%LEBUFSIZ == 1) {
  584.         debug(F110,"le_putchar","buffer is full",0);
  585.         return(-1);
  586.     }
  587.     le_buf[le_end++] = ch;
  588.     if (le_end == LEBUFSIZ)
  589.       le_end = 0;
  590.     le_data = 1;
  591.     return(0);
  592. }
  593.  
  594. int
  595. #ifdef CK_ANSIC
  596. le_puts(CHAR * s, int n)
  597. #else
  598. le_puts(s,n) CHAR * s; int n;
  599. #endif /* CK_ANSIC */
  600. /* le_puts */ {
  601.     int rc = 0;
  602.     int i = 0;
  603.     CHAR * p = (CHAR *)"le_puts";
  604.     hexdump(p,s,n);
  605.     for (i = 0; i < n; i++)
  606.       rc = le_putchar((char)s[i]);
  607.     debug(F101,"le_puts","",rc);
  608.     return(rc);
  609. }
  610.  
  611. int
  612. #ifdef CK_ANSIC
  613. le_putstr(CHAR * s)
  614. #else
  615. le_putstr(s) CHAR * s;
  616. #endif /* CK_ANSIC */
  617. /* le_puts */ {
  618.     CHAR * p;
  619.     int rc = 0;
  620.     p = (CHAR *)"le_putstr";
  621.     hexdump(p,s,(int)strlen((char *)s));
  622.     for (p = s; *p && !rc; p++)
  623.       rc = le_putchar(*p);
  624.     return(rc);
  625. }
  626.  
  627. int
  628. #ifdef CK_ANSIC
  629. le_getchar(CHAR * pch)
  630. #else /* CK_ANSIC */
  631. le_getchar(pch) CHAR * pch;
  632. #endif /* CK_ANSIC */
  633. /* le_gatchar */ {
  634.     int rc = 0;
  635.     if (le_start != le_end) {
  636.         *pch = le_buf[le_start];
  637.         le_buf[le_start] = 0;
  638.         le_start++;
  639.  
  640.         if (le_start == LEBUFSIZ)
  641.           le_start = 0;
  642.  
  643.         if (le_start == le_end) {
  644.             le_data = 0;
  645.         }
  646.         rc++;
  647.     } else {
  648.         *pch = 0;
  649.     }
  650.     return(rc);
  651. }
  652. #endif /* NETLEBUF */
  653.  
  654. #ifdef VMS
  655. /*
  656.   In edit 190, we moved tn_ini() to be called from within netopen().
  657.   But tn_ini() calls ttol(), and ttol() checks to see if it's a net
  658.   connection, but the flag for that isn't set until after netopen()
  659.   is finished.  Since, in this module, we are always doing network
  660.   output anyway, we just call nettol() directly, instead of going thru
  661.   ttol().  Only needed for VMS, since UNIX, AOS/VS, and VOS can handle
  662.   net connections just like regular connections in ttol(), and OS/2
  663.   has a special routine for this.
  664. */
  665. #define ttol nettol
  666. #endif /* VMS */
  667.  
  668. int tcpsrfd = -1;
  669.  
  670. #ifdef CK_KERBEROS
  671.  
  672. char * krb5_d_principal = NULL;         /* Default principal */
  673. char * krb5_d_instance = NULL;          /* Default instance */
  674. char * krb5_d_realm = NULL;             /* Default realm */
  675. char * krb5_d_cc = NULL;                /* Default credentials cache */
  676. char * krb5_d_srv   = NULL;             /* Default Service */
  677. int    krb5_d_lifetime = 600;           /* Default lifetime (10 hours) */
  678. int    krb5_d_forwardable = 0;          /* creds not forwardable */
  679. int    krb5_d_proxiable = 0;            /* creds not proxiable */
  680. int    krb5_d_renewable = 0;            /* creds not renewable (0 min) */
  681. int    krb5_autoget = 1;                /* Autoget TGTs */
  682. int    krb5_autodel = 0;                /* Auto delete TGTs */
  683. int    krb5_d_getk4 = 0;                /* K5 Kinit gets K4 TGTs */
  684. int    krb5_checkaddrs = 1;             /* Check TGT Addrs */
  685. int    krb5_d_no_addresses = 0;         /* Do not include IP Addresses */
  686. char * krb5_d_addrs[KRB5_NUM_OF_ADDRS+1]={NULL,NULL}; /* Addrs to include */
  687. int    krb5_errno = 0;                  /* Last K5 errno */
  688. char * krb5_errmsg = NULL;              /* Last K5 errmsg */
  689. char * k5_keytab = NULL;
  690.  
  691. char * krb4_d_principal = NULL;         /* Default principal */
  692. char * krb4_d_realm = NULL;             /* Default realm */
  693. char * krb4_d_srv   = NULL;             /* Default Service */
  694. int    krb4_d_lifetime = 600;           /* Default lifetime (10 hours) */
  695. int    krb4_d_preauth = 1;              /* Use preauth requests */
  696. char * krb4_d_instance = NULL;          /* Default instance */
  697. int    krb4_autoget = 1;                /* Autoget TGTs */
  698. int    krb4_autodel = 0;                /* Auto delete TGTs */
  699. int    krb4_checkaddrs = 1;             /* Check TGT Addrs */
  700. char * k4_keytab = NULL;
  701.  
  702. int    krb4_errno = 0;                  /* Last K4 errno */
  703. char * krb4_errmsg = NULL;              /* Last K4 errmsg */
  704.  
  705. struct krb_op_data krb_op = {           /* Operational data structure */
  706.     0, NULL                             /* (version, cachefile) */
  707. };
  708.  
  709. struct krb4_init_data krb4_init = {     /* Kerberos 4 INIT data structure */
  710.     0, NULL, NULL, NULL, NULL
  711. };
  712.  
  713. struct krb5_init_data krb5_init = {     /* Kerberos 5 INIT data structure */
  714.     0, 0, 0, 0, 0, 0, NULL, NULL, NULL, NULL, NULL, NULL, 0, 0, 0,
  715.     { NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
  716.       NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL },
  717.     0
  718. };
  719.  
  720. struct krb5_list_cred_data krb5_lc = {  /* List Credentials data structure */
  721.     0, 0, 0
  722. };
  723.  
  724. int krb_action = -1;                    /* Kerberos action to perform */
  725.  
  726. #ifndef CK_AUTHENTICATION
  727. char *
  728. ck_krb4_getrealm() {
  729.     return("");
  730. }
  731. char *
  732. ck_krb5_getrealm(cc) char * cc; {
  733.     return("");
  734. }
  735. char *
  736. ck_krb4_getprincipal() {
  737.     return("");
  738. }
  739. char *
  740. ck_krb5_getprincipal(cc) char * cc; {
  741.     return("");
  742. }
  743. #endif /* CK_AUTHENTICATION */
  744.  
  745. /*  I N I _ K E R B  --  Initialize Kerberos data  */
  746.  
  747. VOID
  748. ini_kerb() {
  749.     int i;
  750.  
  751.     krb_action = -1;                    /* No action specified */
  752.  
  753.     krb_op.version = 0;                 /* Kerberos version (none) */
  754.     krb_op.cache = NULL;                /* Cache file (none) */
  755.  
  756. /* Kerberos 5 */
  757.  
  758.     krb5_init.forwardable = krb5_d_forwardable; /* Init switch values... */
  759.     krb5_init.proxiable   = krb5_d_proxiable;
  760.     krb5_init.lifetime    = krb5_d_lifetime;
  761.     krb5_init.renew       = 0;
  762.     krb5_init.renewable   = krb5_d_renewable;
  763.     krb5_init.validate    = 0;
  764.     krb5_init.no_addresses = krb5_d_no_addresses;
  765.     krb5_init.getk4       = krb5_d_getk4;
  766.     if (krb5_init.postdate) {
  767.         free(krb5_init.postdate);
  768.         krb5_init.postdate = NULL;
  769.     }
  770.     if (krb5_init.service) {
  771.         free(krb5_init.service);
  772.         krb5_init.service = NULL;
  773.     }
  774.     if (!krb5_d_cc || !krb5_d_cc[0]) {  /* Set default cache */
  775.         char * p;
  776.         p = ck_krb5_get_cc_name();
  777.         makestr(&krb5_d_cc,(p && p[0]) ? p : NULL);
  778.     }
  779.     if (!krb5_d_realm || !krb5_d_realm[0]) { /* Set default realm */
  780.         char * p;
  781.         p = ck_krb5_getrealm(krb5_d_cc);
  782.         makestr(&krb5_d_realm,(p && p[0]) ? p : NULL);
  783.     }
  784.     makestr(&krb5_init.instance,krb5_d_instance);
  785.     makestr(&krb5_init.realm,krb5_d_realm); /* Set realm from default */
  786.     if (krb5_init.password) {
  787.         memset(krb5_init.password,0xFF,strlen(krb5_init.password));
  788.         free(krb5_init.password);
  789.         krb5_init.password = NULL;
  790.     }
  791.     if (!krb5_d_principal) {            /* Default principal */
  792.         /* a Null principal indicates the user should be prompted */
  793.         char * p = ck_krb5_getprincipal(krb5_d_cc);
  794.         if (!p || !(*p))
  795.           p = (char *)uidbuf;           /* Principal = user */
  796.                 makestr(&krb5_d_principal,(p && p[0]) ? p : NULL);
  797.     }
  798.     makestr(&krb5_init.principal,krb5_d_principal);
  799.     for (i = 0; i <= KRB5_NUM_OF_ADDRS; i++) {
  800.         if (krb5_init.addrs[i])
  801.           free(krb5_init.addrs[i]);
  802.         krb5_init.addrs[i] = NULL;
  803.     }
  804.     for (i = 0; i <= KRB5_NUM_OF_ADDRS && krb5_d_addrs[i]; i++) {
  805.         makestr(&krb5_init.addrs[i],krb5_d_addrs[i]);
  806.     }
  807.  
  808.     /* Kerberos 4 */
  809.  
  810.     krb4_init.lifetime = krb4_d_lifetime;
  811.     krb4_init.preauth  = krb4_d_preauth;
  812.     makestr(&krb4_init.instance,krb4_d_instance);
  813.     if (!krb4_d_realm || !krb4_d_realm[0]) {/* Set default realm */
  814.         char * p;
  815.         p = ck_krb4_getrealm();
  816.                 makestr(&krb4_d_realm,(p && p[0]) ? p : NULL);
  817.     }
  818.     makestr(&krb4_init.realm,krb4_d_realm);
  819.     if (krb4_init.password) {
  820.         memset(krb4_init.password,0xFF,strlen(krb4_init.password));
  821.         free(krb4_init.password);
  822.         krb4_init.password = NULL;
  823.     }
  824.     if (!krb4_d_principal) {            /* Default principal */
  825.         /* a Null principal indicates the user should be prompted */
  826.         char * p = ck_krb4_getprincipal();
  827.         if (!p || !(*p))
  828.           p = (char *)uidbuf;           /* Principal = user */
  829.         makestr(&(krb4_d_principal),(p && p[0]) ? p : NULL);
  830.     }
  831.     makestr(&(krb4_init.principal),krb4_d_principal);
  832. }
  833.  
  834. /*  D O A U T H  --  AUTHENTICATE action routine  */
  835.  
  836. int
  837. doauth(cx) int cx; {                    /* AUTHENTICATE action routine */
  838.     int rc = 0;                         /* Return code */
  839.  
  840. #ifdef CK_AUTHENTICATION
  841. #ifdef OS2
  842.     if (!ck_security_loaddll())         /* Load various DLLs */
  843.       return(rc);
  844. #endif /* OS2 */
  845.     if (krb_op.version == 4) {          /* Version = 4 */
  846. #ifdef COMMENT
  847.         sho_auth(AUTHTYPE_KERBEROS_V4);
  848. #endif /* COMMENT */
  849.         if (!ck_krb4_is_installed()) {
  850.             printf("?Kerberos 4 is not installed\n");
  851.             return(0);
  852.         }
  853.         switch (krb_action) {           /* Perform V4 functions */
  854.           case KRB_A_IN:                /* INIT */
  855.             rc |= !(ck_krb4_initTGT(&krb_op,&krb4_init) < 0);
  856.             break;
  857.           case KRB_A_DE:                /* DESTROY */
  858.             rc |= !(ck_krb4_destroy(&krb_op) < 0);
  859.             break;
  860.           case KRB_A_LC:                /* LIST-CREDENTIALS */
  861.             rc |= !(ck_krb4_list_creds(&krb_op) < 0);
  862.             break;
  863.         }
  864.     }
  865.     if (krb_op.version == 5) {          /* V5 functions */
  866. #ifdef COMMENT
  867.         sho_auth(AUTHTYPE_KERBEROS_V5);
  868. #endif /* COMMENT */
  869.         if (!ck_krb5_is_installed()) {
  870.             printf("?Kerberos 5 is not installed\n");
  871.             return(0);
  872.         }
  873.         switch (krb_action) {
  874.           case KRB_A_IN:                /* INIT */
  875.             rc |= !(ck_krb5_initTGT(&krb_op,&krb5_init,
  876.                                      krb5_init.getk4 ? &krb4_init : 0) < 0);
  877.             break;
  878.           case KRB_A_DE:                /* DESTROY */
  879.             rc |= !(ck_krb5_destroy(&krb_op) < 0);
  880.             break;
  881.           case KRB_A_LC:                /* LIST-CREDENTIALS */
  882.             if (krb_op.version == 0)
  883.               printf("\n");
  884.             rc |= !(ck_krb5_list_creds(&krb_op,&krb5_lc) < 0);
  885.             break;
  886.         }
  887.     }
  888. #else
  889. #ifndef NOICP
  890. #ifndef NOSHOW
  891.     rc = sho_auth(0);                   /* Show all */
  892. #endif /* NOSHOW */
  893. #endif /* NOICP */
  894. #endif /* CK_AUTHENTICATION */
  895.     return(rc);
  896. }
  897. #endif /* CK_KERBEROS */
  898.  
  899. #ifdef TCPSOCKET
  900. #ifndef OS2
  901. #ifndef NOLISTEN                        /* For incoming connections */
  902.  
  903. #ifndef INADDR_ANY
  904. #define INADDR_ANY 0
  905. #endif /* INADDR_ANY */
  906.  
  907. _PROTOTYP( int ttbufr, ( VOID ) );
  908. _PROTOTYP( int tcpsrv_open, (char *, int *, int, int ) );
  909.  
  910. static unsigned short tcpsrv_port = 0;
  911.  
  912. #endif /* NOLISTEN */
  913. #endif /* OS2 */
  914.  
  915. static char svcbuf[80];                 /* TCP service string */
  916. static int svcnum = 0;                  /* TCP port number */
  917.  
  918. #endif /* TCPSOCKET */
  919.  
  920. /*
  921.   TCPIPLIB means use separate socket calls for i/o, while on UNIX the
  922.   normal file system calls are used for TCP/IP sockets too.
  923.   Means "DEC_TCPIP or MULTINET or WINTCP or OS2 or BEBOX" (see ckcnet.h),
  924. */
  925.  
  926. #ifdef TCPIPLIB
  927.  
  928. /* For buffered network reads... */
  929. /*
  930.   If the buffering code is written right, it shouldn't matter
  931.   how long this buffer is.
  932. */
  933. #ifdef OS2
  934. #ifdef NT
  935. #define TTIBUFL 64240                   /* 44 * 1460 (MSS) */
  936. #else
  937. #define TTIBUFL 32120                   /* 22 * 1460 (MSS) */
  938. #endif /* NT */
  939. #else /* OS2 */
  940. #define TTIBUFL 8191                    /* Let's use 8K. */
  941. #endif /* OS2 */
  942.  
  943. CHAR ttibuf[TTIBUFL+1];
  944.  
  945. /*
  946.   select() is used in preference to alarm()/signal(), but different systems
  947.   use different forms of select()...
  948. */
  949. #ifndef NOSELECT         /* Option to override BSDSELECT */
  950. #ifdef BELLV10
  951. /*
  952.   Note: Although BELLV10 does have TCP/IP support, and does use the unique
  953.   form of select() that is evident in this module (and in ckutio.c), it does
  954.   not have a sockets library and so we can't build Kermit TCP/IP support for
  955.   it.  For this, somebody would have to write TCP/IP streams code.
  956. */
  957. #define BELLSELECT
  958. #ifndef FD_SETSIZE
  959. #define FD_SETSIZE 128
  960. #endif /* FD_SETSIZE */
  961. #else
  962. #ifdef WINTCP                           /* VMS with Wollongong WIN/TCP */
  963. #ifndef OLD_TWG                         /* TWG 3.2 has only select(read) */
  964. #define BSDSELECT
  965. #endif /* OLD_TWG */
  966. #else
  967. #ifdef CMU_TCPIP                        /* LIBCMU can do select */
  968. #define BSDSELECT
  969. #else
  970. #ifdef DEC_TCPIP
  971. #define BSDSELECT
  972. #else
  973. #ifdef OS2                              /* OS/2 with TCP/IP */
  974. #ifdef NT
  975. #define BSDSELECT
  976. #else /* NT */
  977. #define IBMSELECT
  978. #endif /* NT */
  979. #endif /* OS2 */
  980. #endif /* DEC_TCPIP */
  981. #endif /* CMU_TCPIP */
  982. #endif /* WINTCP */
  983. #endif /* BELLV10 */
  984. #endif /* NOSELECT */
  985. /*
  986.   Others (TGV, TCPware, ...) use alarm()/signal().  The BSDSELECT case does not
  987.   compile at all; the IBMSELECT case compiles and links but crashes at runtime.
  988.   NOTE: If any of these can be converted to select(), they should be for two
  989.   reasons: (1) It's a lot faster; (2) certain sockets libraries do not like
  990.   their socket_read() calls to be interrupted; subsequent socket_read()'s tend
  991.   to fail with EBUSY.  This happened in the UCX case before it was converted
  992.   to use select().
  993. */
  994. #ifndef OS2
  995. #ifndef VMS
  996. static                                  /* These are used in CKVTIO.C */
  997. #endif /* VMS */                        /* And in CKONET.C            */
  998. #endif /* OS2 */
  999. int
  1000.   ttibp = 0,
  1001.   ttibn = 0;
  1002. /*
  1003.   Read bytes from network into internal buffer ttibuf[].
  1004.   To be called when input buffer is empty, i.e. when ttibn == 0.
  1005.  
  1006.   Other network reading routines, like ttinc, ttinl, ttxin, should check the
  1007.   internal buffer first, and call this routine for a refill if necessary.
  1008.  
  1009.   Returns -1 on error, 0 if nothing happens.  When data is read successfully,
  1010.   returns number of bytes read, and sets global ttibn to that number and
  1011.   ttibp (the buffer pointer) to zero.
  1012. */
  1013. _PROTOTYP( int ttbufr, ( VOID ) );
  1014. int
  1015. ttbufr() {                              /* TT Buffer Read */
  1016.     int count;
  1017.  
  1018.     if (ttnet != NET_TCPB)              /* First make sure current net is */
  1019.       return(-1);                       /* TCP/IP; if not, do nothing. */
  1020.  
  1021. #ifdef OS2
  1022.     RequestTCPIPMutex(SEM_INDEFINITE_WAIT);
  1023. #endif /* OS2 */
  1024.  
  1025.     if (ttibn > 0) {                    /* Our internal buffer is not empty, */
  1026. #ifdef OS2
  1027.         ReleaseTCPIPMutex();
  1028. #endif /* OS2 */
  1029.         return(ttibn);                  /* so keep using it. */
  1030.     }
  1031.  
  1032.     if (ttyfd == -1) {                  /* No connection, error */
  1033. #ifdef OS2
  1034.         ReleaseTCPIPMutex();
  1035. #endif /* OS2 */
  1036.         return(-2);
  1037.     }
  1038.  
  1039.     ttibp = 0;                          /* Else reset pointer to beginning */
  1040.  
  1041. #ifdef WINTCP
  1042.     count = 512;                        /* This works for WIN/TCP */
  1043. #else
  1044. #ifdef DEC_TCPIP
  1045.     count = 512;                        /* UCX */
  1046. #else
  1047. #ifdef OS2
  1048.     count = TTIBUFL;
  1049. #else                                   /* Multinet, etc. */
  1050.     count = ttchk();                    /* Check network input buffer, */
  1051.     if (ttibn > 0) {                    /* which can put a char there! */
  1052.         debug(F111,"ttbufr","ttchk() returns",count);
  1053. #ifdef OS2
  1054.         ReleaseTCPIPMutex();
  1055. #endif /* OS2 */
  1056.         return(ttibn);
  1057.     }
  1058.     if (count < 0) {                     /* Read error - connection closed */
  1059. #ifdef OS2
  1060.         ReleaseTCPIPMutex();
  1061. #endif /* OS2 */
  1062.         return(-2);
  1063.     }
  1064.     else if (count > TTIBUFL)           /* Too many to read */
  1065.       count = TTIBUFL;
  1066.     else if (count == 0)                /* None, so force blocking read */
  1067.       count = 1;
  1068. #endif /* OS2 */
  1069. #endif /* DEC_TCPIP */
  1070. #endif /* WINTCP */
  1071.     debug(F101,"ttbufr count 1","",count);
  1072.  
  1073. #ifdef CK_SSL
  1074.     if (ssl_active_flag || tls_active_flag) {
  1075.         int error;
  1076.       ssl_read:
  1077.         if (ssl_active_flag)
  1078.           count = SSL_read(ssl_con, ttibuf, count);
  1079.         else
  1080.           count = SSL_read(tls_con, ttibuf, count);
  1081.         error = SSL_get_error(ssl_active_flag?ssl_con:tls_con,count);
  1082.         switch (error) {
  1083.           case SSL_ERROR_NONE:
  1084.             debug(F111,"ttbufr SSL_ERROR_NONE","count",count);
  1085.             if (count > 0) {
  1086.                 ttibp = 0;              /* Reset buffer pointer. */
  1087.                 ttibn = count;
  1088. #ifdef OS2
  1089.                 ReleaseTCPIPMutex();
  1090. #endif /* OS2 */
  1091.                 return(ttibn);          /* Return buffer count. */
  1092.             } else if (count < 0) {
  1093. #ifdef OS2
  1094.                 ReleaseTCPIPMutex();
  1095. #endif /* OS2 */
  1096.                 return(-1);
  1097.             } else {
  1098.                 netclos();
  1099. #ifdef OS2
  1100.                 ReleaseTCPIPMutex();
  1101. #endif /* OS2 */
  1102.                 return(-2);
  1103.             }
  1104.           case SSL_ERROR_WANT_WRITE:
  1105.             debug(F100,"ttbufr SSL_ERROR_WANT_WRITE","",0);
  1106. #ifdef OS2
  1107.               ReleaseTCPIPMutex();
  1108. #endif /* OS2 */
  1109.             return(-1);
  1110.           case SSL_ERROR_WANT_READ:
  1111.             debug(F100,"ttbufr SSL_ERROR_WANT_READ","",0);
  1112. #ifdef OS2
  1113.             ReleaseTCPIPMutex();
  1114. #endif /* OS2 */
  1115.             return(-1);
  1116.           case SSL_ERROR_SYSCALL:
  1117.               if ( count == 0 ) { /* EOF */
  1118.                   netclos();
  1119. #ifdef OS2
  1120.                   ReleaseTCPIPMutex();
  1121. #endif /* OS2 */
  1122.                   return(-2);
  1123.               } else {
  1124.                   int rc = -1;
  1125. #ifdef NT
  1126.                   int gle = GetLastError();
  1127.                   debug(F111,"ttbufr SSL_ERROR_SYSCALL",
  1128.                          "GetLastError()",gle);
  1129.                   rc = os2socketerror(gle);
  1130.                   if (rc == -1)
  1131.                       rc = -2;
  1132.                   else if ( rc == -2 )
  1133.                       rc = -1;
  1134. #endif /* NT */
  1135. #ifdef OS2
  1136.                   ReleaseTCPIPMutex();
  1137. #endif /* OS2 */
  1138.                   return(rc);
  1139.               }
  1140.           case SSL_ERROR_WANT_X509_LOOKUP:
  1141.             debug(F100,"ttbufr SSL_ERROR_WANT_X509_LOOKUP","",0);
  1142.             netclos();
  1143. #ifdef OS2
  1144.               ReleaseTCPIPMutex();
  1145. #endif /* OS2 */
  1146.             return(-2);
  1147.           case SSL_ERROR_SSL:
  1148.               if (bio_err!=NULL) {
  1149.                   int len;
  1150.                   extern char ssl_err[];
  1151.                   BIO_printf(bio_err,"ttbufr SSL_ERROR_SSL\n");
  1152.                   ERR_print_errors(bio_err);
  1153.                   len = BIO_read(bio_err,ssl_err,SSL_ERR_BFSZ);
  1154.                   ssl_err[len < SSL_ERR_BFSZ ? len : SSL_ERR_BFSZ] = '\0';
  1155.                   debug(F110,"ttbufr SSL_ERROR_SSL",ssl_err,0);
  1156.                   if (ssl_debug_flag)                  
  1157.                       printf(ssl_err);
  1158.               } else if (ssl_debug_flag) {
  1159.                   debug(F100,"ttbufr SSL_ERROR_SSL","",0);
  1160.                   fflush(stderr);
  1161.                   fprintf(stderr,"ttbufr SSL_ERROR_SSL\n");
  1162.                   ERR_print_errors_fp(stderr);
  1163.               }
  1164. #ifdef COMMENT
  1165.           netclos();
  1166. #endif /* COMMENT */
  1167. #ifdef OS2
  1168.               ReleaseTCPIPMutex();
  1169. #endif /* OS2 */
  1170.             return(-2);
  1171.           case SSL_ERROR_ZERO_RETURN:
  1172.             debug(F100,"ttbufr SSL_ERROR_ZERO_RETURN","",0);
  1173.             netclos();
  1174. #ifdef OS2
  1175.               ReleaseTCPIPMutex();
  1176. #endif /* OS2 */
  1177.             return(-2);
  1178.           default:
  1179.               debug(F100,"ttbufr SSL_ERROR_?????","",0);
  1180.               netclos();
  1181. #ifdef OS2
  1182.               ReleaseTCPIPMutex();
  1183. #endif /* OS2 */
  1184.               return(-2);
  1185.           }
  1186.     }
  1187. #endif /* CK_SSL */
  1188.  
  1189. #ifdef COMMENT
  1190. /*
  1191.  This is for nonblocking reads, which we don't do any more.  This code didn't
  1192.  work anyway, in the sense that a broken connection was never sensed.
  1193. */
  1194.     if ((count = socket_read(ttyfd,&ttibuf[ttibp+ttibn],count)) < 1) {
  1195.         if (count == -1 && socket_errno == EWOULDBLOCK) {
  1196.             debug(F100,"ttbufr finds nothing","",0);
  1197. #ifdef OS2
  1198.             ReleaseTCPIPMutex();
  1199. #endif /* OS2 */
  1200.             return(0);
  1201.         } else {
  1202.             debug(F101,"ttbufr socket_read error","",socket_errno);
  1203. #ifdef OS2
  1204.             ReleaseTCPIPMutex();
  1205. #endif /* OS2 */
  1206.             return(-1);
  1207.         }
  1208.  
  1209.     } else if (count == 0) {
  1210.         debug(F100,"ttbufr socket eof","",0);
  1211. #ifdef OS2
  1212.         ReleaseTCPIPMutex();
  1213. #endif /* OS2 */
  1214.         return(-1);
  1215.     }
  1216. #else /* COMMENT */
  1217.  
  1218. /* This is for blocking reads */
  1219.  
  1220. #ifndef VMS
  1221. #ifdef SO_OOBINLINE
  1222.     {
  1223.         int outofband = 0;
  1224. #ifdef BELLSELECT
  1225.         if (select(128, NULL, NULL, efds, 0) > 0 && FD_ISSET(ttyfd, efds))
  1226.           outofband = 1;
  1227. #else
  1228. #ifdef BSDSELECT
  1229.         fd_set efds;
  1230.         struct timeval tv;
  1231.         FD_ZERO(&efds);
  1232.         FD_SET(ttyfd, &efds);
  1233.         tv.tv_sec  = tv.tv_usec = 0L;
  1234.         debug(F100,"Out-of-Band BSDSELECT","",0);
  1235. #ifdef NT
  1236.         WSASafeToCancel = 1;
  1237. #endif /* NT */
  1238.         if (select(FD_SETSIZE, NULL, NULL, &efds, &tv) > 0 &&
  1239.             FD_ISSET(ttyfd, &efds))
  1240.           outofband = 1;
  1241. #ifdef NT
  1242.         WSASafeToCancel = 0;
  1243. #endif /* NT */
  1244. #else /* !BSDSELECT */
  1245. #ifdef IBMSELECT
  1246. /* Is used by OS/2 ... */
  1247. /* ... and it came in handy!  For our TCP/IP layer, it avoids all the fd_set */
  1248. /* and timeval stuff since this is the only place where it is used. */
  1249.         int socket = ttyfd;
  1250.         debug(F100,"Out-of-Band IBMSELECT","",0);
  1251.         if ((select(&socket, 0, 0, 1, 0L) == 1) && (socket == ttyfd))
  1252.           outofband = 1;
  1253. #else /* !IBMSELECT */
  1254. /*
  1255.   If we can't use select(), then we use the regular alarm()/signal()
  1256.   timeout mechanism.
  1257. */
  1258.       debug(F101,"Out-of-Band data not supported","",0);
  1259.       outofband = 0;
  1260.  
  1261. #endif /* IBMSELECT */
  1262. #endif /* BSDSELECT */
  1263. #endif /* BELLSELECT */
  1264.       if (outofband) {
  1265.          /* Get the Urgent Data */
  1266.          /* if OOBINLINE is disabled this should be only a single byte      */
  1267.          /* MS Winsock has a bug in Windows 95.  Extra bytes are delivered  */
  1268.          /* That were never sent.                                           */
  1269. #ifdef OS2
  1270.           RequestTCPIPMutex(SEM_INDEFINITE_WAIT);
  1271. #endif /* OS2 */
  1272.           count = socket_recv(ttyfd,&ttibuf[ttibp+ttibn],count,MSG_OOB);
  1273. #ifdef OS2
  1274.           ReleaseTCPIPMutex();
  1275. #endif /* OS2 */
  1276.           if (count <= 0) {
  1277.               int s_errno = socket_errno;
  1278.               debug(F101, "ttbufr socket_recv MSG_OOB","",count);
  1279.               debug(F101, "ttbufr socket_errno","",s_errno);
  1280. #ifdef OS2ONLY
  1281.               if (count < 0 && (s_errno == 0 || s_errno == 23)) {
  1282.                   /* These appear in OS/2 - don't know why   */
  1283.                   /* ignore it and read as normal data       */
  1284.                   /* and break, then we will attempt to read */
  1285.                   /* the port using normal read() techniques */
  1286.                   debug(F100,"ttbufr handing as in-band data","",0);
  1287.                   count = 1;
  1288.               } else {
  1289.                   netclos();                    /* *** *** */
  1290. #ifdef OS2
  1291.                   ReleaseTCPIPMutex();
  1292. #endif /* OS2 */
  1293.                   return(-2);
  1294.               }
  1295. #else /* OS2ONLY */
  1296.               netclos();                        /* *** *** */
  1297. #ifdef OS2
  1298.               ReleaseTCPIPMutex();
  1299. #endif /* OS2 */
  1300.               return(-2);
  1301. #endif /* OS2ONLY */
  1302.           } else {                      /* we got out-of-band data */
  1303.               hexdump("ttbufr out-of-band chars",&ttibuf[ttibp+ttibn],count);
  1304. #ifdef BETADEBUG
  1305.               bleep(BP_NOTE);
  1306. #endif /* BETADEBUG */
  1307. #ifdef RLOGCODE                         /* blah */
  1308.               if (ttnproto == NP_RLOGIN  ||
  1309.                   ttnproto == NP_K4LOGIN || ttnproto == NP_EK4LOGIN ||
  1310.                   ((ttnproto == NP_K5LOGIN || ttnproto == NP_EK5LOGIN) && 
  1311.                   !rlog_inband)
  1312.                    )
  1313.               {
  1314.                   /*
  1315.                     When urgent data is read with MSG_OOB and not OOBINLINE
  1316.                     then urgent data and normal data are not mixed.  So
  1317.                     treat the entire buffer as urgent data.
  1318.                   */
  1319.                   rlog_oob(&ttibuf[ttibp+ttibn], count);
  1320. #ifdef OS2
  1321.                   ReleaseTCPIPMutex();
  1322. #endif /* OS2 */
  1323.                   return ttbufr();
  1324.               } else
  1325. #endif /* RLOGCODE */ /* blah */
  1326. #ifdef COMMENT
  1327.             /*
  1328.                I haven't written this yet, nor do I know what it should do
  1329.              */
  1330.                 if (ttnproto == NP_TELNET) {
  1331.                     tn_oob();
  1332. #ifdef OS2
  1333.                     ReleaseTCPIPMutex();
  1334. #endif /* OS2 */
  1335.                     return 0;
  1336.               } else
  1337. #endif /* COMMENT */
  1338.               {
  1339.                   /* For any protocols we don't have a special out-of-band  */
  1340.                   /* handler for, just put the bytes in the normal buffer   */
  1341.                   /* and return                                             */
  1342.  
  1343.                   ttibp += 0;       /* Reset buffer pointer. */
  1344.                   ttibn += count;
  1345. #ifdef DEBUG
  1346.                   /* Got some bytes. */
  1347.                   debug(F101,"ttbufr count 2","",count);
  1348.                   if (count > 0)
  1349.                       ttibuf[ttibp+ttibn] = '\0';
  1350.                   debug(F111,"ttbufr ttibuf",ttibuf,ttibp);
  1351. #endif /* DEBUG */
  1352. #ifdef OS2
  1353.                   ReleaseTCPIPMutex();
  1354. #endif /* OS2 */
  1355.                   return(ttibn);    /* Return buffer count. */
  1356.               }
  1357.           }
  1358.       }
  1359.     }
  1360. #endif /* SO_OOBINLINE */
  1361. #endif /* VMS */
  1362.  
  1363.     count = socket_read(ttyfd,&ttibuf[ttibp+ttibn],count);
  1364.     if (count <= 0) {
  1365.         int s_errno = socket_errno;
  1366.         debug(F101,"ttbufr socket_read","",count);
  1367.         debug(F101,"ttbufr socket_errno","",s_errno);
  1368. #ifdef OS2
  1369.         if (count == 0 || os2socketerror(s_errno) < 0) {
  1370.             netclos();
  1371.             ReleaseTCPIPMutex();
  1372.             return(-2);
  1373.         }
  1374.         ReleaseTCPIPMutex();
  1375.         return(-1);
  1376. #else /* OS2 */
  1377.         netclos();                      /* *** *** */
  1378.         return(-2);
  1379. #endif /* OS2 */
  1380.     }
  1381. #endif /* COMMENT */ /* (blocking vs nonblock reads...) */
  1382.     else {
  1383.         ttibp = 0;                      /* Reset buffer pointer. */
  1384.         ttibn += count;
  1385. #ifdef DEBUG
  1386.         debug(F101,"ttbufr count 2","",count); /* Got some bytes. */
  1387.         if (count > 0)
  1388.           ttibuf[ttibp+ttibn] = '\0';
  1389.         debug(F111,"ttbufr ttibuf",&ttibuf[ttibp],ttibn);
  1390. #endif /* DEBUG */
  1391.  
  1392. #ifdef OS2
  1393.         ReleaseTCPIPMutex();
  1394. #endif /* OS2 */
  1395.         return(ttibn);                  /* Return buffer count. */
  1396.     }
  1397. }
  1398. #endif /* TCPIPLIB */
  1399.  
  1400. #ifndef IBMSELECT
  1401. #ifndef BELLSELECT
  1402. #ifndef BSDSELECT               /* Non-TCPIPLIB case */
  1403. #ifdef SELECT
  1404. #define BSDSELECT
  1405. #endif /* SELECT */
  1406. #endif /* BSDSELECT */
  1407. #endif /* BELLSELECT */
  1408. #endif /* IBMSELECT */
  1409.  
  1410. #define TELNET_PORT 23          /* Should do lookup, but it won't change */
  1411. #define RLOGIN_PORT 513
  1412. #define KERMIT_PORT 1649
  1413. #define KLOGIN_PORT 543
  1414. #define EKLOGIN_PORT 2105
  1415.  
  1416. #ifndef NONET
  1417. /*
  1418.   C-Kermit network open/close functions for BSD-sockets.
  1419.   Much of this code shared by SunLink X.25, which also uses the socket library.
  1420. */
  1421.  
  1422. /*  N E T O P N  --  Open a network connection.  */
  1423. /*
  1424.   Call with:
  1425.     name of host (or host:service),
  1426.     lcl - local-mode flag to be set if this function succeeds,
  1427.     network type - value defined in ckunet.h.
  1428. */
  1429. #ifdef TCPSOCKET
  1430. struct hostent *
  1431. #ifdef CK_ANSIC
  1432. ck_copyhostent(struct hostent * h)
  1433. #else /* CK_ANSIC */
  1434. ck_copyhostent(h) struct hostent * h;
  1435. #endif /* CK_ANSIC */
  1436. {
  1437.     /*
  1438.      *  The hostent structure is dynamic in nature.
  1439.      *  struct  hostent {
  1440.      *  char    * h_name;
  1441.      *  char    * * h_aliases;
  1442.      *  short   h_addrtype;
  1443.      *  short   h_length;
  1444.      *  char    * * h_addr_list;
  1445.      *  #define h_addr  h_addr_list[0]
  1446.      */
  1447. #define HOSTENTCNT 5
  1448.     static struct hostent hosts[HOSTENTCNT] = {{NULL,NULL,0,0,NULL},
  1449.                                                {NULL,NULL,0,0,NULL},
  1450.                                                {NULL,NULL,0,0,NULL},
  1451.                                                {NULL,NULL,0,0,NULL},
  1452.                                                {NULL,NULL,0,0,NULL}};
  1453.     static int    next = 0;
  1454.     int    i,cnt;
  1455.     char ** pp;
  1456.  
  1457.     if ( h == NULL )
  1458.         return(NULL);
  1459.  
  1460.     if (next == HOSTENTCNT)
  1461.         next = 0;
  1462.  
  1463.     if ( hosts[next].h_name ) {
  1464.         free(hosts[next].h_name);
  1465.         hosts[next].h_name = NULL;
  1466.     }
  1467.     if ( hosts[next].h_aliases ) {
  1468.         pp = hosts[next].h_aliases;
  1469.         while ( *pp ) {
  1470.             free(*pp);
  1471.             pp++;
  1472.         }
  1473.         free(hosts[next].h_aliases);
  1474.     }
  1475. #ifdef HADDRLIST
  1476.     if ( hosts[next].h_addr_list ) {
  1477.         pp = hosts[next].h_addr_list;
  1478.         while ( *pp ) {
  1479.             free(*pp);
  1480.             pp++;
  1481.         }
  1482.         free(hosts[next].h_addr_list);
  1483.     }
  1484. #endif /* HADDRLIST */
  1485.  
  1486.     makestr(&hosts[next].h_name,h->h_name);
  1487.     if (h->h_aliases) {
  1488.         for ( cnt=0,pp=h->h_aliases; pp && *pp; pp++,cnt++) ;
  1489.         /* The following can give warnings in non-ANSI builds */
  1490.         hosts[next].h_aliases = (char **) malloc(sizeof(char *) * (cnt+1));
  1491.         for ( i=0; i<cnt; i++) {
  1492.             hosts[next].h_aliases[i] = NULL;
  1493.             makestr(&hosts[next].h_aliases[i],h->h_aliases[i]);
  1494.         }
  1495.         hosts[next].h_aliases[i] = NULL;
  1496.     } else
  1497.         hosts[next].h_aliases = NULL;
  1498.  
  1499.     hosts[next].h_addrtype = h->h_addrtype;
  1500.     hosts[next].h_length = h->h_length;
  1501.  
  1502. #ifdef HADDRLIST
  1503. #ifdef h_addr
  1504.     if (h->h_addr_list) {
  1505.         for ( cnt=0,pp=h->h_addr_list; pp && *pp; pp++,cnt++) ;
  1506.         /* The following can give warnings non-ANSI builds */
  1507.         hosts[next].h_addr_list = (char **) malloc(sizeof(char *) * (cnt+1));
  1508.         for ( i=0; i<cnt; i++) {
  1509.             hosts[next].h_addr_list[i] = malloc(h->h_length);
  1510.             bcopy(h->h_addr_list[i],hosts[next].h_addr_list[i],h->h_length);
  1511.         }
  1512.         hosts[next].h_addr_list[i] = NULL;
  1513.     } else
  1514.         hosts[next].h_addr_list = NULL;
  1515. #else
  1516.     bcopy(h->h_addr, &hosts[next].h_addr, h->h_length);
  1517. #endif /* h_addr */
  1518. #else /* HADDRLIST */
  1519.     bcopy(h->h_addr, &hosts[next].h_addr, h->h_length);
  1520. #endif /* HADDRLIST */
  1521.  
  1522.     return(&hosts[next++]);
  1523. }
  1524.  
  1525. #ifdef EXCELAN
  1526. /*
  1527.   Most other BSD sockets implementations define these in header files
  1528.   and libraries.
  1529. */
  1530. struct servent {
  1531.     unsigned short s_port;
  1532. };
  1533.  
  1534. struct hostent {
  1535.     short h_addrtype;
  1536.     struct in_addr h_addr;
  1537.     int h_length;
  1538. };
  1539.  
  1540. struct servent *
  1541. getservbyname(service, connection) char *service,*connection; {
  1542.     static struct servent servrec;
  1543.     int port;
  1544.  
  1545.     port = 0;
  1546.     if (strcmp(service, "telnet") == 0) port = 23;
  1547.     else if (strcmp(service, "smtp") == 0) port = 25;
  1548.     else port = atoi(service);
  1549.  
  1550.     debug(F101,"getservbyname return port ","",port);
  1551.  
  1552.     if (port > 0) {
  1553.         servrec.s_port = htons(port);
  1554.         return(&servrec);
  1555.     }
  1556.     return((struct servent *) NULL);
  1557. }
  1558.  
  1559. struct hostent *
  1560. gethostbyname(hostname) char *hostname; {
  1561.     return((struct hostent *) NULL);
  1562. }
  1563.  
  1564. unsigned long
  1565. inet_addr(name) char *name; {
  1566.     unsigned long addr;
  1567.  
  1568.     addr = rhost(&name);
  1569.     debug(F111,"inet_addr ",name,(int)addr);
  1570.     return(addr);
  1571. }
  1572.  
  1573. char *
  1574. inet_ntoa(in) struct in_addr in; {
  1575.     static char name[80];
  1576.     ckmakxmsg(name, ckuitoa(in.s_net),".",ckuitoa(in.s_host),".",
  1577.                ckuitoa(in.s_lh),".", ckuitoa(in.s_impno));
  1578.     return(name);
  1579. }
  1580. #else
  1581. #ifdef DEC_TCPIP                        /* UCX */
  1582.  
  1583. int ucx_port_bug = 0;                   /* Explained below */
  1584.  
  1585. #ifndef __DECC                          /* VAXC or GCC */
  1586.  
  1587. #define getservbyname my_getservbyname
  1588.  
  1589. #ifdef CK_ANSIC
  1590. globalref int (*C$$GA_UCX_GETSERVBYNAME)();
  1591. extern void C$$TRANSLATE();
  1592. extern void C$$SOCK_TRANSLATE();
  1593. #else
  1594. globalref int (*C$$GA_UCX_GETSERVBYNAME)();
  1595. extern VOID C$$TRANSLATE();
  1596. extern VOID C$$SOCK_TRANSLATE();
  1597. #endif /* CK_ANSIC */
  1598.  
  1599. struct servent *
  1600. my_getservbyname (service, proto) char *service, *proto; {
  1601.     static struct servent sent;
  1602.     struct iosb {
  1603.         union {
  1604.             unsigned long status;
  1605.             unsigned short st[2];
  1606.         } sb;
  1607.         unsigned long spare;
  1608.     } s;
  1609.     struct {
  1610.         struct iosb *s;
  1611.         char *serv;
  1612.         char *prot;
  1613.     } par;
  1614.     unsigned long e;
  1615.     char sbuf[30], pbuf[30];
  1616.     char *p;
  1617.  
  1618.     debug(F111,"UCX getservbyname",service,(int)C$$GA_UCX_GETSERVBYNAME);
  1619.  
  1620.     p = sbuf;
  1621.     ckstrncpy(p, service, 29);
  1622.     while (*p = toupper(*p), *p++) {}
  1623.     p = pbuf;
  1624.     ckstrncpy(p, proto, 29);
  1625.     while (*p = toupper(*p), *p++) {}
  1626.  
  1627.     par.s = &s;
  1628.  
  1629.     par.serv = "";
  1630.     par.prot = "";
  1631.     /* reset file pointer or something like that!?!? */
  1632.     e = (*C$$GA_UCX_GETSERVBYNAME)(&par, &sent, par.s);
  1633.     par.serv = sbuf;
  1634.     par.prot = pbuf;            /* that is don't care */
  1635.     e = (*C$$GA_UCX_GETSERVBYNAME)(&par, &sent, par.s);
  1636.     if ((long)e == -1L)
  1637.       return NULL;
  1638.     if ((e & 1) == 0L) {
  1639.         C$$TRANSLATE(e);
  1640.         return NULL;
  1641.     }
  1642.     if ((s.sb.st[0] & 1) == 0) {
  1643.         C$$SOCK_TRANSLATE(&s.sb.st[0]);
  1644.         return NULL;
  1645.     }
  1646. /*
  1647.   sent.s_port is supposed to be returned by UCX in network byte order.
  1648.   However, UCX 2.0 through 2.0C did not do this; 2.0D and later do it.
  1649.   But there is no way of knowing which UCX version, so we have a user-settable
  1650.   runtime variable.  Note: UCX 2.0 was only for the VAX.
  1651. */
  1652.     debug(F101,"UCX getservbyname port","",sent.s_port);
  1653.     debug(F101,"UCX getservbyname ntohs(port)","",ntohs(sent.s_port));
  1654.     if (ucx_port_bug) {
  1655.         sent.s_port = htons(sent.s_port);
  1656.         debug(F100,"UCX-PORT-BUG ON: swapping bytes","",0);
  1657.         debug(F101,"UCX swapped port","",sent.s_port);
  1658.         debug(F101,"UCX swapped ntohs(port)","",ntohs(sent.s_port));
  1659.     }
  1660.     return &sent;
  1661. }
  1662. #endif /* __DECC */
  1663. #endif /* DEC_TCPIP */
  1664. #endif /* EXCELAN */
  1665. #endif /* TCPSOCKET */
  1666.  
  1667. #ifndef NOTCPOPTS
  1668. #ifndef datageneral
  1669. int
  1670. ck_linger(sock, onoff, timo) int sock; int onoff; int timo; {
  1671. /*
  1672.   The following, from William Bader, turns off the socket linger parameter,
  1673.   which makes a close() block until all data is sent.  "I don't think that
  1674.   disabling linger can ever cause kermit to lose data, but you telnet to a
  1675.   flaky server (or to our modem server when the modem is in use), disabling
  1676.   linger prevents kermit from hanging on the close if you try to exit."
  1677.  
  1678.   Modified by Jeff Altman to be generally useful.
  1679. */
  1680. #ifdef SOL_SOCKET
  1681. #ifdef SO_LINGER
  1682.     struct linger set_linger_opt;
  1683.     struct linger get_linger_opt;
  1684.     SOCKOPT_T x;
  1685.  
  1686. #ifdef IKSD
  1687.     if (!inserver)
  1688. #endif /* IKSD */
  1689.       if (sock == -1 ||
  1690.         nettype != NET_TCPA && nettype != NET_TCPB &&
  1691.         nettype != NET_SSH || ttmdm >= 0) {
  1692.         tcp_linger = onoff;
  1693.         tcp_linger_tmo = timo;
  1694.         return(1);
  1695.     }
  1696.     x = sizeof(get_linger_opt);
  1697.     if (getsockopt(sock, SOL_SOCKET, SO_LINGER,
  1698.                     (char *)&get_linger_opt, &x)) {
  1699.         debug(F111,"TCP ck_linger can't get SO_LINGER",ck_errstr(),errno);
  1700.     } else if (x != sizeof(get_linger_opt)) {
  1701. #ifdef OS2
  1702.         struct _linger16 {
  1703.             short s_linger;
  1704.             short s_onoff;
  1705.         } get_linger_opt16, set_linger_opt16;
  1706.         if ( x == sizeof(get_linger_opt16) ) {
  1707.             debug(F111,"TCP setlinger warning: SO_LINGER","len is 16-bit",x);
  1708.             if (getsockopt(sock,
  1709.                            SOL_SOCKET, SO_LINGER,
  1710.                            (char *)&get_linger_opt16, &x)
  1711.                 ) {
  1712.                 debug(F111,
  1713.                       "TCP ck_linger can't get SO_LINGER",ck_errstr(),errno);
  1714.             } else if (get_linger_opt16.s_onoff != onoff ||
  1715.                        get_linger_opt16.s_linger != timo)
  1716.             {
  1717.                 set_linger_opt16.s_onoff  = onoff;
  1718.                 set_linger_opt16.s_linger = timo;
  1719.                 if (setsockopt(sock,
  1720.                                SOL_SOCKET,
  1721.                                SO_LINGER,
  1722.                                (char *)&set_linger_opt16,
  1723.                                sizeof(set_linger_opt16))
  1724.                     ) {
  1725.                     debug(F111,
  1726.                           "TCP ck_linger can't set SO_LINGER",
  1727.                           ck_errstr(),
  1728.                           errno
  1729.                           );
  1730.                     tcp_linger = get_linger_opt16.s_onoff;
  1731.                     tcp_linger_tmo = get_linger_opt16.s_linger;
  1732.                 } else {
  1733.                     debug(F101,
  1734.                           "TCP ck_linger new SO_LINGER","",
  1735.                           set_linger_opt16.s_onoff);
  1736.                     tcp_linger = set_linger_opt16.s_onoff;
  1737.                     tcp_linger_tmo = set_linger_opt16.s_linger;
  1738.                     return 1;
  1739.                 }
  1740.             } else {
  1741.                 debug(F101,"TCP ck_linger SO_LINGER unchanged","",
  1742.                        get_linger_opt16.s_onoff);
  1743.                 tcp_linger = get_linger_opt16.s_onoff;
  1744.                 tcp_linger_tmo = get_linger_opt16.s_linger;
  1745.                 return 1;
  1746.             }
  1747.             return(0);
  1748.         }
  1749. #endif /* OS2 */
  1750.         debug(F111,"TCP ck_linger error: SO_LINGER","len",x);
  1751.         debug(F111,"TCP ck_linger SO_LINGER",
  1752.               "expected len",sizeof(get_linger_opt));
  1753.         debug(F111,"TCP ck_linger SO_LINGER","linger_opt.l_onoff",
  1754.               get_linger_opt.l_onoff);
  1755.         debug(F111,"TCP linger SO_LINGER","linger_opt.l_linger",
  1756.                get_linger_opt.l_linger);
  1757.     } else if (get_linger_opt.l_onoff != onoff ||
  1758.                get_linger_opt.l_linger != timo) {
  1759.         set_linger_opt.l_onoff  = onoff;
  1760.         set_linger_opt.l_linger = timo;
  1761.         if (setsockopt(sock,
  1762.                        SOL_SOCKET,
  1763.                        SO_LINGER,
  1764.                        (char *)&set_linger_opt,
  1765.                        sizeof(set_linger_opt))) {
  1766.             debug(F111,"TCP ck_linger can't set SO_LINGER",ck_errstr(),errno);
  1767.             tcp_linger = get_linger_opt.l_onoff;
  1768.             tcp_linger_tmo = get_linger_opt.l_linger;
  1769.          } else {
  1770.              debug(F101,
  1771.                    "TCP ck_linger new SO_LINGER",
  1772.                    "",
  1773.                    set_linger_opt.l_onoff
  1774.                    );
  1775.              tcp_linger = set_linger_opt.l_onoff;
  1776.              tcp_linger_tmo = set_linger_opt.l_linger;
  1777.              return 1;
  1778.          }
  1779.     } else {
  1780.         debug(F101,"TCP ck_linger SO_LINGER unchanged","",
  1781.               get_linger_opt.l_onoff);
  1782.         tcp_linger = get_linger_opt.l_onoff;
  1783.         tcp_linger_tmo = get_linger_opt.l_linger;
  1784.         return 1;
  1785.     }
  1786. #else
  1787.     debug(F100,"TCP ck_linger SO_LINGER not defined","",0);
  1788. #endif /* SO_LINGER */
  1789. #else
  1790.     debug(F100,"TCP ck_linger SO_SOCKET not defined","",0);
  1791. #endif /* SOL_SOCKET */
  1792.     return(0);
  1793. }
  1794.  
  1795. int
  1796. sendbuf(sock,size) int sock; int size; {
  1797. /*
  1798.   The following, from William Bader, allows changing of socket buffer sizes,
  1799.   in case that might affect performance.
  1800.  
  1801.   Modified by Jeff Altman to be generally useful.
  1802. */
  1803. #ifdef SOL_SOCKET
  1804. #ifdef SO_SNDBUF
  1805.     int i, j;
  1806.     SOCKOPT_T x;
  1807.  
  1808. #ifdef IKSD
  1809.     if (!inserver)
  1810. #endif /* IKSD */
  1811.       if (sock == -1 ||
  1812.         nettype != NET_TCPA && nettype != NET_TCPB && nettype != NET_SSH
  1813.                 || ttmdm >= 0) {
  1814.         tcp_sendbuf = size;
  1815.         return 1;
  1816.     }
  1817.     x = sizeof(i);
  1818.     if (getsockopt(sock, SOL_SOCKET, SO_SNDBUF, (char *)&i, &x)) {
  1819.         debug(F111,"TCP sendbuf can't get SO_SNDBUF",ck_errstr(),errno);
  1820.     } else if (x != sizeof(i)) {
  1821. #ifdef OS2
  1822.         short i16,j16;
  1823.         if (x == sizeof(i16)) {
  1824.             debug(F111,"TCP sendbuf warning: SO_SNDBUF","len is 16-bit",x);
  1825.             if (getsockopt(sock,
  1826.                            SOL_SOCKET, SO_SNDBUF,
  1827.                            (char *)&i16, &x)
  1828.                 ) {
  1829.                 debug(F111,"TCP sendbuf can't get SO_SNDBUF",
  1830.                       ck_errstr(),errno);
  1831.             } else if (size <= 0) {
  1832.                 tcp_sendbuf = i16;
  1833.                 debug(F101,"TCP sendbuf SO_SNDBUF retrieved","",i16);
  1834.                 return 1;
  1835.             } else if (i16 != size) {
  1836.                 j16 = size;
  1837.                 if (setsockopt(sock,
  1838.                                SOL_SOCKET,
  1839.                                SO_SNDBUF,
  1840.                                (char *)&j16,
  1841.                                sizeof(j16))
  1842.                     ) {
  1843.                     debug(F111,"TCP sendbuf can't set SO_SNDBUF",
  1844.                           ck_errstr(),errno);
  1845.                 } else {
  1846.                     debug(F101,"TCP sendbuf old SO_SNDBUF","",i16);
  1847.                     debug(F101,"TCP sendbuf new SO_SNDBUF","",j16);
  1848.                     tcp_sendbuf = size;
  1849.                     return 1;
  1850.                 }
  1851.             } else {
  1852.                 debug(F101,"TCP sendbuf SO_SNDBUF unchanged","",i16);
  1853.                 tcp_sendbuf = size;
  1854.                 return 1;
  1855.             }
  1856.             return(0);
  1857.         }
  1858. #endif /* OS2 */
  1859.         debug(F111,"TCP sendbuf error: SO_SNDBUF","len",x);
  1860.         debug(F111,"TCP sendbuf SO_SNDBUF","expected len",sizeof(i));
  1861.         debug(F111,"TCP sendbuf SO_SNDBUF","i",i);
  1862.     } else if (size <= 0) {
  1863.         tcp_sendbuf = i;
  1864.         debug(F101,"TCP sendbuf SO_SNDBUF retrieved","",i);
  1865.         return 1;
  1866.     } else if (i != size) {
  1867.         j = size;
  1868.         if (setsockopt(sock, SOL_SOCKET, SO_SNDBUF, (char *)&j, sizeof(j))) {
  1869.             debug(F111,"TCP sendbuf can't set SO_SNDBUF",ck_errstr(),errno);
  1870.             tcp_sendbuf = i;
  1871.         } else {
  1872.             debug(F101,"TCP sendbuf old SO_SNDBUF","",i);
  1873.             debug(F101,"TCP sendbuf new SO_SNDBUF","",j);
  1874.             tcp_sendbuf = size;
  1875.             return 1;
  1876.         }
  1877.     } else {
  1878.         debug(F101,"TCP sendbuf SO_SNDBUF unchanged","",i);
  1879.         tcp_sendbuf = size;
  1880.         return 1;
  1881.     }
  1882. #else
  1883.     debug(F100,"TCP sendbuf SO_SNDBUF not defined","",0);
  1884. #endif /* SO_SNDBUF */
  1885. #else
  1886.     debug(F100,"TCP sendbuf SO_SOCKET not defined","",0);
  1887. #endif /* SOL_SOCKET */
  1888.     return(0);
  1889. }
  1890.  
  1891. int
  1892. recvbuf(sock,size) int sock; int size; {
  1893. /*
  1894.   The following, from William Bader, allows changing of socket buffer sizes,
  1895.   in case that might affect performance.
  1896.  
  1897.   Modified by Jeff Altman to be generally useful.
  1898. */
  1899. #ifdef SOL_SOCKET
  1900. #ifdef SO_RCVBUF
  1901.     int i, j;
  1902.     SOCKOPT_T x;
  1903.  
  1904. #ifdef IKSD
  1905.     if (!inserver)
  1906. #endif /* IKSD */
  1907.       if (sock == -1 ||
  1908.       nettype != NET_TCPA && nettype != NET_TCPB &&
  1909.       nettype != NET_SSH || ttmdm >= 0) {
  1910.         tcp_recvbuf = size;
  1911.         return(1);
  1912.     }
  1913.     x = sizeof(i);
  1914.     if (getsockopt(sock, SOL_SOCKET, SO_RCVBUF, (char *)&i, &x)) {
  1915.         debug(F111,"TCP recvbuf can't get SO_RCVBUF",ck_errstr(),errno);
  1916.     } else if (x != sizeof(i)) {
  1917. #ifdef OS2
  1918.         short i16,j16;
  1919.         if ( x == sizeof(i16) ) {
  1920.             debug(F111,"TCP recvbuf warning: SO_RCVBUF","len is 16-bit",x);
  1921.             if (getsockopt(sock,
  1922.                            SOL_SOCKET, SO_RCVBUF,
  1923.                            (char *)&i16, &x)
  1924.                 ) {
  1925.                 debug(F111,"TCP recvbuf can't get SO_RCVBUF",
  1926.                       ck_errstr(),errno);
  1927.             } else if (size <= 0) {
  1928.                 tcp_recvbuf = i16;
  1929.                 debug(F101,"TCP recvbuf SO_RCVBUF retrieved","",i16);
  1930.                 return 1;
  1931.             } else if (i16 != size) {
  1932.                 j16 = size;
  1933.                 if (setsockopt(sock, SOL_SOCKET, SO_RCVBUF, (char *)&j16,
  1934.                                sizeof(j16))) {
  1935.                     debug(F111,"TCP recvbuf can' set SO_RCVBUF",
  1936.                           ck_errstr(),errno);
  1937.                 } else {
  1938.                     debug(F101,"TCP recvbuf old SO_RCVBUF","",i16);
  1939.                     debug(F101,"TCP recvbuf new SO_RCVBUF","",j16);
  1940.                     tcp_recvbuf = size;
  1941.                     return 1;
  1942.                 }
  1943.             } else {
  1944.                 debug(F101,"TCP recvbuf SO_RCVBUF unchanged","",i16);
  1945.                 tcp_recvbuf = size;
  1946.                 return 1;
  1947.             }
  1948.             return(0);
  1949.         }
  1950. #endif /* OS2 */
  1951.         debug(F111,"TCP recvbuf error: SO_RCVBUF","len",x);
  1952.         debug(F111,"TCP recvbuf SO_RCVBUF","expected len",sizeof(i));
  1953.         debug(F111,"TCP recvbuf SO_RCVBUF","i",i);
  1954.     } else if (size <= 0) {
  1955.         tcp_recvbuf = i;
  1956.         debug(F101,"TCP recvbuf SO_RCVBUF retrieved","",i);
  1957.         return 1;
  1958.     } else if (i != size) {
  1959.         j = size;
  1960.         if (setsockopt(sock, SOL_SOCKET, SO_RCVBUF, (char *)&j, sizeof(j))) {
  1961.             debug(F111,"TCP recvbuf can't set SO_RCVBUF",ck_errstr(),errno);
  1962.             tcp_recvbuf = i;
  1963.         } else {
  1964.             debug(F101,"TCP recvbuf old SO_RCVBUF","",i);
  1965.             debug(F101,"TCP recvbuf new SO_RCVBUF","",j);
  1966.             tcp_recvbuf = size;
  1967.             return 1;
  1968.         }
  1969.     } else {
  1970.         debug(F101,"TCP recvbuf SO_RCVBUF unchanged","",i);
  1971.         tcp_recvbuf = size;
  1972.         return 1;
  1973.     }
  1974. #else
  1975.     debug(F100,"TCP recvbuf SO_RCVBUF not defined","",0);
  1976. #endif /* SO_RCVBUF */
  1977. #else
  1978.     debug(F100,"TCP recvbuf SO_SOCKET not defined","",0);
  1979. #endif /* SOL_SOCKET */
  1980.     return 0;
  1981. }
  1982.  
  1983. int
  1984. keepalive(sock,onoff) int sock; int onoff; {
  1985. #ifdef SOL_SOCKET
  1986. #ifdef SO_KEEPALIVE
  1987.     int get_keepalive_opt;
  1988.     int set_keepalive_opt;
  1989.     SOCKOPT_T x;
  1990.  
  1991.     debug(F111,"TCP keepalive","sock",sock);
  1992.     debug(F111,"TCP keepalive","nettype",nettype);
  1993.     debug(F111,"TCP keepalive","ttmdm",ttmdm);
  1994.  
  1995. #ifdef IKSD
  1996.     if (!inserver)
  1997. #endif /* IKSD */
  1998.       if (sock == -1 ||
  1999.         nettype != NET_TCPA && nettype != NET_TCPB && nettype != NET_SSH
  2000.                 || ttmdm >= 0) {
  2001.         tcp_keepalive = onoff;
  2002.         return 1;
  2003.     }
  2004.     x = sizeof(get_keepalive_opt);
  2005.     if (getsockopt(sock,
  2006.                    SOL_SOCKET, SO_KEEPALIVE, (char *)&get_keepalive_opt, &x)) {
  2007.         debug(F111,"TCP keepalive can't get SO_KEEPALIVE",ck_errstr(),errno);
  2008.     } else if (x != sizeof(get_keepalive_opt)) {
  2009. #ifdef OS2
  2010.         short get_keepalive_opt16;
  2011.         short set_keepalive_opt16;
  2012.         if (x == sizeof(get_keepalive_opt16)) {
  2013.             debug(F111,"TCP keepalive warning: SO_KEEPALIVE",
  2014.                   "len is 16-bit",x);
  2015.             if (getsockopt(sock,
  2016.                            SOL_SOCKET, SO_KEEPALIVE,
  2017.                            (char *)&get_keepalive_opt16, &x)
  2018.                 ) {
  2019.                 debug(F111,
  2020.                       "TCP keepalive can't get SO_KEEPALIVE",
  2021.                       ck_errstr(),
  2022.                       errno
  2023.                       );
  2024.             } else if (get_keepalive_opt16 != onoff) {
  2025.                 set_keepalive_opt16 = onoff;
  2026.                 if (setsockopt(sock,
  2027.                                SOL_SOCKET,
  2028.                                SO_KEEPALIVE,
  2029.                                (char *)&set_keepalive_opt16,
  2030.                                sizeof(set_keepalive_opt16))
  2031.                     ) {
  2032.                     debug(F111,
  2033.                           "TCP keepalive can't clear SO_KEEPALIVE",
  2034.                           ck_errstr(),
  2035.                           errno
  2036.                           );
  2037.                     tcp_keepalive = get_keepalive_opt16;
  2038.                 } else {
  2039.                     debug(F101,
  2040.                           "TCP keepalive new SO_KEEPALIVE","",
  2041.                           set_keepalive_opt16);
  2042.                     tcp_keepalive = set_keepalive_opt16;
  2043.                     return 1;
  2044.                 }
  2045.             } else {
  2046.                 debug(F101,"TCP keepalive SO_KEEPALIVE unchanged","",
  2047.                       get_keepalive_opt16);
  2048.                 tcp_keepalive = onoff;
  2049.                 return 1;
  2050.             }
  2051.             return(0);
  2052.         }
  2053. #endif /* OS2 */
  2054.         debug(F111,"TCP keepalive error: SO_KEEPALIVE","len",x);
  2055.         debug(F111,
  2056.               "TCP keepalive SO_KEEPALIVE",
  2057.               "expected len",
  2058.               sizeof(get_keepalive_opt)
  2059.               );
  2060.         debug(F111,
  2061.               "TCP keepalive SO_KEEPALIVE",
  2062.               "keepalive_opt",
  2063.               get_keepalive_opt
  2064.               );
  2065.     } else if (get_keepalive_opt != onoff) {
  2066.             set_keepalive_opt = onoff;
  2067.             if (setsockopt(sock,
  2068.                             SOL_SOCKET,
  2069.                             SO_KEEPALIVE,
  2070.                             (char *)&set_keepalive_opt,
  2071.                             sizeof(set_keepalive_opt))
  2072.                 ) {
  2073.                 debug(F111,
  2074.                       "TCP keepalive can't clear SO_KEEPALIVE",
  2075.                       ck_errstr(),
  2076.                       errno
  2077.                       );
  2078.                 tcp_keepalive = get_keepalive_opt;
  2079.             } else {
  2080.                 debug(F101,
  2081.                       "TCP keepalive new SO_KEEPALIVE",
  2082.                       "",
  2083.                       set_keepalive_opt
  2084.                       );
  2085.                 tcp_keepalive = onoff;
  2086.                 return 1;
  2087.             }
  2088.         } else {
  2089.             debug(F101,"TCP keepalive SO_KEEPALIVE unchanged",
  2090.                   "",
  2091.                   get_keepalive_opt
  2092.                   );
  2093.             tcp_keepalive = onoff;
  2094.             return 1;
  2095.     }
  2096. #else
  2097.     debug(F100,"TCP keepalive SO_KEEPALIVE not defined","",0);
  2098. #endif /* SO_KEEPALIVE */
  2099. #else
  2100.     debug(F100,"TCP keepalive SO_SOCKET not defined","",0);
  2101. #endif /* SOL_SOCKET */
  2102.     return(0);
  2103. }
  2104.  
  2105. int
  2106. dontroute(sock,onoff) int sock; int onoff; {
  2107. #ifdef SOL_SOCKET
  2108. #ifdef SO_DONTROUTE
  2109.     int get_dontroute_opt;
  2110.     int set_dontroute_opt;
  2111.     SOCKOPT_T x;
  2112.  
  2113. #ifdef IKSD
  2114.     if (!inserver)
  2115. #endif /* IKSD */
  2116.       if (sock == -1 ||
  2117.         nettype != NET_TCPA && nettype != NET_TCPB && nettype != NET_SSH
  2118.                 || ttmdm >= 0) {
  2119.         tcp_dontroute = onoff;
  2120.         return 1;
  2121.     }
  2122.     x = sizeof(get_dontroute_opt);
  2123.     if (getsockopt(sock,
  2124.                    SOL_SOCKET, SO_DONTROUTE, (char *)&get_dontroute_opt, &x)) {
  2125.         debug(F111,"TCP dontroute can't get SO_DONTROUTE",ck_errstr(),errno);
  2126.     } else if (x != sizeof(get_dontroute_opt)) {
  2127. #ifdef OS2
  2128.         short get_dontroute_opt16;
  2129.         short set_dontroute_opt16;
  2130.         if (x == sizeof(get_dontroute_opt16)) {
  2131.             debug(F111,"TCP dontroute warning: SO_DONTROUTE",
  2132.                   "len is 16-bit",x);
  2133.             if (getsockopt(sock,
  2134.                            SOL_SOCKET, SO_DONTROUTE,
  2135.                            (char *)&get_dontroute_opt16, &x)
  2136.                 ) {
  2137.                 debug(F111,
  2138.                       "TCP dontroute can't get SO_DONTROUTE",
  2139.                       ck_errstr(),
  2140.                       errno
  2141.                       );
  2142.             } else if (get_dontroute_opt16 != onoff) {
  2143.                 set_dontroute_opt16 = onoff;
  2144.                 if (setsockopt(sock,
  2145.                                SOL_SOCKET,
  2146.                                SO_DONTROUTE,
  2147.                                (char *)&set_dontroute_opt16,
  2148.                                sizeof(set_dontroute_opt16))
  2149.                     ) {
  2150.                     debug(F111,
  2151.                           "TCP dontroute can't clear SO_DONTROUTE",
  2152.                           ck_errstr(),
  2153.                           errno
  2154.                           );
  2155.                     tcp_dontroute = get_dontroute_opt16;
  2156.                 } else {
  2157.                     debug(F101,
  2158.                           "TCP dontroute new SO_DONTROUTE","",
  2159.                           set_dontroute_opt16);
  2160.                     tcp_dontroute = set_dontroute_opt16;
  2161.                     return 1;
  2162.                 }
  2163.             } else {
  2164.                 debug(F101,"TCP dontroute SO_DONTROUTE unchanged","",
  2165.                       get_dontroute_opt16);
  2166.                 tcp_dontroute = onoff;
  2167.                 return 1;
  2168.             }
  2169.             return(0);
  2170.         }
  2171. #endif /* OS2 */
  2172.         debug(F111,"TCP dontroute error: SO_DONTROUTE","len",x);
  2173.         debug(F111,
  2174.               "TCP dontroute SO_DONTROUTE",
  2175.               "expected len",
  2176.               sizeof(get_dontroute_opt)
  2177.               );
  2178.         debug(F111,
  2179.               "TCP dontroute SO_DONTROUTE",
  2180.               "dontroute_opt",
  2181.               get_dontroute_opt
  2182.               );
  2183.     } else if (get_dontroute_opt != onoff) {
  2184.             set_dontroute_opt = onoff;
  2185.             if (setsockopt(sock,
  2186.                             SOL_SOCKET,
  2187.                             SO_DONTROUTE,
  2188.                             (char *)&set_dontroute_opt,
  2189.                             sizeof(set_dontroute_opt))
  2190.                 ) {
  2191.                 debug(F111,
  2192.                       "TCP dontroute can't clear SO_DONTROUTE",
  2193.                       ck_errstr(),
  2194.                       errno
  2195.                       );
  2196.                 tcp_dontroute = get_dontroute_opt;
  2197.             } else {
  2198.                 debug(F101,
  2199.                       "TCP dontroute new SO_DONTROUTE",
  2200.                       "",
  2201.                       set_dontroute_opt
  2202.                       );
  2203.                 tcp_dontroute = onoff;
  2204.                 return 1;
  2205.             }
  2206.         } else {
  2207.             debug(F101,"TCP dontroute SO_DONTROUTE unchanged",
  2208.                   "",
  2209.                   get_dontroute_opt
  2210.                   );
  2211.             tcp_dontroute = onoff;
  2212.             return 1;
  2213.     }
  2214. #else
  2215.     debug(F100,"TCP dontroute SO_DONTROUTE not defined","",0);
  2216. #endif /* SO_DONTROUTE */
  2217. #else
  2218.     debug(F100,"TCP dontroute SO_SOCKET not defined","",0);
  2219. #endif /* SOL_SOCKET */
  2220.     return(0);
  2221. }
  2222.  
  2223. int
  2224. no_delay(sock,onoff)  int sock; int onoff; {
  2225. #ifdef SOL_SOCKET
  2226. #ifdef TCP_NODELAY
  2227.     int get_nodelay_opt;
  2228.     int set_nodelay_opt;
  2229.     SOCKOPT_T x;
  2230.  
  2231. #ifdef IKSD
  2232.     if (!inserver)
  2233. #endif /* IKSD */
  2234.       if (sock == -1 ||
  2235.         nettype != NET_TCPA && nettype != NET_TCPB && nettype != NET_SSH
  2236.                 || ttmdm >= 0) {
  2237.         tcp_nodelay = onoff;
  2238.         return(1);
  2239.     }
  2240.     x = sizeof(get_nodelay_opt);
  2241.     if (getsockopt(sock,IPPROTO_TCP,TCP_NODELAY,
  2242.                    (char *)&get_nodelay_opt,&x)) {
  2243.         debug(F111,
  2244.               "TCP no_delay can't get TCP_NODELAY",
  2245.               ck_errstr(),
  2246.               errno);
  2247.     } else if (x != sizeof(get_nodelay_opt)) {
  2248. #ifdef OS2
  2249.         short get_nodelay_opt16;
  2250.         short set_nodelay_opt16;
  2251.         if (x == sizeof(get_nodelay_opt16)) {
  2252.             debug(F111,"TCP no_delay warning: TCP_NODELAY","len is 16-bit",x);
  2253.             if (getsockopt(sock,
  2254.                            IPPROTO_TCP, TCP_NODELAY,
  2255.                            (char *)&get_nodelay_opt16, &x)
  2256.                 ) {
  2257.                 debug(F111,
  2258.                       "TCP no_delay can't get TCP_NODELAY",
  2259.                       ck_errstr(),
  2260.                       errno);
  2261.             } else if (get_nodelay_opt16 != onoff) {
  2262.                 set_nodelay_opt16 = onoff;
  2263.                 if (setsockopt(sock,
  2264.                                IPPROTO_TCP,
  2265.                                TCP_NODELAY,
  2266.                                (char *)&set_nodelay_opt16,
  2267.                                sizeof(set_nodelay_opt16))
  2268.                     ) {
  2269.                     debug(F111,
  2270.                           "TCP no_delay can't clear TCP_NODELAY",
  2271.                           ck_errstr(),
  2272.                           errno);
  2273.                     tcp_nodelay = get_nodelay_opt16;
  2274.                 } else {
  2275.                     debug(F101,
  2276.                           "TCP no_delay new TCP_NODELAY",
  2277.                           "",
  2278.                           set_nodelay_opt16);
  2279.                     tcp_nodelay = onoff;
  2280.                     return 1;
  2281.                 }
  2282.             } else {
  2283.                 debug(F101,"TCP no_delay TCP_NODELAY unchanged","",
  2284.                       get_nodelay_opt16);
  2285.                 tcp_nodelay = onoff;
  2286.                 return 1;
  2287.             }
  2288.             return(0);
  2289.         }
  2290. #endif /* OS2 */
  2291.         debug(F111,"TCP no_delay error: TCP_NODELAY","len",x);
  2292.         debug(F111,"TCP no_delay TCP_NODELAY","expected len",
  2293.               sizeof(get_nodelay_opt));
  2294.         debug(F111,"TCP no_delay TCP_NODELAY","nodelay_opt",get_nodelay_opt);
  2295.     } else if (get_nodelay_opt != onoff) {
  2296.         set_nodelay_opt = onoff;
  2297.         if (setsockopt(sock,
  2298.                        IPPROTO_TCP,
  2299.                        TCP_NODELAY,
  2300.                        (char *)&set_nodelay_opt,
  2301.                        sizeof(set_nodelay_opt))) {
  2302.             debug(F111,
  2303.                   "TCP no_delay can't clear TCP_NODELAY",
  2304.                   ck_errstr(),
  2305.                   errno
  2306.                   );
  2307.             tcp_nodelay = get_nodelay_opt;
  2308.         } else {
  2309.             debug(F101,"TCP no_delay new TCP_NODELAY","",set_nodelay_opt);
  2310.             tcp_nodelay = onoff;
  2311.             return 1;
  2312.         }
  2313.     } else {
  2314.         debug(F101,"TCP no_delay TCP_NODELAY unchanged","",get_nodelay_opt);
  2315.         tcp_nodelay = onoff;
  2316.         return(1);
  2317.     }
  2318. #else
  2319.     debug(F100,"TCP no_delay TCP_NODELAY not defined","",0);
  2320. #endif /* TCP_NODELAY */
  2321. #else
  2322.     debug(F100,"TCP no_delay SO_SOCKET not defined","",0);
  2323. #endif /* SOL_SOCKET */
  2324.     return 0;
  2325. }
  2326. #endif /* datageneral */
  2327. #endif /* NOTCPOPTS */
  2328.  
  2329. #ifdef SUNX25
  2330. #ifndef X25_WR_FACILITY
  2331. /* For Solaris 2.3 / SunLink 8.x - see comments in ckcnet.h */
  2332. void
  2333. bzero(s,n) char *s; int n; {
  2334.     memset(s,0,n);
  2335. }
  2336. #endif /* X25_WR_FACILITY */
  2337. #endif /* SUNX25 */
  2338.  
  2339. #ifdef TCPSOCKET
  2340. #ifndef OS2
  2341. #ifndef NOLISTEN
  2342.  
  2343. #ifdef BSDSELECT
  2344. #ifndef VMS
  2345. #ifndef BELLV10
  2346. #ifndef datageneral
  2347. #ifdef hp9000s500                       /* HP-9000/500 HP-U 5.21 */
  2348. #include <time.h>
  2349. #else
  2350.  
  2351. /****** THIS SECTION ADDED BY STEVE RANCE - OS9 NETWORK SERVER
  2352. *       ------------------------------------------------------
  2353. *
  2354. *       Due to OS9's Lack of a select() call, the following seems to be
  2355. *       enough to fool the rest of the code into compiling. The only
  2356. *       effect that I can see is using control L to refresh the status
  2357. *       display gets qued up until some network packets arrive.
  2358. *
  2359. *       This solution is by no means elegant but works enough to be
  2360. *       a (the) solution.
  2361. *
  2362. *       Also with the defines I had specified in my makefile I had to
  2363. *       have an #endif right at the end of the file when compiling.
  2364. *       I did not bother speding time to find out why.
  2365. *
  2366. *       COPTS   = -to=osk -d=OSK -d=TCPSOCKET -d=SELECT -d=VOID=void -d=SIG_V \
  2367. *          -d=DYNAMIC -d=PARSENSE -d=KANJI -d=MYCURSES -d=ZFCDAT \
  2368. *          -d=CK_APC -d=CK_REDIR -d=RENAME -d=CK_TTYFD -d=NOOLDMODEMS \
  2369. *          -d=CK_ANSIC -d=CK_XYZ -tp=68040d -l=netdb.l -l=socklib.l \
  2370. *          -l=termlib.l -l=math.l -l=sys_clib.l
  2371. *
  2372. *       stever@ozemail.com.au
  2373. */
  2374.  
  2375. #ifdef  OSK
  2376. #define BSDSELECT                       /* switch on BSD select code */
  2377. #define FD_SETSIZE 32                   /* Max # of paths in OS9 */
  2378. #define FD_ZERO(p)                      ((*p)=0)
  2379. #define FD_SET(n,b)                     ((*b)|=(1<<(n)))
  2380. #define FD_ISSET(n,b)           1       /* always say data is ready */
  2381. #define select(a,b,c,d,e)       1       /* always say 1 path has data */
  2382. typedef int     fd_set;                 /* keep BSD Code Happy */
  2383. struct timeval {int tv_sec,tv_usec;};   /* keep BSD Code Happy */
  2384.  
  2385. /****** END OF OS9 MODS FROM STEVE RANCE **************************/
  2386. #endif /* OSK */
  2387.  
  2388. #include <sys/time.h>
  2389. #endif /* hp9000s500 */
  2390. #endif /* datageneral */
  2391. #endif /* BELLV10 */
  2392. #endif /* VMS */
  2393. #ifdef SELECT_H
  2394. #include <sys/select.h>
  2395. #endif /* SELECT_H */
  2396. #endif /* BSDSELECT */
  2397.  
  2398. #ifdef SELECT
  2399. #ifdef CK_SCOV5
  2400. #include <sys/select.h>
  2401. #endif /* CK_SCOV5 */
  2402. #endif /* SELECT */
  2403.  
  2404. #ifdef NOTUSED
  2405. /* T C P S O C K E T _ O P E N -- Open a preexisting socket number */
  2406.  
  2407. int
  2408. tcpsocket_open(name,lcl,nett,timo) char * name; int * lcl; int nett; int timo {
  2409.     int on = 1;
  2410.     static struct servent *service, servrec;
  2411.     static struct hostent *host;
  2412.     static struct sockaddr_in saddr;
  2413.     static
  2414. #ifdef UCX50
  2415.       unsigned
  2416. #endif /* UCX50 */
  2417.       int saddrlen;
  2418. #ifdef BSDSELECT
  2419.     fd_set rfds;
  2420.     struct timeval tv;
  2421. #else
  2422. #ifdef BELLSELECT
  2423.     fd_set rfds;
  2424. #else
  2425.     fd_set rfds;
  2426.     fd_set rfds;
  2427.     struct timeval {
  2428.         long tv_sec;
  2429.         long tv_usec;
  2430.     } tv;
  2431. #endif /* BELLSELECT */
  2432. #endif /* BSDSELECT */
  2433.  
  2434.     debug(F101,"tcpsocket_open nett","",nett);
  2435.     *ipaddr = '\0';
  2436.  
  2437.     if (nett != NET_TCPB)
  2438.       return(-1);                       /* BSD socket support */
  2439.  
  2440.     netclos();                          /* Close any previous connection. */
  2441.     ckstrncpy(namecopy, name, NAMECPYL); /* Copy the hostname. */
  2442.     if (ttnproto != NP_TCPRAW &&
  2443.     ttnproto != NP_SSLRAW &&
  2444.     ttnproto != NP_TLSRAW)
  2445.       ttnproto = NP_NONE;               /* No protocol selected yet. */
  2446.     debug(F110,"tcpsocket_open namecopy",namecopy,0);
  2447.  
  2448.     /* Assign the socket number to ttyfd and then fill in tcp structures */
  2449.     ttyfd = atoi(&name[1]);
  2450.     debug(F111,"tcpsocket_open","ttyfd",ttyfd);
  2451.  
  2452. #ifndef NOTCPOPTS
  2453. #ifdef SOL_SOCKET
  2454.     setsockopt(ttyfd, SOL_SOCKET, SO_OOBINLINE,(char *) &on, sizeof on);
  2455.  
  2456. #ifndef datageneral
  2457. #ifdef TCP_NODELAY
  2458.     no_delay(ttyfd,tcp_nodelay);
  2459. #endif /* TCP_NODELAY */
  2460. #ifdef SO_KEEPALIVE
  2461.     keepalive(ttyfd,tcp_keepalive);
  2462. #endif /* SO_KEEPALIVE */
  2463. #ifdef SO_LINGER
  2464.     ck_linger(ttyfd,tcp_linger, tcp_linger_tmo);
  2465. #endif /* SO_LINGER */
  2466. #ifdef SO_SNDBUF
  2467.     sendbuf(ttyfd,tcp_sendbuf);
  2468. #endif /* SO_SNDBUF */
  2469. #ifdef SO_RCVBUF
  2470.     recvbuf(ttyfd,tcp_recvbuf);
  2471. #endif /* SO_RCVBUF */
  2472. #endif /* datageneral */
  2473. #endif /* SOL_SOCKET */
  2474. #endif /* NOTCPOPTS */
  2475.  
  2476. #ifdef NT_TCP_OVERLAPPED
  2477.     OverlappedWriteInit();
  2478.     OverlappedReadInit();
  2479. #endif /* NT_TCP_OVERLAPPED */
  2480.  
  2481.  
  2482.     /* Get the name of the host we are connected to */
  2483.  
  2484.     saddrlen = sizeof(saddr);
  2485.     getpeername(ttyfd,(struct sockaddr *)&saddr,&saddrlen);
  2486.  
  2487.     ckstrncpy(ipaddr,(char *)inet_ntoa(saddr.sin_addr),20);
  2488.  
  2489.     if (tcp_rdns == SET_ON
  2490. #ifdef CK_KERBEROS
  2491.         || tcp_rdns == SET_AUTO &&
  2492.          (ck_krb5_is_installed() || ck_krb4_is_installed())
  2493. #endif /* CK_KERBEROS */
  2494. #ifndef NOHTTP
  2495.           && (tcp_http_proxy == NULL)
  2496. #endif /* NOHTTP */
  2497. #ifdef CK_SSL
  2498.           && !(ssl_only_flag || tls_only_flag)
  2499. #endif /* CK_SSL */
  2500.          ) {                            /* Reverse DNS */
  2501.         if (!quiet) {
  2502.             printf(" Reverse DNS Lookup... ");
  2503.             fflush(stdout);
  2504.         }
  2505.         host = gethostbyaddr((char *)&saddr.sin_addr,4,PF_INET);
  2506.         debug(F110,"tcpsocket_open gethostbyaddr",host ? "OK" : "FAILED",0);
  2507.         if (host) {
  2508.             host = ck_copyhostent(host);
  2509.             debug(F100,"tcpsocket_open gethostbyaddr != NULL","",0);
  2510.             if (!quiet) {
  2511.                 printf("(OK)\n");
  2512.                 fflush(stdout);
  2513.             }
  2514.             ckstrncpy(name, host->h_name, 80);
  2515.             ckstrncat(name, ":", 80);
  2516.             ckstrncat(name,ckuitoa(ntohs(saddr.sin_port)), 80);
  2517.             if (!quiet
  2518. #ifndef NOICP
  2519.                 && !doconx
  2520. #endif /* NOICP */
  2521.                 )
  2522.               printf("%s connected on port %d\n",
  2523.                    host->h_name,
  2524.                    ntohs(saddr.sin_port)
  2525.                    );
  2526.         } else if (!quiet)
  2527.           printf("Failed\n");
  2528.     } else if (!quiet)
  2529.       printf("(OK)\n");
  2530.  
  2531.     if (tcp_rdns != SET_ON || !host) {
  2532.         ckstrncpy(name,ipaddr,80);
  2533.         ckstrncat(name,":",80);
  2534.         ckstrncat(name,ckuitoa(ntohs(saddr.sin_port)),80);
  2535.         if (!quiet
  2536. #ifdef NOICP
  2537.             && !doconx
  2538. #endif /* NOICP */
  2539.             )
  2540.           printf("%s connected on port %d\n",ipaddr,ntohs(saddr.sin_port));
  2541.     }
  2542.     if (!quiet) fflush(stdout);
  2543.     ttnet = nett;                       /* TCP/IP (sockets) network */
  2544.  
  2545. #ifdef RLOGCODE
  2546.     if (ntohs(saddr.sin_port) == 513)
  2547.         ttnproto = NP_LOGIN;
  2548.     else
  2549. #endif /* RLOGCODE */
  2550.     /* Assume the service is TELNET. */
  2551.     if (ttnproto != NP_TCPRAW &&
  2552.             ttnproto != NP_SSLRAW &&
  2553.             ttnproto != NP_TLSRAW)
  2554.         ttnproto = NP_TELNET;           /* Yes, set global flag. */
  2555. #ifdef CK_SECURITY
  2556.     /* Before Initialization Telnet/Rlogin Negotiations Init Kerberos */
  2557.     ck_auth_init((tcp_rdns && host && host->h_name && host->h_name[0]) ?
  2558.                 host->h_name : ipaddr,
  2559.                 ipaddr,
  2560.                 uidbuf,
  2561.                 ttyfd
  2562.                 );
  2563. #endif /* CK_SECURITY */
  2564.     if (tn_ini() < 0)                   /* Start/Reset TELNET negotiations */
  2565.       if (ttchk() < 0)                  /* Did it fail due to connect loss? */
  2566.         return(-1);
  2567.  
  2568.     if (*lcl < 0) *lcl = 1;             /* Local mode. */
  2569.  
  2570.     return(0);                          /* Done. */
  2571. }
  2572. #endif /* NOTUSED */
  2573.  
  2574. /*  T C P S R V _ O P E N  --  Open a TCP/IP Server connection  */
  2575. /*
  2576.   Calling conventions same as ttopen(), except third argument is network
  2577.   type rather than modem type.
  2578. */
  2579. int
  2580. tcpsrv_open(name,lcl,nett,timo) char * name; int * lcl; int nett; int timo; {
  2581.     char *p;
  2582.     int i, x;
  2583.     SOCKOPT_T on = 1;
  2584.     int ready_to_accept = 0;
  2585.     static struct servent *service, *service2, servrec;
  2586.     static struct hostent *host;
  2587.     static struct sockaddr_in saddr;
  2588.     struct sockaddr_in l_addr;
  2589.     GSOCKNAME_T l_slen;
  2590. #ifdef UCX50
  2591.     static u_int saddrlen;
  2592. #else
  2593.     static SOCKOPT_T saddrlen;
  2594. #endif /* UCX50 */
  2595.  
  2596. #ifdef BSDSELECT
  2597.     fd_set rfds;
  2598.     struct timeval tv;
  2599. #else
  2600. #ifdef BELLSELCT
  2601.     fd_set rfds;
  2602. #else
  2603.     fd_set rfds;
  2604.     struct timeval {
  2605.         long tv_sec;
  2606.         long tv_usec;
  2607.     } tv;
  2608. #endif /* BELLSELECT */
  2609. #endif /* BSDSELECT */
  2610. #ifdef CK_SSL
  2611.     int ssl_failed = 0;
  2612. #endif /* CK_SSL */
  2613.  
  2614.     debug(F101,"tcpsrv_open nett","",nett);
  2615.     *ipaddr = '\0';
  2616.  
  2617.     if (nett != NET_TCPB)
  2618.       return(-1);                       /* BSD socket support */
  2619.  
  2620.     netclos();                          /* Close any previous connection. */
  2621.     ckstrncpy(namecopy, name, NAMECPYL); /* Copy the hostname. */
  2622. #ifdef COMMENT
  2623.     /* Don't do this. */
  2624.     if (ttnproto != NP_TCPRAW &&
  2625.     ttnproto != NP_SSLRAW &&
  2626.     ttnproto != NP_TLSRAW)
  2627.       ttnproto = NP_NONE;               /* No protocol selected yet. */
  2628. #endif /* COMMENT */
  2629.     debug(F110,"tcpsrv_open namecopy",namecopy,0);
  2630.  
  2631.     p = namecopy;                       /* Was a service requested? */
  2632.     while (*p != '\0' && *p != ':')
  2633.       p++; /* Look for colon */
  2634.     if (*p == ':') {                    /* Have a colon */
  2635.         *p++ = '\0';                    /* Get service name or number */
  2636.     } else {                            /* Otherwise use kermit */
  2637.         p = "kermit";
  2638.     }
  2639.     debug(F110,"tcpsrv_open service requested",p,0);
  2640.     if (isdigit(*p)) {                  /* Use socket number without lookup */
  2641.         service = &servrec;
  2642.         service->s_port = htons((unsigned short)atoi(p));
  2643.     } else {                            /* Otherwise lookup the service name */
  2644.         service = getservbyname(p, "tcp");
  2645.     }
  2646.     if (!service && !strcmp("kermit",p)) { /* Use Kermit service port */
  2647.         service = &servrec;
  2648.         service->s_port = htons(1649);
  2649.     }
  2650. #ifdef RLOGCODE
  2651.     if (service && !strcmp("login",p) && service->s_port != htons(513)) {
  2652.         fprintf(stderr,
  2653.                 "  Warning: login service on port %d instead of port 513\n",
  2654.                  ntohs(service->s_port));
  2655.         fprintf(stderr, "  Edit SERVICES file if RLOGIN fails to connect.\n");
  2656.         debug(F101,"tcpsrv_open login on port","",ntohs(service->s_port));
  2657.     }
  2658. #endif /* RLOGCODE */
  2659.     if (!service) {
  2660.         fprintf(stderr, "Cannot find port for service: %s\n", p);
  2661.         debug(F111,"tcpsrv_open can't get service",p,errno);
  2662.         errno = 0;                      /* rather than mislead */
  2663.         return(-1);
  2664.     }
  2665.  
  2666.     /* If we currently have a listen active but port has changed then close */
  2667.  
  2668.     debug(F101,"tcpsrv_open checking previous connection","",tcpsrfd);
  2669.     debug(F101,"tcpsrv_open previous tcpsrv_port","",tcpsrv_port);
  2670.     if (tcpsrfd != -1 &&
  2671.         tcpsrv_port != ntohs((unsigned short)service->s_port)) {
  2672.         debug(F100,"tcpsrv_open closing previous connection","",0);
  2673. #ifdef TCPIPLIB
  2674.         socket_close(tcpsrfd);
  2675. #else
  2676.         close(tcpsrfd);
  2677. #endif /* TCPIPLIB */
  2678.         tcpsrfd = -1;
  2679.     }
  2680.     debug(F100,"tcpsrv_open tcpsrfd","",tcpsrfd);
  2681.     if (tcpsrfd == -1) {
  2682.  
  2683.         /* Set up socket structure and get host address */
  2684.  
  2685.         bzero((char *)&saddr, sizeof(saddr));
  2686.         debug(F100,"tcpsrv_open bzero ok","",0);
  2687.         saddr.sin_family = AF_INET;
  2688.         if (tcp_address) {
  2689. #ifdef INADDRX
  2690.             inaddrx = inet_addr(tcp_address);
  2691.             saddr.sin_addr.s_addr = *(unsigned long *)&inaddrx;
  2692. #else
  2693.             saddr.sin_addr.s_addr = inet_addr(tcp_address);
  2694. #endif /* INADDRX */
  2695.         } else
  2696.           saddr.sin_addr.s_addr = INADDR_ANY;
  2697.  
  2698.         /* Get a file descriptor for the connection. */
  2699.  
  2700.         saddr.sin_port = service->s_port;
  2701.         ipaddr[0] = '\0';
  2702.  
  2703.         debug(F100,"tcpsrv_open calling socket","",0);
  2704.         if ((tcpsrfd = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
  2705.             perror("TCP socket error");
  2706.             debug(F101,"tcpsrv_open socket error","",errno);
  2707.             return (-1);
  2708.         }
  2709.         errno = 0;
  2710.  
  2711.         /* Specify the Port may be reused */
  2712.  
  2713.         debug(F100,"tcpsrv_open calling setsockopt","",0);
  2714.         x = setsockopt(tcpsrfd,
  2715.                        SOL_SOCKET,SO_REUSEADDR,(char *)&on,sizeof on);
  2716.         debug(F101,"tcpsrv_open setsockopt","",x);
  2717.  
  2718.        /* Now bind to the socket */
  2719.         printf("\nBinding socket to port %d ...\n",
  2720.                ntohs((unsigned short)service->s_port));
  2721.         if (bind(tcpsrfd, (struct sockaddr *)&saddr, sizeof(saddr)) < 0) {
  2722.             i = errno;                  /* Save error code */
  2723. #ifdef TCPIPLIB
  2724.             socket_close(tcpsrfd);
  2725. #else /* TCPIPLIB */
  2726.             close(tcpsrfd);
  2727. #endif /* TCPIPLIB */
  2728.             tcpsrfd = -1;
  2729.             tcpsrv_port = 0;
  2730.             ttyfd = -1;
  2731.             wasclosed = 1;
  2732.             errno = i;                  /* and report this error */
  2733.             debug(F101,"tcpsrv_open bind errno","",errno);
  2734.             printf("?Unable to bind to socket (errno = %d)\n",errno);
  2735.             return(-1);
  2736.         }
  2737.         debug(F100,"tcpsrv_open bind OK","",0);
  2738.         printf("Listening ...\n");
  2739.         if (listen(tcpsrfd, 15) < 0) {
  2740.             i = errno;                  /* Save error code */
  2741. #ifdef TCPIPLIB
  2742.             socket_close(tcpsrfd);
  2743. #else /* TCPIPLIB */
  2744.             close(tcpsrfd);
  2745. #endif /* TCPIPLIB */
  2746.             tcpsrfd = -1;
  2747.             tcpsrv_port = 0;
  2748.             ttyfd = -1;
  2749.             wasclosed = 1;
  2750.             errno = i;                  /* And report this error */
  2751.             debug(F101,"tcpsrv_open listen errno","",errno);
  2752.             return(-1);
  2753.         }
  2754.         debug(F100,"tcpsrv_open listen OK","",0);
  2755.         tcpsrv_port = ntohs((unsigned short)service->s_port);
  2756.     }
  2757.  
  2758. #ifdef CK_SSL
  2759.     if (ck_ssleay_is_installed()) {
  2760.         if (!ssl_tn_init(SSL_SERVER)) {
  2761.             ssl_failed = 1;
  2762.             if (bio_err!=NULL) {
  2763.                 BIO_printf(bio_err,"do_ssleay_init() failed\n");
  2764.                 ERR_print_errors(bio_err);
  2765.             } else {
  2766.                 fflush(stderr);
  2767.                 fprintf(stderr,"do_ssleay_init() failed\n");
  2768.                 ERR_print_errors_fp(stderr);
  2769.             }
  2770.             if (tls_only_flag || ssl_only_flag) {
  2771. #ifdef TCPIPLIB
  2772.                 socket_close(ttyfd);
  2773.                 socket_close(tcpsrfd);
  2774. #else /* TCPIPLIB */
  2775.                 close(ttyfd);
  2776.                 close(tcpsrfd);
  2777. #endif /* TCPIPLIB */
  2778.                 ttyfd = -1;
  2779.                 wasclosed = 1;
  2780.                 tcpsrfd = -1;
  2781.                 tcpsrv_port = 0;
  2782.                 return(-1);
  2783.             }
  2784.             /* we will continue to accept the connection   */
  2785.             /* without SSL or TLS support unless required. */
  2786.             if ( TELOPT_DEF_S_ME_MODE(TELOPT_START_TLS) != TN_NG_MU )
  2787.                 TELOPT_DEF_S_ME_MODE(TELOPT_START_TLS) = TN_NG_RF;
  2788.             if ( TELOPT_DEF_S_U_MODE(TELOPT_START_TLS) != TN_NG_MU )
  2789.                 TELOPT_DEF_S_U_MODE(TELOPT_START_TLS) = TN_NG_RF;
  2790.             if ( TELOPT_DEF_C_ME_MODE(TELOPT_START_TLS) != TN_NG_MU )
  2791.                 TELOPT_DEF_C_ME_MODE(TELOPT_START_TLS) = TN_NG_RF;
  2792.             if ( TELOPT_DEF_C_U_MODE(TELOPT_START_TLS) != TN_NG_MU )
  2793.                 TELOPT_DEF_C_U_MODE(TELOPT_START_TLS) = TN_NG_RF;
  2794.         }
  2795.     }
  2796. #endif /* CK_SSL */
  2797.  
  2798.     printf("\nWaiting to Accept a TCP/IP connection on port %d ...\n",
  2799.            ntohs((unsigned short)service->s_port));
  2800.     saddrlen = sizeof(saddr);
  2801.  
  2802. #ifdef BSDSELECT
  2803.     tv.tv_sec  = tv.tv_usec = 0L;
  2804.     if (timo < 0)
  2805.       tv.tv_usec = (long) -timo * 10000L;
  2806.     else
  2807.       tv.tv_sec = timo;
  2808.     debug(F101,"tcpsrv_open BSDSELECT","",timo);
  2809. #else
  2810.     debug(F101,"tcpsrv_open not BSDSELECT","",timo);
  2811. #endif /* BSDSELECT */
  2812.  
  2813.     if (timo) {
  2814.         while (!ready_to_accept) {
  2815. #ifdef BSDSELECT
  2816.             FD_ZERO(&rfds);
  2817.             FD_SET(tcpsrfd, &rfds);
  2818.             ready_to_accept =
  2819.               ((select(FD_SETSIZE,
  2820. #ifdef HPUX
  2821. #ifdef HPUX1010
  2822.                        (fd_set *)
  2823. #else
  2824.  
  2825.                        (int *)
  2826. #endif /* HPUX1010 */
  2827. #else
  2828. #ifdef __DECC
  2829.                        (fd_set *)
  2830. #endif /* __DECC */
  2831. #endif /* HPUX */
  2832.                        &rfds, NULL, NULL, &tv) > 0) &&
  2833.                FD_ISSET(tcpsrfd, &rfds));
  2834. #else /* BSDSELECT */
  2835. #ifdef IBMSELECT
  2836. #define ck_sleepint 250
  2837.             ready_to_accept =
  2838.               (select(&tcpsrfd, 1, 0, 0,
  2839.                       timo < 0 ? -timo :
  2840.                       (timo > 0 ? timo * 1000L : ck_sleepint)) == 1
  2841.                );
  2842. #else
  2843. #ifdef BELLSELECT
  2844.             FD_ZERO(rfds);
  2845.             FD_SET(tcpsrfd, rfds);
  2846.             ready_to_accept =
  2847.               ((select(128, rfds, NULL, NULL, timo < 0 ? -timo :
  2848.                       (timo > 0 ? timo * 1000L)) > 0) &&
  2849.                FD_ISSET(tcpsrfd, rfds));
  2850. #else
  2851. /* Try this - what's the worst that can happen... */
  2852.  
  2853.             FD_ZERO(&rfds);
  2854.             FD_SET(tcpsrfd, &rfds);
  2855.             ready_to_accept =
  2856.               ((select(FD_SETSIZE,
  2857.                        (fd_set *) &rfds, NULL, NULL, &tv) > 0) &&
  2858.                FD_ISSET(tcpsrfd, &rfds));
  2859.  
  2860. #endif /* BELLSELECT */
  2861. #endif /* IBMSELECT */
  2862. #endif /* BSDSELECT */
  2863.         }
  2864.     }
  2865.     if (ready_to_accept || timo == 0) {
  2866.         if ((ttyfd = accept(tcpsrfd,
  2867.                             (struct sockaddr *)&saddr,&saddrlen)) < 0) {
  2868.             i = errno;                  /* save error code */
  2869. #ifdef TCPIPLIB
  2870.             socket_close(tcpsrfd);
  2871. #else /* TCPIPLIB */
  2872.             close(tcpsrfd);
  2873. #endif /* TCPIPLIB */
  2874.             ttyfd = -1;
  2875.             wasclosed = 1;
  2876.             tcpsrfd = -1;
  2877.             tcpsrv_port = 0;
  2878.             errno = i;                  /* and report this error */
  2879.             debug(F101,"tcpsrv_open accept errno","",errno);
  2880.             return(-1);
  2881.         }
  2882.         setsockopt(ttyfd, SOL_SOCKET, SO_OOBINLINE,(char *) &on, sizeof on);
  2883.  
  2884. #ifndef NOTCPOPTS
  2885. #ifndef datageneral
  2886. #ifdef SOL_SOCKET
  2887. #ifdef TCP_NODELAY
  2888.         no_delay(ttyfd,tcp_nodelay);
  2889.         debug(F101,"tcpsrv_open no_delay","",tcp_nodelay);
  2890. #endif /* TCP_NODELAY */
  2891. #ifdef SO_KEEPALIVE
  2892.         keepalive(ttyfd,tcp_keepalive);
  2893.         debug(F101,"tcpsrv_open keepalive","",tcp_keepalive);
  2894. #endif /* SO_KEEPALIVE */
  2895. #ifdef SO_LINGER
  2896.         ck_linger(ttyfd,tcp_linger, tcp_linger_tmo);
  2897.         debug(F101,"tcpsrv_open linger","",tcp_linger_tmo);
  2898. #endif /* SO_LINGER */
  2899. #ifdef SO_SNDBUF
  2900.         sendbuf(ttyfd,tcp_sendbuf);
  2901. #endif /* SO_SNDBUF */
  2902. #ifdef SO_RCVBUF
  2903.         recvbuf(ttyfd,tcp_recvbuf);
  2904. #endif /* SO_RCVBUF */
  2905. #endif /* SOL_SOCKET */
  2906. #endif /* datageneral */
  2907. #endif /* NOTCPOPTS */
  2908.  
  2909.         ttnet = nett;                   /* TCP/IP (sockets) network */
  2910.         tcp_incoming = 1;               /* This is an incoming connection */
  2911.         sstelnet = 1;                   /* Do server-side Telnet protocol */
  2912.  
  2913.         /* See if the service is TELNET. */
  2914.         x = (unsigned short)service->s_port;
  2915.         service2 = getservbyname("telnet", "tcp");
  2916.         if (service2 && x == service2->s_port) {
  2917.         if (ttnproto != NP_TCPRAW &&
  2918.          ttnproto != NP_SSLRAW &&
  2919.          ttnproto != NP_TLSRAW) /* Yes and if raw port not requested */
  2920.               ttnproto = NP_TELNET;     /* Set protocol to TELNET. */
  2921.         }
  2922.  
  2923.         ckstrncpy(ipaddr,(char *)inet_ntoa(saddr.sin_addr),20);
  2924.         if (tcp_rdns) {
  2925.             if (!quiet) {
  2926.                 printf(" Reverse DNS Lookup... ");
  2927.                 fflush(stdout);
  2928.             }
  2929.             if (host = gethostbyaddr((char *)&saddr.sin_addr,4,PF_INET)) {
  2930.                 host = ck_copyhostent(host);
  2931.                 debug(F100,"tcpsrv_open gethostbyaddr != NULL","",0);
  2932.                 if (!quiet) {
  2933.                     printf("(OK)\n");
  2934.                     fflush(stdout);
  2935.                 }
  2936.                 name[0] = '*';
  2937.                 ckstrncpy(&name[1],host->h_name,78);
  2938.                 ckstrncat(name,":",80-strlen(name));
  2939.                 ckstrncat(name,p,80-strlen(name));
  2940.                 if (!quiet
  2941. #ifndef NOICP
  2942.                     && !doconx
  2943. #endif /* NOICP */
  2944.                     )
  2945.                   printf("%s connected on port %s\n",host->h_name,p);
  2946.             } else {
  2947.                 if (!quiet) printf("Failed.\n");
  2948.             }
  2949.         } else if (!quiet) printf("(OK)\n");
  2950.  
  2951.         if (!tcp_rdns || !host) {
  2952.             ckstrncpy(name,ipaddr,80);
  2953.             ckstrncat(name,":",80);
  2954.             ckstrncat(name,ckuitoa(ntohs(saddr.sin_port)),80);
  2955.             if (!quiet
  2956. #ifndef NOICP
  2957.                 && !doconx
  2958. #endif /* NOICP */
  2959.                 )
  2960.               printf("%s connected on port %d\n",ipaddr,ntohs(saddr.sin_port));
  2961.         }
  2962.         if (!quiet) fflush(stdout);
  2963.  
  2964. #ifdef CK_SECURITY
  2965.         /* Before Initialization Telnet/Rlogin Negotiations Init Kerberos */
  2966.         ck_auth_init((tcp_rdns && host && host->h_name && host->h_name[0]) ?
  2967.                      (char *)host->h_name : ipaddr,
  2968.                      ipaddr,
  2969.                      uidbuf,
  2970.                      ttyfd
  2971.                      );
  2972. #endif /* CK_SECURITY */
  2973.  
  2974. #ifdef CK_SSL
  2975.         if (ck_ssleay_is_installed() && !ssl_failed) {
  2976.             if (ck_ssl_incoming(ttyfd) < 0) {
  2977. #ifdef TCPIPLIB
  2978.                     socket_close(ttyfd);
  2979.                     socket_close(tcpsrfd);
  2980. #else /* TCPIPLIB */
  2981.                     close(ttyfd);
  2982.                     close(tcpsrfd);
  2983. #endif /* TCPIPLIB */
  2984.                     ttyfd = -1;
  2985.                     wasclosed = 1;
  2986.                     tcpsrfd = -1;
  2987.                     tcpsrv_port = 0;
  2988.                     return(-1);
  2989.             }
  2990.         }
  2991. #endif /* CK_SSL */
  2992.  
  2993. #ifndef datageneral
  2994.         /* Find out our own IP address. */
  2995.         l_slen = sizeof(l_addr);
  2996.         bzero((char *)&l_addr, l_slen);
  2997. #ifndef EXCELAN
  2998.         if (!getsockname(ttyfd, (struct sockaddr *)&l_addr, &l_slen)) {
  2999.             char * s = (char *)inet_ntoa(l_addr.sin_addr);
  3000.             ckstrncpy(myipaddr, s,20);
  3001.             debug(F110,"getsockname",myipaddr,0);
  3002.         }
  3003. #endif /* EXCELAN */
  3004. #endif /* datageneral */
  3005.  
  3006.         if (tn_ini() < 0)               /* Start TELNET negotiations. */
  3007.           if (ttchk() < 0) {            /* Disconnected? */
  3008.               i = errno;                /* save error code */
  3009. #ifdef TCPIPLIB
  3010.               socket_close(tcpsrfd);
  3011. #else /* TCPIPLIB */
  3012.               close(tcpsrfd);
  3013. #endif /* TCPIPLIB */
  3014.               ttyfd = -1;
  3015.               wasclosed = 1;
  3016.               tcpsrfd = -1;
  3017.               tcpsrv_port = 0;
  3018.               errno = i;                /* and report this error */
  3019.               debug(F101,"tcpsrv_open accept errno","",errno);
  3020.               return(-1);
  3021.           }
  3022.         debug(F101,"tcpsrv_open service","",x);
  3023.         if (*lcl < 0)                   /* Set local mode. */
  3024.           *lcl = 1;
  3025.  
  3026. #ifdef CK_KERBEROS
  3027. #ifdef KRB5_U2U
  3028.         if ( ttnproto == NP_K5U2U ) {
  3029.             if (k5_user_to_user_server_auth() != 0) {
  3030.                 i = errno;                /* save error code */
  3031. #ifdef TCPIPLIB
  3032.                 socket_close(tcpsrfd);
  3033. #else /* TCPIPLIB */
  3034.                 close(tcpsrfd);
  3035. #endif /* TCPIPLIB */
  3036.                 ttyfd = -1;
  3037.                 wasclosed = 1;
  3038.                 tcpsrfd = -1;
  3039.                 tcpsrv_port = 0;
  3040.                 errno = i;                /* and report this error */
  3041.                 debug(F101,"tcpsrv_open accept errno","",errno);
  3042.                 return(-1);
  3043.             }
  3044.         }
  3045. #endif /* KRB5_U2U */
  3046. #endif /* CK_KERBEROS */
  3047.         return(0);                      /* Done. */
  3048.     } else {
  3049.         i = errno;                      /* save error code */
  3050. #ifdef TCPIPLIB
  3051.         socket_close(tcpsrfd);
  3052. #else /* TCPIPLIB */
  3053.         close(tcpsrfd);
  3054. #endif /* TCPIPLIB */
  3055.         ttyfd = -1;
  3056.         wasclosed = 1;
  3057.         tcpsrfd = -1;
  3058.         tcpsrv_port = 0;
  3059.         errno = i;                      /* and report this error */
  3060.         debug(F101,"tcpsrv_open accept errno","",errno);
  3061.         return(-1);
  3062.     }
  3063. }
  3064. #endif /* NOLISTEN */
  3065. #endif /* OS2 */
  3066. #endif /* TCPSOCKET */
  3067. #endif /* NONET */
  3068.  
  3069. #ifdef TCPSOCKET
  3070. char *
  3071. ckname2addr(name) char * name;
  3072. {
  3073. #ifdef HPUX5
  3074.     return("");
  3075. #else
  3076.     struct hostent *host;
  3077.  
  3078.     if (name == NULL || *name == '\0')
  3079.         return("");
  3080.  
  3081.     host = gethostbyname(name);
  3082.     if ( host ) {
  3083.         host = ck_copyhostent(host);
  3084.         return(inet_ntoa(*((struct in_addr *) host->h_addr)));
  3085.     }
  3086.     return("");
  3087. #endif /* HPUX5 */
  3088. }
  3089.  
  3090. char *
  3091. ckaddr2name(addr) char * addr;
  3092. {
  3093. #ifdef HPUX5
  3094.     return("");
  3095. #else
  3096.     struct hostent *host;
  3097.     struct in_addr sin_addr;
  3098.  
  3099.     if (addr == NULL || *addr == '\0')
  3100.         return("");
  3101.  
  3102.     sin_addr.s_addr = inet_addr(addr);
  3103.     host = gethostbyaddr((char *)&sin_addr,4,AF_INET);
  3104.     if (host) {
  3105.         host = ck_copyhostent(host);
  3106.         return((char *)host->h_name);
  3107.     }
  3108.     return("");
  3109. #endif /* HPUX5 */
  3110. }
  3111. #endif /* TCPSOCKET */
  3112.  
  3113. unsigned long peerxipaddr = 0L;
  3114.  
  3115. char *
  3116. ckgetpeer() {
  3117. #ifdef TCPSOCKET
  3118.     static char namebuf[256];
  3119.     static struct hostent *host;
  3120.     static struct sockaddr_in saddr;
  3121. #ifdef PTX
  3122.     static size_t saddrlen;
  3123. #else
  3124. #ifdef AIX42
  3125.     /* It's size_t in 4.2 but int in 4.1 and earlier. */
  3126.     /* Note: the 4.2 man page lies; believe socket.h. */
  3127.     static size_t saddrlen;
  3128. #else
  3129. #ifdef UNIXWARE
  3130.     static size_t saddrlen;
  3131. #else  /* UNIXWARE */
  3132. #ifdef DEC_TCPIP
  3133.     static unsigned int saddrlen;
  3134. #else
  3135. #ifdef CK_64BIT
  3136.     static socklen_t saddrlen;
  3137. #else
  3138.     static int saddrlen;
  3139. #endif    /* CK_64BIT */
  3140. #endif /* VMS */
  3141. #endif /* UNIXWARE */
  3142. #endif /* AIX42 */
  3143. #endif /* PTX */
  3144.     saddrlen = sizeof(saddr);
  3145.     if (getpeername(ttyfd,(struct sockaddr *)&saddr,&saddrlen) < 0) {
  3146.         debug(F111,"ckgetpeer failure",ckitoa(ttyfd),errno);
  3147.         return(NULL);
  3148.     }
  3149.     host = gethostbyaddr((char *)&saddr.sin_addr,4,AF_INET);
  3150.     if (host) {
  3151.         host = ck_copyhostent(host);
  3152.         ckstrncpy(namebuf,(char *)host->h_name,80);
  3153.     } else {
  3154.         ckstrncpy(namebuf,(char *)inet_ntoa(saddr.sin_addr),80);
  3155.     }
  3156.     peerxipaddr = ntohl(saddr.sin_addr.s_addr);
  3157.     debug(F111,"ckgetpeer",namebuf,peerxipaddr);
  3158.     return(namebuf);
  3159. #else
  3160.     return(NULL);
  3161. #endif /* TCPSOCKET */
  3162. }
  3163.  
  3164. /* Get fully qualified IP hostname */
  3165.  
  3166. #ifndef NONET
  3167. char *
  3168. #ifdef CK_ANSIC
  3169. ckgetfqhostname(char * name)
  3170. #else
  3171. ckgetfqhostname(name) char * name;
  3172. #endif /* CK_ANSIC */
  3173. {
  3174. #ifdef NOCKGETFQHOST
  3175.  
  3176.     return(name);
  3177.  
  3178. #else /* If the following code dumps core, define NOCKGETFQHOST and rebuild. */
  3179.  
  3180.     static char namebuf[256];
  3181.     struct hostent *host=NULL;
  3182.     struct sockaddr_in r_addr;
  3183.     int i;
  3184.  
  3185.     debug(F110,"ckgetfqhn()",name,0);
  3186.  
  3187.     ckstrncpy(namebuf,name,256);
  3188.     namebuf[255] = '\0';
  3189.     i = ckindex(":",namebuf,0,0,0);
  3190.     if (i)
  3191.       namebuf[i-1] = '\0';
  3192.  
  3193.     bzero((char *)&r_addr, sizeof(r_addr));
  3194.  
  3195.     host = gethostbyname(namebuf);
  3196.     if (host) {
  3197.         host = ck_copyhostent(host);
  3198.         debug(F100,"ckgetfqhn() gethostbyname != NULL","",0);
  3199.         r_addr.sin_family = host->h_addrtype;
  3200. #ifdef HADDRLIST
  3201. #ifdef h_addr
  3202.         /* This is for trying multiple IP addresses - see <netdb.h> */
  3203.         if (!(host->h_addr_list))
  3204.           goto exit_func;
  3205.         bcopy(host->h_addr_list[0],
  3206.               (caddr_t)&r_addr.sin_addr,
  3207.               host->h_length
  3208.               );
  3209. #else
  3210.         bcopy(host->h_addr, (caddr_t)&r_addr.sin_addr, host->h_length);
  3211. #endif /* h_addr */
  3212. #else  /* HADDRLIST */
  3213.         bcopy(host->h_addr, (caddr_t)&r_addr.sin_addr, host->h_length);
  3214. #endif /* HADDRLIST */
  3215. #ifdef COMMENT
  3216. #ifndef EXCELAN
  3217.         debug(F111,"BCOPY","host->h_addr",host->h_addr);
  3218. #endif /* EXCELAN */
  3219.         debug(F111,"BCOPY"," (caddr_t)&r_addr.sin_addr",
  3220.               (caddr_t)&r_addr.sin_addr);
  3221. #endif    /* COMMENT */
  3222.         debug(F111,"BCOPY","host->h_length",host->h_length);
  3223.  
  3224. #ifdef NT
  3225.         /* Windows 95/98 requires a 1 second wait between calls to Microsoft */
  3226.         /* provided DNS functions.  Otherwise, the TTL of the DNS response */
  3227.         /* is ignored. */
  3228.         if (isWin95())
  3229.           sleep(1);
  3230. #endif /* NT */
  3231.         host = gethostbyaddr((char *)&r_addr.sin_addr,4,PF_INET);
  3232.         if (host) {
  3233.             host = ck_copyhostent(host);
  3234.             debug(F100,"ckgetfqhn() gethostbyaddr != NULL","",0);
  3235.             ckstrncpy(namebuf, host->h_name, 256);
  3236.         }
  3237.     }
  3238.  
  3239. #ifdef HADDRLIST
  3240. #ifdef h_addr
  3241.   exit_func:
  3242. #endif /* h_addr */
  3243. #endif /* HADDRLIST */
  3244.  
  3245.     if (i > 0)
  3246.       ckstrncat(namebuf,&name[i-1],256-strlen(namebuf)-strlen(&name[i-1]));
  3247.     debug(F110,"ckgetfqhn()",namebuf,0);
  3248.     return(namebuf);
  3249. #endif /* NOCKGETFQHOST */
  3250. }
  3251.  
  3252. VOID
  3253. #ifdef CK_ANSIC
  3254. setnproto(char * p)
  3255. #else
  3256. setnproto(p) char * p;
  3257. #endif /* CK_ANSIC */
  3258. {
  3259.     if (!isdigit(*p)) {
  3260.         if (!strcmp("kermit",p))
  3261.           ttnproto = NP_KERMIT;
  3262.         else if (!strcmp("telnet",p))
  3263.           ttnproto = NP_TELNET;
  3264.         else if (!strcmp("http",p))
  3265.           ttnproto = NP_TCPRAW;
  3266. #ifdef RLOGCODE
  3267.         else if (!strcmp("login",p))
  3268.           ttnproto = NP_RLOGIN;
  3269. #endif /* RLOGCODE */
  3270. #ifdef CK_SSL
  3271.         /* Commonly used SSL ports (might not be in services file) */
  3272.         else if (!strcmp("https",p)) {
  3273.           ttnproto = NP_SSL_RAW;
  3274.           ssl_only_flag = 1;
  3275.         } else if (!strcmp("ssl-telnet",p)) {
  3276.           ttnproto = NP_TELNET;
  3277.           ssl_only_flag = 1;
  3278.         } else if (!strcmp("telnets",p)) {
  3279.           ttnproto = NP_TELNET;
  3280.           ssl_only_flag = 1;
  3281.         }
  3282. #endif /* CK_SSL */
  3283. #ifdef CK_KERBEROS
  3284. #ifdef RLOGCODE
  3285.         else if (!strcmp("klogin",p)) {
  3286.             if (ck_krb5_is_installed())
  3287.               ttnproto = NP_K5LOGIN;
  3288.             else if (ck_krb4_is_installed())
  3289.               ttnproto = NP_K4LOGIN;
  3290.             else
  3291.               ttnproto = NP_RLOGIN;
  3292.         } else if (!strcmp("eklogin",p)) {
  3293.             if (ck_krb5_is_installed())
  3294.               ttnproto = NP_EK5LOGIN;
  3295.             else if (ck_krb4_is_installed())
  3296.               ttnproto = NP_EK4LOGIN;
  3297.             else
  3298.               ttnproto = NP_RLOGIN;
  3299.         }
  3300. #endif /* RLOGCODE */
  3301. #endif /* CK_KERBEROS */
  3302.         else
  3303.           ttnproto = NP_NONE;
  3304.     } else {
  3305.         switch (atoi(p)) {
  3306.           case 23:                      /* Telnet */
  3307.             ttnproto = NP_TELNET;
  3308.             break;
  3309.           case 513:
  3310.             ttnproto = NP_RLOGIN;
  3311.             break;
  3312.           case 1649:
  3313.             ttnproto = NP_KERMIT;
  3314.             break;
  3315. #ifdef CK_SSL
  3316.           case 443:
  3317.             ttnproto = NP_SSL_RAW;
  3318.             ssl_only_flag = 1;
  3319.             break;
  3320.           case 151:
  3321.           case 992:
  3322.             ttnproto = NP_TELNET;
  3323.             ssl_only_flag = 1;
  3324.             break;
  3325. #endif /* CK_SSL */
  3326. #ifdef CK_KERBEROS
  3327.           case 543:
  3328.             if (ck_krb5_is_installed())
  3329.               ttnproto = NP_K5LOGIN;
  3330.             else if (ck_krb4_is_installed())
  3331.               ttnproto = NP_K4LOGIN;
  3332.             else
  3333.               ttnproto = NP_RLOGIN;
  3334.             break;
  3335.           case 2105:
  3336.             if (ck_krb5_is_installed())
  3337.               ttnproto = NP_EK5LOGIN;
  3338.             else if (ck_krb4_is_installed())
  3339.               ttnproto = NP_EK4LOGIN;
  3340.             else
  3341.               ttnproto = NP_RLOGIN;
  3342.             break;
  3343. #endif /* CK_KERBEROS */
  3344.           case 80:                      /* HTTP */
  3345.             ttnproto = NP_TCPRAW;
  3346.             break;
  3347.           default:
  3348.             ttnproto = NP_NONE;
  3349.             break;
  3350.         }
  3351.     }
  3352. }
  3353.  
  3354. /* ckgetservice() is used to determine the port number for a given */
  3355. /* service taking into account the use of DNS SRV records.         */
  3356.  
  3357. static struct servent servrec;
  3358. static struct servent *
  3359. ckgetservice(hostname, servicename, ip, iplen)
  3360.     char *hostname; char * servicename; char * ip; int iplen;
  3361. {
  3362.     struct servent * service = NULL;
  3363. #ifdef CK_DNS_SRV
  3364.     struct sockaddr * dns_addrs = NULL;
  3365.     int dns_naddrs = 0;
  3366. #endif /* CK_DNS_SRV */
  3367.  
  3368.     if (isdigit(*servicename)) {        /* Use socket number without lookup */
  3369.         service = &servrec;
  3370.         service->s_port = htons((unsigned short)atoi(servicename));
  3371.     } else {                            /* Otherwise lookup the service name */
  3372. #ifdef CK_DNS_SRV
  3373.         if (tcp_dns_srv && !quiet) {
  3374.             printf(" DNS SRV Lookup... ");
  3375.             fflush(stdout);
  3376.         }
  3377.         if (tcp_dns_srv &&
  3378.             locate_srv_dns(hostname,
  3379.                            servicename,
  3380.                            "tcp",
  3381.                            &dns_addrs,
  3382.                            &dns_naddrs
  3383.                            )
  3384.             ) {
  3385.             /* Use the first one.  Eventually we should cycle through all */
  3386.             /* the returned IP addresses and port numbers. */
  3387.             struct sockaddr_in *sin = NULL;
  3388. #ifdef BETADEBUG
  3389.             int i;
  3390.             printf("\r\n");
  3391.             for ( i=0;i<dns_naddrs;i++ ) {
  3392.                 sin = (struct sockaddr_in *) &dns_addrs[i];
  3393.                 printf("dns_addrs[%d] = %s %d\r\n", i,
  3394.                         (char *)inet_ntoa(sin->sin_addr),
  3395.                         ntohs(sin->sin_port));
  3396.             }
  3397. #endif /* BETADEBUG */
  3398.             sin = (struct sockaddr_in *) &dns_addrs[0];
  3399.             if ( ip && iplen > 0 )
  3400.                 ckstrncpy(ip,(char *)inet_ntoa(sin->sin_addr),iplen);
  3401.             service = &servrec;
  3402.             service->s_port = sin->sin_port;
  3403.  
  3404.             free(dns_addrs);
  3405.             dns_addrs = NULL;
  3406.             dns_naddrs = 0;
  3407.         } else
  3408. #endif /* CK_DNS_SRV */
  3409.             service = getservbyname(servicename, "tcp");
  3410.     }
  3411.     if (!service) {
  3412.         if (!ckstrcmp("kermit",servicename,-1,0)) { /* Kermit service port */
  3413.             service = &servrec;
  3414.             service->s_port = htons(1649);
  3415.         } else if (!ckstrcmp("telnet",servicename,-1,0)) { /* Telnet port */
  3416.             service = &servrec;
  3417.             service->s_port = htons(23);
  3418.         } else if (!ckstrcmp("http",servicename,-1,0)) {
  3419.             service = &servrec;
  3420.             service->s_port = htons(80);
  3421.         }
  3422. #ifdef RLOGCODE
  3423.         else if (!ckstrcmp("login",servicename,-1,0)) {
  3424.             service = &servrec;
  3425.             service->s_port = htons(513);
  3426.         }
  3427. #endif /* RLOGCODE */
  3428. #ifdef CK_SSL
  3429.         /* Commonly used SSL ports (might not be in services file) */
  3430.         else if (!ckstrcmp("https",servicename,-1,0)) {
  3431.             service = &servrec;
  3432.             service->s_port = htons(443);
  3433.         } else if (!ckstrcmp("ssl-telnet",servicename,-1,0)) {
  3434.             service = &servrec;
  3435.             service->s_port = htons(151);
  3436.         } else if (!ckstrcmp("telnets",servicename,-1,0)) {
  3437.             service = &servrec;
  3438.             service->s_port = htons(992);
  3439.         }
  3440. #endif /* CK_SSL */
  3441. #ifdef CK_KERBEROS
  3442. #ifdef RLOGCODE
  3443.         else if (!ckstrcmp("klogin",servicename,-1,0)) {
  3444.             service = &servrec;
  3445.             service->s_port = htons(543);
  3446.         } else if (!ckstrcmp("eklogin",servicename,-1,0)) {
  3447.             service = &servrec;
  3448.             service->s_port = htons(2105);
  3449.         }
  3450. #endif /* RLOGCODE */
  3451. #endif /* CK_KERBEROS */
  3452.     }
  3453.     return(service);
  3454. }
  3455.  
  3456. /*  N E T O P E N  --  Open a network connection  */
  3457. /*
  3458.   Calling conventions same as ttopen(), except third argument is network
  3459.   type rather than modem type.  Designed to be called from within ttopen.
  3460. */
  3461.  
  3462. int
  3463. netopen(name, lcl, nett) char *name; int *lcl, nett; {
  3464.     char *p;
  3465.     int i, x, rc_inet_addr = 0, dns = 0;
  3466. #ifdef TCPSOCKET
  3467.     int isconnect = 0;
  3468. #ifdef SO_OOBINLINE
  3469.     int on = 1;
  3470. #endif /* SO_OOBINLINE */
  3471.     struct servent *service=NULL;
  3472.     struct hostent *host=NULL;
  3473.     struct sockaddr_in r_addr;
  3474.     struct sockaddr_in sin;
  3475.     struct sockaddr_in l_addr;
  3476.     GSOCKNAME_T l_slen;
  3477. #ifdef EXCELAN
  3478.     struct sockaddr_in send_socket;
  3479. #endif /* EXCELAN */
  3480.  
  3481. #ifdef INADDRX
  3482. /* inet_addr() is of type struct in_addr */
  3483. #ifdef datageneral
  3484.     extern struct in_addr inet_addr();
  3485. #else
  3486. #ifdef HPUX5WINTCP
  3487.     extern struct in_addr inet_addr();
  3488. #endif /* HPUX5WINTCP */
  3489. #endif /* datageneral */
  3490.     struct in_addr iax;
  3491. #else
  3492. #ifdef INADDR_NONE
  3493.     struct in_addr iax;
  3494. #else /* INADDR_NONE */
  3495.     long iax;
  3496. #endif /* INADDR_NONE */
  3497. #endif /* INADDRX */
  3498. #endif /* TCPSOCKET */
  3499.  
  3500. #ifdef COMMENT
  3501. /* This causes big trouble */
  3502. #ifndef INADDR_NONE
  3503. #define INADDR_NONE 0xffffffff
  3504. #endif /* INADDR_NONE */
  3505. #endif /* COMMENT */
  3506.  
  3507. #ifdef SUNX25                           /* Code for SunLink X.25 support */
  3508. #define X29PID 1                        /* X.29 Protocol ID */
  3509. _PROTOTYP(SIGTYP x25oobh, (int) );
  3510.     CONN_DB x25host;
  3511. #ifndef X25_WR_FACILITY
  3512.     FACILITY x25facil;
  3513. #else
  3514.     FACILITY_DB x25facil;
  3515. #endif /* X25_WR_FACILITY */
  3516.     static int needh = 1;
  3517.     PID_T pid;
  3518.     extern int linkid, lcn, x25ver;
  3519. #endif /* SUNX25 */
  3520. #ifdef ANYX25
  3521.     extern int revcall, closgr, cudata;
  3522.     extern char udata[];
  3523. #endif /* ANYX25 */
  3524.  
  3525. #ifdef IBMX25                           /* Variables for IBM X25 */
  3526.     extern int x25port;                 /* Logical port to use */
  3527.     extern x25addr_t local_nua;         /* Local X.25 address */
  3528.     extern x25addr_t remote_nua;        /* Remote X.25 address */
  3529.     extern char x25name[];              /* X25 device name (sx25a0) */
  3530.     extern char x25dev[];               /* X25 device file /dev/x25pkt */
  3531.     ulong bind_flags = 0;               /* Flags for binding the X25 stream */
  3532.     ulong token = 0;                    /* Temporary return code */
  3533. #endif /* IBMX25 */
  3534.  
  3535.     debug(F101,"netopen nett","",nett);
  3536.     *ipaddr = '\0';                     /* Initialize IP address string */
  3537.  
  3538. #ifdef SUNX25
  3539.     if (nett == NET_SX25) {             /* If network type is X.25 */
  3540.         netclos();                      /* Close any previous net connection */
  3541.         ttnproto = NP_NONE;             /* No protocol selected yet */
  3542.  
  3543.         /* Set up host structure */
  3544.         bzero((char *)&x25host,sizeof(x25host));
  3545.         if ((x25host.hostlen = pkx121(name,x25host.host)) < 0) {
  3546.             fprintf (stderr,"Invalid X.121 host address %s\n",name);
  3547.             errno = 0;
  3548.             return (-1);
  3549.         }
  3550.         x25host.datalen = X29PIDLEN;
  3551.         x25host.data[0] = X29PID;
  3552.  
  3553.         /* Set call user data if specified */
  3554.         if (cudata) {
  3555.             ckstrncpy((char *)x25host.data+X29PIDLEN,udata,(int)strlen(udata));
  3556.             x25host.datalen += (int)strlen(udata);
  3557.         }
  3558.  
  3559.         /* Open SunLink X.25 socket */
  3560.         if (!quiet && *name) {
  3561.             printf(" Trying %s... ", name);
  3562.             fflush(stdout);
  3563.         }
  3564.         if ((ttyfd = socket(AF_X25, SOCK_STREAM, 0)) < 0) {
  3565.             debug(F101,"netopen socket error","",errno);
  3566.             perror ("X.25 socket error");
  3567.             return (-1);
  3568.         }
  3569.  
  3570.         /* Setting X.25 out-of-band data handler */
  3571.         pid = getpid();
  3572.         if (ioctl(ttyfd,SIOCSPGRP,&pid)) {
  3573.             perror("X.25 set process group id error");
  3574.             return(-1);
  3575.         }
  3576.         (VOID) signal(SIGURG,x25oobh);
  3577.  
  3578.         /* Set reverse charge call and closed user group if requested */
  3579.         bzero ((char *)&x25facil,sizeof(x25facil));
  3580.  
  3581. #ifndef X25_WR_FACILITY
  3582. /*  New SunLink (7.0 or 8.0, not sure which)... */
  3583.         x25facil.type = T_REVERSE_CHARGE; /* Reverse Charge */
  3584.         x25facil.f_reverse_charge = revcall ? 1 : 0;
  3585.         if (ioctl(ttyfd,X25_SET_FACILITY,&x25facil) < 0) {
  3586.             perror ("Setting X.25 reverse charge");
  3587.             return (-1);
  3588.         }
  3589.         if (closgr > -1) {              /* Closed User Group (Outgoing) */
  3590.             bzero ((char *)&x25facil,sizeof(x25facil));
  3591.             x25facil.type = T_CUG;
  3592.             x25facil.f_cug_req = CUG_REQ_ACS;
  3593.             x25facil.f_cug_index = closgr;
  3594.             if (ioctl(ttyfd,X25_SET_FACILITY,&x25facil) < 0) {
  3595.                 perror ("Setting X.25 closed user group");
  3596.                 return (-1);
  3597.             }
  3598.         }
  3599. #else
  3600. /*  Old SunLink 6.0 (or 7.0?)... */
  3601.         if (revcall) x25facil.reverse_charge = revcall;
  3602.         if (closgr > -1) {
  3603.             x25facil.cug_req = 1;
  3604.             x25facil.cug_index = closgr;
  3605.         }
  3606.         if (ioctl(ttyfd,X25_WR_FACILITY,&x25facil) < 0) {
  3607.             perror ("Setting X.25 facilities");
  3608.             return (-1);
  3609.         }
  3610. #endif /* X25_WR_FACILITY */
  3611.  
  3612.         /*  Need X.25 header with bits Q and M */
  3613.         if (ioctl (ttyfd,X25_HEADER,&needh) < 0) {
  3614.             perror ("Setting X.25 header");
  3615.             return (-1);
  3616.         }
  3617.  
  3618.         /* Connects to remote host via SunLink X.25 */
  3619.         if (connect(ttyfd,(struct sockaddr *)&x25host,sizeof(x25host)) < 0) {
  3620.             i = errno;
  3621.             debug(F101,"netopen connect errno","",i);
  3622.             if (i) {
  3623.                 perror("netopen x25 connect");
  3624.                 x25diag();
  3625.             }
  3626.             (VOID) netclos();
  3627.             ttyfd = -1;
  3628.             wasclosed = 1;
  3629.             ttnproto = NP_NONE;
  3630.             errno = i;
  3631.             return (-1);
  3632.         }
  3633.  
  3634.         /* Get X.25 link identification used for the connection */
  3635.         if (ioctl(ttyfd,X25_GET_LINK,&linkid) < 0) {
  3636.             perror ("Getting X.25 link id");
  3637.             return (-1);
  3638.         }
  3639.  
  3640.         /* Get X.25 logical channel number used for the connection */
  3641.         if (ioctl(ttyfd,X25_RD_LCGN,&lcn) < 0) {
  3642.             perror ("Getting X.25 lcn");
  3643.             return (-1);
  3644.         }
  3645.  
  3646.         /* Get SunLink X.25 version */
  3647.         if (ioctl(ttyfd,X25_VERSION,&x25ver) < 0) {
  3648.             perror ("Getting SunLink X.25 version");
  3649.             return (-1);
  3650.         }
  3651.         ttnet = nett;                   /* Sunlink X.25 network */
  3652.         ttnproto = NP_X3;               /* PAD X.3, X.28, X.29 protocol */
  3653.         if (lcl) if (*lcl < 0) *lcl = 1; /* Local mode */
  3654.         return(0);
  3655.     } else /* Note that SUNX25 support can coexist with TCP/IP support. */
  3656. #endif /* SUNX25 */
  3657.  
  3658. #ifdef IBMX25
  3659.     /* riehm */
  3660.     if (nett == NET_IX25) {             /* IBM AIX X.25 */
  3661.         netclos();                      /* Close any previous net connection */
  3662.         ttnproto = NP_NONE;             /* No protocol selected yet */
  3663.  
  3664.         /* find out who we are - this is not so easy on AIX */
  3665.         /* riehm: need to write the code that finds this out
  3666.          * automatically, or at least allow it to be configured
  3667.          * somehow
  3668.          */
  3669.         if (!local_nua[0] && !x25local_nua(local_nua)) {
  3670.             return(-1);
  3671.         }
  3672.  
  3673.         /* Initialise the X25 API (once per process? once per connection?) */
  3674.  
  3675.         debug(F110, "Opening ", x25dev, 0 );
  3676.         /* set O_NDELAY to allow polling? */
  3677.         if ((ttyfd = open(x25dev, O_RDWR)) < 0) {
  3678.             perror ("X.25 device open error");
  3679.             debug(F101,"netopen: device open error","",errno);
  3680.             return (-1);
  3681.         }
  3682.  
  3683.         /* push the NPI onto the STREAM */
  3684.         if (ioctl(ttyfd,I_PUSH,"npi") < 0 ) {
  3685.             close(ttyfd);
  3686.             ttyfd = -1;
  3687.             wasclosed = 1;
  3688.             perror( "kermit: netopen(): couldn't push npi on the X25 stream" );
  3689.             debug(F101,"netopen: can't push npi on the X25 stream","",errno);
  3690.             return (-1);
  3691.         }
  3692.  
  3693.         /* set up server mode - bind the x25 port and wait for
  3694.          * incoming connections
  3695.          */
  3696.         if (name[0] == '*') {           /* Server */
  3697.             /* set up a server - see the warning in x25bind() */
  3698.             bind_flags |= TOKEN_REQUEST;
  3699.  
  3700.             /* bind kermit to the local X25 address */
  3701.             token = x25bind(ttyfd,
  3702.                             local_nua,
  3703.                             udata,
  3704.                             (int)strlen( udata ),
  3705.                             1,
  3706.                             x25port,
  3707.                             bind_flags
  3708.                             );
  3709.             if (token < 0) {
  3710.                 debug(F100,"netopen: couldn't bind to local X25 address","",0);
  3711.                 netclos();
  3712.                 return(-1);
  3713.             }
  3714.             /* Currently not connected to a remote host */
  3715.  
  3716.             remote_nua[0] = '\0';
  3717.  
  3718.             /* store the fd so that incoming calls can have their own fd
  3719.              * This is almost support for a true server (ie: a'la ftpd)
  3720.              * but we're not quite there yet.
  3721.              * used in netclos()
  3722.              */
  3723.             x25serverfd = ttyfd;
  3724.             /*
  3725.              * wait for an incoming call
  3726.              * this should happen in the "server" command and not in
  3727.              * the "set host *" command.
  3728.              */
  3729.             if ((ttyfd = x25getcall(ttyfd)) < 0) {
  3730.                 netclos();
  3731.                 return(-1);
  3732.             }
  3733.         } else {                        /* Client */
  3734.             /* Bind kermit to the local X25 address */
  3735.             token = x25bind(
  3736.                             ttyfd,
  3737.                             local_nua,
  3738.                             (char *)NULL,
  3739.                             0,
  3740.                             0,
  3741.                             x25port,
  3742.                             bind_flags
  3743.                             );
  3744.             if (token < 0) {
  3745.                 debug(F100,"netopen: couldn't bind to local X25 address","",0);
  3746.                 netclos();
  3747.                 return(-1);
  3748.             }
  3749. /* riehm: this should be done via the CONNECT command, not HOST! */
  3750.             {
  3751.                 x25serverfd = 0;
  3752.                 /* call the remote host */
  3753.                 /* name == address of remote host as char* */
  3754.                 if (x25call(ttyfd, name, udata) < 0 ) {
  3755.                     debug(F100,
  3756.                           "netopen: couldn't connect to remote X25 address",
  3757.                           "", 0);
  3758.                     netclos();
  3759.                     return(-1);
  3760.                 }
  3761.                 strcpy(remote_nua, name);
  3762.             }
  3763.         }
  3764.         ttnet = nett;                   /* AIX X.25 network */
  3765.         if (lcl) if (*lcl < 0) *lcl = 1; /* Local mode */
  3766.         return(0);
  3767.  
  3768.     } else /* Note that IBMX25 support can coexist with TCP/IP support. */
  3769. #endif /* IBMX25 */
  3770.  
  3771. /*   Add support for other networks here. */
  3772.  
  3773.       if (nett != NET_TCPB) return(-1); /* BSD socket support */
  3774.  
  3775. #ifdef TCPSOCKET
  3776.     netclos();                          /* Close any previous connection. */
  3777.     ckstrncpy(namecopy, name, NAMECPYL);        /* Copy the hostname. */
  3778.     debug(F110,"netopen namecopy",namecopy,0);
  3779.  
  3780. #ifndef NOLISTEN
  3781.     if (name[0] == '*')
  3782.       return(tcpsrv_open(name, lcl, nett, 0));
  3783. #endif /* NOLISTEN */
  3784.  
  3785.     p = namecopy;                       /* Was a service requested? */
  3786.     while (*p != '\0' && *p != ':') p++; /* Look for colon */
  3787.     if (*p == ':') {                    /* Have a colon */
  3788.         debug(F110,"netopen name has colon",namecopy,0);
  3789.         *p++ = '\0';                    /* Get service name or number */
  3790. #ifdef CK_URL
  3791.         /*
  3792.            Here we have to check for various popular syntaxes:
  3793.            host:port (our original syntax)
  3794.            URL such as telnet:host or telnet://host/
  3795.            Or even telnet://user:password@host:port/path/
  3796.            Or a malformed URL such as generated by Netscape 4.0 like:
  3797.            telnet:telnet or telnet::host.
  3798.         */
  3799.  
  3800.         /*
  3801.          * REPLACE THIS CODE WITH urlparse() but not on the day of the
  3802.          * C-Kermit 8.0 RELEASE.
  3803.          */
  3804.  
  3805.         if (*p == ':')                  /* a second colon */
  3806.           *p++ = '\0';                  /* get rid of that one too */
  3807.         while (*p == '/') *p++ = '\0';  /* and slashes */
  3808.         x = strlen(p);                  /* Length of remainder */
  3809.         if (p[x-1] == '/')              /* If there is a trailing slash */
  3810.           p[x-1] = '\0';                /* remove it. */
  3811.         debug(F110,"netopen namecopy after stripping",namecopy,0);
  3812.         debug(F110,"netopen p after stripping",p,0);
  3813.         service = getservbyname(namecopy,"tcp");
  3814.         if (service ||
  3815. #ifdef RLOGCODE
  3816.             !ckstrcmp("rlogin",namecopy,NAMECPYL,0) ||
  3817. #endif /* RLOGCODE */
  3818. #ifdef CK_SSL
  3819.             !ckstrcmp("telnets",namecopy,NAMECPYL,0) ||
  3820. #endif /* CK_SSL */
  3821.             !ckstrcmp("iksd",namecopy,NAMECPYL,0)
  3822.             ) {
  3823.             char temphost[256], tempservice[80], temppath[256];
  3824.             char * q = p, *r = p, *w = p;
  3825.             int uidfound=0;
  3826.             extern char pwbuf[];
  3827.             extern int pwflg, pwcrypt;
  3828.  
  3829.             if (ttnproto == NP_DEFAULT)
  3830.               setnproto(namecopy);
  3831.  
  3832.             /* Check for userid and possibly password */
  3833.             while (*p != '\0' && *p != '@')
  3834.                 p++; /* look for @ */
  3835.             if (*p == '@') {
  3836.                 /* found username and perhaps password */
  3837.                 debug(F110,"netopen namecopy found @","",0);
  3838.                 *p = '\0';
  3839.                 p++;
  3840.                 while (*w != '\0' && *w != ':')
  3841.                   w++;
  3842.                 if (*w == ':')
  3843.                   *w++ = '\0';
  3844.                 /* r now points to username, save it and the password */
  3845.                 debug(F110,"netopen namecopy username",r,0);
  3846.                 debug(F110,"netopen namecopy password",w,0);
  3847.                 uidfound=1;
  3848.                 if ( strcmp(uidbuf,r) || *w )
  3849.                     ckstrncpy(pwbuf,w,PWBUFL+1);
  3850.                 ckstrncpy(uidbuf,r,UIDBUFLEN);
  3851.                 pwflg = 1;
  3852.                 pwcrypt = 0;
  3853.                 q = p;                  /* Host after user and pwd */
  3854.             } else {
  3855.                 p = q;                  /* No username or password */
  3856.             }
  3857.             /* Now we must look for the optional port. */
  3858.             debug(F110,"netopen x p",p,0);
  3859.             debug(F110,"netopen x q",q,0);
  3860.  
  3861.             /* Look for the port/service or a file/directory path */
  3862.             while (*p != '\0' && *p != ':' && *p != '/')
  3863.               p++;
  3864.             if (*p == ':') {
  3865.                 debug(F110,"netopen found port",q,0);
  3866.                 *p++ = '\0';            /* Found a port name or number */
  3867.                 r = p;
  3868.  
  3869.                 /* Look for the end of port/service or a file/directory path */
  3870.                 while (*p != '\0' && *p != '/')
  3871.                     p++;
  3872.                 if (*p == '/')
  3873.                     *p++ = '\0';
  3874.  
  3875.                 debug(F110,"netopen port",r,0);
  3876.                 ckstrncpy(tempservice,r,80);
  3877.                 ckstrncpy(temphost,q,256);
  3878.                 ckstrncpy(temppath,p,256);
  3879.                 ckstrncpy(namecopy,temphost,NAMECPYL);
  3880.                 debug(F110,"netopen tempservice",tempservice,0);
  3881.                 debug(F110,"netopen temphost",temphost,0);
  3882.                 debug(F110,"netopen temppath",temppath,0);
  3883.  
  3884.                 /* move port/service to a buffer that won't go away */
  3885.                 x = strlen(namecopy);
  3886.                 p = namecopy + x + 1;
  3887.                 ckstrncpy(p, tempservice, NAMECPYL - x);
  3888.             } else {
  3889.                 /* Handle a path if we found one */
  3890.                 if (*p == '/')
  3891.                     *p++ = '\0';
  3892.                 ckstrncpy(temppath,p,256);
  3893.  
  3894.                 /* We didn't find another port, but if q is a service */
  3895.                 /* then assume that namecopy is actually a host.      */
  3896.                 if (getservbyname(q,"tcp")) {
  3897.                     p = q;
  3898.                 } else {
  3899. #ifdef RLOGCODE
  3900.                     /* rlogin is not a valid service */
  3901.                     if (!ckstrcmp("rlogin",namecopy,6,0)) {
  3902.                         ckstrncpy(namecopy,"login",NAMECPYL);
  3903.                     }
  3904. #endif /* RLOGCODE */
  3905.                     /* iksd is not a valid service */
  3906.                     if (!ckstrcmp("iksd",namecopy,6,0)) {
  3907.                         ckstrncpy(namecopy,"kermit",NAMECPYL);
  3908.                     }
  3909.                     /* Reconstruct namecopy */
  3910.                     ckstrncpy(tempservice,namecopy,80);
  3911.                     ckstrncpy(temphost,q,256);
  3912.                     ckstrncpy(namecopy,temphost,NAMECPYL);
  3913.                     debug(F110,"netopen tempservice",tempservice,0);
  3914.                     debug(F110,"netopen temphost",temphost,0);
  3915.                     debug(F110,"netopen temppath",temppath,0);
  3916.  
  3917.                     /* move port/service to a buffer that won't go away */
  3918.                     x = strlen(namecopy);
  3919.                     p = namecopy + x + 1;
  3920.                     ckstrncpy(p, tempservice, NAMECPYL - x - 1);
  3921.                 }
  3922.             }
  3923.             debug(F110,"netopen URL result: host",namecopy,0);
  3924.             debug(F110,"netopen URL result: service",p,0);
  3925.             debug(F110,"netopen URL result: path",temppath,0);
  3926.  
  3927. #ifdef IKS_GET
  3928.             /* If we have set a path specified, we need to try to GET it */
  3929.             /* But we have another problem, we have to login first.  How */
  3930.             /* do we specify that a login must be done before the GET?   */
  3931.             /* The user's name if specified is in 'userid' and the       */
  3932.             /* password if any is in 'pwbuf'.                            */
  3933.             if ( temppath[0] ) {
  3934.                 extern int action;
  3935.                 extern char * cmarg;
  3936.  
  3937.                 if ( !uidfound ) {
  3938.                     /* If no userid was specified as part of the URL but
  3939.                      * a path was specified, then we
  3940.                      * set the user name to anonymous and the password
  3941.                      * to the current userid.
  3942.                      */
  3943.                     ckstrncpy(pwbuf,uidbuf,PWBUFL);
  3944.                     ckstrncat(pwbuf,"@",PWBUFL);
  3945.                     pwflg = 1;
  3946.                     pwcrypt = 0;
  3947.                     ckstrncpy(uidbuf,"anonymous",UIDBUFLEN);
  3948.                 }
  3949.  
  3950.                 /*
  3951.                  * If a file path was specified we perform the GET
  3952.                  * operation and then terminate the connection.
  3953.                  *
  3954.                  * If a directory was given instead of a file, then
  3955.                  * we should REMOTE CD to the directory and list its
  3956.                  * contents.  But how do we tell the difference?
  3957.                  */
  3958.                 makestr(&cmarg,temppath);
  3959.                 action = 'r';
  3960.             }
  3961. #endif /* IKS_GET */
  3962.         }
  3963. #endif /* CK_URL */
  3964.     } else {                            /* Otherwise use telnet */
  3965.         p = "telnet";
  3966.     }
  3967. /*
  3968.   By the time we get here, namecopy[] should hold the null-terminated
  3969.   hostname or address, and p should point to the service name or number.
  3970. */
  3971.     debug(F110,"netopen host",namecopy,0);
  3972.     debug(F110,"netopen service requested",p,0);
  3973.  
  3974.    /* Use the service port to set the default protocol type if necessary */
  3975.     if (ttnproto == NP_DEFAULT)
  3976.        setnproto(p);
  3977.  
  3978.     ckstrncpy(namecopy2,namecopy,NAMECPYL);
  3979.     service = ckgetservice(namecopy,p,namecopy,NAMECPYL);
  3980.     if (!service) {
  3981.         fprintf(stderr, "Can't find port for service %s\n", p);
  3982. #ifdef TGVORWIN
  3983.         debug(F101,"netopen can't get service","",socket_errno);
  3984. #else
  3985.         debug(F101,"netopen can't get service","",errno);
  3986. #endif /* TGVORWIN */
  3987.         errno = 0;                  /* (rather than mislead) */
  3988.         return(-1);
  3989.     } else {
  3990.         if (!ckstrcmp(namecopy,namecopy2,-1,0))
  3991.       namecopy2[0] = '\0';
  3992.         ckstrncpy(svcbuf,ckuitoa(ntohs(service->s_port)),sizeof(svcbuf));
  3993.         debug(F110,"netopen service ok",svcbuf,0);
  3994.     }
  3995.  
  3996. #ifdef RLOGCODE
  3997.     if (service && !strcmp("login",p) && service->s_port != htons(513)) {
  3998.         fprintf(stderr,
  3999.                 "  Warning: login service on port %d instead of port 513\n",
  4000.                  ntohs(service->s_port)
  4001.                 );
  4002.         fprintf(stderr, "  Edit SERVICES file if RLOGIN fails to connect.\n");
  4003.         debug(F101,"tcpsrv_open login on port","",ntohs(service->s_port));
  4004.     }
  4005. #endif /* RLOGCODE */
  4006.  
  4007. #ifndef NOHTTP
  4008.    /* For HTTP connections we must preserve the original hostname and */
  4009.    /* service requested so we can include them in the Host header.    */
  4010.     ckmakmsg(http_host_port,sizeof(http_host_port),namecopy,":",
  4011.               ckitoa(ntohs(service->s_port)),NULL);
  4012.  
  4013.     /* 'namecopy' contains the name of the host to which we want to connect */
  4014.     /* 'svcbuf'   contains the service name                                 */
  4015.     /* 'service->s_port' contains the port number in network byte order     */
  4016.  
  4017.     /* If we are using an http proxy, we need to create a buffer containing */
  4018.     /*   hostname:port-number                                               */
  4019.     /* to pass to the http_connect() function.  Then we need to replace     */
  4020.     /* 'namecopy' with the name of the proxy server and the service->s_port */
  4021.     /* with the port number of the proxy (default port 80).                 */
  4022.  
  4023.     if ( tcp_http_proxy ) {
  4024.         ckmakmsg(proxycopy,sizeof(proxycopy),namecopy,":",
  4025.                  ckuitoa(ntohs(service->s_port)),NULL);
  4026.         ckstrncpy(namecopy,tcp_http_proxy,NAMECPYL);
  4027.  
  4028.         p = namecopy;                       /* Was a service requested? */
  4029.         while (*p != '\0' && *p != ':') p++; /* Look for colon */
  4030.         if (*p == ':') {                    /* Have a colon */
  4031.             debug(F110,"netopen name has colon",namecopy,0);
  4032.             *p++ = '\0';                    /* Get service name or number */
  4033.         } else {
  4034.             strcpy(++p,"http");
  4035.         }
  4036.  
  4037.         service = ckgetservice(namecopy,p,namecopy,NAMECPYL);
  4038.         if (!service) {
  4039.             fprintf(stderr, "Can't find port for service %s\n", p);
  4040. #ifdef TGVORWIN
  4041.             debug(F101,"netopen can't get service for proxy","",socket_errno);
  4042. #else
  4043.             debug(F101,"netopen can't get service for proxy","",errno);
  4044. #endif /* TGVORWIN */
  4045.             errno = 0;                  /* (rather than mislead) */
  4046.             return(-1);
  4047.         }
  4048.         ckstrncpy(p,ckuitoa(ntohs(service->s_port)),NAMECPYL-(p-namecopy));
  4049.  
  4050.     }
  4051. #endif /* NOHTTP */
  4052.  
  4053.     /* Set up socket structure and get host address */
  4054.  
  4055.     bzero((char *)&r_addr, sizeof(r_addr));
  4056.     debug(F100,"netopen bzero ok","",0);
  4057. /*
  4058.    NOTE: Originally the inet_addr() check was #ifdef NT, but is enabled for
  4059.    all as of 20 Sep 97, to allow people to "set host" to a specific numeric IP
  4060.    address without going through the multihomed host sequence and winding up
  4061.    at a different place than the one requested.
  4062. */
  4063. #ifdef INADDR_NONE
  4064.     debug(F101,"netopen INADDR_NONE defined","",INADDR_NONE);
  4065. #else /* INADDR_NONE */
  4066.     debug(F100,"netopen INADDR_NONE not defined","",0);
  4067. #endif /* INADDR_NONE */
  4068. #ifdef INADDRX
  4069.     debug(F100,"netopen INADDRX defined","",0);
  4070. #else /* INADDRX */
  4071.     debug(F100,"netopen INADDRX not defined","",0);
  4072. #endif /* INADDRX */
  4073.  
  4074. #ifndef NOMHHOST
  4075. #ifdef INADDRX
  4076.     iax = inet_addr(namecopy);
  4077.     debug(F111,"netopen inet_addr",namecopy,iax.s_addr);
  4078. #else /* INADDRX */
  4079. #ifdef INADDR_NONE
  4080.     iax.s_addr = inet_addr(namecopy);
  4081.     debug(F111,"netopen inet_addr",namecopy,iax.s_addr);
  4082. #else /* INADDR_NONE */
  4083. #ifdef SOLARIS
  4084.     /* In Solaris inet_addr() is of type in_addr_t which is uint32_t */
  4085.     /* (unsigned) yet it returns -1 (signed) on failure. */
  4086.     /* It makes a difference in 64-bit builds. */
  4087.     rc_inet_addr = inet_addr(namecopy);    /* Assign return code to an int */
  4088.     iax = (unsigned) rc_inet_addr;    /* and from there to whatever.. */
  4089. #else
  4090. #ifndef datageneral
  4091.     iax = (unsigned int) inet_addr(namecopy);
  4092. #else
  4093.     iax = -1L;
  4094. #endif /* datageneral */
  4095. #endif /* SOLARIS */
  4096.     debug(F111,"netopen rc_inet_addr",namecopy,rc_inet_addr);
  4097.     debug(F111,"netopen inet_addr",namecopy,iax);
  4098. #endif /* INADDR_NONE */
  4099. #endif /* INADDRX */
  4100.  
  4101.     dns = 0;
  4102.     if (
  4103. /* This might give warnings on 64-bit platforms but they should be harmless */
  4104. /* because INADDR_NONE should be all 1's anyway, thus the OR part is */
  4105. /* probably superfluous -- not sure why it's even there, maybe it should be */
  4106. /* removed. */
  4107. #ifdef SOLARIS
  4108.     rc_inet_addr == -1
  4109. #else
  4110. #ifdef INADDR_NONE
  4111.         iax.s_addr == INADDR_NONE /* || iax.s_addr == (unsigned long) -1L */
  4112. #else /* INADDR_NONE */
  4113.         iax < 0
  4114. #endif /* INADDR_NONE */
  4115. #endif /* SOLARIS */
  4116.         ) {
  4117.         if (!quiet) {
  4118.             printf(" DNS Lookup... ");
  4119.             fflush(stdout);
  4120.         }
  4121.         if ((host = gethostbyname(namecopy)) != NULL) {
  4122.             debug(F100,"netopen gethostbyname != NULL","",0);
  4123.             host = ck_copyhostent(host);
  4124.             dns = 1;                    /* Remember we performed dns lookup */
  4125.             r_addr.sin_family = host->h_addrtype;
  4126.             if (tcp_rdns && host->h_name && host->h_name[0]
  4127. #ifndef NOHTTP
  4128.                  && (tcp_http_proxy == NULL)
  4129. #endif /* NOHTTP */
  4130.                  ) {                   /* Copying into our argument? */
  4131.                 ckstrncpy(name,host->h_name,80);  /* Bad Bad Bad */
  4132.                 if ( (80-strlen(name)) > (strlen(svcbuf)+1) ) {
  4133.                     ckstrncat(name,":",80-strlen(name));
  4134.                     ckstrncat(name,svcbuf,80-strlen(name));
  4135.                 }
  4136.             }
  4137.  
  4138. #ifdef HADDRLIST
  4139. #ifdef h_addr
  4140.             /* This is for trying multiple IP addresses - see <netdb.h> */
  4141.             if (!(host->h_addr_list))
  4142.               return(-1);
  4143.             bcopy(host->h_addr_list[0],
  4144.                   (caddr_t)&r_addr.sin_addr,
  4145.                   host->h_length
  4146.                   );
  4147. #else
  4148.             bcopy(host->h_addr, (caddr_t)&r_addr.sin_addr, host->h_length);
  4149. #endif /* h_addr */
  4150. #else  /* HADDRLIST */
  4151.             bcopy(host->h_addr, (caddr_t)&r_addr.sin_addr, host->h_length);
  4152. #endif /* HADDRLIST */
  4153. #ifdef COMMENT
  4154. #ifndef EXCELAN
  4155.             debug(F111,"BCOPY","host->h_addr",host->h_addr);
  4156. #endif /* EXCELAN */
  4157.             debug(F111,"BCOPY"," (caddr_t)&r_addr.sin_addr",
  4158.                   (caddr_t)&r_addr.sin_addr);
  4159.             debug(F111,"BCOPY"," r_addr.sin_addr.s_addr",
  4160.                   r_addr.sin_addr.s_addr);
  4161. #endif    /* COMMENT */
  4162.             debug(F111,"BCOPY","host->h_length",host->h_length);
  4163.         }
  4164.     }
  4165. #endif /* NOMHHOST */
  4166.  
  4167.     if (!dns) {
  4168. #ifdef INADDRX
  4169. /* inet_addr() is of type struct in_addr */
  4170.         struct in_addr ina;
  4171.         unsigned long uu;
  4172.         debug(F100,"netopen gethostbyname == NULL: INADDRX","",0);
  4173.         ina = inet_addr(namecopy);
  4174.         uu = *(unsigned int *)&ina;
  4175. #else /* Not INADDRX */
  4176. /* inet_addr() is unsigned long */
  4177.         unsigned long uu;
  4178.         debug(F100,"netopen gethostbyname == NULL: Not INADDRX","",0);
  4179.         uu = inet_addr(namecopy);
  4180. #endif /* INADDRX */
  4181.         debug(F101,"netopen uu","",uu);
  4182.         if (
  4183. #ifdef INADDR_NONE
  4184.             !(uu == INADDR_NONE || uu == (unsigned int) -1L)
  4185. #else   /* INADDR_NONE */
  4186.             uu != ((unsigned long)-1)
  4187. #endif /* INADDR_NONE */
  4188.             ) {
  4189.             r_addr.sin_addr.s_addr = uu;
  4190.             r_addr.sin_family = AF_INET;
  4191.         } else {
  4192. #ifdef VMS
  4193.             fprintf(stdout, "\r\n");    /* complete any previous message */
  4194. #endif /* VMS */
  4195.             fprintf(stderr, "Can't get address for %s\n", namecopy);
  4196. #ifdef TGVORWIN
  4197.             debug(F101,"netopen can't get address","",socket_errno);
  4198. #else
  4199.             debug(F101,"netopen can't get address","",errno);
  4200. #endif /* TGVORWIN */
  4201.             errno = 0;                  /* Rather than mislead */
  4202.             return(-1);
  4203.         }
  4204.     }
  4205.  
  4206.     /* Get a file descriptor for the connection. */
  4207.  
  4208.     r_addr.sin_port = service->s_port;
  4209.     ckstrncpy(ipaddr,(char *)inet_ntoa(r_addr.sin_addr),20);
  4210.     debug(F110,"netopen trying",ipaddr,0);
  4211.     if (!quiet && *ipaddr) {
  4212.         printf(" Trying %s... ", ipaddr);
  4213.         fflush(stdout);
  4214.     }
  4215.  
  4216.     /* Loop to try additional IP addresses, if any. */
  4217.  
  4218.     do {
  4219. #ifdef EXCELAN
  4220.         send_socket.sin_family = AF_INET;
  4221.         send_socket.sin_addr.s_addr = 0;
  4222.         send_socket.sin_port = 0;
  4223.         if ((ttyfd = socket(SOCK_STREAM, (struct sockproto *)0,
  4224.                             &send_socket, SO_REUSEADDR)) < 0)
  4225. #else  /* EXCELAN */
  4226. #ifdef NT
  4227. #ifdef COMMENT_X
  4228.        /*
  4229.          Must make sure that all sockets are opened in
  4230.          Non-overlapped mode since we use the standard
  4231.          C RTL functions to read and write data.
  4232.          But it doesn't seem to work as planned.
  4233.        */
  4234.           {
  4235.               int optionValue = SO_SYNCHRONOUS_NONALERT;
  4236.               if (setsockopt(INVALID_SOCKET, SOL_SOCKET, SO_OPENTYPE,
  4237.                              (char *) &optionValue, sizeof(optionValue))
  4238.                   != NO_ERROR)
  4239.                 return(-1);
  4240.           }
  4241. #endif /* COMMENT */
  4242. #endif /* NT */
  4243.  
  4244.         if ((ttyfd = socket(AF_INET, SOCK_STREAM, 0)) < 0)
  4245. #endif /* EXCELAN */
  4246.             {
  4247. #ifdef EXCELAN
  4248.                 experror("TCP socket error");
  4249. #else
  4250. #ifdef VMS
  4251.                 fprintf(stdout, "\r\n"); /* complete any previous stdout */
  4252. #endif /* VMS */
  4253. #ifdef TGVORWIN
  4254. #ifdef OLD_TWG
  4255.                 errno = socket_errno;
  4256. #endif /* OLD_TWG */
  4257.                 socket_perror("TCP socket error");
  4258.                 debug(F101,"netopen socket error","",socket_errno);
  4259. #else
  4260.                 perror("TCP socket error");
  4261.                 debug(F101,"netopen socket error","",errno);
  4262. #endif /* TGVORWIN */
  4263. #endif /* EXCELAN */
  4264.                 return (-1);
  4265.             }
  4266.         errno = 0;
  4267.  
  4268. #ifdef RLOGCODE
  4269.        /* Not part of the RLOGIN RFC, but the BSD implementation     */
  4270.        /* requires that the client port be a priviliged port (<1024) */
  4271.        /* on a Unix system this would require SuperUser permissions  */
  4272.        /* thereby saying that the root of the Unix system has given  */
  4273.        /* permission for this connection to be created               */
  4274.        if (service->s_port == htons((unsigned short)RLOGIN_PORT)) {
  4275.            static unsigned short lport = 1024;  /* max reserved port */
  4276. #ifdef OS2
  4277.            int s_errno;
  4278. #endif /* OS2 */
  4279.  
  4280.            lport--;                     /* Make sure we do not reuse a port */
  4281.            if (lport == 512)
  4282.              lport = 1023;
  4283.  
  4284.            sin.sin_family = AF_INET;
  4285.            if (tcp_address) {
  4286. #ifdef INADDRX
  4287.                inaddrx = inet_addr(tcp_address);
  4288.                sin.sin_addr.s_addr = *(unsigned long *)&inaddrx;
  4289. #else
  4290.                sin.sin_addr.s_addr = inet_addr(tcp_address);
  4291. #endif /* INADDRX */
  4292.            } else
  4293.              sin.sin_addr.s_addr = INADDR_ANY;
  4294.            while (1) {
  4295.                sin.sin_port = htons(lport);
  4296.                if (bind(ttyfd, (struct sockaddr *)&sin, sizeof(sin)) >= 0)
  4297.                  break;
  4298. #ifdef OS2
  4299.                s_errno = socket_errno;
  4300.                if (s_errno && /* OS2 bind fails with 0, if already in use */
  4301. #ifdef NT
  4302.                    s_errno != WSAEADDRINUSE
  4303. #else
  4304.                    s_errno != SOCEADDRINUSE &&
  4305.                    s_errno != (SOCEADDRINUSE - SOCBASEERR)
  4306. #endif /* NT */
  4307.                    )
  4308. #else /* OS2 */
  4309. #ifdef TGVORWIN
  4310.                  if (socket_errno != EADDRINUSE)
  4311. #else
  4312.                  if (errno != EADDRINUSE)
  4313. #endif /* TGVORWIN */
  4314. #endif /* OS2 */
  4315.                    {
  4316. #ifdef COMMENT
  4317.                        printf("\nBind failed with errno %d  for port %d.\n",
  4318. #ifdef OS2
  4319.                               s_errno
  4320. #else
  4321. #ifdef TGVORWIN
  4322.                               socket_errno
  4323. #else
  4324.                               errno
  4325. #endif /* TGVORWIN */
  4326. #endif /* OS2 */
  4327.                               , lport
  4328.                               );
  4329. #ifdef OS2
  4330.                        debug(F101,"rlogin bind failed","",s_errno);
  4331. #else
  4332. #ifdef TGVORWIN
  4333.                        debug(F101,"rlogin bind failed","",socket_errno);
  4334. #ifdef OLD_TWG
  4335.                        errno = socket_errno;
  4336. #endif /* OLD_TWG */
  4337.                        socket_perror("rlogin bind");
  4338. #else
  4339.                        debug(F101,"rlogin bind failed","",errno);
  4340.                        perror("rlogin bind");
  4341. #endif /* TGVORWIN */
  4342. #endif /* OS2 */
  4343. #else  /* COMMENT */
  4344. #ifdef OS2
  4345.                        debug(F101,"rlogin bind s_errno","",s_errno);
  4346.                        perror("rlogin bind");
  4347. #else
  4348. #ifdef VMS
  4349.                        printf("\r\n");  /* complete any previous message */
  4350. #endif /* VMS */
  4351. #ifdef TGVORWIN
  4352.                        debug(F101,"rlogin bind socket_errno","",socket_errno);
  4353. #ifdef OLD_TWG
  4354.                        errno = socket_errno;
  4355. #endif /* OLD_TWG */
  4356.                        socket_perror("rlogin bind");
  4357. #else
  4358.                        debug(F101,"rlogin bind errno","",errno);
  4359.                        perror("rlogin bind");
  4360. #endif /* TGVORWIN */
  4361. #endif /* OS2 */
  4362.                        debug(F101,"rlogin local port","",lport);
  4363. #endif /* COMMENT */
  4364.                        netclos();
  4365.                        return -1;
  4366.                    }
  4367.                lport--;
  4368.                if (lport == 512 /* lowest reserved port to use */ ) {
  4369.                    printf("\nNo reserved ports available.\n");
  4370.                    netclos();
  4371.                    return -1;
  4372.                }
  4373.            }
  4374.            debug(F101,"rlogin lport","",lport);
  4375.            ttnproto = NP_RLOGIN;
  4376.        } else
  4377. #endif /* RLOGCODE  */
  4378.  
  4379.        /* If a specific TCP address on the local host is desired we */
  4380.        /* must bind it to the socket.                               */
  4381. #ifndef datageneral
  4382.          if (tcp_address) {
  4383.              int s_errno;
  4384.  
  4385.              debug(F110,"netopen binding socket to",tcp_address,0);
  4386.              bzero((char *)&sin,sizeof(sin));
  4387.              sin.sin_family = AF_INET;
  4388. #ifdef INADDRX
  4389.              inaddrx = inet_addr(tcp_address);
  4390.              sin.sin_addr.s_addr = *(unsigned long *)&inaddrx;
  4391. #else
  4392.              sin.sin_addr.s_addr = inet_addr(tcp_address);
  4393. #endif /* INADDRX */
  4394.              sin.sin_port = 0;
  4395.              if (bind(ttyfd, (struct sockaddr *)&sin, sizeof(sin)) < 0) {
  4396.                  s_errno = socket_errno; /* Save error code */
  4397. #ifdef TCPIPLIB
  4398.                  socket_close(ttyfd);
  4399. #else /* TCPIPLIB */
  4400.                  close(ttyfd);
  4401. #endif /* TCPIPLIB */
  4402.                  ttyfd = -1;
  4403.                  wasclosed = 1;
  4404.                  errno = s_errno;       /* and report this error */
  4405.                  debug(F101,"netopen bind errno","",errno);
  4406.                  return(-1);
  4407.              }
  4408.          }
  4409. #endif /* datageneral */
  4410.  
  4411. /* Now connect to the socket on the other end. */
  4412.  
  4413. #ifdef EXCELAN
  4414.         if (connect(ttyfd, &r_addr) < 0)
  4415. #else
  4416. #ifdef NT
  4417.           WSASafeToCancel = 1;
  4418. #endif /* NT */
  4419.         if (connect(ttyfd, (struct sockaddr *)&r_addr, sizeof(r_addr)) < 0)
  4420. #endif /* EXCELAN */
  4421.           {
  4422. #ifdef NT
  4423.               WSASafeToCancel = 0;
  4424. #endif /* NT */
  4425. #ifdef OS2
  4426.               i = socket_errno;
  4427. #else /* OS2 */
  4428. #ifdef TGVORWIN
  4429.               i = socket_errno;
  4430. #else
  4431.               i = errno;                /* Save error code */
  4432. #endif /* TGVORWIN */
  4433. #endif /* OS2 */
  4434. #ifdef RLOGCODE
  4435.               if (
  4436. #ifdef OS2
  4437.                  i && /* OS2 bind fails with 0, if already in use */
  4438. #ifdef NT
  4439.                  i == WSAEADDRINUSE
  4440. #else
  4441.                  (i == SOCEADDRINUSE ||
  4442.                  i == (SOCEADDRINUSE - SOCBASEERR))
  4443. #endif /* NT */
  4444. #else /* OS2 */
  4445. #ifdef TGVORWIN
  4446.                   socket_errno == EADDRINUSE
  4447. #else
  4448.                   errno == EADDRINUSE
  4449. #endif /* TGVORWIN */
  4450. #endif /* OS2 */
  4451.                   && ttnproto == NP_RLOGIN) {
  4452. #ifdef TCPIPLIB
  4453.                    socket_close(ttyfd); /* Close it. */
  4454. #else
  4455.                    close(ttyfd);
  4456. #endif /* TCPIPLIB */
  4457.                    continue;            /* Try a different lport */
  4458.                }
  4459. #endif /* RLOGCODE */
  4460. #ifdef HADDRLIST
  4461. #ifdef h_addr
  4462.               if (host && host->h_addr_list && host->h_addr_list[1]) {
  4463.                   perror("");
  4464.                   host->h_addr_list++;
  4465.                   bcopy(host->h_addr_list[0],
  4466.                         (caddr_t)&r_addr.sin_addr,
  4467.                         host->h_length);
  4468.  
  4469.                   ckstrncpy(ipaddr,(char *)inet_ntoa(r_addr.sin_addr),20);
  4470.                   debug(F110,"netopen h_addr_list",ipaddr,0);
  4471.                   if (!quiet && *ipaddr) {
  4472.                       printf(" Trying %s... ", ipaddr);
  4473.                       fflush(stdout);
  4474.                   }
  4475. #ifdef TCPIPLIB
  4476.                   socket_close(ttyfd); /* Close it. */
  4477. #else
  4478.                   close(ttyfd);
  4479. #endif /* TCPIPLIB */
  4480.                   continue;
  4481.               }
  4482. #endif /* h_addr */
  4483. #endif  /* HADDRLIST */
  4484.               netclos();
  4485.               ttyfd = -1;
  4486.               wasclosed = 1;
  4487.               ttnproto = NP_NONE;
  4488.               errno = i;                /* And report this error */
  4489. #ifdef EXCELAN
  4490.               if (errno) experror("netopen connect");
  4491. #else
  4492. #ifdef TGVORWIN
  4493.               debug(F101,"netopen connect error","",socket_errno);
  4494.               /* if (errno) socket_perror("netopen connect"); */
  4495. #ifdef OLD_TWG
  4496.               errno = socket_errno;
  4497. #endif /* OLD_TWG */
  4498.               if (!quiet)
  4499.                 socket_perror("netopen connect");
  4500. #else /* TGVORWIN */
  4501.               debug(F101,"netopen connect errno","",errno);
  4502. #ifdef VMS
  4503.               if (!quiet)
  4504.                 perror("\r\nFailed");
  4505. #else
  4506.               if (!quiet)
  4507.                 perror("Failed");
  4508. #endif /* VMS */
  4509. #ifdef DEC_TCPIP
  4510.               if (!quiet)
  4511.                 perror("netopen connect");
  4512. #endif /* DEC_TCPIP */
  4513. #ifdef CMU_TCPIP
  4514.               if (!quiet)
  4515.                 perror("netopen connect");
  4516. #endif /* CMU_TCPIP */
  4517. #endif /* TGVORWIN */
  4518. #endif /* EXCELAN */
  4519.               return(-1);
  4520.           }
  4521. #ifdef NT
  4522.         WSASafeToCancel = 0;
  4523. #endif /* NT */
  4524.         isconnect = 1;
  4525.     } while (!isconnect);
  4526.  
  4527. #ifdef NON_BLOCK_IO
  4528.     on = 1;
  4529.     x = socket_ioctl(ttyfd,FIONBIO,&on);
  4530.     debug(F101,"netopen FIONBIO","",x);
  4531. #endif /* NON_BLOCK_IO */
  4532.  
  4533. #ifdef NT_TCP_OVERLAPPED
  4534.     OverlappedWriteInit();
  4535.     OverlappedReadInit();
  4536. #endif /* NT_TCP_OVERLAPPED */
  4537.  
  4538.     ttnet = nett;                       /* TCP/IP (sockets) network */
  4539.  
  4540. #ifndef NOHTTP
  4541.     /* We have succeeded in connecting to the HTTP PROXY.  So now we   */
  4542.     /* need to attempt to connect through the proxy to the actual host */
  4543.     /* If that is successful, we have to pretend that we made a direct */
  4544.     /* connection to the actual host.                                  */
  4545.  
  4546.     if ( tcp_http_proxy ) {
  4547. #ifdef OS2
  4548.         char * agent = "Kermit 95";             /* Default user agent */
  4549. #else
  4550.         char * agent = "C-Kermit";
  4551. #endif /* OS2 */
  4552.  
  4553.         if (http_connect(ttyfd,
  4554.              tcp_http_proxy_agent ? tcp_http_proxy_agent : agent,
  4555.              NULL,
  4556.                          tcp_http_proxy_user,
  4557.                          tcp_http_proxy_pwd,
  4558.                          0,
  4559.                          proxycopy
  4560.                          ) < 0) {
  4561.             netclos();
  4562.             return(-1);
  4563.         }
  4564.  
  4565.         ckstrncpy(namecopy,proxycopy,NAMECPYL);
  4566.         p = namecopy;                       /* Was a service requested? */
  4567.         while (*p != '\0' && *p != ':') p++; /* Look for colon */
  4568.         *p = '\0';
  4569.     }
  4570. #endif /* NOHTTP */
  4571.  
  4572.     /* Jeff - Does this next block of code that set's the protocol */
  4573.     /* need to be here anymore?  5/10/2000                         */
  4574.  
  4575.     /* There are certain magic port numbers that when used require */
  4576.     /* the use of specific protocols.  Check this now before we    */
  4577.     /* set the SO_OOBINLINE state or we might get it wrong.        */
  4578.     x = ntohs((unsigned short)service->s_port);
  4579.     svcnum = x;
  4580.     /* See if the service is TELNET. */
  4581.     if (x == TELNET_PORT) {
  4582.         /* Yes, so if raw port not requested */
  4583.         if (ttnproto != NP_TCPRAW && ttnproto != NP_SSL_RAW && 
  4584.         ttnproto != NP_TLS_RAW && ttnproto != NP_NONE)
  4585.           ttnproto = NP_TELNET;         /* Select TELNET protocol. */
  4586.     }
  4587. #ifdef RLOGCODE
  4588.     else if (x == RLOGIN_PORT) {
  4589.         ttnproto = NP_RLOGIN;
  4590.     }
  4591. #ifdef CK_KERBEROS
  4592.     /* There is no good way to do this.  If the user didn't tell    */
  4593.     /* which one to use up front.  We may guess wrong if the user   */
  4594.     /* has both Kerberos versions installed and valid TGTs for each */
  4595.     else if (x == KLOGIN_PORT &&
  4596.              ttnproto != NP_K4LOGIN &&
  4597.              ttnproto != NP_K5LOGIN) {
  4598.         if (ck_krb5_is_installed() &&
  4599.             ck_krb5_is_tgt_valid())
  4600.           ttnproto = NP_K5LOGIN;
  4601.         else if (ck_krb4_is_installed() && ck_krb4_is_tgt_valid())
  4602.           ttnproto = NP_K4LOGIN;
  4603.         else
  4604.           ttnproto = NP_K4LOGIN;
  4605.     } else if (x == EKLOGIN_PORT &&
  4606.                ttnproto != NP_EK4LOGIN &&
  4607.                ttnproto != NP_EK5LOGIN) {
  4608.         if (ck_krb5_is_installed() && ck_krb5_is_tgt_valid())
  4609.           ttnproto = NP_EK5LOGIN;
  4610.         else if (ck_krb4_is_installed() && ck_krb4_is_tgt_valid())
  4611.           ttnproto = NP_EK4LOGIN;
  4612.         else
  4613.           ttnproto = NP_EK4LOGIN;
  4614.     }
  4615. #endif /* CK_KERBEROS */
  4616. #endif /* RLOGCODE */
  4617. #ifdef IKS_OPTION
  4618.     else if (x == KERMIT_PORT) {        /* IKS uses Telnet protocol */
  4619.         if (ttnproto == NP_NONE)
  4620.           ttnproto = NP_KERMIT;
  4621.     }
  4622. #endif /* IKS_OPTION */
  4623.  
  4624. #ifdef SO_OOBINLINE
  4625. /*
  4626.   The symbol SO_OOBINLINE is not known to Ultrix 2.0.
  4627.   It means "leave out of band data inline".  The normal value is 0x0100,
  4628.   but don't try this on systems where the symbol is undefined.
  4629. */
  4630. /*
  4631.   Note from Jeff Altman: 12/13/95
  4632.   In implementing rlogin protocol I have come to the conclusion that it is
  4633.   a really bad idea to read out-of-band data inline.
  4634.   At least Windows and OS/2 does not handle this well.
  4635.   And if you need to know that data is out-of-band, then it becomes
  4636.   absolutely pointless.
  4637.  
  4638.   Therefore, at least on OS2 and Windows (NT) I have changed the value of
  4639.   on to 0, so that out-of-band data stays out-of-band.
  4640.  
  4641.   12/18/95
  4642.   Actually, OOB data should be read inline when possible.  Especially with
  4643.   protocols that don't care about the Urgent flag.  This is true with Telnet.
  4644.   With Rlogin, you need to be able to catch OOB data.  However, the best
  4645.   way to do this is to set a signal handler on SIGURG.  This isn't possible
  4646.   on OS/2 and Windows.  But it is in UNIX.  We will also need OOB data for
  4647.   FTP so better create a general mechanism.
  4648.  
  4649.   The reason for making OOB data be inline is that the standard ttinc/ttoc
  4650.   calls can be used for reading that data on UNIX systems.  If we didn't
  4651.   have the OOBINLINE option set then we would have to use recv(,MSG_OOB)
  4652.   to read it.
  4653. */
  4654. #ifdef RLOGCODE
  4655. #ifdef TCPIPLIB
  4656.     if (ttnproto == NP_RLOGIN
  4657. #ifdef CK_KERBEROS
  4658.         || ttnproto == NP_K4LOGIN || ttnproto == NP_EK4LOGIN
  4659.         || ttnproto == NP_K5LOGIN || ttnproto == NP_EK5LOGIN
  4660. #endif /* CK_KERBEROS */
  4661.       )
  4662.       on = 0;
  4663. #else /* TCPIPLIB */
  4664.     if (ttnproto == NP_RLOGIN
  4665. #ifdef CK_KERBEROS
  4666.          || ttnproto == NP_K4LOGIN || ttnproto == NP_EK4LOGIN
  4667.          || ttnproto == NP_K5LOGIN || ttnproto == NP_EK5LOGIN
  4668. #endif /* CK_KERBEROS */
  4669.          ) {
  4670.         debug(F100,"Installing rlogoobh on SIGURG","",0);
  4671.         signal(SIGURG, rlogoobh);
  4672.         on = 0;
  4673.     } else {
  4674.         debug(F100,"Ignoring SIGURG","",0);
  4675.         signal(SIGURG, SIG_DFL);
  4676.     }
  4677. #endif /* TCPIPLIB */
  4678. #endif /* RLOGCODE */
  4679.  
  4680. #ifdef datageneral
  4681.     setsockopt(ttyfd, SOL_SOCKET, SO_OOBINLINE,(char *) &on, sizeof on);
  4682. #else
  4683. #ifdef BSD43
  4684.     setsockopt(ttyfd, SOL_SOCKET, SO_OOBINLINE,(char *) &on, sizeof on);
  4685. #else
  4686. #ifdef OSF1
  4687.     setsockopt(ttyfd, SOL_SOCKET, SO_OOBINLINE,(char *) &on, sizeof on);
  4688. #else
  4689. #ifdef POSIX
  4690.     setsockopt(ttyfd, SOL_SOCKET, SO_OOBINLINE,(char *) &on, sizeof on);
  4691. #else
  4692. #ifdef MOTSV88R4
  4693.     setsockopt(ttyfd, SOL_SOCKET, SO_OOBINLINE,(char *) &on, sizeof on);
  4694. #else
  4695. #ifdef SOLARIS
  4696. /*
  4697.   Maybe this applies to all SVR4 versions, but the other (else) way has been
  4698.   compiling and working fine on all the others, so best not to change it.
  4699. */
  4700.     setsockopt(ttyfd, SOL_SOCKET, SO_OOBINLINE,(char *) &on, sizeof on);
  4701. #else
  4702. #ifdef OSK
  4703.     setsockopt(ttyfd, SOL_SOCKET, SO_OOBINLINE,(char *) &on, sizeof on);
  4704. #else
  4705. #ifdef OS2
  4706.     {
  4707.         int rc;
  4708.         rc = setsockopt(ttyfd,
  4709.                         SOL_SOCKET,
  4710.                         SO_OOBINLINE,
  4711.                         (char *) &on,
  4712.                         sizeof on
  4713.                         );
  4714.         debug(F111,"setsockopt SO_OOBINLINE",on ? "on" : "off" ,rc);
  4715.     }
  4716. #else
  4717. #ifdef VMS /* or, at least, VMS with gcc */
  4718.     setsockopt(ttyfd, SOL_SOCKET, SO_OOBINLINE,(char *) &on, sizeof on);
  4719. #else
  4720. #ifdef CLIX
  4721.     setsockopt(ttyfd, SOL_SOCKET, SO_OOBINLINE,(char *) &on, sizeof on);
  4722. #else
  4723.     setsockopt(ttyfd, SOL_SOCKET, SO_OOBINLINE, &on, sizeof on);
  4724. #endif /* CLIX */
  4725. #endif /* VMS */
  4726. #endif /* OS2 */
  4727. #endif /* OSK */
  4728. #endif /* SOLARIS */
  4729. #endif /* MOTSV88R4 */
  4730. #endif /* POSIX */
  4731. #endif /* BSD43 */
  4732. #endif /* OSF1 */
  4733. #endif /* datageneral */
  4734. #endif /* SO_OOBINLINE */
  4735.  
  4736. #ifndef NOTCPOPTS
  4737. #ifndef datageneral
  4738. #ifdef SOL_SOCKET
  4739. #ifdef TCP_NODELAY
  4740.     no_delay(ttyfd,tcp_nodelay);
  4741. #endif /* TCP_NODELAY */
  4742. #ifdef SO_KEEPALIVE
  4743.     keepalive(ttyfd,tcp_keepalive);
  4744. #endif /* SO_KEEPALIVE */
  4745. #ifdef SO_LINGER
  4746.     ck_linger(ttyfd,tcp_linger, tcp_linger_tmo);
  4747. #endif /* SO_LINGER */
  4748. #ifdef SO_SNDBUF
  4749.     sendbuf(ttyfd,tcp_sendbuf);
  4750. #endif /* SO_SNDBUF */
  4751. #ifdef SO_RCVBUF
  4752.     recvbuf(ttyfd,tcp_recvbuf);
  4753. #endif /* SO_RCVBUF */
  4754. #endif /* SOL_SOCKET */
  4755. #endif /* datageneral */
  4756. #endif /* NOTCPOPTS */
  4757.  
  4758. #ifndef datageneral
  4759.     /* Find out our own IP address. */
  4760.     /* We need the l_addr structure for [E]KLOGIN. */
  4761.     l_slen = sizeof(l_addr);
  4762.     bzero((char *)&l_addr, l_slen);
  4763. #ifndef EXCELAN
  4764.     if (!getsockname(ttyfd, (struct sockaddr *)&l_addr, &l_slen)) {
  4765.         char * s = (char *)inet_ntoa(l_addr.sin_addr);
  4766.         ckstrncpy(myipaddr, s, 20);
  4767.         debug(F110,"getsockname",myipaddr,0);
  4768.     }
  4769. #endif /* EXCELAN */
  4770. #endif /* datageneral */
  4771.  
  4772. /*
  4773.   This is really only needed for Kerberos IV but is useful information in any
  4774.   case.  If we connect to a name that is really a pool, we need to get the
  4775.   name of the machine we are actually connecting to for K4 to authenticate
  4776.   properly.  This way we also update the names properly.
  4777.  
  4778.   However, it is a security hole when used with insecure DNS.
  4779.  
  4780.   Note: This does not work on Windows 95 or Windows NT 3.5x.  This is because
  4781.   of the Microsoft implementation of gethostbyaddr() in both Winsock 1.1
  4782.   and Winsock 2.0 on those platforms.  Their algorithm is:
  4783.  
  4784.   1. Check the HOSTENT cache.
  4785.   2. Check the HOSTS file at %SystemRoot%\System32\DRIVERS\ETC.
  4786.   3. Do a DNS query if the DNS server is configured for name resolution.
  4787.   4. Do an additional NetBIOS remote adapter status to an IP address for its
  4788.      NetBIOS name table. This step is specific only to the Windows NT version
  4789.      3.51 implementation.
  4790.  
  4791.   The problem is the use of the HOSTENT cache.  It means that gethostbyaddr()
  4792.   can not be used to resolve the real name of machine if it was originally
  4793.   accessed by an alias used to represent a cluster.
  4794. */
  4795.      if ((tcp_rdns && dns || tcp_rdns == SET_ON
  4796. #ifdef CK_KERBEROS
  4797.          || tcp_rdns == SET_AUTO &&
  4798.           (ck_krb5_is_installed() || ck_krb4_is_installed())
  4799. #endif /* CK_KERBEROS */
  4800.          )
  4801. #ifndef NOHTTP
  4802.           && (tcp_http_proxy == NULL)
  4803. #endif /* NOHTTP */
  4804. #ifdef CK_SSL
  4805.           && !(ssl_only_flag || tls_only_flag)
  4806. #endif /* CK_SSL */
  4807.          ) {
  4808. #ifdef NT
  4809.         if (isWin95())
  4810.           sleep(1);
  4811. #endif /* NT */
  4812.         if (!quiet) {
  4813.             printf(" Reverse DNS Lookup... ");
  4814.             fflush(stdout);
  4815.         }
  4816.         if (host = gethostbyaddr((char *)&r_addr.sin_addr,4,PF_INET)) {
  4817.             char * s;
  4818.             host = ck_copyhostent(host);
  4819.             debug(F100,"netopen gethostbyname != NULL","",0);
  4820.             if (!quiet) {
  4821.                 printf("(OK)\n");
  4822.                 fflush(stdout);
  4823.             }
  4824.             s = host->h_name;
  4825.             if (!s) {                   /* This can happen... */
  4826.                 debug(F100,"netopen host->h_name is NULL","",0);
  4827.                 s = "";
  4828.             }
  4829.             /* Something is wrong with inet_ntoa() on HPUX 10.xx */
  4830.             /* The compiler says "Integral value implicitly converted to */
  4831.             /* pointer in assignment."  The prototype is right there */
  4832.             /* in <arpa/inet.h> so what's the problem? */
  4833.             /* Ditto in HP-UX 5.x, but not 8.x or 9.x... */
  4834.             if (!*s) {                  /* No name so substitute the address */
  4835.                 debug(F100,"netopen host->h_name is empty","",0);
  4836.                 s = inet_ntoa(r_addr.sin_addr); /* Convert address to string */
  4837.                 if (!s)                 /* Trust No 1 */
  4838.                   s = "";
  4839.                 if (*s) {               /* If it worked, use this string */
  4840.                     ckstrncpy(ipaddr,s,20);
  4841.                 }
  4842.                 s = ipaddr;             /* Otherwise stick with the IP */
  4843.                 if (!*s)                /* or failing that */
  4844.                   s = namecopy;         /* the name we were called with. */
  4845.             }
  4846.             if (*s) {                   /* Copying into our argument? */
  4847.                 ckstrncpy(name,s,80);   /* Bad Bad Bad */
  4848.                 if ( (80-strlen(name)) > (strlen(svcbuf)+1) ) {
  4849.                     ckstrncat(name,":",80-strlen(name));
  4850.                     ckstrncat(name,svcbuf,80-strlen(name));
  4851.                 }
  4852.             }
  4853.             if (!quiet && *s
  4854. #ifndef NOICP
  4855.                 && !doconx
  4856. #endif /* NOICP */
  4857.                 ) {
  4858.                 printf(" %s connected on port %s\n",s,p);
  4859. #ifdef BETADEBUG
  4860.                 /* This is simply for testing the DNS entries */
  4861.                 if (host->h_aliases) {
  4862.                     char ** a = host->h_aliases;
  4863.                     while (*a) {
  4864.                         printf(" alias => %s\n",*a);
  4865.                         a++;
  4866.                     }
  4867.                 }
  4868. #endif /* BETADEBUG */
  4869.             }
  4870.         } else {
  4871.             if (!quiet) printf("Failed.\n");
  4872.         }
  4873.     } else if (!quiet) printf("(OK)\n");
  4874.     if (!quiet) fflush(stdout);
  4875.  
  4876.     /* This should already have been done but just in case */
  4877.     ckstrncpy(ipaddr,(char *)inet_ntoa(r_addr.sin_addr),20);
  4878.  
  4879. #ifdef CK_SECURITY
  4880.  
  4881.     /* Before Initialization Telnet/Rlogin Negotiations Init Kerberos */
  4882. #ifndef NOHTTP
  4883.     if (tcp_http_proxy) {
  4884.         for (i=strlen(proxycopy); i >= 0 ; i--)
  4885.             if ( proxycopy[i] == ':' )
  4886.                 proxycopy[i] = '\0';
  4887.     }
  4888. #endif /* NOHTTP */
  4889.     ck_auth_init(
  4890. #ifndef NOHTTP
  4891.                  tcp_http_proxy ? proxycopy :
  4892. #endif /* NOHTTP */
  4893.                  (tcp_rdns && host && host->h_name && host->h_name[0]) ?
  4894.                  (char *)host->h_name : (namecopy2[0] ? namecopy2 : 
  4895.                                         (namecopy[0] ? namecopy : ipaddr)),
  4896.                  ipaddr,
  4897.                  uidbuf,
  4898.                  ttyfd
  4899.                  );
  4900. #endif /* CK_SECURITY */
  4901. #ifdef CK_SSL
  4902.     if (ck_ssleay_is_installed()) {
  4903.         if (!ssl_tn_init(SSL_CLIENT)) {
  4904.             debug(F100,"netopen ssl_tn_init() failed","",0);
  4905.             if (bio_err!=NULL) {
  4906.                 BIO_printf(bio_err,"ssl_tn_init() failed\n");
  4907.                 ERR_print_errors(bio_err);
  4908.             } else {
  4909.                 fflush(stderr);
  4910.                 fprintf(stderr,"ssl_tn_init() failed\n");
  4911.                 ERR_print_errors_fp(stderr);
  4912.             }
  4913.             if (tls_only_flag || ssl_only_flag) {
  4914.                 debug(F100,"netopen ssl/tls required","",0);
  4915.                 netclos();
  4916.                 return(-1);
  4917.             }
  4918.  
  4919.             /* we will continue to accept the connection   */
  4920.             /* without SSL or TLS support unless required. */
  4921.             if ( TELOPT_DEF_S_ME_MODE(TELOPT_START_TLS) != TN_NG_MU )
  4922.                 TELOPT_DEF_S_ME_MODE(TELOPT_START_TLS) = TN_NG_RF;
  4923.             if ( TELOPT_DEF_S_U_MODE(TELOPT_START_TLS) != TN_NG_MU )
  4924.                 TELOPT_DEF_S_U_MODE(TELOPT_START_TLS) = TN_NG_RF;
  4925.             if ( TELOPT_DEF_C_ME_MODE(TELOPT_START_TLS) != TN_NG_MU )
  4926.                 TELOPT_DEF_C_ME_MODE(TELOPT_START_TLS) = TN_NG_RF;
  4927.             if ( TELOPT_DEF_C_U_MODE(TELOPT_START_TLS) != TN_NG_MU )
  4928.                 TELOPT_DEF_C_U_MODE(TELOPT_START_TLS) = TN_NG_RF;
  4929.         } else if ( ck_ssl_outgoing(ttyfd) < 0 ) {
  4930.             debug(F100,"ck_ssl_outgoing() failed","",0);
  4931.             netclos();
  4932.             return(-1);
  4933.         }
  4934.     }
  4935. #endif /* CK_SSL */
  4936.  
  4937. #ifdef RLOGCODE
  4938.     if (ttnproto == NP_RLOGIN
  4939. #ifdef CK_KERBEROS
  4940.         || ttnproto == NP_K4LOGIN || ttnproto == NP_EK4LOGIN
  4941.         || ttnproto == NP_K5LOGIN || ttnproto == NP_EK5LOGIN
  4942. #endif /* CK_KERBEROS */
  4943.         ) {                             /* Similar deal for rlogin */
  4944.         if (rlog_ini((CHAR *)((tcp_rdns && host && host->h_name && host->h_name[0]) ?
  4945.                    host->h_name : (namecopy2[0] ? namecopy2 : 
  4946.                            (namecopy[0] ? namecopy : ipaddr))),
  4947.                      service->s_port,
  4948.                      &l_addr,&r_addr
  4949.                      ) < 0) {
  4950.             debug(F100,"rlogin initialization failed","",0);
  4951.             netclos();
  4952.             return(-1);
  4953.         }
  4954.     } else
  4955. #endif /* RLOGCODE */
  4956.     if (tn_ini() < 0) {                 /* Start Telnet negotiations. */
  4957.         netclos();
  4958.         return(-1);                     /* Gone, so open failed.  */
  4959.     }
  4960.     if (ttchk() < 0) {
  4961.         netclos();
  4962.         return(-1);
  4963.     }
  4964. #ifdef CK_KERBEROS
  4965. #ifdef KRB5_U2U
  4966.    if ( ttnproto == NP_K5U2U ) {
  4967.        if (k5_user_to_user_client_auth()) {
  4968.            netclos();
  4969.            return(-1);
  4970.        }
  4971.    }
  4972. #endif /* KRB5_U2U */
  4973. #endif /* CK_KERBEROS */
  4974.  
  4975.     debug(F101,"netopen service","",svcnum);
  4976.     debug(F110,"netopen name",name,0);
  4977.  
  4978.     if (lcl) if (*lcl < 0)              /* Local mode. */
  4979.       *lcl = 1;
  4980. #endif /* TCPSOCKET */
  4981.     return(0);                          /* Done. */
  4982. }
  4983.  
  4984. /*  N E T C L O S  --  Close current network connection.  */
  4985.  
  4986. #ifndef NOLOCAL
  4987. _PROTOTYP(VOID slrestor,(VOID));
  4988. #ifdef CK_SSL
  4989. int tls_norestore = 0;
  4990. #endif /* CK_SSL */
  4991. #endif /* NOLOCAL */
  4992.  
  4993. int
  4994. netclos() {
  4995.     static int close_in_progress = 0;
  4996.     int x = 0, y, z;
  4997.     debug(F101,"netclos","",ttyfd);
  4998.  
  4999. #ifdef NETLEBUF
  5000.     if (!tt_push_inited)
  5001.       le_init();
  5002. #endif /* NETLEBUF */
  5003.  
  5004.     if (ttyfd == -1)                    /* Was open? */
  5005.       return(0);                        /* Wasn't. */
  5006.  
  5007.     if (close_in_progress)
  5008.       return(0);
  5009.     close_in_progress = 1;              /* Remember */
  5010.  
  5011. #ifndef NOLOCAL
  5012.     /* This function call should not be here since this is a direct call */
  5013.     /* from an I/O routine to a user interface level function.  However, */
  5014.     /* the reality is that we do not have pure interfaces.  If we ever   */
  5015.     /* decide to clean this up the UI level should assign this function  */
  5016.     /* via a pointer assignment.  - Jeff 9/10/1999                       */
  5017. #ifdef CK_SSL
  5018.     if (!tls_norestore)
  5019. #endif /* CK_SSL */
  5020.       slrestor();
  5021. #endif /* NOLOCAL */
  5022. #ifdef OS2
  5023.     RequestTCPIPMutex(SEM_INDEFINITE_WAIT);
  5024. #else /* OS2 */
  5025.     if (ttyfd > -1)                     /* Was. */
  5026. #endif /* OS2 */
  5027.       {
  5028. #ifdef VMS
  5029.       y = 1;                          /* Turn on nonblocking reads */
  5030.       z = socket_ioctl(ttyfd,FIONBIO,&y);
  5031.       debug(F111,"netclos FIONBIO","on",z);
  5032. #endif /* VMS */
  5033. #ifdef TNCODE
  5034.           if (ttnproto == NP_TELNET) {
  5035.             if (!TELOPT_ME(TELOPT_LOGOUT)) {
  5036.         /* Send LOGOUT option before close */
  5037.         if (tn_sopt(DO,TELOPT_LOGOUT) >= 0) {
  5038.             TELOPT_UNANSWERED_DO(TELOPT_LOGOUT) = 1;
  5039.             /* It would be nice to call tn_wait but we can't */
  5040.         }
  5041.         }
  5042.             tn_push();            /* Place any waiting data into input*/
  5043.           }
  5044. #endif /* TNCODE */
  5045. #ifdef CK_SSL
  5046.           if (ssl_active_flag) {
  5047.               if (ssl_debug_flag)
  5048.                 BIO_printf(bio_err,"calling SSL_shutdown\n");
  5049.               SSL_shutdown(ssl_con);
  5050.               ssl_active_flag = 0;
  5051.           }
  5052.           if (tls_active_flag) {
  5053.               if (ssl_debug_flag)
  5054.                 BIO_printf(bio_err,"calling SSL_shutdown\n");
  5055.               SSL_shutdown(tls_con);
  5056.               tls_active_flag = 0;
  5057.           }
  5058. #endif /* CK_SSL */
  5059. #ifdef VMS
  5060.           ck_cancio();                  /* Cancel any outstanding reads. */
  5061. #endif /* VMS */
  5062. #ifdef TCPIPLIB
  5063.           x = socket_close(ttyfd);      /* Close it. */
  5064. #else
  5065. #ifndef OS2
  5066. #ifdef IBMX25
  5067.         if (ttnet == NET_IX25) {
  5068.             /* riehm: should send a disc_req - but only if link is still OK */
  5069.             x = x25clear();
  5070.             close(ttyfd);
  5071.             if (x25serverfd) {
  5072.                   /* we were the passive client of a server, now we
  5073.                    * go back to being the normal client.
  5074.                    * I hope that kermit can cope with the logic that
  5075.                    * there can still be a connection after netclos
  5076.                    * has been called.
  5077.                    */
  5078.                   ttyfd = x25serverfd;
  5079.                   x25serverfd = 0;
  5080.                   /*
  5081.                    * need to close the server connection too - because
  5082.                    * all file descriptors connected to the NPI have the
  5083.                    * same status.
  5084.                    *
  5085.                    * The problem is that any waiting connections get
  5086.                    * lost, the client doesn't realise, and hangs.
  5087.                    */
  5088.                   netclos();
  5089.               }
  5090.             x25_state = X25_CLOSED;     /* riehm: dead code? */
  5091.         } else
  5092. #endif /* IBMX25 */
  5093.           x = close(ttyfd);
  5094. #endif /* OS2 */
  5095. #endif /* TCPIPLIB */
  5096.       }
  5097.     ttyfd = -1;                         /* Mark it as closed. */
  5098.     wasclosed = 1;
  5099. #ifdef OS2
  5100.     ReleaseTCPIPMutex();
  5101. #endif /* OS2 */
  5102. #ifdef TNCODE
  5103. #ifdef CK_FORWARD_X
  5104.     fwdx_close_all();                   /* Shut down any Forward X sockets */
  5105. #endif /* CK_FORWARD_X */
  5106.     tn_reset();                   /* The Reset Telnet Option table.  */
  5107.     debug(F100,"netclose setting tn_init = 0","",0);
  5108.     tn_init = 0;                        /* Remember about telnet protocol... */
  5109.     sstelnet = 0;                       /* Client-side Telnet */
  5110. #endif /* TNCODE */
  5111.     *ipaddr = '\0';                     /* Zero the IP address string */
  5112.     tcp_incoming = 0;                   /* No longer incoming */
  5113.     /* Don't reset ttnproto so that we can remember which protocol is in use */
  5114.  
  5115. #ifdef TCPIPLIB
  5116. /*
  5117.   Empty the internal buffers so they won't be used as invalid input on
  5118.   the next connect attempt (rlogin).
  5119. */
  5120.     ttibp = 0;
  5121.     ttibn = 0;
  5122. #endif /* TCPIPLIB */
  5123. #ifdef CK_KERBEROS
  5124.     /* If we are automatically destroying Kerberos credentials on Close */
  5125.     /* do it now. */
  5126. #ifdef KRB4
  5127.     if (krb4_autodel == KRB_DEL_CL) {
  5128.         extern struct krb_op_data krb_op;
  5129.         krb_op.version = 4;
  5130.         krb_op.cache = NULL;
  5131.         ck_krb4_destroy(&krb_op);
  5132.     }
  5133. #endif /* KRB4 */
  5134. #ifdef KRB5
  5135.     if (krb5_autodel == KRB_DEL_CL) {
  5136.         extern struct krb_op_data krb_op;
  5137.         extern char * krb5_d_cc;
  5138.         krb_op.version = 5;
  5139.         krb_op.cache = krb5_d_cc;
  5140.         ck_krb5_destroy(&krb_op);
  5141.     }
  5142. #endif /* KRB5 */
  5143. #endif /* CK_KERBEROS */
  5144.     close_in_progress = 0;              /* Remember we are done. */
  5145.     return(x);
  5146. }
  5147.  
  5148. #ifdef OS2
  5149. int
  5150. os2socketerror( int s_errno ) {
  5151. #ifdef OS2ONLY
  5152.     if (s_errno > 0 && s_errno <= SOCBASEERR) {
  5153.         /* in OS/2, there is a problem with threading in that
  5154.          * the value of errno is not thread safe.  It can be
  5155.          * set to a value from a previous library call and if
  5156.          * it was not cleared it will appear here.  Only treat
  5157.          * valid socket error codes as errors in this function.
  5158.          */
  5159.         debug(F100,"os2socketerror errno.h","",0);
  5160.         socket_errno = 0;
  5161.         return(0);
  5162.     }
  5163. #endif /* OS2ONLY */
  5164.  
  5165.     switch (s_errno) {
  5166.       case 0:                           /* NO ERROR */
  5167.         debug(F100,"os2socketerror NOERROR","",0);
  5168.         return(0);
  5169. #ifdef NT
  5170.       case WSAECONNRESET:
  5171. #else /* NT */
  5172.       case SOCECONNRESET:
  5173.       case SOCECONNRESET - SOCBASEERR:
  5174. #endif /* NT */
  5175.         debug(F100,"os2socketerror ECONRESET","",0);
  5176.         tn_debug("ECONRESET");
  5177.         netclos();              /* *** *** */
  5178.         return(-1);             /* Connection is broken. */
  5179. #ifdef NT
  5180.       case WSAECONNABORTED:
  5181. #else /* NT */
  5182.       case SOCECONNABORTED:
  5183.       case SOCECONNABORTED - SOCBASEERR:
  5184. #endif /* NT */
  5185.         debug(F100,"os2socketerror ECONNABORTED","",0);
  5186.         tn_debug("ECONNABORTED");
  5187.         netclos();              /* *** *** */
  5188.         return(-1);             /* Connection is broken. */
  5189. #ifdef NT
  5190.       case WSAENETRESET:
  5191. #else /* NT */
  5192.       case SOCENETRESET:
  5193.       case SOCENETRESET - SOCBASEERR:
  5194. #endif /* NT */
  5195.         debug(F100,"os2socketerror ENETRESET","",0);
  5196.         tn_debug("ENETRESET");
  5197.         netclos();              /* *** *** */
  5198.         return(-1);             /* Connection is broken. */
  5199. #ifdef NT
  5200.       case WSAENOTCONN:
  5201. #else /* NT */
  5202.       case SOCENOTCONN:
  5203.       case SOCENOTCONN - SOCBASEERR:
  5204. #endif /* NT */
  5205.         debug(F100,"os2socketerror ENOTCONN","",0);
  5206.         tn_debug("ENOTCONN");
  5207.         netclos();                      /* *** *** */
  5208.         return(-1);                     /* Connection is broken. */
  5209. #ifdef NT
  5210.       case WSAESHUTDOWN:
  5211.         debug(F100,"os2socketerror ESHUTDOWN","",0);
  5212.         tn_debug("ESHUTDOWN");
  5213.         netclos();                      /* *** *** */
  5214.         return(-1);                     /* Connection is broken. */
  5215. #endif /* NT */
  5216. #ifdef NT
  5217.       case WSAEWOULDBLOCK:
  5218. #else
  5219.       case SOCEWOULDBLOCK:
  5220.       case SOCEWOULDBLOCK - SOCBASEERR:
  5221. #endif /* NT */
  5222.         debug(F100,"os2socketerror EWOULDBLOCK","",0);
  5223.         return(0);
  5224. #ifdef NT
  5225.       case ERROR_IO_INCOMPLETE:
  5226.       case ERROR_IO_PENDING:
  5227.       case ERROR_OPERATION_ABORTED:
  5228.         return(0);
  5229. #endif /* NT */
  5230.       default:
  5231.         return(-2);
  5232.     }
  5233.     return(0);
  5234. }
  5235. #endif /* OS2 */
  5236.  
  5237. /*  N E T T C H K  --  Check if network up, and how many bytes can be read */
  5238. /*
  5239.   Returns number of bytes waiting, or -1 if connection has been dropped.
  5240. */
  5241. int                                     /* Check how many bytes are ready */
  5242. nettchk() {                             /* for reading from network */
  5243. #ifdef TCPIPLIB
  5244.     long count = 0;
  5245.     int x = 0, z;
  5246.     long y;
  5247.     char c;
  5248.     int rc;
  5249. #ifdef NT
  5250.     extern int ionoblock;               /* For Overlapped I/O */
  5251. #endif /* NT */
  5252.  
  5253.     debug(F101,"nettchk entry ttibn","",ttibn);
  5254.     debug(F101,"nettchk entry ttibp","",ttibp);
  5255.  
  5256. #ifdef NETLEBUF
  5257.     {
  5258.         int n = 0;
  5259.         if (ttpush >= 0)
  5260.           n++;
  5261.         n += le_inbuf();
  5262.         if (n > 0)
  5263.           return(n);
  5264.     }
  5265. #endif /* NETLEBUF */
  5266.  
  5267. #ifndef OS2
  5268. #ifndef BEBOX
  5269.     socket_errno = 0; /* This is a function call in NT, and BeOS */
  5270. #endif /* BEBOX */
  5271. #endif /* OS2 */
  5272.  
  5273.     if (ttyfd == -1) {
  5274.         debug(F100,"nettchk socket is closed","",0);
  5275.         return(-1);
  5276.     }
  5277. /*
  5278.   Note: this socket_ioctl() call does NOT return an error if the
  5279.   connection has been broken.  (At least not in MultiNet.)
  5280. */
  5281. #ifdef COMMENT
  5282. /*  Another trick that can be tried here is something like this: */
  5283.  
  5284.     if (ttnet == NET_TCPB) {
  5285.         char dummy;
  5286.         x = read(ttyfd,&dummy,0);       /* Try to read nothing */
  5287.         if (x < 0) {                    /* "Connection reset by peer" */
  5288.             perror("TCP/IP");           /* or somesuch... */
  5289.             ttclos(0);                  /* Close our end too. */
  5290.             return(-1);
  5291.         }
  5292.     }
  5293. #endif /* COMMENT */
  5294.  
  5295.  
  5296. #ifdef CK_SSL
  5297.     if (ssl_active_flag) {
  5298. #ifndef IKSDONLY
  5299. #ifdef OS2
  5300.         if ( IsConnectMode() ) {
  5301.             debug(F101,"nettchk (ssl_active_flag) returns","",count);
  5302.             return(0);
  5303.         }
  5304. #endif /* OS2 */
  5305. #endif /* IKSDONLY */
  5306.         count = SSL_pending(ssl_con);
  5307.         if (count < 0) {
  5308.             debug(F111,"nettchk","SSL_pending error",count);
  5309.             netclos();
  5310.             return(-1);
  5311.         }
  5312.         if ( count > 0 )
  5313.             return(count);                  /* Don't perform a read */
  5314.     } else if (tls_active_flag) {
  5315. #ifndef IKSDONLY
  5316. #ifdef OS2
  5317.         if ( IsConnectMode() ) {
  5318.             debug(F101,"nettchk (tls_active_flag) returns","",count);
  5319.             return(0);
  5320.         }
  5321. #endif /* OS2 */
  5322. #endif /* IKSDONLY */
  5323.         count = SSL_pending(tls_con);
  5324.         if (count < 0) {
  5325.             debug(F111,"nettchk","TLS_pending error",count);
  5326.             netclos();
  5327.             return(-1);
  5328.         }
  5329.         if ( count > 0 )
  5330.             return(count);                  /* Don't perform a read */
  5331.     } else
  5332. #endif /* CK_SSL */
  5333.  
  5334.     if (socket_ioctl(ttyfd,FIONREAD,
  5335. #ifdef COMMENT
  5336.     /* Now we've changed the ioctl(..,..,x) prototype for DECC to (void *) */
  5337. #ifdef __DECC
  5338.     /* NOTE: "&count" might need to be "(char *)&count" in some settings. */
  5339.                      /* Cast needed for DECC 4.1 & later? */
  5340.                      /* Maybe, but __DECC_VER only exists in 5.0 and later */
  5341.                      (char *)
  5342. #endif /* __DECC */
  5343. #endif /* COMMENT */
  5344.                      &count
  5345.                      ) < 0) {
  5346.         debug(F101,"nettchk socket_ioctl error","",socket_errno);
  5347.         /* If the connection is gone, the connection is gone. */
  5348.         netclos();
  5349. #ifdef NT_TCP_OVERLAPPED
  5350.         /* Is there anything in the overlapped I/O buffers? */
  5351.         count += OverlappedDataWaiting();
  5352. #endif /* NT_TCP_OVERLAPPED */
  5353.         count += ttibn;
  5354.         return(count>0?count:-1);
  5355.     }
  5356.     debug(F101,"nettchk count","",count);
  5357. #ifdef NT_TCP_OVERLAPPED
  5358.     /* Is there anything in the overlapped I/O buffers? */
  5359.     count += OverlappedDataWaiting();
  5360.     debug(F101,"nettchk count w/overlapped","",count);
  5361. #endif /* NT_TCP_OVERLAPPED */
  5362.  
  5363. #ifdef OS2
  5364. #ifndef IKSDONLY
  5365.     if ( IsConnectMode() ) {
  5366.         debug(F101,"nettchk (FIONREAD) returns","",count);
  5367.         return(count);
  5368.     }
  5369. #endif /* IKSDONLY */
  5370. #endif /* OS2 */
  5371.  
  5372. /* For the sake of efficiency, if there is still data in the ttibuf */
  5373. /* do not go to the bother of checking to see of the connection is  */
  5374. /* still valid.  The handle is still good, so just return the count */
  5375. /* of the bytes that we already have left to process.               */
  5376. #ifdef OS2
  5377.     if ( count > 0 || ttibn > 0 ) {
  5378.         count+=ttibn;
  5379.         debug(F101,"nettchk (count+ttibn > 0) returns","",count);
  5380.         return(count);
  5381.     } else {
  5382.         RequestTCPIPMutex(SEM_INDEFINITE_WAIT);
  5383.         if ( ttibn == 0 )
  5384.             ttibp = 0;      /* reset for next read */
  5385.     }
  5386. #else /* OS2 */
  5387.     if ( count > 0 || ttibn > 0 ) {
  5388.         debug(F101,"nettchk returns","",count+ttibn);
  5389.         return(count+ttibn);
  5390.     }
  5391.     ttibn = ttibp = 0;
  5392. #endif /* OS2 */
  5393.  
  5394. /*
  5395.   The following code works well in most settings, but messes things up in
  5396.   others, including CMU/Tek TCP/IP and UCX 2.0, where it somehow manages to
  5397.   make it impossible to ever make a new connection to the same host again with
  5398.   CONNECT, once it has been logged out from the first time.  Not even if you
  5399.   HANGUP first, or SET HOST<CR>, or SET LINE<CR>.  Reportedly, however, it
  5400.   does work OK in later releases of UCX.  But there is no way we can
  5401.   accommodate both old and new -- we might have static linking or dynamic
  5402.   linking, etc etc.  If we have static, I only have access to 2.0, where this
  5403.   doesn't work, etc etc blah blah.
  5404.  
  5405.   In the following lines, we define a symbol NOCOUNT for builds where we want
  5406.   to omit this code.  By default, it is omitted for CMU/Tek.  You can force
  5407.   omission of it for other combinations by defining NOCOUNT in CFLAGS.  You
  5408.   can force inclusion of this code, even for CMU/Tek, by including NONOCOUNT
  5409.   in CFLAGS.
  5410. */
  5411. #ifdef NONOCOUNT
  5412. #ifdef NOCOUNT
  5413. #undef NOCOUNT
  5414. #endif /* NOCOUNT */
  5415. #else
  5416. #ifndef NOCOUNT
  5417. #ifdef CMU_TCPIP
  5418. #define NOCOUNT
  5419. #endif /* CMU_TCPIP */
  5420. #endif /* NOCOUNT */
  5421. #endif /* NONOCOUNT */
  5422.  
  5423.  
  5424.     /* From this point forward we have a possible race condition in K95
  5425.      * due to its use of multiple threads.  Therefore, we must ensure
  5426.      * that only one thread attempt to read/write from the socket at a
  5427.      * time.  Otherwise, it is possible for a buffer to be overwritten.
  5428.      */
  5429.     /* we know now that count >= 0 and that ttibn == 0 */
  5430.  
  5431.     if (count == 0
  5432. #ifdef RLOGCODE
  5433. #ifdef CK_KERBEROS
  5434.         && ttnproto != NP_EK4LOGIN && ttnproto != NP_EK5LOGIN
  5435. #endif /* CK_KERBEROS */
  5436. #endif /* RLOGCODE */
  5437.         ) {
  5438.         int s_errno = 0;
  5439. #ifndef NOCOUNT
  5440. /*
  5441.   Here we need to tell the difference between a 0 count on an active
  5442.   connection, and a 0 count because the remote end of the socket broke the
  5443.   connection.  There is no mechanism in TGV MultiNet (or WIN/TCP?) to query
  5444.   the status of the connection, so we have to do a read.  -1 means there was
  5445.   no data available (socket_errno == EWOULDBLOCK), 0 means the connection is
  5446.   down.  But if, by chance, we actually get a character, we have to put it
  5447.   where it won't be lost.
  5448. */
  5449. #ifndef NON_BLOCK_IO
  5450. #ifdef OS2
  5451. #ifdef CK_SSL
  5452.         RequestSSLMutex(SEM_INDEFINITE_WAIT);
  5453. #endif /* CK_SSL */
  5454. #endif /* OS2 */
  5455.         y = 1;                          /* Turn on nonblocking reads */
  5456.         z = socket_ioctl(ttyfd,FIONBIO,&y);
  5457.         debug(F111,"nettchk FIONBIO","on",z);
  5458. #ifdef OS2
  5459. #ifdef CK_SSL
  5460.         ReleaseSSLMutex();
  5461. #endif /* CK_SSL */
  5462. #endif /* OS2 */
  5463. #endif /* NON_BLOCK_IO */
  5464. #ifdef NT_TCP_OVERLAPPED
  5465.         ionoblock = 1;                  /* For Overlapped I/O */
  5466. #endif /* NT_TCP_OVERLAPPED */
  5467. #ifdef CK_SSL
  5468.         if ( ssl_active_flag || tls_active_flag ) {
  5469. #ifdef OS2
  5470.       ssl_read:
  5471.             x = SSL_read( ssl_active_flag?ssl_con:tls_con,
  5472.                           &ttibuf[ttibp+ttibn],
  5473.                           TTIBUFL-ttibp-ttibn );
  5474.             switch (SSL_get_error(ssl_active_flag?ssl_con:tls_con,x)) {
  5475.             case SSL_ERROR_NONE:
  5476.                 debug(F111,"nettchk SSL_ERROR_NONE","x",x);
  5477.                 break;
  5478.             case SSL_ERROR_WANT_WRITE:
  5479.                 debug(F100,"nettchk SSL_ERROR_WANT_WRITE","",0);
  5480.                 x = -1;
  5481.                 break;
  5482.             case SSL_ERROR_WANT_READ:
  5483.                 debug(F100,"nettchk SSL_ERROR_WANT_READ","",0);
  5484.                 x = -1;
  5485.                 break;
  5486.             case SSL_ERROR_SYSCALL:
  5487.                 if ( x == 0 ) { /* EOF */
  5488.                     netclos();
  5489.                     rc = -1;
  5490.                     goto nettchk_return;
  5491.               } else {
  5492. #ifdef NT
  5493.                   int gle = GetLastError();
  5494. #endif /* NT */
  5495. #ifndef NON_BLOCK_IO
  5496. #ifdef OS2
  5497. #ifdef CK_SSL
  5498.           RequestSSLMutex(SEM_INDEFINITE_WAIT);
  5499. #endif /* CK_SSL */
  5500. #endif /* OS2 */
  5501.           y = 0;                          /* Turn off nonblocking reads */
  5502.           z = socket_ioctl(ttyfd,FIONBIO,&y);
  5503.           debug(F111,"nettchk FIONBIO","off",z);
  5504. #ifdef OS2
  5505. #ifdef CK_SSL
  5506.           ReleaseSSLMutex();
  5507. #endif /* CK_SSL */
  5508. #endif /* OS2 */
  5509. #endif /* NON_BLOCK_IO */
  5510. #ifdef NT_TCP_OVERLAPPED
  5511.           ionoblock = 0;                  /* For Overlapped I/O */
  5512. #endif /* NT_TCP_OVERLAPPED */
  5513. #ifdef NT
  5514.                   debug(F111,"nettchk SSL_ERROR_SYSCALL",
  5515.                          "GetLastError()",gle);
  5516.                   rc = os2socketerror(gle);
  5517.                   if (rc == -1)
  5518.                       rc = -2;
  5519.                   else if ( rc == -2 )
  5520.                       rc = -1;
  5521.           goto nettchk_return;
  5522. #endif /* NT */
  5523.                   break;
  5524.               }
  5525.           case SSL_ERROR_WANT_X509_LOOKUP:
  5526.                 debug(F100,"nettchk SSL_ERROR_WANT_X509_LOOKUP","",0);
  5527.                 break;
  5528.             case SSL_ERROR_SSL:
  5529.                 if (bio_err!=NULL) {
  5530.                     int len;
  5531.                     extern char ssl_err[];
  5532.                     BIO_printf(bio_err,"nettchk() SSL_ERROR_SSL\n");
  5533.                     ERR_print_errors(bio_err);
  5534.                     len = BIO_read(bio_err,ssl_err,SSL_ERR_BFSZ);
  5535.                     ssl_err[len < SSL_ERR_BFSZ ? len : SSL_ERR_BFSZ] = '\0';
  5536.                     debug(F110,"nettchk SSL_ERROR_SSL",ssl_err,0);
  5537.                     if (ssl_debug_flag)
  5538.                         printf(ssl_err);
  5539.                 } else if (ssl_debug_flag) {
  5540.                     debug(F100,"nettchk SSL_ERROR_SSL","",0);
  5541.                     fflush(stderr);
  5542.                     fprintf(stderr,"nettchk() SSL_ERROR_SSL\n");
  5543.                     ERR_print_errors_fp(stderr);
  5544.                 }
  5545. #ifdef COMMENT
  5546.                 netclos();
  5547.                 rc = -1;
  5548.         goto nettchk_return;
  5549. #else
  5550.                 x = -1;
  5551.         break;
  5552. #endif
  5553.           case SSL_ERROR_ZERO_RETURN:
  5554.                 debug(F100,"nettchk SSL_ERROR_ZERO_RETURN","",0);
  5555.                 netclos();
  5556.                 rc = -1;
  5557.                 goto nettchk_return;
  5558.             default:
  5559.                 debug(F100,"nettchk SSL_ERROR_?????","",0);
  5560.                 netclos();
  5561.                 rc = -1;
  5562.                 goto nettchk_return;
  5563.             }
  5564. #else /* OS2 */
  5565.         /* Do not block */
  5566.         x = -1;
  5567. #endif /* OS2 */
  5568.         } else
  5569. #endif /* CK_SSL */
  5570.         {
  5571. #ifdef OS2
  5572.         x = socket_read(ttyfd,&ttibuf[ttibp+ttibn],
  5573.                          TTIBUFL-ttibp-ttibn);  /* Returns -1 if no data */
  5574. #else /* OS2 */
  5575.         x = socket_read(ttyfd,&c,1);    /* Returns -1 if no data */
  5576. #endif /* OS2 */
  5577.         }
  5578.         s_errno = socket_errno;         /* socket_errno may be a function */
  5579.         debug(F101,"nettchk socket_read","",x);
  5580.  
  5581. #ifndef NON_BLOCK_IO
  5582. #ifdef OS2
  5583. #ifdef CK_SSL
  5584.         RequestSSLMutex(SEM_INDEFINITE_WAIT);
  5585. #endif /* CK_SSL */
  5586. #endif /* OS2 */
  5587.         y = 0;                          /* Turn off nonblocking reads */
  5588.         z = socket_ioctl(ttyfd,FIONBIO,&y);
  5589.         debug(F111,"nettchk FIONBIO","off",z);
  5590. #ifdef OS2
  5591. #ifdef CK_SSL
  5592.         ReleaseSSLMutex();
  5593. #endif /* CK_SSL */
  5594. #endif /* OS2 */
  5595. #endif /* NON_BLOCK_IO */
  5596. #ifdef NT_TCP_OVERLAPPED
  5597.         ionoblock = 0;                  /* For Overlapped I/O */
  5598. #endif /* NT_TCP_OVERLAPPED */
  5599.  
  5600.         if (x == -1) {
  5601.             debug(F101,"nettchk socket_read errno","",s_errno);
  5602. #ifdef OS2
  5603.             if (os2socketerror(s_errno) < 0) {
  5604.                 rc = -1;
  5605.                 goto nettchk_return;
  5606.             }
  5607. #endif /* OS2 */
  5608.         } else if (x == 0) {
  5609.             debug(F100,"nettchk connection closed","",0);
  5610.             netclos();                  /* *** *** */
  5611.             rc = -1;
  5612.             goto nettchk_return;
  5613.         }
  5614.         if (x >= 1) {                   /* Oops, actually got a byte? */
  5615. #ifdef OS2
  5616.             /* In OS/2 we read directly into ttibuf[] */
  5617.             hexdump("nettchk got real data",&ttibuf[ttibp+ttibn],x);
  5618.             ttibn += x;
  5619. #else /* OS2 */
  5620. #ifdef CK_SSL
  5621.         if ( ssl_active_flag || tls_active_flag ) {
  5622.         hexdump("nettchk got real data",&ttibuf[ttibp+ttibn],x);
  5623.         ttibn += x;
  5624.         } else 
  5625. #endif /* CK_SSL */
  5626.         {
  5627.         debug(F101,"nettchk socket_read char","",c);
  5628.         debug(F101,"nettchk ttibp","",ttibp);
  5629.         debug(F101,"nettchk ttibn","",ttibn);
  5630. /*
  5631.   In the case of Overlapped I/O the character would have come from
  5632.   the beginning of the buffer, so put it back.
  5633. */
  5634.         if (ttibp > 0) {
  5635.             ttibp--;
  5636.             ttibuf[ttibp] = c;
  5637.             ttibn++;
  5638.         } else {
  5639.             ttibuf[ttibp+ttibn] = c;
  5640.             ttibn++;
  5641.         }
  5642.         }
  5643. #endif /* OS2 */
  5644.         }
  5645. #else /* NOCOUNT */
  5646.         if (ttnet == NET_TCPB) {
  5647.             char dummy;
  5648.             x = read(ttyfd,&dummy,0);   /* Try to read nothing */
  5649.             if (x < 0) {                /* "Connection reset by peer" */
  5650.                 perror("TCP/IP");       /* or somesuch... */
  5651.                 ttclos(0);              /* Close our end too. */
  5652.                 rc = -1;
  5653.                 goto nettchk_return;
  5654.             }
  5655.         }
  5656. #endif /* NOCOUNT */
  5657.     }
  5658. #ifdef CK_KERBEROS
  5659. #ifdef KRB4
  5660. #ifdef RLOGCODE
  5661.     if (ttnproto == NP_EK4LOGIN)
  5662.       count += krb4_des_avail(ttyfd);
  5663. #endif /* RLOGCODE */
  5664. #endif /* KRB4 */
  5665. #ifdef KRB5
  5666. #ifdef RLOGCODE
  5667.     if (ttnproto == NP_EK5LOGIN)
  5668.       count += krb5_des_avail(ttyfd);
  5669. #endif /* RLOGCODE */
  5670. #ifdef KRB5_U2U
  5671.     if (ttnproto == NP_K5U2U)
  5672.       count += krb5_u2u_avail(ttyfd);
  5673. #endif /* KRB5_U2U */
  5674. #endif /* KRB5 */
  5675. #endif /* CK_KERBEROS */
  5676.  
  5677.     debug(F101,"nettchk returns","",count+ttibn);
  5678.     rc = count + ttibn;
  5679.  
  5680.   nettchk_return:
  5681. #ifdef OS2
  5682.     ReleaseTCPIPMutex();
  5683. #endif /* OS2 */
  5684.     return(rc);
  5685.  
  5686. #else /* Not TCPIPLIB */
  5687. /*
  5688.   UNIX just uses ttchk(), in which the ioctl() calls on the file descriptor
  5689.   seem to work OK.
  5690. */
  5691.     return(ttchk());
  5692. #endif /* TCPIPLIB */
  5693. /*
  5694.   But what about X.25?
  5695. */
  5696. }
  5697.  
  5698. #ifndef OS2
  5699. VOID
  5700. nettout(i) int i; {                     /* Catch the alarm interrupts */
  5701.     debug(F100,"nettout caught timeout","",0);
  5702.     ttimoff();
  5703.     cklongjmp(njbuf, -1);
  5704. }
  5705. #endif /* !OS2 */
  5706.  
  5707. #ifdef TCPIPLIB
  5708.  
  5709. VOID
  5710. #ifdef CK_ANSIC
  5711. donetinc(void * threadinfo)
  5712. #else /* CK_ANSIC */
  5713. donetinc(threadinfo) VOID * threadinfo;
  5714. #endif /* CK_ANSIC */
  5715. /* donetinc */ {
  5716. #ifdef NTSIG
  5717.     extern int TlsIndex;
  5718.     setint();
  5719.     if (threadinfo) {                   /* Thread local storage... */
  5720.         TlsSetValue(TlsIndex,threadinfo);
  5721.     }
  5722. #endif /* NTSIG */
  5723. #ifdef CK_LOGIN
  5724. #ifdef NT
  5725. #ifdef IKSD
  5726.     if (inserver)
  5727.       setntcreds();
  5728. #endif /* IKSD */
  5729. #endif /* NT */
  5730. #endif /* CK_LOGIN */
  5731.     while (1) {
  5732.         if (ttbufr() < 0)               /* Keep trying to refill it. */
  5733.           break;                        /* Till we get an error. */
  5734.         if (ttibn > 0)                  /* Or we get a character. */
  5735.           break;
  5736.     }
  5737. }
  5738. #endif /* TCPIPLIB */
  5739.  
  5740. VOID
  5741. #ifdef CK_ANSIC
  5742. failnetinc(void * threadinfo)
  5743. #else /* CK_ANSIC */
  5744. failnetinc(threadinfo) VOID * threadinfo;
  5745. #endif /* CK_ANSIC */
  5746. /* failnetinc */ {
  5747.     ; /* Nothing to do on an error */
  5748. }
  5749.  
  5750. /* N E T X I N -- Input block of characters from network */
  5751.  
  5752. int
  5753. netxin(n,buf) int n; CHAR * buf; {
  5754.     int len, i, j;
  5755. #ifdef TCPIPLIB
  5756.     int rc;
  5757. #endif /* TCPIPLIB */
  5758.  
  5759.     if (ttyfd == -1) {
  5760.         debug(F100,"netxin socket is closed","",0);
  5761.         return(-2);
  5762.     }
  5763. #ifdef CK_KERBEROS
  5764. #ifdef KRB4
  5765. #ifdef RLOGCODE
  5766.     if (ttnproto == NP_EK4LOGIN) {
  5767.         if ((len = krb4_des_read(ttyfd,buf,n)) < 0)
  5768.           return(-1);
  5769.         else
  5770.           return(len);
  5771.     }
  5772. #endif /* RLOGCODE */
  5773. #endif /* KRB4 */
  5774. #ifdef KRB5
  5775. #ifdef RLOGCODE
  5776.     if (ttnproto == NP_EK5LOGIN) {
  5777.         if ((len = krb5_des_read(ttyfd,buf,n,0)) < 0)
  5778.           return(-1);
  5779.         else
  5780.           return(len);
  5781.     }
  5782. #endif /* RLOGCODE */
  5783. #ifdef KRB5_U2U
  5784.     if (ttnproto == NP_K5U2U) {
  5785.         if ((len = krb5_u2u_read(ttyfd,buf,n)) < 0)
  5786.           return(-1);
  5787.         else
  5788.           return(len);
  5789.     }
  5790. #endif /* KRB5_U2U */
  5791. #endif /* KRB5 */
  5792. #endif /* CK_KERBEROS */
  5793.  
  5794. #ifdef TCPIPLIB
  5795. #ifdef OS2
  5796.     RequestTCPIPMutex(SEM_INDEFINITE_WAIT);
  5797. #endif /* OS2 */
  5798.     if (ttibn == 0)
  5799.       if ((rc = ttbufr()) <= 0) {
  5800. #ifdef OS2
  5801.         ReleaseTCPIPMutex();
  5802. #endif /* OS2 */
  5803.         return(rc);
  5804.       }
  5805.  
  5806.     if (ttibn <= n) {
  5807.         len = ttibn;
  5808.         memcpy(buf,&ttibuf[ttibp],len);         /* safe */
  5809.         ttibp += len;
  5810.         ttibn = 0;
  5811.     } else {
  5812.         memcpy(buf,&ttibuf[ttibp],n);           /* safe */
  5813.         ttibp += n;
  5814.         ttibn -= n;
  5815.         len = n;
  5816.     }
  5817. #ifdef OS2
  5818.     ReleaseTCPIPMutex();
  5819. #endif /* OS2 */
  5820. #else /* TCPIPLIB */
  5821.     for (i = 0; i < n; i++) {
  5822.         if ((j = netinc(0)) < 0) {
  5823.             if (j < -1)
  5824.               return(j);
  5825.             else
  5826.               break;
  5827.         }
  5828.         buf[i] = j;
  5829.     }
  5830.     len = i;
  5831. #endif /* TCPIPLIB */
  5832.  
  5833. #ifdef COMMENT
  5834. #ifdef CK_ENCRYPTION
  5835.     /* This would be great if it worked.  But what if the buffer we read  */
  5836.     /* contains a telnet negotiation that changes the state of the        */
  5837.     /* encryption.  If so, we would be either decrypting unencrypted text */
  5838.     /* or not decrypting encrypted text.  So we must move this call to    */
  5839.     /* all functions that call ttxin().  In OS2 that means os2_netxin()   */
  5840.     /* where the Telnet Negotiations are handled.                         */
  5841.     if (u_encrypt)
  5842.       ck_tn_decrypt(buf,len);
  5843. #endif /* CK_ENCRYPTION */
  5844. #endif /* COMMENT */
  5845.  
  5846.     return(len);
  5847. }
  5848.  
  5849. /*  N E T I N C --  Input character from network */
  5850.  
  5851. #ifdef NETLEBUF
  5852. #define LEBUF
  5853. #endif /* NETLEBUF */
  5854. #ifdef TTLEBUF
  5855. #define LEBUF
  5856. #endif /* TTLEBUF */
  5857. #ifndef LEBUF
  5858. #ifdef OS2
  5859. #define LEBUF
  5860. #endif /* OS2 */
  5861. #endif /* LEBUF */
  5862.  
  5863. int
  5864. netinc(timo) int timo; {
  5865. #ifdef TCPIPLIB
  5866.     int x; unsigned char c;             /* The locals. */
  5867.  
  5868. #ifdef NETLEBUF
  5869.     if (ttpush >= 0) {
  5870.         debug(F111,"netinc","ttpush",ttpush);
  5871.         c = ttpush;
  5872.         ttpush = -1;
  5873.         return(c);
  5874.     }
  5875.     if (le_data) {
  5876.         if (le_getchar((CHAR *)&c) > 0) {
  5877.             debug(F111,"netinc le_getchar","c",c);
  5878.             return(c);
  5879.         }
  5880.     }
  5881. #endif /* NETLEBUF */
  5882.  
  5883.     if (ttyfd == -1) {
  5884.         debug(F100,"netinc socket is closed","",0);
  5885.         return(-2);
  5886.     }
  5887.  
  5888. #ifdef CK_KERBEROS
  5889. #ifdef KRB4
  5890. #ifdef RLOGCODE
  5891.     if (ttnproto == NP_EK4LOGIN) {
  5892.         if ((x = krb4_des_read(ttyfd,&c,1)) == 0)
  5893.           return(-1);
  5894.         else if (x < 0)
  5895.           return(-2);
  5896.         else
  5897.           return(c);
  5898.     }
  5899. #endif /* RLOGCODE */
  5900. #endif /* KRB4 */
  5901. #ifdef KRB5
  5902. #ifdef RLOGCODE
  5903.     if (ttnproto == NP_EK5LOGIN) {
  5904.         if ((x = krb5_des_read(ttyfd,&c,1,0)) == 0)
  5905.           return(-1);
  5906.         else if (x < 0)
  5907.           return(-2);
  5908.         else
  5909.           return(c);
  5910.     }
  5911. #endif /* RLOGCODE */
  5912. #ifdef KRB5_U2U
  5913.     if (ttnproto == NP_K5U2U) {
  5914.         if ((x = krb5_u2u_read(ttyfd,&c,1)) == 0)
  5915.           return(-1);
  5916.         else if (x < 0)
  5917.           return(-2);
  5918.         else
  5919.           return(c);
  5920.     }
  5921. #endif /* KRB5_U2U */
  5922. #endif /* KRB5 */
  5923. #endif /* CK_KERBEROS */
  5924.  
  5925. #ifdef OS2
  5926.     RequestTCPIPMutex(SEM_INDEFINITE_WAIT);
  5927. #endif /* OS2 */
  5928.     if (ttibn > 0) {                    /* Something in internal buffer? */
  5929. #ifdef COMMENT
  5930.         debug(F100,"netinc char in buf","",0); /* Yes. */
  5931. #endif /* COMMENT */
  5932.         x = 0;                          /* Success. */
  5933.     } else {                            /* Else must read from network. */
  5934.         x = -1;                         /* Assume failure. */
  5935. #ifdef DEBUG
  5936.         debug(F101,"netinc goes to net, timo","",timo);
  5937. #endif /* DEBUG */
  5938. #ifdef CK_SSL
  5939.         /*
  5940.          * In the case of OpenSSL, it is possible that there is still
  5941.          * data waiting in the SSL session buffers that has not yet
  5942.          * been read by Kermit.  If this is the case we must process
  5943.          * it without calling select() because select() will not return
  5944.          * with an indication that there is data to be read from the
  5945.          * socket.  If there is no data pending in the SSL session
  5946.          * buffers then fall through to the select() code and wait for
  5947.          * some data to arrive.
  5948.          */
  5949.         if (ssl_active_flag) {
  5950.             x = SSL_pending(ssl_con);
  5951.             if (x < 0) {
  5952.                 debug(F111,"netinc","SSL_pending error",x);
  5953.                 netclos();
  5954. #ifdef OS2
  5955.                 ReleaseTCPIPMutex();
  5956. #endif /* OS2 */
  5957.                 return(-1);
  5958.             } else if ( x > 0 ) {
  5959.                 if ( ttbufr() >= 0 ) {
  5960.                     x = netinc(timo);
  5961. #ifdef OS2
  5962.                     ReleaseTCPIPMutex();
  5963. #endif /* OS2 */
  5964.                     return(x);
  5965.                 }
  5966.             }
  5967.             x = -1;
  5968.         } else if (tls_active_flag) {
  5969.             x = SSL_pending(tls_con);
  5970.             if (x < 0) {
  5971.                 debug(F111,"netinc","TLS_pending error",x);
  5972.                 netclos();
  5973. #ifdef OS2
  5974.                 ReleaseTCPIPMutex();
  5975. #endif /* OS2 */
  5976.                 return(-1);
  5977.             } else if ( x > 0 ) {
  5978.                 if ( ttbufr() >= 0 ) {
  5979.                     x = netinc(timo);
  5980. #ifdef OS2
  5981.                     ReleaseTCPIPMutex();
  5982. #endif /* OS2 */
  5983.                     return(x);
  5984.                 }
  5985.             }
  5986.             x = -1;
  5987.         }
  5988. #endif /* CK_SSL */
  5989. #ifndef LEBUF
  5990.         if (timo == 0) {                /* Untimed case. */
  5991.             while (1) {                 /* Wait forever if necessary. */
  5992.                 if (ttbufr() < 0)       /* Refill buffer. */
  5993.                   break;                /* Error, fail. */
  5994.                 if (ttibn > 0) {        /* Success. */
  5995.                     x = 0;
  5996.                     break;
  5997.                 }
  5998.             }
  5999.         } else                          /* Timed case... */
  6000. #endif /* LEBUF */
  6001.           {
  6002. #ifdef NT_TCP_OVERLAPPED
  6003.             /* This code is for use on NT when we are using */
  6004.             /* Overlapped I/O to handle reads.  In the case */
  6005.             /* of outstanding reads select() doesn't work   */
  6006.  
  6007.             if (WaitForOverlappedReadData(timo)) {
  6008.                 while (1) {
  6009.                     if (ttbufr() < 0)   /* Keep trying to refill it. */
  6010.                         break;          /* Till we get an error. */
  6011.                     if (ttibn > 0) {    /* Or we get a character. */
  6012.                         x = 0;
  6013.                         break;
  6014.                     }
  6015.                 }
  6016.             }
  6017. #else /* NT_TCP_OVERLAPPED */
  6018. #ifdef BSDSELECT
  6019.             fd_set rfds;
  6020.             struct timeval tv;
  6021.             int timeout = timo < 0 ? -timo : 1000 * timo;
  6022.             debug(F101,"netinc BSDSELECT","",timo);
  6023.  
  6024.             for ( ; timeout >= 0; timeout -= (timo ? 100 : 0)) {
  6025.                 int rc;
  6026.                 debug(F111,"netinc","timeout",timeout);
  6027.                 /* Don't move select() initialization out of the loop. */
  6028.                 FD_ZERO(&rfds);
  6029.                 FD_SET(ttyfd, &rfds);
  6030.                 tv.tv_sec  = tv.tv_usec = 0L;
  6031.                 if (timo)
  6032.                   tv.tv_usec = (long) 100000L;
  6033.                 else
  6034.                   tv.tv_sec = 30;
  6035. #ifdef NT
  6036.                 WSASafeToCancel = 1;
  6037. #endif /* NT */
  6038.                 rc = select(FD_SETSIZE,
  6039. #ifndef __DECC
  6040.                             (fd_set *)
  6041. #endif /* __DECC */
  6042.                             &rfds, NULL, NULL, &tv);
  6043.                 if (rc < 0) {
  6044.                     int s_errno = socket_errno;
  6045.                     debug(F111,"netinc","select",rc);
  6046.                     debug(F111,"netinc","socket_errno",s_errno);
  6047.                     if (s_errno) {
  6048. #ifdef OS2
  6049.                         ReleaseTCPIPMutex();
  6050. #endif /* OS2 */
  6051.                         return(-1);
  6052.                     }
  6053.                 }
  6054.                 debug(F111,"netinc","select",rc);
  6055. #ifdef NT
  6056.                 WSASafeToCancel = 0;
  6057. #endif /* NT */
  6058.                 if (!FD_ISSET(ttyfd, &rfds)) {
  6059. #ifdef LEBUF
  6060.                     if (le_inbuf() > 0) {
  6061.                         timeout = -1;
  6062.                         break;
  6063.                     }
  6064. #endif /* LEBUF */
  6065.                     /* If waiting forever we have no way of knowing if the */
  6066.                     /* socket closed so try writing a 0-length TCP packet  */
  6067.                     /* which should force an error if the socket is closed */
  6068.                     if (!timo) {
  6069.                         if ((rc = socket_write(ttyfd,"",0)) < 0) {
  6070.                             int s_errno = socket_errno;
  6071.                             debug(F101,"netinc socket_write error","",s_errno);
  6072. #ifdef OS2
  6073.                             if (os2socketerror(s_errno) < 0) {
  6074.                               ReleaseTCPIPMutex();
  6075.                               return(-2);
  6076.                             }
  6077.                             ReleaseTCPIPMutex();
  6078. #endif /* OS2 */
  6079.                             return(-1); /* Call it an i/o error */
  6080.                         }
  6081.                     }
  6082.                     continue;
  6083.                 }
  6084.                 while (1) {
  6085.                     if (ttbufr() < 0) { /* Keep trying to refill it. */
  6086.                         timeout = -1;
  6087.                         break;          /* Till we get an error. */
  6088.                     }
  6089.                     if (ttibn > 0) {    /* Or we get a character. */
  6090.                         x = 0;
  6091.                         timeout = -1;
  6092.                         break;
  6093.                     }
  6094.                 }
  6095.             }
  6096. #ifdef NT
  6097.             WSASafeToCancel = 0;
  6098. #endif /* NT */
  6099. #else /* !BSDSELECT */
  6100. #ifdef IBMSELECT
  6101. /*
  6102.   Was used by OS/2, currently not used, but might come in handy some day...
  6103.   ... and it came in handy!  For our TCP/IP layer, it avoids all the fd_set
  6104.   and timeval stuff since this is the only place where it is used.
  6105. */
  6106.             int socket = ttyfd;
  6107.             int timeout = timo < 0 ? -timo : 1000 * timo;
  6108.  
  6109.             debug(F101,"netinc IBMSELECT","",timo);
  6110.             for ( ; timeout >= 0; timeout -= (timo ? 100 : 0)) {
  6111.                 if (select(&socket, 1, 0, 0, 100L) == 1) {
  6112.                     while (1) {
  6113.                         if (ttbufr() < 0) { /* Keep trying to refill it. */
  6114.                             timeout = -1;
  6115.                             break;      /* Till we get an error. */
  6116.                         }
  6117.                         if (ttibn > 0) { /* Or we get a character. */
  6118.                             x = 0;
  6119.                             timeout = -1;
  6120.                             break;
  6121.                         }
  6122.                     }
  6123.                 }
  6124. #ifdef LEBUF
  6125.                 else if (le_inbuf() > 0)  {
  6126.                     timeout = -1;
  6127.                     break;
  6128.                 }
  6129. #endif /* LEBUF */
  6130.             }
  6131. #else /* !IBMSELECT */
  6132. #ifdef WINSOCK
  6133.        /* Actually, under WinSock we have a better mechanism than select() */
  6134.        /* for setting timeouts (SO_RCVTIMEO, SO_SNDTIMEO) */
  6135.             SOCKET socket = ttyfd;
  6136.             debug(F101,"netinc NTSELECT","",timo);
  6137.             if (setsockopt(socket, SOL_SOCKET, SO_RCVTIMEO, (char *)&timo,
  6138.                             sizeof(timo))  == NO_ERROR)
  6139.               while (1) {
  6140.                   if (ttbufr() < 0)     /* Keep trying to refill it. */
  6141.                     break;              /* Till we get an error. */
  6142.                   if (ttibn > 0) {      /* Or we get a character. */
  6143.                       x = 0;
  6144.                       break;
  6145.                   }
  6146.               }
  6147. #else /* WINSOCK */
  6148. /*
  6149.   If we can't use select(), then we use the regular alarm()/signal()
  6150.   timeout mechanism.
  6151. */
  6152.             debug(F101,"netinc alarm","",timo);
  6153.             x = alrm_execute(ckjaddr(njbuf),timo,nettout,donetinc,failnetinc);
  6154.             ttimoff();                  /* Timer off. */
  6155. #endif /* WINSOCK */
  6156. #endif /* IBMSELECT */
  6157. #endif /* BSDSELECT */
  6158. #endif /* NT_TCP_OVERLAPPED */
  6159.         }
  6160.     }
  6161.  
  6162. #ifdef LEBUF
  6163.     if (le_inbuf() > 0) {               /* If data was inserted into the */
  6164.         if (le_getchar((CHAR *)&c) > 0) {/* Local Echo buffer while the   */
  6165. #ifdef OS2                               /* was taking place do not mix   */
  6166.           ReleaseTCPIPMutex();           /* the le data with the net data */
  6167. #endif /* OS2 */
  6168.           return(c);
  6169.         }
  6170.     }
  6171. #endif /* LEBUF */
  6172.     if (x < 0) {                        /* Return -1 if we failed. */
  6173.         debug(F100,"netinc timed out","",0);
  6174. #ifdef OS2
  6175.         ReleaseTCPIPMutex();
  6176. #endif /* OS2 */
  6177.         return(-1);
  6178.     } else {                            /* Otherwise */
  6179.         c = ttibuf[ttibp];              /* Return the first char in ttibuf[] */
  6180.         if (deblog) {
  6181. #ifndef COMMENT
  6182.             debug(F101,"netinc returning","",c);
  6183. #endif /* COMMENT */
  6184.             if (c == 0) {
  6185.                 debug(F101,"netinc 0 ttibn","",ttibn);
  6186.                 debug(F101,"netinc 0 ttibp","",ttibp);
  6187. #ifdef BETADEBUG
  6188.                 {
  6189. #ifdef OS2
  6190.                     extern int tt_type_mode;
  6191.                     if ( !ISVTNT(tt_type_mode) )
  6192. #endif /* OS2 */
  6193.                     hexdump("netinc &ttbuf[ttibp]",&ttibuf[ttibp],ttibn);
  6194.                 }
  6195. #endif /* BETADEBUG */
  6196.             }
  6197.         }
  6198.         ttibp++;
  6199.         ttibn--;
  6200. #ifdef OS2
  6201.         ReleaseTCPIPMutex();
  6202. #endif /* OS2 */
  6203. #ifdef CK_ENCRYPTION
  6204.         if (TELOPT_U(TELOPT_ENCRYPTION))
  6205.           ck_tn_decrypt(&c,1);
  6206. #endif /* CK_ENCRYPTION */
  6207.         return(c);
  6208.     }
  6209. #else /* Not using TCPIPLIB */
  6210.     return(-1);
  6211. #endif /* TCPIPLIB */
  6212. }
  6213.  
  6214. /*  N E T T O L  --  Output a string of bytes to the network  */
  6215. /*
  6216.   Call with s = pointer to string, n = length.
  6217.   Returns number of bytes actually written on success, or
  6218.   -1 on i/o error, -2 if called improperly.
  6219. */
  6220.  
  6221. int
  6222. nettol(s,n) CHAR *s; int n; {
  6223. #ifdef TCPIPLIB
  6224.     int count = 0;
  6225.     int len = n;
  6226.     int try = 0;
  6227.  
  6228.     if (ttyfd == -1) {
  6229.         debug(F100,"nettol socket is closed","",0);
  6230.         return -1;
  6231.     }
  6232.     debug(F101,"nettol TCPIPLIB ttnet","",ttnet);
  6233. #ifdef COMMENT
  6234.     hexdump("nettol",s,n);
  6235. #endif /* COMMENT */
  6236.  
  6237. #ifdef CK_KERBEROS
  6238. #ifdef KRB4
  6239. #ifdef RLOGCODE
  6240.     if (ttnproto == NP_EK4LOGIN) {
  6241.         return(krb4_des_write(ttyfd,s,n));
  6242.     }
  6243. #endif /* RLOGCODE */
  6244. #endif /* KRB4 */
  6245. #ifdef KRB5
  6246. #ifdef RLOGCODE
  6247.     if (ttnproto == NP_EK5LOGIN) {
  6248.         return(krb5_des_write(ttyfd,s,n,0));
  6249.     }
  6250. #endif /* RLOGCODE */
  6251. #ifdef KRB5_U2U
  6252.     if (ttnproto == NP_K5U2U) {
  6253.         return(krb5_u2u_write(ttyfd,s,n));
  6254.     }
  6255. #endif /* KRB5_U2U */
  6256. #endif /* KRB5 */
  6257. #endif /* CK_KERBEROS */
  6258.  
  6259. #ifdef CK_ENCRYPTION
  6260.     if (TELOPT_ME(TELOPT_ENCRYPTION))
  6261.       ck_tn_encrypt(s,n);
  6262. #endif /* CK_ENCRYPTION */
  6263.  
  6264. #ifdef CK_SSL
  6265.     if (ssl_active_flag || tls_active_flag) {
  6266.         int error, r;
  6267.         /* Write using SSL */
  6268.       ssl_retry:
  6269.         if (ssl_active_flag)
  6270.           r = SSL_write(ssl_con, s, len /* >1024?1024:len */);
  6271.         else
  6272.           r = SSL_write(tls_con, s, len /* >1024?1024:len */);
  6273.         switch (SSL_get_error(ssl_active_flag?ssl_con:tls_con,r)) {
  6274.           case SSL_ERROR_NONE:
  6275.             debug(F111,"nettol","SSL_write",r);
  6276.             if ( r == len )
  6277.                 return(n);
  6278.              s += r;
  6279.              len -= r;
  6280.              goto ssl_retry;
  6281.           case SSL_ERROR_WANT_WRITE:
  6282.             debug(F100,"nettol SSL_ERROR_WANT_WRITE","",0);
  6283.             return(-1);
  6284.           case SSL_ERROR_WANT_READ:
  6285.             debug(F100,"nettol SSL_ERROR_WANT_READ","",0);
  6286.             return(-1);
  6287.           case SSL_ERROR_SYSCALL:
  6288.               if ( r == 0 ) { /* EOF */
  6289.                   netclos();
  6290.                   return(-2);
  6291.               } else {
  6292.                   int rc = -1;
  6293. #ifdef NT
  6294.                   int gle = GetLastError();
  6295.                   debug(F111,"nettol SSL_ERROR_SYSCALL",
  6296.                          "GetLastError()",gle);
  6297.                   rc = os2socketerror(gle);
  6298.                   if (rc == -1)
  6299.                       rc = -2;
  6300.                   else if ( rc == -2 )
  6301.                       rc = -1;
  6302. #endif /* NT */
  6303.                   return(rc);
  6304.               }
  6305.           case SSL_ERROR_WANT_X509_LOOKUP:
  6306.             debug(F100,"nettol SSL_ERROR_WANT_X509_LOOKUP","",0);
  6307.             netclos();
  6308.             return(-2);
  6309.           case SSL_ERROR_SSL:
  6310.             debug(F100,"nettol SSL_ERROR_SSL","",0);
  6311.               if (bio_err!=NULL) {
  6312.                   int len;
  6313.                   extern char ssl_err[];
  6314.                   BIO_printf(bio_err,"nettol() SSL_ERROR_SSL\n");
  6315.                   ERR_print_errors(bio_err);
  6316.                   len = BIO_read(bio_err,ssl_err,SSL_ERR_BFSZ);
  6317.                   ssl_err[len < SSL_ERR_BFSZ ? len : SSL_ERR_BFSZ] = '\0';
  6318.                   debug(F110,"nettol SSL_ERROR_SSL",ssl_err,0);
  6319.                   if (ssl_debug_flag)
  6320.                       printf(ssl_err);
  6321.               } else if (ssl_debug_flag) {
  6322.                   debug(F100,"nettol SSL_ERROR_SSL","",0);
  6323.                   fflush(stderr);
  6324.                   fprintf(stderr,"nettol() SSL_ERROR_SSL\n");
  6325.                   ERR_print_errors_fp(stderr);
  6326.               }
  6327. #ifdef COMMENT
  6328.               netclos();
  6329.               return(-2);
  6330. #else
  6331.               return(-1);
  6332. #endif
  6333.           case SSL_ERROR_ZERO_RETURN:
  6334.             debug(F100,"nettol SSL_ERROR_ZERO_RETURN","",0);
  6335.             netclos();
  6336.             return(-2);
  6337.           default:
  6338.             debug(F100,"nettol SSL_ERROR_?????","",0);
  6339.             netclos();
  6340.             return(-2);
  6341.         }
  6342.     }
  6343. #endif /* CK_SSL */
  6344.  
  6345.   nettol_retry:
  6346.     try++;                              /* Increase the try counter */
  6347.  
  6348.     if (ttnet == NET_TCPB) {
  6349. #ifdef BSDSELECT
  6350.         fd_set wfds;
  6351.         struct timeval tv;
  6352.  
  6353.         debug(F101,"nettol BSDSELECT","",0);
  6354.         tv.tv_usec = 0L;
  6355.         tv.tv_sec=30;
  6356. #ifdef NT
  6357.         WSASafeToCancel = 1;
  6358. #endif /* NT */
  6359. #ifdef STREAMING
  6360.       do_select:
  6361. #endif /* STREAMING */
  6362.         FD_ZERO(&wfds);
  6363.         FD_SET(ttyfd, &wfds);
  6364.         if (select(FD_SETSIZE, NULL,
  6365. #ifdef __DECC
  6366. #ifndef __DECC_VER
  6367.                     (int *)
  6368. #endif /* __DECC_VER */
  6369. #endif /* __DECC */
  6370.                    &wfds, NULL, &tv) < 0) {
  6371.             int s_errno = socket_errno;
  6372.             debug(F101,"nettol select failed","",s_errno);
  6373. #ifdef BETADEBUG
  6374.             printf("nettol select failed: %d\n", s_errno);
  6375. #endif /* BETADEBUG */
  6376. #ifdef NT
  6377.             WSASafeToCancel = 0;
  6378.             if (!win95selectbug)
  6379. #endif /* NT */
  6380.               return(-1);
  6381.         }
  6382.         if (!FD_ISSET(ttyfd, &wfds)) {
  6383. #ifdef STREAMING
  6384.             if (streaming)
  6385.               goto do_select;
  6386. #endif /* STREAMING */
  6387.             debug(F111,"nettol","!FD_ISSET",ttyfd);
  6388. #ifdef NT
  6389.             WSASafeToCancel = 0;
  6390.             if (!win95selectbug)
  6391. #endif /* NT */
  6392.               return(-1);
  6393.         }
  6394. #ifdef NT
  6395.         WSASafeToCancel = 0;
  6396. #endif /* NT */
  6397. #else /* BSDSELECT */
  6398. #ifdef IBMSELECT
  6399.         {
  6400.             int tries = 0;
  6401.             debug(F101,"nettol IBMSELECT","",0);
  6402.             while (select(&ttyfd, 0, 1, 0, 1000) != 1) {
  6403.                 int count;
  6404.                 if (tries++ >= 60) {
  6405.                     /* if after 60 seconds we can't get permission to write */
  6406.                     debug(F101,"nettol select failed","",socket_errno);
  6407.                     return(-1);
  6408.                 }
  6409.                 if ((count = nettchk()) < 0) {
  6410.                     debug(F111,"nettol","nettchk()",count);
  6411.                     return(count);
  6412.                 }
  6413.             }
  6414.         }
  6415. #endif /* IBMSELECT */
  6416. #endif /* BSDSELECT */
  6417.         if ((count = socket_write(ttyfd,s,n)) < 0) {
  6418.             int s_errno = socket_errno; /* maybe a function */
  6419.             debug(F101,"nettol socket_write error","",s_errno);
  6420. #ifdef OS2
  6421.             if (os2socketerror(s_errno) < 0)
  6422.               return(-2);
  6423. #endif /* OS2 */
  6424.             return(-1);                 /* Call it an i/o error */
  6425.         }
  6426.         if (count < n) {
  6427.             debug(F111,"nettol socket_write",s,count);
  6428.             if (try > 25) {
  6429.                 /* don't try more than 25 times */
  6430.                 debug(F100,"nettol tried more than 25 times","",0);
  6431.                 return(-1);
  6432.             }
  6433.             if (count > 0) {
  6434.                 s += count;
  6435.                 n -= count;
  6436.             }
  6437.             debug(F111,"nettol retry",s,n);
  6438.             goto nettol_retry;
  6439.         } else {
  6440.             debug(F111,"nettol socket_write",s,count);
  6441.             return(len); /* success - return total length */
  6442.         }
  6443.     } else
  6444.       return(-2);
  6445. #else
  6446.     debug(F100,"nettol TCPIPLIB not defined","",0);
  6447.     return(-2);
  6448. #endif /* TCPIPLIB */
  6449. }
  6450.  
  6451. /*  N E T T O C  --   Output character to network */
  6452. /*
  6453.   Call with character to be transmitted.
  6454.   Returns 0 if transmission was successful, or
  6455.   -1 upon i/o error, or -2 if called improperly.
  6456. */
  6457. int
  6458. #ifdef CK_ANSIC
  6459. nettoc(CHAR c)
  6460. #else
  6461. nettoc(c) CHAR c;
  6462. #endif /* CK_ANSIC */
  6463. /* nettoc */ {
  6464. #ifdef UNIX
  6465.     return(ttoc(c));
  6466. #else
  6467. #ifdef TCPIPLIB
  6468.     unsigned char cc;
  6469.     if (ttyfd == -1) {
  6470.         debug(F100,"nettoc socket is closed","",0);
  6471.         return -1;
  6472.     }
  6473.     cc = c;
  6474.     debug(F101,"nettoc cc","",cc);
  6475.  
  6476. #ifdef CK_KERBEROS
  6477. #ifdef KRB4
  6478. #ifdef RLOGCODE
  6479.     if (ttnproto == NP_EK4LOGIN) {
  6480.         return(krb4_des_write(ttyfd,&cc,1)==1?0:-1);
  6481.     }
  6482. #endif /* RLOGCODE */
  6483. #endif /* KRB4 */
  6484. #ifdef KRB5
  6485. #ifdef RLOGCODE
  6486.     if (ttnproto == NP_EK5LOGIN) {
  6487.         return(krb5_des_write(ttyfd,&cc,1,0)==1?0:-1);
  6488.     }
  6489. #endif /* RLOGCODE */
  6490. #ifdef KRB5_U2U
  6491.     if (ttnproto == NP_K5U2U) {
  6492.         return(krb5_u2u_write(ttyfd,&cc,1)==1?0:-1);
  6493.     }
  6494. #endif /* KRB5_U2U */
  6495. #endif /* KRB5 */
  6496. #endif /* CK_KERBEROS */
  6497.  
  6498. #ifdef CK_ENCRYPTION
  6499.         if ( TELOPT_ME(TELOPT_ENCRYPTION) )
  6500.             ck_tn_encrypt(&cc,1);
  6501. #endif /* CK_ENCRYPTION */
  6502. #ifdef CK_SSL
  6503.     if (ssl_active_flag || tls_active_flag) {
  6504.         int len, error;
  6505.         /* Write using SSL */
  6506.       ssl_retry:
  6507.         if (ssl_active_flag)
  6508.           len = SSL_write(ssl_con, &cc, 1);
  6509.         else
  6510.           len = SSL_write(tls_con, &cc, 1);
  6511.         switch (SSL_get_error(ssl_active_flag?ssl_con:tls_con,len)) {
  6512.           case SSL_ERROR_NONE:
  6513.             debug(F111,"nettoc","SSL_write",len);
  6514.             return(len == 1 ? 0 : -1);
  6515.           case SSL_ERROR_WANT_WRITE:
  6516.         case SSL_ERROR_WANT_READ:
  6517.             return(-1);
  6518.           case SSL_ERROR_SYSCALL:
  6519.               if ( len == 0 ) { /* EOF */
  6520.                   netclos();
  6521.                   return(-2);
  6522.               } else {
  6523.                   int rc = -1;
  6524. #ifdef NT
  6525.                   int gle = GetLastError();
  6526.                   debug(F111,"nettoc SSL_ERROR_SYSCALL",
  6527.                          "GetLastError()",gle);
  6528.                   rc = os2socketerror(gle);
  6529.                   if (rc == -1)
  6530.                       rc = -2;
  6531.                   else if ( rc == -2 )
  6532.                       rc = -1;
  6533. #endif /* NT */
  6534.                   return(rc);
  6535.               }
  6536.         case SSL_ERROR_SSL:
  6537.               if (bio_err!=NULL) {
  6538.                   int len;
  6539.                   extern char ssl_err[];
  6540.                   BIO_printf(bio_err,"nettoc() SSL_ERROR_SSL\n");
  6541.                   ERR_print_errors(bio_err);
  6542.                   len = BIO_read(bio_err,ssl_err,SSL_ERR_BFSZ);
  6543.                   ssl_err[len < SSL_ERR_BFSZ ? len : SSL_ERR_BFSZ] = '\0';
  6544.                   debug(F110,"nettoc SSL_ERROR_SSL",ssl_err,0);
  6545.                   if (ssl_debug_flag)
  6546.                       printf(ssl_err);
  6547.               } else if (ssl_debug_flag) {
  6548.                   debug(F100,"nettoc SSL_ERROR_SSL","",0);
  6549.                   fflush(stderr);
  6550.                   fprintf(stderr,"nettoc() SSL_ERROR_SSL\n");
  6551.                   ERR_print_errors_fp(stderr);
  6552.               }
  6553.               return(-1);
  6554.               break;
  6555.           case SSL_ERROR_WANT_X509_LOOKUP:
  6556.           case SSL_ERROR_ZERO_RETURN:
  6557.           default:
  6558.             netclos();
  6559.             return(-2);
  6560.         }
  6561.     }
  6562. #endif /* CK_SSL */
  6563.     if (ttnet == NET_TCPB) {
  6564. #ifdef BSDSELECT
  6565.         fd_set wfds;
  6566.         struct timeval tv;
  6567.  
  6568.         debug(F101,"nettoc BSDSELECT","",0);
  6569.         tv.tv_usec = 0L;
  6570.         tv.tv_sec = 30;
  6571.  
  6572. #ifdef STREAMING
  6573.       do_select:
  6574. #endif /* STREAMING */
  6575.  
  6576.         FD_ZERO(&wfds);
  6577.         FD_SET(ttyfd, &wfds);
  6578.         if (select(FD_SETSIZE, NULL,
  6579. #ifdef __DECC
  6580. #ifndef __DECC_VER
  6581.                    (int *)
  6582. #endif /* __DECC_VER */
  6583. #endif /* __DECC */
  6584.                    &wfds, NULL, &tv) < 0) {
  6585.             int s_errno = socket_errno;
  6586.             debug(F101,"nettoc select failed","",s_errno);
  6587. #ifdef BETADEBUG
  6588.             printf("nettoc select failed: %d\n", s_errno);
  6589. #endif /* BETADEBUG */
  6590. #ifdef NT
  6591.             WSASafeToCancel = 0;
  6592.             if (!win95selectbug)
  6593. #endif /* NT */
  6594.               return(-1);
  6595.         }
  6596.         if (!FD_ISSET(ttyfd, &wfds)) {
  6597. #ifdef STREAMING
  6598.             if (streaming)
  6599.               goto do_select;
  6600. #endif /* STREAMING */
  6601.             debug(F111,"nettoc","!FD_ISSET",ttyfd);
  6602. #ifdef NT
  6603.             WSASafeToCancel = 0;
  6604.             if (!win95selectbug)
  6605. #endif /* NT */
  6606.               return(-1);
  6607.         }
  6608. #ifdef NT
  6609.         WSASafeToCancel = 0;
  6610. #endif /* NT */
  6611. #else /* BSDSELECT */
  6612. #ifdef IBMSELECT
  6613.         {
  6614.             int tries = 0;
  6615.             while (select(&ttyfd, 0, 1, 0, 1000) != 1) {
  6616.                 int count;
  6617.                 if (tries++ >= 60) {
  6618.                     /* if after 60 seconds we can't get permission to write */
  6619.                     debug(F101,"nettoc select failed","",socket_errno);
  6620.                     return(-1);
  6621.                 }
  6622.                 if ((count = nettchk()) < 0) {
  6623.                     debug(F111,"nettoc","nettchk()",count);
  6624.                     return(count);
  6625.                 }
  6626.             }
  6627.         }
  6628. #endif /* IBMSELECT */
  6629. #endif /* BSDSELECT */
  6630.         if (socket_write(ttyfd,&cc,1) < 1) {
  6631.             int s_errno = socket_errno;         /* maybe a function */
  6632.             debug(F101,"nettoc socket_write error","",s_errno);
  6633. #ifdef OS2
  6634.             if (os2socketerror(s_errno) < 0)
  6635.               return(-2);
  6636. #endif /* OS2 */
  6637.             return(-1);
  6638.         }
  6639.         debug(F101,"nettoc socket_write","", cc);
  6640.         return(0);
  6641.     } else return(-2);
  6642. #else
  6643.     return(-2);
  6644. #endif /* TCPIPLIB */
  6645. #endif /* UNIX */
  6646. }
  6647.  
  6648. /*  N E T F L U I  --  Flush network input buffer  */
  6649.  
  6650. #ifdef TNCODE
  6651. static int
  6652. #ifdef CK_ANSIC
  6653. netgetc(int timo)                       /* Input function to point to... */
  6654. #else  /* CK_ANSIC */
  6655. netgetc(timo) int timo;
  6656. #endif /* CK_ANSIC */
  6657. {                                       /* ...in the tn_doop() call */
  6658. #ifdef TCPIPLIB
  6659.     return netinc(timo);
  6660. #else /* TCPIPLIB */
  6661.     return ttinc(timo);
  6662. #endif /* TCPIPLIB */
  6663. }
  6664. #endif /* TNCODE */
  6665.  
  6666. int
  6667. netflui() {
  6668.     int n;
  6669.     int ch;
  6670. #ifdef NETLEBUF
  6671.     ttpush = -1;                        /* Clear the peek-ahead char */
  6672.     while (le_data && (le_inbuf() > 0)) {
  6673.         CHAR ch = '\0';
  6674.         if (le_getchar(&ch) > 0) {
  6675.             debug(F101,"ttflui le_inbuf ch","",ch);
  6676.         }
  6677.     }
  6678. #endif /* NETLEBUF */
  6679.  
  6680. #ifdef TCPIPLIB
  6681. #ifdef OS2
  6682.     RequestTCPIPMutex(SEM_INDEFINITE_WAIT);
  6683. #endif /* OS2 */
  6684. #ifdef TNCODE
  6685.     if (ttnproto == NP_TELNET) {
  6686.         /* Netflui must process Telnet negotiations or get out of sync */
  6687.         if ((n = nettchk()) <= 0)
  6688.           goto exit_flui;
  6689.         while (n-- > 0) {
  6690.             ch = netinc(1);
  6691.             if (ch == IAC) {
  6692.                 extern int duplex;  /* this really shouldn't be here but ... */
  6693.                 int tx = tn_doop((CHAR)(ch & 0xff),duplex,netgetc);
  6694.                 if (tx == 1) duplex = 1;
  6695.                 else if (tx == 2) duplex = 0;
  6696.                 n = nettchk();
  6697.             }
  6698.         }
  6699.     } else
  6700. #endif /* TNCODE */
  6701.     {
  6702.         ttibuf[ttibp+ttibn] = '\0';
  6703.         debug(F111,"netflui 1",ttibuf,ttibn);
  6704. #ifdef CK_ENCRYPTION
  6705.         if (TELOPT_U(TELOPT_ENCRYPTION)) {
  6706.             ck_tn_decrypt(&ttibuf[ttibp],ttibn);
  6707.         }
  6708. #endif /* CK_ENCRYPTION */
  6709.         ttibn = ttibp = 0;              /* Flush internal buffer *FIRST* */
  6710.         if (ttyfd < 1)
  6711.           goto exit_flui;
  6712.         if ((n = nettchk()) > 0) {      /* Now see what's waiting on the net */
  6713.             if (n > TTIBUFL) n = TTIBUFL;       /* and sponge it up */
  6714.             debug(F101,"netflui 2","",n);       /* ... */
  6715.             n = socket_read(ttyfd,ttibuf,n); /* into our buffer */
  6716.             if (n >= 0) ttibuf[n] = '\0';
  6717.             debug(F111,"netflui 3",ttibuf,n);
  6718. #ifdef CK_ENCRYPTION
  6719.             if (TELOPT_U(TELOPT_ENCRYPTION)) {
  6720.                 ck_tn_decrypt(&ttibuf[ttibp],n);
  6721.             }
  6722. #endif /* CK_ENCRYPTION */
  6723.             ttibuf[0] = '\0';
  6724.         }
  6725.     }
  6726. #else  /* !TCPIPLIB */
  6727.     if (ttyfd < 1)
  6728.       goto exit_flui;
  6729. #ifdef TNCODE
  6730.     if (ttnproto == NP_TELNET) {
  6731.         if ((n = ttchk()) <= 0)
  6732.           goto exit_flui;
  6733.         while (n-- >= 0) {
  6734.             /* Netflui must process Telnet negotiations or get out of sync */
  6735.             ch = ttinc(1);
  6736.             if (ch == IAC) {
  6737.                 extern int duplex;  /* this really shouldn't be here but ... */
  6738.                 int tx = tn_doop((CHAR)(ch & 0xff),duplex,netgetc);
  6739.                 if (tx == 1) duplex = 1;
  6740.                 else if (tx == 2) duplex = 0;
  6741.                 n = ttchk();
  6742.             }
  6743.         };
  6744.     } else
  6745. #endif /* TNCODE */
  6746.     if ((n = ttchk()) > 0) {
  6747.         debug(F101,"netflui non-TCPIPLIB","",n);
  6748.         while ((n--) && ttinc(1) > -1)  /* Don't worry, ttinc() is buffered */
  6749.           ;                             /* and it handles the decryption... */
  6750.     }
  6751. #endif /* TCPIPLIB */
  6752.   exit_flui:
  6753. #ifdef OS2
  6754.     ReleaseTCPIPMutex();
  6755. #endif /* OS2 */
  6756.     return(0);
  6757. }
  6758.  
  6759. #ifdef CK_KERBEROS
  6760. /* The following two functions are required for encrypted rlogin */
  6761. /* They are called with nettoc() or nettol() are transmitting    */
  6762. /* encrypted data.  They call a function to encrypt the data     */
  6763. /* and that function needs to be able to write to/read from the  */
  6764. /* network in an unimpeded manner.  Hence, these two simple fns. */
  6765. int
  6766. net_write(fd, buf, len)
  6767.     int fd;
  6768.     register const char *buf;
  6769.     int len;
  6770. {
  6771.     int cc;
  6772.     register int wrlen = len;
  6773.     do {
  6774. #ifdef TCPIPLIB
  6775.         cc = socket_write(fd, buf, wrlen);
  6776. #else
  6777.         cc = write(fd,buf,wrlen);
  6778. #endif /* TCPIPLIB */
  6779.         if (cc < 0) {
  6780.             int s_errno = socket_errno;
  6781.             debug(F101,"net_write error","",s_errno);
  6782. #ifdef OS2
  6783.             if (os2socketerror(s_errno) < 0)
  6784.                 return(-1);
  6785.             else
  6786.                 continue;
  6787. #else /* OS2 */
  6788.             if (errno == EINTR)
  6789.                 continue;
  6790.             return(-1);
  6791. #endif /* OS2 */
  6792.         }
  6793.         else {
  6794.             buf += cc;
  6795.             wrlen -= cc;
  6796.         }
  6797.     } while (wrlen > 0);
  6798.     return(len);
  6799. }
  6800. int
  6801. net_read(fd, buf, len)
  6802.     int fd;
  6803.     register char *buf;
  6804.     register int len;
  6805. {
  6806.     int cc, len2 = 0;
  6807.  
  6808.     do {
  6809. #ifdef TCPIPLIB
  6810.         cc = socket_read(fd, buf, len);
  6811. #else
  6812.         cc = read(fd,buf,len);
  6813. #endif
  6814.         if (cc < 0) {
  6815.             int s_errno = socket_errno;
  6816.             debug(F101,"net_read error","",s_errno);
  6817. #ifdef OS2
  6818.             if (os2socketerror(s_errno) < 0)
  6819.                 return(-1);
  6820. #endif /* OS2 */
  6821.             return(cc);          /* errno is already set */
  6822.         }
  6823.         else if (cc == 0) {
  6824.             netclos();
  6825.             return(len2);
  6826.         } else {
  6827.             buf += cc;
  6828.             len2 += cc;
  6829.             len -= cc;
  6830.         }
  6831.     } while (len > 0);
  6832.     return(len2);
  6833. }
  6834. #endif /* CK_KERBEROS */
  6835. #endif /* NONET */
  6836.  
  6837. /* getlocalipaddr() attempts to resolve an IP Address for the local machine.
  6838.  *   If the host is multi-homed it returns only one address.
  6839.  *
  6840.  * Two techniques are used.
  6841.  * (1) get the local host name and perform a DNS lookup, then take
  6842.  *     the first entry;
  6843.  * (2) open a UDP socket, use it to connect to a fictitious host (it's OK,
  6844.  *    no data is sent), then retrieve the local address from the socket.
  6845.  * Note: the second technique won't work on Microsoft systems.  See
  6846.  * Article ID: Q129065 PRB: Getsockname() Returns IP Address 0.0.0.0 for UDP
  6847.  */
  6848.  
  6849. /* Technique number one cannot work reliably if the machine is a laptop
  6850.  * and the hostname is associated with a physical adapter which is not
  6851.  * installed and a PPP connection is being used instead.  This is because
  6852.  * the hostname DNS lookup will succeed for the physical adapter even though
  6853.  * it would be impossible to use it.  In NT4 SP4, the gethostbyname()
  6854.  * when given the result of gethostname() returns not the real DNS entries
  6855.  * for that name+domain.  Instead it returns all of the static and dynamic
  6856.  * IP addresses assigned to any physical or virtual adapter defined in the
  6857.  * system regardless of whether or not it is installed.  The order of the
  6858.  * addresses is fixed according to the binding order in the NT registry.
  6859.  */
  6860.  
  6861. /*
  6862.  * It appears that calling gethostbyname(NULL) is more reliable than
  6863.  * calling gethostbyname(gethostname()) on Windows.  So on Windows we will
  6864.  * only call gethostbyname(NULL).
  6865.  */
  6866.  
  6867. int
  6868. getlocalipaddr() {
  6869. #ifndef datageneral
  6870.     struct sockaddr_in l_sa;
  6871.     struct sockaddr_in r_sa;
  6872.     GSOCKNAME_T slen = sizeof(struct sockaddr_in);
  6873.     int sock;
  6874.     int rc;
  6875.     struct in_addr laddr;
  6876.  
  6877.     /* if still not resolved, then try second strategy */
  6878.     /* This second strategy does not work on Windows */
  6879.  
  6880.     memset(&l_sa,0,slen);
  6881.     memset(&r_sa,0,slen);
  6882.  
  6883.     /* get a UDP socket */
  6884.     sock = socket(AF_INET, SOCK_DGRAM, 0);
  6885.     if (sock != -1) {
  6886.         /* connect to arbirary port and address (NOT loopback) */
  6887.         r_sa.sin_family = AF_INET;
  6888.         r_sa.sin_port = htons(IPPORT_ECHO);
  6889.  
  6890.         /* The following is an "illegal conversion" in AOS/VS */
  6891.         /* (and who knows where else) */
  6892.  
  6893. #ifdef INADDRX
  6894.         inaddrx = inet_addr("128.127.50.1");
  6895.         r_sa.sin_addr.s_addr = *(unsigned long *)&inaddrx;
  6896. #else
  6897.         r_sa.sin_addr.s_addr = inet_addr("128.127.50.1");
  6898. #endif /* INADDRX */
  6899.         rc = connect(sock, (struct sockaddr *) &r_sa, sizeof(struct sockaddr));
  6900.         if (!rc) {                      /* get local address */
  6901.             getsockname(sock,(struct sockaddr *)&l_sa,&slen);
  6902. #ifdef TCPIPLIB
  6903.             socket_close(sock);         /* We're done with the socket */
  6904. #else
  6905.             close(sock);
  6906. #endif /* TCPIPLIB */
  6907.             if (l_sa.sin_addr.s_addr != INADDR_ANY) {
  6908.                 myxipaddr = ntohl(l_sa.sin_addr.s_addr);
  6909.                 ckstrncpy(myipaddr,(char *)inet_ntoa(l_sa.sin_addr),20);
  6910.                 debug(F110,"getlocalipaddr setting buf to",myipaddr,0);
  6911.                 return(0);
  6912.             }
  6913.         }
  6914.     }
  6915.     return getlocalipaddrs(myipaddr,sizeof(myipaddr),0);
  6916. #else /* datageneral */
  6917.     return(-1);
  6918. #endif /* datageneral */
  6919. }
  6920.  
  6921. int
  6922. getlocalipaddrs(buf,bufsz,index)
  6923.     char * buf;
  6924.     int    bufsz;
  6925.     int    index;
  6926. /* getlocalipaddrs */ {
  6927. #ifndef datageneral
  6928.     char localhost[256];
  6929.     struct hostent * host=NULL;
  6930.     struct sockaddr_in l_sa;
  6931.     struct sockaddr_in r_sa;
  6932.     GSOCKNAME_T slen = sizeof(struct sockaddr_in);
  6933.     int rc;
  6934. #ifdef COMMENT
  6935.     int sock;
  6936.     char messageBuf[60];
  6937.     struct in_addr laddr;
  6938. #endif /* COMMENT */
  6939.  
  6940.     memset(&l_sa,0,slen);
  6941.     memset(&r_sa,0,slen);
  6942.  
  6943.     /* init local address (to zero) */
  6944.     l_sa.sin_addr.s_addr = INADDR_ANY;
  6945.  
  6946. #ifdef CKGHNLHOST
  6947.     rc = gethostname(localhost, 256);
  6948.     debug(F110,"getlocalipaddrs localhost",localhost,0);
  6949. #else
  6950.     /* This doesn't work on some platforms, e.g. Solaris */
  6951.     rc = 0;
  6952.     localhost[0] = '\0';
  6953. #ifdef NT
  6954.     if ( winsock_version < 20 ) {
  6955.         rc = gethostname(localhost, 256);
  6956.         debug(F110,"getlocalipaddrs localhost",localhost,0);
  6957.     }
  6958. #endif /* NT */
  6959. #endif /* CKGHNLHOST */
  6960.     if (!rc) {
  6961.         /* resolve host name for local address */
  6962.         debug(F110,"getlocalipaddrs","calling gethostbyname()",0);
  6963.         host = gethostbyname(localhost);
  6964.         /* debug(F111,"getlocalipaddrs","gethostbyname() returned",host); */
  6965.         if (host) {
  6966. #ifdef HADDRLIST
  6967.             host = ck_copyhostent(host);
  6968.             if ( index < 0 || index > 63 || !host->h_addr_list[index] ) {
  6969.                 buf[0] = '\0';
  6970.                 return(-1);
  6971.             }
  6972.             l_sa.sin_addr.s_addr =
  6973.               *((unsigned long *) (host->h_addr_list[index]));
  6974.             ckstrncpy(buf,(char *)inet_ntoa(l_sa.sin_addr),20);
  6975.             debug(F110,"getlocalipaddrs setting buf to",buf,0);
  6976.  
  6977. #ifdef COMMENT
  6978.             /* This is for reporting multiple IP Address */
  6979.             while (host->h_addr_list && host->h_addr_list[0]) {
  6980.                 l_sa.sin_addr.s_addr =
  6981.                   *((unsigned long *) (host->h_addr_list[0]));
  6982.                 ckstrncpy(messageBuf,
  6983.                         (char *)inet_ntoa(l_sa.sin_addr),60);
  6984.                 if (tcp_address) {
  6985.                     if (!strcmp(messageBuf,tcp_address))
  6986.                       ckstrncpy(myipaddr,tcp_address,20);
  6987.                 }
  6988.                 debug(F110,"getlocalipaddrs ip address list", messageBuf, 0);
  6989.                 host->h_addr_list++;
  6990.             }
  6991. #endif /* COMMENT */
  6992. #else   /* HADDRLIST */
  6993.             if (index != 0) {
  6994.                 buf[0] = '\0';
  6995.                 return(-1);
  6996.             }
  6997.             l_sa.sin_addr.s_addr = *((unsigned long *) (host->h_addr));
  6998.             ckstrncpy(buf,(char *)inet_ntoa(l_sa.sin_addr),bufsz);
  6999.             debug(F110,"getlocalipaddrs setting buf to",buf,0);
  7000. #endif  /* HADDRLIST */
  7001.             return(0);
  7002.         } else debug(F110,
  7003.                      "getlocalipaddrs: gethostbyname() failed",
  7004.                      localhost,
  7005.                      0
  7006.                      );
  7007.     }
  7008. #endif /* datageneral */
  7009.     return(-1);
  7010. }
  7011.  
  7012. #ifdef RLOGCODE                 /* TCP/IP RLOGIN protocol support code */
  7013. int
  7014. rlog_naws() {
  7015.     struct rlog_naws {
  7016.         unsigned char id[4];
  7017.         unsigned short rows, cols, ypix, xpix;
  7018.     } nawsbuf;
  7019.  
  7020.     if (ttnet != NET_TCPB)
  7021.       return 0;
  7022.     if (ttnproto != NP_RLOGIN
  7023. #ifdef CK_KERBEROS
  7024.         && ttnproto != NP_K4LOGIN
  7025.         && ttnproto != NP_EK4LOGIN
  7026.         && ttnproto != NP_K5LOGIN
  7027.         && ttnproto != NP_EK5LOGIN
  7028. #endif /* CK_KERBEROS */
  7029.          )
  7030.       return 0;
  7031.     if (!TELOPT_ME(TELOPT_NAWS))
  7032.       return 0;
  7033.  
  7034.     debug(F100,"rlogin Window Size sent","",0);
  7035.  
  7036.     nawsbuf.id[0] = nawsbuf.id[1] = 0377;
  7037.     nawsbuf.id[2] = nawsbuf.id[3] = 's';
  7038. #ifdef OS2
  7039.     nawsbuf.rows = htons((unsigned short) (VscrnGetHeight(VTERM)
  7040.                           -(tt_status[VTERM]?1:0)));
  7041.     nawsbuf.cols = htons((unsigned short) VscrnGetWidth(VTERM));
  7042. #else /* OS2 */
  7043.     nawsbuf.rows = htons((unsigned short) tt_rows);
  7044.     nawsbuf.cols = htons((unsigned short) tt_cols);
  7045. #endif /* OS2 */
  7046.     nawsbuf.ypix = htons(0);            /* y pixels */
  7047.  
  7048.     nawsbuf.xpix = htons(0);            /* x pixels */
  7049.     if (ttol((CHAR *)(&nawsbuf), sizeof(nawsbuf)) < 0)
  7050.       return(-1);
  7051.     return(0);
  7052. }
  7053.  
  7054. #ifdef OS2ORUNIX
  7055. #define RLOGOUTBUF
  7056. #endif /* OS2 */
  7057. static int
  7058. #ifdef CK_ANSIC
  7059. rlog_ini(CHAR * hostname, int port,
  7060.          struct sockaddr_in * l_addr, struct sockaddr_in * r_addr)
  7061. #else /* CK_ANSIC */
  7062. rlog_ini(hostname, port, l_addr, r_addr)
  7063.     CHAR * hostname;
  7064.     int port;
  7065.     struct sockaddr_in * l_addr;
  7066.     struct sockaddr_in * r_addr;
  7067. #endif /* CK_ANSIC */
  7068. /* rlog_ini */ {
  7069.  
  7070. #ifdef RLOGOUTBUF
  7071.     char outbuf[512];
  7072.     int  outbytes=0;
  7073. #endif /* RLOGOUTBUF */
  7074.     int flag = 0;
  7075. #define TERMLEN 16
  7076. #define CONSPDLEN 16
  7077.     CHAR localuser[UIDBUFLEN+1];
  7078.     CHAR remoteuser[UIDBUFLEN+1];
  7079.     int userlen = 0;
  7080.     CHAR term_speed[TERMLEN+CONSPDLEN+1];
  7081. #ifdef CONGSPD
  7082.     long conspd = -1L;
  7083. #endif /* CONGSPD */
  7084. #ifdef OS2
  7085.     extern int tt_type, max_tt;
  7086.     extern struct tt_info_rec tt_info[];
  7087. #endif /* OS2 */
  7088.     int i, n;
  7089.  
  7090.     int rc = 0;
  7091.     tn_reset();                 /* This call will reset all of the Telnet */
  7092.                                 /* options and then quit.  We need to do  */
  7093.                                 /* this since we use the Telnet options   */
  7094.                                 /* to hold various state information      */
  7095.     duplex = 0;                 /* Rlogin is always remote echo */
  7096.     rlog_inband = 0;
  7097.  
  7098. #ifdef CK_TTGWSIZ
  7099. /*
  7100.   But compute the values anyway before the first read since the out-
  7101.   of-band NAWS request would arrive before the first data byte (NULL).
  7102. */
  7103. #ifdef OS2
  7104.     /* Console terminal screen rows and columns */
  7105.     debug(F101,"rlog_ini tt_rows 1","",VscrnGetHeight(VTERM)
  7106.            -(tt_status[VTERM]?1:0));
  7107.     debug(F101,"rlog_ini tt_cols 1","",VscrnGetWidth(VTERM));
  7108.     /* Not known yet */
  7109.     if (VscrnGetWidth(VTERM) < 0 ||
  7110.         VscrnGetHeight(VTERM)-(tt_status[VTERM]?1:0) < 0) {
  7111.         ttgwsiz();                      /* Try to get screen dimensions */
  7112.     }
  7113.     debug(F101,
  7114.           "rlog_ini tt_rows 2",
  7115.           "",
  7116.           VscrnGetHeight(VTERM)-(tt_status[VTERM]?1:0)
  7117.           );
  7118.     debug(F101,"rlog_ini tt_cols 2","",VscrnGetWidth(VTERM));
  7119. #else /* OS2 */
  7120.     debug(F101,"rlog_ini tt_rows 1","",tt_rows);
  7121.     debug(F101,"rlog_ini tt_cols 1","",tt_cols);
  7122.     if (tt_rows < 0 || tt_cols < 0) {   /* Not known yet */
  7123.         ttgwsiz();                      /* Try to find out */
  7124.     }
  7125.     debug(F101,"rlog_ini tt_rows 2","",tt_rows);
  7126.     debug(F101,"rlog_ini tt_cols 2","",tt_cols);
  7127. #endif /* OS2 */
  7128. #endif /* CK_TTGWSIZ */
  7129.  
  7130.     ttflui();                           /* Start by flushing the buffers */
  7131.  
  7132.     rlog_mode = RL_COOKED;
  7133.  
  7134.     /* Determine the user's local username ... */
  7135.  
  7136.     localuser[0] = '\0';
  7137. #ifdef NT
  7138.     {
  7139.         char localuid[UIDBUFLEN+1];
  7140.         ckstrncpy((char *)localuser,(char *)GetLocalUser(),UIDBUFLEN);
  7141.     }
  7142.  
  7143.     if ( !localuser[0] )
  7144. #endif /* NT */
  7145.     {
  7146.         char * user = getenv("USER");
  7147.         if (!user)
  7148.           user = "";
  7149.         userlen = strlen(user);
  7150.         debug(F111,"rlogin getenv(USER)",user,userlen);
  7151.         ckstrncpy((char *)localuser,user,UIDBUFLEN);
  7152.         debug(F110,"rlog_ini localuser 1",localuser,0);
  7153.     }
  7154.     if ( !localuser[0] )
  7155.         strcpy((char *)localuser,"unknown");
  7156.     else if (ck_lcname) {
  7157.         cklower((char *)localuser);
  7158.         debug(F110,"rlog_ini localuser 2",localuser,0);
  7159.     }
  7160.  
  7161.     /* And the username to login with */
  7162.     if (uidbuf[0]) {
  7163.         ckstrncpy((char *)remoteuser,uidbuf,UIDBUFLEN);
  7164.         debug(F110,"rlog_ini remoteuser 1",remoteuser,0);
  7165.     } else if (localuser[0]) {
  7166.         ckstrncpy((char *)remoteuser,(char *)localuser,UIDBUFLEN);
  7167.         debug(F110,"rlog_ini remoteuser 2",remoteuser,0);
  7168.     } else {
  7169.         remoteuser[0] = '\0';
  7170.         debug(F110,"rlog_ini remoteuser 3",remoteuser,0);
  7171.     }
  7172.     if (ck_lcname)
  7173.       cklower((char *)remoteuser);
  7174.     debug(F110,"rlog_ini remoteuser 4",remoteuser,0);
  7175.  
  7176.     /* The command to issue is the terminal type and speed */
  7177.     term_speed[0] = '\0';
  7178.     if (tn_term) {                      /* SET TELNET TERMINAL-TYPE value */
  7179.         if (*tn_term) {                 /* (if any) takes precedence. */
  7180.             ckstrncpy((char *)term_speed, tn_term, TERMLEN);
  7181.             flag = 1;
  7182.         }
  7183.     } else {                            /* Otherwise the local terminal type */
  7184. #ifdef OS2
  7185.         /* In terminal-emulating versions, it's the SET TERM TYPE value */
  7186.         ckstrncpy(term_speed, (tt_type >= 0 && tt_type <= max_tt) ?
  7187.                 tt_info[tt_type].x_name : "network", TERMLEN);
  7188. #else
  7189.         /* In the others, we just look at the TERM environment variable */
  7190.         {
  7191.             char *p = getenv("TERM");
  7192.             if (p)
  7193.               ckstrncpy((char *)term_speed,p,TERMLEN);
  7194.             else
  7195.               term_speed[0] = '\0';
  7196. #ifdef VMS
  7197.             for (p = (char *) term_speed; *p; p++) {
  7198.                 if (*p == '-' && (!strcmp(p,"-80") || !strcmp(p,"-132")))
  7199.                   break;
  7200.                 else if (isupper(*p))
  7201.                   *p = tolower(*p);
  7202.             }
  7203.             *p = '\0';
  7204. #endif /* VMS */
  7205.         }
  7206. #endif /* OS2 */
  7207.     }
  7208.     n = strlen((char *)term_speed);
  7209.     if (n > 0) {                        /* We have a terminal type */
  7210.         if (!flag) {                    /* If not user-specified */
  7211.             for (i = 0; i < n; i++)     /* then lowercase it.    */
  7212.               if (isupper(term_speed[i]))
  7213.                 term_speed[i] = tolower(term_speed[i]);
  7214.         }
  7215.         debug(F110,"rlog_ini term_speed 1",term_speed,0);
  7216.  
  7217. #ifdef CONGSPD
  7218.         /* conspd() is not yet defined in all ck*tio.c modules */
  7219.         conspd = congspd();
  7220.         if (conspd > 0L) {
  7221.             ckstrncat((char *)term_speed,"/",sizeof(term_speed));
  7222.             ckstrncat((char *)term_speed,ckltoa(conspd),sizeof(term_speed));
  7223.         } else
  7224. #endif /* CONGSPD */
  7225.           ckstrncat((char *)term_speed,"/19200",sizeof(term_speed));
  7226.         debug(F110,"rlog_ini term_speed 2",term_speed,0);
  7227.     } else {
  7228.         term_speed[0] = '\0';
  7229.         debug(F110,"rlog_ini term_speed 3",term_speed,0);
  7230.     }
  7231.  
  7232. #ifdef CK_KERBEROS
  7233.     if (ttnproto == NP_K4LOGIN || ttnproto == NP_EK4LOGIN ||
  7234.         ttnproto == NP_K5LOGIN || ttnproto == NP_EK5LOGIN) {
  7235.         int kver, encrypt, rc;
  7236.         switch (ttnproto) {
  7237.           case NP_K4LOGIN:
  7238.             kver = 4;
  7239.             encrypt = 0;
  7240.             break;
  7241.           case NP_EK4LOGIN:
  7242.             kver = 4;
  7243.             encrypt = 1;
  7244.             break;
  7245.           case NP_K5LOGIN:
  7246.             kver = 5;
  7247.             encrypt = 0;
  7248.             break;
  7249.           case NP_EK5LOGIN:
  7250.             kver = 5;
  7251.             encrypt = 1;
  7252.             break;
  7253.         default:
  7254.             kver = 0;
  7255.             encrypt = 0;
  7256.         }
  7257.         rc = ck_krb_rlogin(hostname, port,
  7258.                            localuser, remoteuser, term_speed,
  7259.                            l_addr, r_addr, kver, encrypt);
  7260.         if (!rc) {                      /* success */
  7261.             TELOPT_ME(TELOPT_NAWS) = 1;
  7262.             rc = rlog_naws();
  7263.         }
  7264.         return(rc);
  7265.     } else
  7266. #endif /* CK_KERBEROS */
  7267.     if (ttnproto == NP_RLOGIN) {
  7268. #ifdef RLOGOUTBUF
  7269.         /*
  7270.          *  The rcmds start the connection with a series of init data:
  7271.          *
  7272.          *    a port number upon which client is listening for stderr data
  7273.          *    the user's name on the client machine
  7274.          *    the user's name on the server machine
  7275.          *    the terminal_type/speed or command to execute
  7276.          */
  7277.         outbuf[outbytes++] = 0;
  7278.         strcpy((char *)outbuf+outbytes,(char *)localuser);
  7279.         outbytes += strlen((char *)localuser) + 1;
  7280.         strcpy((char *)outbuf+outbytes,(char *)remoteuser);
  7281.         outbytes += strlen((char *)remoteuser) + 1;
  7282.         strcpy((char *)outbuf+outbytes,(char *)term_speed);
  7283.         outbytes += strlen((char *)term_speed) + 1;
  7284.         rc = ttol((CHAR *)outbuf,outbytes);
  7285. #else /* RLOGOUTBUF */
  7286.         ttoc(0);                        /* Send an initial NUL as wake-up */
  7287.         /* Send each variable with the trailing NUL */
  7288.         rc = ttol(localuser,strlen((char *)localuser)+1);
  7289.         if (rc > 0)
  7290.           rc = ttol(remoteuser,strlen((char *)remoteuser)+1);
  7291.         if (rc > 0)
  7292.           rc = ttol(term_speed,strlen((char *)term_speed)+1);
  7293. #endif /* RLOGOUTBUF */
  7294.  
  7295.         /* Now we are supposed to get back a single NUL as confirmation */
  7296.         errno = 0;
  7297.         rc = ttinc(60);
  7298.         debug(F101,"rlogin first ttinc","",rc);
  7299.         if (rc > 0) {
  7300.             debug(F101,"rlogin ttinc 1","",rc);
  7301.             printf(
  7302.                "Rlogin protocol error - 0x%x received instead of 0x00\n", rc);
  7303.             return(-1);
  7304.         } else if (rc < 0) {
  7305.             debug(F101,"rlogin ttinc errno","",errno);
  7306.             /* printf("Network error: %d\n", errno); */
  7307.             return(-1);
  7308.         }
  7309.     }
  7310.     return(0);
  7311. }
  7312.  
  7313. /* two control messages are defined:
  7314.  
  7315.    a double flag byte of 'o' indicates a one-byte message which is
  7316.    identical to what was once carried out of band.
  7317.  
  7318.    a double flag byte of 'q' indicates a zero-byte message.  This
  7319.    message is interpreted as two \377 data bytes.  This is just a
  7320.    quote rule so that binary data from the server does not confuse the
  7321.    client.  */
  7322.  
  7323. int 
  7324. rlog_ctrl(cp, n)
  7325.      unsigned char *cp;
  7326.      int n;
  7327. {
  7328.     if ((n >= 5) && (cp[2] == 'o') && (cp[3] == 'o')) {
  7329.         if (rlog_oob(&cp[4],1))
  7330.             return(-5);
  7331.         return(5);
  7332.     } else if ((n >= 4) && (cp[2] == 'q') && (cp[3] == 'q')) {
  7333.         /* this is somewhat of a hack */
  7334.         cp[2] = '\377';
  7335.         cp[3] = '\377';
  7336.         return(2);
  7337.     }
  7338.     return(0);
  7339. }
  7340.  
  7341. static int
  7342. rlog_oob(oobdata, count) CHAR * oobdata; int count; {
  7343.     int i;
  7344.     int flush = 0;
  7345.  
  7346.     debug(F111,"rlogin out_of_band","count",count);
  7347.  
  7348.     for (i = 0; i<count; i++)   {
  7349.         debug(F101,"rlogin out_of_band","",oobdata[i]);
  7350.                 if (oobdata[i] & 0x01)
  7351.                         continue;
  7352.  
  7353.         if (oobdata[i] & 0x02) { /* Flush Buffered Data not yet displayed */
  7354.             debug(F101,"rlogin Flush Buffered Data command","",oobdata[i]);
  7355.  
  7356.             /* Only flush the data if in fact we are in a mode that won't */
  7357.             /* get out of sync.  Ie, not when we are in protocol mode.    */
  7358.             switch ( what ) {
  7359.             case W_NOTHING:
  7360.             case W_CONNECT:
  7361.             case W_COMMAND:
  7362.                 if ( rlog_inband )
  7363.                     flush = 1;
  7364.                 else
  7365.                     ttflui();
  7366.                 break;
  7367.             }
  7368.         }
  7369.         if (oobdata[i] & 0x10) {        /* Switch to RAW mode */
  7370.             debug(F101,"rlogin Raw Mode command","",oobdata[i]);
  7371.             rlog_mode = RL_RAW;
  7372.         }
  7373.  
  7374.         if (oobdata[i] & 0x20) {        /* Switch to COOKED mode */
  7375.             debug(F101,"rlogin Cooked Mode command","",oobdata[i]);
  7376.             rlog_mode = RL_COOKED;
  7377.         }
  7378.         if (oobdata[i] & 0x80)
  7379.         {        /* Send Window Size Info */
  7380.             debug(F101,"rlogin Window Size command","",oobdata[i]);
  7381.             /* Remember to send WS Info when Window Size changes */
  7382.             if ( !TELOPT_ME(TELOPT_NAWS) ) {
  7383.                 TELOPT_ME(TELOPT_NAWS) = 1;
  7384.                 rlog_naws();
  7385.             }
  7386.         }
  7387.     }
  7388.     return(flush);
  7389. }
  7390. #ifndef TCPIPLIB
  7391. static SIGTYP
  7392. rlogoobh(sig) int sig; {
  7393. #ifdef SOLARIS
  7394.     char                                /* Or should it be char for all? */
  7395. #else
  7396.     CHAR
  7397. #endif /* SOLARIS */
  7398.       oobdata;
  7399.  
  7400.     /* int  count = 0; */ /* (not used) */
  7401.  
  7402.     while (recv(ttyfd, &oobdata, 1, MSG_OOB) < 0) {
  7403.       /*
  7404.        * We need to do some special processing here.
  7405.        * Just in case the socket is blocked for input
  7406.        *
  7407.        */
  7408.         switch (errno) {
  7409.           case EWOULDBLOCK:
  7410.             break;
  7411.           default:
  7412.             return;
  7413.         }
  7414.     }
  7415.     debug(F101,"rlogin out_of_band","",oobdata);
  7416.     if (oobdata == 0x02) {      /* Flush Buffered Data not yet displayed */
  7417.         debug(F101,"rlogin Flush Buffered Data command","",oobdata);
  7418.         netflui();
  7419.     }
  7420.     if (oobdata & 0x10) {               /* Switch to raw mode */
  7421.         debug(F101,"rlogin Raw Mode command","",oobdata);
  7422.         rlog_mode = RL_RAW;
  7423.     }
  7424.     if (oobdata & 0x20) {               /* Switch to cooked mode */
  7425.         debug(F101,"rlogin Cooked Mode command","",oobdata);
  7426.         rlog_mode = RL_COOKED;
  7427.     }
  7428.     if (oobdata & 0x80) {                 /* Send Window Size Info */
  7429.         debug(F101,"rlogin Window Size command","",oobdata);
  7430.         /* Remember to send WS Info when Window Size changes */
  7431.         if ( !TELOPT_ME(TELOPT_NAWS) ) {
  7432.             TELOPT_ME(TELOPT_NAWS) = 1;
  7433.             rlog_naws();
  7434.         }
  7435.     }
  7436. }
  7437. #endif /* TCPIPLIB */
  7438. #endif /* RLOGCODE */
  7439.  
  7440. /* Send network BREAK */
  7441. /*
  7442.   Returns -1 on error, 0 if nothing happens, 1 if BREAK sent successfully.
  7443. */
  7444. int
  7445. netbreak() {
  7446.     CHAR buf[3];
  7447.     if (ttnet == NET_TCPB) {
  7448.         if (ttnproto == NP_TELNET) {
  7449. #ifdef TNCODE
  7450.             buf[0] = (CHAR) IAC; buf[1] = (CHAR) BREAK; buf[2] = (CHAR) 0;
  7451.             if (
  7452. #ifdef OS2
  7453.                 nettol((char *) buf, 2)
  7454. #else
  7455.                 ttol(buf, 2)
  7456. #endif /* OS2 */
  7457.                 < 2)
  7458.               return(-1);
  7459.             if (tn_deb || debses || deblog) {
  7460.                 extern char tn_msg[];
  7461.                 ckmakmsg(tn_msg,TN_MSG_LEN,"TELNET SENT ",TELCMD(BREAK),
  7462.                           NULL,NULL);
  7463.                 debug(F101,tn_msg,"",BREAK);
  7464.                 if (debses || tn_deb) tn_debug(tn_msg);
  7465.             }
  7466.             return(1);
  7467. #else
  7468.             debug(F100,"netbreak no TNCODE","",0);
  7469.             return(0);
  7470. #endif /* TNCODE */
  7471.         }
  7472.         /* Insert other TCP/IP protocols here */
  7473.     }
  7474.     /* Insert other networks here */
  7475.     return(0);
  7476. }
  7477. #endif /* NETCONN */
  7478.  
  7479.  
  7480. #ifdef NETCONN
  7481. #ifdef SUNX25
  7482. /*
  7483.   SunLink X.25 support by Marcello Frutig, Catholic University,
  7484.   Rio de Janeiro, Brazil, 1990.
  7485. */
  7486.  
  7487. /* PAD X.3, X.28 and X.29 support */
  7488.  
  7489. static CHAR x29err[MAXPADPARMS+3] = { X29_ERROR, INVALID_PAD_PARM, '\0' };
  7490.  
  7491. /* Initialize PAD */
  7492.  
  7493. extern CHAR padparms[];
  7494.  
  7495. VOID
  7496. initpad() {
  7497.   padparms[PAD_BREAK_CHARACTER]        = 0;  /* Break character */
  7498.   padparms[PAD_ESCAPE]                 = 1;  /* Escape permitted */
  7499.   padparms[PAD_ECHO]                   = 1;  /* Kermit PAD does echo */
  7500.   padparms[PAD_DATA_FORWARD_CHAR]      = 2;  /* forward character CR */
  7501.   padparms[PAD_DATA_FORWARD_TIMEOUT]   = 0;  /* no timeout forward condition */
  7502.   padparms[PAD_FLOW_CONTROL_BY_PAD]    = 0;  /* not used */
  7503.   padparms[PAD_SUPPRESSION_OF_SIGNALS] = 1;  /* allow PAD service signals */
  7504.   padparms[PAD_BREAK_ACTION]           = 21; /* brk action: INT pk + brk ind*/
  7505.   padparms[PAD_SUPPRESSION_OF_DATA]    = 0;  /* no supression of user data */
  7506.   padparms[PAD_PADDING_AFTER_CR]       = 0;  /* no padding after CR */
  7507.   padparms[PAD_LINE_FOLDING]           = 0;  /* no line fold */
  7508.   padparms[PAD_LINE_SPEED]             = 0;  /* line speed - don't care */
  7509.   padparms[PAD_FLOW_CONTROL_BY_USER]   = 0;  /* flow cont of PAD - not used */
  7510.   padparms[PAD_LF_AFTER_CR]            = 0;  /* no LF insertion after CR */
  7511.   padparms[PAD_PADDING_AFTER_LF]       = 0;  /* no padding after LF */
  7512.   padparms[PAD_EDITING]                = 1;  /* can edit */
  7513.   padparms[PAD_CHAR_DELETE_CHAR]       = 8;  /* character delete character */
  7514.   padparms[PAD_BUFFER_DELETE_CHAR]     = 21; /* buffer delete character */
  7515.   padparms[PAD_BUFFER_DISPLAY_CHAR]    = 18; /* buffer display character */
  7516. }
  7517.  
  7518. /* Set PAD parameters */
  7519.  
  7520. VOID
  7521. setpad(s,n) CHAR *s; int n; {
  7522.     int i;
  7523.     CHAR *ps = s;
  7524.  
  7525.     if (n < 1) {
  7526.         initpad();
  7527.     } else {
  7528.         for (i = 0; i < n; i++) {
  7529.             if (*ps > MAXPADPARMS)
  7530.               x29err[i+2] = *ps;
  7531.             else
  7532.               padparms[*ps] = *(ps+1);
  7533.             ps += 2;
  7534.         }
  7535.     }
  7536. }
  7537.  
  7538. /* Read PAD parameters */
  7539.  
  7540. VOID
  7541. readpad(s,n,r) CHAR *s; int n; CHAR *r; {
  7542.     int i;
  7543.     CHAR *ps = s;
  7544.     CHAR *pr = r;
  7545.  
  7546.     *pr++ = X29_PARAMETER_INDICATION;
  7547.     if (n > 0) {
  7548.         for (i = 0; i < n; i++, ps++) {
  7549.             if (*ps > MAXPADPARMS) {
  7550.                 x29err[i+2] = *ps++;
  7551.             } else {
  7552.                 *pr++ = *ps;
  7553.                 *pr++ = padparms[*ps++];
  7554.             }
  7555.         }
  7556.     } else {
  7557.         for (i = 1; i < MAXPADPARMS; i++) {
  7558.             *pr++ = i;
  7559.             *pr++ = padparms[i];
  7560.         }
  7561.     }
  7562. }
  7563.  
  7564. int
  7565. qbitpkt(s,n) CHAR *s; int n; {
  7566.     CHAR *ps = s;
  7567.     int x29cmd = *ps;
  7568.     CHAR *psa = s+1;
  7569.     CHAR x29resp[(MAXPADPARMS*2)+1];
  7570.  
  7571.     switch (x29cmd) {
  7572.  
  7573.         case X29_SET_PARMS:
  7574.             setpad (ps+1,n/2);
  7575.             if ((int)strlen((char *)x29err) > 2) {
  7576.                 ttol(x29err,(int)strlen((char *)x29err));
  7577.                 x29err[2] = '\0';
  7578.             }
  7579.             return (-2);
  7580.         case X29_READ_PARMS:
  7581.             readpad (ps+1,n/2,x29resp);
  7582.             setqbit ();
  7583.             ttol(x29resp,(n>1)?(n+1):(2*MAXPADPARMS+1));
  7584.             if ((int)strlen((char *)x29err) > 2) {
  7585.                 ttol(x29err,(int)strlen((char *)x29err));
  7586.                 x29err[2] = '\0';
  7587.             }
  7588.             resetqbit();
  7589.             break;
  7590.         case X29_SET_AND_READ_PARMS:
  7591.             setpad (ps+1,n/2);
  7592.             readpad (ps+1,n/2,x29resp);
  7593.             setqbit();
  7594.             ttol(x29resp,(n>1)?(n+1):(2*MAXPADPARMS+1));
  7595.             if ((int)strlen((char *)x29err) > 2) {
  7596.                 ttol (x29err,(int)strlen((char *)x29err));
  7597.                 x29err [2] = '\0';
  7598.             }
  7599.             resetqbit();
  7600.             return (-2);
  7601.         case X29_INVITATION_TO_CLEAR:
  7602.             (VOID) x25clear();
  7603.             return (-1);
  7604.         case X29_INDICATION_OF_BREAK:
  7605.             break;
  7606.     }
  7607.     return (0);
  7608. }
  7609.  
  7610. /* PAD break action processor */
  7611.  
  7612. VOID
  7613. breakact() {
  7614.     extern char x25obuf[MAXOX25];
  7615.     extern int obufl;
  7616.     extern int active;
  7617.     extern unsigned char tosend;
  7618.     static CHAR indbrk[3] = {
  7619.         X29_INDICATION_OF_BREAK,
  7620.         PAD_SUPPRESSION_OF_DATA,
  7621.         1
  7622.     };
  7623.     CHAR intudat, cause, diag;
  7624.  
  7625.     if (x25stat() < 0) return;  /* Ignore if no virtual call established */
  7626.  
  7627.     if (padparms[PAD_BREAK_ACTION] != 0) /* Forward condition */
  7628.         if (ttol((CHAR *)x25obuf,obufl) < 0) {
  7629.             perror ("\r\nCan't send characters");
  7630.             active = 0;
  7631.         } else {
  7632.             bzero (x25obuf,sizeof(x25obuf));
  7633.             obufl = 0;
  7634.             tosend = 0;
  7635.         };
  7636.  
  7637.     switch (padparms[PAD_BREAK_ACTION]) {
  7638.  
  7639.        case 0 : break;                  /* do nothing */
  7640.        case 1 : /* send interrupt packet with interrupt user data field = 1 */
  7641.                 intudat = 1;
  7642.                 x25intr (intudat);
  7643.                 break;
  7644.        case 2 : /* send reset packet with cause and diag = 0 */
  7645.                 cause = diag = 0;
  7646.                 x25reset (cause,diag);
  7647.                 break;
  7648.        case 5 : /* send interrupt packet with interrupt user data field = 0 */
  7649.                 intudat = 0;
  7650.                 x25intr (intudat);
  7651.                 setqbit ();
  7652.                 /* send indication of break without a parameter field */
  7653.                 ttoc(X29_INDICATION_OF_BREAK);
  7654.                 resetqbit ();
  7655.                 break;
  7656.        case 8 : active = 0;             /* leave data transfer */
  7657.                 conol ("\r\n");
  7658.                 break;
  7659.        case 21: /* send interrupt packet with interrupt user data field = 0 */
  7660.                 intudat = 0;
  7661.                 x25intr (intudat);
  7662.                 setpad (indbrk+1,2);    /* set pad to discard input */
  7663.                 setqbit ();
  7664.                 /* send indication of break with parameter field */
  7665.                 ttol (indbrk,sizeof(indbrk));
  7666.                 resetqbit ();
  7667.                 break;
  7668.      }
  7669. }
  7670.  
  7671. /* X.25 support functions */
  7672.  
  7673. X25_CAUSE_DIAG diag;
  7674.  
  7675. /*
  7676.   Convert a null-terminated string representing an X.121 address
  7677.   to a packed BCD form.
  7678. */
  7679. int
  7680. pkx121(str,bcd) char *str; CHAR *bcd; {
  7681.     int i, j;
  7682.     u_char c;
  7683.  
  7684.     i = j = 0;
  7685.     while (str[i]) {
  7686.         if (i >= 15 || str [i] < '0' || str [i] > '9')
  7687.           return (-1);
  7688.         c = str [i] - '0';
  7689.         if (i & 1)
  7690.           bcd [j++] |= c;
  7691.         else
  7692.           bcd [j] = c << 4;
  7693.         i++;
  7694.     }
  7695.     return (i);
  7696. }
  7697.  
  7698. /* Reads and prints X.25 diagnostic */
  7699.  
  7700. int
  7701. x25diag () {
  7702.     int i;
  7703.  
  7704.     bzero ((char *)&diag,sizeof(diag));
  7705.     if (ioctl(ttyfd,X25_RD_CAUSE_DIAG,&diag)) {
  7706.         perror ("Reading X.25 diagnostic");
  7707.         return(-1);
  7708.     }
  7709.     if (diag.datalen > 0) {
  7710.         printf ("X.25 Diagnostic :");
  7711.         for (i = 0; i < (int)diag.datalen; i++)
  7712.           printf(" %02h",diag.data[i])+
  7713.         printf ("\r\n");
  7714.     }
  7715.     return(0);
  7716. }
  7717.  
  7718. /* X.25 Out-of-Band Signal Handler */
  7719.  
  7720. SIGTYP
  7721. x25oobh(foo) int foo; {
  7722.     int oobtype;
  7723.     u_char oobdata;
  7724.     int t;
  7725.  
  7726.     (VOID) signal(SIGURG,x25oobh);
  7727.     do {
  7728.         if (ioctl(ttyfd,X25_OOB_TYPE,&oobtype)) {
  7729.             perror ("Getting signal type");
  7730.             return;
  7731.         }
  7732.         switch (oobtype) {
  7733.           case INT_DATA:
  7734.             if (recv(ttyfd,(char *)&oobdata,1,MSG_OOB) < 0) {
  7735.                 perror ("Receiving X.25 interrupt data");
  7736.                 return;
  7737.             }
  7738.             t = oobdata;
  7739.             printf ("\r\nInterrupt received, data = %d\r\n", t);
  7740.             break;
  7741.           case VC_RESET:
  7742.             printf ("\r\nVirtual circuit reset\r\n");
  7743.             x25diag ();
  7744.             break;
  7745.           case N_RESETS:
  7746.             printf ("\r\nReset timeout\r\n");
  7747.             break;
  7748.           case N_CLEARS:
  7749.             printf ("\r\nClear timeout\r\n");
  7750.             break;
  7751.           case MSG_TOO_LONG:
  7752.             printf ("\r\nMessage discarded, too long\r\n");
  7753.             break;
  7754.           default:
  7755.             if (oobtype) printf("\r\nUnknown oob type %d\r\n",oobtype);
  7756.             break;
  7757.         }
  7758.     } while (oobtype);
  7759. }
  7760.  
  7761. /* Send a X.25 interrupt packet */
  7762.  
  7763. int
  7764. #ifdef CK_ANSIC
  7765. x25intr(char intr)
  7766. #else
  7767. x25intr(intr) char intr;
  7768. #endif /* CK_ANSIC */
  7769. /* x25intr */ {
  7770.     if (send(ttyfd,&intr,1,MSG_OOB) < 0) return(-1);
  7771.     debug(F100,"X.25 intr","",0);
  7772.     return(0);
  7773. }
  7774.  
  7775. /* Reset X.25 virtual circuit */
  7776. int
  7777. #ifdef CK_ANSIC
  7778. x25reset(char cause, char diagn)
  7779. #else
  7780. x25reset(cause, diagn) char cause; char diagn;
  7781. #endif /* CK_ANSIC */
  7782. /* x25reset */ {
  7783.     bzero ((char *)&diag,sizeof(diag));
  7784.     diag.flags   = 0;
  7785.     diag.datalen = 2;
  7786.     diag.data[0] = cause;
  7787.     diag.data[1] = diagn;
  7788.     if (ioctl(ttyfd,X25_WR_CAUSE_DIAG,&diag) < 0)
  7789.       return(-1);
  7790.     debug(F100,"X.25 reset","",0);
  7791.     return(0);
  7792. }
  7793.  
  7794. /* Clear X.25 virtual circuit */
  7795. int
  7796. x25clear() {
  7797.     int i;
  7798.     debug(F100,"X.25 clear","",0);
  7799.     bzero ((char *)&diag,sizeof(diag));
  7800.     diag.flags = (1 << DIAG_TYPE);
  7801.     diag.datalen = 2;
  7802.     diag.data[0] = 0;
  7803.     diag.data[1] = 0;
  7804.     ioctl (ttyfd,X25_WR_CAUSE_DIAG,&diag); /* Send Clear Request */
  7805.     return(ttclos(0));                  /* Close socket */
  7806. }
  7807.  
  7808. /* X.25 status */
  7809. int
  7810. x25stat() {
  7811.     if (ttyfd == -1) return (-1);
  7812.     return(0);
  7813. }
  7814.  
  7815. /* Set Q_BIT on */
  7816. VOID
  7817. setqbit() {
  7818.     static int qbiton = 1 << Q_BIT;
  7819.     ioctl (ttyfd,X25_SEND_TYPE,&qbiton);
  7820. }
  7821.  
  7822. /* Set Q_BIT off */
  7823. VOID
  7824. resetqbit() {
  7825.     static int qbitoff = 0;
  7826.     ioctl (ttyfd,X25_SEND_TYPE,&qbitoff);
  7827. }
  7828.  
  7829. /* Read n characters from X.25 circuit into buf */
  7830.  
  7831. int
  7832. x25xin(n,buf) int n; CHAR *buf; {
  7833.     register int x, c;
  7834.     int qpkt;
  7835.  
  7836.     do {
  7837.         x = read(ttyfd,buf,n);
  7838.         if (buf[0] & (1 << Q_BIT)) { /* If Q_BIT packet, process it */
  7839.             /* If return -1 : invitation to clear; -2 : PAD changes */
  7840.             if ((c=qbitpkt(buf+1,x-2)) < 0) return(c);
  7841.             qpkt = 1;
  7842.         } else qpkt = 0;
  7843.     } while (qpkt);
  7844.  
  7845. #ifdef COMMENT                  /* Disabled by Stephen Riehm 19.12.97 */
  7846.     /* BUG!
  7847.      * if buf[] is full, then this null lands in nirvana!
  7848.      * I was unable to find any code which needs a trailing null in buf[]
  7849.      */
  7850.     if (x > 0) buf[x] = '\0';
  7851. #endif /* COMMENT */
  7852.     if (x < 1) x = -1;
  7853.     debug(F101,"x25xin x","",x);
  7854.  
  7855.     return(x);
  7856. }
  7857.  
  7858. #ifdef COMMENT /* NO LONGER NEEDED! */
  7859. /* X.25 read a line */
  7860.  
  7861. int
  7862. #ifdef PARSENSE
  7863. #ifdef CK_ANSIC
  7864. x25inl(CHAR *dest, int max,int timo, CHAR eol, CHAR start)
  7865. #else
  7866. x25inl(dest,max,timo,eol,start) int max,timo; CHAR *dest, eol, start;
  7867. #endif /* CK_ANSIC */
  7868. #else /* not PARSENSE */
  7869. #ifdef CK_ANSIC
  7870. x25inl(CHAR *dest, int max,int timo, CHAR eol)
  7871. #else
  7872. x25inl(dest,max,timo,eol) int max,timo; CHAR *dest, eol;
  7873. #endif /* __SDTC__ */
  7874. #endif /*PARSENSE */
  7875.  /* x25inl */ {
  7876.     CHAR *pdest;
  7877.     int pktype, goteol, rest, n;
  7878.     int i, flag = 0;
  7879.     extern int ttprty, ttpflg;
  7880.     int ttpmsk;
  7881.  
  7882.     ttpmsk = (ttprty) ? 0177 : 0377;    /* Set parity stripping mask */
  7883.  
  7884.     debug(F101,"x25inl max","",max);
  7885.     debug(F101,"x25inl eol","",eol);
  7886.     pdest  = dest;
  7887.     rest   = max;
  7888.     goteol = 0;
  7889.     do {
  7890.         n = read(ttyfd,pdest,rest);
  7891.         n--;
  7892.         pktype = *pdest & 0x7f;
  7893.         switch (pktype) {
  7894.           case 1 << Q_BIT:
  7895.             if (qbitpkt(pdest+1,--n) < 0) return(-2);
  7896.             break;
  7897.           default:
  7898.             if (flag == 0) { /* if not in packet, search start */
  7899.                 for (i = 1; (i < n) &&
  7900.                      !(flag = ((dest[i] & 0x7f) == start));
  7901.                      i++);
  7902.                 if (flag == 0) { /* not found, discard junk */
  7903.                     debug(F101,"x25inl skipping","",n);
  7904.                     continue;
  7905.                 } else {                /* found, discard junk before start */
  7906.                     int k;
  7907.                     n = n - i + 1;
  7908.                     for (k = 1; k <= n; k++, i++) dest[k] = dest[i];
  7909.                 }
  7910.             }
  7911.             for (i = 0; (i < n) && /* search for eol */
  7912.                  !(goteol=(((*pdest = *(pdest+1)&ttpmsk)&0x7f)== eol));
  7913.                  i++,pdest++);
  7914.             *pdest = '\0';
  7915.             rest -= n;
  7916.         }
  7917.     } while ((rest > 0) && (!goteol));
  7918.  
  7919.     if (goteol) {
  7920.         n = max - rest;
  7921.         debug (F111,"x25inl X.25 got",(char *) dest,n);
  7922.         if (timo) ttimoff();
  7923.         if (ttpflg++ == 0 && ttprty == 0) {
  7924.             if ((ttprty = parchk(dest,start,n)) > 0) {
  7925.                 int j;
  7926.                 debug(F101,"x25inl senses parity","",ttprty);
  7927.                 debug(F110,"x25inl packet before",(char *)dest,0);
  7928.                 ttpmsk = 0x7f;
  7929.                 for (j = 0; j < n; j++)
  7930.                   dest[j] &= 0x7f; /* Strip parity from packet */
  7931.                 debug(F110,"x25inl packet after ",dest,0);
  7932.             } else {
  7933.                 debug(F101,"parchk","",ttprty);
  7934.                 if (ttprty < 0) { ttprty = 0; n = -1; }
  7935.             }
  7936.         }
  7937.         ttimoff();
  7938.         return(n);
  7939.     }
  7940.     ttimoff();
  7941.     return(-1);
  7942. }
  7943. #endif /* COMMENT */
  7944. #endif /* SUNX25 */
  7945.  
  7946. #ifdef IBMX25
  7947. /*
  7948.  * IBM X25 support - using the NPI streams interface
  7949.  * written by Stephen Riehm, pc-plus, Munich Germany
  7950.  */
  7951.  
  7952. /* riehm: missing functions / TODO list */
  7953.  
  7954. /*
  7955.   x25intr() - Send an interrupt packet
  7956. */
  7957.  
  7958. /* return an error message depending on packet type */
  7959. char *
  7960. x25err(n) int n; {
  7961.     static char buf[30];
  7962.     switch (n) {
  7963.       case NBADADDR:     return "invalid address";
  7964.       case NBADOPT:      return "invalid options";
  7965.       case NACCESS:      return "no permission";
  7966.       case NNOADDR:      return "unable to allocate address";
  7967.       case NOUTSTATE:    return "invalid state";
  7968.       case NBADSEQ:      return "invalid sequence number";
  7969.       case NSYSERR:      return "system error";
  7970.       case NBADDATA:     return "invalid data size";
  7971.       case NBADFLAG:     return "invalid flag";
  7972.       case NNOTSUPPORT:  return "unsupported primitive";
  7973.       case NBOUND:       return "address in use";
  7974.       case NBADQOSPARAM: return "bad QOS parameters";
  7975.       case NBADQOSTYPE:  return "bad QOS type";
  7976.       case NBADTOKEN:    return "bad token value";
  7977.       case NNOPROTOID:   return "protocol id could not be allocated";
  7978.       case NODDCUD:      return "odd length call user data";
  7979.       default:
  7980.         ckmakmsg(buf,sizeof(buf),"Unknown NPI error ",ckitoa(n),NULL,NULL);
  7981.         return buf;
  7982.     }
  7983. }
  7984.  
  7985. /* turn a meaningless primitive number into a meaningful primitive name */
  7986. char *
  7987. x25prim(n) int n; {
  7988.     static char buf[30];
  7989.     switch(n) {
  7990.       case N_BIND_ACK:     return "N_BIND_ACK";
  7991.       case N_BIND_REQ:     return "N_BIND_REQ";
  7992.       case N_CONN_CON:     return "N_CONN_CON";
  7993.       case N_CONN_IND:     return "N_CONN_IND";
  7994.       case N_CONN_REQ:     return "N_CONN_REQ";
  7995.       case N_CONN_RES:     return "N_CONN_RES";
  7996.       case N_DATACK_IND:   return "N_DATAACK_IND";
  7997.       case N_DATACK_REQ:   return "N_DATAACK_REQ";
  7998.       case N_DATA_IND:     return "N_DATA_IND";
  7999.       case N_DATA_REQ:     return "N_DATA_REQ";
  8000.       case N_DISCON_IND:   return "N_DISCON_IND";
  8001.       case N_DISCON_REQ:   return "N_DISCON_REQ";
  8002.       case N_ERROR_ACK:    return "N_ERROR_ACK";
  8003.       case N_EXDATA_IND:   return "N_EXDATA_IND";
  8004.       case N_EXDATA_REQ:   return "N_EXDATA_REQ";
  8005.       case N_INFO_ACK:     return "N_INFO_ACK";
  8006.       case N_INFO_REQ:     return "N_INFO_REQ";
  8007.       case N_OK_ACK:       return "N_OK_ACK";
  8008.       case N_OPTMGMT_REQ:  return "N_OPTMGMT_REQ";
  8009.       case N_RESET_CON:    return "N_RESET_CON";
  8010.       case N_RESET_IND:    return "N_RESET_IND";
  8011.       case N_RESET_REQ:    return "N_RESET_REQ";
  8012.       case N_RESET_RES:    return "N_RESET_RES";
  8013.       case N_UDERROR_IND:  return "N_UDERROR_IND";
  8014.       case N_UNBIND_REQ:   return "N_UNBIND_REQ";
  8015.       case N_UNITDATA_REQ: return "N_UNITDATA_REQ";
  8016.       case N_UNITDATA_IND: return "N_UNITDATA_IND";
  8017.       default:
  8018.         ckmakmsg(buf,sizeof(buf),"UNKNOWN (",ckitoa(n),")",NULL);
  8019.         return buf;
  8020.     }
  8021. }
  8022.  
  8023. /*****************************************************************************
  8024.  * Function: x25getmsg()
  8025.  * Description: get a STREAMS message, and check it for errors
  8026.  *
  8027.  * Parameters:
  8028.  * fd           - file descriptor to x25 device (opened)
  8029.  * control      - control buffer (pre-allocated)
  8030.  * ctl_size     - size of control buffer
  8031.  * data         - data buffer (pre-allocated)
  8032.  * data_size    - size of data buffer
  8033.  * flags        - flags for getmsg()
  8034.  * expected     - expected Primitive type
  8035.  *
  8036.  * Return Value:
  8037.  *      >= 0    OK (size of data returned)
  8038.  *      -1      error
  8039.  *
  8040.  */
  8041. int
  8042. x25getmsg( fd, control, ctl_size, data, data_size, get_flags, expected )
  8043.     int                 fd;             /* X25 device (opened) */
  8044.     N_npi_ctl_t         *control;       /* control buffer (pre-allocated) */
  8045.     int                 ctl_size;       /* size of control buffer */
  8046.     N_npi_data_t        *data;          /* data buffer (pre-allocated) */
  8047.     int                 data_size;      /* size of data buffer */
  8048.     int                 *get_flags;     /* getmsg() flags */
  8049.     int                 expected;       /* expected primitive type */
  8050. /* x25getmsg */ {
  8051.     int                 rc = 0;         /* return code */
  8052.     struct strbuf       *get_ctl=NULL;  /* getmsg control */
  8053.     struct strbuf       *get_data=NULL; /* getmsg data */
  8054.     int                 more = 0;       /* flag for more data etc */
  8055.     int                 file_status = -1; /* file async status */
  8056.     N_npi_ctl_t         * result;       /* pointer to simplify switch() */
  8057.     int                 packet_type = -1; /* unknown packet thus far */
  8058.  
  8059. #ifdef TRACE
  8060.     printf( "TRACE: entering x25getmsg\n" );
  8061. #endif /* TRACE */
  8062.  
  8063.     debug( F110, "x25getmsg waiting for packet ", x25prim( expected ), 0);
  8064.     /* prepare the control structures for getmsg */
  8065.     if (control) {
  8066.         if ((get_ctl = (struct strbuf*)malloc(sizeof(struct strbuf))) == NULL)
  8067.           {
  8068.               perror("kermit x25getmsg(): get_ctl malloc failed\n");
  8069.               debug( F100, "x25getmsg malloc failed for get_ctl\n", "", 0);
  8070.               return(-1);
  8071.           }
  8072.         /* allow getmsg to return an unexpected packet type (which may be
  8073.          * larger than the expected one)
  8074.          */
  8075.         get_ctl->maxlen = NPI_MAX_CTL;
  8076.         get_ctl->len = 0;
  8077.         get_ctl->buf = (char *)control;
  8078.     } else {
  8079.         printf(
  8080.  "kermit x25getmsg(): internal error. control buffer MUST be pre-allocated!\n"
  8081.                );
  8082.         debug(F100,"x25getmsg internal error. no buffer pre-allocated","",0);
  8083.         return( -1 );
  8084.     }
  8085.     if (data) {
  8086.         if ((get_data = (struct strbuf*)malloc(sizeof(struct strbuf))) == NULL)
  8087.           {
  8088.             perror("kermit x25getmsg(): get_data malloc failed\n");
  8089.             debug( F100, "x25getmsg malloc failed for get_data\n", "", 0);
  8090.             return(-1);
  8091.         }
  8092.         get_data->maxlen = (NPI_MAX_DATA < data_size ) ?
  8093.           NPI_MAX_DATA :
  8094.             data_size;
  8095.         get_data->len = 0;
  8096.         get_data->buf = (char *)data;
  8097.     }
  8098.  
  8099.     /* get an X.25 packet -
  8100.      * it may be any kind of packet, so check for special cases
  8101.      * it may be split into multiple parts - so loop if necessary
  8102.      */
  8103.     do {
  8104. #ifdef DEBUG
  8105.         printf( "kermit: x25getmsg(): getting a message\n" );
  8106. #endif /* DEBUG */
  8107.         errno = 0;
  8108.         if ((more = getmsg(fd, get_ctl, get_data, get_flags)) < 0) {
  8109. #ifdef DEBUG
  8110.             printf( "kermit: x25getmsg(): getmsg returned an error\n" );
  8111.             perror( "getmsg error was" );
  8112. #endif /* DEBUG */
  8113.             debug(F101, "x25getmsg getmsg returned an error\n", "", errno);
  8114.             if ((errno == EAGAIN) && (get_data && (get_data->len > 0)) ) {
  8115.                 /* was in non-blocking mode, nothing to get, but we're
  8116.                  * already waiting for the rest of the packet -
  8117.                  * switch to blocking mode for the next read.
  8118.                  * file_status used to reset file status before returning
  8119.                  */
  8120.                 if ((file_status = fcntl(fd, F_GETFL, 0)) < 0
  8121.                     || fcntl(fd, F_SETFL, file_status & ~O_NDELAY) < 0)
  8122.                   {
  8123.                       perror("x25getmsg(): couldn't change x25 blocking mode");
  8124.                       debug(F101,
  8125.                             "x25getmsg fcntl returned an error\n", "", errno);
  8126.                       /* netclos(); */
  8127.                       rc = -1;
  8128.                       break;
  8129.                   } else {
  8130.                       /* loop again into a blocking getmsg() */
  8131.                       continue;
  8132.                   }
  8133.             } else {
  8134.                 /* no data to get in non-blocking mode - return empty handed */
  8135.                 perror( "x25getmsg(): getmsg failed" );
  8136.                 debug(F101,"x25getmsg getmsg returned an error\n", "", errno);
  8137.                 rc = -1;
  8138.                 break;
  8139.             }
  8140.         } else if (more & MORECTL) {
  8141.             /* panic - the control information was larger than the
  8142.              * maximum control buffer size!
  8143.              */
  8144.             /* riehm: close connection? */
  8145. #ifdef DEBUG
  8146.             printf("x25getmsg(): received partial control packet - panic\n");
  8147. #endif /* DEBUG */
  8148.             debug( F101, "x25getmsg getmsg bad control block\n", "", errno);
  8149.             rc = -1;
  8150.             break;
  8151.         }
  8152.  
  8153.         if (result = (N_npi_ctl_t *)control) {
  8154.             packet_type = result->bind_ack.PRIM_type;
  8155.             if (packet_type != N_OK_ACK) {
  8156.                 x25lastmsg = packet_type;
  8157.             }
  8158.         }
  8159. #ifdef DEBUG
  8160.         /* printf( "kermit: x25getmsg(): getting " ); */
  8161.         if (get_ctl->len > 0) {
  8162.             x25dump_prim(result);
  8163.         }
  8164.         debug(F110,
  8165.               "x25getmsg got packet ",
  8166.               x25prim( result->bind_ack.PRIM_type ),
  8167.               0
  8168.               );
  8169. #endif /* DEBUG */
  8170.  
  8171.         if (get_ctl->len >= (int)sizeof(result->bind_ack.PRIM_type)) {
  8172.             /* not as pretty as a switch(), but switch can't handle
  8173.              * runtime variable values :-(
  8174.              */
  8175.             if (packet_type == expected ) {
  8176.                 /* got what we wanted, special case for DATA_IND
  8177.                  * packets though */
  8178.                 /* riehm: check Q-bit ? */
  8179. #ifdef DEBUG
  8180.                 printf("x25getmsg(): got expected packet\nrc is %d\n", rc);
  8181. #endif /* DEBUG */
  8182.                 if (packet_type == N_DATA_IND ) {
  8183.                     /* data received. May be incomplete, even though
  8184.                      * getmsg returned OK
  8185.                      */
  8186.                     if (result->data_ind.DATA_xfer_flags & N_MORE_DATA_FLAG)
  8187.                         more |= MOREDATA;
  8188.                     if (result->data_ind.DATA_xfer_flags & N_RC_FLAG)
  8189.                         printf( "x25getmsg(): data packet wants ack\n" );
  8190.                 }
  8191.             } else if( packet_type == N_DISCON_IND) {
  8192.                 printf( "X25 diconnected\n" );
  8193.                 /* riehm: need to acknowledge a disconnection? */
  8194.                 x25clear();
  8195.                 /* x25unbind( ttyfd ); */
  8196.                 rc = -1;
  8197.             } else if( packet_type == N_ERROR_ACK) {
  8198.                 errno = result->error_ack.UNIX_error;
  8199.                 perror( "X25 error received" );
  8200.                 rc = -1;
  8201.             } else {
  8202.                 printf("x25getmsg(): failed %s\n", x25err(packet_type));
  8203.                 rc = -1;
  8204.             }
  8205.         }
  8206. #ifdef COMMENT
  8207.         else {
  8208.             /* Panic - no control data */
  8209.             printf( "kermit: x25getmsg(): no control data with packet\n" );
  8210.             rc = -1;
  8211.         }
  8212. #endif /* COMMENT */
  8213.  
  8214.         if (get_data && (get_data->len >= 0)) {
  8215.             get_data->buf += get_data->len;
  8216.             get_data->maxlen -= get_data->len;
  8217.         }
  8218.     } while ((rc == 0)
  8219.              && (get_data && (get_data->maxlen > 0))
  8220.              && (more & MOREDATA)
  8221.              );
  8222.  
  8223.     /* return the file status to its original value, unless its still
  8224.      * set to -1, or one of the fcntl's failed */
  8225.     if ((file_status >= 0) && fcntl(fd, F_SETFL, file_status) < 0)
  8226.         rc = -1;
  8227.  
  8228.     /*
  8229.      * Verify that we received an expected primitive
  8230.      * there is apparantly an error case where the primitive is set
  8231.      * correctly, but there is not enough data in the control structure
  8232.      */
  8233.     if ((packet_type != expected) && (get_ctl->len >= ctl_size) ) {
  8234.         fprintf(stderr,
  8235.                 "x25getmsg(): %s NOT received. Primitive received was %s\n",
  8236.                 x25prim( expected ), x25prim( packet_type ));
  8237.         debug(F110, "x25getmsg got an unexpected packet ",
  8238.               x25prim(packet_type),
  8239.               0
  8240.               );
  8241.         rc = -1;
  8242.     }
  8243.  
  8244.     if (rc == 0) {
  8245.         if (get_data && ( get_data->len >= 0)) {
  8246.             rc = get_data->len;
  8247.         }
  8248.     }
  8249.  
  8250.     if (get_ctl)  { free(get_ctl); get_ctl = NULL; }
  8251.     if (get_data) { free(get_data); get_data = NULL; }
  8252.  
  8253. #ifdef COMMENT
  8254. #ifdef DEBUG
  8255.     printf( "kermit x25getmsg(): returning %d\n", rc );
  8256. #endif /* DEBUG */
  8257. #endif /* COMMENT */
  8258.     debug(F110, "x25getmsg returning packet ", x25prim( packet_type ), 0);
  8259.  
  8260. #ifdef TRACE
  8261.     printf( "TRACE: leaving x25getmsg\n" );
  8262. #endif /* TRACE */
  8263.     return(rc);
  8264. }
  8265.  
  8266. /*****************************************************************************
  8267.  * Function: x25putmsg()
  8268.  *
  8269.  * Description:
  8270.  *      send a message to a X25 STREAM
  8271.  *
  8272.  * Parameters:
  8273.  *      fd              - file descriptor to x25 device (opened)
  8274.  *      control         - control buffer (pre-allocated)
  8275.  *      data            - data buffer (pre-allocated)
  8276.  *      data_len        - length of data to be transmitted
  8277.  *      put_flags       - flags for putmsg()
  8278.  *
  8279.  * Return Value:
  8280.  *      >= 0    number of bytes transmitted
  8281.  *      -1      error
  8282.  */
  8283. int
  8284. x25putmsg(fd, control, data, data_len, put_flags)
  8285.     int                 fd;             /* X25 device (opened) */
  8286.     N_npi_ctl_t         *control;       /* control buffer (pre-allocated) */
  8287.     N_npi_data_t        *data;          /* data buffer (pre-allocated) */
  8288.     int                 data_len;       /* length of data (not the size of
  8289.                                            the buffer) */
  8290.     int                 *put_flags;     /* putmsg() flags */
  8291. /* x25putmsg */ {
  8292.     int                 rc = 0;         /* return code */
  8293.     ulong               type;           /* primitive type */
  8294.     struct strbuf       *put_ctl = NULL; /* putmsg control */
  8295.     struct strbuf       *put_data = NULL; /* putmsg data */
  8296.  
  8297. #ifdef TRACE
  8298.     printf( "TRACE: entering x25putmsg\n" );
  8299. #endif /* TRACE */
  8300.  
  8301. #ifdef DEBUG
  8302.     printf( "kermit: x25putmsg(): putting " );
  8303.     x25dump_prim( control );
  8304.     printf( "\tdata:\t\t" );
  8305.     x25dump_data( data, 0, data_len );
  8306.     debug(F110,"x25putmsg: putting packet ",x25prim(control->PRIM_type),0);
  8307. #endif /* DEBUG */
  8308.  
  8309.     if (control) {
  8310.         put_ctl = (struct strbuf *)malloc( sizeof( struct strbuf ) );
  8311.         if (put_ctl == NULL) {
  8312.             perror("kermit x25putmsg(): put_ctl malloc failed\n");
  8313.             return(-1);
  8314.         }
  8315.         put_ctl->maxlen = 0;                    /* unused by putmsg */
  8316.         put_ctl->len = NPI_MAX_CTL;
  8317.         put_ctl->buf = (char *)control;
  8318.     }
  8319.     if (data && ( data_len > 0)) {
  8320.         put_data = (struct strbuf *)malloc( sizeof( struct strbuf ) );
  8321.         if( put_data == NULL) {
  8322.             perror("kermit x25putmsg(): put_data malloc failed\n");
  8323.             return(-1);
  8324.         }
  8325.         put_data->maxlen = 0;                   /* unused by putmsg */
  8326.         put_data->len = data_len;
  8327.         put_data->buf = (char *)data;
  8328.     }
  8329.  
  8330.     errno = 0;
  8331.     rc = putmsg (fd, put_ctl, put_data, 0);
  8332.     if (rc < 0) {
  8333.         printf("x25putmsg(): couldn't put %s\n",x25prim(control->PRIM_type));
  8334.         perror("kermit: x25putmsg(): putmsg failed");
  8335.         return(-1);
  8336.     }
  8337.  
  8338.     /* riehm: this should perhaps be discounted! */
  8339.     x25lastmsg = control->PRIM_type;
  8340.  
  8341. #ifdef COMMENT
  8342. #ifdef DEBUG
  8343.     printf( "kermit debug: x25putmsg() returning %d\n", data_len );
  8344. #endif /* DEBUG */
  8345. #endif /* COMMENT */
  8346.     debug( F101, "x25putmsg block size put ", "", data_len);
  8347.  
  8348. #ifdef TRACE
  8349.     printf( "TRACE: leaving x25putmsg\n" );
  8350. #endif /* TRACE */
  8351.  
  8352.     return( data_len );
  8353. }
  8354.  
  8355. /*****************************************************************************
  8356. * Function: x25bind
  8357. * Description:  The bind submitted to NPI provides the information required
  8358. *               by the packet layer for it to listen for suitable incoming
  8359. *               calls.
  8360. *
  8361. * WARNING:
  8362. *
  8363. * This routine needs to be called in a completely different manner for
  8364. * the client and server side. When starting a client, the
  8365. * num_waiting_calls and CUD information should all be set to 0! The
  8366. * client's CUD must be inserted in the CONN_REQ data block.
  8367. * When starting a server, the CUD must be set to a CUD pattern, and
  8368. * the number of waiting calls should be set to a number other than 0.
  8369. * (num waiting calls is the number of incomming calls which are to be
  8370. * put on hold while the server is servicing another client.)
  8371. *
  8372. * Who invented this crap?
  8373. *
  8374. * Parameters:
  8375. *       fd              - X25 device (opened)
  8376. *       addr            - local address
  8377. *       cud             - User Data (null terminated)
  8378. *       cud_len         - User Data length
  8379. *       num_waiting_calls - number of outstanding calls allowed on this stream
  8380. *       line            - logical port number (1)
  8381. *       flags           - 0, DEFAULT_LISTENER or TOKEN_REQUEST
  8382. *
  8383. * Return Value:
  8384. *       if binding is successful, 0 is returned for a client, and a token is
  8385. *       returned for a server
  8386. *
  8387. * Return code: 0 if successful
  8388. *              -1 if unsuccessful
  8389. *****************************************************************************/
  8390.  
  8391. ulong
  8392. x25bind(fd, addr, cud, cud_len, num_waiting_calls, line, bind_flags)
  8393.     int fd;                             /* X25 device (opened) */
  8394.     char * addr;                        /* local address */
  8395.     char * cud;                         /* Call User Data (null terminated) */
  8396.     int cud_len;                        /* User Data length */
  8397.     int num_waiting_calls;              /* Outstanding calls allowed */
  8398.     int line;                           /* logical port number */
  8399.     ulong bind_flags;           /* 0, DEFAULT_LISTENER or TOKEN_REQUEST */
  8400. /* x25bind */ {
  8401.     ulong rc;                           /* return code */
  8402.     int get_flags;                      /* priority flag passed to getmsg */
  8403.     int put_flags = 0;                  /* output flags for putmsg, always 0 */
  8404.     ulong type;                         /* primitive type */
  8405.     N_bind_req_t *bind_req;             /* pointer to N_BIND_REQ primitive */
  8406.     N_bind_ack_t *bind_ack;             /* pointer to N_BIND_ACK primitive */
  8407.     char *addtl_info;                   /* pointer to info in addition to
  8408.                                          * the N_BIND_REQ primitive that is
  8409.                                          * passed in the control structure
  8410.                                          * to putmsg */
  8411.     int addr_len = 0;                   /* length of address string */
  8412.     ulong bind_req_t_size;              /* for debugging only */
  8413.  
  8414. #ifdef TRACE
  8415.     printf("TRACE: entering x25bind\n" );
  8416. #endif /* TRACE */
  8417.  
  8418. #ifdef DEBUG
  8419.     printf("TRACE: x25bind( %d, %s, %s, %d, %d )\n",
  8420.            fd, addr, cud, line, bind_flags
  8421.            );
  8422. #endif /* DEBUG */
  8423.  
  8424.     /*
  8425.      * Allocate  and zero out space to hold the control portion of the
  8426.      * message passed to putmsg. This will contain the N_BIND_REQ
  8427.      * primitive and any additional info required for that.
  8428.      *
  8429.      * Note: allocated space is the size of the union typedef
  8430.      * N_npi_ctl_t to allow the use fo the generic x25putmsg routine.
  8431.      */
  8432.     bind_req = (N_bind_req_t *) malloc(sizeof( N_npi_ctl_t));
  8433.     if (bind_req == NULL) {
  8434.         perror("kermit: x25bind(): bind_req malloc failed");
  8435.         debug(F100, "x25bind bind_req malloc failed", "", 0);
  8436.         return(-1);
  8437.     }
  8438.     bzero((char *)bind_req, sizeof(N_npi_ctl_t));
  8439.  
  8440.     /* Build the Bind Request Primitive */
  8441.     bind_req->PRIM_type = (ulong) N_BIND_REQ;
  8442.  
  8443.     /* Note that the address length is n+2 and NOT n. Two bytes MUST preceed
  8444.      * the actual address in an N_BIND_REQ. The first byte contains the
  8445.      * line number being used with this address, and the second byte is the
  8446.      * X.121 address prefix, which must be zero.
  8447.      */
  8448.     addr_len = strlen(addr);
  8449.     bind_req->ADDR_length = (ulong) (addr_len + 2);
  8450.     bind_req->ADDR_offset = (ulong)(sizeof(N_bind_req_t));
  8451.     bind_req->CONIND_number = (ulong)num_waiting_calls; /* server only */
  8452.     bind_req->BIND_flags = (ulong) bind_flags; /* 0 in client */
  8453.     bind_req->PROTOID_length = (ulong) cud_len; /* 0 in client */
  8454.     if (cud_len == 0) {
  8455.         bind_req->PROTOID_offset = (ulong) 0;
  8456.     } else {
  8457.         /* need to remember the trailing NULL in the address - not
  8458.          * counted in the address length
  8459.          */
  8460.         bind_req->PROTOID_offset
  8461.           = (ulong) (sizeof(N_bind_req_t) + bind_req->ADDR_length);
  8462.     }
  8463.  
  8464.     /*
  8465.      * Now fill in the additional information required with this primitive
  8466.      * (address and protocol information (Call User Data))
  8467.      */
  8468.     addtl_info = (char *) ((void *)bind_req + bind_req->ADDR_offset);
  8469.     /*
  8470.      * The bitwise "&" ensures that the line number is only one byte long
  8471.      */
  8472.     *addtl_info++ = (char) line & 0xff;
  8473.     *addtl_info++ = (char) 0; /* X.121 format */
  8474.     bcopy( addr, addtl_info, addr_len ); /* include trailing null */
  8475.     addtl_info += addr_len;
  8476.     if (cud_len > 0)
  8477.       bcopy( cud, addtl_info, cud_len );
  8478.     /*
  8479.      * Call putmsg() to put the bind request message on the stream
  8480.      */
  8481.     if (x25putmsg(fd,
  8482.                   (N_npi_ctl_t*)bind_req,
  8483.                   (N_npi_data_t *)NULL,
  8484.                   0,
  8485.                   &put_flags
  8486.                   ) < 0) {
  8487.         printf( "kermit: x25bind(): x25putmsg failed\n" );
  8488.         return(-1);
  8489.     }
  8490.  
  8491.     /*
  8492.      * Allocate and zero out space for the N_BIND_ACK primitive
  8493.      */
  8494.     bind_ack = (N_bind_ack_t *) malloc(sizeof(N_npi_ctl_t));
  8495.     if (bind_ack == NULL){
  8496.         perror("kermit: x25bind(): bind_ack malloc failed");
  8497.         return(-1);
  8498.     }
  8499.     bzero(bind_ack, sizeof(N_npi_ctl_t));
  8500.     /*
  8501.      * Initialize the control structure and flag variable sent to getmsg
  8502.      */
  8503.     get_flags=0;
  8504.  
  8505.     /* get the ACK for the bind */
  8506. #ifdef DEBUG
  8507.     printf( "kermit: x25bind() trying to get a BIND_ACK\n" );
  8508. #endif /* DEBUG */
  8509.     rc = (ulong)x25getmsg( fd, (N_npi_ctl_t*)bind_ack,
  8510.             (int)sizeof( N_bind_ack_t ), (N_npi_data_t*)NULL, 0, &get_flags,
  8511.             N_BIND_ACK );
  8512.  
  8513.     /* turn quantitive return code into a qualitative one */
  8514.     if (rc > 0) rc = 0;
  8515.  
  8516.     /* if all went well, get the token from the acknowledgement packet */
  8517.     if ((bind_flags & TOKEN_REQUEST ) && ( rc >= 0)) {
  8518.         rc = bind_ack->TOKEN_value;
  8519.     }
  8520.  
  8521.     /* free up the memory we allocated earlier */
  8522.     free(bind_req);
  8523.     free(bind_ack);
  8524.  
  8525. #ifdef TRACE
  8526.     printf( "TRACE: leaving x25bind\n" );
  8527. #endif /* TRACE */
  8528.  
  8529.     return( rc );
  8530. }
  8531.  
  8532. /*****************************************************************************
  8533. * Function: x25call
  8534. * Description:  This routine builds and sends an N_CONN_REQ primitive, then
  8535. *               checks for an N_CONN_CON primitive in return.
  8536. *
  8537. * Parameters:
  8538. * fd    - file descriptor of stream
  8539. * caddr - called address (remote address)
  8540. *
  8541. * Functions Referenced:
  8542. * malloc()
  8543. * bzero()
  8544. * getmsg()
  8545. * putmsg()
  8546. *
  8547. * Return code:
  8548. * 0 - if successful
  8549. * -1 if not successful
  8550. *****************************************************************************/
  8551. int
  8552. x25call(fd, remote_nua, cud)
  8553.     int fd;                             /* X25 device (opened) */
  8554.     char * remote_nua;                  /* remote address to call */
  8555.     char * cud;                         /* call user data */
  8556. /* x25call */ {
  8557.     int rc;                             /* return code */
  8558.     int flags;                          /* Connection flags */
  8559.     int get_flags;                      /* priority flags for getmsg */
  8560.     ulong type;                         /* primitive type */
  8561.     N_conn_req_t *connreq_ctl;          /* pointer to N_CONN_REQ primitive */
  8562.     N_npi_data_t *connreq_data;         /* pointer to N_CONN_REQ data (CUD) */
  8563.     int connreq_data_len;               /* length of filled data buffer */
  8564.     N_conn_con_t *conncon_ctl;          /* pointer to N_CONN_CON primitive */
  8565.     N_npi_data_t *conncon_data;         /* pointer to any data associated with
  8566.                                          * the N_CONN_CON primitive */
  8567.     char *addtl_info;                   /* pointer to additional info needed
  8568.                                          * for N_CONN_REQ primitive */
  8569.     int addr_len;                       /* length of address */
  8570.  
  8571. #ifdef TRACE
  8572.     printf( "TRACE: entering x25call\n" );
  8573. #endif /* TRACE */
  8574.  
  8575. #ifdef DEBUG
  8576.     printf( "x25call( %d, %s )\n", fd, remote_nua );
  8577.     printf( "connecting to %s on fd %d\n", remote_nua, fd );
  8578. #endif /* DEBUG */
  8579.  
  8580.     /*
  8581.      * Allocate and zero out space for the N_CONN_REQ primitive
  8582.      * use the size of the generic NPI primitive control buffer
  8583.      */
  8584.     connreq_ctl  = (N_conn_req_t *) malloc(sizeof(N_npi_ctl_t));
  8585.     if (connreq_ctl == NULL){
  8586.         perror("kermit: x25call(): connreq_ctl malloc failed");
  8587.         return(-1);
  8588.     }
  8589.     bzero(connreq_ctl,sizeof(N_npi_ctl_t));
  8590.     /*
  8591.      * Build the Connection Request Primitive
  8592.      */
  8593.     flags = 0;
  8594.     connreq_ctl->PRIM_type = (ulong) N_CONN_REQ;
  8595.  
  8596.     /* Note that the address length is nchai+1 and not n+2. The line number
  8597.      * is only passed with the address for the bind. The first byte of
  8598.      * the address for the N_CONN primitives contains the X.121
  8599.      * address prefix, which must be zero. The remaining bytes are the
  8600.      * address itself.
  8601.      */
  8602.     addr_len = strlen( remote_nua );
  8603.     connreq_ctl->DEST_length = (ulong) (addr_len + 1);
  8604.     connreq_ctl->DEST_offset = (ulong) sizeof(N_conn_req_t);
  8605.     /* connreq_ctl->CONN_flags = (ulong)EX_DATA_OPT | REC_CONF_OPT; */
  8606.     connreq_ctl->CONN_flags = (ulong) 0;
  8607.     connreq_ctl->QOS_length = (ulong) 0;        /* unsupported in AIX 4.1 */
  8608.     connreq_ctl->QOS_offset = (ulong) 0;        /* unsupported in AIX 4.1 */
  8609.  
  8610.     addtl_info = (char *) ((void*)connreq_ctl + connreq_ctl->DEST_offset);
  8611.     *addtl_info++ = (char) 0; /* X.121 format */
  8612.     bcopy( remote_nua, addtl_info, addr_len );
  8613.  
  8614.     /*
  8615.      * setup the data buffer for the connection request
  8616.      */
  8617.     connreq_data  = (N_npi_data_t *) malloc(sizeof(N_npi_data_t));
  8618.     if (connreq_data == NULL){
  8619.         perror("kermit: x25call(): connreq_data malloc failed");
  8620.         return(-1);
  8621.     }
  8622.     bzero(connreq_data,sizeof(N_npi_data_t));
  8623.  
  8624.     /* facility selection needs to be put in the front of connreq_data */
  8625.     connreq_data_len = 0;
  8626.     connreq_data_len += x25facilities( (char *)connreq_data );
  8627.     if (cud && *cud) {
  8628.         bcopy(cud,
  8629.               (char *)((char *)connreq_data + connreq_data_len),
  8630.               strlen(cud)
  8631.               );
  8632.         connreq_data_len += strlen( cud );
  8633.         }
  8634.  
  8635.     /*
  8636.      * Call putmsg() to put the connection request message on the stream
  8637.      */
  8638.     rc = x25putmsg( fd, (N_npi_ctl_t*)connreq_ctl, connreq_data,
  8639.             connreq_data_len, &flags );
  8640.     if (rc < 0) {
  8641.         return(-1);
  8642.     }
  8643.  
  8644.     /*
  8645.      * Allocate and zero out space for the N_CONN_CON primitive
  8646.      */
  8647.     if ((conncon_ctl = (N_conn_con_t *) malloc(sizeof(N_npi_ctl_t))) == NULL) {
  8648.         perror("kermit: x25call(): conncon_ctl malloc failed");
  8649.         return(-1);
  8650.     }
  8651.     bzero(conncon_ctl, sizeof(N_npi_ctl_t));
  8652.  
  8653.     /*
  8654.      * Allocate and zero out space for any data associated with N_CONN_CON
  8655.      */
  8656.     if ( (conncon_data = (N_npi_data_t *) malloc(NPI_MAX_DATA)) == NULL) {
  8657.         perror("kermit: x25call(): conncon_data malloc failed");
  8658.         return(-1);
  8659.     }
  8660.     bzero(conncon_data, NPI_MAX_DATA);
  8661.  
  8662.     /* Initialize and build the structures for getmsg */
  8663.     get_flags=0;
  8664.  
  8665.     rc = x25getmsg( fd, (N_npi_ctl_t*)conncon_ctl, (int)sizeof( N_conn_con_t ),
  8666.             conncon_data, NPI_MAX_DATA, &get_flags, N_CONN_CON );
  8667.  
  8668.     /* turn quantitive return code into a qualitative one */
  8669.     if (rc > 0) rc = 0;
  8670.  
  8671.     /* Free the space that we no longer need */
  8672.     if (connreq_ctl) { free(connreq_ctl); connreq_ctl = NULL; }
  8673.     if (conncon_ctl) { free(conncon_ctl); conncon_ctl = NULL; }
  8674.     if (conncon_data) { free(conncon_data); conncon_data = NULL; }
  8675.  
  8676. #ifdef TRACE
  8677.     printf( "TRACE: leaving x25call\n" );
  8678. #endif /* TRACE */
  8679.  
  8680.     return(rc);
  8681. }
  8682.  
  8683. /*****************************************************************************
  8684.  * Function: x25getcall
  8685.  *
  8686.  * Description: This routine checks for an incomming call, verified
  8687.  * that it is a CONNIND (connection indication) message, and then
  8688.  * accepts the call and returns the file descriptor of the new stream
  8689.  *
  8690.  * Parameters:
  8691.  * fd   - file descriptor of listening stream
  8692.  *
  8693.  * Return Codes:
  8694.  * callfd       - file descriptor of connected incomming call.
  8695.  *              - set to -1 if an error occured
  8696.  *
  8697.  *****************************************************************************/
  8698. int
  8699. x25getcall(fd) int fd; {
  8700.     int x25callfd;                      /* fd of incomming call */
  8701.     N_conn_ind_t *connind_ctl;          /* connind controll buffer */
  8702.     N_npi_data_t *connind_data;         /* connind data buffer */
  8703.     int get_flags;                      /* flags for getmsg */
  8704.     ulong flags;                        /* connection flags */
  8705.     int rc;                             /* return code */
  8706.  
  8707.     extern x25addr_t remote_nua;        /* remote X.25 addr global var */
  8708.  
  8709. #ifdef TRACE
  8710.     printf( "TRACE: entering x25getcall\n" );
  8711. #endif /* TRACE */
  8712.  
  8713.     /* allocate space for connection indication buffers */
  8714.     if ((connind_ctl = (N_conn_ind_t *)malloc(sizeof(N_npi_ctl_t))) == NULL) {
  8715.         perror("kermit: x25getcall(): connind_ctl malloc failed");
  8716.         return (-1);
  8717.     }
  8718.     bzero(connind_ctl, sizeof(N_npi_ctl_t));
  8719.  
  8720.     if ((connind_data = (N_npi_data_t *)malloc(NPI_MAX_DATA)) == NULL) {
  8721.         perror("kermit: x25getcall(): connind_data malloc failed");
  8722.         return (-1);
  8723.     }
  8724.     bzero(connind_data, NPI_MAX_DATA);
  8725.  
  8726.     /* initialise control structures */
  8727.     get_flags = 0;
  8728.  
  8729.     /* call getmsg to check for a connection indication */
  8730.     if (x25getmsg(fd,
  8731.                   (N_npi_ctl_t*)connind_ctl,
  8732.                   (int)sizeof(N_conn_ind_t),
  8733.                   connind_data,
  8734.                   NPI_MAX_DATA,
  8735.                   &get_flags,
  8736.                   N_CONN_IND
  8737.                   ) < 0) {
  8738. #ifdef DEBUG
  8739.         printf( "x25getcall(): errno is: %d\n", errno );
  8740. #endif /* DEBUG */
  8741.         perror ("x25getcall(): getmsg failed");
  8742.         return(-1);
  8743.     }
  8744.  
  8745.     /* a connection indication was received
  8746.      * - pull it to bits and answer the call
  8747.      */
  8748.     x25seqno = connind_ctl->SEQ_number;
  8749.     flags = connind_ctl->CONN_flags;
  8750. #ifdef DEBUG
  8751.     printf( "setting remote_nua to a new value due to incomming call\n" );
  8752. #endif /* DEBUG */
  8753.     /*
  8754.      * no guarantee that the address is null terminated, ensure that
  8755.      * after copying that it is (assumption: remote_nua is longer than
  8756.      * the address + 1)
  8757.      */
  8758.     bzero(remote_nua, sizeof(remote_nua));
  8759.     /* note: connind_ctl contains a x121 address, which has a null as
  8760.      * the FIRST character - strip it off!
  8761.      */
  8762.     ckstrncpy(remote_nua,
  8763.             (char*)((char*)connind_ctl + connind_ctl->SRC_offset + 1),
  8764.             connind_ctl->SRC_length - 1
  8765.             );
  8766. #ifdef DEBUG
  8767.     printf( "remote_nua set to new value of %s\n", remote_nua );
  8768. #endif /* DEBUG */
  8769.  
  8770.     /* errors handled by callee */
  8771.     x25callfd = x25accept(x25seqno, flags);
  8772.  
  8773.     /* free the malloc'd buffers */
  8774.     if (connind_ctl) { free(connind_ctl); connind_ctl = NULL; }
  8775.     if (connind_data) { free(connind_data); connind_data = NULL; }
  8776.  
  8777. #ifdef TRACE
  8778.     printf( "TRACE: leaving x25getcall\n" );
  8779. #endif /* TRACE */
  8780.  
  8781.     /* return the file descriptor (or error if < 0) */
  8782.     return( x25callfd );
  8783. }
  8784.  
  8785. /*****************************************************************************
  8786.  * Function: x25accept
  8787.  *
  8788.  * Description: accept an incomming call
  8789.  *              This essentially means opening a new STREAM and sending
  8790.  *              an acknowledge back to the caller.
  8791.  *
  8792.  * Parameters:
  8793.  *      seqno   - sequence number for acknowledgement
  8794.  *      flags   - flags passed to us by the caller
  8795.  *
  8796.  * Return Codes:
  8797.  *      fd      - file descriptor of new STREAM
  8798.  *                set to -1 if an error occured
  8799.  *
  8800.  *****************************************************************************/
  8801. int
  8802. x25accept(seqno,flags)
  8803.     ulong seqno;                        /* connection sequence number */
  8804.     ulong flags;                        /* connection flags */
  8805. /* x25accept */ {
  8806.     int x25callfd;                      /* fd for incomming call */
  8807.     int get_flags;                      /* priority flags for getmsg */
  8808.     int put_flags = 0;                  /* flags for putmsg, always 0 */
  8809.     int addr_len;                       /* length of local address */
  8810.     ulong token;                        /* connection token */
  8811.     N_conn_res_t *conn_res;             /* N_CONN_RES primitive */
  8812.     N_ok_ack_t *ok_ack;                 /* N_OK_ACK primitive */
  8813.     char *addtl_info;                   /* temp pointer */
  8814.     int rc;                             /* temporary return code */
  8815.  
  8816. /* global variables from ckcmai.c */
  8817.     extern int revcall, closgr, cudata;
  8818.     extern char udata[];
  8819.     extern x25addr_t local_nua;         /* local X.25 address */
  8820.     extern char x25name[];              /* x25 device name (sx25a0) */
  8821.     extern char x25dev[];               /* x25 device file /dev/x25pkt */
  8822.     extern int x25port;                 /* logical port to use */
  8823.     ulong bind_flags = 0;               /* flags for binding the X25 stream */
  8824.  
  8825. #ifdef TRACE
  8826.     printf( "TRACE: entering x25accept\n" );
  8827. #endif /* TRACE */
  8828.  
  8829.     /* open a new packet level stream */
  8830.     if ((x25callfd = open(x25dev, O_RDWR)) < 0) {
  8831.         perror ("kermit: x25accept(): X.25 device open error");
  8832.         debug(F101,"x25accept() device open error","",errno);
  8833.         return(-1);
  8834.     }
  8835.  
  8836.     /* push the NPI onto the STREAM */
  8837.     if (ioctl(x25callfd,I_PUSH,"npi") < 0) {
  8838.         perror( "kermit: x25accept(): couldn't push npi on the X25 stream" );
  8839.         debug(F101,"x25accept can't push npi on the X25 stream","",errno);
  8840.         return (-1);
  8841.     }
  8842.  
  8843.     /* bind kermit server to the local X25 address */
  8844.     /* taken from /usr/samples/sx25/npi/npiserver.c (AIX 4) */
  8845.     bind_flags |= TOKEN_REQUEST;
  8846.     token = x25bind(x25callfd,local_nua,(char *)NULL,0,0,x25port,bind_flags);
  8847.     if (token < 0) {
  8848.         printf( "kermit: x25accept(): couldn't bind to local X25 address\n" );
  8849.         netclos();
  8850.         return(-1);
  8851.     }
  8852.  
  8853.     /* allocate connection response primitive */
  8854.     if ((conn_res = (N_conn_res_t *)malloc( NPI_MAX_CTL )) == NULL) {
  8855.         perror("kermit: x25accept(): conn_res malloc failed");
  8856.         return (-1);
  8857.     }
  8858.     bzero((char *)conn_res, NPI_MAX_CTL);
  8859.  
  8860.     /* setup connection response primitive */
  8861.     addr_len = strlen( local_nua );
  8862.     conn_res->PRIM_type = (ulong)N_CONN_RES;
  8863.     conn_res->TOKEN_value = token;
  8864.     /* note address length is n+1 to accomodate the X.121 address prefix */
  8865.     conn_res->RES_length = (ulong)(addr_len + 1);
  8866.     conn_res->RES_offset = (ulong)sizeof( N_conn_res_t );
  8867.     conn_res->SEQ_number = seqno;
  8868.     conn_res->CONN_flags = 0;
  8869.     conn_res->QOS_length = 0;           /* unsupported - must be 0 (!?) */
  8870.     conn_res->QOS_offset = 0;
  8871.  
  8872.     addtl_info = (char *)((char *)conn_res + conn_res->RES_offset);
  8873.     *addtl_info++ = (char)0;    /* X.121 address prefix */
  8874.     bcopy( local_nua, addtl_info, addr_len );
  8875.  
  8876.     /*
  8877.      * send off the connect response
  8878.      */
  8879.     if (x25putmsg(x25callfd,
  8880.                   (N_npi_ctl_t*)conn_res,
  8881.                   (N_npi_data_t *)NULL,
  8882.                   0,
  8883.                   &put_flags
  8884.                   ) < 0 ) {
  8885.         perror("kermit: x25accept(): putmsg connect response failed");
  8886.         return(-1);
  8887.     }
  8888.  
  8889.     /*
  8890.      * Allocate and zero out space for the OK_ACK primitive
  8891.      */
  8892.     if ((ok_ack = (N_ok_ack_t *) malloc(sizeof(N_npi_ctl_t))) == NULL) {
  8893.         perror("kermit: x25call(): ok_ack malloc failed");
  8894.         return(-1);
  8895.     }
  8896.     bzero(ok_ack, sizeof(N_npi_ctl_t));
  8897.  
  8898.     /* Initialize and build the structures for getmsg */
  8899.     get_flags=0;
  8900.  
  8901.     rc = (int)x25getmsg(x25callfd,
  8902.                         (N_npi_ctl_t*)ok_ack,
  8903.                         (int)sizeof(N_ok_ack_t),
  8904.                         (N_npi_data_t*)NULL,
  8905.                         0,
  8906.                         &get_flags,
  8907.                         N_OK_ACK
  8908.                         );
  8909.     if (rc == 0) {
  8910.         /* sequence number is only for disconnecting when not connected !? */
  8911.         x25seqno = 0;
  8912.     }
  8913.  
  8914.     /* free up malloc'ed buffer space */
  8915.     if (conn_res) { free(conn_res); conn_res = NULL; }
  8916.     if (ok_ack) { free(ok_ack); ok_ack = NULL; }
  8917.  
  8918. #ifdef TRACE
  8919.     printf( "TRACE: leaving x25accept\n" );
  8920. #endif /* TRACE */
  8921.  
  8922.     return( ( rc >= 0 ) ? x25callfd : -1 );
  8923. }
  8924.  
  8925. /*****************************************************************************
  8926.  * Function: x25unbind
  8927.  *
  8928.  * Description:  This subroutine builds and sends an unbind request and gets
  8929.  * the acknowledgement for it.
  8930.  *
  8931.  * Parameters:
  8932.  * fd - File descriptor of the stream
  8933.  *
  8934.  * Functions Referenced:
  8935.  * getmsg()
  8936.  * putmsg()
  8937.  * malloc()
  8938.  * bzero()
  8939.  *
  8940.  * Return code:
  8941.  * 0 - if successful
  8942.  * -1 - if not successful
  8943.  *****************************************************************************/
  8944. int
  8945. x25unbind(fd) int fd; {                 /* X25 device (opened) */
  8946.     int rc;                             /* return code */
  8947.     int flags;                          /* bind flags */
  8948.     int get_flags;                      /* priority flag for getmsg */
  8949.     ulong type;                         /* primitive type */
  8950.     N_unbind_req_t *unbind_req;         /* pointer to N_UNBIND_REQ */
  8951.     N_ok_ack_t *ok_ack;                 /* pointer to N_OK_ACK */
  8952.  
  8953. #ifdef TRACE
  8954.     printf( "TRACE: entering x25unbind\n" );
  8955. #endif /* TRACE */
  8956.  
  8957. #ifdef DEBUG
  8958.     /* printf( "x25unbind( %d )\n", fd ); */
  8959. #endif /* DEBUG */
  8960.     debug(F101,"x25unbind closing x25 connection #","",fd);
  8961.  
  8962.     /* Allocate and zero out space to hold the N_UNBIND_REQ primitive */
  8963.     unbind_req = (N_unbind_req_t *) malloc(sizeof(N_npi_ctl_t));
  8964.     if (unbind_req == NULL) {
  8965.         perror("kermit: x25unbind(): unbind_req malloc failed");
  8966.         return(-1);
  8967.     }
  8968.     bzero(unbind_req, sizeof(N_npi_ctl_t));
  8969.  
  8970.     /*
  8971.      * Build the Unbind Request Primitive
  8972.      */
  8973.     flags = 0;
  8974.     unbind_req->PRIM_type = (ulong) N_UNBIND_REQ;
  8975.  
  8976.     /*
  8977.      * Call putmsg() to put the bind request message on the stream
  8978.      */
  8979.     if (x25putmsg(fd,
  8980.                   (N_npi_ctl_t*)unbind_req,
  8981.                   (N_npi_data_t *)NULL,
  8982.                   0,
  8983.                   &flags
  8984.                   ) < 0) {
  8985.         perror ("kermit: x25unbind(): putmsg failed");
  8986.         return(-1);
  8987.     }
  8988.  
  8989.     /* Allocate and Zero out space for the N_OK_ACK primitive */
  8990.     ok_ack = (N_ok_ack_t *) malloc(sizeof(N_npi_ctl_t));
  8991.     if (ok_ack == NULL) {
  8992.         perror("kermit x25unbind(): ok_ack malloc failed\n");
  8993.         return(-1);
  8994.     }
  8995.     bzero(ok_ack, sizeof(N_npi_ctl_t));
  8996.  
  8997.     /* Initialize and build the control structure for getmsg */
  8998.     get_flags=0;
  8999.  
  9000.     /* Call getmsg() to check for an acknowledgement */
  9001.     rc = x25getmsg(fd,
  9002.                    (N_npi_ctl_t*)ok_ack,
  9003.                    (int)sizeof(N_ok_ack_t),
  9004.                    (N_npi_data_t*)NULL,
  9005.                    0,
  9006.                    &get_flags,
  9007.                    N_OK_ACK
  9008.                    );
  9009.     if (rc < 0) {
  9010.         perror ("kermit: x25unbind: getmsg failed");
  9011.         return(-1);
  9012.     }
  9013.  
  9014.     /* Free up the space that we no longer need */
  9015.     if (unbind_req) { free(unbind_req); unbind_req = NULL; }
  9016.     if (ok_ack) { free(ok_ack); ok_ack = NULL; }
  9017.  
  9018. #ifdef TRACE
  9019.     printf( "TRACE: leaving x25unbind\n" );
  9020. #endif /* TRACE */
  9021.  
  9022.     return(0);
  9023. }
  9024.  
  9025. /*****************************************************************************
  9026.  * Function: x25xin
  9027.  *
  9028.  * Description:
  9029.  *      Read n characters from X.25 circuit into buf (AIX only)
  9030.  *
  9031.  * Parameters:
  9032.  *      data_buf_len    maximum size of data buffer
  9033.  *      data_buf        pointer to pre-allocated buffer space
  9034.  *
  9035.  * Return Value:
  9036.  *      the number of characters actually read
  9037.  */
  9038. int
  9039. x25xin(data_buf_len,data_buf) int data_buf_len; CHAR *data_buf; {
  9040.     struct strbuf getmsg_ctl;           /* streams control structure */
  9041.     struct strbuf getmsg_data;          /* streams data structure */
  9042.     int rc = 0;                         /* return code */
  9043.     int getmsg_flags;                   /* packet priority flags */
  9044.     char * ctl_buf;                     /* npi control buffer */
  9045.     N_npi_ctl_t * result;               /* pointer to simplify switch() */
  9046.  
  9047. #ifdef TRACE
  9048.     printf( "TRACE: entering x25xin\n" );
  9049. #endif /* TRACE */
  9050.  
  9051.     /* ensure that no maximum's are overridden */
  9052.     data_buf_len = (NPI_MAX_DATA < data_buf_len) ? NPI_MAX_DATA : data_buf_len;
  9053.  
  9054.     /* allocate space for packet control info */
  9055.     if ((ctl_buf = (char *)malloc(NPI_MAX_CTL)) == NULL) {
  9056.         perror( "kermit: x25xin(): ctl_buf malloc" );
  9057.         return(-1);
  9058.     }
  9059. #ifdef COMMENT
  9060.     /* riehm: need zeroed buffer for getmsg? */
  9061.     bzero( ctl_buf, NPI_MAX_CTL );
  9062.     /* clear data buffer */
  9063.     bzero( data_buf, data_buf_len );
  9064. #endif /* COMMENT */
  9065.  
  9066.     getmsg_flags = 0;                   /* get the first packet available */
  9067.  
  9068.     rc = x25getmsg(ttyfd,
  9069.                    ctl_buf,
  9070.                    NPI_MAX_CTL,
  9071.                    data_buf,
  9072.                    data_buf_len,
  9073.                    &getmsg_flags,
  9074.                    N_DATA_IND
  9075.                    );
  9076. #ifdef COMMENT
  9077. #ifdef DEBUG
  9078.     if (rc >= 0) {
  9079.         printf( "kermit: x25xin(): got " );
  9080.         x25dump_data( data_buf, 0, rc );
  9081.     } else {
  9082.         printf( "x25xin(): attempt to get data resulted in an error\n" );
  9083.     }
  9084. #endif /* DEBUG */
  9085. #endif /* COMMENT */
  9086.  
  9087.     /* free buffers */
  9088.     if (ctl_buf) { free(ctl_buf); ctl_buf = NULL; }
  9089.  
  9090. #ifdef TRACE
  9091.     printf( "TRACE: leaving x25xi\n" );
  9092. #endif /* TRACE */
  9093.  
  9094.     return(rc);
  9095. }
  9096.  
  9097. /*****************************************************************************
  9098.  * Function: x25write
  9099.  *
  9100.  * Description:
  9101.  *      write a block of characters to the X25 STREAM (AIX)
  9102.  *
  9103.  * Parameters:
  9104.  *      fd              file descriptor to write to
  9105.  *      databuf         buffer containing data to write
  9106.  *      databufsize             size of the buffer to write
  9107.  *
  9108.  * Return Value:
  9109.  *      size            the number of bytes actually transmitted
  9110.  */
  9111. int
  9112. x25write(fd, databuf, databufsize)
  9113.     int         fd;                  /* X25 STREAMS file descriptor (ttyfd) */
  9114.     char        *databuf;               /* buffer to write */
  9115.     int         databufsize;            /* buffer size */
  9116. /* x25write */ {
  9117.     N_data_req_t *data_req_ctl;
  9118.     int rc;                             /* return code (size transmitted) */
  9119.     int write_flags = 0;                /* always 0 !? */
  9120.  
  9121. #ifdef TRACE
  9122.     printf( "TRACE: entering x25write\n" );
  9123. #endif /* TRACE */
  9124.  
  9125.     if ((data_req_ctl = (N_data_req_t *)malloc(NPI_MAX_CTL) ) == NULL) {
  9126.         perror( "kermit: x25write(): data_req_ctl malloc" );
  9127.         return(-1);
  9128.     }
  9129.     data_req_ctl->PRIM_type = N_DATA_REQ;
  9130.     data_req_ctl->DATA_xfer_flags = 0;
  9131.  
  9132.     /* riehm: possible extension
  9133.      * possibly need to think about splitting up the data buffer
  9134.      * into multiple parts if databufsize > NPI_MAX_DATA
  9135.      */
  9136.  
  9137. #ifdef COMMENT
  9138. #ifdef DEBUG
  9139.     printf( "kermit: x25write(): writing data to x25 stream\n" );
  9140.     printf( "\tdata:\t" );
  9141.     x25dump_data(databuf, 0, databufsize);
  9142. #endif /* DEBUG */
  9143. #endif /* COMMENT */
  9144.     rc = x25putmsg(fd,
  9145.                    (N_npi_ctl_t*)data_req_ctl,
  9146.                    (N_npi_data_t*)databuf,
  9147.                    databufsize,
  9148.                    &write_flags
  9149.                    );
  9150.     if (data_req) { free(data_req_ctl);  data_req = NULL; }
  9151.  
  9152. #ifdef TRACE
  9153.     printf( "TRACE: leaving x25write\n" );
  9154. #endif /* TRACE */
  9155.  
  9156.     return(rc);
  9157. }
  9158.  
  9159. /*****************************************************************************
  9160.  * Function: x25local_nua
  9161.  *
  9162.  * Description:
  9163.  *      This routine is only interesting for IBM computers. In order
  9164.  *      to set up a connection (see x25bind()) you need to know the
  9165.  *      local NUA (x25 address). Unfortunately, you need all this code
  9166.  *      to find that out, I just hope this works for everyone else!
  9167.  *
  9168.  * Parameters:
  9169.  *      a pre-allocated character buffer, long enough to hold an X.25 address
  9170.  *      and the tailing null.
  9171.  *
  9172.  * Return Value:
  9173.  *      the length of the address string.
  9174.  *      0 = error
  9175.  */
  9176. int
  9177. x25local_nua(char *buf) {
  9178.     struct CuAt *response;      /* structure to fill with info from ODM */
  9179.     CLASS_SYMBOL retClass;      /* ODM class */
  9180.     char query[64];             /* odm database query */
  9181.     int rc = 0;                 /* return value (length of local NUA) */
  9182.     extern char x25name[];      /* x25 device name (sx25a0) */
  9183.  
  9184. #ifdef TRACE
  9185.     printf( "TRACE: entering x25local_nua\n" );
  9186. #endif /* TRACE */
  9187.  
  9188.     /* set up query string */
  9189.     if (x25name[0] == '\0') {
  9190. #ifdef DEBUG
  9191.         printf( "kermit: x25local_nua(): No x25 device set, trying sx25a0\n" );
  9192. #endif /* DEBUG */
  9193.         strcpy( x25name, "sx25a0" );
  9194.     }
  9195.     ckmakmsg(query, sizeof(query), "name like ",x25name,
  9196.              " and attribute like local_nua");
  9197.  
  9198.     /* initialise ODM database */
  9199.     odmerrno = 0;
  9200.     if (odm_initialize() == -1) {
  9201.         printf( "x25local_nua(): can't initialize ODM database");
  9202.         switch (odmerrno) {
  9203.           case ODMI_INVALID_PATH:
  9204.             printf( "invalid path\n" );
  9205.             break;
  9206.           case ODMI_MALLOC_ERR:
  9207.             printf( "malloc failed\n" );
  9208.             break;
  9209.           default:
  9210.             printf( "unknown error %d\nPlease call IBM\n", odmerrno );
  9211.         }
  9212.         return(rc);
  9213.     }
  9214.  
  9215.     /* open the CuAt class */
  9216.     retClass = odm_open_class(CuAt_CLASS);
  9217.     if (((int)retClass) == -1) {
  9218.         printf( "kermit: x25local_nua(): can't open CuAt class in odm. " );
  9219.         switch (odmerrno) {
  9220.           case ODMI_CLASS_DNE:
  9221.             printf( "CuAt class doesn't exist\n" );
  9222.             break;
  9223.           case ODMI_CLASS_PERMS:
  9224.             printf( "permission to CuAt class file denied\n" );
  9225.             break;
  9226.           case ODMI_MAGICNO_ERR:
  9227.             printf( "CuAt is an invalid ODM object class\n" );
  9228.             break;
  9229.           case ODMI_OPEN_ERR:
  9230.             printf( "cannot open CuAt class - and don't know why!\n" );
  9231.             break;
  9232.           case ODMI_INVALID_PATH:
  9233.             printf( "invalid path\n" );
  9234.             break;
  9235.           case ODMI_TOOMANYCLASSES:
  9236.             printf( "too many object classes have been opened\n" );
  9237.             break;
  9238.           default:
  9239.             printf( "unknown error %d\nPlease call IBM\n", odmerrno );
  9240.         }
  9241.         return(rc);
  9242.     }
  9243.  
  9244. #ifdef DEBUG
  9245.     printf("retClass= %d\n", retClass);
  9246. #endif /* DEBUG */
  9247.  
  9248.     response = (struct CuAt *)odm_get_first( retClass, query, NULL );
  9249.     if (((int)response) == -1) {
  9250.         printf( "kermit: x25local_nua(): odm query failed " );
  9251.         switch (odmerrno) {
  9252.           case ODMI_BAD_CRIT:           /* Programming error */
  9253.             printf( "bad search criteria\n" );
  9254.             break;
  9255.           case ODMI_CLASS_DNE:
  9256.             printf( "CuAt class doesn't exist\n" );
  9257.             break;
  9258.           case ODMI_CLASS_PERMS:
  9259.             printf( "permission to CuAt class file denied\n" );
  9260.             break;
  9261.           case ODMI_INTERNAL_ERR:
  9262.             printf("odm internal error\nPlease contact your administrator\n" );
  9263.             break;
  9264.           case ODMI_INVALID_CLXN:
  9265.             printf("CuAt is invalid or inconsistent odm class collection\n");
  9266.             break;
  9267.           case ODMI_INVALID_PATH:
  9268.             printf( "invalid path\n" );
  9269.             break;
  9270.           case ODMI_MAGICNO_ERR:
  9271.             printf( "CuAt is an invalid ODM object class\n" );
  9272.             break;
  9273.           case ODMI_MALLOC_ERR:
  9274.             printf( "malloc failed\n" );
  9275.             break;
  9276.           case ODMI_OPEN_ERR:
  9277.             printf( "cannot open CuAt class - and don't know why!\n" );
  9278.             break;
  9279.           case ODMI_TOOMANYCLASSES:
  9280.             printf( "too many object classes have been opened\n" );
  9281.             break;
  9282.           default:
  9283.             printf( "unknown error %d\nPlease call IBM\n", odmerrno );
  9284.         }
  9285.         return(rc);
  9286.     }
  9287.  
  9288.     /* check for a meaningfull response */
  9289.     if (response != NULL) {
  9290.         if (response->value != NULL) {
  9291.             strcpy(buf, response->value);
  9292.             rc = strlen( buf );
  9293. #ifdef DEBUG
  9294. /*
  9295.             printf( "attribute name is: %s\n", (char *)response->attribute );
  9296.             printf( "I think my address is %s\n", (char*)response->value );
  9297. */
  9298. #endif /* DEBUG */
  9299.         } else {
  9300.             printf( "kermit: x25local_nua(): couldn't find the local NUA\n" );
  9301.         }
  9302.     } else {
  9303.         switch (odmerrno) {
  9304.           case ODMI_BAD_CRIT:
  9305.             printf( "Error: ODMI_BAD_CRIT - bad criteria\n" );
  9306.             break;
  9307.           case ODMI_CLASS_DNE:
  9308.             printf( "Error: ODMI_CLASS_DNE - class doesn't exist\n" );
  9309.             break;
  9310.           case ODMI_CLASS_PERMS:
  9311.             printf( "Error: ODMI_CLASS_PERMS - class permissions\n" );
  9312.             break;
  9313.           case ODMI_INTERNAL_ERR:
  9314.             printf( "Error: ODMI_INTERNAL_ERR - panic\n" );
  9315.             break;
  9316.           case ODMI_INVALID_CLXN:
  9317.             printf( "Error: ODMI_INVALID_CLXN - invalid collection\n" );
  9318.             break;
  9319.           case ODMI_INVALID_PATH:
  9320.             printf( "Error: ODMI_INVALID_PATH - invalid path - what path?\n" );
  9321.             break;
  9322.           case ODMI_MAGICNO_ERR:
  9323.             printf( "Error: ODMI_MAGICNO_ERR - invalid object magic\n" );
  9324.             break;
  9325.           case ODMI_MALLOC_ERR:
  9326.             printf( "Error: ODMI_MALLOC_ERR - malloc failed\n" );
  9327.             break;
  9328.           case ODMI_OPEN_ERR:
  9329.             printf( "Error: ODMI_OPEN_ERR - cannot open class\n" );
  9330.             break;
  9331.           case ODMI_TOOMANYCLASSES:
  9332.             printf( "Error: ODMI_TOOMANYCLASSES - too many classes\n" );
  9333.             break;
  9334.           default:
  9335.             printf( "Unknown error!\n" );
  9336.         }
  9337.         return(rc);
  9338.     }
  9339.  
  9340.     /* close the database again */
  9341.     odm_close_class( retClass );
  9342.  
  9343.     /* forget about ODM all together */
  9344.     odm_terminate();
  9345.  
  9346. #ifdef TRACE
  9347.     printf( "TRACE: leaving x25local_nua\n" );
  9348. #endif /* TRACE */
  9349.  
  9350.     debug(F110, "x25local_nua local address is ", buf, 0);
  9351.     return(rc);
  9352. }
  9353.  
  9354. /*****************************************************************************
  9355.  * Function: x25facilities
  9356.  *
  9357.  * Description:
  9358.  *      build up the facilities data packet for a connection request
  9359.  *
  9360.  * Parameters:
  9361.  *      a pre-allocated char buffer, normally NPI_MAX_DATA big.
  9362.  *
  9363.  * Return Value:
  9364.  *      the number of characters inserted into the buffer
  9365.  */
  9366. int
  9367. x25facilities(buffer) char *buffer; {
  9368.     extern int revcall;
  9369.     extern int closgr;
  9370.     char *p;                            /* temp pointer */
  9371.     char *start;                        /* temp pointer */
  9372.  
  9373. #ifdef TRACE
  9374.     printf( "TRACE: entering x25facilities\n" );
  9375. #endif /* TRACE */
  9376.  
  9377.     p = buffer + 1;
  9378.     start = p;
  9379.  
  9380. #ifdef DEBUG
  9381.     printf( "kermit: x25facilities(): getting X25 facilities\n" );
  9382. #endif /* DEBUG */
  9383.  
  9384.     if (revcall != 0) {
  9385. #ifdef DEBUG
  9386.         printf("reverse charge: %d\n", revcall );
  9387. #endif /* DEBUG */
  9388.         *++p = 0x01;
  9389.         *++p = revcall;
  9390.     }
  9391.     if (closgr > 0) {
  9392. #ifdef DEBUG
  9393.         printf("closed user group: %d\n", closgr );
  9394. #endif /* DEBUG */
  9395.         *++p = 0x03;
  9396.         *++p = closgr;
  9397.     }
  9398.  
  9399. #ifdef DEBUG
  9400.     if (p == start) {
  9401.         printf( "no facilities\n" );
  9402.     }
  9403. #endif /* DEBUG */
  9404.  
  9405.     /* set the size of the facilities buffer */
  9406.     *buffer = (char)( p - start ) & 0xff;
  9407.  
  9408. #ifdef DEBUG
  9409.     printf( "kermit: x25facilities(): returning %d\n", (int)(p - buffer)  );
  9410. #endif /* DEBUG */
  9411.  
  9412. #ifdef TRACE
  9413.     printf( "TRACE: leaving x25facilities\n" );
  9414. #endif /* TRACE */
  9415.  
  9416.     /* return the size of the facilities with size byte */
  9417.     /* 1 == no facilities, 0 byte returned as facilities size */
  9418.     return( (int)(p - buffer) );
  9419. }
  9420.  
  9421. /*
  9422.  * reset the connection
  9423.  */
  9424. int
  9425. x25reset(cause, diagn) char cause; char diagn; {
  9426.     /* not implemented */
  9427.  
  9428. #ifdef TRACE
  9429.     printf( "TRACE: entering x25reset\n" );
  9430. #endif /* TRACE */
  9431.  
  9432. #ifdef TRACE
  9433.     printf( "TRACE: leaving x25reset\n" );
  9434. #endif /* TRACE */
  9435.  
  9436.     return(0);
  9437. }
  9438.  
  9439. /*
  9440.  * clear the x25 connection - ie: hang up
  9441.  */
  9442. int
  9443. x25clear() {
  9444.     int get_flags = 0;                  /* priority flag for getmsg */
  9445.     int put_flags = 0;                  /* send flags, always 0 */
  9446.     ulong type;                         /* primitive type */
  9447.     N_discon_req_t *discon_req;         /* pointer to N_DISCON_REQ */
  9448.     N_discon_ind_t *discon_ind;         /* pointer to N_DISCON_IND */
  9449.     N_npi_data_t *discon_data;          /* pointer to N_DISCON_IND data */
  9450.     int rc = 0;                         /* return code */
  9451.  
  9452. #ifdef TRACE
  9453.     printf( "TRACE: entering x25clear\n" );
  9454. #endif /* TRACE */
  9455.  
  9456. #ifdef DEBUG
  9457.     /* printf( "x25clear(): checking last msg: %s\n", x25prim(x25lastmsg)); */
  9458. #endif /* DEBUG */
  9459.  
  9460.     /*
  9461.     * The following checks are used to ensure that we don't disconnect
  9462.     * or unbind twice - this seems to throw the NPI interface right out of
  9463.     * kilter.
  9464.     */
  9465.     switch(x25lastmsg) {
  9466.       case N_BIND_ACK:
  9467.       case N_CONN_CON:
  9468.       case N_CONN_REQ:
  9469.       case N_DATA_REQ:
  9470.       case N_DATA_IND:
  9471.         {
  9472. #ifdef DEBUG
  9473.             /* printf("x25clear(): actively disconnecting\n"); */
  9474. #endif /* DEBUG */
  9475.  
  9476.                 discon_req = (N_discon_req_t *)malloc(NPI_MAX_CTL);
  9477.                 if (discon_req == NULL) {
  9478.                     perror("kermit x25clear(): discon_req malloc failed\n");
  9479.                     /* fallthrough, try to unbind the NPI anyway */
  9480.                 } else {
  9481.                     discon_req->PRIM_type = N_DISCON_REQ;
  9482.                     discon_req->DISCON_reason = 0;      /* not used by AIX */
  9483.                     discon_req->RES_length = 0;
  9484.                     discon_req->RES_offset = (ulong)(sizeof(N_discon_req_t));
  9485.                     discon_req->SEQ_number = x25seqno;  /* global */
  9486.  
  9487.                     if (x25putmsg(ttyfd,
  9488.                                   (N_npi_ctl_t*)discon_req,
  9489.                                   (N_npi_data_t*)NULL,
  9490.                                   0,
  9491.                                   &put_flags
  9492.                                   ) < 0) {
  9493.                         perror("x25putmsg failed in x25clear()");
  9494.                     }
  9495.                     discon_ind = (N_discon_ind_t *)malloc(NPI_MAX_CTL);
  9496.                     discon_data = (N_npi_data_t *)malloc(NPI_MAX_DATA);
  9497.                     if((discon_ind == NULL) || (discon_data == NULL)) {
  9498.                         perror("x25clear(): discon_ind malloc failed\n");
  9499.                         /* fallthrough, try to unbind the NPI anyway */
  9500.                     } else {
  9501.                         if(x25getmsg(ttyfd,
  9502.                                      (N_npi_ctl_t*)discon_ind,
  9503.                                      NPI_MAX_CTL,
  9504.                                      (N_npi_data_t*)discon_data,
  9505.                                      NPI_MAX_DATA,
  9506.                                      &get_flags,
  9507.                                      N_OK_ACK
  9508.                                      ) < 0 ) {
  9509.                             perror("x25getmsg failed in x25clear()");
  9510.                             /* fallthrough, try to unbind the NPI anyway */
  9511.                         }
  9512.                     }
  9513.                 }
  9514.                 break;
  9515.             }
  9516.     }
  9517.  
  9518.     if (x25lastmsg != N_UNBIND_REQ) {
  9519.         rc = x25unbind(ttyfd);
  9520.     }
  9521.  
  9522. #ifdef TRACE
  9523.     printf( "TRACE: leaving x25clear\n" );
  9524. #endif /* TRACE */
  9525.  
  9526.     return(rc);
  9527. }
  9528.  
  9529. #ifdef DEBUG
  9530. /*
  9531.  * only for debugging
  9532.  *
  9533.  * turn the internal representation of a datablock into something
  9534.  * half-way readable. Because the length is known, we can print
  9535.  * the string including null's etc (important, because the first(!)
  9536.  * byte of an X121 address is a null! (X121 addr == 0 + X25 addr)
  9537.  */
  9538. x25dump_data(char *addr, ulong offset, ulong length) {
  9539.     char *ptr = addr + offset;
  9540.     ulong i = length;
  9541.     /* allocate enough memory for all unprintable chars */
  9542.     char *buf = (char *)malloc( length * 4 );
  9543.     char *bptr = buf;   /* pointer to current place in the print buffer */
  9544.  
  9545.     while (i > 0) {
  9546.         if (isprint(*ptr)) {
  9547.             *bptr++ = *ptr;
  9548.         } else {
  9549.             *bptr++ = '[';
  9550.             strcpy(bptr,ckctox(*ptr,1)); bptr += 2;
  9551.             *bptr++ = ']';
  9552.         }
  9553.         ptr++;
  9554.         i--;
  9555.     }
  9556.     if (length > 0) {
  9557.         *bptr = '\0';
  9558.         printf( "%s", buf );
  9559.     }
  9560.     printf( " (%d+%d)\n", offset, length );
  9561.  
  9562.     if (buf) { free(buf); buf = NULL; }
  9563.     return;
  9564. }
  9565.  
  9566. /*
  9567.  * only for debugging
  9568.  * print as much useful information about a packet as possible
  9569.  */
  9570. x25dump_prim(primitive)    N_npi_ctl_t *primitive; {
  9571.     printf("Primitive");
  9572.     switch (primitive->PRIM_type) {
  9573.       case N_BIND_ACK:
  9574.         printf( "\tN_BIND_ACK\n\taddress:\t" );
  9575.         x25dump_data( (char *)primitive,
  9576.                      primitive->bind_ack.ADDR_offset,
  9577.                      primitive->bind_ack.ADDR_length );
  9578.         printf( "\tproto id:\t" );
  9579.         x25dump_data( (char *)primitive,
  9580.                      primitive->bind_ack.PROTOID_offset,
  9581.                      primitive->bind_ack.PROTOID_length );
  9582.         printf( "\tconnind:\t%d\n\ttoken:\t\t%d\n",
  9583.                primitive->bind_ack.CONIND_number,
  9584.                primitive->bind_ack.TOKEN_value );
  9585.         break;
  9586.  
  9587.       case N_BIND_REQ:
  9588.         printf( "\tN_BIND_REQ\n\taddress:\t" );
  9589.         x25dump_data( (char *)primitive,
  9590.                      primitive->bind_req.ADDR_offset,
  9591.                      primitive->bind_req.ADDR_length );
  9592.         printf( "\tproto id:\t" );
  9593.         x25dump_data( (char *)primitive,
  9594.                      primitive->bind_req.PROTOID_offset,
  9595.                      primitive->bind_req.PROTOID_length );
  9596.         printf( "\tconnind:\t%d\n\tflags:\t\t%d\n",
  9597.                primitive->bind_req.CONIND_number,
  9598.                primitive->bind_req.BIND_flags );
  9599.         break;
  9600.  
  9601.       case N_CONN_CON:
  9602.         printf( "\tN_CONN_CON\n" );
  9603.         printf( "\tRES\t\t" );
  9604.         x25dump_data( (char *)primitive,
  9605.                      primitive->conn_con.RES_offset,
  9606.                      primitive->conn_con.RES_length );
  9607.         printf( "\tflags:\t%d\n", primitive->conn_con.CONN_flags );
  9608.         break;
  9609.  
  9610.       case N_CONN_IND:
  9611.         printf( "\tN_CONN_IND\n" );
  9612.         printf( "\tsource:\t\t" );
  9613.         x25dump_data( (char *)primitive,
  9614.                      primitive->conn_ind.SRC_offset,
  9615.                      primitive->conn_ind.SRC_length );
  9616.         printf( "\tdestination:\t" );
  9617.         x25dump_data( (char *)primitive,
  9618.                      primitive->conn_ind.DEST_offset,
  9619.                      primitive->conn_ind.DEST_length );
  9620.         printf( "\tSEQ_number:\t%d\n", primitive->conn_ind.SEQ_number );
  9621.         printf( "\tflags:\t%d\n", primitive->conn_ind.CONN_flags );
  9622.         break;
  9623.  
  9624.       case N_CONN_REQ:
  9625.         printf( "\tN_CONN_REQ\n\tdestination:\t" );
  9626.         x25dump_data( (char *)primitive,
  9627.                      primitive->conn_req.DEST_offset,
  9628.                      primitive->conn_req.DEST_length );
  9629.         printf( "\tflags:\t%d\n", primitive->conn_req.CONN_flags );
  9630.         break;
  9631.  
  9632.       case N_CONN_RES:
  9633.         printf( "\tN_CONN_RES\n" );
  9634.         printf( "\tTOKEN_value\t%d\n", primitive->conn_res.TOKEN_value );
  9635.         printf( "\tSEQ_number\t%d\n", primitive->conn_res.SEQ_number );
  9636.         printf( "\tCONN_flags\t%d\n", primitive->conn_res.CONN_flags );
  9637.         printf( "\tRES\t\t" );
  9638.         x25dump_data( (char *)primitive,
  9639.                      primitive->conn_res.RES_offset,
  9640.                      primitive->conn_res.RES_length );
  9641.         break;
  9642.  
  9643.       case N_DATACK_IND:
  9644.         printf( "\tN_DATACK_IND\n" );
  9645.         break;
  9646.  
  9647.       case N_DATACK_REQ:
  9648.         printf( "\tN_DATACK_REQ\n" );
  9649.         printf( "\tflags:\t%d\n", primitive->data_req.DATA_xfer_flags );
  9650.         break;
  9651.  
  9652.       case N_DATA_IND:
  9653.         printf( "\tN_DATA_IND\n" );
  9654.         printf( "\tflags:\t%d\n", primitive->data_ind.DATA_xfer_flags );
  9655.         break;
  9656.  
  9657.       case N_DATA_REQ:
  9658.         printf( "\tN_DATA_REQ\n" );
  9659.         break;
  9660.  
  9661.       case N_DISCON_IND:
  9662.         printf( "\tN_DISCON_IND\n" );
  9663.         printf( "\torigin:\t%d\n", primitive->discon_ind.DISCON_orig );
  9664.         printf( "\treason:\t\t%d\n", primitive->discon_ind.DISCON_reason );
  9665.         printf( "\tseq no:\t\t%d\n", primitive->discon_ind.SEQ_number );
  9666.         printf( "\tRES:\t" );
  9667.         x25dump_data( (char *)primitive,
  9668.                      primitive->discon_ind.RES_offset,
  9669.                      primitive->discon_ind.RES_length );
  9670.         break;
  9671.  
  9672.       case N_DISCON_REQ:
  9673.         printf( "\tN_DISCON_REQ\n" );
  9674.         printf( "\tDISCON_reason:\t%d\n",
  9675.                primitive->discon_req.DISCON_reason );
  9676.         printf( "\tRES:\t" );
  9677.         x25dump_data( (char *)primitive,
  9678.                      primitive->discon_req.RES_offset,
  9679.                      primitive->discon_req.RES_length );
  9680.         printf( "\tSEQ_number:\t%d\n", primitive->discon_req.SEQ_number );
  9681.         break;
  9682.  
  9683.       case N_ERROR_ACK:
  9684.         printf( "\tN_ERROR_ACK\n" );
  9685.         printf( "\tCaused by:\t%s\n",
  9686.                x25prim( primitive->error_ack.ERROR_prim ) );
  9687.         printf( "\tNPI error:\t%s\n",
  9688.                x25err( primitive->error_ack.NPI_error ));
  9689.         errno = primitive->error_ack.UNIX_error;
  9690.         perror( "\t" );
  9691.         break;
  9692.  
  9693.       case N_EXDATA_IND:
  9694.         printf( "\tN_EXDATA_ACK\n" );
  9695.         break;
  9696.  
  9697.       case N_EXDATA_REQ:
  9698.         printf( "\tN_EXDATA_REQ\n" );
  9699.         break;
  9700.  
  9701.       case N_INFO_ACK:
  9702.         printf( "\tN_INFO_ACK\n" );
  9703.         printf( "\tNSDU size:\t%d\n", primitive->info_ack.NSDU_size );
  9704.         printf( "\tENSDU size:\t%d\n", primitive->info_ack.ENSDU_size );
  9705.         printf( "\tCDATA size:\t%d\n", primitive->info_ack.CDATA_size );
  9706.         printf( "\tDDATA size:\t%d\n", primitive->info_ack.DDATA_size );
  9707.         printf( "\tADDR size:\t%d\n", primitive->info_ack.ADDR_size );
  9708.         printf( "\tNIDU size:\t%d\n", primitive->info_ack.NIDU_size );
  9709.         break;
  9710.  
  9711.       case N_INFO_REQ:
  9712.         printf( "\tN_INFO_REQ\n" );
  9713.         break;
  9714.  
  9715.       case N_OK_ACK:
  9716.         printf( "\tN_OK_ACK\n" );
  9717.         break;
  9718.  
  9719.       case N_OPTMGMT_REQ:
  9720.         printf( "\tN_OPTMGMT_REQ\n" );
  9721.         break;
  9722.  
  9723.       case N_RESET_CON:
  9724.         printf( "\tN_RESET_CON\n" );
  9725.         break;
  9726.  
  9727.       case N_RESET_IND:
  9728.         printf( "\tN_RESET_IND\n" );
  9729.         printf( "\treason:\t\t%d\n", primitive->reset_ind.RESET_reason );
  9730.         printf( "\torigin:\t\t%d\n", primitive->reset_ind.RESET_orig );
  9731.         break;
  9732.  
  9733.       case N_RESET_REQ:
  9734.         printf( "\tN_RESET_REQ\n" );
  9735.         printf( "\treason:\t\t%d\n", primitive->reset_req.RESET_reason );
  9736.         break;
  9737.  
  9738.       case N_RESET_RES:
  9739.         printf( "\tN_RESET_RES\n" );
  9740.         break;
  9741.  
  9742.       case N_UDERROR_IND:
  9743.         printf( "\tN_UDERROR_IND\n" );
  9744.         break;
  9745.  
  9746.       case N_UNBIND_REQ:
  9747.         printf( "\tN_UNBIND_REQ\n" );
  9748.         break;
  9749.  
  9750.       case N_UNITDATA_REQ:
  9751.         printf( "\tN_UNITDATA_REQ\n" );
  9752.         break;
  9753.  
  9754.       case N_UNITDATA_IND:
  9755.         printf( "\tN_UNITDATA_IND\n" );
  9756.         break;
  9757.  
  9758.       default:
  9759.         (void) printf( "Unknown NPI error %d", primitive->PRIM_type );
  9760.         return 0;
  9761.     }
  9762. }
  9763. #endif /* DEBUG */
  9764.  
  9765. /* it looks like signal handling is not needed with streams! */
  9766. /* x25oobh()    - handle SIGURG signals - take from isode ? */
  9767.  
  9768. #endif /* IBMX25 */
  9769.  
  9770. #ifndef NOHTTP
  9771. /*
  9772.   Which time.h files to include... See ckcdeb.h for defaults.
  9773.   Note that 0, 1, 2, or all 3 of these can be included according to
  9774.   the symbol definitions.
  9775. */
  9776. #ifndef NOTIMEH
  9777. #ifdef TIMEH
  9778. #include <time.h>
  9779. #endif /* TIMEH */
  9780. #endif /* NOTIMEH */
  9781.  
  9782. #ifndef NOSYSTIMEH
  9783. #ifdef SYSTIMEH
  9784. #include <sys/time.h>
  9785. #endif /* SYSTIMEH */
  9786. #endif /* NOSYSTIMEH */
  9787.  
  9788. #ifndef NOSYSTIMEBH
  9789. #ifdef SYSTIMEBH
  9790. #include <sys/timeb.h>
  9791. #endif /* SYSTIMEBH */
  9792. #endif /* NOSYSTIMEBH */
  9793.  
  9794. #ifndef TIMEH
  9795. #ifndef SYSTIMEH
  9796. #ifndef SYSTIMEBH
  9797. #ifdef Plan9
  9798. #include <sys/time.h>
  9799. #else
  9800. #ifdef AIX41
  9801. #include <time.h>
  9802. #else
  9803. #ifdef SUNOS4
  9804. #include <sys/time.h>
  9805. #else
  9806. #ifdef SYSTIMEH
  9807. #include <sys/time.h>
  9808. #else
  9809. #ifdef POSIX
  9810. #include <posix/time.h>
  9811. #else
  9812. #ifdef CLIX
  9813. #include <sys/time.h>
  9814. #else
  9815. #ifdef OS2
  9816. #include <time.h>
  9817. #else
  9818. #include <time.h>
  9819. /* #include <utime.h> */
  9820. #endif /* OS2 */
  9821. #endif /* CLIX */
  9822. #endif /* POSIX */
  9823. #endif /* SYSTIMEH */
  9824. #endif /* SUNOS4 */
  9825. #endif /* AIX41 */
  9826. #endif /* Plan9 */
  9827. #endif
  9828. #endif
  9829. #endif
  9830.  
  9831. #ifdef OS2
  9832. #include <sys/utime.h>
  9833. #ifdef NT
  9834. #define utimbuf _utimbuf
  9835. #endif /* NT */
  9836. #define utime   _utime
  9837. #else
  9838. #ifdef SYSUTIMEH                        /* <sys/utime.h> if requested,  */
  9839. #include <sys/utime.h>                  /* for extra fields required by */
  9840. #else                                   /* 88Open spec. */
  9841. #ifdef UTIMEH                           /* or <utime.h> if requested */
  9842. #include <utime.h>                      /* (SVR4, POSIX) */
  9843. #define SYSUTIMEH                       /* Use this for both cases. */
  9844. #endif /* UTIMEH */
  9845. #endif /* SYSUTIMEH */
  9846. #endif /* OS2 */
  9847.  
  9848. #ifndef HTTP_VERSION
  9849. #define HTTP_VERSION "HTTP/1.1"
  9850. #endif /* HTTP_VERSION */
  9851.  
  9852. #ifdef CMDATE2TM
  9853. time_t
  9854. #ifdef CK_ANSIC
  9855. http_date(char * date)
  9856. #else
  9857. http_date(date) char * date;
  9858. #endif /* CK_ANSIC */
  9859. /* http_date */ {
  9860.     /* HTTP dates are of the form:  "Sun, 06 Oct 1997 20:11:47 GMT" */
  9861.     /* There are two older formats which we are required to parse
  9862.      * that we currently do not:
  9863.      *
  9864.      * RFC 850:   "Sunday, 06-Oct-97 20:11:47 GMT"
  9865.      * asctime(): "Sun Nov  6 20:11:47 1997"
  9866.      *
  9867.      * However, it is required that all dates be sent in the form we
  9868.      * do accept.  The other two formats are for compatibility with
  9869.      * really old servers.
  9870.      */
  9871.     extern char cmdatebuf[18];
  9872.     struct tm t_tm;
  9873.     time_t t;
  9874.     char ldate[32];
  9875.     int j;
  9876.  
  9877.     j = ckindex(",",date,0,0,0);
  9878.     ckstrncpy(ldate,&date[j+1],25);
  9879.  
  9880.     {   /*
  9881.            cmcvtate() date changed to return a string pointer.
  9882.            fdc, 12 Aug 2001.
  9883.         */
  9884.         char * dp;
  9885.         dp = (char *)cmcvtdate(ldate,0); /* Convert to normal form */
  9886.         if (!dp)
  9887.           return(0);
  9888.         t_tm = *cmdate2tm(dp,1);
  9889.     }
  9890. /*
  9891.   From Lucas Hart, 5 Dec 2001:
  9892.   "On the systems to which I have access (SunOS 4.1.1, Solaris 8, and Tru64),
  9893.   setting tm_isdst to -1 maintains the correct timezone offsets, i.e., writes
  9894.   the specified (GMT) time if the buffer size is 21, or the contemporaneous
  9895.   localtime if the buffer size is 25.  Perhaps tm_isdst should be set in
  9896.   cmdate2tm(), rather than only in http_date."
  9897. */
  9898. #ifndef NOTM_ISDST                      /* For platforms where */
  9899.     t_tm.tm_isdst = -1;                 /* tm_isdst doesn't exist. */
  9900. #endif /* NOTM_ISDST */
  9901.  
  9902.     t = mktime(&t_tm);                  /* NOT PORTABLE */
  9903.  
  9904. #ifdef XX_TIMEZONE
  9905.     t -= _timezone;
  9906. #endif /* XX_TIMEZONE */
  9907.  
  9908.     return(t);
  9909. }
  9910. #endif /* CMDATE2TM */
  9911.  
  9912. char *
  9913. http_now() {
  9914.     static char nowstr[32];
  9915. #ifdef CMDATE2TM
  9916.     struct tm  *gmt;
  9917.     time_t ltime;                       /* NOT PORTABLE */
  9918.  
  9919.     time(<ime);
  9920.  
  9921.     gmt = gmtime(<ime);               /* PROBABLY NOT PORTABLE */
  9922.     strftime(nowstr,32,"%a, %d %b %Y %H:%M:%S GMT",gmt); /* NOT PORTABLE */
  9923.     /* not only is it not portable but it's locale-dependent */
  9924. #else
  9925. /*
  9926.   This is hopeless.  First of all, it seems that HTTP wants Day and Month
  9927.   NAMES?  In English?  Whose idea was that?  Even worse, the date/time must be
  9928.   expressed in Zulu (UTC (GMT)), and converting from local time to GMT is a
  9929.   nightmare.  Every platform does it differently, if at all -- even if we
  9930.   restrict ourselves to UNIX.  For example (quoting from recent C-Kermit edit
  9931.   history), "Fixed a longstanding bug in the BSDI version, in which incoming
  9932.   file dates were set in GMT rather than local time.  It seems in 4.4BSD,
  9933.   localtime() does not return the local time, but rather Zero Meridian (Zulu)
  9934.   time (GMT), and must be adjusted by the tm_gmtoff value."  Swell.  For
  9935.   greater appreciation of the scope of the problem, just take a look at the
  9936.   time-related #ifdefs in ckutio.c.  The only right way to do this is to add
  9937.   our own portable API for converting between local time and GMT/UTC/Zulu
  9938.   that shields us not only from UNIXisms like time_t and struct tm, but also
  9939.   the unbelievable amount of differences in time-related APIs -- e.g. is
  9940.   "timezone" an external variable or a function; which header file(s) do we
  9941.   include, etc etc etc.  It's a major project.
  9942. */
  9943.     int x;
  9944.     x = cmcvtdate("",1);
  9945.  
  9946. Evidently this code is not used -- if it is, it must be fixed to use
  9947. new (aug 2001) cmcvtdate() calling conventions.
  9948.  
  9949.     if (x < 0)
  9950.       return("");
  9951. /*  yyyymmdd hh:mm:ss */
  9952. /*  01234567890123456 */
  9953.     nowstr[0]  = 'X';                   /* 1st letter of day */
  9954.     nowstr[1]  = 'x';                   /* 2nd letter of day */
  9955.     nowstr[2]  = 'x';                   /* 3rd letter of day */
  9956.     nowstr[3]  = ',';
  9957.     nowstr[4]  = ' ';
  9958.     nowstr[5]  = cmdate[6];
  9959.     nowstr[6]  = cmdate[7];
  9960.     nowstr[7]  = ' ';
  9961.     nowstr[8]  = ' ';                   /* first letter of month */
  9962.     nowstr[9]  = ' ';                   /* second letter of month */
  9963.     nowstr[10] = ' ';                   /* third letter of month */
  9964.     nowstr[11] = ' ';
  9965.     nowstr[12] = cmdate[0];
  9966.     nowstr[13] = cmdate[1];
  9967.     nowstr[14] = cmdate[2];
  9968.     nowstr[15] = cmdate[3];
  9969.     nowstr[16] = ' ';
  9970.     nowstr[17] = cmdate[9];
  9971.     nowstr[18] = cmdate[10];
  9972.     nowstr[19] = cmdate[11];
  9973.     nowstr[20] = cmdate[12];
  9974.     nowstr[21] = cmdate[13];
  9975.     nowstr[22] = cmdate[14];
  9976.     nowstr[23] = cmdate[15];
  9977.     nowstr[24] = cmdate[16];
  9978.     nowstr[25] = ' ';
  9979.     nowstr[26] = 'G';
  9980.     nowstr[27] = 'M';
  9981.     nowstr[28] = 'T';
  9982.     nowstr[29] = '\0';
  9983. #endif /* CMDATE2TM */
  9984.     return(nowstr);
  9985. }
  9986.  
  9987. #ifndef OS2
  9988. #ifndef CK_AUTHENTICATION
  9989. /* from ckuusr.h, which this module normally doesn't include */
  9990. _PROTOTYP( int dclarray, (char, int) );
  9991. #endif /* CK_AUTHENTICATION */
  9992. #endif /* OS2 */
  9993. /*
  9994.   Assign http response pairs to given array.
  9995.   For best results, response pairs should contain no spaces.
  9996.  
  9997.   Call with:
  9998.     resp  =  pointer to response list.
  9999.     n     =  size of response list.
  10000.     array =  array letter.
  10001.   Returns:
  10002.     0 on failure.
  10003.     >= 1, size of array, on success.
  10004. */
  10005. static int
  10006. #ifdef CK_ANSIC
  10007. http_mkarray(char ** resp, int n, char array)
  10008. #else
  10009. http_mkarray(resp, n, array) char ** resp; int n; char array;
  10010. #endif /* CK_ANSIC */
  10011. {
  10012. #ifndef NOSPL
  10013.     int i, x;
  10014.     char ** ap;
  10015.     extern char ** a_ptr[];
  10016.     extern int a_dim[];
  10017.  
  10018.     if (!array || n <= 0)
  10019.       return(0);
  10020.     if ((x = dclarray(array,n)) < 0) {
  10021.         printf("?Array declaration failure\n");
  10022.         return(-9);
  10023.     }
  10024.     /* Note: argument array is 0-based but Kermit array is 1-based */
  10025.     ap = a_ptr[x];
  10026.     ap[0] = NULL;                       /* 0th element is empty */
  10027.     for (i = 1; i <= n; i++) {
  10028.         ap[i] = resp[i-1];              /* If resp elements were malloc'd */
  10029.         resp[i-1] = NULL;
  10030.     }
  10031.     a_dim[x] = n;
  10032.     return(n);
  10033. #else
  10034.     return(0);
  10035. #endif /* NOSPL */
  10036. }
  10037.  
  10038. #define HTTPHEADCNT 64
  10039. int
  10040. http_get_chunk_len()
  10041. {
  10042.     int len = 0;
  10043.     int i = 0, j = -1;
  10044.     char buf[24];
  10045.     int ch;
  10046.  
  10047.     while ((ch = http_inc(0)) >= 0 && i < 24) {
  10048.         buf[i] = ch;
  10049.         if ( buf[i] == ';' )            /* Find chunk-extension (if any) */
  10050.             j = i;
  10051.         if ( buf[i] == 10 ) {           /* found end of line */
  10052.             if (i > 0 && buf[i-1] == 13)
  10053.                 i--;
  10054.             buf[i] = '\0';
  10055.             break;
  10056.         }
  10057.         i++;
  10058.     }
  10059.     if ( i < 24 ) {                     /* buf now contains len in Hex */
  10060.         len = hextoulong(buf, j == -1 ? i : j-1);
  10061.     }
  10062.  
  10063.     return(len);
  10064. }
  10065.  
  10066. int
  10067. http_isconnected()
  10068. {
  10069.     return(httpfd != -1);
  10070. }
  10071.  
  10072. char *
  10073. http_host()
  10074. {
  10075.     return(httpfd != -1 ? http_host_port : "");
  10076. }
  10077.  
  10078. char *
  10079. http_security()
  10080. {
  10081.     if ( httpfd == -1 )
  10082.         return("NULL");
  10083. #ifdef CK_SSL
  10084.     if (tls_http_active_flag) {
  10085.         SSL_CIPHER * cipher;
  10086.         const char *cipher_list;
  10087.         static char buf[128];
  10088.         buf[0] = NUL;
  10089.         cipher = SSL_get_current_cipher(tls_http_con);
  10090.         cipher_list = SSL_CIPHER_get_name(cipher);
  10091.         SSL_CIPHER_description(cipher,buf,sizeof(buf));
  10092.         return(buf);
  10093.     }
  10094. #endif /* CK_SSL */
  10095.     return("NULL");
  10096. }
  10097.  
  10098. int
  10099. http_reopen()
  10100. {
  10101.     int rc = 0;
  10102.     char * s = NULL;                    /* strdup is not portable */
  10103.     if ( tcp_http_proxy ) {
  10104.         char * p;
  10105.         makestr(&s,(char *)http_host_port);
  10106.         p = s;
  10107.         while (*p != '\0' && *p != ':') p++; /* Look for colon */
  10108.         if (*p == ':') {                     /* Have a colon */
  10109.             *p++ = '\0';                     /* Get service name or number */
  10110.         } else {
  10111.             p="http";
  10112.         }
  10113.         rc = http_open(s,p,http_ssl,NULL,0,http_agent);
  10114.     } else {
  10115.         makestr(&s,(char *)http_ip);
  10116.         rc = http_open(s,ckuitoa(http_port),http_ssl,NULL,0,http_agent);
  10117.     }
  10118.     free(s);
  10119.     return(rc);
  10120. }
  10121.  
  10122.  
  10123. int
  10124. #ifdef CK_ANSIC
  10125. http_open(char * hostname, char * svcname, int use_ssl, char * rdns_name,
  10126.           int rdns_len, char * agent)
  10127. #else /* CK_ANSIC */
  10128. http_open(hostname, svcname, use_ssl, rdns_name, rdns_len, agent)
  10129.     char * hostname;
  10130.     char * svcname;
  10131.     int    use_ssl;
  10132.     char * rdns_name;
  10133.     int    rdns_len;
  10134.     char * agent;
  10135. #endif /* CK_ANSIC */
  10136. {
  10137.     char namecopy[NAMECPYL];
  10138.     char *p;
  10139.     int i, x, dns = 0;
  10140. #ifdef TCPSOCKET
  10141.     int isconnect = 0;
  10142. #ifdef SO_OOBINLINE
  10143.     int on = 1;
  10144. #endif /* SO_OOBINLINE */
  10145.     struct servent *service=NULL;
  10146.     struct hostent *host=NULL;
  10147.     struct sockaddr_in r_addr;
  10148.     struct sockaddr_in sin;
  10149.     struct sockaddr_in l_addr;
  10150.     GSOCKNAME_T l_slen;
  10151. #ifdef EXCELAN
  10152.     struct sockaddr_in send_socket;
  10153. #endif /* EXCELAN */
  10154.  
  10155. #ifdef INADDRX
  10156. /* inet_addr() is of type struct in_addr */
  10157. #ifdef datageneral
  10158.     extern struct in_addr inet_addr();
  10159. #else
  10160. #ifdef HPUX5WINTCP
  10161.     extern struct in_addr inet_addr();
  10162. #endif /* HPUX5WINTCP */
  10163. #endif /* datageneral */
  10164.     struct in_addr iax;
  10165. #else
  10166. #ifdef INADDR_NONE
  10167.     struct in_addr iax;
  10168. #else /* INADDR_NONE */
  10169.     long iax;
  10170. #endif /* INADDR_NONE */
  10171. #endif /* INADDRX */
  10172.  
  10173.     if ( rdns_name == NULL || rdns_len < 0 )
  10174.         rdns_len = 0;
  10175.  
  10176.     *http_ip = '\0';                     /* Initialize IP address string */
  10177.     namecopy[0] = '\0';
  10178.  
  10179. #ifdef DEBUG
  10180.     if (deblog) {
  10181.         debug(F110,"http_open hostname",hostname,0);
  10182.         debug(F110,"http_open svcname",svcname,0);
  10183.     }
  10184. #endif /* DEBUG */
  10185.     if (!hostname) hostname = "";
  10186.     if (!svcname) svcname = "";
  10187.     if (!*hostname || !*svcname) return(-1);
  10188.  
  10189.     
  10190.     service = ckgetservice(hostname,svcname,http_ip,20);
  10191.  
  10192.     if (service == NULL) {
  10193.         if ( !quiet )
  10194.             printf("?Invalid service: %s\r\n",svcname);
  10195.         return(-1);
  10196.     }
  10197.  
  10198.     /* For HTTP connections we must preserve the original hostname and */
  10199.     /* service requested so we can include them in the Host header.    */
  10200.     ckmakmsg(http_host_port,sizeof(http_host_port),hostname,":",
  10201.               ckuitoa(ntohs(service->s_port)),NULL);
  10202.     http_port = ntohs(service->s_port);
  10203.     http_ssl = use_ssl;
  10204.     debug(F111,"http_open",http_host_port,http_port);
  10205.  
  10206.     /* 'http_ip' contains the IP address to which we want to connect        */
  10207.     /* 'svcnam'   contains the service name                                 */
  10208.     /* 'service->s_port' contains the port number in network byte order     */
  10209.  
  10210.     /* If we are using an http proxy, we need to create a buffer containing */
  10211.     /*   hostname:port-number                                               */
  10212.     /* to pass to the http_connect() function.  Then we need to replace     */
  10213.     /* 'namecopy' with the name of the proxy server and the service->s_port */
  10214.     /* with the port number of the proxy (default port 80).                 */
  10215.  
  10216.     if ( tcp_http_proxy ) {
  10217.  
  10218.         ckmakmsg(proxycopy,sizeof(proxycopy),hostname,":",
  10219.                  ckuitoa(ntohs(service->s_port)),NULL);
  10220.         ckstrncpy(namecopy,tcp_http_proxy,NAMECPYL);
  10221.  
  10222.         p = namecopy;                       /* Was a service requested? */
  10223.         while (*p != '\0' && *p != ':') p++; /* Look for colon */
  10224.         if (*p == ':') {                    /* Have a colon */
  10225.             debug(F110,"http_open name has colon",namecopy,0);
  10226.             *p++ = '\0';                    /* Get service name or number */
  10227.         } else {
  10228.             strcpy(++p,"http");
  10229.         }
  10230.  
  10231.         service = ckgetservice(namecopy,p,http_ip,20);
  10232.         if (!service) {
  10233.             fprintf(stderr, "Can't find port for service %s\n", p);
  10234. #ifdef TGVORWIN
  10235.             debug(F101,"http_open can't get service for proxy","",socket_errno);
  10236. #else
  10237.             debug(F101,"http_open can't get service for proxy","",errno);
  10238. #endif /* TGVORWIN */
  10239.             errno = 0;                  /* (rather than mislead) */
  10240.             return(-1);
  10241.         }
  10242.  
  10243.         /* copy the proxyname and remove the service if any so we can use 
  10244.          * it as the hostname 
  10245.          */
  10246.         ckstrncpy(namecopy,tcp_http_proxy,NAMECPYL);
  10247.         p = namecopy;                       /* Was a service requested? */
  10248.         while (*p != '\0' && *p != ':') p++; /* Look for colon */
  10249.         if (*p == ':') {                    /* Have a colon */
  10250.             *p = '\0';                      /* terminate string */
  10251.         }        
  10252.         hostname = namecopy;                /* use proxy as hostname */
  10253.     }
  10254.  
  10255.     /* Set up socket structure and get host address */
  10256.     bzero((char *)&r_addr, sizeof(r_addr));
  10257.     debug(F100,"http_open bzero ok","",0);
  10258.  
  10259. #ifdef INADDR_NONE
  10260.     debug(F101,"http_open INADDR_NONE defined","",INADDR_NONE);
  10261. #else /* INADDR_NONE */
  10262.     debug(F100,"http_open INADDR_NONE not defined","",0);
  10263. #endif /* INADDR_NONE */
  10264. #ifdef INADDRX
  10265.     debug(F100,"http_open INADDRX defined","",0);
  10266. #else /* INADDRX */
  10267.     debug(F100,"http_open INADDRX not defined","",0);
  10268. #endif /* INADDRX */
  10269.  
  10270. #ifndef NOMHHOST
  10271. #ifdef INADDRX
  10272.     iax = inet_addr(http_ip[0]?http_ip:hostname);
  10273.     debug(F111,"http_open inet_addr",http_ip[0]?http_ip:hostname,iax.s_addr);
  10274. #else /* INADDRX */
  10275. #ifdef INADDR_NONE
  10276.     iax.s_addr = inet_addr(http_ip[0]?http_ip:hostname);
  10277.     debug(F111,"http_open inet_addr",http_ip[0]?http_ip:hostname,iax.s_addr);
  10278. #else /* INADDR_NONE */
  10279. #ifndef datageneral
  10280.     iax = (unsigned int) inet_addr(http_ip[0]?http_ip:hostname);
  10281. #else
  10282.     iax = -1L;
  10283. #endif /* datageneral */
  10284.     debug(F111,"http_open inet_addr",http_ip[0]?http_ip:hostname,iax);
  10285. #endif /* INADDR_NONE */
  10286. #endif /* INADDRX */
  10287.  
  10288.     dns = 0;
  10289.     if (
  10290. #ifdef INADDR_NONE
  10291. /* This might give warnings on 64-bit platforms but they should be harmless */
  10292. /* because INADDR_NONE should be all 1's anyway, thus the OR part is */
  10293. /* probably superfluous -- not sure why it's even there, maybe it should be */
  10294. /* removed. */
  10295.         iax.s_addr == INADDR_NONE /* || iax.s_addr == (unsigned long) -1L */
  10296. #else /* INADDR_NONE */
  10297.         iax == -1
  10298. #endif /* INADDR_NONE */
  10299.         ) {
  10300.         if (!quiet) {
  10301.             printf(" DNS Lookup... ");
  10302.             fflush(stdout);
  10303.         }
  10304.         if ((host = gethostbyname(http_ip[0] ? http_ip : hostname)) != NULL) {
  10305.             debug(F100,"http_open gethostbyname != NULL","",0);
  10306.             host = ck_copyhostent(host);
  10307.             dns = 1;                    /* Remember we performed dns lookup */
  10308.             r_addr.sin_family = host->h_addrtype;
  10309.             if (tcp_rdns && host->h_name && host->h_name[0] && (rdns_len > 0)
  10310.                  && (tcp_http_proxy == NULL)
  10311.                  )
  10312.                 ckmakmsg(rdns_name,rdns_len,host->h_name,":",svcname,NULL);
  10313.  
  10314. #ifdef HADDRLIST
  10315. #ifdef h_addr
  10316.             /* This is for trying multiple IP addresses - see <netdb.h> */
  10317.             if (!(host->h_addr_list))
  10318.               return(-1);
  10319.             bcopy(host->h_addr_list[0],
  10320.                   (caddr_t)&r_addr.sin_addr,
  10321.                   host->h_length
  10322.                   );
  10323. #else
  10324.             bcopy(host->h_addr, (caddr_t)&r_addr.sin_addr, host->h_length);
  10325. #endif /* h_addr */
  10326. #else  /* HADDRLIST */
  10327.             bcopy(host->h_addr, (caddr_t)&r_addr.sin_addr, host->h_length);
  10328. #endif /* HADDRLIST */
  10329. #ifdef COMMENT
  10330. #ifndef EXCELAN
  10331.             debug(F111,"BCOPY","host->h_addr",host->h_addr);
  10332. #endif /* EXCELAN */
  10333.             debug(F111,"BCOPY"," (caddr_t)&r_addr.sin_addr",
  10334.                   (caddr_t)&r_addr.sin_addr);
  10335.             debug(F111,"BCOPY"," r_addr.sin_addr.s_addr",
  10336.                   r_addr.sin_addr.s_addr);
  10337. #endif    /* COMMENT */
  10338.             debug(F111,"BCOPY","host->h_length",host->h_length);
  10339.         }
  10340.     }
  10341. #endif /* NOMHHOST */
  10342.  
  10343.     if (!dns) {
  10344. #ifdef INADDRX
  10345. /* inet_addr() is of type struct in_addr */
  10346.         struct in_addr ina;
  10347.         unsigned long uu;
  10348.         debug(F100,"http_open gethostbyname == NULL: INADDRX","",0);
  10349.         ina = inet_addr(http_ip[0]?http_ip:hostname);
  10350.         uu = *(unsigned int *)&ina;
  10351. #else /* Not INADDRX */
  10352. /* inet_addr() is unsigned long */
  10353.         unsigned long uu;
  10354.         debug(F100,"http_open gethostbyname == NULL: Not INADDRX","",0);
  10355.         uu = inet_addr(http_ip[0]?http_ip:hostname);
  10356. #endif /* INADDRX */
  10357.         debug(F101,"http_open uu","",uu);
  10358.         if (
  10359. #ifdef INADDR_NONE
  10360.             !(uu == INADDR_NONE || uu == (unsigned int) -1L)
  10361. #else   /* INADDR_NONE */
  10362.             uu != ((unsigned long)-1)
  10363. #endif /* INADDR_NONE */
  10364.             ) {
  10365.             r_addr.sin_addr.s_addr = uu;
  10366.             r_addr.sin_family = AF_INET;
  10367.         } else {
  10368. #ifdef VMS
  10369.             fprintf(stdout, "\r\n");    /* complete any previous message */
  10370. #endif /* VMS */
  10371.             fprintf(stderr, "Can't get address for %s\n",
  10372.                      http_ip[0]?http_ip:hostname);
  10373. #ifdef TGVORWIN
  10374.             debug(F101,"http_open can't get address","",socket_errno);
  10375. #else
  10376.             debug(F101,"http_open can't get address","",errno);
  10377. #endif /* TGVORWIN */
  10378.             errno = 0;                  /* Rather than mislead */
  10379.             return(-1);
  10380.         }
  10381.     }
  10382.  
  10383.     /* Get a file descriptor for the connection. */
  10384.  
  10385.     r_addr.sin_port = service->s_port;
  10386.     ckstrncpy(http_ip,(char *)inet_ntoa(r_addr.sin_addr),20);
  10387.     debug(F110,"http_open trying",http_ip,0);
  10388.     if (!quiet && *http_ip) {
  10389.         printf(" Trying %s... ", http_ip);
  10390.         fflush(stdout);
  10391.     }
  10392.  
  10393.     /* Loop to try additional IP addresses, if any. */
  10394.  
  10395.     do {
  10396. #ifdef EXCELAN
  10397.         send_socket.sin_family = AF_INET;
  10398.         send_socket.sin_addr.s_addr = 0;
  10399.         send_socket.sin_port = 0;
  10400.         if ((httpfd = socket(SOCK_STREAM, (struct sockproto *)0,
  10401.                             &send_socket, SO_REUSEADDR)) < 0)
  10402. #else  /* EXCELAN */
  10403.         if ((httpfd = socket(AF_INET, SOCK_STREAM, 0)) < 0)
  10404. #endif /* EXCELAN */
  10405.             {
  10406. #ifdef EXCELAN
  10407.                 experror("TCP socket error");
  10408. #else
  10409. #ifdef TGVORWIN
  10410. #ifdef OLD_TWG
  10411.                 errno = socket_errno;
  10412. #endif /* OLD_TWG */
  10413.                 socket_perror("TCP socket error");
  10414.                 debug(F101,"http_open socket error","",socket_errno);
  10415. #else
  10416.                 perror("TCP socket error");
  10417.                 debug(F101,"http_open socket error","",errno);
  10418. #endif /* TGVORWIN */
  10419. #endif /* EXCELAN */
  10420.                 return (-1);
  10421.             }
  10422.         errno = 0;
  10423.  
  10424.        /* If a specific TCP address on the local host is desired we */
  10425.        /* must bind it to the socket.                               */
  10426. #ifndef datageneral
  10427.          if (tcp_address) {
  10428.              int s_errno;
  10429.  
  10430.              debug(F110,"http_open binding socket to",tcp_address,0);
  10431.              bzero((char *)&sin,sizeof(sin));
  10432.              sin.sin_family = AF_INET;
  10433. #ifdef INADDRX
  10434.              inaddrx = inet_addr(tcp_address);
  10435.              sin.sin_addr.s_addr = *(unsigned long *)&inaddrx;
  10436. #else
  10437.              sin.sin_addr.s_addr = inet_addr(tcp_address);
  10438. #endif /* INADDRX */
  10439.              sin.sin_port = 0;
  10440.              if (bind(httpfd, (struct sockaddr *)&sin, sizeof(sin)) < 0) {
  10441.                  s_errno = socket_errno; /* Save error code */
  10442. #ifdef TCPIPLIB
  10443.                  socket_close(httpfd);
  10444. #else /* TCPIPLIB */
  10445.                  close(httpfd);
  10446. #endif /* TCPIPLIB */
  10447.                  httpfd = -1;
  10448.                  errno = s_errno;       /* and report this error */
  10449.                  debug(F101,"http_open bind errno","",errno);
  10450.                  return(-1);
  10451.              }
  10452.          }
  10453. #endif /* datageneral */
  10454.  
  10455. /* Now connect to the socket on the other end. */
  10456.  
  10457. #ifdef EXCELAN
  10458.         if (connect(httpfd, &r_addr) < 0)
  10459. #else
  10460. #ifdef NT
  10461.           WSASafeToCancel = 1;
  10462. #endif /* NT */
  10463.         if (connect(httpfd, (struct sockaddr *)&r_addr, sizeof(r_addr)) < 0)
  10464. #endif /* EXCELAN */
  10465.           {
  10466. #ifdef NT
  10467.               WSASafeToCancel = 0;
  10468. #endif /* NT */
  10469. #ifdef OS2
  10470.               i = socket_errno;
  10471. #else /* OS2 */
  10472. #ifdef TGVORWIN
  10473.               i = socket_errno;
  10474. #else
  10475.               i = errno;                /* Save error code */
  10476. #endif /* TGVORWIN */
  10477. #endif /* OS2 */
  10478. #ifdef HADDRLIST
  10479. #ifdef h_addr
  10480.               if (host && host->h_addr_list && host->h_addr_list[1]) {
  10481.                   perror("");
  10482.                   host->h_addr_list++;
  10483.                   bcopy(host->h_addr_list[0],
  10484.                         (caddr_t)&r_addr.sin_addr,
  10485.                         host->h_length);
  10486.  
  10487.                   ckstrncpy(http_ip,(char *)inet_ntoa(r_addr.sin_addr),20);
  10488.                   debug(F110,"http_open h_addr_list",http_ip,0);
  10489.                   if (!quiet && *http_ip) {
  10490.                       printf(" Trying %s... ", http_ip);
  10491.                       fflush(stdout);
  10492.                   }
  10493. #ifdef TCPIPLIB
  10494.                   socket_close(httpfd); /* Close it. */
  10495. #else
  10496.                   close(httpfd);
  10497. #endif /* TCPIPLIB */
  10498.                   continue;
  10499.               }
  10500. #endif /* h_addr */
  10501. #endif  /* HADDRLIST */
  10502.               http_close();
  10503.               httpfd = -1;
  10504.               errno = i;                /* And report this error */
  10505. #ifdef EXCELAN
  10506.               if (errno) experror("http_open connect");
  10507. #else
  10508. #ifdef TGVORWIN
  10509.               debug(F101,"http_open connect error","",socket_errno);
  10510.               /* if (errno) socket_perror("http_open connect"); */
  10511. #ifdef OLD_TWG
  10512.               errno = socket_errno;
  10513. #endif /* OLD_TWG */
  10514.               if (!quiet)
  10515.                 socket_perror("http_open connect");
  10516. #else /* TGVORWIN */
  10517.               debug(F101,"http_open connect errno","",errno);
  10518. #ifdef VMS
  10519.               if (!quiet)
  10520.                 perror("\r\nFailed");
  10521. #else
  10522.               if (!quiet)
  10523.                 perror("Failed");
  10524. #endif /* VMS */
  10525. #ifdef DEC_TCPIP
  10526.               if (!quiet)
  10527.                 perror("http_open connect");
  10528. #endif /* DEC_TCPIP */
  10529. #ifdef CMU_TCPIP
  10530.               if (!quiet)
  10531.                 perror("http_open connect");
  10532. #endif /* CMU_TCPIP */
  10533. #endif /* TGVORWIN */
  10534. #endif /* EXCELAN */
  10535.               return(-1);
  10536.           }
  10537. #ifdef NT
  10538.         WSASafeToCancel = 0;
  10539. #endif /* NT */
  10540.         isconnect = 1;
  10541.     } while (!isconnect);
  10542.  
  10543. #ifdef NON_BLOCK_IO
  10544.     on = 1;
  10545.     x = socket_ioctl(httpfd,FIONBIO,&on);
  10546.     debug(F101,"http_open FIONBIO","",x);
  10547. #endif /* NON_BLOCK_IO */
  10548.  
  10549.     /* We have succeeded in connecting to the HTTP PROXY.  So now we   */
  10550.     /* need to attempt to connect through the proxy to the actual host */
  10551.     /* If that is successful, we have to pretend that we made a direct */
  10552.     /* connection to the actual host.                                  */
  10553.  
  10554.     if ( tcp_http_proxy ) {
  10555. #ifdef OS2
  10556.         if (!agent) 
  10557.       agent = "Kermit 95";    /* Default user agent */
  10558. #else
  10559.         if (!agent) 
  10560.       agent = "C-Kermit";
  10561. #endif /* OS2 */
  10562.  
  10563.         if (http_connect(httpfd,
  10564.                          tcp_http_proxy_agent ? tcp_http_proxy_agent : agent,
  10565.                          NULL,
  10566.                          tcp_http_proxy_user,
  10567.                          tcp_http_proxy_pwd,
  10568.                          0,
  10569.                          proxycopy
  10570.                          ) < 0) {
  10571.             http_close();
  10572.             return(-1);
  10573.         }
  10574.     }
  10575.  
  10576. #ifdef SO_OOBINLINE
  10577.     /* See note on SO_OOBINLINE in netopen() */
  10578. #ifdef datageneral
  10579.     setsockopt(httpfd, SOL_SOCKET, SO_OOBINLINE,(char *) &on, sizeof on);
  10580. #else
  10581. #ifdef BSD43
  10582.     setsockopt(httpfd, SOL_SOCKET, SO_OOBINLINE,(char *) &on, sizeof on);
  10583. #else
  10584. #ifdef OSF1
  10585.     setsockopt(httpfd, SOL_SOCKET, SO_OOBINLINE,(char *) &on, sizeof on);
  10586. #else
  10587. #ifdef POSIX
  10588.     setsockopt(httpfd, SOL_SOCKET, SO_OOBINLINE,(char *) &on, sizeof on);
  10589. #else
  10590. #ifdef MOTSV88R4
  10591.     setsockopt(httpfd, SOL_SOCKET, SO_OOBINLINE,(char *) &on, sizeof on);
  10592. #else
  10593. #ifdef SOLARIS
  10594. /*
  10595.   Maybe this applies to all SVR4 versions, but the other (else) way has been
  10596.   compiling and working fine on all the others, so best not to change it.
  10597. */
  10598.     setsockopt(httpfd, SOL_SOCKET, SO_OOBINLINE,(char *) &on, sizeof on);
  10599. #else
  10600. #ifdef OSK
  10601.     setsockopt(httpfd, SOL_SOCKET, SO_OOBINLINE,(char *) &on, sizeof on);
  10602. #else
  10603. #ifdef OS2
  10604.     {
  10605.         int rc;
  10606.         rc = setsockopt(httpfd,
  10607.                         SOL_SOCKET,
  10608.                         SO_OOBINLINE,
  10609.                         (char *) &on,
  10610.                         sizeof on
  10611.                         );
  10612.         debug(F111,"setsockopt SO_OOBINLINE",on ? "on" : "off" ,rc);
  10613.     }
  10614. #else
  10615. #ifdef VMS /* or, at least, VMS with gcc */
  10616.     setsockopt(httpfd, SOL_SOCKET, SO_OOBINLINE,(char *) &on, sizeof on);
  10617. #else
  10618. #ifdef CLIX
  10619.     setsockopt(httpfd, SOL_SOCKET, SO_OOBINLINE,(char *) &on, sizeof on);
  10620. #else
  10621.     setsockopt(httpfd, SOL_SOCKET, SO_OOBINLINE, &on, sizeof on);
  10622. #endif /* CLIX */
  10623. #endif /* VMS */
  10624. #endif /* OS2 */
  10625. #endif /* OSK */
  10626. #endif /* SOLARIS */
  10627. #endif /* MOTSV88R4 */
  10628. #endif /* POSIX */
  10629. #endif /* BSD43 */
  10630. #endif /* OSF1 */
  10631. #endif /* datageneral */
  10632. #endif /* SO_OOBINLINE */
  10633.  
  10634. #ifndef NOTCPOPTS
  10635. #ifndef datageneral
  10636. #ifdef SOL_SOCKET
  10637. #ifdef TCP_NODELAY
  10638.     no_delay(ttyfd,tcp_nodelay);
  10639. #endif /* TCP_NODELAY */
  10640. #ifdef SO_KEEPALIVE
  10641.     keepalive(ttyfd,tcp_keepalive);
  10642. #endif /* SO_KEEPALIVE */
  10643. #ifdef SO_LINGER
  10644.     ck_linger(ttyfd,tcp_linger, tcp_linger_tmo);
  10645. #endif /* SO_LINGER */
  10646. #ifdef SO_SNDBUF
  10647.     sendbuf(ttyfd,tcp_sendbuf);
  10648. #endif /* SO_SNDBUF */
  10649. #ifdef SO_RCVBUF
  10650.     recvbuf(ttyfd,tcp_recvbuf);
  10651. #endif /* SO_RCVBUF */
  10652. #endif /* SOL_SOCKET */
  10653. #endif /* datageneral */
  10654. #endif /* NOTCPOPTS */
  10655.  
  10656. #ifndef datageneral
  10657.     /* Find out our own IP address. */
  10658.     /* We need the l_addr structure for [E]KLOGIN. */
  10659.     l_slen = sizeof(l_addr);
  10660.     bzero((char *)&l_addr, l_slen);
  10661. #ifndef EXCELAN
  10662.     if (!getsockname(httpfd, (struct sockaddr *)&l_addr, &l_slen)) {
  10663.         char * s = (char *)inet_ntoa(l_addr.sin_addr);
  10664.         ckstrncpy(myipaddr, s, 20);
  10665.         debug(F110,"getsockname",myipaddr,0);
  10666.     }
  10667. #endif /* EXCELAN */
  10668. #endif /* datageneral */
  10669.  
  10670. /* See note in netopen() on Reverse DNS lookups */
  10671.      if (tcp_rdns == SET_ON) {
  10672. #ifdef NT
  10673.         if (isWin95())
  10674.           sleep(1);
  10675. #endif /* NT */
  10676.         if (!quiet) {
  10677.             printf(" Reverse DNS Lookup... ");
  10678.             fflush(stdout);
  10679.         }
  10680.         if (host = gethostbyaddr((char *)&r_addr.sin_addr,4,PF_INET)) {
  10681.             char * s;
  10682.             host = ck_copyhostent(host);
  10683.             debug(F100,"http_open gethostbyname != NULL","",0);
  10684.             if (!quiet) {
  10685.                 printf("(OK)\n");
  10686.                 fflush(stdout);
  10687.             }
  10688.             s = host->h_name;
  10689.             if (!s) {                   /* This can happen... */
  10690.                 debug(F100,"http_open host->h_name is NULL","",0);
  10691.                 s = "";
  10692.             }
  10693.             /* Something is wrong with inet_ntoa() on HPUX 10.xx */
  10694.             /* The compiler says "Integral value implicitly converted to */
  10695.             /* pointer in assignment."  The prototype is right there */
  10696.             /* in <arpa/inet.h> so what's the problem? */
  10697.             /* Ditto in HP-UX 5.x, but not 8.x or 9.x... */
  10698.             if (!*s) {                  /* No name so substitute the address */
  10699.                 debug(F100,"http_open host->h_name is empty","",0);
  10700.                 s = inet_ntoa(r_addr.sin_addr); /* Convert address to string */
  10701.                 if (!s)                 /* Trust No 1 */
  10702.                   s = "";
  10703.                 if (*s) {               /* If it worked, use this string */
  10704.                     ckstrncpy(http_ip,s,20);
  10705.                 }
  10706.                 s = http_ip;             /* Otherwise stick with the IP */
  10707.                 if (!*s)                 /* or failing that */
  10708.                   s = http_host_port;    /* the name we were called with. */
  10709.             }
  10710.             if (*s)                     /* return the rdns name */
  10711.                 ckmakmsg(rdns_name,rdns_len,s,":",svcname,NULL);
  10712.  
  10713.             if (!quiet && *s
  10714. #ifndef NOICP
  10715.                 && !doconx
  10716. #endif /* NOICP */
  10717.                 ) {
  10718.                 printf(" %s connected on port %s\n",s,
  10719.                        ckuitoa(ntohs(service->s_port)));
  10720. #ifdef BETADEBUG
  10721.                 /* This is simply for testing the DNS entries */
  10722.                 if (host->h_aliases) {
  10723.                     char ** a = host->h_aliases;
  10724.                     while (*a) {
  10725.                         printf(" alias => %s\n",*a);
  10726.                         a++;
  10727.                     }
  10728.                 }
  10729. #endif /* BETADEBUG */
  10730.             }
  10731.         } else {
  10732.             if (!quiet) printf("Failed.\n");
  10733.         }
  10734.     } else if (!quiet) printf("(OK)\n");
  10735.     if (!quiet) fflush(stdout);
  10736.  
  10737.  
  10738.     if ( tcp_http_proxy ) {
  10739.         /* Erase the IP address since we cannot reuse it */
  10740.         http_ip[0] = '\0';
  10741.     } else {
  10742.         /* This should already have been done but just in case */
  10743.         ckstrncpy(http_ip,(char *)inet_ntoa(r_addr.sin_addr),20);
  10744.     }
  10745.     makestr(&http_agent,agent);
  10746.  
  10747. #ifdef CK_SSL
  10748.     if (use_ssl && ck_ssleay_is_installed()) {
  10749.         if (!ssl_http_init(hostname)) {
  10750.             if (bio_err!=NULL) {
  10751.                 BIO_printf(bio_err,"ssl_tn_init() failed\n");
  10752.                 ERR_print_errors(bio_err);
  10753.             } else {
  10754.                 fflush(stderr);
  10755.                 fprintf(stderr,"ssl_tn_init() failed\n");
  10756.                 ERR_print_errors_fp(stderr);
  10757.             }
  10758.             http_close();
  10759.             return(-1);
  10760.         } else if ( ck_ssl_http_client(httpfd,hostname) < 0 ) {
  10761.             http_close();
  10762.             return(-1);
  10763.         }
  10764.     }
  10765. #endif /* CK_SSL */
  10766. #endif /* TCPSOCKET */
  10767.  
  10768.     return(0);                          /* Done. */
  10769. }
  10770.  
  10771. int
  10772. #ifdef CK_ANSIC
  10773. http_close(VOID)
  10774. #else /* CK_ANSIC */
  10775. http_close()
  10776. #endif /* CK_ANSIC */
  10777. {
  10778.     int x = 0;
  10779.     debug(F101,"http_close","",httpfd);
  10780.  
  10781. #ifdef HTTP_BUFFERING
  10782.     http_count = 0;
  10783.     http_bufp = 0;
  10784. #endif    /* HTTP_BUFFERING */
  10785.  
  10786.     if (httpfd == -1)                    /* Was open? */
  10787.       return(0);                        /* Wasn't. */
  10788.  
  10789. #ifndef OS2
  10790.     if (httpfd > -1)                     /* Was. */
  10791. #endif /* OS2 */
  10792.       {
  10793. #ifdef CK_SSL
  10794.           if (tls_http_active_flag) {
  10795.               if (ssl_debug_flag)
  10796.                 BIO_printf(bio_err,"calling SSL_shutdown\n");
  10797.               SSL_shutdown(tls_http_con);
  10798.               tls_http_active_flag = 0;
  10799.           }
  10800. #endif /* CK_SSL */
  10801. #ifdef TCPIPLIB
  10802.           x = socket_close(httpfd);      /* Close it. */
  10803. #else
  10804. #ifndef OS2
  10805.           x = close(httpfd);
  10806. #endif /* OS2 */
  10807. #endif /* TCPIPLIB */
  10808.       }
  10809.     httpfd = -1;                          /* Mark it as closed. */
  10810.     /* do not erase http_host_port, http_ip, http_port so they */
  10811.     /* can be used by http_reopen() */
  10812.     return(x);
  10813. }
  10814.  
  10815.  
  10816. /* http_tol()
  10817.  * Call with s = pointer to string, n = length.
  10818.  * Returns number of bytes actually written on success, or
  10819.  * -1 on i/o error, -2 if called improperly.
  10820.  */
  10821.  
  10822. int
  10823. http_tol(s,n) CHAR *s; int n; {
  10824.     int count = 0;
  10825.     int len = n;
  10826.     int try = 0;
  10827.  
  10828.     if (httpfd == -1) {
  10829.         debug(F100,"http_tol socket is closed","",0);
  10830.         return -1;
  10831.     }
  10832.     debug(F101,"http_tol TCPIPLIB ttnet","",ttnet);
  10833. #ifdef COMMENT
  10834.     hexdump("http_tol",s,n);
  10835. #endif /* COMMENT */
  10836.  
  10837. #ifdef CK_SSL
  10838.     if (tls_http_active_flag) {
  10839.         int error, r;
  10840.         /* Write using SSL */
  10841.       ssl_retry:
  10842.           r = SSL_write(tls_http_con, s, len /* >1024?1024:len */);
  10843.         switch (SSL_get_error(tls_http_con,r)) {
  10844.           case SSL_ERROR_NONE:
  10845.             debug(F111,"http_tol","SSL_write",r);
  10846.             if ( r == len )
  10847.                 return(n);
  10848.              s += r;
  10849.              len -= r;
  10850.              goto ssl_retry;
  10851.           case SSL_ERROR_WANT_WRITE:
  10852.             debug(F100,"http_tol SSL_ERROR_WANT_WRITE","",0);
  10853.           return(-1);
  10854.           case SSL_ERROR_WANT_READ:
  10855.             debug(F100,"http_tol SSL_ERROR_WANT_READ","",0);
  10856.             return(-1);
  10857.           case SSL_ERROR_SYSCALL:
  10858.               if ( r == 0 ) { /* EOF */
  10859.                   http_close();
  10860.                   return(-2);
  10861.               } else {
  10862.                   int rc = -1;
  10863. #ifdef NT
  10864.                   int gle = GetLastError();
  10865.                   debug(F111,"http_tol SSL_ERROR_SYSCALL",
  10866.                          "GetLastError()",gle);
  10867.                   rc = os2socketerror(gle);
  10868.                   if (rc == -1)
  10869.                       rc = -2;
  10870.                   else if ( rc == -2 )
  10871.                       return -1;
  10872. #endif /* NT */
  10873.                   return(rc);
  10874.               }
  10875.           case SSL_ERROR_WANT_X509_LOOKUP:
  10876.             debug(F100,"http_tol SSL_ERROR_WANT_X509_LOOKUP","",0);
  10877.             http_close();
  10878.             return(-2);
  10879.           case SSL_ERROR_SSL:
  10880.             debug(F100,"http_tol SSL_ERROR_SSL","",0);
  10881.             http_close();
  10882.             return(-2);
  10883.           case SSL_ERROR_ZERO_RETURN:
  10884.             debug(F100,"http_tol SSL_ERROR_ZERO_RETURN","",0);
  10885.             http_close();
  10886.             return(-2);
  10887.           default:
  10888.             debug(F100,"http_tol SSL_ERROR_?????","",0);
  10889.             http_close();
  10890.             return(-2);
  10891.         }
  10892.     }
  10893. #endif /* CK_SSL */
  10894.  
  10895.   http_tol_retry:
  10896.     try++;                              /* Increase the try counter */
  10897.  
  10898.     {
  10899. #ifdef BSDSELECT
  10900.         fd_set wfds;
  10901.         struct timeval tv;
  10902.  
  10903.         debug(F101,"http_tol BSDSELECT","",0);
  10904.         tv.tv_usec = 0L;
  10905.         tv.tv_sec=30;
  10906. #ifdef NT
  10907.         WSASafeToCancel = 1;
  10908. #endif /* NT */
  10909. #ifdef STREAMING
  10910.       do_select:
  10911. #endif /* STREAMING */
  10912.         FD_ZERO(&wfds);
  10913.         FD_SET(httpfd, &wfds);
  10914.         if (select(FD_SETSIZE, NULL,
  10915. #ifdef __DECC
  10916. #ifndef __DECC_VER
  10917.                     (int *)
  10918. #endif /* __DECC_VER */
  10919. #endif /* __DECC */
  10920.                    &wfds, NULL, &tv) < 0) {
  10921.             int s_errno = socket_errno;
  10922.             debug(F101,"http_tol select failed","",s_errno);
  10923. #ifdef BETADEBUG
  10924.             printf("http_tol select failed: %d\n", s_errno);
  10925. #endif /* BETADEBUG */
  10926. #ifdef NT
  10927.             WSASafeToCancel = 0;
  10928.             if (!win95selectbug)
  10929. #endif /* NT */
  10930.               return(-1);
  10931.         }
  10932.         if (!FD_ISSET(httpfd, &wfds)) {
  10933. #ifdef STREAMING
  10934.             if (streaming)
  10935.               goto do_select;
  10936. #endif /* STREAMING */
  10937.             debug(F111,"http_tol","!FD_ISSET",ttyfd);
  10938. #ifdef NT
  10939.             WSASafeToCancel = 0;
  10940.             if (!win95selectbug)
  10941. #endif /* NT */
  10942.               return(-1);
  10943.         }
  10944. #ifdef NT
  10945.         WSASafeToCancel = 0;
  10946. #endif /* NT */
  10947. #else /* BSDSELECT */
  10948. #ifdef IBMSELECT
  10949.         {
  10950.             int tries = 0;
  10951.             debug(F101,"http_tol IBMSELECT","",0);
  10952.             while (select(&httpfd, 0, 1, 0, 1000) != 1) {
  10953.                 int count;
  10954.                 if (tries++ >= 60) {
  10955.                     /* if after 60 seconds we can't get permission to write */
  10956.                     debug(F101,"http_tol select failed","",socket_errno);
  10957.                     return(-1);
  10958.                 }
  10959. #ifdef COMMENT
  10960.                 if ((count = http_tchk()) < 0) {
  10961.                     debug(F111,"http_tol","http_tchk()",count);
  10962.                     return(count);
  10963.                 }
  10964. #endif /* COMMENT */
  10965.             }
  10966.         }
  10967. #endif /* IBMSELECT */
  10968. #endif /* BSDSELECT */
  10969. #ifdef TCPIPLIB
  10970.         if ((count = socket_write(httpfd,s,n)) < 0) {
  10971.             int s_errno = socket_errno; /* maybe a function */
  10972.             debug(F101,"http_tol socket_write error","",s_errno);
  10973. #ifdef OS2
  10974.             if (os2socketerror(s_errno) < 0)
  10975.               return(-2);
  10976. #endif /* OS2 */
  10977.             return(-1);                 /* Call it an i/o error */
  10978.         }
  10979. #else /* TCPIPLIB */
  10980.         if ((count = write(httpfd,s,n)) < 0) {
  10981.             debug(F101,"http_tol socket_write error","",errno);
  10982.             return(-1);                 /* Call it an i/o error */
  10983.         }
  10984. #endif /* TCPIPLIB */
  10985.         if (count < n) {
  10986.             debug(F111,"http_tol socket_write",s,count);
  10987.             if (try > 25) {
  10988.                 /* don't try more than 25 times */
  10989.                 debug(F100,"http_tol tried more than 25 times","",0);
  10990.                 return(-1);
  10991.             }
  10992.             if (count > 0) {
  10993.                 s += count;
  10994.                 n -= count;
  10995.             }
  10996.             debug(F111,"http_tol retry",s,n);
  10997.             goto http_tol_retry;
  10998.         } else {
  10999.             debug(F111,"http_tol socket_write",s,count);
  11000.             return(len); /* success - return total length */
  11001.         }
  11002.     }
  11003. }
  11004.  
  11005. int
  11006. http_inc(timo) int timo; {
  11007.     int x=-1; unsigned char c;             /* The locals. */
  11008.  
  11009.     if (httpfd == -1) {
  11010. #ifdef HTTP_BUFFERING
  11011.     http_count = 0;
  11012.       http_bufp = 0;
  11013. #endif    /* HTTP_BUFFERING */
  11014.         debug(F100,"http_inc socket is closed","",0);
  11015.         return(-2);
  11016.     }
  11017.  
  11018. #ifdef CK_SSL
  11019.     /*
  11020.      * In the case of OpenSSL, it is possible that there is still
  11021.      * data waiting in the SSL session buffers that has not yet
  11022.      * been read by Kermit.  If this is the case we must process
  11023.      * it without calling select() because select() will not return
  11024.      * with an indication that there is data to be read from the
  11025.      * socket.  If there is no data pending in the SSL session
  11026.      * buffers then fall through to the select() code and wait for
  11027.      * some data to arrive.
  11028.      */
  11029.     if (tls_http_active_flag) {
  11030.         int error;
  11031.  
  11032.         x = SSL_pending(tls_http_con);
  11033.         if (x < 0) {
  11034.             debug(F111,"http_inc","SSL_pending error",x);
  11035.             http_close();
  11036.             return(-1);
  11037.         } else if ( x > 0 ) {
  11038.       ssl_read:
  11039.             x = SSL_read(tls_http_con, &c, 1);
  11040.             error = SSL_get_error(tls_http_con,x);
  11041.             switch (error) {
  11042.             case SSL_ERROR_NONE:
  11043.                 debug(F111,"http_inc SSL_ERROR_NONE","x",x);
  11044.                 if (x > 0) {
  11045. #ifdef OS2
  11046.                     ReleaseTCPIPMutex();
  11047. #endif /* OS2 */
  11048.                     return(c);          /* Return character. */
  11049.                 } else if (x < 0) {
  11050. #ifdef OS2
  11051.                     ReleaseTCPIPMutex();
  11052. #endif /* OS2 */
  11053.                     return(-1);
  11054.                 } else {
  11055.                     http_close();
  11056. #ifdef OS2
  11057.                     ReleaseTCPIPMutex();
  11058. #endif /* OS2 */
  11059.                     return(-2);
  11060.                 }
  11061.             case SSL_ERROR_WANT_WRITE:
  11062.                 debug(F100,"http_inc SSL_ERROR_WANT_WRITE","",0);
  11063. #ifdef OS2
  11064.                 ReleaseTCPIPMutex();
  11065. #endif /* OS2 */
  11066.                 return(-1);
  11067.             case SSL_ERROR_WANT_READ:
  11068.                 debug(F100,"http_inc SSL_ERROR_WANT_READ","",0);
  11069. #ifdef OS2
  11070.                 ReleaseTCPIPMutex();
  11071. #endif /* OS2 */
  11072.                 return(-1);
  11073.             case SSL_ERROR_SYSCALL:
  11074.                 if ( x == 0 ) { /* EOF */
  11075.                     http_close();
  11076. #ifdef OS2
  11077.                     ReleaseTCPIPMutex();
  11078. #endif /* OS2 */
  11079.                     return(-2);
  11080.                 } else {
  11081.                     int rc = -1;
  11082. #ifdef NT
  11083.                     int gle = GetLastError();
  11084.                     debug(F111,"http_inc SSL_ERROR_SYSCALL",
  11085.                            "GetLastError()",gle);
  11086.                     rc = os2socketerror(gle);
  11087.                     if (rc == -1)
  11088.                         rc = -2;
  11089.                     else if ( rc == -2 )
  11090.                         rc = -1;
  11091. #endif /* NT */
  11092. #ifdef OS2
  11093.                     ReleaseTCPIPMutex();
  11094. #endif /* OS2 */
  11095.                     return(rc);
  11096.                 }
  11097.             case SSL_ERROR_WANT_X509_LOOKUP:
  11098.                 debug(F100,"http_inc SSL_ERROR_WANT_X509_LOOKUP","",0);
  11099.                 http_close();
  11100. #ifdef OS2
  11101.                 ReleaseTCPIPMutex();
  11102. #endif /* OS2 */
  11103.                 return(-2);
  11104.             case SSL_ERROR_SSL:
  11105.                 debug(F100,"http_inc SSL_ERROR_SSL","",0);
  11106. #ifdef COMMENT
  11107.                 http_close();
  11108. #endif /* COMMENT */
  11109. #ifdef OS2
  11110.                 ReleaseTCPIPMutex();
  11111. #endif /* OS2 */
  11112.                 return(-2);
  11113.             case SSL_ERROR_ZERO_RETURN:
  11114.                 debug(F100,"http_inc SSL_ERROR_ZERO_RETURN","",0);
  11115.                 http_close();
  11116. #ifdef OS2
  11117.                 ReleaseTCPIPMutex();
  11118. #endif /* OS2 */
  11119.                 return(-2);
  11120.             default:
  11121.                 debug(F100,"http_inc SSL_ERROR_?????","",0);
  11122.                 http_close();
  11123. #ifdef OS2
  11124.                 ReleaseTCPIPMutex();
  11125. #endif /* OS2 */
  11126.                 return(-2);
  11127.             }
  11128.         }
  11129.     }
  11130. #endif /* CK_SSL */
  11131.  
  11132. #ifdef HTTP_BUFFERING
  11133.     /* Skip all the select() stuff if we have bytes buffered locally */
  11134.     if (http_count > 0)
  11135.       goto getfrombuffer;
  11136. #endif    /* HTTP_BUFFERING */
  11137.  
  11138.     {
  11139. #ifdef BSDSELECT
  11140.         fd_set rfds;
  11141.         struct timeval tv;
  11142.         int timeout = timo < 0 ? -timo : 1000 * timo;
  11143.         debug(F101,"http_inc BSDSELECT","",timo);
  11144.  
  11145.         for ( ; timeout >= 0; timeout -= (timo ? 100 : 0)) {
  11146.             int rc;
  11147.             debug(F111,"http_inc","timeout",timeout);
  11148.             /* Don't move select() initialization out of the loop. */
  11149.             FD_ZERO(&rfds);
  11150.             FD_SET(httpfd, &rfds);
  11151.             tv.tv_sec  = tv.tv_usec = 0L;
  11152.             if (timo)
  11153.                 tv.tv_usec = (long) 100000L;
  11154.             else
  11155.                 tv.tv_sec = 30;
  11156. #ifdef NT
  11157.             WSASafeToCancel = 1;
  11158. #endif /* NT */
  11159.             rc = select(FD_SETSIZE,
  11160. #ifndef __DECC
  11161.                          (fd_set *)
  11162. #endif /* __DECC */
  11163.                          &rfds, NULL, NULL, &tv);
  11164.             if (rc < 0) {
  11165.                 int s_errno = socket_errno;
  11166.                 debug(F111,"http_inc","select",rc);
  11167.                 debug(F111,"http_inc","socket_errno",s_errno);
  11168. #ifdef HTTP_BUFFERING
  11169.         http_count = 0;
  11170.         http_bufp = 0;
  11171. #endif    /* HTTP_BUFFERING */
  11172.                 if (s_errno)
  11173.                     return(-1);
  11174.             }
  11175.             debug(F111,"http_inc","select",rc);
  11176. #ifdef NT
  11177.             WSASafeToCancel = 0;
  11178. #endif /* NT */
  11179.             if (FD_ISSET(httpfd, &rfds)) {
  11180.                 x = 0;
  11181.                 break;
  11182.             } else {
  11183.                 /* If waiting forever we have no way of knowing if the */
  11184.                 /* socket closed so try writing a 0-length TCP packet  */
  11185.                 /* which should force an error if the socket is closed */
  11186.                 if (!timo) {
  11187. #ifdef TCPIPLIB
  11188.                     if ((rc = socket_write(httpfd,"",0)) < 0) {
  11189. #ifdef HTTP_BUFFERING
  11190.             http_count = 0;
  11191.             http_bufp = 0;
  11192. #endif    /* HTTP_BUFFERING */
  11193.                         int s_errno = socket_errno;
  11194.                         debug(F101,"http_inc socket_write error","",s_errno);
  11195. #ifdef OS2
  11196.                         if (os2socketerror(s_errno) < 0)
  11197.                             return(-2);
  11198. #endif /* OS2 */
  11199.                         return(-1); /* Call it an i/o error */
  11200.                     }
  11201. #else /* TCPIPLIB */
  11202.                     if ((rc = write(httpfd,"",0)) < 0) {
  11203. #ifdef HTTP_BUFFERING
  11204.             http_count = 0;
  11205.             http_bufp = 0;
  11206. #endif    /* HTTP_BUFFERING */
  11207.                         debug(F101,"http_inc socket_write error","",errno);
  11208.                         return(-1); /* Call it an i/o error */
  11209.                     }
  11210. #endif /* TCPIPLIB */
  11211.                 }
  11212.                 continue;
  11213.             }
  11214.         }
  11215. #ifdef NT
  11216.         WSASafeToCancel = 0;
  11217. #endif /* NT */
  11218. #else /* !BSDSELECT */
  11219. #ifdef IBMSELECT
  11220.  /*
  11221.   Was used by OS/2, currently not used, but might come in handy some day...
  11222.   ... and it came in handy!  For our TCP/IP layer, it avoids all the fd_set
  11223.   and timeval stuff since this is the only place where it is used.
  11224.  */
  11225.         int socket = httpfd;
  11226.         int timeout = timo < 0 ? -timo : 1000 * timo;
  11227.  
  11228.         debug(F101,"http_inc IBMSELECT","",timo);
  11229.         for ( ; timeout >= 0; timeout -= (timo ? 100 : 0)) {
  11230.             if (select(&socket, 1, 0, 0, 100L) == 1) {
  11231.                 x = 0;
  11232.                 break;
  11233.             }
  11234.         }
  11235. #else /* !IBMSELECT */
  11236.         SELECT is required for this code
  11237. #endif /* IBMSELECT */
  11238. #endif /* BSDSELECT */
  11239.     }
  11240.  
  11241.     if (timo && x < 0) {        /* select() timed out */
  11242. #ifdef HTTP_BUFFERING
  11243.     http_count = 0;
  11244.     http_bufp = 0;
  11245. #endif    /* HTTP_BUFFERING */
  11246.         debug(F100,"http_inc select() timed out","",0);
  11247.         return(-1); /* Call it an i/o error */
  11248.     }
  11249.  
  11250. #ifdef CK_SSL
  11251.         if ( tls_http_active_flag ) {
  11252.             int error;
  11253.       ssl_read2:
  11254.             x = SSL_read(tls_http_con, &c, 1);
  11255.             error = SSL_get_error(tls_http_con,x);
  11256.             switch (error) {
  11257.             case SSL_ERROR_NONE:
  11258.                 debug(F111,"http_inc SSL_ERROR_NONE","x",x);
  11259.                 if (x > 0) {
  11260. #ifdef OS2
  11261.                     ReleaseTCPIPMutex();
  11262. #endif /* OS2 */
  11263.                     return(c);          /* Return character. */
  11264.                 } else if (x < 0) {
  11265. #ifdef OS2
  11266.                     ReleaseTCPIPMutex();
  11267. #endif /* OS2 */
  11268.                     return(-1);
  11269.                 } else {
  11270.                     http_close();
  11271. #ifdef OS2
  11272.                     ReleaseTCPIPMutex();
  11273. #endif /* OS2 */
  11274.                     return(-2);
  11275.                 }
  11276.             case SSL_ERROR_WANT_WRITE:
  11277.                 debug(F100,"http_inc SSL_ERROR_WANT_WRITE","",0);
  11278. #ifdef OS2
  11279.                 ReleaseTCPIPMutex();
  11280. #endif /* OS2 */
  11281.                 return(-1);
  11282.             case SSL_ERROR_WANT_READ:
  11283.                 debug(F100,"http_inc SSL_ERROR_WANT_READ","",0);
  11284. #ifdef OS2
  11285.                 ReleaseTCPIPMutex();
  11286. #endif /* OS2 */
  11287.                 return(-1);
  11288.             case SSL_ERROR_SYSCALL:
  11289.                 if ( x == 0 ) { /* EOF */
  11290.                     http_close();
  11291. #ifdef OS2
  11292.                     ReleaseTCPIPMutex();
  11293. #endif /* OS2 */
  11294.                     return(-2);
  11295.                 } else {
  11296.                     int rc = -1;
  11297. #ifdef NT
  11298.                     int gle = GetLastError();
  11299.                     debug(F111,"http_inc SSL_ERROR_SYSCALL",
  11300.                            "GetLastError()",gle);
  11301.                     rc = os2socketerror(gle);
  11302.                     if (rc == -1)
  11303.                         rc = -2;
  11304.                     else if ( rc == -2 )
  11305.                         rc = -1;
  11306. #endif /* NT */
  11307. #ifdef OS2
  11308.                     ReleaseTCPIPMutex();
  11309. #endif /* OS2 */
  11310.                     return(rc);
  11311.                 }
  11312.             case SSL_ERROR_WANT_X509_LOOKUP:
  11313.                 debug(F100,"http_inc SSL_ERROR_WANT_X509_LOOKUP","",0);
  11314.                 http_close();
  11315. #ifdef OS2
  11316.                 ReleaseTCPIPMutex();
  11317. #endif /* OS2 */
  11318.                 return(-2);
  11319.             case SSL_ERROR_SSL:
  11320.                 debug(F100,"http_inc SSL_ERROR_SSL","",0);
  11321. #ifdef COMMENT
  11322.                 http_close();
  11323. #endif /* COMMENT */
  11324. #ifdef OS2
  11325.                 ReleaseTCPIPMutex();
  11326. #endif /* OS2 */
  11327.                 return(-2);
  11328.             case SSL_ERROR_ZERO_RETURN:
  11329.                 debug(F100,"http_inc SSL_ERROR_ZERO_RETURN","",0);
  11330.                 http_close();
  11331. #ifdef OS2
  11332.                 ReleaseTCPIPMutex();
  11333. #endif /* OS2 */
  11334.                 return(-2);
  11335.             default:
  11336.                 debug(F100,"http_inc SSL_ERROR_?????","",0);
  11337.                 http_close();
  11338. #ifdef OS2
  11339.                 ReleaseTCPIPMutex();
  11340. #endif /* OS2 */
  11341.                 return(-2);
  11342.             }
  11343.         }
  11344. #endif /* CK_SSL */
  11345.  
  11346. #ifdef HTTP_BUFFERING
  11347. /*
  11348.   Buffering code added by fdc 15 Dec 2005 for non-SSL case only because HTTP
  11349.   GETs were orders of magnitude too slow due to the single-byte read()s.  The
  11350.   file-descriptor swapping is pretty gross, but the more elegant solution
  11351.   (calling a nettchk() like routine with the fd as a parameter) doesn't work,
  11352.   because nettchk() relies on too many other routines that, like itself, are
  11353.   hardwired for ttyfd.
  11354. */
  11355.   getfrombuffer:
  11356.     if (--http_count >= 0) {
  11357.         c = http_inbuf[http_bufp++];
  11358.         x = 1;
  11359.     } else {
  11360.         int savefd;
  11361.         savefd = ttyfd;
  11362.         ttyfd = httpfd;
  11363.         x = nettchk();
  11364.         ttyfd = savefd;        
  11365.         debug(F101,"http_inc nettchk","",x);
  11366.         if (x > HTTP_INBUFLEN)
  11367.           x = HTTP_INBUFLEN;
  11368. #ifdef TCPIPLIB
  11369.         x = socket_read(httpfd,http_inbuf,x);
  11370. #else  /* Not TCPIPLIB */
  11371.         x = read(httpfd,http_inbuf,x);
  11372. #endif    /* TCPIPLIB */
  11373.         http_count = 0;
  11374.         http_bufp = 0;
  11375.         if (x > 0) {
  11376.         c = http_inbuf[http_bufp++];
  11377.         http_count = x - 1;
  11378.         }
  11379.     }
  11380. #else  /* Not HTTP_BUFFERING */
  11381. #ifdef TCPIPLIB
  11382.         x = socket_read(httpfd,&c,1);
  11383. #else  /* Not TCPIPLIB */
  11384.         x = read(httpfd,&c,1);
  11385. #endif    /* TCPIPLIB */
  11386. #endif    /* HTTP_BUFFERING */
  11387.  
  11388.         if (x <= 0) {
  11389.             int s_errno = socket_errno;
  11390.             debug(F101,"ttbufr socket_read","",x);
  11391.             debug(F101,"ttbufr socket_errno","",s_errno);
  11392. #ifdef OS2
  11393.             if (x == 0 || os2socketerror(s_errno) < 0) {
  11394.                 http_close();
  11395.                 ReleaseTCPIPMutex();
  11396.                 return(-2);
  11397.             }
  11398.             ReleaseTCPIPMutex();
  11399.             return(-1);
  11400. #else /* OS2 */
  11401.             http_close();                      /* *** *** */
  11402.             return(-2);
  11403. #endif /* OS2 */
  11404.         }
  11405.         return(c);
  11406. }
  11407.  
  11408. void
  11409. #ifdef CK_ANSIC
  11410. http_set_code_reply(char * msg)
  11411. #else
  11412. http_set_code_reply(msg)
  11413.     char * msg;
  11414. #endif /* CK_ANSIC */
  11415. {
  11416.     char * p = msg;
  11417.     char buf[16];
  11418.     int i=0;
  11419.  
  11420.     while ( *p != SP && *p != NUL ) {
  11421.         buf[i] = *p;
  11422.         p++;
  11423.         i++;
  11424.     }
  11425.  
  11426.     http_code = atoi(buf);
  11427.  
  11428.     while ( *p == SP )
  11429.         p++;
  11430.  
  11431.     ckstrncpy(http_reply_str,p,HTTPBUFLEN);
  11432. }
  11433.  
  11434. int
  11435. #ifdef CK_ANSIC
  11436. http_get(char * agent, char ** hdrlist, char * user,
  11437.          char * pwd, char array, char * local, char * remote,
  11438.          int stdio)
  11439. #else
  11440. http_get(agent, hdrlist, user, pwd, array, local, remote, stdio)
  11441.     char * agent; char ** hdrlist; char * user;
  11442.     char * pwd; char array; char * local; char * remote;
  11443.     int stdio;
  11444. #endif /* CK_ANSIC */
  11445. {
  11446.     char * request = NULL;
  11447.     int    i, j, len = 0, hdcnt = 0, rc = 0;
  11448.     int    ch;
  11449.     int    http_fnd = 0;
  11450.     char   buf[HTTPBUFLEN], *p;
  11451.     int    nullline;
  11452. #ifdef OS2
  11453.     struct utimbuf u_t;
  11454. #else /* OS2 */
  11455. #ifdef SYSUTIMEH
  11456.     struct utimbuf u_t;
  11457. #else
  11458.     struct utimbuf {
  11459.         time_t atime;
  11460.         time_t mtime;
  11461.     } u_t;
  11462. #endif /* SYSUTIMH */
  11463. #endif /* OS2 */
  11464.     time_t mod_t = 0;
  11465.     time_t srv_t = 0;
  11466.     time_t local_t = 0;
  11467.     char passwd[64];
  11468.     char b64in[128];
  11469.     char b64out[256];
  11470.     char * headers[HTTPHEADCNT];
  11471.     int closecon = 0;
  11472.     int chunked = 0;
  11473.     int zfile = 0;
  11474.     int first = 1;
  11475.  
  11476. #ifdef DEBUG
  11477.     if (deblog) {
  11478.         debug(F101,"http_get httpfd","",httpfd);
  11479.         debug(F110,"http_agent",agent,0);
  11480.         debug(F110,"http_user",user,0);
  11481.         debug(F110,"http_local",local,0);
  11482.         debug(F110,"http_remote",remote,0);
  11483.     }
  11484. #endif /* DEBUG */
  11485.     if (!remote) remote = "";
  11486.  
  11487.     if (httpfd == -1)
  11488.       return(-1);
  11489.  
  11490.     if (array) {
  11491.         for (i = 0; i < HTTPHEADCNT; i++)
  11492.           headers[i] = NULL;
  11493.     }
  11494.     len = 8;                            /* GET */
  11495.     len += strlen(HTTP_VERSION);
  11496.     len += strlen(remote);
  11497.     len += 16;
  11498.  
  11499.     if (hdrlist) {
  11500.         for (i = 0; hdrlist[i]; i++)
  11501.             len += strlen(hdrlist[i]) + 2;
  11502.     }
  11503.     len += (int) strlen(http_host_port) + 8;
  11504.  
  11505.     if (agent)
  11506.       len += 13 + strlen(agent);
  11507.     if (user) {
  11508.         if (!pwd) {
  11509.             readpass("Password: ",passwd,64);
  11510.             pwd = passwd;
  11511.         }
  11512.         ckmakmsg(b64in,sizeof(b64in),user,":",pwd,NULL);
  11513.         j = b8tob64(b64in,strlen(b64in),b64out,256);
  11514.         memset(pwd,0,strlen(pwd));      /* NOT PORTABLE */
  11515.         if (j < 0)
  11516.           return(-1);
  11517.         b64out[j] = '\0';
  11518.         len += j + 24;
  11519.     }
  11520. #ifdef HTTP_CLOSE
  11521.     len += 19;                          /* Connection: close */
  11522. #endif
  11523.     len += 3;                           /* blank line + null */
  11524.  
  11525.     request = malloc(len);
  11526.     if (!request)
  11527.       return(-1);
  11528.  
  11529.     sprintf(request,"GET %s %s\r\n",remote,HTTP_VERSION);       /* safe */
  11530.     ckstrncat(request,"Host: ", len);
  11531.     ckstrncat(request,http_host_port, len);
  11532.     ckstrncat(request,"\r\n",len);
  11533.     if (agent) {
  11534.         ckstrncat(request,"User-agent: ",len);
  11535.         ckstrncat(request,agent,len);
  11536.         ckstrncat(request,"\r\n",len);
  11537.     }
  11538.     if (user) {
  11539.         ckstrncat(request,"Authorization: Basic ",len);
  11540.         ckstrncat(request,b64out,len);
  11541.         ckstrncat(request,"\r\n",len);
  11542.     }
  11543.     if ( hdrlist ) {
  11544.         for (i = 0; hdrlist[i]; i++) {
  11545.             ckstrncat(request,hdrlist[i],len);
  11546.             ckstrncat(request,"\r\n",len);
  11547.         }
  11548.     }
  11549. #ifdef HTTP_CLOSE
  11550.     ckstrncat(request,"Connection: close\r\n",len);
  11551. #endif
  11552.     ckstrncat(request,"\r\n",len);
  11553.  
  11554.   getreq:
  11555.     if (http_tol((CHAR *)request,strlen(request)) < 0)
  11556.     {
  11557.         http_close();
  11558.         if ( first ) {
  11559.             first--;
  11560.             http_reopen();
  11561.             goto getreq;
  11562.         }
  11563.         rc = -1;
  11564.         goto getexit;
  11565.     }
  11566.  
  11567.     /* Process the headers */
  11568.     local_t = time(NULL);
  11569.     nullline = 0;
  11570.     i = 0;
  11571.     len = -1;
  11572.     while (!nullline && (ch = http_inc(0)) >= 0 && i < HTTPBUFLEN) {
  11573.         buf[i] = ch;
  11574.         if ( buf[i] == 10 ) { /* found end of line */
  11575.             if (i > 0 && buf[i-1] == 13)
  11576.               i--;
  11577.             if (i < 1)
  11578.               nullline = 1;
  11579.             buf[i] = '\0';
  11580.             if (array && !nullline && hdcnt < HTTPHEADCNT)
  11581.               makestr(&headers[hdcnt++],buf);
  11582.             if (!ckstrcmp(buf,"HTTP",4,0)) {
  11583.                 http_fnd = 1;
  11584.                 j = ckindex(" ",buf,0,0,0);
  11585.                 p = &buf[j];
  11586.                 while ( isspace(*p) )
  11587.                   p++;
  11588.                 switch ( p[0] ) {
  11589.                   case '1':             /* Informational message */
  11590.                     break;
  11591.                   case '2':             /* Success */
  11592.                     break;
  11593.                   case '3':             /* Redirection */
  11594.                   case '4':             /* Client failure */
  11595.                   case '5':             /* Server failure */
  11596.                   default:              /* Unknown */
  11597.                     if (!quiet)
  11598.                       printf("Failure: Server reports %s\n",p);
  11599.                     rc = -1;
  11600.                     local = NULL;
  11601.                 }
  11602.                 http_set_code_reply(p);
  11603. #ifdef CMDATE2TM
  11604.             } else if (!ckstrcmp(buf,"Last-Modified",13,0)) {
  11605.                 mod_t = http_date(&buf[15]);
  11606.             } else if (!ckstrcmp(buf,"Date",4,0)) {
  11607.                 srv_t = http_date(&buf[4]);
  11608. #endif /* CMDATE2TM */
  11609.             } else if (!ckstrcmp(buf,"Connection:",11,0)) {
  11610.                 if ( ckindex("close",buf,11,0,0) != 0 )
  11611.                     closecon = 1;
  11612.             } else if (!ckstrcmp(buf,"Content-Length:",15,0)) {
  11613.                 len = atoi(&buf[16]);
  11614.             } else if (!ckstrcmp(buf,"Transfer-Encoding:",18,0)) {
  11615.                 if ( ckindex("chunked",buf,18,0,0) != 0 )
  11616.                     chunked = 1;
  11617.         debug(F101,"http_get chunked","",chunked);
  11618.             }
  11619.             i = 0;
  11620.         } else {
  11621.             i++;
  11622.         }
  11623.     }
  11624.     if (ch < 0 && first) {
  11625.         first--;
  11626.         http_close();
  11627.         http_reopen();
  11628.         goto getreq;
  11629.     }
  11630.     if (http_fnd == 0) {
  11631.         rc = -1;
  11632.         closecon = 1;
  11633.         goto getexit;
  11634.     }
  11635.  
  11636.     /* Now we have the contents of the file */
  11637.     if ( local && local[0] ) {
  11638.         if (zopeno(ZOFILE,local,NULL,NULL))
  11639.             zfile = 1;
  11640.         else
  11641.             rc = -1;
  11642.     }
  11643.  
  11644.     if ( chunked ) {
  11645.         while ((len = http_get_chunk_len()) > 0) {
  11646.             while (len && (ch = http_inc(0)) >= 0) {
  11647.                 len--;
  11648.                 if ( zfile )
  11649.                     zchout(ZOFILE,(CHAR)ch);
  11650.                 if ( stdio )
  11651.                     conoc((CHAR)ch);
  11652.             }
  11653.             if ((ch = http_inc(0)) != CR)
  11654.                 break;
  11655.             if ((ch = http_inc(0)) != LF)
  11656.                 break;
  11657.         }
  11658.     } else {
  11659.         while (len && (ch = http_inc(0)) >= 0) {
  11660.             len--;
  11661.             if ( zfile )
  11662.                 zchout(ZOFILE,(CHAR)ch);
  11663.             if ( stdio )
  11664.                 conoc((CHAR)ch);
  11665.         }
  11666.     }
  11667.  
  11668.     if ( zfile )
  11669.         zclose(ZOFILE);
  11670.  
  11671.     if ( chunked ) {            /* Parse Trailing Headers */
  11672.         nullline = 0;
  11673.         while (!nullline && (ch = http_inc(0)) >= 0 && i < HTTPBUFLEN) {
  11674.             buf[i] = ch;
  11675.             if ( buf[i] == 10 ) { /* found end of line */
  11676.                 if (i > 0 && buf[i-1] == 13)
  11677.                   i--;
  11678.                 if (i < 1)
  11679.                   nullline = 1;
  11680.                 buf[i] = '\0';
  11681.                 if (array && !nullline && hdcnt < HTTPHEADCNT)
  11682.                     makestr(&headers[hdcnt++],buf);
  11683. #ifdef CMDATE2TM
  11684.                 if (!ckstrcmp(buf,"Last-Modified",13,0)) {
  11685.                     mod_t = http_date(&buf[15]);
  11686.                 } else if (!ckstrcmp(buf,"Date",4,0)) {
  11687.                     srv_t = http_date(&buf[4]);
  11688.                 }
  11689. #endif /* CMDATE2TM */
  11690.                 else if (!ckstrcmp(buf,"Connection:",11,0)) {
  11691.                     if ( ckindex("close",buf,11,0,0) != 0 )
  11692.                         closecon = 1;
  11693.                 }
  11694.                 i = 0;
  11695.             } else {
  11696.                 i++;
  11697.             }
  11698.         }
  11699.     }
  11700.  
  11701.     if ( zfile ) {              /* Set timestamp */
  11702. #ifndef NOSETTIME
  11703. #ifdef OS2
  11704.         u_t.actime = srv_t ? srv_t : local_t;
  11705.         u_t.modtime = mod_t ? mod_t : local_t;
  11706. #else /* OS2 */
  11707. #ifdef SYSUTIMEH
  11708.         u_t.actime = srv_t ? srv_t : local_t;
  11709.         u_t.modtime = mod_t ? mod_t : local_t;
  11710. #else
  11711. #ifdef BSD44
  11712.         u_t[0].tv_sec = srv_t ? srv_t : local_t;
  11713.         u_t[1].tv_sec = mod_t ? mod_t : local_t;
  11714. #else
  11715.         u_t.mtime = srv_t ? srv_t : local_t;
  11716.         u_t.atime = mod_t ? mod_t : local_t;
  11717. #endif /* BSD44 */
  11718. #endif /* SYSUTIMEH */
  11719. #endif /* OS2 */
  11720.             utime(local,&u_t);
  11721. #endif /* NOSETTIME */
  11722.     }
  11723.  
  11724.   getexit:
  11725.     if (array)
  11726.       http_mkarray(headers,hdcnt,array);
  11727.  
  11728.     if ( closecon )
  11729.         http_close();
  11730.     free(request);
  11731.     for (i = 0; i < hdcnt; i++) {
  11732.         if (headers[i])
  11733.           free(headers[i]);
  11734.     }
  11735.     return(rc);
  11736. }
  11737.  
  11738. int
  11739. #ifdef CK_ANSIC
  11740. http_head(char * agent, char ** hdrlist, char * user,
  11741.           char * pwd, char array, char * local, char * remote,
  11742.           int stdio)
  11743. #else
  11744. http_head(agent, hdrlist, user, pwd, array, local, remote, stdio)
  11745.     char * agent; char ** hdrlist; char * user;
  11746.     char * pwd; char array; char * local; char * remote;
  11747.     int stdio;
  11748. #endif /* CK_ANSIC */
  11749. {
  11750.     char * request = NULL;
  11751.     int    i, j, len = 0, hdcnt = 0, rc = 0;
  11752.     int    ch;
  11753.     int    http_fnd = 0;
  11754.     char   buf[HTTPBUFLEN], *p;
  11755.     int    nullline;
  11756.     time_t mod_t;
  11757.     time_t srv_t;
  11758.     time_t local_t;
  11759.     char passwd[64];
  11760.     char b64in[128];
  11761.     char b64out[256];
  11762.     char * headers[HTTPHEADCNT];
  11763.     int  closecon = 0;
  11764.     int  first = 1;
  11765.  
  11766.     if (httpfd == -1)
  11767.       return(-1);
  11768.  
  11769.     if (array) {
  11770.         for (i = 0; i < HTTPHEADCNT; i++)
  11771.           headers[i] = NULL;
  11772.     }
  11773.     len = 9;                            /* HEAD */
  11774.     len += strlen(HTTP_VERSION);
  11775.     len += strlen(remote);
  11776.     len += 16;
  11777.  
  11778.     if ( hdrlist ) {
  11779.         for (i = 0; hdrlist[i]; i++)
  11780.             len += strlen(hdrlist[i]) + 2;
  11781.     }
  11782.     len += strlen(http_host_port) + 8;
  11783.  
  11784.     if (agent)
  11785.       len += 13 + strlen(agent);
  11786.     if (user) {
  11787.         if (!pwd) {
  11788.             readpass("Password: ",passwd,64);
  11789.             pwd = passwd;
  11790.         }
  11791.         ckmakmsg(b64in,sizeof(b64in),user,":",pwd,NULL);
  11792.         j = b8tob64(b64in,strlen(b64in),b64out,256);
  11793.         memset(pwd,0,strlen(pwd));      /* NOT PORTABLE */
  11794.         if (j < 0)
  11795.           return(-1);
  11796.         b64out[j] = '\0';
  11797.         len += j + 24;
  11798.     }
  11799. #ifdef HTTP_CLOSE
  11800.     len += 19;                          /* Connection: close */
  11801. #endif
  11802.     len += 3;                           /* blank line + null */
  11803.  
  11804.     request = (char *)malloc(len);
  11805.     if (!request)
  11806.       return(-1);
  11807.  
  11808.     sprintf(request,"HEAD %s %s\r\n",remote,HTTP_VERSION);
  11809.     ckstrncat(request,"Host: ", len);
  11810.     ckstrncat(request,http_host_port, len);
  11811.     ckstrncat(request,"\r\n",len);
  11812.     if (agent) {
  11813.         ckstrncat(request,"User-agent: ",len);
  11814.         ckstrncat(request,agent,len);
  11815.         ckstrncat(request,"\r\n",len);
  11816.     }
  11817.     if (user) {
  11818.         ckstrncat(request,"Authorization: Basic ",len);
  11819.         ckstrncat(request,b64out,len);
  11820.         ckstrncat(request,"\r\n",len);
  11821.     }
  11822.     if ( hdrlist ) {
  11823.         for (i = 0; hdrlist[i]; i++) {
  11824.             ckstrncat(request,hdrlist[i],len);
  11825.             ckstrncat(request,"\r\n",len);
  11826.         }
  11827.     }
  11828. #ifdef HTTP_CLOSE
  11829.     ckstrncat(request,"Connection: close\r\n",len);
  11830. #endif
  11831.     ckstrncat(request,"\r\n",len);
  11832.  
  11833.     if ( local && local[0] ) {
  11834.         if (!zopeno(ZOFILE,local,NULL,NULL)) {
  11835.             free(request);
  11836.             return(-1);
  11837.         }
  11838.     }
  11839.  
  11840.   headreq:
  11841.     if (http_tol((CHAR *)request,strlen(request)) < 0)
  11842.     {
  11843.         http_close();
  11844.         if ( first ) {
  11845.             first--;
  11846.             http_reopen();
  11847.             goto headreq;
  11848.         }
  11849.         rc = -1;
  11850.         goto headexit;
  11851.     }
  11852.  
  11853.     /* Process the headers */
  11854.  
  11855.     local_t = time(NULL);
  11856.     nullline = 0;
  11857.     i = 0;
  11858.     while (!nullline && (ch = http_inc(0)) >= 0 && i < HTTPBUFLEN) {
  11859.         buf[i] = ch;
  11860.         if (buf[i] == 10) {             /* found end of line */
  11861.             if (i > 0 && buf[i-1] == 13)
  11862.               i--;
  11863.             if (i < 1)
  11864.               nullline = 1;
  11865.             buf[i] = '\0';
  11866.             if (array && !nullline && hdcnt < HTTPHEADCNT)
  11867.               makestr(&headers[hdcnt++],buf);
  11868.             if (!ckstrcmp(buf,"HTTP",4,0)) {
  11869.                 http_fnd = 1;
  11870.                 j = ckindex(" ",buf,0,0,0);
  11871.                 p = &buf[j];
  11872.                 while (isspace(*p))
  11873.                   p++;
  11874.                 switch (p[0]) {
  11875.                   case '1':             /* Informational message */
  11876.                     break;
  11877.                   case '2':             /* Success */
  11878.                     break;
  11879.                   case '3':             /* Redirection */
  11880.                   case '4':             /* Client failure */
  11881.                   case '5':             /* Server failure */
  11882.                   default:              /* Unknown */
  11883.                     if (!quiet)
  11884.                       printf("Failure: Server reports %s\n",p);
  11885.                     rc = -1;
  11886.                 }
  11887.                 http_set_code_reply(p);
  11888.             } else {
  11889.                 if (!ckstrcmp(buf,"Connection:",11,0)) {
  11890.                     if ( ckindex("close",buf,11,0,0) != 0 )
  11891.                         closecon = 1;
  11892.                 }
  11893.                 if ( local && local[0] ) {
  11894.                     zsout(ZOFILE,buf);
  11895.                     zsout(ZOFILE,"\r\n");
  11896.                 }
  11897.                 if (stdio)
  11898.                     printf("%s\r\n",buf);
  11899.             }
  11900.             i = 0;
  11901.         } else {
  11902.             i++;
  11903.         }
  11904.     }
  11905.     if (ch < 0 && first) {
  11906.         first--;
  11907.         http_close();
  11908.         http_reopen();
  11909.         goto headreq;
  11910.     }
  11911.     if ( http_fnd == 0 )
  11912.         rc = -1;
  11913.  
  11914.     if (array)
  11915.       http_mkarray(headers,hdcnt,array);
  11916.  
  11917.   headexit:
  11918.     if ( local && local[0] )
  11919.         zclose(ZOFILE);
  11920.     if (closecon)
  11921.         http_close();
  11922.     free(request);
  11923.     for (i = 0; i < hdcnt; i++) {
  11924.         if (headers[i])
  11925.           free(headers[i]);
  11926.     }
  11927.     return(rc);
  11928. }
  11929.  
  11930. int
  11931. #ifdef CK_ANSIC
  11932. http_index(char * agent, char ** hdrlist, char * user, char * pwd,
  11933.              char array, char * local, char * remote, int stdio)
  11934. #else
  11935. http_index(agent, hdrlist, user, pwd, array, local, remote, stdio)
  11936.     char * agent; char ** hdrlist; char * user; char * pwd;
  11937.     char array; char * local; char * remote; int stdio;
  11938. #endif /* CK_ANSIC */
  11939. {
  11940.     char * request = NULL;
  11941.     int    i, j, len = 0, hdcnt = 0, rc = 0;
  11942.     int    ch;
  11943.     int    http_fnd = 0;
  11944.     char   buf[HTTPBUFLEN], *p;
  11945.     int    nullline;
  11946.     time_t mod_t;
  11947.     time_t srv_t;
  11948.     time_t local_t;
  11949.     char passwd[64];
  11950.     char b64in[128];
  11951.     char b64out[256];
  11952.     char * headers[HTTPHEADCNT];
  11953.     int  closecon = 0;
  11954.     int  chunked = 0;
  11955.     int  zfile = 0;
  11956.     int  first = 1;
  11957.  
  11958.     if (httpfd == -1)
  11959.       return(-1);
  11960.  
  11961.     if (array) {
  11962.         for (i = 0; i < HTTPHEADCNT; i++)
  11963.           headers[i] = NULL;
  11964.     }
  11965.     len = 10;                            /* INDEX */
  11966.     len += strlen(HTTP_VERSION);
  11967.     len += strlen(remote);
  11968.     len += 16;
  11969.  
  11970.     if ( hdrlist ) {
  11971.         for (i = 0; hdrlist[i]; i++)
  11972.             len += strlen(hdrlist[i]) + 2;
  11973.     }
  11974.     len += strlen(http_host_port) + 8;
  11975.  
  11976.     if (agent)
  11977.         len += 13 + strlen(agent);
  11978.     if (user) {
  11979.         if (!pwd) {
  11980.             readpass("Password: ",passwd,64);
  11981.             pwd = passwd;
  11982.         }
  11983.         ckmakmsg(b64in,sizeof(b64in),user,":",pwd,NULL);
  11984.         j = b8tob64(b64in,strlen(b64in),b64out,256);
  11985.         memset(pwd,0,strlen(pwd));
  11986.         if (j < 0)
  11987.           return(-1);
  11988.         b64out[j] = '\0';
  11989.         len += j + 24;
  11990.     }
  11991. #ifdef HTTP_CLOSE
  11992.     len += 19;                          /* Connection: close */
  11993. #endif
  11994.     len += 3;                           /* blank line + null */
  11995.  
  11996.     request = malloc(len);
  11997.     if (!request)
  11998.       return(-1);
  11999.  
  12000.     sprintf(request,"INDEX %s\r\n",HTTP_VERSION);
  12001.     ckstrncat(request,"Host: ", len);
  12002.     ckstrncat(request,http_host_port, len);
  12003.     ckstrncat(request,"\r\n",len);
  12004.     if (agent) {
  12005.         ckstrncat(request,"User-agent: ",len);
  12006.         ckstrncat(request,agent,len);
  12007.         ckstrncat(request,"\r\n",len);
  12008.     }
  12009.     if (user) {
  12010.         ckstrncat(request,"Authorization: Basic ",len);
  12011.         ckstrncat(request,b64out,len);
  12012.         ckstrncat(request,"\r\n",len);
  12013.     }
  12014.     if ( hdrlist ) {
  12015.         for (i = 0; hdrlist[i]; i++) {
  12016.             ckstrncat(request,hdrlist[i],len);
  12017.             ckstrncat(request,"\r\n",len);
  12018.         }
  12019.     }
  12020. #ifdef HTTP_CLOSE
  12021.     ckstrncat(request,"Connection: close\r\n",len);
  12022. #endif
  12023.     ckstrncat(request,"\r\n",len);
  12024.   indexreq:
  12025.     if (http_tol((CHAR *)request,strlen(request)) < 0)
  12026.     {
  12027.         http_close();
  12028.         if ( first ) {
  12029.             first--;
  12030.             http_reopen();
  12031.             goto indexreq;
  12032.         }
  12033.         rc = -1;
  12034.         goto indexexit;
  12035.     }
  12036.  
  12037.     /* Process the headers */
  12038.     local_t = time(NULL);
  12039.     nullline = 0;
  12040.     i = 0;
  12041.     len = -1;
  12042.     while (!nullline && (ch = http_inc(0)) >= 0 && i < HTTPBUFLEN) {
  12043.         buf[i] = ch;
  12044.         if (buf[i] == 10) {             /* found end of line */
  12045.             if (i > 0 && buf[i-1] == 13)
  12046.               i--;
  12047.             if (i < 1)
  12048.               nullline = 1;
  12049.             buf[i] = '\0';
  12050.             if (array && !nullline && hdcnt < HTTPHEADCNT)
  12051.               makestr(&headers[hdcnt++],buf);
  12052.             if (!ckstrcmp(buf,"HTTP",4,0)) {
  12053.                 http_fnd = 1;
  12054.                 j = ckindex(" ",buf,0,0,0);
  12055.                 p = &buf[j];
  12056.                 while (isspace(*p))
  12057.                   p++;
  12058.                 switch ( p[0] ) {
  12059.                   case '1':             /* Informational message */
  12060.                     break;
  12061.                   case '2':             /* Success */
  12062.                     break;
  12063.                   case '3':             /* Redirection */
  12064.                   case '4':             /* Client failure */
  12065.                   case '5':             /* Server failure */
  12066.                   default:              /* Unknown */
  12067.                     if (!quiet)
  12068.                       printf("Failure: Server reports %s\n",p);
  12069.                     rc = -1;
  12070.                 }
  12071.                 http_set_code_reply(p);
  12072.             } else if ( !nullline ) {
  12073.                 if (!ckstrcmp(buf,"Connection:",11,0)) {
  12074.                     if ( ckindex("close",buf,11,0,0) != 0 )
  12075.                         closecon = 1;
  12076.                 } else if (!ckstrcmp(buf,"Content-Length:",15,0)) {
  12077.                     len = atoi(&buf[16]);
  12078.                 } else if (!ckstrcmp(buf,"Transfer-Encoding:",18,0)) {
  12079.                     if ( ckindex("chunked",buf,18,0,0) != 0 )
  12080.                         chunked = 1;
  12081.             debug(F101,"http_index chunked","",chunked);
  12082.                 }
  12083.                 printf("%s\n",buf);
  12084.             }
  12085.             i = 0;
  12086.         } else {
  12087.             i++;
  12088.         }
  12089.     }
  12090.  
  12091.     if (ch < 0 && first) {
  12092.         first--;
  12093.         http_close();
  12094.         http_reopen();
  12095.         goto indexreq;
  12096.     }
  12097.     if ( http_fnd == 0 ) {
  12098.         rc = -1;
  12099.         closecon = 1;
  12100.         goto indexexit;
  12101.     }
  12102.  
  12103.     /* Now we have the contents of the file */
  12104.     if ( local && local[0] ) {
  12105.         if (zopeno(ZOFILE,local,NULL,NULL))
  12106.             zfile = 1;
  12107.         else
  12108.             rc = -1;
  12109.     }
  12110.  
  12111.     if ( chunked ) {
  12112.         while ((len = http_get_chunk_len()) > 0) {
  12113.             while (len && (ch = http_inc(0)) >= 0) {
  12114.                 len--;
  12115.                 if ( zfile )
  12116.                     zchout(ZOFILE,(CHAR)ch);
  12117.                 if ( stdio )
  12118.                     conoc((CHAR)ch);
  12119.             }
  12120.             if ((ch = http_inc(0)) != CR)
  12121.                 break;
  12122.             if ((ch = http_inc(0)) != LF)
  12123.                 break;
  12124.         }
  12125.     } else {
  12126.         while (len && (ch = http_inc(0)) >= 0) {
  12127.             len--;
  12128.             if ( zfile )
  12129.                 zchout(ZOFILE,(CHAR)ch);
  12130.             if ( stdio )
  12131.                 conoc((CHAR)ch);
  12132.         }
  12133.     }
  12134.  
  12135.     if ( zfile )
  12136.         zclose(ZOFILE);
  12137.  
  12138.     if ( chunked ) {            /* Parse Trailing Headers */
  12139.         nullline = 0;
  12140.         while (!nullline && (ch = http_inc(0)) >= 0 && i < HTTPBUFLEN) {
  12141.             buf[i] = ch;
  12142.             if ( buf[i] == 10 ) { /* found end of line */
  12143.                 if (i > 0 && buf[i-1] == 13)
  12144.                   i--;
  12145.                 if (i < 1)
  12146.                   nullline = 1;
  12147.                 buf[i] = '\0';
  12148.                 if (array && !nullline && hdcnt < HTTPHEADCNT)
  12149.                     makestr(&headers[hdcnt++],buf);
  12150.                 if (!ckstrcmp(buf,"Connection:",11,0)) {
  12151.                     if ( ckindex("close",buf,11,0,0) != 0 )
  12152.                         closecon = 1;
  12153.                 }
  12154.                 i = 0;
  12155.             } else {
  12156.                 i++;
  12157.             }
  12158.         }
  12159.     }
  12160.     rc = 0;
  12161.  
  12162.   indexexit:
  12163.     if (array)
  12164.       http_mkarray(headers,hdcnt,array);
  12165.  
  12166.     if (closecon)
  12167.         http_close();
  12168.     free(request);
  12169.     for (i = 0; i < hdcnt; i++) {
  12170.         if (headers[i])
  12171.           free(headers[i]);
  12172.     }
  12173.     return(rc);
  12174. }
  12175.  
  12176. int
  12177. #ifdef CK_ANSIC
  12178. http_put(char * agent, char ** hdrlist, char * mime, char * user,
  12179.          char * pwd, char array, char * local, char * remote,
  12180.          char * dest, int stdio)
  12181. #else
  12182. http_put(agent, hdrlist, mime, user, pwd, array, local, remote, dest, stdio)
  12183.     char * agent; char ** hdrlist; char * mime; char * user;
  12184.     char * pwd; char array; char * local; char * remote; char * dest;
  12185.     int stdio;
  12186. #endif /* CK_ANSIC */
  12187. {
  12188.     char * request=NULL;
  12189.     int    i, j, len = 0, hdcnt = 0, rc = 0;
  12190.     int    ch;
  12191.     int    http_fnd = 0;
  12192.     char   buf[HTTPBUFLEN], *p;
  12193.     int    nullline;
  12194.     time_t mod_t;
  12195.     time_t srv_t;
  12196.     time_t local_t;
  12197.     char passwd[64];
  12198.     char b64in[128];
  12199.     char b64out[256];
  12200.     int  filelen;
  12201.     char * headers[HTTPHEADCNT];
  12202.     int  closecon = 0;
  12203.     int  chunked = 0;
  12204.     int  first = 1;
  12205.     int  zfile = 0;
  12206.  
  12207.     if (httpfd == -1)
  12208.       return(-1);
  12209.     if (!mime) mime = "";
  12210.     if (!remote) remote = "";
  12211.     if (!local) local = "";
  12212.     if (!*local) return(-1);
  12213.  
  12214.     if (array) {
  12215.         for (i = 0; i < HTTPHEADCNT; i++)
  12216.           headers[i] = NULL;
  12217.     }
  12218.     filelen = zchki(local);
  12219.     if (filelen < 0)
  12220.       return(-1);
  12221.  
  12222.     /* Compute length of request header */
  12223.     len = 8;                            /* PUT */
  12224.     len += strlen(HTTP_VERSION);
  12225.     len += strlen(remote);
  12226.     len += 16;
  12227.  
  12228.     if ( hdrlist ) {
  12229.         for (i = 0; hdrlist[i]; i++)
  12230.             len += strlen(hdrlist[i]) + 2;
  12231.     }
  12232.     len += strlen(http_host_port) + 8;
  12233.  
  12234.     if (agent)
  12235.       len += 13 + strlen(agent);
  12236.     if (user) {
  12237.         if (!pwd) {
  12238.             readpass("Password: ",passwd,64);
  12239.             pwd = passwd;
  12240.         }
  12241.         ckmakmsg(b64in,sizeof(b64in),user,":",pwd,NULL);
  12242.         j = b8tob64(b64in,strlen(b64in),b64out,256);
  12243.         memset(pwd,0,strlen(pwd));
  12244.         if (j < 0)
  12245.           return(-1);
  12246.         b64out[j] = '\0';
  12247.         len += j + 24;
  12248.     }
  12249.     len += 16 + strlen(mime);           /* Content-type: */
  12250.     len += 32;                          /* Content-length: */
  12251.     len += 32;                          /* Date: */
  12252. #ifdef HTTP_CLOSE
  12253.     len += 19;                          /* Connection: close */
  12254. #endif
  12255.     len += 3;                           /* blank line + null */
  12256.  
  12257.     request = malloc(len);
  12258.     if (!request)
  12259.       return(-1);
  12260.  
  12261.     sprintf(request,"PUT %s %s\r\n",remote,HTTP_VERSION);
  12262.     ckstrncat(request,"Date: ",len);
  12263. #ifdef CMDATE2TM
  12264.     ckstrncat(request,http_now(),len);
  12265. #else
  12266.     ckstrncat(request,...,len);
  12267. #endif /* CMDATE2TM */
  12268.     ckstrncat(request,"\r\n",len);
  12269.     ckstrncat(request,"Host: ", len);
  12270.     ckstrncat(request,http_host_port, len);
  12271.     ckstrncat(request,"\r\n",len);
  12272.     if (agent) {
  12273.         ckstrncat(request,"User-agent: ",len);
  12274.         ckstrncat(request,agent,len);
  12275.         ckstrncat(request,"\r\n",len);
  12276.     }
  12277.     if (user) {
  12278.         ckstrncat(request,"Authorization: Basic ",len);
  12279.         ckstrncat(request,b64out,len);
  12280.         ckstrncat(request,"\r\n",len);
  12281.     }
  12282.     if ( hdrlist ) {
  12283.         for (i = 0; hdrlist[i]; i++) {
  12284.             ckstrncat(request,hdrlist[i],len);
  12285.             ckstrncat(request,"\r\n",len);
  12286.         }
  12287.     }
  12288.     ckstrncat(request,"Content-type: ",len);
  12289.     ckstrncat(request,mime,len);
  12290.     ckstrncat(request,"\r\n",len);
  12291.     sprintf(buf,"Content-length: %d\r\n",filelen); /* safe */
  12292.     ckstrncat(request,buf,len);
  12293. #ifdef HTTP_CLOSE
  12294.     ckstrncat(request,"Connection: close\r\n",len);
  12295. #endif
  12296.     ckstrncat(request,"\r\n",len);
  12297.  
  12298.     /* Now we have the contents of the file */
  12299.     if (zopeni(ZIFILE,local)) {
  12300.  
  12301.       putreq:                /* Send request */
  12302.         if (http_tol((CHAR *)request,strlen(request)) <= 0) {
  12303.             http_close();
  12304.             if ( first ) {
  12305.                 first--;
  12306.                 http_reopen();
  12307.                 goto putreq;
  12308.             }
  12309.             zclose(ZIFILE);
  12310.             rc = -1;
  12311.             goto putexit;
  12312.         }
  12313.         /* Request headers have been sent */
  12314.  
  12315.         i = 0;
  12316.         while (zchin(ZIFILE,&ch) == 0) {
  12317.             buf[i++] = ch;
  12318.             if (i == HTTPBUFLEN) {
  12319.                 if (http_tol((CHAR *)buf,HTTPBUFLEN) <= 0) {
  12320.                     http_close();
  12321.                     if ( first ) {
  12322.                         first--;
  12323.                         http_reopen();
  12324.                         goto putreq;
  12325.                     }
  12326.                 }
  12327.                 i = 0;
  12328.             }
  12329.         }
  12330.         if (i > 0) {
  12331.             if (http_tol((CHAR *)buf,i) < 0) {
  12332.                 http_close();
  12333.                 if ( first ) {
  12334.                     first--;
  12335.                     http_reopen();
  12336.                     goto putreq;
  12337.                 }
  12338.             }
  12339.         }
  12340.         zclose(ZIFILE);
  12341.  
  12342.         /* Process the response headers */
  12343.         local_t = time(NULL);
  12344.         nullline = 0;
  12345.         i = 0;
  12346.         len = -1;
  12347.         while (!nullline && (ch = http_inc(0)) >= 0 && i < HTTPBUFLEN) {
  12348.             buf[i] = ch;
  12349.             if (buf[i] == 10) {         /* found end of line */
  12350.                 if (i > 0 && buf[i-1] == 13)
  12351.                   i--;
  12352.                 if (i < 1)
  12353.                   nullline = 1;
  12354.                 buf[i] = '\0';
  12355.                 if (array && !nullline && hdcnt < HTTPHEADCNT)
  12356.                   makestr(&headers[hdcnt++],buf);
  12357.                 if (!ckstrcmp(buf,"HTTP",4,0)) {
  12358.                     http_fnd = 1;
  12359.                     j = ckindex(" ",buf,0,0,0);
  12360.                     p = &buf[j];
  12361.                     while (isspace(*p))
  12362.                       p++;
  12363.                     switch (p[0]) {
  12364.                       case '1':         /* Informational message */
  12365.                         break;
  12366.                       case '2':         /* Success */
  12367.                         break;
  12368.                       case '3':         /* Redirection */
  12369.                       case '4':         /* Client failure */
  12370.                       case '5':         /* Server failure */
  12371.                       default:          /* Unknown */
  12372.                         if (!quiet)
  12373.                           printf("Failure: Server reports %s\n",p);
  12374.                         rc = -1;
  12375.                     }
  12376.                     http_set_code_reply(p);
  12377.                 } else {
  12378.                     if (!ckstrcmp(buf,"Connection:",11,0)) {
  12379.                         if ( ckindex("close",buf,11,0,0) != 0 )
  12380.                             closecon = 1;
  12381.                     } else if (!ckstrcmp(buf,"Content-Length:",15,0)) {
  12382.                         len = atoi(&buf[16]);
  12383.                     } else if (!ckstrcmp(buf,"Transfer-Encoding:",18,0)) {
  12384.                         if ( ckindex("chunked",buf,18,0,0) != 0 )
  12385.                             chunked = 1;
  12386.             debug(F101,"http_put chunked","",chunked);
  12387.                     }
  12388.                     if ( stdio )
  12389.                         printf("%s\n",buf);
  12390.                 }
  12391.                 i = 0;
  12392.             } else {
  12393.                 i++;
  12394.             }
  12395.         }
  12396.         if (ch < 0 && first) {
  12397.             first--;
  12398.             http_close();
  12399.             http_reopen();
  12400.             goto putreq;
  12401.         }
  12402.         if ( http_fnd == 0 ) {
  12403.             closecon = 1;
  12404.             rc = -1;
  12405.             goto putexit;
  12406.         }
  12407.  
  12408.         /* Any response data? */
  12409.         if ( dest && dest[0] ) {
  12410.             if (zopeno(ZOFILE,dest,NULL,NULL))
  12411.                 zfile = 1;
  12412.             else
  12413.                 rc = -1;
  12414.         }
  12415.  
  12416.         if ( chunked ) {
  12417.             while ((len = http_get_chunk_len()) > 0) {
  12418.                 while (len && (ch = http_inc(0)) >= 0) {
  12419.                     len--;
  12420.                     if ( zfile )
  12421.                         zchout(ZOFILE,(CHAR)ch);
  12422.                     if ( stdio )
  12423.                         conoc((CHAR)ch);
  12424.                 }
  12425.                 if ((ch = http_inc(0)) != CR)
  12426.                     break;
  12427.                 if ((ch = http_inc(0)) != LF)
  12428.                     break;
  12429.             }
  12430.         } else {
  12431.             while (len && (ch = http_inc(0)) >= 0) {
  12432.                 len--;
  12433.                 if ( zfile )
  12434.                     zchout(ZOFILE,(CHAR)ch);
  12435.                 if ( stdio )
  12436.                     conoc((CHAR)ch);
  12437.             }
  12438.         }
  12439.  
  12440.         if ( zfile )
  12441.             zclose(ZOFILE);
  12442.  
  12443.         if ( chunked ) {            /* Parse Trailing Headers */
  12444.             nullline = 0;
  12445.             while (!nullline && (ch = http_inc(0)) >= 0 && i < HTTPBUFLEN) {
  12446.                 buf[i] = ch;
  12447.                 if ( buf[i] == 10 ) { /* found end of line */
  12448.                     if (i > 0 && buf[i-1] == 13)
  12449.                       i--;
  12450.                     if (i < 1)
  12451.                       nullline = 1;
  12452.                     buf[i] = '\0';
  12453.                     if (array && !nullline && hdcnt < HTTPHEADCNT)
  12454.                         makestr(&headers[hdcnt++],buf);
  12455.                     if (!ckstrcmp(buf,"Connection:",11,0)) {
  12456.                         if ( ckindex("close",buf,11,0,0) != 0 )
  12457.                             closecon = 1;
  12458.                     }
  12459.                     i = 0;
  12460.                 } else {
  12461.                     i++;
  12462.                 }
  12463.             }
  12464.         }
  12465.     } else {
  12466.         rc = -1;
  12467.     }
  12468.  
  12469.   putexit:
  12470.     if ( array )
  12471.         http_mkarray(headers,hdcnt,array);
  12472.  
  12473.     if (closecon)
  12474.         http_close();
  12475.     free(request);
  12476.     for (i = 0; i < hdcnt; i++) {
  12477.         if (headers[i])
  12478.           free(headers[i]);
  12479.     }
  12480.     return(rc);
  12481. }
  12482.  
  12483. int
  12484. #ifdef CK_ANSIC
  12485. http_delete(char * agent, char ** hdrlist, char * user,
  12486.           char * pwd, char array, char * remote)
  12487. #else
  12488. http_delete(agent, hdrlist, user, pwd, array, remote)
  12489.     char * agent; char ** hdrlist; char * user;
  12490.     char * pwd; char array; char * remote;
  12491. #endif /* CK_ANSIC */
  12492. {
  12493.     char * request=NULL;
  12494.     int    i, j, len = 0, hdcnt = 0, rc = 0;
  12495.     int    ch;
  12496.     int    http_fnd = 0;
  12497.     char   buf[HTTPBUFLEN], *p;
  12498.     int    nullline;
  12499.     time_t mod_t;
  12500.     time_t srv_t;
  12501.     time_t local_t;
  12502.     char passwd[64];
  12503.     char b64in[128];
  12504.     char b64out[256];
  12505.     char * headers[HTTPHEADCNT];
  12506.     int  closecon = 0;
  12507.     int  chunked = 0;
  12508.     int  first = 1;
  12509.  
  12510.     if (httpfd == -1)
  12511.       return(-1);
  12512.  
  12513.     if (array) {
  12514.         for (i = 0; i < HTTPHEADCNT; i++)
  12515.           headers[i] = NULL;
  12516.     }
  12517.  
  12518.     /* Compute length of request header */
  12519.     len = 11;                            /* DELETE */
  12520.     len += strlen(HTTP_VERSION);
  12521.     len += strlen(remote);
  12522.     len += 16;
  12523.  
  12524.     if ( hdrlist ) {
  12525.         for (i = 0; hdrlist[i]; i++)
  12526.             len += strlen(hdrlist[i]) + 2;
  12527.     }
  12528.     len += strlen(http_host_port) + 8;
  12529.  
  12530.     if (agent)
  12531.       len += 13 + strlen(agent);
  12532.     if (user) {
  12533.         if (!pwd) {
  12534.             readpass("Password: ",passwd,64);
  12535.             pwd = passwd;
  12536.         }
  12537.         ckmakmsg(b64in,sizeof(b64in),user,":",pwd,NULL);
  12538.         j = b8tob64(b64in,strlen(b64in),b64out,256);
  12539.         memset(pwd,0,strlen(pwd));
  12540.         if (j < 0)
  12541.           return(-1);
  12542.         b64out[j] = '\0';
  12543.         len += j + 24;
  12544.     }
  12545.     len += 32;                          /* Date: */
  12546. #ifdef HTTP_CLOSE
  12547.     len += 19;                          /* Connection: close */
  12548. #endif
  12549.     len += 3;                           /* blank line + null */
  12550.  
  12551.     request = malloc(len);
  12552.     if (!request)
  12553.       return(-1);
  12554.  
  12555.     sprintf(request,"DELETE %s %s\r\n",remote,HTTP_VERSION);
  12556.     ckstrncat(request,"Date: ",len);
  12557. #ifdef CMDATE2TM
  12558.     ckstrncat(request,http_now(),len);
  12559. #else
  12560.     ckstrncat(request,...,len);
  12561. #endif /* CMDATE2TM */
  12562.     ckstrncat(request,"\r\n",len);
  12563.     ckstrncat(request,"Host: ", len);
  12564.     ckstrncat(request,http_host_port, len);
  12565.     ckstrncat(request,"\r\n",len);
  12566.     if (agent) {
  12567.         ckstrncat(request,"User-agent: ",len);
  12568.         ckstrncat(request,agent,len);
  12569.         ckstrncat(request,"\r\n",len);
  12570.     }
  12571.     if (user) {
  12572.         ckstrncat(request,"Authorization: Basic ",len);
  12573.         ckstrncat(request,b64out,len);
  12574.         ckstrncat(request,"\r\n",len);
  12575.     }
  12576.     if ( hdrlist ) {
  12577.         for (i = 0; hdrlist[i]; i++) {
  12578.             ckstrncat(request,hdrlist[i],len);
  12579.             ckstrncat(request,"\r\n",len);
  12580.         }
  12581.     }
  12582. #ifdef HTTP_CLOSE
  12583.     ckstrncat(request,"Connection: close\r\n",len);
  12584. #endif
  12585.     ckstrncat(request,"\r\n",len);
  12586.   delreq:
  12587.     if (http_tol((CHAR *)request,strlen(request)) < 0)
  12588.     {
  12589.         http_close();
  12590.         if ( first ) {
  12591.             first--;
  12592.             http_reopen();
  12593.             goto delreq;
  12594.         }
  12595.         rc = -1;
  12596.         goto delexit;
  12597.     }
  12598.  
  12599.     /* Process the response headers */
  12600.     local_t = time(NULL);
  12601.     nullline = 0;
  12602.     i = 0;
  12603.     len = -1;
  12604.     while (!nullline && (ch = http_inc(0)) >= 0 && i < HTTPBUFLEN) {
  12605.         buf[i] = ch;
  12606.         if (buf[i] == 10) {         /* found end of line */
  12607.             if (i > 0 && buf[i-1] == 13)
  12608.               i--;
  12609.             if (i < 1)
  12610.               nullline = 1;
  12611.             buf[i] = '\0';
  12612.             if (array && !nullline && hdcnt < HTTPHEADCNT)
  12613.                 makestr(&headers[hdcnt++],buf);
  12614.             if (!ckstrcmp(buf,"HTTP",4,0)) {
  12615.                 http_fnd = 1;
  12616.                 j = ckindex(" ",buf,0,0,0);
  12617.                 p = &buf[j];
  12618.                 while (isspace(*p))
  12619.                   p++;
  12620.                 switch (p[0]) {
  12621.                   case '1':             /* Informational message */
  12622.                     break;
  12623.                   case '2':             /* Success */
  12624.                     break;
  12625.                   case '3':             /* Redirection */
  12626.                   case '4':             /* Client failure */
  12627.                   case '5':             /* Server failure */
  12628.                   default:              /* Unknown */
  12629.                     if (!quiet)
  12630.                       printf("Failure: Server reports %s\n",p);
  12631.                     rc = -1;
  12632.                 }
  12633.                 http_set_code_reply(p);
  12634.             } else {
  12635.                 if (!ckstrcmp(buf,"Connection:",11,0)) {
  12636.                     if ( ckindex("close",buf,11,0,0) != 0 )
  12637.                         closecon = 1;
  12638.                 } else if (!ckstrcmp(buf,"Content-Length:",15,0)) {
  12639.                     len = atoi(&buf[16]);
  12640.                 } else if (!ckstrcmp(buf,"Transfer-Encoding:",18,0)) {
  12641.                     if ( ckindex("chunked",buf,18,0,0) != 0 )
  12642.                         chunked = 1;
  12643.             debug(F101,"http_delete chunked","",chunked);
  12644.                 }
  12645.                 printf("%s\n",buf);
  12646.             }
  12647.             i = 0;
  12648.         } else {
  12649.             i++;
  12650.         }
  12651.     }
  12652.     if (ch < 0 && first) {
  12653.         first--;
  12654.         http_close();
  12655.         http_reopen();
  12656.         goto delreq;
  12657.     }
  12658.     if ( http_fnd == 0 ) {
  12659.         rc = -1;
  12660.         closecon = 1;
  12661.         goto delexit;
  12662.     }
  12663.  
  12664.     /* Any response data? */
  12665.     if ( chunked ) {
  12666.         while ((len = http_get_chunk_len()) > 0) {
  12667.             while (len && (ch = http_inc(0)) >= 0) {
  12668.                 len--;
  12669.                 conoc((CHAR)ch);
  12670.             }
  12671.             if ((ch = http_inc(0)) != CR)
  12672.                 break;
  12673.             if ((ch = http_inc(0)) != LF)
  12674.                 break;
  12675.         }
  12676.     } else {
  12677.         while (len && (ch = http_inc(0)) >= 0) {
  12678.             len--;
  12679.             conoc((CHAR)ch);
  12680.         }
  12681.     }
  12682.  
  12683.     if ( chunked ) {            /* Parse Trailing Headers */
  12684.         nullline = 0;
  12685.         while (!nullline && (ch = http_inc(0)) >= 0 && i < HTTPBUFLEN) {
  12686.             buf[i] = ch;
  12687.             if ( buf[i] == 10 ) { /* found end of line */
  12688.                 if (i > 0 && buf[i-1] == 13)
  12689.                   i--;
  12690.                 if (i < 1)
  12691.                   nullline = 1;
  12692.                 buf[i] = '\0';
  12693.                 if (array && !nullline && hdcnt < HTTPHEADCNT)
  12694.                     makestr(&headers[hdcnt++],buf);
  12695.                 if (!ckstrcmp(buf,"Connection:",11,0)) {
  12696.                     if ( ckindex("close",buf,11,0,0) != 0 )
  12697.                         closecon = 1;
  12698.                 }
  12699.                 i = 0;
  12700.             } else {
  12701.                 i++;
  12702.             }
  12703.         }
  12704.     }
  12705.  
  12706.   delexit:
  12707.     if (array)
  12708.         http_mkarray(headers,hdcnt,array);
  12709.  
  12710.     if (closecon)
  12711.         http_close();
  12712.     free(request);
  12713.     for (i = 0; i < hdcnt; i++) {
  12714.         if (headers[i])
  12715.           free(headers[i]);
  12716.     }
  12717.     return(rc);
  12718. }
  12719.  
  12720. int
  12721. #ifdef CK_ANSIC
  12722. http_post(char * agent, char ** hdrlist, char * mime, char * user,
  12723.           char * pwd, char array, char * local, char * remote,
  12724.           char * dest, int stdio)
  12725. #else
  12726. http_post(agent, hdrlist, mime, user, pwd, array, local, remote, dest,
  12727.           stdio)
  12728.     char * agent; char ** hdrlist; char * mime; char * user;
  12729.     char * pwd; char array; char * local; char * remote; char * dest;
  12730.     int stdio;
  12731. #endif /* CK_ANSIC */
  12732. {
  12733.     char * request=NULL;
  12734.     int    i, j, len = 0, hdcnt = 0, rc = 0;
  12735.     int    ch;
  12736.     int    http_fnd = 0;
  12737.     char   buf[HTTPBUFLEN], *p;
  12738.     int    nullline;
  12739.     time_t mod_t;
  12740.     time_t srv_t;
  12741.     time_t local_t;
  12742.     char passwd[64];
  12743.     char b64in[128];
  12744.     char b64out[256];
  12745.     int  filelen;
  12746.     char * headers[HTTPHEADCNT];
  12747.     int  closecon = 0;
  12748.     int  chunked = 0;
  12749.     int  zfile = 0;
  12750.     int  first = 1;
  12751.  
  12752.     if (httpfd == -1)
  12753.       return(-1);
  12754.  
  12755.     if (array) {
  12756.         for (i = 0; i < HTTPHEADCNT; i++)
  12757.           headers[i] = NULL;
  12758.     }
  12759.     filelen = zchki(local);
  12760.     if (filelen < 0)
  12761.       return(-1);
  12762.  
  12763.     /* Compute length of request header */
  12764.     len = 9;                            /* POST */
  12765.     len += strlen(HTTP_VERSION);
  12766.     len += strlen(remote);
  12767.     len += 16;
  12768.  
  12769.     if ( hdrlist ) {
  12770.         for (i = 0; hdrlist[i]; i++)
  12771.             len += strlen(hdrlist[i]) + 2;
  12772.     }
  12773.     len += strlen(http_host_port) + 8;
  12774.  
  12775.     if (agent)
  12776.       len += 13 + strlen(agent);
  12777.     if (user) {
  12778.         if (!pwd) {
  12779.             readpass("Password: ",passwd,64);
  12780.             pwd = passwd;
  12781.         }
  12782.         ckmakmsg(b64in,sizeof(b64in),user,":",pwd,NULL);
  12783.         j = b8tob64(b64in,strlen(b64in),b64out,256);
  12784.         memset(pwd,0,strlen(pwd));
  12785.         if (j < 0)
  12786.           return(-1);
  12787.         b64out[j] = '\0';
  12788.         len += j + 24;
  12789.     }
  12790.     len += 16 + strlen(mime);           /* Content-type: */
  12791.     len += 32;                          /* Content-length: */
  12792.     len += 32;                          /* Date: */
  12793. #ifdef HTTP_CLOSE
  12794.     len += 19;                          /* Connection: close */
  12795. #endif
  12796.     len += 3;                           /* blank line + null */
  12797.  
  12798.     request = malloc(len);
  12799.     if (!request)
  12800.       return(-1);
  12801.  
  12802.     sprintf(request,"POST %s %s\r\n",remote,HTTP_VERSION);
  12803.     ckstrncat(request,"Date: ",len);
  12804.     ckstrncat(request,http_now(),len);
  12805.     ckstrncat(request,"\r\n",len);
  12806.     ckstrncat(request,"Host: ", len);
  12807.     ckstrncat(request,http_host_port, len);
  12808.     ckstrncat(request,"\r\n",len);
  12809.     if (agent) {
  12810.         ckstrncat(request,"User-agent: ",len);
  12811.         ckstrncat(request,agent,len);
  12812.         ckstrncat(request,"\r\n",len);
  12813.     }
  12814.     if (user) {
  12815.         ckstrncat(request,"Authorization: Basic ",len);
  12816.         ckstrncat(request,b64out,len);
  12817.         ckstrncat(request,"\r\n",len);
  12818.     }
  12819.     if ( hdrlist ) {
  12820.         for (i = 0; hdrlist[i]; i++) {
  12821.             ckstrncat(request,hdrlist[i],len);
  12822.             ckstrncat(request,"\r\n",len);
  12823.         }
  12824.     }
  12825.     ckstrncat(request,"Content-type: ",len);
  12826.     ckstrncat(request,mime,len);
  12827.     ckstrncat(request,"\r\n",len);
  12828. #ifdef HTTP_CLOSE
  12829.     ckstrncat(request,"Connection: close\r\n",len);
  12830. #endif
  12831.     sprintf(buf,"Content-length: %d\r\n",filelen); /* safe */
  12832.     ckstrncat(request,buf,len);
  12833.     ckstrncat(request,"\r\n",len);
  12834. #ifdef COMMENT
  12835.     /* This is apparently a mistake - the previous ckstrncat() already  */
  12836.     /* appended a blank line to the request.  There should only be one. */
  12837.     /* Servers are not required by RFC 2616 to ignore extraneous empty  */
  12838.     /* lines.  -fdc, 28 Aug 2005. */
  12839.     ckstrncat(request,"\r\n",len);
  12840. #endif    /* COMMENT */
  12841.  
  12842.     /* Now we have the contents of the file */
  12843.   postopen:
  12844.     if (zopeni(ZIFILE,local)) {
  12845.       postreq:
  12846.         if (http_tol((CHAR *)request,strlen(request)) < 0)
  12847.         {
  12848.             http_close();
  12849.             if ( first ) {
  12850.                 first--;
  12851.                 http_reopen();
  12852.                 goto postreq;
  12853.             }
  12854.             rc = -1;
  12855.             zclose(ZIFILE);
  12856.             goto postexit;
  12857.         }
  12858.  
  12859.         i = 0;
  12860.         while (zchin(ZIFILE,&ch) == 0) {
  12861.             buf[i++] = ch;
  12862.             if (i == HTTPBUFLEN) {
  12863.                 http_tol((CHAR *)buf,HTTPBUFLEN);
  12864.                 i = 0;
  12865.             }
  12866.         }
  12867.         if (i > 0)
  12868.           http_tol((CHAR *)buf,HTTPBUFLEN);
  12869.         zclose(ZIFILE);
  12870.  
  12871.         /* Process the response headers */
  12872.         local_t = time(NULL);
  12873.         nullline = 0;
  12874.         i = 0;
  12875.         len = -1;
  12876.         while (!nullline && (ch = http_inc(0)) >= 0 && i < HTTPBUFLEN) {
  12877.             buf[i] = ch;
  12878.             if (buf[i] == 10) {         /* found end of line */
  12879.                 if (i > 0 && buf[i-1] == 13)
  12880.                   i--;
  12881.                 if (i < 1)
  12882.                   nullline = 1;
  12883.                 buf[i] = '\0';
  12884.                 if (array && !nullline && hdcnt < HTTPHEADCNT)
  12885.                   makestr(&headers[hdcnt++],buf);
  12886.                 if (!ckstrcmp(buf,"HTTP",4,0)) {
  12887.                     http_fnd = 1;
  12888.                     j = ckindex(" ",buf,0,0,0);
  12889.                     p = &buf[j];
  12890.                     while (isspace(*p))
  12891.                       p++;
  12892.                     switch (p[0]) {
  12893.                       case '1':         /* Informational message */
  12894.                         break;
  12895.                       case '2':         /* Success */
  12896.                         break;
  12897.                       case '3':         /* Redirection */
  12898.                       case '4':         /* Client failure */
  12899.                       case '5':         /* Server failure */
  12900.                       default:          /* Unknown */
  12901.                         if (!quiet)
  12902.                           printf("Failure: Server reports %s\n",p);
  12903.                         rc = -1;
  12904.                     }
  12905.                     http_set_code_reply(p);
  12906.                 } else {
  12907.                     if (!ckstrcmp(buf,"Connection:",11,0)) {
  12908.                         if ( ckindex("close",buf,11,0,0) != 0 )
  12909.                             closecon = 1;
  12910.                     } else if (!ckstrcmp(buf,"Content-Length:",15,0)) {
  12911.                         len = atoi(&buf[16]);
  12912.                     } else if (!ckstrcmp(buf,"Transfer-Encoding:",18,0)) {
  12913.                         if ( ckindex("chunked",buf,18,0,0) != 0 )
  12914.                             chunked = 1;
  12915.             debug(F101,"http_post chunked","",chunked);
  12916.                     }
  12917.                     if (stdio)
  12918.                         printf("%s\n",buf);
  12919.                 }
  12920.                 i = 0;
  12921.             } else {
  12922.                 i++;
  12923.             }
  12924.         }
  12925.         if (ch < 0 && first) {
  12926.             first--;
  12927.             http_close();
  12928.             http_reopen();
  12929.             goto postopen;
  12930.         }
  12931.         if (http_fnd == 0) {
  12932.             rc = -1;
  12933.             closecon = 1;
  12934.             goto postexit;
  12935.         }
  12936.  
  12937.         /* Any response data? */
  12938.         if ( dest && dest[0] ) {
  12939.             if (zopeno(ZOFILE,dest,NULL,NULL))
  12940.                 zfile = 1;
  12941.             else
  12942.                 rc = -1;
  12943.         }
  12944.  
  12945.         if ( chunked ) {
  12946.             while ((len = http_get_chunk_len()) > 0) {
  12947.                 while (len && (ch = http_inc(0)) >= 0) {
  12948.                     len--;
  12949.                     if ( zfile )
  12950.                         zchout(ZOFILE,(CHAR)ch);
  12951.                     if ( stdio )
  12952.                         conoc((CHAR)ch);
  12953.                 }
  12954.                 if ((ch = http_inc(0)) != CR)
  12955.                     break;
  12956.                 if ((ch = http_inc(0)) != LF)
  12957.                     break;
  12958.             }
  12959.         } else {
  12960.             while (len && (ch = http_inc(0)) >= 0) {
  12961.                 len--;
  12962.                 if ( zfile )
  12963.                     zchout(ZOFILE,(CHAR)ch);
  12964.                 if ( stdio )
  12965.                     conoc((CHAR)ch);
  12966.             }
  12967.         }
  12968.  
  12969.         if ( zfile )
  12970.             zclose(ZOFILE);
  12971.  
  12972.         if ( chunked ) {            /* Parse Trailing Headers */
  12973.             nullline = 0;
  12974.             while (!nullline && (ch = http_inc(0)) >= 0 && i < HTTPBUFLEN) {
  12975.                 buf[i] = ch;
  12976.                 if ( buf[i] == 10 ) { /* found end of line */
  12977.                     if (i > 0 && buf[i-1] == 13)
  12978.                       i--;
  12979.                     if (i < 1)
  12980.                       nullline = 1;
  12981.                     buf[i] = '\0';
  12982.                     if (array && !nullline && hdcnt < HTTPHEADCNT)
  12983.                         makestr(&headers[hdcnt++],buf);
  12984.                     if (!ckstrcmp(buf,"Connection:",11,0)) {
  12985.                         if ( ckindex("close",buf,11,0,0) != 0 )
  12986.                             closecon = 1;
  12987.                     }
  12988.                     i = 0;
  12989.                 } else {
  12990.                     i++;
  12991.                 }
  12992.             }
  12993.         }
  12994.     } else {
  12995.         rc = -1;
  12996.     }
  12997.  
  12998.   postexit:
  12999.     if (array)
  13000.         http_mkarray(headers,hdcnt,array);
  13001.     if (closecon)
  13002.         http_close();
  13003.     free(request);
  13004.     for (i = 0; i < hdcnt; i++) {
  13005.         if (headers[i])
  13006.           free(headers[i]);
  13007.     }
  13008.     return(rc);
  13009. }
  13010.  
  13011. int
  13012. #ifdef CK_ANSIC
  13013. http_connect(int socket, char * agent, char ** hdrlist, char * user,
  13014.              char * pwd, char array, char * host_port)
  13015. #else
  13016. http_connect(socket, agent, hdrlist, user, pwd, array, host_port)
  13017.     int socket;
  13018.     char * agent; char ** hdrlist; char * user;
  13019.     char * pwd; char array; char * host_port;
  13020. #endif /* CK_ANSIC */
  13021. {
  13022.     char * request=NULL;
  13023.     int    i, j, len = 0, hdcnt = 0, rc = 0;
  13024.     int    http_fnd = 0;
  13025.     char   buf[HTTPBUFLEN], *p, ch;
  13026.     int    nullline;
  13027.     time_t mod_t;
  13028.     time_t srv_t;
  13029.     time_t local_t;
  13030.     char passwd[64];
  13031.     char b64in[128];
  13032.     char b64out[256];
  13033.     char * headers[HTTPHEADCNT];
  13034.     int    connected = 0;
  13035.  
  13036.     tcp_http_proxy_errno = 0;
  13037.  
  13038.     if (socket == -1)
  13039.       return(-1);
  13040.  
  13041.     if (array) {
  13042.         for (i = 0; i < HTTPHEADCNT; i++)
  13043.           headers[i] = NULL;
  13044.     }
  13045.  
  13046.     /* Compute length of request header */
  13047.     len = 12;                            /* CONNECT */
  13048.     len += strlen(HTTP_VERSION);
  13049.     len += strlen(host_port);
  13050.     len += (int) strlen(http_host_port) + 8;
  13051.     len += 16;
  13052.     len += strlen("Proxy-Connection: Keep-Alive\r\n");
  13053.     if ( hdrlist ) {
  13054.         for (i = 0; hdrlist[i]; i++)
  13055.             len += strlen(hdrlist[i]) + 2;
  13056.     }
  13057.     if (agent && agent[0])
  13058.       len += 13 + strlen(agent);
  13059.     if (user && user[0]) {
  13060.         if (!pwd) {
  13061.             readpass("Password: ",passwd,64);
  13062.             pwd = passwd;
  13063.         }
  13064.         ckmakmsg(b64in,sizeof(b64in),user,":",pwd,NULL);
  13065.         j = b8tob64(b64in,strlen(b64in),b64out,256);
  13066.         memset(pwd,0,strlen(pwd));
  13067.         if (j < 0)
  13068.           return(-1);
  13069.         b64out[j] = '\0';
  13070.         len += j + 72;
  13071.     }
  13072.     len += 32;                          /* Date: */
  13073.     len += 3;                           /* blank line + null */
  13074.  
  13075.     request = malloc(len);
  13076.     if (!request)
  13077.       return(-1);
  13078.  
  13079.     sprintf(request,"CONNECT %s %s\r\n",host_port,HTTP_VERSION);
  13080.     ckstrncat(request,"Date: ",len);
  13081. #ifdef CMDATE2TM
  13082.     ckstrncat(request,http_now(),len);
  13083. #else
  13084.     strcat(request,...);
  13085. #endif /* CMDATE2TM */
  13086.     ckstrncat(request,"\r\n",len);
  13087.     ckstrncat(request,"Host: ", len);
  13088.     ckstrncat(request,http_host_port, len);
  13089.     ckstrncat(request,"\r\n",len);
  13090.     if (agent && agent[0]) {
  13091.         ckstrncat(request,"User-agent: ",len);
  13092.         ckstrncat(request,agent,len);
  13093.         ckstrncat(request,"\r\n",len);
  13094.     }
  13095.     if (user && user[0]) {
  13096.         ckstrncat(request,"Proxy-authorization: Basic ",len);
  13097.         ckstrncat(request,b64out,len);
  13098.         ckstrncat(request,"\r\n",len);
  13099.         ckstrncat(request,"Extension: Security/Remote-Passphrase\r\n",len);
  13100.     }
  13101.     ckstrncat(request,"Proxy-Connection: Keep-Alive\r\n",len);
  13102.     if ( hdrlist ) {
  13103.         for (i = 0; hdrlist[i]; i++) {
  13104.             ckstrncat(request,hdrlist[i],len);
  13105.             ckstrncat(request,"\r\n",len);
  13106.         }
  13107.     }
  13108.     ckstrncat(request,"\r\n",len);
  13109.     len = strlen(request);
  13110.  
  13111. #ifdef TCPIPLIB
  13112.     /* Send request */
  13113.     if (socket_write(socket,(CHAR *)request,strlen(request)) < 0) {
  13114.       rc = -1;
  13115.       goto connexit;
  13116.     }
  13117. #else
  13118.     if (write(socket,(CHAR *)request,strlen(request)) < 0) { /* Send request */
  13119.         rc = -1;
  13120.         goto connexit;
  13121.     }
  13122. #endif /* TCPIPLIB */
  13123.  
  13124.     /* Process the response headers */
  13125.     local_t = time(NULL);
  13126.     nullline = 0;
  13127.     i = 0;
  13128.     while (!nullline &&
  13129. #ifdef TCPIPLIB
  13130.            (socket_read(socket,&ch,1) == 1) &&
  13131. #else
  13132.            (read(socket,&ch,1) == 1) &&
  13133. #endif /* TCPIPLIB */
  13134.            i < HTTPBUFLEN) {
  13135.         buf[i] = ch;
  13136.         if (buf[i] == 10) {         /* found end of line */
  13137.             if (i > 0 && buf[i-1] == 13)
  13138.               i--;
  13139.             if (i < 1)
  13140.               nullline = 1;
  13141.             buf[i] = '\0';
  13142.  
  13143.             if (array && !nullline && hdcnt < HTTPHEADCNT)
  13144.                 makestr(&headers[hdcnt++],buf);
  13145.             if (!ckstrcmp(buf,"HTTP",4,0)) {
  13146.                 http_fnd = 1;
  13147.                 j = ckindex(" ",buf,0,0,0);
  13148.                 p = &buf[j];
  13149.                 while (isspace(*p))
  13150.                   p++;
  13151.                 tcp_http_proxy_errno = atoi(p);
  13152.                 switch (p[0]) {
  13153.                   case '1':             /* Informational message */
  13154.                     break;
  13155.                   case '2':             /* Success */
  13156.                     connected = 1;
  13157.                     break;
  13158.                   case '3':             /* Redirection */
  13159.                   case '4':             /* Client failure */
  13160.                   case '5':             /* Server failure */
  13161.                   default:              /* Unknown */
  13162.                     if (!quiet)
  13163.                       printf("Failure: Server reports %s\n",p);
  13164.                     rc = -1;
  13165.                 }
  13166.                 http_set_code_reply(p);
  13167.             } else {
  13168.                 printf("%s\n",buf);
  13169.             }
  13170.             i = 0;
  13171.         } else {
  13172.             i++;
  13173.         }
  13174.     }
  13175.     if ( http_fnd == 0 )
  13176.         rc = -1;
  13177.  
  13178.     if (array)
  13179.         http_mkarray(headers,hdcnt,array);
  13180.  
  13181.   connexit:
  13182.     if ( !connected ) {
  13183.         if ( socket == ttyfd ) {
  13184.             ttclos(0);
  13185.         }
  13186.         else if ( socket == httpfd ) {
  13187.             http_close();
  13188.         }
  13189.     }
  13190.  
  13191.     free(request);
  13192.     for (i = 0; i < hdcnt; i++) {
  13193.         if (headers[i])
  13194.           free(headers[i]);
  13195.     }
  13196.     return(rc);
  13197. }
  13198. #endif /* NOHTTP */
  13199.  
  13200. #ifdef CK_DNS_SRV
  13201.  
  13202. #define INCR_CHECK(x,y) x += y; if (x > size + answer.bytes) goto dnsout
  13203. #define CHECK(x,y) if (x + y > size + answer.bytes) goto dnsout
  13204. #define NTOHSP(x,y) x[0] << 8 | x[1]; x += y
  13205.  
  13206. #ifndef CKQUERYTYPE
  13207. #ifdef UNIXWARE
  13208. #ifndef UW7
  13209. #define CKQUERYTYPE CHAR
  13210. #endif /* UW7 */
  13211. #endif /* UNIXWARE */
  13212. #endif /* CKQUERYTYPE */
  13213.  
  13214. #ifndef CKQUERYTYPE
  13215. #define CKQUERYTYPE char
  13216. #endif /* CKQUERYTYPE */
  13217.  
  13218. /* 1 is success, 0 is failure */
  13219. int
  13220. locate_srv_dns(host, service, protocol, addr_pp, naddrs)
  13221.     char *host;
  13222.     char *service;
  13223.     char *protocol;
  13224.     struct sockaddr **addr_pp;
  13225.     int *naddrs;
  13226. {
  13227.     int nout, j, count;
  13228.     union {
  13229.         unsigned char bytes[2048];
  13230.         HEADER hdr;
  13231.     } answer;
  13232.     unsigned char *p=NULL;
  13233.     CKQUERYTYPE query[MAX_DNS_NAMELEN];
  13234. #ifdef CK_ANSIC
  13235.     const char * h;
  13236. #else
  13237.     char * h;
  13238. #endif /* CK_ANSIC */
  13239.     struct sockaddr *addr = NULL;
  13240.     struct sockaddr_in *sin = NULL;
  13241.     struct hostent *hp = NULL;
  13242.     int type, class;
  13243.     int priority, weight, size, len, numanswers, numqueries, rdlen;
  13244.     unsigned short port;
  13245. #ifdef CK_ANSIC
  13246.     const
  13247. #endif /* CK_ANSIC */
  13248.       int hdrsize = sizeof(HEADER);
  13249.     struct srv_dns_entry {
  13250.         struct srv_dns_entry *next;
  13251.         int priority;
  13252.         int weight;
  13253.         unsigned short port;
  13254.         char *host;
  13255.     };
  13256.     struct srv_dns_entry *head = NULL;
  13257.     struct srv_dns_entry *srv = NULL, *entry = NULL;
  13258.     char * s = NULL;
  13259.  
  13260.     nout = 0;
  13261.     addr = (struct sockaddr *) malloc(sizeof(struct sockaddr));
  13262.     if (addr == NULL)
  13263.       return 0;
  13264.  
  13265.     count = 1;
  13266.  
  13267.     /*
  13268.      * First build a query of the form:
  13269.      *
  13270.      *   service.protocol.host
  13271.      *
  13272.      * which will most likely be something like:
  13273.      *
  13274.      *   _telnet._tcp.host
  13275.      *
  13276.      */
  13277.     if (((int)strlen(service) + strlen(protocol) + strlen(host) + 5)
  13278.         > MAX_DNS_NAMELEN
  13279.         )
  13280.       goto dnsout;
  13281.  
  13282.     /* Realm names don't (normally) end with ".", but if the query
  13283.        doesn't end with "." and doesn't get an answer as is, the
  13284.        resolv code will try appending the local domain.  Since the
  13285.        realm names are absolutes, let's stop that.
  13286.  
  13287.        But only if a name has been specified.  If we are performing
  13288.        a search on the prefix alone then the intention is to allow
  13289.        the local domain or domain search lists to be expanded.
  13290.     */
  13291.     h = host + strlen (host);
  13292.     ckmakxmsg(query, sizeof(query), "_",service,"._",protocol,".", host,
  13293.               ((h > host) && (h[-1] != '.')?".":NULL),
  13294.                NULL,NULL,NULL,NULL,NULL);
  13295.  
  13296.     size = res_search(query, C_IN, T_SRV, answer.bytes, sizeof(answer.bytes));
  13297.  
  13298.     if (size < hdrsize)
  13299.       goto dnsout;
  13300.  
  13301.     /* We got a reply - See how many answers it contains. */
  13302.  
  13303.     p = answer.bytes;
  13304.  
  13305.     numqueries = ntohs(answer.hdr.qdcount);
  13306.     numanswers = ntohs(answer.hdr.ancount);
  13307.  
  13308.     p += sizeof(HEADER);
  13309.  
  13310.     /*
  13311.      * We need to skip over all of the questions, so we have to iterate
  13312.      * over every query record.  dn_expand() is able to tell us the size
  13313.      * of compressed DNS names, so we use it.
  13314.      */
  13315.     while (numqueries--) {
  13316.         len = dn_expand(answer.bytes,answer.bytes+size,p,query,sizeof(query));
  13317.         if (len < 0)
  13318.           goto dnsout;
  13319.         INCR_CHECK(p, len + 4);
  13320.     }
  13321.  
  13322.     /*
  13323.      * We're now pointing at the answer records.  Only process them if
  13324.      * they're actually T_SRV records (they might be CNAME records,
  13325.      * for instance).
  13326.      *
  13327.      * But in a DNS reply, if you get a CNAME you always get the associated
  13328.      * "real" RR for that CNAME.  RFC 1034, 3.6.2:
  13329.      *
  13330.      * CNAME RRs cause special action in DNS software.  When a name server
  13331.      * fails to find a desired RR in the resource set associated with the
  13332.      * domain name, it checks to see if the resource set consists of a CNAME
  13333.      * record with a matching class.  If so, the name server includes the CNAME
  13334.      * record in the response and restarts the query at the domain name
  13335.      * specified in the data field of the CNAME record.  The one exception to
  13336.      * this rule is that queries which match the CNAME type are not restarted.
  13337.      *
  13338.      * In other words, CNAMEs do not need to be expanded by the client.
  13339.      */
  13340.     while (numanswers--) {
  13341.  
  13342.         /* First is the name; use dn_expand() to get the compressed size. */
  13343.         len = dn_expand(answer.bytes,answer.bytes+size,p,query,sizeof(query));
  13344.         if (len < 0)
  13345.           goto dnsout;
  13346.         INCR_CHECK(p, len);
  13347.  
  13348.         CHECK(p,2);                     /* Query type */
  13349.         type = NTOHSP(p,2);
  13350.  
  13351.         CHECK(p, 6);                    /* Query class */
  13352.         class = NTOHSP(p,6);            /* Also skip over 4-byte TTL */
  13353.  
  13354.         CHECK(p,2);                     /* Record data length */
  13355.         rdlen = NTOHSP(p,2);
  13356.         /*
  13357.          * If this is an SRV record, process it.  Record format is:
  13358.          *
  13359.          * Priority
  13360.          * Weight
  13361.          * Port
  13362.          * Server name
  13363.          */
  13364.         if (class == C_IN && type == T_SRV) {
  13365.             CHECK(p,2);
  13366.             priority = NTOHSP(p,2);
  13367.             CHECK(p, 2);
  13368.             weight = NTOHSP(p,2);
  13369.             CHECK(p, 2);
  13370.             port = NTOHSP(p,2);
  13371.             len = dn_expand(answer.
  13372.                             bytes,
  13373.                             answer.bytes + size,
  13374.                             p,
  13375.                             query,
  13376.                             sizeof(query)
  13377.                             );
  13378.             if (len < 0)
  13379.               goto dnsout;
  13380.             INCR_CHECK(p, len);
  13381.             /*
  13382.              * We got everything.  Insert it into our list, but make sure
  13383.              * it's in the right order.  Right now we don't do anything
  13384.              * with the weight field
  13385.              */
  13386.             srv = (struct srv_dns_entry *)malloc(sizeof(struct srv_dns_entry));
  13387.             if (srv == NULL)
  13388.               goto dnsout;
  13389.  
  13390.             srv->priority = priority;
  13391.             srv->weight = weight;
  13392.             srv->port = port;
  13393.             makestr(&s,(char *)query);  /* strdup() is not portable */
  13394.             srv->host = s;
  13395.  
  13396.             if (head == NULL || head->priority > srv->priority) {
  13397.                 srv->next = head;
  13398.                 head = srv;
  13399.             } else
  13400.                 /*
  13401.                  * Confusing.  Insert an entry into this spot only if:
  13402.                  *  . The next person has a higher priority (lower
  13403.                  *    priorities are preferred), or:
  13404.                  *  . There is no next entry (we're at the end)
  13405.                  */
  13406.               for (entry = head; entry != NULL; entry = entry->next)
  13407.                 if ((entry->next &&
  13408.                      entry->next->priority > srv->priority) ||
  13409.                     entry->next == NULL) {
  13410.                     srv->next = entry->next;
  13411.                     entry->next = srv;
  13412.                     break;
  13413.                 }
  13414.         } else
  13415.           INCR_CHECK(p, rdlen);
  13416.     }
  13417.  
  13418.     /*
  13419.      * Now we've got a linked list of entries sorted by priority.
  13420.      * Start looking up A records and returning addresses.
  13421.      */
  13422.     if (head == NULL)
  13423.       goto dnsout;
  13424.  
  13425.     for (entry = head; entry != NULL; entry = entry->next) {
  13426.         hp = gethostbyname(entry->host);
  13427.         if (hp != 0) {
  13428.  
  13429.             /* Watch out - memset() and memcpy() are not portable... */
  13430.  
  13431.             switch (hp->h_addrtype) {
  13432.               case AF_INET:
  13433.                 for (j = 0; hp->h_addr_list[j]; j++) {
  13434.                     sin = (struct sockaddr_in *) &addr[nout++];
  13435.                     memset ((char *) sin, 0, sizeof (struct sockaddr));
  13436.                     sin->sin_family = hp->h_addrtype;
  13437.                     sin->sin_port = htons(entry->port);
  13438.                     memcpy((char *) &sin->sin_addr,
  13439.                            (char *) hp->h_addr_list[j],
  13440.                            sizeof(struct in_addr));             /* safe */
  13441.                     if (nout + 1 >= count) {
  13442.                         count += 5;
  13443.                         addr = (struct sockaddr *)
  13444.                           realloc((char *) addr,
  13445.                                   sizeof(struct sockaddr) * count);
  13446.                         if (!addr)
  13447.                           goto dnsout;
  13448.                     }
  13449.                 }
  13450.                 break;
  13451.               default:
  13452.                 break;
  13453.             }
  13454.         }
  13455.     }
  13456.     for (entry = head; entry != NULL;) {
  13457.         free(entry->host);
  13458.         entry->host = NULL;
  13459.         srv = entry;
  13460.         entry = entry->next;
  13461.         free(srv);
  13462.         srv = NULL;
  13463.     }
  13464.  
  13465.   dnsout:
  13466.     if (srv)
  13467.       free(srv);
  13468.  
  13469.     if (nout == 0) {                    /* No good servers */
  13470.         if (addr)
  13471.           free(addr);
  13472.         return 0;
  13473.     }
  13474.     *addr_pp = addr;
  13475.     *naddrs = nout;
  13476.     return 1;
  13477. }
  13478. #undef INCR_CHECK
  13479. #undef CHECK
  13480. #undef NTOHSP
  13481.  
  13482. #define INCR_CHECK(x, y) x += y; if (x > size + answer.bytes) \
  13483.                          return 0
  13484. #define CHECK(x, y) if (x + y > size + answer.bytes) \
  13485.                          return 0
  13486. #define NTOHSP(x, y) x[0] << 8 | x[1]; x += y
  13487.  
  13488. int
  13489. locate_txt_rr(prefix, name, retstr)
  13490.     char *prefix, *name;
  13491.     char **retstr;
  13492. {
  13493.     union {
  13494.         unsigned char bytes[2048];
  13495.         HEADER hdr;
  13496.     } answer;
  13497.     unsigned char *p;
  13498.     char host[MAX_DNS_NAMELEN], *h;
  13499.     int size;
  13500.     int type, class, numanswers, numqueries, rdlen, len;
  13501.  
  13502.     /*
  13503.      * Form our query, and send it via DNS
  13504.      */
  13505.  
  13506.     if (name == NULL || name[0] == '\0') {
  13507.         strcpy(host,prefix);
  13508.     } else {
  13509.         if ( strlen(prefix) + strlen(name) + 3 > MAX_DNS_NAMELEN )
  13510.             return 0;
  13511.  
  13512.         /* Realm names don't (normally) end with ".", but if the query
  13513.            doesn't end with "." and doesn't get an answer as is, the
  13514.            resolv code will try appending the local domain.  Since the
  13515.            realm names are absolutes, let's stop that.
  13516.  
  13517.            But only if a name has been specified.  If we are performing
  13518.            a search on the prefix alone then the intention is to allow
  13519.            the local domain or domain search lists to be expanded.
  13520.         */
  13521.         h = host + strlen (host);
  13522.         ckmakmsg(host,sizeof(host),prefix, ".", name,
  13523.                  ((h > host) && (h[-1] != '.'))?".":NULL);
  13524.  
  13525.     }
  13526.     size = res_search(host, C_IN, T_TXT, answer.bytes, sizeof(answer.bytes));
  13527.  
  13528.     if (size < 0)
  13529.         return 0;
  13530.  
  13531.     p = answer.bytes;
  13532.  
  13533.     numqueries = ntohs(answer.hdr.qdcount);
  13534.     numanswers = ntohs(answer.hdr.ancount);
  13535.  
  13536.     p += sizeof(HEADER);
  13537.  
  13538.     /*
  13539.      * We need to skip over the questions before we can get to the answers,
  13540.      * which means we have to iterate over every query record.  We use
  13541.      * dn_expand to tell us how long each compressed name is.
  13542.      */
  13543.  
  13544.     while (numqueries--) {
  13545.         len = dn_expand(answer.bytes, answer.bytes + size, p, host,
  13546.                          sizeof(host));
  13547.         if (len < 0)
  13548.             return 0;
  13549.         INCR_CHECK(p, len + 4);         /* Name plus type plus class */
  13550.     }
  13551.  
  13552.     /*
  13553.      * We're now pointing at the answer records.  Process the first
  13554.      * TXT record we find.
  13555.      */
  13556.  
  13557.     while (numanswers--) {
  13558.  
  13559.         /* First the name; use dn_expand to get the compressed size */
  13560.         len = dn_expand(answer.bytes, answer.bytes + size, p,
  13561.                         host, sizeof(host));
  13562.         if (len < 0)
  13563.             return 0;
  13564.         INCR_CHECK(p, len);
  13565.  
  13566.         /* Next is the query type */
  13567.         CHECK(p, 2);
  13568.         type = NTOHSP(p,2);
  13569.  
  13570.         /* Next is the query class; also skip over 4 byte TTL */
  13571.         CHECK(p,6);
  13572.         class = NTOHSP(p,6);
  13573.  
  13574.         /* Record data length - make sure we aren't truncated */
  13575.  
  13576.         CHECK(p,2);
  13577.         rdlen = NTOHSP(p,2);
  13578.  
  13579.         if (p + rdlen > answer.bytes + size)
  13580.             return 0;
  13581.  
  13582.         /*
  13583.          * If this is a TXT record, return the string.  Note that the
  13584.          * string has a 1-byte length in the front
  13585.          */
  13586.         /* XXX What about flagging multiple TXT records as an error?  */
  13587.  
  13588.         if (class == C_IN && type == T_TXT) {
  13589.             len = *p++;
  13590.             if (p + len > answer.bytes + size)
  13591.                 return 0;
  13592.             *retstr = malloc(len + 1);
  13593.             if (*retstr == NULL)
  13594.                 return ENOMEM;
  13595.             strncpy(*retstr, (char *) p, len);
  13596.             (*retstr)[len] = '\0';
  13597.             /* Avoid a common error. */
  13598.             if ( (*retstr)[len-1] == '.' )
  13599.                 (*retstr)[len-1] = '\0';
  13600.             return 1;
  13601.         }
  13602.     }
  13603.  
  13604.     return 0;
  13605. }
  13606. #undef INCR_CHECK
  13607. #undef CHECK
  13608. #undef NTOHSP
  13609. #endif /* CK_DNS_SRV */
  13610.  
  13611. #ifdef TNCODE
  13612. #ifdef CK_FORWARD_X
  13613. #ifdef UNIX
  13614. #include <sys/un.h>
  13615. #define FWDX_UNIX_SOCK
  13616. #ifndef AF_LOCAL
  13617. #define AF_LOCAL AF_UNIX
  13618. #endif
  13619. #ifndef PF_LOCAL
  13620. #define PF_LOCAL PF_UNIX
  13621. #endif
  13622. #ifndef SUN_LEN
  13623. /* Evaluate to actual length of the `sockaddr_un' structure.  */
  13624. #define SUN_LEN(ptr) ((size_t) (((struct sockaddr_un *) 0)->sun_path)         \
  13625.                       + strlen ((ptr)->sun_path))
  13626. #endif
  13627. #endif /* UNIX */
  13628. int
  13629. fwdx_create_listen_socket(screen) int screen; {
  13630. #ifdef NOPUTENV
  13631.     return(-1);
  13632. #else /* NOPUTENV */
  13633.     struct sockaddr_in saddr;
  13634.     int display, port, sock=-1, i;
  13635.     static char env[512];
  13636.  
  13637.     /*
  13638.      * X Windows Servers support multiple displays by listening on
  13639.      * one socket per display.  Display 0 is port 6000; Display 1 is
  13640.      * port 6001; etc.
  13641.      *
  13642.      * We start by trying to open port 6001 so that display 0 is
  13643.      * reserved for the local X Windows Server.
  13644.      */
  13645.  
  13646.     for ( display=1; display < 1000 ; display++  ) {
  13647.  
  13648.         if ((sock = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
  13649.             debug(F111,"fwdx_create_listen_socket()","socket() < 0",sock);
  13650.             return(-1);
  13651.         }
  13652.  
  13653.         port = 6000 + display;
  13654.         bzero((char *)&saddr, sizeof(saddr));
  13655.         saddr.sin_family = AF_INET;
  13656.         saddr.sin_addr.s_addr = inet_addr(myipaddr);
  13657.         saddr.sin_port = htons(port);
  13658.  
  13659.         if (bind(sock, (struct sockaddr *)&saddr, sizeof(saddr)) < 0) {
  13660.             i = errno;                  /* Save error code */
  13661. #ifdef TCPIPLIB
  13662.             socket_close(sock);
  13663. #else /* TCPIPLIB */
  13664.             close(sock);
  13665. #endif /* TCPIPLIB */
  13666.             sock = -1;
  13667.             debug(F110,"fwdx_create_listen_socket()","bind() < 0",0);
  13668.             continue;
  13669.         }
  13670.  
  13671.         debug(F100,"fdwx_create_listen_socket() bind OK","",0);
  13672.         break;
  13673.     }
  13674.  
  13675.     if ( display > 1000 ) {
  13676.         debug(F100,"fwdx_create_listen_socket() Out of Displays","",0);
  13677.         return(-1);
  13678.     }
  13679.  
  13680.     if (listen(sock, 5) < 0) {
  13681.         i = errno;                  /* Save error code */
  13682. #ifdef TCPIPLIB
  13683.         socket_close(sock);
  13684. #else /* TCPIPLIB */
  13685.         close(sock);
  13686. #endif /* TCPIPLIB */
  13687.         debug(F101,"fdwx_create_listen_socket() listen() errno","",errno);
  13688.         return(-1);
  13689.     }
  13690.     debug(F100,"fwdx_create_listen_socket() listen OK","",0);
  13691.  
  13692.     TELOPT_SB(TELOPT_FORWARD_X).forward_x.listen_socket = sock;
  13693.     if (!myipaddr[0])
  13694.         getlocalipaddr();
  13695.     if ( myipaddr[0] )
  13696.         ckmakxmsg(env,sizeof(env),"DISPLAY=",myipaddr,":",
  13697.                   ckuitoa(display),":",ckuitoa(screen),
  13698.                   NULL,NULL,NULL,NULL,NULL,NULL);
  13699.     else
  13700.         ckmakmsg(env,sizeof(env),"DISPLAY=",ckuitoa(display),":",
  13701.                  ckuitoa(screen));
  13702.     putenv(env);
  13703.     return(0);
  13704. #endif /* NOPUTENV */
  13705. }
  13706.  
  13707.  
  13708. int
  13709. fwdx_open_client_channel(channel) int channel; {
  13710.     char * env;
  13711.     struct sockaddr_in saddr;
  13712. #ifdef FWDX_UNIX_SOCK
  13713.     struct sockaddr_un saddr_un = { AF_LOCAL };
  13714. #endif /* FWDX_UNIX_SOCK */
  13715.     int colon, dot, display, port, sock, i, screen;
  13716.     int family;
  13717.     char buf[256], * host=NULL, * rest=NULL;
  13718. #ifdef TCP_NODELAY
  13719.     int on=1;
  13720. #endif /* TCP_NODELAY */
  13721.  
  13722.     debug(F111,"fwdx_create_client_channel()","channel",channel);
  13723.  
  13724.     for ( i=0; i<MAXFWDX ; i++ ) {
  13725.         if (TELOPT_SB(TELOPT_FORWARD_X).forward_x.channel[i].id == channel) {
  13726.             /* Already open */
  13727.             debug(F110,"fwdx_create_client_channel()","already open",0);
  13728.             return(0);
  13729.         }
  13730.     }
  13731.  
  13732.     env = getenv("DISPLAY");
  13733.     if ( !env )
  13734.         env = tn_get_display();
  13735.     if ( env )
  13736.         ckstrncpy(buf,env,256);
  13737.     else
  13738.         ckstrncpy(buf,"127.0.0.1:0.0",256);
  13739.  
  13740.     bzero((char *)&saddr,sizeof(saddr));
  13741.     saddr.sin_family = AF_INET;
  13742.  
  13743.     if (!fwdx_parse_displayname(buf,
  13744.                                 &family,
  13745.                                 &host,
  13746.                                 &display,
  13747.                                 &screen,
  13748.                                 &rest
  13749.                                 )
  13750.         ) {
  13751.         if ( host ) free(host);
  13752.         if ( rest ) free(rest);
  13753.         return(0);
  13754.     }
  13755.     if (rest) free(rest);
  13756.  
  13757. #ifndef FWDX_UNIX_SOCK
  13758.   /* if $DISPLAY indicates use of unix domain sockets, but we don't support it,
  13759.    * we change things to use inet sockets on the ip loopback interface instead,
  13760.    * and hope that it works.
  13761.    */
  13762.     if (family == FamilyLocal) {
  13763.         debug(F100,"fwdx_create_client_channel() FamilyLocal","",0);
  13764.         family = FamilyInternet;
  13765.         if (host) free(host);
  13766.         if (host = malloc(strlen("localhost") + 1))
  13767.             strcpy(host, "localhost");
  13768.         else {
  13769.             return(-1);
  13770.         }
  13771.     }
  13772. #else /* FWDX_UNIX_SOCK */
  13773.     if (family == FamilyLocal) {
  13774.         if (host) free(host);
  13775.         sock = socket(PF_LOCAL, SOCK_STREAM, 0);
  13776.         if (sock < 0)
  13777.             return(-1);
  13778.  
  13779.         ckmakmsg(buf,sizeof(buf),"/tmp/.X11-unix/X",ckitoa(display),NULL,NULL);
  13780.         strncpy(saddr_un.sun_path, buf, sizeof(saddr_un.sun_path));
  13781.         if (connect(sock,(struct sockaddr *)&saddr_un, SUN_LEN(&saddr_un)) < 0)
  13782.           return(-1);
  13783.     } else
  13784. #endif  /* FWDX_UNIX_SOCK */
  13785.     {
  13786.         /* Otherwise, we are assuming FamilyInternet */
  13787.         if (host) {
  13788.             ckstrncpy(buf,host,sizeof(buf));
  13789.             free(host);
  13790.         } else
  13791.             ckstrncpy(buf,myipaddr,sizeof(buf));
  13792.  
  13793.         debug(F111,"fwdx_create_client_channel()","display",display);
  13794.  
  13795.         port = 6000 + display;
  13796.         saddr.sin_port = htons(port);
  13797.  
  13798.         debug(F110,"fwdx_create_client_channel() ip-address",buf,0);
  13799.         saddr.sin_addr.s_addr = inet_addr(buf);
  13800.         if ( saddr.sin_addr.s_addr == (unsigned long) -1
  13801. #ifdef INADDR_NONE
  13802.              || saddr.sin_addr.s_addr == INADDR_NONE
  13803. #endif /* INADDR_NONE */
  13804.              )
  13805.         {
  13806.             struct hostent *host;
  13807.             host = gethostbyname(buf);
  13808.             if ( host == NULL )
  13809.                 return(-1);
  13810.             host = ck_copyhostent(host);
  13811. #ifdef HADDRLIST
  13812. #ifdef h_addr
  13813.             /* This is for trying multiple IP addresses - see <netdb.h> */
  13814.             if (!(host->h_addr_list))
  13815.                 return(-1);
  13816.             bcopy(host->h_addr_list[0],
  13817.                    (caddr_t)&saddr.sin_addr,
  13818.                    host->h_length
  13819.                    );
  13820. #else
  13821.             bcopy(host->h_addr, (caddr_t)&saddr.sin_addr, host->h_length);
  13822. #endif /* h_addr */
  13823. #else  /* HADDRLIST */
  13824.             bcopy(host->h_addr, (caddr_t)&saddr.sin_addr, host->h_length);
  13825. #endif /* HADDRLIST */
  13826.         }
  13827.  
  13828.         if ((sock = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
  13829.             debug(F111,"fwdx_create_client_channel()","socket() < 0",sock);
  13830.             return(-1);
  13831.         }
  13832.  
  13833.         if ( connect(sock, (struct sockaddr *)&saddr, sizeof(saddr)) < 0) {
  13834.             debug(F110,"fwdx_create_client_channel()","connect() failed",0);
  13835. #ifdef TCPIPLIB
  13836.             socket_close(sock);
  13837. #else /* TCPIPLIB */
  13838.             close(sock);
  13839. #endif /* TCPIPLIB */
  13840.             return(-1);
  13841.         }
  13842.  
  13843. #ifdef TCP_NODELAY
  13844.         setsockopt(sock,IPPROTO_TCP,TCP_NODELAY,(char *)&on,sizeof(on));
  13845. #endif /* TCP_NODELAY */
  13846.     }
  13847.  
  13848.     for (i = 0; i < MAXFWDX; i++) {
  13849.      if (TELOPT_SB(TELOPT_FORWARD_X).forward_x.channel[i].id == -1) {
  13850.          TELOPT_SB(TELOPT_FORWARD_X).forward_x.channel[i].fd = sock;
  13851.          TELOPT_SB(TELOPT_FORWARD_X).forward_x.channel[i].id = channel;
  13852.        TELOPT_SB(TELOPT_FORWARD_X).forward_x.channel[i].need_to_send_xauth = 1;
  13853.          debug(F111,"fwdx_create_client_channel()","socket",sock);
  13854.          return(0);
  13855.      }
  13856.     }
  13857.     return(-1);
  13858. }
  13859.  
  13860. int
  13861. fwdx_server_avail() {
  13862.     char * env;
  13863.     struct sockaddr_in saddr;
  13864. #ifdef FWDX_UNIX_SOCK
  13865.     struct sockaddr_un saddr_un = { AF_LOCAL };
  13866. #endif  /* FWDX_UNIX_SOCK */
  13867.     int colon, dot, display, port, sock, i, screen;
  13868.     char buf[256], *host=NULL, *rest=NULL;
  13869. #ifdef TCP_NODELAY
  13870.     int on=1;
  13871. #endif /* TCP_NODELAY */
  13872.     int family;
  13873.  
  13874.     env = getenv("DISPLAY");
  13875.     if ( !env )
  13876.         env = tn_get_display();
  13877.     if ( env )
  13878.         ckstrncpy(buf,env,256);
  13879.     else
  13880.         ckstrncpy(buf,"127.0.0.1:0.0",256);
  13881.  
  13882.     bzero((char *)&saddr,sizeof(saddr));
  13883.     saddr.sin_family = AF_INET;
  13884.  
  13885.     if (!fwdx_parse_displayname(buf,&family,&host,&display,&screen,&rest)) {
  13886.         if ( host ) free(host);
  13887.         if ( rest ) free(rest);
  13888.         return(0);
  13889.     }
  13890.     if (rest) free(rest);
  13891.  
  13892. #ifndef FWDX_UNIX_SOCK
  13893.   /* if $DISPLAY indicates use of unix domain sockets, but we don't support it,
  13894.    * we change things to use inet sockets on the ip loopback interface instead,
  13895.    * and hope that it works.
  13896.    */
  13897.     if (family == FamilyLocal) {
  13898.         family = FamilyInternet;
  13899.         if (host) free(host);
  13900.         if (host = malloc(strlen("localhost") + 1))
  13901.             strcpy(host, "localhost");
  13902.         else {
  13903.             return(-1);
  13904.         }
  13905.     }
  13906. #else /* FWDX_UNIX_SOCK */
  13907.     if (family == FamilyLocal) {
  13908.         debug(F100,"fwdx_server_avail() FamilyLocal","",0);
  13909.         if (host) free(host);
  13910.         sock = socket(PF_LOCAL, SOCK_STREAM, 0);
  13911.         if (sock < 0)
  13912.             return(0);
  13913.  
  13914.         ckmakmsg(buf,sizeof(buf),"/tmp/.X11-unix/X",ckitoa(display),NULL,NULL);
  13915.         strncpy(saddr_un.sun_path, buf, sizeof(saddr_un.sun_path));
  13916.         if (connect(sock,(struct sockaddr *)&saddr_un,SUN_LEN(&saddr_un)) < 0)
  13917.             return(0);
  13918.         close(sock);
  13919.         return(1);
  13920.     }
  13921. #endif  /* FWDX_UNIX_SOCK */
  13922.  
  13923.     /* Otherwise, we are assuming FamilyInternet */
  13924.     if (host) {
  13925.         ckstrncpy(buf,host,sizeof(buf));
  13926.         free(host);
  13927.     } else
  13928.         ckstrncpy(buf,myipaddr,sizeof(buf));
  13929.  
  13930.     debug(F111,"fwdx_server_avail()","display",display);
  13931.  
  13932.     port = 6000 + display;
  13933.     saddr.sin_port = htons(port);
  13934.  
  13935.     debug(F110,"fwdx_server_avail() ip-address",buf,0);
  13936.     saddr.sin_addr.s_addr = inet_addr(buf);
  13937.     if ( saddr.sin_addr.s_addr == (unsigned long) -1
  13938. #ifdef INADDR_NONE
  13939.          || saddr.sin_addr.s_addr == INADDR_NONE
  13940. #endif /* INADDR_NONE */
  13941.          )
  13942.     {
  13943.         struct hostent *host;
  13944.         host = gethostbyname(buf);
  13945.         if ( host == NULL ) {
  13946.             debug(F110,"fwdx_server_avail() gethostbyname() failed",
  13947.                    myipaddr,0);
  13948.             return(-1);
  13949.         }
  13950.         host = ck_copyhostent(host);
  13951. #ifdef HADDRLIST
  13952. #ifdef h_addr
  13953.         /* This is for trying multiple IP addresses - see <netdb.h> */
  13954.         if (!(host->h_addr_list))
  13955.             return(-1);
  13956.         bcopy(host->h_addr_list[0],
  13957.                (caddr_t)&saddr.sin_addr,
  13958.                host->h_length
  13959.                );
  13960. #else
  13961.         bcopy(host->h_addr, (caddr_t)&saddr.sin_addr, host->h_length);
  13962. #endif /* h_addr */
  13963. #else  /* HADDRLIST */
  13964.         bcopy(host->h_addr, (caddr_t)&saddr.sin_addr, host->h_length);
  13965. #endif /* HADDRLIST */
  13966.     }
  13967.  
  13968.     if ((sock = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
  13969.         debug(F111,"fwdx_server_avail()","socket() < 0",sock);
  13970.         return(0);
  13971.     }
  13972.  
  13973.     if ( connect(sock, (struct sockaddr *)&saddr, sizeof(saddr)) < 0) {
  13974.         debug(F110,"fwdx_server_avail()","connect() failed",0);
  13975. #ifdef TCPIPLIB
  13976.         socket_close(sock);
  13977. #else /* TCPIPLIB */
  13978.         close(sock);
  13979. #endif /* TCPIPLIB */
  13980.         return(0);
  13981.     }
  13982.  
  13983. #ifdef TCPIPLIB
  13984.     socket_close(sock);
  13985. #else /* TCPIPLIB */
  13986.     close(sock);
  13987. #endif /* TCPIPLIB */
  13988.     return(1);
  13989. }
  13990.  
  13991. int
  13992. fwdx_open_server_channel() {
  13993.     int sock, ready_to_accept, sock2,channel,i;
  13994. #ifdef TCP_NODELAY
  13995.     int on=1;
  13996. #endif /* TCP_NODELAY */
  13997. #ifdef UCX50
  13998.     static u_int saddrlen;
  13999. #else
  14000.     static SOCKOPT_T saddrlen;
  14001. #endif /* UCX50 */
  14002.     struct sockaddr_in saddr;
  14003.     char sb[8];
  14004.     extern char tn_msg[];
  14005. #ifdef BSDSELECT
  14006.     fd_set rfds;
  14007.     struct timeval tv;
  14008. #else
  14009. #ifdef BELLSELCT
  14010.     fd_set rfds;
  14011. #else
  14012.     fd_set rfds;
  14013.     struct timeval {
  14014.         long tv_sec;
  14015.         long tv_usec;
  14016.     } tv;
  14017. #endif /* BELLSELECT */
  14018. #endif /* BSDSELECT */
  14019.     unsigned short nchannel;
  14020.     unsigned char * p;
  14021.  
  14022.     sock = TELOPT_SB(TELOPT_FORWARD_X).forward_x.listen_socket;
  14023.  
  14024.   try_again:
  14025.  
  14026. #ifdef BSDSELECT
  14027.     tv.tv_sec  = tv.tv_usec = 0L;
  14028.     tv.tv_usec = 50;
  14029.     FD_ZERO(&rfds);
  14030.     FD_SET(sock, &rfds);
  14031.     ready_to_accept =
  14032.         ((select(FD_SETSIZE,
  14033. #ifdef HPUX
  14034. #ifdef HPUX1010
  14035.                   (fd_set *)
  14036. #else
  14037.  
  14038.                   (int *)
  14039. #endif /* HPUX1010 */
  14040. #else
  14041. #ifdef __DECC
  14042.                   (fd_set *)
  14043. #endif /* __DECC */
  14044. #endif /* HPUX */
  14045.                   &rfds, NULL, NULL, &tv) > 0) &&
  14046.           FD_ISSET(sock, &rfds));
  14047. #else /* BSDSELECT */
  14048. #ifdef IBMSELECT
  14049.     ready_to_accept = (select(&sock, 1, 0, 0, 50) == 1);
  14050. #else
  14051. #ifdef BELLSELECT
  14052.     FD_ZERO(rfds);
  14053.     FD_SET(sock, rfds);
  14054.     ready_to_accept =
  14055.         ((select(128, rfds, NULL, NULL, 50) > 0) &&
  14056.           FD_ISSET(sock, rfds));
  14057. #else
  14058. /* Try this - what's the worst that can happen... */
  14059.  
  14060.     tv.tv_sec  = tv.tv_usec = 0L;
  14061.     tv.tv_usec = 50;
  14062.     FD_ZERO(&rfds);
  14063.     FD_SET(sock, &rfds);
  14064.     ready_to_accept =
  14065.         ((select(FD_SETSIZE,
  14066.                   (fd_set *) &rfds, NULL, NULL, &tv) > 0) &&
  14067.           FD_ISSET(sock, &rfds));
  14068. #endif /* BELLSELECT */
  14069. #endif /* IBMSELECT */
  14070. #endif /* BSDSELECT */
  14071.  
  14072.     if ( !ready_to_accept )
  14073.         return(0);
  14074.  
  14075.     if ((sock2 = accept(sock,(struct sockaddr *)&saddr,&saddrlen)) < 0) {
  14076.         int i = errno;                  /* save error code */
  14077.         debug(F101,"tcpsrv_open accept errno","",i);
  14078.         return(-1);
  14079.     }
  14080.  
  14081.     /*
  14082.      * Now we have the open socket.  We must now find a channel to store
  14083.      * it in, and then notify the client.
  14084.      */
  14085.  
  14086.     for ( channel=0;channel<MAXFWDX;channel++ ) {
  14087.         if ( TELOPT_SB(TELOPT_FORWARD_X).forward_x.channel[channel].fd == -1 )
  14088.             break;
  14089.     }
  14090.  
  14091.     if ( channel == MAXFWDX ) {
  14092. #ifdef TCPIPLIB
  14093.         socket_close(sock2);
  14094. #else /* TCPIPLIB */
  14095.         close(sock2);
  14096. #endif /* TCPIPLIB */
  14097.         return(-1);
  14098.     }
  14099.  
  14100.     if ( fwdx_send_open(channel) < 0 ) {
  14101. #ifdef TCPIPLIB
  14102.         socket_close(sock2);
  14103. #else /* TCPIPLIB */
  14104.         close(sock2);
  14105. #endif /* TCPIPLIB */
  14106.     }
  14107.  
  14108. #ifdef TCP_NODELAY
  14109.     setsockopt(sock2,IPPROTO_TCP,TCP_NODELAY,(char *)&on,sizeof(on));
  14110. #endif /* TCP_NODELAY */
  14111.  
  14112.     TELOPT_SB(TELOPT_FORWARD_X).forward_x.channel[channel].fd = sock2;
  14113.     TELOPT_SB(TELOPT_FORWARD_X).forward_x.channel[channel].id = channel;
  14114.     goto try_again;
  14115.  
  14116.     return(0);  /* never reached */
  14117. }
  14118.  
  14119. int
  14120. fwdx_close_channel(channel) int channel; {
  14121.     int i,fd;
  14122.  
  14123.     for ( i=0; i<MAXFWDX ; i++ ) {
  14124.         if (TELOPT_SB(TELOPT_FORWARD_X).forward_x.channel[i].id == channel)
  14125.             break;
  14126.     }
  14127.     if ( i == MAXFWDX )
  14128.         return(-1);
  14129.  
  14130.     fd = TELOPT_SB(TELOPT_FORWARD_X).forward_x.channel[i].fd;
  14131.     TELOPT_SB(TELOPT_FORWARD_X).forward_x.channel[i].fd = -1;
  14132.     TELOPT_SB(TELOPT_FORWARD_X).forward_x.channel[i].id = -1;
  14133.     TELOPT_SB(TELOPT_FORWARD_X).forward_x.channel[i].need_to_send_xauth = 0;
  14134.  
  14135. #ifdef TCPIPLIB
  14136.     socket_close(fd);
  14137. #else /* TCPIPLIB */
  14138.     close(fd);
  14139. #endif /* TCPIPLIB */
  14140.     return(0);
  14141. }
  14142.  
  14143. int
  14144. fwdx_close_all() {
  14145.     int x,fd;
  14146.  
  14147.     debug(F111,"fwdx_close_all()",
  14148.           "TELOPT_SB(TELOPT_FORWARD_X).forward_x.listen_socket",
  14149.           TELOPT_SB(TELOPT_FORWARD_X).forward_x.listen_socket);
  14150.  
  14151.     if ( TELOPT_SB(TELOPT_FORWARD_X).forward_x.listen_socket != -1 ) {
  14152. #ifdef TCPIPLIB
  14153.         socket_close(TELOPT_SB(TELOPT_FORWARD_X).forward_x.listen_socket);
  14154. #else /* TCPIPLIB */
  14155.         close(TELOPT_SB(TELOPT_FORWARD_X).forward_x.listen_socket);
  14156. #endif /* TCPIPLIB */
  14157.         TELOPT_SB(TELOPT_FORWARD_X).forward_x.listen_socket = -1;
  14158.     }
  14159.  
  14160.     for (x = 0; x < MAXFWDX; x++) {
  14161.      if (TELOPT_SB(TELOPT_FORWARD_X).forward_x.channel[x].fd != -1) {
  14162.       fd = TELOPT_SB(TELOPT_FORWARD_X).forward_x.channel[x].fd;
  14163.       TELOPT_SB(TELOPT_FORWARD_X).forward_x.channel[x].fd = -1;
  14164.       TELOPT_SB(TELOPT_FORWARD_X).forward_x.channel[x].id = -1;
  14165.       TELOPT_SB(TELOPT_FORWARD_X).forward_x.channel[x].need_to_send_xauth = 0;
  14166.       TELOPT_SB(TELOPT_FORWARD_X).forward_x.channel[x].suspend = 0;
  14167. #ifdef TCPIPLIB
  14168.       socket_close(fd);
  14169. #else /* TCPIPLIB */
  14170.       close(fd);
  14171. #endif /* TCPIPLIB */
  14172.      }
  14173.     }
  14174.     return(0);
  14175. }
  14176.  
  14177. /* The following definitions are for Unix */
  14178. #ifndef socket_write
  14179. #define socket_write(f,s,n)    write(f,s,n)
  14180. #endif /* socket_write */
  14181. #ifndef socket_read
  14182. #define socket_read(f,s,n)     read(f,s,n)
  14183. #endif /* socket_read */
  14184.  
  14185. int
  14186. fwdx_write_data_to_channel(channel, data, len)
  14187.     int channel; char * data; int len;
  14188. {
  14189.     int sock, count, try=0, length = len, i;
  14190.  
  14191.     if ( len <= 0 )
  14192.         return(0);
  14193.  
  14194.     for ( i=0; i<MAXFWDX ; i++ ) {
  14195.         if (TELOPT_SB(TELOPT_FORWARD_X).forward_x.channel[i].id == channel)
  14196.             break;
  14197.     }
  14198.     if ( i == MAXFWDX ) {
  14199.         debug(F110,"fwdx_write_data_to_channel",
  14200.                "attempting to write to closed channel",0);
  14201.         return(-1);
  14202.     }
  14203.  
  14204.     sock = TELOPT_SB(TELOPT_FORWARD_X).forward_x.channel[i].fd;
  14205.     debug(F111,"fwdx_write_data_to_channel","socket",sock);
  14206.     hexdump("fwdx_write_data_to_channel",data,len);
  14207.  
  14208.   fwdx_write_data_to_channel_retry:
  14209.  
  14210.     if ((count = socket_write(sock,data,len)) < 0) {
  14211.         int s_errno = socket_errno; /* maybe a function */
  14212.         debug(F101,"fwdx_write_data_to_channel socket_write error","",s_errno);
  14213. #ifdef BETATEST
  14214.         printf("fwdx_write_data_to_channel error\r\n");
  14215. #endif /* BETATEST */
  14216. #ifdef OS2
  14217.         if (os2socketerror(s_errno) < 0)
  14218.             return(-2);
  14219. #endif /* OS2 */
  14220.         return(-1);                 /* Call it an i/o error */
  14221.     }
  14222.     if (count < len) {
  14223.         debug(F111,"fwdx_write_data_to_channel socket_write",data,count);
  14224.         if (count > 0) {
  14225.             data += count;
  14226.             len -= count;
  14227.         }
  14228.         debug(F111,"fwdx_write_data_to_channel retry",data,len);
  14229.         if ( len > 0 )
  14230.             goto fwdx_write_data_to_channel_retry;
  14231.     }
  14232.  
  14233.     debug(F111,"fwdx_write_data_to_channel complete",data,length);
  14234.     return(length); /* success - return total length */
  14235. }
  14236.  
  14237. VOID
  14238. fwdx_check_sockets(fd_set *ibits)
  14239. {
  14240.     int x, sock, channel, bytes;
  14241.     static char buffer[32000];
  14242.  
  14243.     debug(F100,"fwdx_check_sockets()","",0);
  14244.     if ( sstelnet && !TELOPT_ME(TELOPT_FORWARD_X) ||
  14245.          !sstelnet && !TELOPT_U(TELOPT_FORWARD_X)) {
  14246.         debug(F110,"fwdx_check_sockets()","TELOPT_FORWARD_X not negotiated",0);
  14247.         return;
  14248.     }
  14249.  
  14250.     for (x = 0; x < MAXFWDX; x++) {
  14251.         if ( TELOPT_SB(TELOPT_FORWARD_X).forward_x.channel[x].fd == -1 ||
  14252.              TELOPT_SB(TELOPT_FORWARD_X).forward_x.channel[x].suspend )
  14253.             continue;
  14254.  
  14255.         sock = TELOPT_SB(TELOPT_FORWARD_X).forward_x.channel[x].fd;
  14256.         if (FD_ISSET(sock, ibits))
  14257.         {
  14258.             channel = TELOPT_SB(TELOPT_FORWARD_X).forward_x.channel[x].id;
  14259.             debug(F111,"fwdx_check_sockets()","channel set",channel);
  14260.  
  14261.             bytes = socket_read(sock, buffer, sizeof(buffer));
  14262.             if (bytes > 0)
  14263.                 fwdx_send_data_from_channel(channel, buffer, bytes);
  14264.             else if (bytes == 0) {
  14265.                 fwdx_close_channel(channel);
  14266.                 fwdx_send_close(channel);
  14267.             }
  14268.         }
  14269.     }
  14270. }
  14271.  
  14272. int
  14273. fwdx_init_fd_set(fd_set *ibits)
  14274. {
  14275.     int x,set=0,cnt=0;
  14276.  
  14277.     if ( sstelnet && !TELOPT_ME(TELOPT_FORWARD_X) ||
  14278.          !sstelnet && !TELOPT_U(TELOPT_FORWARD_X)) {
  14279.         debug(F110,"fwdx_init_fd_set()","TELOPT_FORWARD_X not negotiated",0);
  14280.         return(0);
  14281.     }
  14282.  
  14283.     if (TELOPT_SB(TELOPT_FORWARD_X).forward_x.listen_socket != -1) {
  14284.         set++;
  14285.         FD_SET(TELOPT_SB(TELOPT_FORWARD_X).forward_x.listen_socket, ibits);
  14286.     }
  14287.     for (x = 0; x < MAXFWDX; x++) {
  14288.         if (TELOPT_SB(TELOPT_FORWARD_X).forward_x.channel[x].fd != -1) {
  14289.             cnt++;
  14290.             if (TELOPT_SB(TELOPT_FORWARD_X).forward_x.channel[x].suspend)
  14291.                 continue;
  14292.             set++;
  14293.             FD_SET(TELOPT_SB(TELOPT_FORWARD_X).forward_x.channel[x].fd, ibits);
  14294.         }
  14295.     }
  14296.     if (set + cnt == 0) {
  14297.         return(-1);
  14298.     } else {
  14299.         return(set);
  14300.     }
  14301. }
  14302.  
  14303. #ifdef NT
  14304. VOID
  14305. fwdx_thread( VOID * dummy )
  14306. {
  14307.     fd_set ifds;
  14308.     struct timeval tv;
  14309.     extern int priority;
  14310.     int n;
  14311.  
  14312.     setint();
  14313.     SetThreadPrty(priority,isWin95() ? 3 : 11);
  14314.  
  14315.     while ( !sstelnet && TELOPT_U(TELOPT_FORWARD_X) ||
  14316.             sstelnet && TELOPT_ME(TELOPT_FORWARD_X))
  14317.     {
  14318.         FD_ZERO(&ifds);
  14319.         n = fwdx_init_fd_set(&ifds);
  14320.         if (n > 0) {
  14321.             tv.tv_sec = 0;
  14322.             tv.tv_usec = 2500;
  14323.             if ( select(FD_SETSIZE, &ifds, NULL, NULL, &tv) > 0 )
  14324.                 fwdx_check_sockets(&ifds);
  14325.  
  14326.         } else if (n < 0) {
  14327.             TELOPT_SB(TELOPT_FORWARD_X).forward_x.thread_started = 0;
  14328.             ckThreadEnd(NULL);
  14329.         } else {
  14330.             sleep(1);
  14331.         }
  14332.     }
  14333. }
  14334. #endif /* NT */
  14335. #endif /* CK_FORWARD_X */
  14336. #endif /* TNCODE */
  14337. #endif /* NETCONN */
  14338.