📄 ckcnet.c
字号:
char *cknetv = "Network support, 5A(015) 23 Nov 92";/* C K C N E T -- Network support *//* Authors: Frank da Cruz (fdc@columbia.edu, FDCCU@CUVMA.BITNET), Columbia University Center for Computing Activities. netopen() routine for TCP/IP originally by Ken Yap, Rochester University (ken@cs.rochester.edu) (no longer at that address). Missing pieces for Excelan sockets library from William Bader, Moravian College <bader@moravian.edu>. TELNET protocol by Frank da Cruz. TGV MultiNet code by Frank da Cruz. MultiNet code adapted to WIN/TCP by Ray Hunter of TWG. MultiNet code adapted to DEC TCP/IP by Lee Tibbert of DEC and Frank da Cruz. SunLink X.25 support by Marcello Frutig, Catholic University, Rio de Janeiro, Brazil (frutig@rnp.impa.br) with fixes from Stefaan Eeckels, Eurokom, Luxembourg. Other contributions as indicated below. Copyright (C) 1985, 1992, Trustees of Columbia University in the City of New York. Permission is granted to any individual or institution to use this software as long as it is not sold for profit. This copyright notice must be retained. This software may not be included in commercial products without written permission of Columbia University.*/#include "ckcdeb.h"#include "ckcker.h"#ifdef I386IX /* Has to come before ckcnet.h in */#include <errno.h> /* this version, but after in others */#endif /* I386IX */#include "ckcnet.h"#ifdef NETCONN/* Don't need these if there is no network support. */#ifdef WINTCP#include <errno.h>#include <setjmp.h>#include <signal.h>/* * The WIN/TCP code path is the same as that for Multinet. Only the routine * names have changed ... */#define socket_errno errno#define socket_read netread#define socket_ioctl ioctl#define socket_write netwrite#define socket_perror win$perror#define socket_close netclose#else /* Not WINTCP */#ifndef I386IX#include <errno.h>#endif /* I386IX */#include <signal.h>#ifndef ZILOG#include <setjmp.h>#else#include <setret.h>#endif /* ZILOG */#endif /* WINTCP */#ifdef datageneral /* Data General AOS/VS */#include <:usr:include:vs_tcp_errno.h>#include <:usr:include:sys:vs_tcp_types.h>#include <:usr:include:sys:socket.h>#include <:usr:include:netinet:in.h>#include <:usr:include:netdb.h>#endif /* datageneral */extern SIGTYP (*saval)(); /* For saving alarm handler */_PROTOTYP( VOID bgchk, (void) );extern int /* External variables */ duplex, debses, seslog, ttyfd, quiet, msgflg;#ifdef SVR4/* These suggested by Rob Healey, rhealey@kas.helios.mn.org, to avoid bugs in Berkeley compatibility library on Sys V R4 systems, but untested by me (fdc). Remove this bit if it gives you trouble. (Later corrected by Marc Boucher <mboucher@iro.umontreal.ca> because bzero/bcopy are not argument-compatible with memset/memcpy|memmove.)*/#define bzero(s,n) memset(s,0,n)#define bcopy(h,a,l) memmove(a,h,l)#else#ifdef PTX /* Sequent DYNIX PTX 1.3 */#define bzero(s,n) memset(s,0,n)#define bcopy(h,a,l) memcpy(a,h,l)#endif /* PTX */#endif /* SVR4 */#define NAMECPYL 100 /* Local copy of hostname */static char namecopy[NAMECPYL];char ipaddr[20] = { '\0' }; /* Global copy of IP address *//* VMSTCPIP means "DEC_TCPIP or MULTINET or WINTCP" (defined in ckcnet.h).*/#ifdef VMSTCPIP/* General global variables, but so far used only by MultiNet and WIN/TCP. Kept within #ifdef MULTINET..#endif to keep strict compilers (and lint) from complaining about unused variables.*/static jmp_buf njbuf; /* For timeout longjumps */#endif /* VMSTCPIP */#endif /* NETCONN */int ttnet = NET_NONE; /* Network type */int ttnproto = NP_NONE; /* Network virtual terminal protocol */int tn_init = 0; /* Telnet protocol initialized flag */int tn_duplex = 1; /* Initial echo status */char *tn_term = NULL; /* Terminal type override */int tn_nlm = 1; /* Telnet CR -> CR LF mode */#ifndef NETCONN/* Network support not defined. Dummy functions here in case #ifdef's forgotten elsewhere.*/int /* Open network connection */netopen(name, lcl, nett) char *name; int *lcl, nett; { return(-1);}int /* Close network connection */netclos() { return(-1);}int /* Check network input buffer */nettchk() { return(-1);}int /* Flush network input buffer */netflui() { return(-1);}int /* Send network BREAK */netbreak() { return(-1);}int /* Input character from network */netinc(timo) int timo; {}int /* Output character to network */#ifdef CK_ANSICnettoc(char c)#elsenettoc(c) char c;#endif /* CK_ANSIC *//* nettoc */ { return(-1);}intnettol(s,n) char *s; int n; { return(-1);}#else /* NETCONN is defined (rest of this module...) */#ifdef VMSTCPIP/* For buffered network reads... *//* If the buffering code is written right, it shouldn't matter how long this buffer is -- it could even be shorter than a Kermit packet.*/#define TTIBUFL 8192 /* Maybe 8K?... */CHAR ttibuf[TTIBUFL+1];int ttibp = 0, ttibn = 0;/* Read bytes from network into internal buffer ttibuf[]. To be called when input buffer is empty, i.e. when ttibn == 0. Other network reading routines, like ttinc, ttinl, ttxin, should check the internal buffer first, and call this routine for a refill if necessary. Returns -1 on error, 0 if nothing happens. When data is read successfully, returns number of bytes read, and sets global ttibn to that number and ttibp (the buffer pointer) to zero.*/intttbufr() { /* TT Buffer Read */ int count; if (ttnet != NET_TCPB) { /* First make sure current net is */ return(-1); /* TCP/IP; if not, do nothing. */ } else { if (ttibn > 0) /* Out internal buffer is not empty, */ return(ttibn); /* so keep using it. */#ifdef WINTCP count = 512; /* This works for WIN/TCP */#else /* Not WINTCP, i.e it's Multinet */#ifdef DEC_TCPIP count = 512; /* This works for WIN/TCP */#else /* Not WINTCP, i.e it's Multinet */ count = nettchk(); /* Check network input buffer, */ if (ttibn > 0) return(ttibn); /* which can put a char there! */ if (count < 0) /* Read error */ return(-1); else if (count > TTIBUFL) /* Too many to read */ count = TTIBUFL; else if (count == 0) /* None, so force blocking read */ count = 1;#endif /* DEC_TCPIP */#endif /* WINTCP */ debug(F101,"ttbufr count 1","",count);#ifdef COMMENT/* This is for nonblocking reads, which we don't do any more. This code didn't work anyway, in the sense that a broken connection was never sensed.*/ if ((count = socket_read(ttyfd,ttibuf,count)) < 1) { if (count == -1 && socket_errno == EWOULDBLOCK) { debug(F100,"ttbufr finds nothing","",0); return(0); } else if (count == 0) { debug(F100,"ttbufr socket eof","",0); return(-1); } else { debug(F101,"ttbufr socket_read error","",socket_errno); return(-1); } }#else/* This is for blocking reads */ if ((count = socket_read(ttyfd,ttibuf,count)) < 1) { debug(F101,"ttbufr socket_read","",count); debug(F101,"ttbufr socket_errno","",socket_errno); return(-1); }#endif /* COMMENT */ ttibp = 0; /* Reset buffer pointer. */ ttibn = count;#ifdef DEBUG debug(F101,"ttbufr count 2","",count); /* Got some bytes. */ if (count > 0) ttibuf[count] = '\0'; debug(F111,"ttbufr ttibuf",ttibuf,ttibp);#endif /* DEBUG */ return(ttibn); /* Return buffer count. */ }}#endif /* VMSTCPIP *//* C-Kermit network open/close functions for BSD-sockets. Much of this code shared by SunLink X.25, which also uses the socket library.*//* N E T O P E N -- Open a network connection. *//* Returns 0 on success, -1 on failure. */#define TELNET_PORT 23 /* Should do lookup, but it won't change *//* This symbol is not known to, e.g., Ultrix 2.0 */#ifndef TELOPT_TTYPE#define TELOPT_TTYPE 24#endif /* TELOPT_TTYPE *//* N E T O P N -- Open a network connection. *//* Call with: name of host (or host:service), lcl - local-mode flag to be set if this function succeeds, network type - value defined in ckunet.h.*/#ifdef EXCELAN/* Most other BSD sockets implementations define these in header files and libraries.*/struct servent { unsigned short s_port;};struct hostent { short h_addrtype; struct in_addr h_addr; int h_length;};struct servent *getservbyname(service, connection) char *service,*connection; { static struct servent servrec; int port; port = 0; if (strcmp(service, "telnet") == 0) port = 23; else if (strcmp(service, "smtp") == 0) port = 25; else port = atoi(service); debug(F101,"getservbyname return port ","",port); if (port > 0) { servrec.s_port = htons(port); return( &servrec ); } return( (struct servent *) NULL );}struct hostent *gethostbyname(hostname) char *hostname; { return( (struct hostent *) NULL );}unsigned longinet_addr(name) char *name; { unsigned long addr; addr = rhost(&name); debug(F111,"inet_addr ",name,(int)addr); return(addr);}char *inet_ntoa(in) struct in_addr in; { static char name[80]; sprintf(name, "%d.%d.%d.%d", in.s_net, in.s_host, in.s_lh, in.s_impno); return(name);}#endif /* EXCELAN *//* N E T O P E N -- Open a network connection *//* Calling conventions same as ttopen(), except third argument is network type rather than modem type. Designed to be called from within ttopen.*/intnetopen(name, lcl, nett) char *name; int *lcl, nett; { char *p;#ifdef SO_OOBINLINE int on = 1;#endif /* SO_OOBINLINE */ int i, x; struct servent *service, servrec; struct hostent *host; struct sockaddr_in saddr;#ifdef EXCELAN struct sockaddr_in send_socket;#endif /* EXCELAN */#ifdef SUNX25 /* Code for SunLink X.25 support */#define X29PID 1 /* X.29 Protocol ID */ VOID x25oobh(); CONN_DB x25host; FACILITY_DB x25facil; static int needh = 1; PID_T pid; extern int linkid, lcn, x25ver; extern int revcall, closgr, cudata; extern char udata[MAXCUDATA];#endif /* SUNX25 */ debug(F101,"netopen nett","",nett); *ipaddr = '\0'; /* Initialize IP address string */#ifdef SUNX25 if (nett == NET_SX25) { /* If network type is X.25 */ netclos(); /* Close any previous net connection */ ttnproto = NP_NONE; /* No protocol selected yet */ /* Set up host structure */ bzero ((char *)&x25host,sizeof(x25host)); if ((x25host.hostlen = pkx121 (name,x25host.host)) < 0) { fprintf (stderr,"Invalid X.121 host address %s\n",name); errno = 0; return (-1); } x25host.datalen = X29PIDLEN; x25host.data[0] = X29PID; /* Set call user data if specified */ if (cudata) { strncpy(x25host.data+X29PIDLEN,udata,(int)strlen(udata)); x25host.datalen += (int)strlen(udata); } /* Open SunLink X.25 socket */ if ((ttyfd = socket (AF_X25, SOCK_STREAM, 0)) < 0) { debug(F101,"netopen socket error","",errno); perror ("X.25 connect socket error"); return (-1); } /* Setting X.25 out-of-band data handler */ pid = getpid(); if (ioctl(ttyfd,SIOCSPGRP,&pid)) { perror("Setting process group id"); return(-1); } (VOID) signal(SIGURG,x25oobh); /* Set reverse charge call and closed user group if requested */ bzero ((char *)&x25facil,sizeof(x25facil)); if (revcall) x25facil.reverse_charge = revcall; if (closgr > -1) { x25facil.cug_req = 1; x25facil.cug_index = closgr; } if (ioctl(ttyfd,X25_WR_FACILITY,&x25facil) < 0) { perror ("Setting X.25 facilities"); return (-1); } /* Need X.25 header with bits Q and M */ if (ioctl (ttyfd,X25_HEADER,&needh) < 0) { perror ("Setting X.25 header"); return (-1); } /* Connects to remote host via SunLink X.25 */ if (connect(ttyfd,&x25host,sizeof(x25host)) < 0) { debug(F101,"netopen connect errno","",errno); i = errno; if (errno) {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -