|
|
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: unsigned int tcp_localaddr = INADDR_ANY; ! 9: unsigned int tcp_acceptaddr; ! 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: return(fd); ! 21: if(errno != ENXIO) ! 22: break; ! 23: } ! 24: perror(name); ! 25: return(-1); ! 26: } ! 27: ! 28: tcp_connect(fd, lport, faddr, fport) ! 29: int fd; ! 30: tcp_port lport, fport; ! 31: in_addr faddr; ! 32: { ! 33: struct tcpreply tr; ! 34: ! 35: errno = 0; ! 36: if(tcp_cmd(fd, TCPC_CONNECT, lport, faddr, fport) < 0) ! 37: return(-1); ! 38: if(tcp_wait(fd, &tr) < 0) ! 39: return(-1); ! 40: if(tr.reply != TCPR_OK) ! 41: return(-1); ! 42: return(0); ! 43: } ! 44: ! 45: tcp_wait(fd, trp) ! 46: int fd; ! 47: struct tcpreply *trp; ! 48: { ! 49: int n; ! 50: /* timeout? */ ! 51: n = read(fd, trp, sizeof(struct tcpreply)); ! 52: if(n != sizeof(struct tcpreply)) ! 53: return(-1); ! 54: return(0); ! 55: } ! 56: ! 57: tcp_listen(fd, lport, faddr, fport) ! 58: int fd; ! 59: tcp_port lport, fport; ! 60: in_addr faddr; ! 61: { ! 62: struct tcpreply tr; ! 63: ! 64: if(tcp_cmd(fd, TCPC_LISTEN, lport, faddr, fport) < 0) ! 65: return(-1); ! 66: return(0); ! 67: } ! 68: ! 69: tcp_cmd(fd, cmd, lport, faddr, fport) ! 70: int fd; ! 71: int cmd; ! 72: tcp_port lport, fport; ! 73: in_addr faddr; ! 74: { ! 75: struct tcpuser tu; ! 76: ! 77: tu.cmd = cmd; ! 78: tu.src = tcp_localaddr; ! 79: tu.dst = faddr; ! 80: tu.sport = lport; ! 81: tu.dport = fport; ! 82: if(write(fd, &tu, sizeof(tu)) != sizeof(tu)){ ! 83: perror("cmd write"); ! 84: return(-1); ! 85: } ! 86: return(0); ! 87: } ! 88: ! 89: tcp_accept(fd, faddrp, fportp, devp) ! 90: int fd; ! 91: in_addr *faddrp; ! 92: tcp_port *fportp; ! 93: int *devp; ! 94: { ! 95: char name[32]; ! 96: int nfd; ! 97: struct tcpreply tr; ! 98: ! 99: if(tcp_wait(fd, &tr) < 0) ! 100: return(-1); ! 101: if(tr.reply != TCPR_OK) ! 102: return(-1); ! 103: *devp = tr.tcpdev; ! 104: sprintf(name, "/dev/tcp%02d", tr.tcpdev); ! 105: nfd = open(name, 2); ! 106: if(nfd < 0) ! 107: return(-1); ! 108: if (faddrp != 0) ! 109: *faddrp = tr.dst; ! 110: if (fportp != 0) ! 111: *fportp = tr.dport; ! 112: tcp_acceptaddr = tr.src; ! 113: return(nfd); ! 114: } ! 115: ! 116: ! 117: tcp_rcmd(host, port, locuser, remuser, cmd, fd2p) ! 118: char *host, *port, *locuser, *remuser, *cmd; ! 119: int *fd2p; ! 120: { ! 121: char buf[1]; ! 122: int fd; ! 123: unsigned int faddr, fport; ! 124: struct in_service *sp; ! 125: ! 126: faddr = in_address(host); ! 127: if(faddr == 0){ ! 128: fprintf(stderr, "%s: unknown host\n", host); ! 129: return(-1); ! 130: } ! 131: sp = in_service(port, "tcp", 0); ! 132: if(sp == 0){ ! 133: fprintf(stderr, "%s: unknown service\n", port); ! 134: return(-1); ! 135: } ! 136: fport = sp->port; ! 137: fd = tcp_sock(); ! 138: if(fd < 0){ ! 139: perror(host); ! 140: return(-1); ! 141: } ! 142: if(tcp_connect(fd, 0, faddr, fport) < 0){ ! 143: fprintf(stderr, "%s: connection failed\n", host); ! 144: goto bad; ! 145: } ! 146: ! 147: if(fd2p == 0) ! 148: write(fd, "", 1); ! 149: else ! 150: goto bad; /* later */ ! 151: ! 152: write(fd, locuser, strlen(locuser)+1); ! 153: write(fd, remuser, strlen(remuser)+1); ! 154: write(fd, cmd, strlen(cmd)+1); ! 155: ! 156: if(read(fd, buf, 1) != 1){ ! 157: perror(host); ! 158: goto bad; ! 159: } ! 160: if(buf[0] != 0){ ! 161: while(read(fd, buf, 1) == 1){ ! 162: write(2, buf, 1); ! 163: if(buf[0] == '\n') ! 164: break; ! 165: } ! 166: goto bad; ! 167: } ! 168: return(fd); ! 169: bad: ! 170: close(fd); ! 171: return(-1); ! 172: } ! 173:
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.