nc.cpp 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. /*
  2. * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright notice, this
  9. * list of conditions and the following disclaimer.
  10. *
  11. * 2. Redistributions in binary form must reproduce the above copyright notice,
  12. * this list of conditions and the following disclaimer in the documentation
  13. * and/or other materials provided with the distribution.
  14. *
  15. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  16. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  17. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  18. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  19. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  20. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  21. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  22. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  23. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  24. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. */
  26. #include <LibCore/ArgsParser.h>
  27. #include <arpa/inet.h>
  28. #include <errno.h>
  29. #include <netinet/in.h>
  30. #include <stdio.h>
  31. #include <stdlib.h>
  32. #include <string.h>
  33. #include <sys/select.h>
  34. #include <sys/socket.h>
  35. #include <sys/time.h>
  36. #include <sys/types.h>
  37. #include <unistd.h>
  38. int main(int argc, char** argv)
  39. {
  40. bool should_listen = false;
  41. bool verbose = false;
  42. bool should_close = false;
  43. const char* addr = nullptr;
  44. int port = 0;
  45. Core::ArgsParser args_parser;
  46. args_parser.add_option(should_listen, "Listen instead of connecting", "listen", 'l');
  47. args_parser.add_option(verbose, "Log everything that's happening", "verbose", 'v');
  48. args_parser.add_option(should_close, "Close connection after reading stdin to the end", nullptr, 'N');
  49. args_parser.add_positional_argument(addr, "Address to connect to or listen on", "address");
  50. args_parser.add_positional_argument(port, "Port to connect to or listen on", "port");
  51. args_parser.parse(argc, argv);
  52. int fd;
  53. if (should_listen) {
  54. int listen_fd = socket(AF_INET, SOCK_STREAM, 0);
  55. if (listen_fd < 0) {
  56. perror("socket");
  57. return 1;
  58. }
  59. struct sockaddr_in sa;
  60. memset(&sa, 0, sizeof sa);
  61. sa.sin_family = AF_INET;
  62. sa.sin_port = htons(port);
  63. sa.sin_addr.s_addr = htonl(INADDR_ANY);
  64. if (addr) {
  65. if (inet_pton(AF_INET, addr, &sa.sin_addr) < 0) {
  66. perror("inet_pton");
  67. return 1;
  68. }
  69. }
  70. if (bind(listen_fd, (struct sockaddr*)&sa, sizeof(sa)) == -1) {
  71. perror("bind");
  72. return 1;
  73. }
  74. if (listen(listen_fd, 1) == -1) {
  75. perror("listen");
  76. return 1;
  77. }
  78. char addr_str[100];
  79. struct sockaddr_in sin;
  80. socklen_t len;
  81. len = sizeof(sin);
  82. if (getsockname(listen_fd, (struct sockaddr*)&sin, &len) == -1) {
  83. perror("getsockname");
  84. return 1;
  85. }
  86. if (verbose)
  87. fprintf(stderr, "waiting for a connection on %s:%d\n", inet_ntop(sin.sin_family, &sin.sin_addr, addr_str, sizeof(addr_str) - 1), ntohs(sin.sin_port));
  88. len = sizeof(sin);
  89. fd = accept(listen_fd, (struct sockaddr*)&sin, &len);
  90. if (fd == -1) {
  91. perror("accept");
  92. return 1;
  93. }
  94. if (verbose)
  95. fprintf(stderr, "got connection from %s:%d\n", inet_ntop(sin.sin_family, &sin.sin_addr, addr_str, sizeof(addr_str) - 1), ntohs(sin.sin_port));
  96. if (close(listen_fd) == -1) {
  97. perror("close");
  98. return 1;
  99. };
  100. } else {
  101. fd = socket(AF_INET, SOCK_STREAM, 0);
  102. if (fd < 0) {
  103. perror("socket");
  104. return 1;
  105. }
  106. struct timeval timeout {
  107. 3, 0
  108. };
  109. if (setsockopt(fd, SOL_SOCKET, SO_RCVTIMEO, &timeout, sizeof(timeout)) < 0) {
  110. perror("setsockopt");
  111. return 1;
  112. }
  113. if (setsockopt(fd, SOL_SOCKET, SO_SNDTIMEO, &timeout, sizeof(timeout)) < 0) {
  114. perror("setsockopt");
  115. return 1;
  116. }
  117. char addr_str[100];
  118. struct sockaddr_in dst_addr;
  119. memset(&dst_addr, 0, sizeof(dst_addr));
  120. dst_addr.sin_family = AF_INET;
  121. dst_addr.sin_port = htons(port);
  122. if (inet_pton(AF_INET, addr, &dst_addr.sin_addr) < 0) {
  123. perror("inet_pton");
  124. return 1;
  125. }
  126. if (verbose)
  127. fprintf(stderr, "connecting to %s:%d\n", inet_ntop(dst_addr.sin_family, &dst_addr.sin_addr, addr_str, sizeof(addr_str) - 1), ntohs(dst_addr.sin_port));
  128. if (connect(fd, (struct sockaddr*)&dst_addr, sizeof(dst_addr)) < 0) {
  129. perror("connect");
  130. return 1;
  131. }
  132. if (verbose)
  133. fprintf(stderr, "connected!\n");
  134. }
  135. bool stdin_closed = false;
  136. bool fd_closed = false;
  137. fd_set readfds, writefds, exceptfds;
  138. while (!stdin_closed || !fd_closed) {
  139. FD_ZERO(&readfds);
  140. FD_ZERO(&writefds);
  141. FD_ZERO(&exceptfds);
  142. int highest_fd = 0;
  143. if (!stdin_closed) {
  144. FD_SET(STDIN_FILENO, &readfds);
  145. FD_SET(STDIN_FILENO, &exceptfds);
  146. highest_fd = max(highest_fd, STDIN_FILENO);
  147. }
  148. if (!fd_closed) {
  149. FD_SET(fd, &readfds);
  150. FD_SET(fd, &exceptfds);
  151. highest_fd = max(highest_fd, fd);
  152. }
  153. int ready = select(highest_fd + 1, &readfds, &writefds, &exceptfds, NULL);
  154. if (ready == -1) {
  155. if (errno == EINTR)
  156. continue;
  157. perror("select");
  158. return 1;
  159. }
  160. if (!stdin_closed && FD_ISSET(STDIN_FILENO, &readfds)) {
  161. char buf[1024];
  162. int nread = read(STDIN_FILENO, buf, sizeof(buf));
  163. if (nread < 0) {
  164. perror("read(STDIN_FILENO)");
  165. return 1;
  166. }
  167. // stdin closed
  168. if (nread == 0) {
  169. stdin_closed = true;
  170. if (verbose)
  171. fprintf(stderr, "stdin closed\n");
  172. if (should_close) {
  173. close(fd);
  174. fd_closed = true;
  175. }
  176. } else if (write(fd, buf, nread) < 0) {
  177. perror("write(fd)");
  178. return 1;
  179. }
  180. }
  181. if (!fd_closed && FD_ISSET(fd, &readfds)) {
  182. char buf[1024];
  183. int nread = read(fd, buf, sizeof(buf));
  184. if (nread < 0) {
  185. perror("read(fd)");
  186. return 1;
  187. }
  188. // remote end closed
  189. if (nread == 0) {
  190. close(STDIN_FILENO);
  191. stdin_closed = true;
  192. fd_closed = true;
  193. if (verbose)
  194. fprintf(stderr, "remote closed\n");
  195. } else if (write(STDOUT_FILENO, buf, nread) < 0) {
  196. perror("write(STDOUT_FILENO)");
  197. return 1;
  198. }
  199. }
  200. }
  201. return 0;
  202. }