Annotation of researchv10no/ipc/libin/tcp_lib.c, revision 1.1.1.1

1.1       root        1: #include <sys/inet/tcp_user.h>
                      2: #include <ctype.h>
                      3: #include <stdio.h>
                      4: #include <errno.h>
                      5: #include "config.h"
                      6: 
                      7: extern errno;
                      8: extern char *errstr;
                      9: 
                     10: tcp_sock()
                     11: {
                     12:        int fd, n;
                     13:        char name[32];
                     14: 
                     15:        for(n = 01; n < 100; n += 2){
                     16:                sprintf(name, "/dev/tcp%02d", n);
                     17:                fd = open(name, 2);
                     18:                if(fd >= 0){
                     19:                        flabclr(fd);            /* for security unix */
                     20:                        return(fd);
                     21:                }
                     22:                if(errno != ENXIO)
                     23:                        break;
                     24:        }
                     25:        perror(name);
                     26:        return(-1);
                     27: }
                     28: 
                     29: static
                     30: tcp_rv(fd, tp)
                     31:        int fd;
                     32:        struct tcpuser *tp;
                     33: {
                     34:        int n;
                     35: 
                     36:        n = read(fd, (char *)tp, sizeof(struct tcpuser));
                     37:        if (n != sizeof(struct tcpuser)){
                     38:                if(errno==EINTR){
                     39:                        errstr = "timeout";
                     40:                } else {
                     41:                        errstr = "connection rejected";
                     42:                        errno = EACCES;
                     43:                }
                     44:                return -1;
                     45:        }
                     46: 
                     47:        switch(tp->code){
                     48:        case TCPC_OK:
                     49:                return 0;
                     50:        case TCPC_BADDEV:
                     51:                errstr = "tcp dev broken";
                     52:                errno = EIO;
                     53:                break;
                     54:        case TCPC_NOROUTE:
                     55:                errstr = "no route";
                     56:                errno = ENOENT;
                     57:                break;
                     58:        case TCPC_BADLOCAL:
                     59:                errstr = "invalid local address";
                     60:                errno = EINVAL;
                     61:                break;
                     62:        case TCPC_BOUND:
                     63:                errstr = "address(es) already in use";
                     64:                errno = ENOENT;
                     65:                break;
                     66:        default:
                     67:                errstr = "unknown error";
                     68:                errno = EIO;
                     69:                break;
                     70:        }
                     71:        return -1;
                     72: }
                     73: 
                     74: tcp_connect(fd, tp)
                     75:        int fd;
                     76:        struct tcpuser *tp;
                     77: {
                     78:        tp->code = TCPC_CONNECT;
                     79:        if (write(fd, (char *)tp, sizeof(struct tcpuser))!=sizeof(struct tcpuser)){
                     80:                errstr = "can't write tcp dev";
                     81:                return -1;
                     82:        }
                     83:        return tcp_rv(fd, tp);
                     84: }
                     85: 
                     86: tcp_listen(fd, tp)
                     87:        int fd;
                     88:        struct tcpuser *tp;
                     89: {
                     90:        tp->code = TCPC_LISTEN;
                     91:        if (write(fd, (char *)tp, sizeof(struct tcpuser))!=sizeof(struct tcpuser)){
                     92:                errstr = "can't write tcp dev";
                     93:                return -1;
                     94:        }
                     95:        return tcp_rv(fd, tp);
                     96: }
                     97: 
                     98: tcp_accept(fd, tp)
                     99:        int fd;
                    100:        struct tcpuser *tp;
                    101: {
                    102:        char name[128];
                    103:        int nfd;
                    104: 
                    105:        if (read(fd, (char *)tp, sizeof(struct tcpuser))!=sizeof(struct tcpuser))
                    106:                return -1;
                    107:        if (tp->code != TCPC_OK)
                    108:                return -1;
                    109:        sprintf(name, "/dev/tcp%02d", tp->param);
                    110:        nfd = open(name, 2);
                    111:        if(nfd>=0)
                    112:                flabclr(nfd);           /* for security unix */
                    113:        return nfd;
                    114: }
                    115: 
                    116: tcp_rcmd(host, port, locuser, remuser, cmd, fd2p)
                    117: char *host, *port, *locuser, *remuser, *cmd;
                    118: int *fd2p;
                    119: {
                    120:        char buf[1];
                    121:        int fd;
                    122:        struct in_service *sp;
                    123:        struct tcpuser tu;
                    124:        in_addr in_address();
                    125: 
                    126:        tu.faddr = in_address(host);
                    127:        if(tu.faddr == 0){
                    128:                fprintf(stderr, "%s: unknown host\n", host);
                    129:                return(-1);
                    130:        }
                    131:        sp = in_service(port, "tcp", 0L);
                    132:        if(sp == 0){
                    133:                fprintf(stderr, "%s: unknown service\n", port);
                    134:                return(-1);
                    135:        }
                    136:        tu.fport = sp->port;
                    137:        fd = tcp_sock();
                    138:        if(fd < 0){
                    139:                perror(host);
                    140:                return(-1);
                    141:        }
                    142:        tu.laddr = INADDR_ANY;
                    143:        tu.lport = 0;
                    144:        tu.param = 0;
                    145:        if(tcp_connect(fd, &tu) < 0){
                    146:                fprintf(stderr, "%s: connection failed\n", host);
                    147:                goto bad;
                    148:        }
                    149: 
                    150:        if(fd2p == 0)
                    151:                write(fd, "", 1);
                    152:        else
                    153:                goto bad;       /* later */
                    154: 
                    155:        write(fd, locuser, strlen(locuser)+1);
                    156:        write(fd, remuser, strlen(remuser)+1);
                    157:        write(fd, cmd, strlen(cmd)+1);
                    158: 
                    159:        if(read(fd, buf, 1) != 1){
                    160:                perror(host);
                    161:                goto bad;
                    162:        }
                    163:        if(buf[0] != 0){
                    164:                while(read(fd, buf, 1) == 1){
                    165:                        write(2, buf, 1);
                    166:                        if(buf[0] == '\n')
                    167:                                break;
                    168:                }
                    169:                goto bad;
                    170:        }
                    171:        return(fd);
                    172: bad:
                    173:        close(fd);
                    174:        return(-1);
                    175: }

unix.superglobalmegacorp.com

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