⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ckcnet.c

📁 linux终端仿真程序
💻 C
📖 第 1 页 / 共 5 页
字号:
#endif /* VMS */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.*/_PROTOTYP( int ttbufr, ( VOID ) );intttbufr() {				/* TT Buffer Read */    int count;    if (ttnet != NET_TCPB)		/* First make sure current net is */      return(-1);			/* TCP/IP; if not, do nothing. */    if (ttibn > 0)			/* Our internal buffer is not empty, */      return(ttibn);			/* so keep using it. */#ifdef WINTCP    count = 512;			/* This works for WIN/TCP */#else#ifdef DEC_TCPIP    count = 512;			/* UCX */#else#ifdef OS2    count = TTIBUFL;#else					/* Multinet, etc. */    count = ttchk();			/* Check network input buffer, */    if (ttibn > 0) return(ttibn);	/* which can put a char there! */    if (count < 0)			/* Read error - connection closed */      return(-2);    else if (count > TTIBUFL)		/* Too many to read */      count = TTIBUFL;    else if (count == 0)		/* None, so force blocking read */      count = 1;#endif /* OS2 */#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 {	    debug(F101,"ttbufr socket_read error","",socket_errno);	    return(-1);	}    } else if (count == 0) {	debug(F100,"ttbufr socket eof","",0);			return(-1);    }#else /* COMMENT *//* This is for blocking reads */#ifndef VMS#ifdef SO_OOBINLINE    {	int outofband = 0;#ifdef BELLSELECT	if (select(128, NULL, NULL, efds, 0) > 0 && FD_ISSET(ttyfd, efds))	  outofband = 1;#else#ifdef BSDSELECT	fd_set efds;	struct timeval tv;	FD_ZERO(&efds);	FD_SET(ttyfd, &efds);	tv.tv_sec  = tv.tv_usec = 0L;	debug(F100,"Out-of-Band BSDSELECT","",0);#ifdef NT	WSASafeToCancel = 1;#endif /* NT */	if (select(FD_SETSIZE, NULL, NULL, &efds, &tv) > 0 &&	    FD_ISSET(ttyfd, &efds))	  outofband = 1;#ifdef NT	WSASafeToCancel = 0;#endif /* NT */#else /* !BSDSELECT */#ifdef IBMSELECT/* Was used by OS/2, currently not used, but might come in handy some day... *//* ... and it came in handy!  For our TCP/IP layer, it avoids all the fd_set *//* and timeval stuff since this is the only place where it is used. */	int socket = ttyfd;	debug(F100,"Out-of-Band IBMSELECT","",0);	if ((select(&socket, 0, 0, 1, 0L) == 1) && (socket == ttyfd))	  outofband = 1;#else /* !IBMSELECT *//*  If we can't use select(), then we use the regular alarm()/signal()  timeout mechanism.*/      debug(F101,"Out-of-Band data not supported","",0);      outofband = 0;#endif /* IBMSELECT */#endif /* BSDSELECT */#endif /* BELLSELECT */      if (outofband) {         /* Get the Urgent Data */         /* if OOBINLINE is disabled this should be only a single byte      */         /* MS Winsock has a bug in Windows 95.  Extra bytes are delivered  */         /* That were never sent.                                           */	  if ((count = socket_recv(ttyfd,ttibuf,count,MSG_OOB)) <= 0) {	      int s_errno = socket_errno;	      debug(F101, "ttbufr socket_recv MSG_OOB","",count);	      debug(F101, "ttbufr socket_errno","",s_errno);#ifndef OS2	      netclos();			/* *** *** */  	      return(-2);#else /* OS2 */	      if (count == 0) {		  debug(F100,"ttbufr Closing Connection","",0);		  ttclos(0);		/* if the connection was  */		  if (ttname[0] == '*') { /* incoming, wait for another */		      int local;		      os2_netopen(ttname,&local,ttnet);		      debug(F101,			    "ttbufr returns zero - try again immediately",			    "",			    0			    );		      return 0;		/* try again immediately */		  } else {		      debug(F101, "ttbufr returns hard error","",-3);		      return -3;	/* return a hard error    */		  }	      } else {#ifdef NT		  if (s_errno == WSAETIMEDOUT)		    debug(F100,"WSAETIMEDOUT","",0);#endif /* NT */ 		  switch (s_errno) {#ifdef NT		    case WSAETIMEDOUT:#else		    case SOCETIMEDOUT:    		    case SOCETIMEDOUT - SOCBASEERR:#endif /* NT */		      debug(F100,"ttbufr ETIMEDOUT","",0);		      return(-1);#ifdef NT		    case WSAECONNRESET:#else /* NT */		    case SOCECONNRESET:		    case SOCECONNRESET - SOCBASEERR:#endif /* NT */		      debug(F100,"ttbufr ECONRESET","",0);		      netclos();	/* *** *** */		      return(-2);	/* Connection is broken. */#ifdef NT		    case WSAECONNABORTED:#else /* NT */		    case SOCECONNABORTED:		    case SOCECONNABORTED - SOCBASEERR:#endif /* NT */		      debug(F100,"ttbufr ECONNABORTED","",0);		      netclos();	/* *** *** */		      return(-2);	/* Connection is broken. */#ifdef NT		    case WSAENETRESET:  #else /* NT */		    case SOCENETRESET:		    case SOCENETRESET - SOCBASEERR:#endif /* NT */		      debug(F100,"ttbufr ENETRESET","",0);		      netclos();	/* *** *** */		      return(-2);	/* Connection is broken. */#ifdef NT		    case WSAENOTCONN:#else /* NT */		    case SOCENOTCONN:		    case SOCENOTCONN - SOCBASEERR:#endif /* NT */		      debug(F100,"ttbufr ENOTCONN","",0);		      netclos();	/* *** *** */		      return(-2);	/* Connection is broken. */#ifdef NT		    case WSAEWOULDBLOCK:#else		    case SOCEWOULDBLOCK:		    case SOCEWOULDBLOCK - SOCBASEERR:#endif /* NT */		      debug(F100,"ttbufr EWOULDBLOCK","",0);		      count = 1;		      break;#ifdef NT		    case WSAEINVAL:#else /* NT */		    case SOCEINVAL:		    case SOCEINVAL - SOCBASEERR:#endif /* NT */                      case 0:                      case 23: /* ??? */		      /* These appear in OS/2 - don't know why   */		      /* ignore it and read as normal data       */		      /* and break, then we will attempt to read */		      /* the port using normal read() techniques */		      debug(F100,"ttbufr handing as in-band data","",0);		      count = 1;		      break;		    default:		      debug(F101, "ttbufr Unknown Error ","",s_errno);		      netclos();		      return -2;	/* Return a hard error */		  }	      }   #endif /* OS2 */	  } else {			/* we got out-of-band data */	      debug(F111,"ttbufr out-of-band chars","",count);#ifdef RLOGCODE				/* blah */	      if (ttnproto == NP_RLOGIN) {		  /*		    When urgent data is read with MSG_OOB and not OOBINLINE		    then urgent data and normal data are not mixed.  So		    treat the entire buffer as urgent data.		  */		  rlog_oob(ttibuf, count);		  return ttbufr();	      } else #endif /* RLOGCODE */ /* blah */#ifdef COMMENT            /*	       I haven't written this yet, nor do I know what it should do	     */		if (ttnproto == NP_TELNET) {		    tn_oob();		    return 0;		} else #endif /* COMMENT */		  {		   /* For any protocols we don't have a special out-of-band  */                   /* handler for, just put the bytes in the normal buffer   */                   /* and return                                             */		      ttibp = 0;	/* Reset buffer pointer. */		      ttibn = count;#ifdef DEBUG		      /* Got some bytes. */		      debug(F101,"ttbufr count 2","",count);		      if (count > 0) 			ttibuf[count] = '\0';		      debug(F111,"ttbufr ttibuf",ttibuf,ttibp);#endif /* DEBUG */		      return(ttibn);	/* Return buffer count. */		  }	  }      }    }#endif /* SO_OOBINLINE */#endif /* VMS */    if ((count = socket_read(ttyfd,ttibuf,count)) <= 0) {	int s_errno = socket_errno;	debug(F101,"ttbufr socket_read","",count);	debug(F101,"ttbufr socket_errno","",s_errno);#ifndef OS2	netclos();			/* *** *** */  	return(-2);#else /* OS2 */	if (count == 0) {	    debug(F100,"ttbufr Closing Connection","",0);	    ttclos(0);			/* if the connection was  */	    if (ttname[0] == '*') {	/* incoming, wait for another */		int local;		os2_netopen(ttname,&local,ttnet);		debug(F101,		      "ttbufr returns zero - try again immediately","",0);		return 0;		/* try again immediately */	    } else {		debug(F101, "ttbufr returns hard error","",-3);		return -3;		/* return a hard error    */	    }	} else {#ifdef NT	    if (s_errno == WSAETIMEDOUT)	      debug(F100, "WSAETIMEDOUT","",0);#endif /* NT */	    switch (s_errno) {#ifdef NT	      case WSAETIMEDOUT:#else	      case SOCETIMEDOUT:	      case SOCETIMEDOUT - SOCBASEERR:#endif /* NT */		debug(F100,"ttbufr ETIMEDOUT","",0);		return(-1);#ifdef NT	      case WSAECONNRESET:#else /* NT */	      case SOCECONNRESET:	      case SOCECONNRESET - SOCBASEERR:#endif /* NT */		debug(F100,"ttbufr ECONRESET","",0);		netclos();			/* *** *** */		return(-2);			/* Connection is broken. */#ifdef NT	      case WSAECONNABORTED:#else /* NT */	      case SOCECONNABORTED:	      case SOCECONNABORTED - SOCBASEERR:#endif /* NT */		debug(F100,"ttbufr ECONNABORTED","",0);		netclos();			/* *** *** */		return(-2);			/* Connection is broken. */#ifdef NT	      case WSAENETRESET:  #else /* NT */	      case SOCENETRESET:	      case SOCENETRESET - SOCBASEERR:#endif /* NT */		debug(F100,"ttbufr ENETRESET","",0);		netclos();			/* *** *** */		return(-2);			/* Connection is broken. */#ifdef NT	      case WSAENOTCONN:#else /* NT */	      case SOCENOTCONN:	      case SOCENOTCONN - SOCBASEERR:#endif /* NT */		debug(F100,"ttbufr ENOTCONN","",0);		netclos();			/* *** *** */		return(-2);			/* Connection is broken. */#ifdef NT	      case WSAEWOULDBLOCK:#else	      case SOCEWOULDBLOCK:	      case SOCEWOULDBLOCK - SOCBASEERR:#endif /* NT */		debug(F100,"ttbufr EWOULDBLOCK","",0);		break;	    }	    debug(F101, "ttbufr returns timeout","",-1);	    return -1;			/* Return a timeout */	}#endif /* OS2 */    }#endif /* COMMENT */    else {	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 /* TCPIPLIB */#ifndef IBMSELECT#ifndef BELLSELECT#ifndef BSDSELECT		/* Non-TCPIPLIB case */#ifdef SELECT#define BSDSELECT#endif /* SELECT */#endif /* BSDSELECT */#endif /* BELLSELECT */#endif /* IBMSELECT */#define	TELNET_PORT 23	 	/* Should do lookup, but it won't change */#define RLOGIN_PORT 513#define KERMIT_PORT 1649/* This symbol is not known to, e.g., Ultrix 2.0 */#ifndef TELOPT_TTYPE#define TELOPT_TTYPE 24#endif /* TELOPT_TTYPE *//* This one seems to be not known to UCX */#ifndef TELOPT_BINARY#define TELOPT_BINARY 0#endif /* TELOPT_BINARY *//* Type needed as 5th argument (length) to get/setsockopt() */#ifndef SOCKOPT_T#define SOCKOPT_T int#ifdef UNIXWARE#undef SOCKOPT_T#define SOCKOPT_T size_t#else#ifdef VMS#ifdef DEC_TCPIP#ifdef __DECC_VER#undef SOCKOPT_T#define SOCKOPT_T size_t#endif /* __DECC_VER */#endif /* DEC_TCPIP */#endif /* VMS */#endif /* UNIXWARE */#endif /* SOCKOPT_T *//*  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 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 TCPSOCKET#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);

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -