Serenity Operating System
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
27#include <arpa/inet.h>
28#include <errno.h>
29#include <getopt.h>
30#include <netinet/in.h>
31#include <stdio.h>
32#include <stdlib.h>
33#include <string.h>
34#include <sys/select.h>
35#include <sys/socket.h>
36#include <sys/time.h>
37#include <sys/types.h>
38#include <unistd.h>
39
40#define max(a, b) ((a > b) ? a : b)
41
42void exit_with_usage(int rc)
43{
44 fprintf(stderr, "Usage: nc [-l] [-v] [-h] [-N] [-s source] [-p source_port] [destination] [port]\n");
45 exit(rc);
46}
47
48int main(int argc, char** argv)
49{
50 bool should_listen = false;
51 bool verbose = false;
52 char *source_addr = nullptr;
53 int source_port = 0;
54 bool should_close = false;
55
56 int opt;
57 while ((opt = getopt(argc, argv, "hlvs:p:N")) != -1) {
58 switch (opt) {
59 case 'h':
60 exit_with_usage(0);
61 break;
62 case 'l':
63 should_listen = true;
64 break;
65 case 'v':
66 verbose = true;
67 break;
68 case 's':
69 source_addr = strdup(optarg);
70 break;
71 case 'p':
72 source_port = atoi(optarg);
73 break;
74 case 'N':
75 should_close = true;
76 break;
77 }
78 }
79
80 int fd;
81
82 if (should_listen) {
83 int listen_fd = socket(AF_INET, SOCK_STREAM, 0);
84 if (listen_fd < 0) {
85 perror("socket");
86 return 1;
87 }
88
89 struct sockaddr_in sa;
90 memset(&sa, 0, sizeof sa);
91 sa.sin_family = AF_INET;
92 sa.sin_port = htons(source_port);
93 sa.sin_addr.s_addr = htonl(INADDR_ANY);
94 if (source_addr) {
95 if (inet_pton(AF_INET, source_addr, &sa.sin_addr) < 0) {
96 perror("inet_pton");
97 return 1;
98 }
99 }
100
101 if (bind(listen_fd, (struct sockaddr*)&sa, sizeof(sa)) == -1) {
102 perror("bind");
103 return 1;
104 }
105
106 if (listen(listen_fd, 1) == -1) {
107 perror("listen");
108 return 1;
109 }
110
111 char addr_str[100];
112
113 struct sockaddr_in sin;
114 socklen_t len;
115
116 len = sizeof(sin);
117 if (getsockname(listen_fd, (struct sockaddr*)&sin, &len) == -1) {
118 perror("getsockname");
119 return 1;
120 }
121 if (verbose)
122 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));
123
124 len = sizeof(sin);
125 fd = accept(listen_fd, (struct sockaddr*)&sin, &len);
126 if (fd == -1) {
127 perror("accept");
128 return 1;
129 }
130
131 if (verbose)
132 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));
133
134 if (close(listen_fd) == -1) {
135 perror("close");
136 return 1;
137 };
138 } else {
139 if ((argc - optind) < 2) {
140 exit_with_usage(-1);
141 }
142
143 const char* addr_arg = argv[optind];
144 int port_arg = atoi(argv[optind + 1]);
145 if (port_arg < 0) {
146 perror("atoi");
147 return 1;
148 }
149
150 fd = socket(AF_INET, SOCK_STREAM, 0);
151 if (fd < 0) {
152 perror("socket");
153 return 1;
154 }
155
156 struct timeval timeout {
157 3, 0
158 };
159 if (setsockopt(fd, SOL_SOCKET, SO_RCVTIMEO, &timeout, sizeof(timeout)) < 0) {
160 perror("setsockopt");
161 return 1;
162 }
163 if (setsockopt(fd, SOL_SOCKET, SO_SNDTIMEO, &timeout, sizeof(timeout)) < 0) {
164 perror("setsockopt");
165 return 1;
166 }
167
168 char addr_str[100];
169
170 struct sockaddr_in dst_addr;
171 memset(&dst_addr, 0, sizeof(dst_addr));
172
173 dst_addr.sin_family = AF_INET;
174 dst_addr.sin_port = htons(port_arg);
175 if (inet_pton(AF_INET, addr_arg, &dst_addr.sin_addr) < 0) {
176 perror("inet_pton");
177 return 1;
178 }
179
180 if (verbose)
181 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));
182 if (connect(fd, (struct sockaddr*)&dst_addr, sizeof(dst_addr)) < 0) {
183 perror("connect");
184 return 1;
185 }
186 if (verbose)
187 fprintf(stderr, "connected!\n");
188 }
189
190 bool stdin_closed = false;
191 bool fd_closed = false;
192
193 fd_set readfds, writefds, exceptfds;
194
195 while (!stdin_closed || !fd_closed) {
196 FD_ZERO(&readfds);
197 FD_ZERO(&writefds);
198 FD_ZERO(&exceptfds);
199
200 int highest_fd = 0;
201
202 if (!stdin_closed) {
203 FD_SET(STDIN_FILENO, &readfds);
204 FD_SET(STDIN_FILENO, &exceptfds);
205 highest_fd = max(highest_fd, STDIN_FILENO);
206 }
207 if (!fd_closed) {
208 FD_SET(fd, &readfds);
209 FD_SET(fd, &exceptfds);
210 highest_fd = max(highest_fd, fd);
211 }
212
213 int ready = select(highest_fd + 1, &readfds, &writefds, &exceptfds, NULL);
214 if (ready == -1) {
215 if (errno == EINTR)
216 continue;
217
218 perror("select");
219 return 1;
220 }
221
222 if (!stdin_closed && FD_ISSET(STDIN_FILENO, &readfds)) {
223 char buf[1024];
224 int nread = read(STDIN_FILENO, buf, sizeof(buf));
225 if (nread < 0) {
226 perror("read(STDIN_FILENO)");
227 return 1;
228 }
229
230 // stdin closed
231 if (nread == 0) {
232 stdin_closed = true;
233 if (verbose)
234 fprintf(stderr, "stdin closed\n");
235 if (should_close) {
236 close(fd);
237 fd_closed = true;
238 }
239 } else if (write(fd, buf, nread) < 0) {
240 perror("write(fd)");
241 return 1;
242 }
243 }
244
245 if (!fd_closed && FD_ISSET(fd, &readfds)) {
246 char buf[1024];
247 int nread = read(fd, buf, sizeof(buf));
248 if (nread < 0) {
249 perror("read(fd)");
250 return 1;
251 }
252
253 // remote end closed
254 if (nread == 0) {
255 close(STDIN_FILENO);
256 stdin_closed = true;
257 fd_closed = true;
258 if (verbose)
259 fprintf(stderr, "remote closed\n");
260 } else if (write(STDOUT_FILENO, buf, nread) < 0) {
261 perror("write(STDOUT_FILENO)");
262 return 1;
263 }
264 }
265 }
266
267 return 0;
268}