|
|
1.1 ! root 1: #include <sys/inet/tcp_user.h> ! 2: #include <sys/inet/socket.h> ! 3: #include <ctype.h> ! 4: #include <stdio.h> ! 5: #include <errno.h> ! 6: #include "config.h" ! 7: ! 8: extern errno; ! 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: return(fd); ! 20: if(errno != ENXIO) ! 21: break; ! 22: } ! 23: perror(name); ! 24: return(-1); ! 25: } ! 26: ! 27: tcp_connect(fd, tp) ! 28: int fd; ! 29: struct tcpuser *tp; ! 30: { ! 31: tp->code = TCPC_CONNECT; ! 32: if (write(fd, (char *)tp, sizeof(struct tcpuser))!=sizeof(struct tcpuser)) ! 33: return -1; ! 34: if (read(fd, (char *)tp, sizeof(struct tcpuser))!=sizeof(struct tcpuser)) ! 35: return -2; ! 36: if(tp->code != TCPC_OK) ! 37: return -3; ! 38: return 0; ! 39: } ! 40: ! 41: tcp_listen(fd, tp) ! 42: int fd; ! 43: struct tcpuser *tp; ! 44: { ! 45: tp->code = TCPC_LISTEN; ! 46: if (write(fd, (char *)tp, sizeof(struct tcpuser))!=sizeof(struct tcpuser)) ! 47: return -1; ! 48: if (read(fd, (char *)tp, sizeof(struct tcpuser))!=sizeof(struct tcpuser)) ! 49: return -1; ! 50: if(tp->code != TCPC_OK) ! 51: return -1; ! 52: return 0; ! 53: } ! 54: ! 55: tcp_accept(fd, tp) ! 56: int fd; ! 57: struct tcpuser *tp; ! 58: { ! 59: char name[128]; ! 60: ! 61: if (read(fd, (char *)tp, sizeof(struct tcpuser))!=sizeof(struct tcpuser)) ! 62: return -1; ! 63: if (tp->code != TCPC_OK) ! 64: return -1; ! 65: sprintf(name, "/dev/tcp%02d", tp->param); ! 66: return open(name, 2); ! 67: } ! 68: ! 69: tcp_rcmd(host, port, locuser, remuser, cmd, fd2p) ! 70: char *host, *port, *locuser, *remuser, *cmd; ! 71: int *fd2p; ! 72: { ! 73: char buf[1]; ! 74: int fd; ! 75: struct in_service *sp; ! 76: struct tcpuser tu; ! 77: in_addr in_address(); ! 78: ! 79: tu.faddr = in_address(host); ! 80: if(tu.faddr == 0){ ! 81: fprintf(stderr, "%s: unknown host\n", host); ! 82: return(-1); ! 83: } ! 84: sp = in_service(port, "tcp", 0L); ! 85: if(sp == 0){ ! 86: fprintf(stderr, "%s: unknown service\n", port); ! 87: return(-1); ! 88: } ! 89: tu.fport = sp->port; ! 90: fd = tcp_sock(); ! 91: if(fd < 0){ ! 92: perror(host); ! 93: return(-1); ! 94: } ! 95: tu.laddr = INADDR_ANY; ! 96: tu.lport = 0; ! 97: tu.param = 0; ! 98: if(tcp_connect(fd, &tu) < 0){ ! 99: fprintf(stderr, "%s: connection failed\n", host); ! 100: goto bad; ! 101: } ! 102: ! 103: if(fd2p == 0) ! 104: write(fd, "", 1); ! 105: else ! 106: goto bad; /* later */ ! 107: ! 108: write(fd, locuser, strlen(locuser)+1); ! 109: write(fd, remuser, strlen(remuser)+1); ! 110: write(fd, cmd, strlen(cmd)+1); ! 111: ! 112: if(read(fd, buf, 1) != 1){ ! 113: perror(host); ! 114: goto bad; ! 115: } ! 116: if(buf[0] != 0){ ! 117: while(read(fd, buf, 1) == 1){ ! 118: write(2, buf, 1); ! 119: if(buf[0] == '\n') ! 120: break; ! 121: } ! 122: goto bad; ! 123: } ! 124: return(fd); ! 125: bad: ! 126: close(fd); ! 127: return(-1); ! 128: }
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.