Annotation of researchv10no/ipc/mgrs/gate/tcp_lib.c, revision 1.1

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

unix.superglobalmegacorp.com

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