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