[9] | 1 | /* rget 2.6 - remote pipe Author: Kees J. Bot
|
---|
| 2 | * 20 Mar 1989
|
---|
| 3 | *
|
---|
| 4 | * here$ ... | rput key there$ rget -h here key | ...
|
---|
| 5 | * here$ rput key command ... there$ rget -h here key command ...
|
---|
| 6 | *
|
---|
| 7 | * (Once my first try at network programming, completely reworked by now.)
|
---|
| 8 | */
|
---|
| 9 | #define nil ((void*)0)
|
---|
| 10 | #include <sys/types.h>
|
---|
| 11 | #include <sys/ioctl.h>
|
---|
| 12 | #include <stdio.h>
|
---|
| 13 | #include <fcntl.h>
|
---|
| 14 | #include <stdlib.h>
|
---|
| 15 | #include <unistd.h>
|
---|
| 16 | #include <string.h>
|
---|
| 17 | #include <errno.h>
|
---|
| 18 | #if __minix
|
---|
| 19 | #include <net/gen/in.h>
|
---|
| 20 | #include <net/gen/inet.h>
|
---|
| 21 | #include <net/gen/netdb.h>
|
---|
| 22 | #include <net/gen/socket.h>
|
---|
| 23 | #include <net/gen/tcp.h>
|
---|
| 24 | #include <net/gen/tcp_hdr.h>
|
---|
| 25 | #include <net/gen/tcp_io.h>
|
---|
| 26 | #include <net/hton.h>
|
---|
| 27 | #include <net/netlib.h>
|
---|
| 28 | #else
|
---|
| 29 | #include <sys/socket.h>
|
---|
| 30 | #include <netinet/in.h>
|
---|
| 31 | #include <netdb.h>
|
---|
| 32 | #endif
|
---|
| 33 |
|
---|
| 34 | static char *name;
|
---|
| 35 | static int iflag, oflag, hflag, lflag, cflag; /* -iolch? */
|
---|
| 36 | static char *host; /* Argument to -h. */
|
---|
| 37 | static struct hostent *hent; /* gethostbyname(host) */
|
---|
| 38 | static char *key; /* key (port) */
|
---|
| 39 | static char **cmdv; /* command [arg ...] */
|
---|
| 40 |
|
---|
| 41 | static void fatal(const char *label)
|
---|
| 42 | {
|
---|
| 43 | int err= errno;
|
---|
| 44 |
|
---|
| 45 | fprintf(stderr, "%s: %s: %s\n", name, label, strerror(err));
|
---|
| 46 | exit(1);
|
---|
| 47 | }
|
---|
| 48 |
|
---|
| 49 | static unsigned name2port(char *n)
|
---|
| 50 | {
|
---|
| 51 | char *end;
|
---|
| 52 | unsigned port;
|
---|
| 53 |
|
---|
| 54 | port= strtoul(n, &end, 0);
|
---|
| 55 | if (end == n || *end != 0) {
|
---|
| 56 | port= 1;
|
---|
| 57 | while (*n != 0) port *= (*n++ & 0xFF);
|
---|
| 58 | port |= 0x8000;
|
---|
| 59 | }
|
---|
| 60 | return htons(port & 0xFFFF);
|
---|
| 61 | }
|
---|
| 62 |
|
---|
| 63 | static void usage(void)
|
---|
| 64 | {
|
---|
| 65 | fprintf(stderr,
|
---|
| 66 | "Usage: %s [-lcio] [-h host] key [command [arg ...]]\n"
|
---|
| 67 | "\t-l: Open TCP socket and listen (default for rput)\n"
|
---|
| 68 | "\t-c: Connect to a remote TCP socket (default for rget)\n"
|
---|
| 69 | "\t-i: Tie standard input to the TCP stream (default for rget)\n"
|
---|
| 70 | "\t-o: Tie standard output to the TCP stream (default for rput)\n"
|
---|
| 71 | "\t-io: Bidirectional!\n"
|
---|
| 72 | "\tkey: A word to hash into a port number, or simply a port number\n",
|
---|
| 73 | name);
|
---|
| 74 | exit(1);
|
---|
| 75 | }
|
---|
| 76 |
|
---|
| 77 | int main(int argc, char **argv)
|
---|
| 78 | {
|
---|
| 79 | int i, s;
|
---|
| 80 |
|
---|
| 81 | if ((name= strrchr(argv[0], '/')) == nil) name= argv[0]; else name++;
|
---|
| 82 |
|
---|
| 83 | if (strcmp(name, "rget") != 0 && strcmp(name, "rput") != 0) {
|
---|
| 84 | fprintf(stderr, "Don't know what to do if you call me '%s'\n", name);
|
---|
| 85 | exit(1);
|
---|
| 86 | }
|
---|
| 87 |
|
---|
| 88 | i= 1;
|
---|
| 89 | while (i < argc && argv[i][0] == '-') {
|
---|
| 90 | char *opt= argv[i++]+1;
|
---|
| 91 |
|
---|
| 92 | if (opt[0] == '-' && opt[1] == 0) break; /* -- */
|
---|
| 93 |
|
---|
| 94 | while (*opt != 0) switch (*opt++) {
|
---|
| 95 | case 'l': lflag= 1; break;
|
---|
| 96 | case 'c': cflag= 1; break;
|
---|
| 97 | case 'i': iflag= 1; break;
|
---|
| 98 | case 'o': oflag= 1; break;
|
---|
| 99 | case 'h':
|
---|
| 100 | hflag= 1;
|
---|
| 101 | if (*opt == 0) {
|
---|
| 102 | if (i == argc) usage();
|
---|
| 103 | opt= argv[i++];
|
---|
| 104 | }
|
---|
| 105 | host= opt;
|
---|
| 106 | opt= "";
|
---|
| 107 | break;
|
---|
| 108 | default: usage(); break;
|
---|
| 109 | }
|
---|
| 110 | }
|
---|
| 111 |
|
---|
| 112 | if (i == argc) usage();
|
---|
| 113 | key= argv[i++];
|
---|
| 114 | cmdv= argv + i;
|
---|
| 115 |
|
---|
| 116 | /* Defaults. */
|
---|
| 117 | if (!lflag && !cflag) {
|
---|
| 118 | if (name[1] == 'p') lflag= 1;
|
---|
| 119 | if (name[1] == 'g') cflag= 1;
|
---|
| 120 | }
|
---|
| 121 | if (!iflag && !oflag) {
|
---|
| 122 | if (name[1] == 'g') iflag= 1;
|
---|
| 123 | if (name[1] == 'p') oflag= 1;
|
---|
| 124 | }
|
---|
| 125 |
|
---|
| 126 | /* Constraints. */
|
---|
| 127 | if (lflag && cflag) {
|
---|
| 128 | fprintf(stderr, "%s: -c and -l don't mix\n", name);
|
---|
| 129 | usage();
|
---|
| 130 | }
|
---|
| 131 | if (cflag && !hflag) {
|
---|
| 132 | fprintf(stderr, "%s: -c requires a host name given with -h\n", name);
|
---|
| 133 | usage();
|
---|
| 134 | }
|
---|
| 135 | if (lflag && hflag) {
|
---|
| 136 | fprintf(stderr, "%s: -l does not require a host name given with -h\n",
|
---|
| 137 | name);
|
---|
| 138 | usage();
|
---|
| 139 | }
|
---|
| 140 | if (iflag && oflag && cmdv[0] == nil) {
|
---|
| 141 | fprintf(stderr, "%s: -io requires that a command is given\n", name);
|
---|
| 142 | usage();
|
---|
| 143 | }
|
---|
| 144 |
|
---|
| 145 | if (hflag) {
|
---|
| 146 | if ((hent= gethostbyname(host)) == nil) {
|
---|
| 147 | fprintf(stderr, "%s: %s: Name lookup failed\n", name, host);
|
---|
| 148 | exit(1);
|
---|
| 149 | }
|
---|
| 150 | }
|
---|
| 151 |
|
---|
| 152 | s= -1;
|
---|
| 153 | if (lflag) {
|
---|
| 154 | /* We need to listen and wait. (We're "rput", most likely.) */
|
---|
| 155 | #if __minix
|
---|
| 156 | char *tcp_device;
|
---|
| 157 | struct nwio_tcpconf tcpconf;
|
---|
| 158 | struct nwio_tcpcl tcplistenopt;
|
---|
| 159 |
|
---|
| 160 | if ((tcp_device= getenv("TCP_DEVICE")) == nil) tcp_device= "/dev/tcp";
|
---|
| 161 | if ((s= open(tcp_device, O_RDWR)) < 0) fatal(tcp_device);
|
---|
| 162 |
|
---|
| 163 | tcpconf.nwtc_flags=
|
---|
| 164 | NWTC_EXCL | NWTC_LP_SET | NWTC_UNSET_RA | NWTC_UNSET_RP;
|
---|
| 165 | tcpconf.nwtc_locport= name2port(key);
|
---|
| 166 | if (ioctl(s, NWIOSTCPCONF, &tcpconf) < 0) fatal("NWIOSTCPCONF");
|
---|
| 167 |
|
---|
| 168 | tcplistenopt.nwtcl_flags= 0;
|
---|
| 169 | if (ioctl(s, NWIOTCPLISTEN, &tcplistenopt) < 0) fatal("NWIOTCPLISTEN");
|
---|
| 170 | #else
|
---|
| 171 | int sa;
|
---|
| 172 | struct sockaddr_in channel;
|
---|
| 173 | static int on= 1;
|
---|
| 174 |
|
---|
| 175 | if ((s= socket(AF_INET, SOCK_STREAM, IPPROTO_TCP))<0) fatal("socket()");
|
---|
| 176 |
|
---|
| 177 | (void) setsockopt(s, SOL_SOCKET, SO_REUSEADDR, (char *) &on,
|
---|
| 178 | sizeof(on));
|
---|
| 179 | memset(&channel, 0, sizeof(channel));
|
---|
| 180 | channel.sin_family= AF_INET;
|
---|
| 181 | channel.sin_addr.s_addr= htonl(INADDR_ANY);
|
---|
| 182 | channel.sin_port= name2port(key);
|
---|
| 183 | if (bind(s, (struct sockaddr *) &channel, sizeof(channel)) < 0)
|
---|
| 184 | fatal("bind()");
|
---|
| 185 |
|
---|
| 186 | if (listen(s, 0) < 0) fatal("listen()");
|
---|
| 187 |
|
---|
| 188 | if ((sa= accept(s, nil, nil)) < 0) fatal("accept()");
|
---|
| 189 | close(s);
|
---|
| 190 | s= sa;
|
---|
| 191 | #endif
|
---|
| 192 | }
|
---|
| 193 |
|
---|
| 194 | if (cflag) {
|
---|
| 195 | /* Connect to the remote end. (We're "rget", most likely.) */
|
---|
| 196 | #if __minix
|
---|
| 197 | int n;
|
---|
| 198 | char *tcp_device;
|
---|
| 199 | nwio_tcpconf_t tcpconf;
|
---|
| 200 | nwio_tcpcl_t tcpconnopt;
|
---|
| 201 |
|
---|
| 202 | if ((tcp_device= getenv("TCP_DEVICE")) == nil) tcp_device= "/dev/tcp";
|
---|
| 203 |
|
---|
| 204 | n=60;
|
---|
| 205 | for (;;) {
|
---|
| 206 | if ((s= open(tcp_device, O_RDWR)) < 0) fatal(tcp_device);
|
---|
| 207 |
|
---|
| 208 | tcpconf.nwtc_flags= NWTC_LP_SEL | NWTC_SET_RA | NWTC_SET_RP;
|
---|
| 209 | memcpy(&tcpconf.nwtc_remaddr, hent->h_addr,
|
---|
| 210 | sizeof(tcpconf.nwtc_remaddr));
|
---|
| 211 | tcpconf.nwtc_remport= name2port(key);
|
---|
| 212 | if (ioctl(s, NWIOSTCPCONF, &tcpconf) < 0) fatal("NWIOSTCPCONF");
|
---|
| 213 |
|
---|
| 214 | tcpconnopt.nwtcl_flags= 0;
|
---|
| 215 | if (ioctl(s, NWIOTCPCONN, &tcpconnopt) == 0) break;
|
---|
| 216 |
|
---|
| 217 | if (--n > 0) sleep(2); else fatal("NWIOTCPCONN");
|
---|
| 218 | close(s);
|
---|
| 219 | }
|
---|
| 220 | #else
|
---|
| 221 | int n;
|
---|
| 222 | struct sockaddr_in channel;
|
---|
| 223 |
|
---|
| 224 | n=60;
|
---|
| 225 | for (;;) {
|
---|
| 226 | if ((s= socket(PF_INET, SOCK_STREAM, IPPROTO_TCP)) < 0)
|
---|
| 227 | fatal("socket()");
|
---|
| 228 |
|
---|
| 229 | memset(&channel, 0, sizeof(channel));
|
---|
| 230 | channel.sin_family= AF_INET;
|
---|
| 231 | memcpy(&channel.sin_addr.s_addr, hent->h_addr,
|
---|
| 232 | sizeof(channel.sin_addr.s_addr));
|
---|
| 233 | channel.sin_port= name2port(key);
|
---|
| 234 | if (connect(s, (struct sockaddr *) &channel,
|
---|
| 235 | sizeof(channel)) >= 0) break;
|
---|
| 236 |
|
---|
| 237 | if (--n > 0) sleep(2); else fatal("connect()");
|
---|
| 238 | close(s);
|
---|
| 239 | }
|
---|
| 240 | #endif
|
---|
| 241 | }
|
---|
| 242 |
|
---|
| 243 | if (cmdv[0] != nil) {
|
---|
| 244 | /* A command is given, so execute it with standard input (rget),
|
---|
| 245 | * standard output (rput) or both (-io) tied to the TCP stream.
|
---|
| 246 | */
|
---|
| 247 | if (iflag) dup2(s, 0);
|
---|
| 248 | if (oflag) dup2(s, 1);
|
---|
| 249 | close(s);
|
---|
| 250 |
|
---|
| 251 | execvp(cmdv[0], cmdv);
|
---|
| 252 | fatal(cmdv[0]);
|
---|
| 253 | } else {
|
---|
| 254 | /* Without a command we have to copy bytes ourselves, probably to or
|
---|
| 255 | * from a command that is connected to us with a pipe. (The original
|
---|
| 256 | * function of rput/rget, a remote pipe.)
|
---|
| 257 | */
|
---|
| 258 | int fi, fo;
|
---|
| 259 | int n;
|
---|
| 260 | char buf[8192];
|
---|
| 261 |
|
---|
| 262 | if (iflag) {
|
---|
| 263 | fi= s;
|
---|
| 264 | fo= 1;
|
---|
| 265 | } else {
|
---|
| 266 | fi= 0;
|
---|
| 267 | fo= s;
|
---|
| 268 | }
|
---|
| 269 |
|
---|
| 270 | while ((n= read(fi, buf, sizeof(buf))) > 0) {
|
---|
| 271 | char *bp= buf;
|
---|
| 272 |
|
---|
| 273 | while (n > 0) {
|
---|
| 274 | int r;
|
---|
| 275 |
|
---|
| 276 | if ((r= write(fo, bp, n)) <= 0) {
|
---|
| 277 | if (r == 0) {
|
---|
| 278 | fprintf(stderr, "%s: write(): Unexpected EOF\n", name);
|
---|
| 279 | exit(1);
|
---|
| 280 | }
|
---|
| 281 | fatal("write()");
|
---|
| 282 | }
|
---|
| 283 | bp+= r;
|
---|
| 284 | n-= r;
|
---|
| 285 | }
|
---|
| 286 | }
|
---|
| 287 | if (n < 0) fatal("read()");
|
---|
| 288 | }
|
---|
| 289 | return 0;
|
---|
| 290 | }
|
---|