1 /* rget 2.6 - remote pipe Author: Kees J. Bot
4 * here$ ... | rput key there$ rget -h here key | ...
5 * here$ rput key command ... there$ rget -h here key command ...
7 * (Once my first try at network programming, completely reworked by now.)
10 #include <sys/types.h>
11 #include <sys/ioctl.h>
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>
27 #include <net/netlib.h>
29 #include <sys/socket.h>
30 #include <netinet/in.h>
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 ...] */
41 static void fatal(const char *label
)
45 fprintf(stderr
, "%s: %s: %s\n", name
, label
, strerror(err
));
49 static unsigned name2port(char *n
)
54 port
= strtoul(n
, &end
, 0);
55 if (end
== n
|| *end
!= 0) {
57 while (*n
!= 0) port
*= (*n
++ & 0xFF);
60 return htons(port
& 0xFFFF);
63 static void usage(void)
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",
77 int main(int argc
, char **argv
)
81 if ((name
= strrchr(argv
[0], '/')) == nil
) name
= argv
[0]; else name
++;
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
);
89 while (i
< argc
&& argv
[i
][0] == '-') {
90 char *opt
= argv
[i
++]+1;
92 if (opt
[0] == '-' && opt
[1] == 0) break; /* -- */
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;
102 if (i
== argc
) usage();
108 default: usage(); break;
112 if (i
== argc
) usage();
117 if (!lflag
&& !cflag
) {
118 if (name
[1] == 'p') lflag
= 1;
119 if (name
[1] == 'g') cflag
= 1;
121 if (!iflag
&& !oflag
) {
122 if (name
[1] == 'g') iflag
= 1;
123 if (name
[1] == 'p') oflag
= 1;
127 if (lflag
&& cflag
) {
128 fprintf(stderr
, "%s: -c and -l don't mix\n", name
);
131 if (cflag
&& !hflag
) {
132 fprintf(stderr
, "%s: -c requires a host name given with -h\n", name
);
135 if (lflag
&& hflag
) {
136 fprintf(stderr
, "%s: -l does not require a host name given with -h\n",
140 if (iflag
&& oflag
&& cmdv
[0] == nil
) {
141 fprintf(stderr
, "%s: -io requires that a command is given\n", name
);
146 if ((hent
= gethostbyname(host
)) == nil
) {
147 fprintf(stderr
, "%s: %s: Name lookup failed\n", name
, host
);
154 /* We need to listen and wait. (We're "rput", most likely.) */
157 struct nwio_tcpconf tcpconf
;
158 struct nwio_tcpcl tcplistenopt
;
160 if ((tcp_device
= getenv("TCP_DEVICE")) == nil
) tcp_device
= "/dev/tcp";
161 if ((s
= open(tcp_device
, O_RDWR
)) < 0) fatal(tcp_device
);
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");
168 tcplistenopt
.nwtcl_flags
= 0;
169 if (ioctl(s
, NWIOTCPLISTEN
, &tcplistenopt
) < 0) fatal("NWIOTCPLISTEN");
172 struct sockaddr_in channel
;
175 if ((s
= socket(AF_INET
, SOCK_STREAM
, IPPROTO_TCP
))<0) fatal("socket()");
177 (void) setsockopt(s
, SOL_SOCKET
, SO_REUSEADDR
, (char *) &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)
186 if (listen(s
, 0) < 0) fatal("listen()");
188 if ((sa
= accept(s
, nil
, nil
)) < 0) fatal("accept()");
195 /* Connect to the remote end. (We're "rget", most likely.) */
199 nwio_tcpconf_t tcpconf
;
200 nwio_tcpcl_t tcpconnopt
;
202 if ((tcp_device
= getenv("TCP_DEVICE")) == nil
) tcp_device
= "/dev/tcp";
206 if ((s
= open(tcp_device
, O_RDWR
)) < 0) fatal(tcp_device
);
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");
214 tcpconnopt
.nwtcl_flags
= 0;
215 if (ioctl(s
, NWIOTCPCONN
, &tcpconnopt
) == 0) break;
217 if (--n
> 0) sleep(2); else fatal("NWIOTCPCONN");
222 struct sockaddr_in channel
;
226 if ((s
= socket(PF_INET
, SOCK_STREAM
, IPPROTO_TCP
)) < 0)
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;
237 if (--n
> 0) sleep(2); else fatal("connect()");
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.
247 if (iflag
) dup2(s
, 0);
248 if (oflag
) dup2(s
, 1);
251 execvp(cmdv
[0], cmdv
);
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.)
270 while ((n
= read(fi
, buf
, sizeof(buf
))) > 0) {
276 if ((r
= write(fo
, bp
, n
)) <= 0) {
278 fprintf(stderr
, "%s: write(): Unexpected EOF\n", name
);
287 if (n
< 0) fatal("read()");