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

📄 ckcnet.c

📁 linux终端仿真程序
💻 C
📖 第 1 页 / 共 5 页
字号:
    if ((host = gethostbyaddr((char *)&saddr.sin_addr,4,PF_INET)) != NULL) {	debug(F100,"tcpsocket_open gethostbyname != NULL","",0);	strncpy(name, host->h_name, 79);	strncat(name, ":", 80 - strlen(name));#ifdef COMMENT	itoa(ntohs(saddr.sin_port), name + strlen(name), 10);#else	sprintf(name + strlen(name),"%d",ntohs(saddr.sin_port));#endif /* COMMENT */	sprintf(ipaddr,"%s", (char *)inet_ntoa(saddr.sin_addr));	printf("%s connected on port %d\n",host->h_name,ntohs(saddr.sin_port));    }    ttnet = nett;			/* TCP/IP (sockets) network */#ifdef RLOGCODE    if ( ntohs(saddr.sin_port) == 23 )	ttnproto = NP_LOGIN ;    else #endif /* RLOGCODE */    /* Assume the service is TELNET. */    {	ttnproto = NP_TELNET;		/* Yes, set global flag. */	tn_ini();			/* Start TELNET negotiations. */    }    if (*lcl < 0) *lcl = 1;		/* Local mode. */    return(0);				/* Done. */}#endif /* NOTUSED *//*  T C P S R V _ O P E N  --  Open a TCP/IP Server connection  *//*  Calling conventions same as ttopen(), except third argument is network  type rather than modem type.*/inttcpsrv_open(name,lcl,nett,timo) char * name; int * lcl; int nett; int timo; {    char *p;    int i, x;    SOCKOPT_T on = 1;    int ready_to_accept = 0;    static struct servent *service, servrec;    static struct hostent *host;    static struct sockaddr_in saddr;    static SOCKOPT_T saddrlen;#ifdef BSDSELECT    fd_set rfds;    struct timeval tv;#else#ifdef BELLSELCT    fd_set rfds;#endif /* BELLSELECT */#endif /* BSDSELECT */    debug(F101,"tcpsrv_open nett","",nett);    *ipaddr = '\0';    if (nett != NET_TCPB)      return(-1);			/* BSD socket support */    netclos();				/* Close any previous connection. */    strncpy(namecopy, name, NAMECPYL);	/* Copy the hostname. */    ttnproto = NP_NONE;			/* No protocol selected yet. */    debug(F110,"tcpsrv_open namecopy",namecopy,0);#ifdef COMMENT    if (tcpsrfd != -1) {	socket_close(tcpsrfd);	tcpsrfd = -1;	tcpsrv_port = 0;    }#endif /* COMMENT */    p = namecopy;			/* Was a service requested? */    while (*p != '\0' && *p != ':')      p++; /* Look for colon */    if (*p == ':') {			/* Have a colon */	*p++ = '\0';			/* Get service name or number */    } else {				/* Otherwise use kermit */	p = "kermit";    }    debug(F110,"tcpsrv_open service requested",p,0);    if (isdigit(*p)) {			/* Use socket number without lookup */	service = &servrec;	service->s_port = htons((unsigned short)atoi(p));    } else {				/* Otherwise lookup the service name */	service = getservbyname(p, "tcp");    }    if (!service && !strcmp("kermit",p)) { /* Use Kermit service port */	service = &servrec;	service->s_port = htons(1649);    }#ifdef RLOGCODE    if (service && !strcmp("login",p) && service->s_port != htons(513)) {	fprintf(stderr,		"  Warning: login service on port %d instead of port 513\n", 		 ntohs(service->s_port));	fprintf(stderr, "  Edit SERVICES file if RLOGIN fails to connect.\n");	debug(F101,"tcpsrv_open login on port","",ntohs(service->s_port));    }#endif /* RLOGCODE */    if (!service) {	fprintf(stderr, "Cannot find port for service %s\n", p);	debug(F101,"tcpsrv_open can't get service","",errno);	errno = 0;			/* rather than mislead */	return(-1);    }    /* If we currently have a listen active but port has changed then close */    debug(F101,"tcpsrv_open checking previous connection","",tcpsrfd);    debug(F101,"tcpsrv_open previous tcpsrv_port","",tcpsrv_port);    if (tcpsrfd != -1 &&	tcpsrv_port != ntohs((unsigned short)service->s_port)) {	debug(F100,"tcpsrv_open closing previous connection","",0);#ifdef TCPIPLIB	socket_close(tcpsrfd);#else	close(tcpsrfd);#endif /* TCPIPLIB */	tcpsrfd = -1;    }    debug(F100,"tcpsrv_open tcpsrfd","",tcpsrfd);    if (tcpsrfd == -1) {	/* Set up socket structure and get host address */	bzero((char *)&saddr, sizeof(saddr));	debug(F100,"tcpsrv_open bzero ok","",0);	saddr.sin_family = AF_INET;	saddr.sin_addr.s_addr = INADDR_ANY;	/* Get a file descriptor for the connection. */	saddr.sin_port = service->s_port;	ipaddr[0] = '\0';	debug(F100,"tcpsrv_open calling socket","",0);	if ((tcpsrfd = socket(AF_INET, SOCK_STREAM, 0)) < 0) {	    perror("TCP socket error");	    debug(F101,"tcpsrv_open socket error","",errno);	    return (-1);	}	errno = 0;	/* Specify the Port may be reused */	debug(F100,"tcpsrv_open calling setsockopt","",0);	x = setsockopt(tcpsrfd,		       SOL_SOCKET,SO_REUSEADDR,(char *)&on,sizeof on);	debug(F101,"tcpsrv_open setsockopt","",x);#ifndef NOTCPOPTS#ifndef datageneral#ifdef SOL_SOCKET#ifdef TCP_NODELAY	no_delay(tcp_nodelay);	debug(F101,"tcpsrv_open no_delay","",tcp_nodelay);#endif /* TCP_NODELAY */#ifdef SO_KEEPALIVE	keepalive(tcp_keepalive);	debug(F101,"tcpsrv_open keepalive","",tcp_keepalive);#endif /* SO_KEEPALIVE */#ifdef SO_LINGER 	ck_linger(tcp_linger, tcp_linger_tmo);	debug(F101,"tcpsrv_open linger","",tcp_linger_tmo);#endif /* SO_LINGER */#ifdef SO_SNDBUF	sendbuf(tcp_sendbuf);	debug(F101,"tcpsrv_open sendbuf","",tcp_sendbuf);#endif /* SO_SNDBUF */#ifdef SO_RCVBUF	recvbuf(tcp_recvbuf);	debug(F101,"tcpsrv_open recvbuf","",tcp_recvbuf);#endif /* SO_RCVBUF */#endif /* SOL_SOCKET */#endif /* datageneral */#endif /* NOTCPOPTS */       /* Now bind to the socket */	printf("\nBinding socket to port %d ...\n",	       ntohs((unsigned short)service->s_port));	if (bind(tcpsrfd, (struct sockaddr *)&saddr, sizeof(saddr)) < 0) {	    i = errno;			/* Save error code */	    close(tcpsrfd);	    tcpsrfd = -1;	    tcpsrv_port = 0;	    ttyfd = -1;	    errno = i;			/* and report this error */	    debug(F101,"tcpsrv_open bind errno","",errno);	    return(-1);	}	debug(F100,"tcpsrv_open bind OK","",0);	printf("Listening ...\n");	if (listen(tcpsrfd, 15) < 0) {	    i = errno;			/* Save error code */	    close(tcpsrfd);	    tcpsrfd = -1;	    tcpsrv_port = 0;	    ttyfd = -1;	    errno = i;			/* And report this error */	    debug(F101,"tcpsrv_open listen errno","",errno);	    return(-1);	}	debug(F100,"tcpsrv_open listen OK","",0);	tcpsrv_port = ntohs((unsigned short)service->s_port);    }    printf("\nWaiting to Accept a TCP/IP connection on port %d ...\n",           ntohs((unsigned short)service->s_port));    saddrlen = sizeof(saddr);#ifdef BSDSELECT    tv.tv_sec  = tv.tv_usec = 0L;    if (timo < 0)      tv.tv_usec = (long) -timo * 10000L;    else      tv.tv_sec = timo;    debug(F101,"tcpsrv_open BSDSELECT","",timo);#else    debug(F101,"tcpsrv_open not BSDSELECT","",timo);#endif /* BSDSELECT */    if (!timo) {	while (!ready_to_accept) {#ifdef BSDSELECT	    FD_ZERO(&rfds);	    FD_SET(tcpsrfd, &rfds);	    ready_to_accept =	      ((select(FD_SETSIZE, #ifdef HPUX#ifdef HPUX1010		       (fd_set *)#else		       (int *)#endif /* HPUX1010 */#else#ifdef __DECC		       (fd_set *)#endif /* __DECC */#endif /* HPUX */		       &rfds, NULL, NULL, &tv) > 0) && 	       FD_ISSET(tcpsrfd, &rfds));#else /* BSDSELECT */#ifdef IBMSELECT#define ck_sleepint 250	    ready_to_accept =	      (select(&tcpsrfd, 1, 0, 0,		      timo < 0 ? -timo :		      (timo > 0 ? timo * 1000L : ck_sleepint)) == 1	       );#else#ifdef BELLSELECT	    FD_ZERO(rfds);	    FD_SET(tcpsrfd, rfds);	    ready_to_accept =	      ((select(128, rfds, NULL, NULL, timo < 0 ? -timo :		      (timo > 0 ? timo * 1000L)) > 0) && 	       FD_ISSET(tcpsrfd, rfds));#elseSOME_FORM_OF_SELECT_IS_NEEDED_HERE#endif /* BELLSELECT */#endif /* IBMSELECT */#endif /* BSDSELECT */	}    }    if (ready_to_accept) {	if ((ttyfd = accept(tcpsrfd,			    (struct sockaddr *)&saddr,&saddrlen)) < 0) {	    i = errno;			/* save error code */	    close(tcpsrfd);	    ttyfd = -1;	    tcpsrfd = -1;	    tcpsrv_port = 0;	    errno = i;			/* and report this error */	    debug(F101,"tcpsrv_open accept errno","",errno);	    return(-1);	}	setsockopt(ttyfd, SOL_SOCKET, SO_OOBINLINE,(char *) &on, sizeof on);	ttnet = nett;			/* TCP/IP (sockets) network */	/* See if the service is TELNET. */	if ((x = ntohs((unsigned short)service->s_port)) ==	    getservbyname("telnet", "tcp")->s_port) {	    ttnproto = NP_TELNET;	/* Yes, set global flag. */	    tn_ini();			/* Start TELNET negotiations. */	} else {	    tn_ini();			/* Initialize TELNET negotiations. */	}	debug(F101,"tcpsrv_open service","",x);	if (*lcl < 0)			/* Set local mode. */	  *lcl = 1;#ifdef COMMENT	close(tcpsrfd);	tcpsrfd = -1;	tcpsrv_port = 0;#endif /* COMMENT */	if (host = gethostbyaddr((char *)&saddr.sin_addr,4,PF_INET)) {	    debug(F100,"tcpsrv_open gethostbyname != NULL","",0);	    name[0] = '*';	    strncpy(&name[1],host->h_name,79);	    strncat(name,":",80-strlen(name));	    strncat(name,p,80-strlen(name));	    sprintf(ipaddr,"%s", (char *)inet_ntoa(saddr.sin_addr));	    printf("%s connected on port %s\n",host->h_name,p);	}	return(0);			/* Done. */    } else {	i = errno;			/* save error code */	close(tcpsrfd);	ttyfd = -1;	tcpsrfd = -1;	tcpsrv_port = 0;	errno = i;			/* and report this error */	debug(F101,"tcpsrv_open accept errno","",errno);	return(-1);    }}#endif /* NOLISTEN */#endif /* OS2 */#endif /* TCPSOCKET *//*  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;    int i, x;#ifdef TCPSOCKET    int isconnect = 0;#ifdef SO_OOBINLINE    int on = 1;#endif /* SO_OOBINLINE */    struct servent *service=NULL, servrec;    struct hostent *host=NULL;    struct sockaddr_in saddr;#ifdef EXCELAN    struct sockaddr_in send_socket;#endif /* EXCELAN */#endif /* TCPSOCKET */#ifdef SUNX25				/* Code for SunLink X.25 support */#define X29PID 1			/* X.29 Protocol ID */_PROTOTYP(SIGTYP x25oobh, (int) );    CONN_DB x25host;#ifndef X25_WR_FACILITY    FACILITY x25facil;#else    FACILITY_DB x25facil;#endif /* X25_WR_FACILITY */    static int needh = 1;    PID_T pid;    extern int linkid, lcn, x25ver;    extern int revcall, closgr, cudata;    extern char udata[];#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((char *)x25host.data+X29PIDLEN,udata,(int)strlen(udata));            x25host.datalen += (int)strlen(udata);        }        /* Open SunLink X.25 socket */	if (!quiet && *name) printf(" Trying %s...\n", name);        if ((ttyfd = socket(AF_X25, SOCK_STREAM, 0)) < 0) {	    debug(F101,"netopen socket error","",errno);            perror ("X.25 socket error");            return (-1);        }        /* Setting X.25 out-of-band data handler */        pid = getpid();        if (ioctl(ttyfd,SIOCSPGRP,&pid)) {            perror("X.25 set process group id error");            return(-1);        }        (VOID) signal(SIGURG,x25oobh);        /* Set reverse charge call and closed user group if requested */        bzero ((char *)&x25facil,sizeof(x25facil));#ifndef X25_WR_FACILITY/*  New SunLink (7.0 or 8.0, not sure which)... */	x25facil.type = T_REVERSE_CHARGE; /* Reverse Charge */	x25facil.f_reverse_charge = revcall ? 1 : 0;        if (ioctl(ttyfd,X25_SET_FACILITY,&x25facil) < 0) {            perror ("Setting X.25 reverse charge");            return (-1);        }	if (closgr > -1) {		/* Closed User Group (Outgoing) */	    bzero ((char *)&x25facil,sizeof(x25facil));	    x25facil.type = T_CUG;	    x25facil.f_cug_req = CUG_REQ_ACS;	    x25facil.f_cug_index = closgr;	    if (ioctl(ttyfd,X25_SET_FACILITY,&x25facil) < 0) {		perror ("Setting X.25 closed user group");		return (-1);	    }	}#else/*  Old SunLink 6.0 (or 7.0?)... */        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);        }#endif /* X25_WR_FACILITY */        /*  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,(struct sockaddr *)&x25host,sizeof(x25host)) < 0) {	    i = errno;	    debug(F101,"netopen connect errno","",i);	    if (i) {		perror("netopen x25 connect");		x25diag();	    }	    (VOID) netclos();	    ttyfd = -1;	    ttnproto = NP_NONE;	    errno = i;	    return (-1);	}        /* Get X.25 link identification used for the connection */        if (ioctl(ttyfd,X25_GET_LINK,&linkid) < 0) {            perror ("Getting X.25 link id");            return (-1);        }        /* Get X.25 logical channel number used for the connection */        if (ioctl(ttyfd,X25_RD_LCGN,&lcn) < 0) {

⌨️ 快捷键说明

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