Annotation of researchv10no/cmd/nupas/ipc/utcpconnect.c, revision 1.1.1.1

1.1       root        1: #include <stdio.h>
                      2: #include <sys/types.h>
                      3: #include <sys/ioctl.h>
                      4: #include <time.h>
                      5: #include <sys/net/in.h>
                      6: #include <sys/net/inet.h>
                      7: #include <sys/net/netdb.h>
                      8: #include <sysexits.h>
                      9: #include <string.h>
                     10: 
                     11: extern int ipcdebug;
                     12: 
                     13: #define htons(x)       (x)
                     14: 
                     15: int
                     16: tcp_connect(dest, param)
                     17: char *dest, *param;
                     18: {
                     19:        struct hostent *hp;
                     20:        char    *inet_ntoa();
                     21:        int s, port, i;
                     22:        long l=0;
                     23:        unsigned long addr;
                     24:        char buf[100];
                     25:        char *host, *service;
                     26: 
                     27:        if (ipcdebug)
                     28:                fprintf(stderr, "connecting via tcp `%s'\n", dest);
                     29: 
                     30:        strcpy(buf, dest);
                     31:        host = buf;
                     32: 
                     33:        if ((port = tcp_service(host)) < 0) {
                     34:                ipcabort(EX_UNAVAILABLE, "unknown service");
                     35:                return(-1);
                     36:        }
                     37: 
                     38:        if (ipcdebug)
                     39:                fprintf(stderr, "TCP port number is %d\n", port);
                     40: 
                     41:        if ((addr = inet_addr(host)) != (unsigned long) -1) {
                     42:                if (ipcdebug)
                     43:                        fprintf(stderr, "Numeric address: %s %lx\n",
                     44:                                host, addr);
                     45: 
                     46:                if((s = s5sockbcon(inet_ntoa(l),port,"/dev/tcp"))>=0)
                     47:                        return(s);
                     48:                ipcabort(EX_OSERR, "can't open connection");
                     49:                t_close(s);
                     50:                return -1;
                     51:        }
                     52: 
                     53:        if((hp = gethostbyname(host)) == (struct hostent *)0){
                     54:                ipcabort(EX_NOHOST, "unknown host");
                     55:                return(-1);
                     56:        }
                     57: 
                     58:        memcpy(&l,hp->h_addr,hp->h_length);
                     59:        if (ipcdebug)
                     60:                fprintf(stderr, "Trying address %s\n",inet_ntoa(l));
                     61: 
                     62:        s = s5sockbcon(inet_ntoa(l),port,"/dev/tcp");
                     63:        if(s < 0){
                     64:                ipcabort(EX_OSERR, "can't open connection");
                     65:        } else return(s);
                     66: 
                     67:        t_close(s);
                     68: 
                     69:        ipcabort(EX_NOHOST, "can't connect to host");
                     70:        return -1;
                     71: }
                     72: 
                     73: int
                     74: tcp_service(path)
                     75: char *path;
                     76: {
                     77:        struct servent *sp;
                     78:        char *service;
                     79: 
                     80:        if ((service = strchr(path, '!')) == NULL)
                     81:                service = "";
                     82:        else
                     83:                *service++ = '\0';
                     84: 
                     85:        /*
                     86:         *  translate service/port name
                     87:         */
                     88:        if(strncmp(service, "tcp.", 4)==0)
                     89:                service += 4;
                     90:        if(strncmp(service, "/cs/", 4)==0)
                     91:                service += 4;
                     92:        if(strcmp(service, "")==0)
                     93:                return 0;
                     94:        else if(atoi(service)!=0)
                     95:                return htons(atoi(service));
                     96:        else if ((sp = getservbyname (service, "tcp")) != NULL)
                     97:                return sp->s_port;
                     98:        return -1;
                     99: }
                    100: 
                    101: /*
                    102:  * The following is a fixed version of the inet_addr() routine, since
                    103:  * the one delivered with UTS 5.2.6b has a bug that truncates the
                    104:  * passed string.  Sigh !!
                    105:  */
                    106: 
                    107: int
                    108: inet_addr(str)
                    109: {
                    110:        int val;
                    111:        char *b;
                    112:        char *ptr;
                    113:        int byte[4];
                    114:        int nb, bb;
                    115:        char buf[16];
                    116: 
                    117:        strcpy(buf,str);
                    118:        nb = 0;
                    119:        for (b = strtok(buf,"."); b; b = strtok(NULL,".")) {
                    120:                if (nb == 4) return -1;
                    121:                byte[nb++] = strtol(b,&ptr,0);
                    122:                if (ptr == b || *ptr) return -1;
                    123:        }
                    124:        if (nb == 0) return -1;
                    125:        if (nb == 1) return byte[0];
                    126:        val = 0;
                    127:        for (bb = 0; bb < nb-1; bb++) val = (val << 8) | byte[bb];
                    128:        return (val << 8*(4-bb)) | byte[bb];
                    129: }

unix.superglobalmegacorp.com

This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.