nc.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. /* vi: set sw=4 ts=4: */
  2. /* nc: mini-netcat - built from the ground up for LRP
  3. Copyright (C) 1998 Charles P. Wright
  4. 0.0.1 6K It works.
  5. 0.0.2 5K Smaller and you can also check the exit condition if you wish.
  6. 0.0.3 Uses select()
  7. 19980918 Busy Boxed! Dave Cinege
  8. 19990512 Uses Select. Charles P. Wright
  9. 19990513 Fixes stdin stupidity and uses buffers. Charles P. Wright
  10. This program is free software; you can redistribute it and/or modify
  11. it under the terms of the GNU General Public License as published by
  12. the Free Software Foundation; either version 2 of the License, or
  13. (at your option) any later version.
  14. This program is distributed in the hope that it will be useful,
  15. but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. GNU General Public License for more details.
  18. You should have received a copy of the GNU General Public License
  19. along with this program; if not, write to the Free Software
  20. Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  21. */
  22. #include <stdio.h>
  23. #include <stdlib.h>
  24. #include <string.h>
  25. #include <unistd.h>
  26. #include <signal.h>
  27. #include <sys/types.h>
  28. #include <sys/socket.h>
  29. #include <netinet/in.h>
  30. #include <arpa/inet.h>
  31. #include <netdb.h>
  32. #include <sys/time.h>
  33. #include <sys/ioctl.h>
  34. #include "busybox.h"
  35. static void timeout(int signum)
  36. {
  37. bb_error_msg_and_die("Timed out");
  38. }
  39. int nc_main(int argc, char **argv)
  40. {
  41. int do_listen = 0, lport = 0, delay = 0, wsecs = 0, tmpfd, opt, sfd, x;
  42. char buf[BUFSIZ];
  43. #ifdef CONFIG_NC_GAPING_SECURITY_HOLE
  44. char *pr00gie = NULL;
  45. #endif
  46. struct sockaddr_in address;
  47. struct hostent *hostinfo;
  48. fd_set readfds, testfds;
  49. while ((opt = getopt(argc, argv, "lp:i:e:w:")) > 0) {
  50. switch (opt) {
  51. case 'l':
  52. do_listen++;
  53. break;
  54. case 'p':
  55. lport = bb_lookup_port(optarg, "tcp", 0);
  56. break;
  57. case 'i':
  58. delay = atoi(optarg);
  59. break;
  60. #ifdef CONFIG_NC_GAPING_SECURITY_HOLE
  61. case 'e':
  62. pr00gie = optarg;
  63. break;
  64. #endif
  65. case 'w':
  66. wsecs = atoi(optarg);
  67. break;
  68. default:
  69. bb_show_usage();
  70. }
  71. }
  72. #ifdef CONFIG_NC_GAPING_SECURITY_HOLE
  73. if (pr00gie) {
  74. /* won't need stdin */
  75. close(STDIN_FILENO);
  76. }
  77. #endif /* CONFIG_NC_GAPING_SECURITY_HOLE */
  78. if ((do_listen && optind != argc) || (!do_listen && optind + 2 != argc))
  79. bb_show_usage();
  80. if ((sfd = socket(AF_INET, SOCK_STREAM, 0)) < 0)
  81. bb_perror_msg_and_die("socket");
  82. x = 1;
  83. if (setsockopt(sfd, SOL_SOCKET, SO_REUSEADDR, &x, sizeof (x)) == -1)
  84. bb_perror_msg_and_die("reuseaddr");
  85. address.sin_family = AF_INET;
  86. if (wsecs) {
  87. signal(SIGALRM, timeout);
  88. alarm(wsecs);
  89. }
  90. if (lport != 0) {
  91. memset(&address.sin_addr, 0, sizeof(address.sin_addr));
  92. address.sin_port = lport;
  93. if (bind(sfd, (struct sockaddr *) &address, sizeof(address)) < 0)
  94. bb_perror_msg_and_die("bind");
  95. }
  96. if (do_listen) {
  97. socklen_t addrlen = sizeof(address);
  98. if (listen(sfd, 1) < 0)
  99. bb_perror_msg_and_die("listen");
  100. if ((tmpfd = accept(sfd, (struct sockaddr *) &address, &addrlen)) < 0)
  101. bb_perror_msg_and_die("accept");
  102. close(sfd);
  103. sfd = tmpfd;
  104. } else {
  105. hostinfo = xgethostbyname(argv[optind]);
  106. address.sin_addr = *(struct in_addr *) *hostinfo->h_addr_list;
  107. address.sin_port = bb_lookup_port(argv[optind+1], "tcp", 0);
  108. if (connect(sfd, (struct sockaddr *) &address, sizeof(address)) < 0)
  109. bb_perror_msg_and_die("connect");
  110. }
  111. if (wsecs) {
  112. alarm(0);
  113. signal(SIGALRM, SIG_DFL);
  114. }
  115. #ifdef CONFIG_NC_GAPING_SECURITY_HOLE
  116. /* -e given? */
  117. if (pr00gie) {
  118. dup2(sfd, 0);
  119. close(sfd);
  120. dup2(0, 1);
  121. dup2(0, 2);
  122. execl(pr00gie, pr00gie, NULL);
  123. /* Don't print stuff or it will go over the wire.... */
  124. _exit(-1);
  125. }
  126. #endif /* CONFIG_NC_GAPING_SECURITY_HOLE */
  127. FD_ZERO(&readfds);
  128. FD_SET(sfd, &readfds);
  129. FD_SET(STDIN_FILENO, &readfds);
  130. while (1) {
  131. int fd;
  132. int ofd;
  133. int nread;
  134. testfds = readfds;
  135. if (select(FD_SETSIZE, &testfds, NULL, NULL, NULL) < 0)
  136. bb_perror_msg_and_die("select");
  137. for (fd = 0; fd < FD_SETSIZE; fd++) {
  138. if (FD_ISSET(fd, &testfds)) {
  139. if ((nread = safe_read(fd, buf, sizeof(buf))) < 0)
  140. bb_perror_msg_and_die(bb_msg_read_error);
  141. if (fd == sfd) {
  142. if (nread == 0)
  143. exit(0);
  144. ofd = STDOUT_FILENO;
  145. } else {
  146. if (nread == 0)
  147. shutdown(sfd, 1);
  148. ofd = sfd;
  149. }
  150. if (bb_full_write(ofd, buf, nread) < 0)
  151. bb_perror_msg_and_die(bb_msg_write_error);
  152. if (delay > 0) {
  153. sleep(delay);
  154. }
  155. }
  156. }
  157. }
  158. }