PageRenderTime 54ms CodeModel.GetById 26ms RepoModel.GetById 0ms app.codeStats 0ms

commands/rget/rget.c

http://www.minix3.org/
C | 290 lines | 232 code | 39 blank | 19 comment | 92 complexity | e33922ed58ac68c101c1b1ea42ff3a19 MD5 | raw file
Possible License(s): MIT, WTFPL, AGPL-1.0, BSD-3-Clause, GPL-3.0, LGPL-2.0, JSON, 0BSD
  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. static char *name;
  34. static int iflag, oflag, hflag, lflag, cflag; /* -iolch? */
  35. static char *host; /* Argument to -h. */
  36. static struct hostent *hent; /* gethostbyname(host) */
  37. static char *key; /* key (port) */
  38. static char **cmdv; /* command [arg ...] */
  39. static void fatal(const char *label)
  40. {
  41. int err= errno;
  42. fprintf(stderr, "%s: %s: %s\n", name, label, strerror(err));
  43. exit(1);
  44. }
  45. static unsigned name2port(char *n)
  46. {
  47. char *end;
  48. unsigned port;
  49. port= strtoul(n, &end, 0);
  50. if (end == n || *end != 0) {
  51. port= 1;
  52. while (*n != 0) port *= (*n++ & 0xFF);
  53. port |= 0x8000;
  54. }
  55. return htons(port & 0xFFFF);
  56. }
  57. static void usage(void)
  58. {
  59. fprintf(stderr,
  60. "Usage: %s [-lcio] [-h host] key [command [arg ...]]\n"
  61. "\t-l: Open TCP socket and listen (default for rput)\n"
  62. "\t-c: Connect to a remote TCP socket (default for rget)\n"
  63. "\t-i: Tie standard input to the TCP stream (default for rget)\n"
  64. "\t-o: Tie standard output to the TCP stream (default for rput)\n"
  65. "\t-io: Bidirectional!\n"
  66. "\tkey: A word to hash into a port number, or simply a port number\n",
  67. name);
  68. exit(1);
  69. }
  70. int main(int argc, char **argv)
  71. {
  72. int i, s;
  73. if ((name= strrchr(argv[0], '/')) == nil) name= argv[0]; else name++;
  74. if (strcmp(name, "rget") != 0 && strcmp(name, "rput") != 0) {
  75. fprintf(stderr, "Don't know what to do if you call me '%s'\n", name);
  76. exit(1);
  77. }
  78. i= 1;
  79. while (i < argc && argv[i][0] == '-') {
  80. char *opt= argv[i++]+1;
  81. if (opt[0] == '-' && opt[1] == 0) break; /* -- */
  82. while (*opt != 0) switch (*opt++) {
  83. case 'l': lflag= 1; break;
  84. case 'c': cflag= 1; break;
  85. case 'i': iflag= 1; break;
  86. case 'o': oflag= 1; break;
  87. case 'h':
  88. hflag= 1;
  89. if (*opt == 0) {
  90. if (i == argc) usage();
  91. opt= argv[i++];
  92. }
  93. host= opt;
  94. opt= "";
  95. break;
  96. default: usage(); break;
  97. }
  98. }
  99. if (i == argc) usage();
  100. key= argv[i++];
  101. cmdv= argv + i;
  102. /* Defaults. */
  103. if (!lflag && !cflag) {
  104. if (name[1] == 'p') lflag= 1;
  105. if (name[1] == 'g') cflag= 1;
  106. }
  107. if (!iflag && !oflag) {
  108. if (name[1] == 'g') iflag= 1;
  109. if (name[1] == 'p') oflag= 1;
  110. }
  111. /* Constraints. */
  112. if (lflag && cflag) {
  113. fprintf(stderr, "%s: -c and -l don't mix\n", name);
  114. usage();
  115. }
  116. if (cflag && !hflag) {
  117. fprintf(stderr, "%s: -c requires a host name given with -h\n", name);
  118. usage();
  119. }
  120. if (lflag && hflag) {
  121. fprintf(stderr, "%s: -l does not require a host name given with -h\n",
  122. name);
  123. usage();
  124. }
  125. if (iflag && oflag && cmdv[0] == nil) {
  126. fprintf(stderr, "%s: -io requires that a command is given\n", name);
  127. usage();
  128. }
  129. if (hflag) {
  130. if ((hent= gethostbyname(host)) == nil) {
  131. fprintf(stderr, "%s: %s: Name lookup failed\n", name, host);
  132. exit(1);
  133. }
  134. }
  135. s= -1;
  136. if (lflag) {
  137. /* We need to listen and wait. (We're "rput", most likely.) */
  138. #if __minix
  139. char *tcp_device;
  140. struct nwio_tcpconf tcpconf;
  141. struct nwio_tcpcl tcplistenopt;
  142. if ((tcp_device= getenv("TCP_DEVICE")) == nil) tcp_device= "/dev/tcp";
  143. if ((s= open(tcp_device, O_RDWR)) < 0) fatal(tcp_device);
  144. tcpconf.nwtc_flags=
  145. NWTC_EXCL | NWTC_LP_SET | NWTC_UNSET_RA | NWTC_UNSET_RP;
  146. tcpconf.nwtc_locport= name2port(key);
  147. if (ioctl(s, NWIOSTCPCONF, &tcpconf) < 0) fatal("NWIOSTCPCONF");
  148. tcplistenopt.nwtcl_flags= 0;
  149. if (ioctl(s, NWIOTCPLISTEN, &tcplistenopt) < 0) fatal("NWIOTCPLISTEN");
  150. #else
  151. int sa;
  152. struct sockaddr_in channel;
  153. static int on= 1;
  154. if ((s= socket(AF_INET, SOCK_STREAM, IPPROTO_TCP))<0) fatal("socket()");
  155. (void) setsockopt(s, SOL_SOCKET, SO_REUSEADDR, (char *) &on,
  156. sizeof(on));
  157. memset(&channel, 0, sizeof(channel));
  158. channel.sin_family= AF_INET;
  159. channel.sin_addr.s_addr= htonl(INADDR_ANY);
  160. channel.sin_port= name2port(key);
  161. if (bind(s, (struct sockaddr *) &channel, sizeof(channel)) < 0)
  162. fatal("bind()");
  163. if (listen(s, 0) < 0) fatal("listen()");
  164. if ((sa= accept(s, nil, nil)) < 0) fatal("accept()");
  165. close(s);
  166. s= sa;
  167. #endif
  168. }
  169. if (cflag) {
  170. /* Connect to the remote end. (We're "rget", most likely.) */
  171. #if __minix
  172. int n;
  173. char *tcp_device;
  174. nwio_tcpconf_t tcpconf;
  175. nwio_tcpcl_t tcpconnopt;
  176. if ((tcp_device= getenv("TCP_DEVICE")) == nil) tcp_device= "/dev/tcp";
  177. n=60;
  178. for (;;) {
  179. if ((s= open(tcp_device, O_RDWR)) < 0) fatal(tcp_device);
  180. tcpconf.nwtc_flags= NWTC_LP_SEL | NWTC_SET_RA | NWTC_SET_RP;
  181. memcpy(&tcpconf.nwtc_remaddr, hent->h_addr,
  182. sizeof(tcpconf.nwtc_remaddr));
  183. tcpconf.nwtc_remport= name2port(key);
  184. if (ioctl(s, NWIOSTCPCONF, &tcpconf) < 0) fatal("NWIOSTCPCONF");
  185. tcpconnopt.nwtcl_flags= 0;
  186. if (ioctl(s, NWIOTCPCONN, &tcpconnopt) == 0) break;
  187. if (--n > 0) sleep(2); else fatal("NWIOTCPCONN");
  188. close(s);
  189. }
  190. #else
  191. int n;
  192. struct sockaddr_in channel;
  193. n=60;
  194. for (;;) {
  195. if ((s= socket(PF_INET, SOCK_STREAM, IPPROTO_TCP)) < 0)
  196. fatal("socket()");
  197. memset(&channel, 0, sizeof(channel));
  198. channel.sin_family= AF_INET;
  199. memcpy(&channel.sin_addr.s_addr, hent->h_addr,
  200. sizeof(channel.sin_addr.s_addr));
  201. channel.sin_port= name2port(key);
  202. if (connect(s, (struct sockaddr *) &channel,
  203. sizeof(channel)) >= 0) break;
  204. if (--n > 0) sleep(2); else fatal("connect()");
  205. close(s);
  206. }
  207. #endif
  208. }
  209. if (cmdv[0] != nil) {
  210. /* A command is given, so execute it with standard input (rget),
  211. * standard output (rput) or both (-io) tied to the TCP stream.
  212. */
  213. if (iflag) dup2(s, 0);
  214. if (oflag) dup2(s, 1);
  215. close(s);
  216. execvp(cmdv[0], cmdv);
  217. fatal(cmdv[0]);
  218. } else {
  219. /* Without a command we have to copy bytes ourselves, probably to or
  220. * from a command that is connected to us with a pipe. (The original
  221. * function of rput/rget, a remote pipe.)
  222. */
  223. int fi, fo;
  224. int n;
  225. char buf[8192];
  226. if (iflag) {
  227. fi= s;
  228. fo= 1;
  229. } else {
  230. fi= 0;
  231. fo= s;
  232. }
  233. while ((n= read(fi, buf, sizeof(buf))) > 0) {
  234. char *bp= buf;
  235. while (n > 0) {
  236. int r;
  237. if ((r= write(fo, bp, n)) <= 0) {
  238. if (r == 0) {
  239. fprintf(stderr, "%s: write(): Unexpected EOF\n", name);
  240. exit(1);
  241. }
  242. fatal("write()");
  243. }
  244. bp+= r;
  245. n-= r;
  246. }
  247. }
  248. if (n < 0) fatal("read()");
  249. }
  250. return 0;
  251. }