]>
Commit | Line | Data |
---|---|---|
53e1b683 | 1 | /* SPDX-License-Identifier: LGPL-2.1+ */ |
912b54ad | 2 | |
912b54ad | 3 | #include <errno.h> |
3f6fd1ba | 4 | #include <fcntl.h> |
912b54ad | 5 | #include <getopt.h> |
3f6fd1ba | 6 | #include <netdb.h> |
912b54ad DS |
7 | #include <stdio.h> |
8 | #include <stdlib.h> | |
9 | #include <string.h> | |
912b54ad DS |
10 | #include <sys/socket.h> |
11 | #include <sys/un.h> | |
12 | #include <unistd.h> | |
13 | ||
912b54ad DS |
14 | #include "sd-daemon.h" |
15 | #include "sd-event.h" | |
fb69d709 | 16 | #include "sd-resolve.h" |
3f6fd1ba | 17 | |
b5efdb8a | 18 | #include "alloc-util.h" |
3ffd4af2 | 19 | #include "fd-util.h" |
96c374d0 | 20 | #include "log.h" |
37ec0fdd | 21 | #include "parse-util.h" |
3f6fd1ba LP |
22 | #include "path-util.h" |
23 | #include "set.h" | |
912b54ad | 24 | #include "socket-util.h" |
07630cea | 25 | #include "string-util.h" |
37ec0fdd | 26 | #include "terminal-util.h" |
912b54ad | 27 | #include "util.h" |
8569a776 LP |
28 | |
29 | #define BUFFER_SIZE (256 * 1024) | |
dc3b8afb | 30 | static unsigned arg_connections_max = 256; |
912b54ad | 31 | |
fb69d709 LP |
32 | static const char *arg_remote_host = NULL; |
33 | ||
8569a776 | 34 | typedef struct Context { |
fb69d709 LP |
35 | sd_event *event; |
36 | sd_resolve *resolve; | |
37 | ||
8569a776 LP |
38 | Set *listen; |
39 | Set *connections; | |
40 | } Context; | |
912b54ad | 41 | |
8569a776 | 42 | typedef struct Connection { |
e633ea1c LP |
43 | Context *context; |
44 | ||
8569a776 LP |
45 | int server_fd, client_fd; |
46 | int server_to_client_buffer[2]; /* a pipe */ | |
47 | int client_to_server_buffer[2]; /* a pipe */ | |
912b54ad | 48 | |
8569a776 LP |
49 | size_t server_to_client_buffer_full, client_to_server_buffer_full; |
50 | size_t server_to_client_buffer_size, client_to_server_buffer_size; | |
51 | ||
52 | sd_event_source *server_event_source, *client_event_source; | |
912b54ad | 53 | |
fb69d709 LP |
54 | sd_resolve_query *resolve_query; |
55 | } Connection; | |
912b54ad | 56 | |
8569a776 LP |
57 | static void connection_free(Connection *c) { |
58 | assert(c); | |
32d3c809 | 59 | |
e633ea1c LP |
60 | if (c->context) |
61 | set_remove(c->context->connections, c); | |
62 | ||
8569a776 LP |
63 | sd_event_source_unref(c->server_event_source); |
64 | sd_event_source_unref(c->client_event_source); | |
32d3c809 | 65 | |
03e334a1 LP |
66 | safe_close(c->server_fd); |
67 | safe_close(c->client_fd); | |
32d3c809 | 68 | |
3d94f76c LP |
69 | safe_close_pair(c->server_to_client_buffer); |
70 | safe_close_pair(c->client_to_server_buffer); | |
32d3c809 | 71 | |
fb69d709 LP |
72 | sd_resolve_query_unref(c->resolve_query); |
73 | ||
8569a776 LP |
74 | free(c); |
75 | } | |
32d3c809 | 76 | |
8569a776 | 77 | static void context_free(Context *context) { |
8569a776 LP |
78 | assert(context); |
79 | ||
224b0e7a ZJS |
80 | set_free_with_destructor(context->listen, sd_event_source_unref); |
81 | set_free_with_destructor(context->connections, connection_free); | |
32d3c809 | 82 | |
fb69d709 LP |
83 | sd_event_unref(context->event); |
84 | sd_resolve_unref(context->resolve); | |
8569a776 | 85 | } |
912b54ad | 86 | |
8569a776 LP |
87 | static int connection_create_pipes(Connection *c, int buffer[2], size_t *sz) { |
88 | int r; | |
912b54ad | 89 | |
8569a776 LP |
90 | assert(c); |
91 | assert(buffer); | |
92 | assert(sz); | |
912b54ad | 93 | |
8569a776 LP |
94 | if (buffer[0] >= 0) |
95 | return 0; | |
912b54ad | 96 | |
8569a776 | 97 | r = pipe2(buffer, O_CLOEXEC|O_NONBLOCK); |
4a62c710 MS |
98 | if (r < 0) |
99 | return log_error_errno(errno, "Failed to allocate pipe buffer: %m"); | |
912b54ad | 100 | |
25dbe4f5 | 101 | (void) fcntl(buffer[0], F_SETPIPE_SZ, BUFFER_SIZE); |
8569a776 LP |
102 | |
103 | r = fcntl(buffer[0], F_GETPIPE_SZ); | |
4a62c710 MS |
104 | if (r < 0) |
105 | return log_error_errno(errno, "Failed to get pipe buffer size: %m"); | |
912b54ad | 106 | |
8569a776 LP |
107 | assert(r > 0); |
108 | *sz = r; | |
109 | ||
912b54ad DS |
110 | return 0; |
111 | } | |
112 | ||
8569a776 LP |
113 | static int connection_shovel( |
114 | Connection *c, | |
115 | int *from, int buffer[2], int *to, | |
116 | size_t *full, size_t *sz, | |
117 | sd_event_source **from_source, sd_event_source **to_source) { | |
118 | ||
119 | bool shoveled; | |
120 | ||
121 | assert(c); | |
122 | assert(from); | |
123 | assert(buffer); | |
124 | assert(buffer[0] >= 0); | |
125 | assert(buffer[1] >= 0); | |
126 | assert(to); | |
127 | assert(full); | |
128 | assert(sz); | |
129 | assert(from_source); | |
130 | assert(to_source); | |
131 | ||
132 | do { | |
133 | ssize_t z; | |
134 | ||
135 | shoveled = false; | |
136 | ||
137 | if (*full < *sz && *from >= 0 && *to >= 0) { | |
138 | z = splice(*from, NULL, buffer[1], NULL, *sz - *full, SPLICE_F_MOVE|SPLICE_F_NONBLOCK); | |
139 | if (z > 0) { | |
140 | *full += z; | |
141 | shoveled = true; | |
4c701096 | 142 | } else if (z == 0 || IN_SET(errno, EPIPE, ECONNRESET)) { |
8569a776 | 143 | *from_source = sd_event_source_unref(*from_source); |
03e334a1 | 144 | *from = safe_close(*from); |
ec2ce0c5 | 145 | } else if (!IN_SET(errno, EAGAIN, EINTR)) |
4a62c710 | 146 | return log_error_errno(errno, "Failed to splice: %m"); |
912b54ad DS |
147 | } |
148 | ||
8569a776 LP |
149 | if (*full > 0 && *to >= 0) { |
150 | z = splice(buffer[0], NULL, *to, NULL, *full, SPLICE_F_MOVE|SPLICE_F_NONBLOCK); | |
151 | if (z > 0) { | |
152 | *full -= z; | |
153 | shoveled = true; | |
4c701096 | 154 | } else if (z == 0 || IN_SET(errno, EPIPE, ECONNRESET)) { |
8569a776 | 155 | *to_source = sd_event_source_unref(*to_source); |
03e334a1 | 156 | *to = safe_close(*to); |
ec2ce0c5 | 157 | } else if (!IN_SET(errno, EAGAIN, EINTR)) |
4a62c710 | 158 | return log_error_errno(errno, "Failed to splice: %m"); |
8569a776 LP |
159 | } |
160 | } while (shoveled); | |
912b54ad | 161 | |
8569a776 | 162 | return 0; |
912b54ad DS |
163 | } |
164 | ||
fb69d709 | 165 | static int connection_enable_event_sources(Connection *c); |
8569a776 LP |
166 | |
167 | static int traffic_cb(sd_event_source *s, int fd, uint32_t revents, void *userdata) { | |
168 | Connection *c = userdata; | |
912b54ad DS |
169 | int r; |
170 | ||
8569a776 LP |
171 | assert(s); |
172 | assert(fd >= 0); | |
173 | assert(c); | |
912b54ad | 174 | |
8569a776 LP |
175 | r = connection_shovel(c, |
176 | &c->server_fd, c->server_to_client_buffer, &c->client_fd, | |
177 | &c->server_to_client_buffer_full, &c->server_to_client_buffer_size, | |
178 | &c->server_event_source, &c->client_event_source); | |
179 | if (r < 0) | |
180 | goto quit; | |
912b54ad | 181 | |
8569a776 LP |
182 | r = connection_shovel(c, |
183 | &c->client_fd, c->client_to_server_buffer, &c->server_fd, | |
184 | &c->client_to_server_buffer_full, &c->client_to_server_buffer_size, | |
185 | &c->client_event_source, &c->server_event_source); | |
186 | if (r < 0) | |
187 | goto quit; | |
912b54ad | 188 | |
8569a776 LP |
189 | /* EOF on both sides? */ |
190 | if (c->server_fd == -1 && c->client_fd == -1) | |
191 | goto quit; | |
912b54ad | 192 | |
8569a776 LP |
193 | /* Server closed, and all data written to client? */ |
194 | if (c->server_fd == -1 && c->server_to_client_buffer_full <= 0) | |
195 | goto quit; | |
912b54ad | 196 | |
8569a776 LP |
197 | /* Client closed, and all data written to server? */ |
198 | if (c->client_fd == -1 && c->client_to_server_buffer_full <= 0) | |
199 | goto quit; | |
912b54ad | 200 | |
fb69d709 | 201 | r = connection_enable_event_sources(c); |
8569a776 LP |
202 | if (r < 0) |
203 | goto quit; | |
912b54ad | 204 | |
8569a776 | 205 | return 1; |
912b54ad | 206 | |
8569a776 LP |
207 | quit: |
208 | connection_free(c); | |
209 | return 0; /* ignore errors, continue serving */ | |
912b54ad DS |
210 | } |
211 | ||
fb69d709 | 212 | static int connection_enable_event_sources(Connection *c) { |
8569a776 LP |
213 | uint32_t a = 0, b = 0; |
214 | int r; | |
912b54ad | 215 | |
8569a776 | 216 | assert(c); |
912b54ad | 217 | |
8569a776 LP |
218 | if (c->server_to_client_buffer_full > 0) |
219 | b |= EPOLLOUT; | |
220 | if (c->server_to_client_buffer_full < c->server_to_client_buffer_size) | |
221 | a |= EPOLLIN; | |
912b54ad | 222 | |
8569a776 LP |
223 | if (c->client_to_server_buffer_full > 0) |
224 | a |= EPOLLOUT; | |
225 | if (c->client_to_server_buffer_full < c->client_to_server_buffer_size) | |
226 | b |= EPOLLIN; | |
912b54ad | 227 | |
8569a776 LP |
228 | if (c->server_event_source) |
229 | r = sd_event_source_set_io_events(c->server_event_source, a); | |
230 | else if (c->server_fd >= 0) | |
fb69d709 | 231 | r = sd_event_add_io(c->context->event, &c->server_event_source, c->server_fd, a, traffic_cb, c); |
8569a776 LP |
232 | else |
233 | r = 0; | |
234 | ||
f647962d MS |
235 | if (r < 0) |
236 | return log_error_errno(r, "Failed to set up server event source: %m"); | |
912b54ad | 237 | |
8569a776 LP |
238 | if (c->client_event_source) |
239 | r = sd_event_source_set_io_events(c->client_event_source, b); | |
240 | else if (c->client_fd >= 0) | |
fb69d709 | 241 | r = sd_event_add_io(c->context->event, &c->client_event_source, c->client_fd, b, traffic_cb, c); |
8569a776 LP |
242 | else |
243 | r = 0; | |
912b54ad | 244 | |
f647962d MS |
245 | if (r < 0) |
246 | return log_error_errno(r, "Failed to set up client event source: %m"); | |
912b54ad | 247 | |
8569a776 | 248 | return 0; |
912b54ad DS |
249 | } |
250 | ||
fb69d709 LP |
251 | static int connection_complete(Connection *c) { |
252 | int r; | |
253 | ||
254 | assert(c); | |
255 | ||
256 | r = connection_create_pipes(c, c->server_to_client_buffer, &c->server_to_client_buffer_size); | |
257 | if (r < 0) | |
258 | goto fail; | |
259 | ||
260 | r = connection_create_pipes(c, c->client_to_server_buffer, &c->client_to_server_buffer_size); | |
261 | if (r < 0) | |
262 | goto fail; | |
263 | ||
264 | r = connection_enable_event_sources(c); | |
265 | if (r < 0) | |
266 | goto fail; | |
267 | ||
268 | return 0; | |
269 | ||
270 | fail: | |
271 | connection_free(c); | |
272 | return 0; /* ignore errors, continue serving */ | |
273 | } | |
274 | ||
8569a776 LP |
275 | static int connect_cb(sd_event_source *s, int fd, uint32_t revents, void *userdata) { |
276 | Connection *c = userdata; | |
277 | socklen_t solen; | |
278 | int error, r; | |
279 | ||
280 | assert(s); | |
281 | assert(fd >= 0); | |
282 | assert(c); | |
283 | ||
284 | solen = sizeof(error); | |
285 | r = getsockopt(fd, SOL_SOCKET, SO_ERROR, &error, &solen); | |
286 | if (r < 0) { | |
56f64d95 | 287 | log_error_errno(errno, "Failed to issue SO_ERROR: %m"); |
40976028 DS |
288 | goto fail; |
289 | } | |
912b54ad | 290 | |
8569a776 | 291 | if (error != 0) { |
279d3c9c | 292 | log_error_errno(error, "Failed to connect to remote host: %m"); |
912b54ad DS |
293 | goto fail; |
294 | } | |
295 | ||
8569a776 LP |
296 | c->client_event_source = sd_event_source_unref(c->client_event_source); |
297 | ||
fb69d709 | 298 | return connection_complete(c); |
912b54ad | 299 | |
fb69d709 LP |
300 | fail: |
301 | connection_free(c); | |
302 | return 0; /* ignore errors, continue serving */ | |
303 | } | |
912b54ad | 304 | |
fb69d709 LP |
305 | static int connection_start(Connection *c, struct sockaddr *sa, socklen_t salen) { |
306 | int r; | |
307 | ||
308 | assert(c); | |
309 | assert(sa); | |
310 | assert(salen); | |
311 | ||
312 | c->client_fd = socket(sa->sa_family, SOCK_STREAM|SOCK_NONBLOCK|SOCK_CLOEXEC, 0); | |
313 | if (c->client_fd < 0) { | |
56f64d95 | 314 | log_error_errno(errno, "Failed to get remote socket: %m"); |
8569a776 | 315 | goto fail; |
fb69d709 LP |
316 | } |
317 | ||
318 | r = connect(c->client_fd, sa, salen); | |
319 | if (r < 0) { | |
320 | if (errno == EINPROGRESS) { | |
321 | r = sd_event_add_io(c->context->event, &c->client_event_source, c->client_fd, EPOLLOUT, connect_cb, c); | |
322 | if (r < 0) { | |
da927ba9 | 323 | log_error_errno(r, "Failed to add connection socket: %m"); |
fb69d709 LP |
324 | goto fail; |
325 | } | |
326 | ||
327 | r = sd_event_source_set_enabled(c->client_event_source, SD_EVENT_ONESHOT); | |
328 | if (r < 0) { | |
da927ba9 | 329 | log_error_errno(r, "Failed to enable oneshot event source: %m"); |
fb69d709 LP |
330 | goto fail; |
331 | } | |
332 | } else { | |
56f64d95 | 333 | log_error_errno(errno, "Failed to connect to remote host: %m"); |
fb69d709 LP |
334 | goto fail; |
335 | } | |
336 | } else { | |
337 | r = connection_complete(c); | |
338 | if (r < 0) | |
339 | goto fail; | |
340 | } | |
912b54ad | 341 | |
8569a776 | 342 | return 0; |
912b54ad | 343 | |
8569a776 LP |
344 | fail: |
345 | connection_free(c); | |
346 | return 0; /* ignore errors, continue serving */ | |
347 | } | |
348 | ||
fb69d709 LP |
349 | static int resolve_cb(sd_resolve_query *q, int ret, const struct addrinfo *ai, void *userdata) { |
350 | Connection *c = userdata; | |
351 | ||
352 | assert(q); | |
353 | assert(c); | |
354 | ||
355 | if (ret != 0) { | |
356 | log_error("Failed to resolve host: %s", gai_strerror(ret)); | |
357 | goto fail; | |
358 | } | |
359 | ||
360 | c->resolve_query = sd_resolve_query_unref(c->resolve_query); | |
361 | ||
362 | return connection_start(c, ai->ai_addr, ai->ai_addrlen); | |
363 | ||
364 | fail: | |
365 | connection_free(c); | |
366 | return 0; /* ignore errors, continue serving */ | |
367 | } | |
368 | ||
369 | static int resolve_remote(Connection *c) { | |
370 | ||
371 | static const struct addrinfo hints = { | |
372 | .ai_family = AF_UNSPEC, | |
373 | .ai_socktype = SOCK_STREAM, | |
374 | .ai_flags = AI_ADDRCONFIG | |
375 | }; | |
376 | ||
1ec6af16 | 377 | union sockaddr_union sa = {}; |
fb69d709 | 378 | const char *node, *service; |
fb69d709 LP |
379 | int r; |
380 | ||
15a3e96f LP |
381 | if (IN_SET(arg_remote_host[0], '/', '@')) { |
382 | int salen; | |
383 | ||
384 | salen = sockaddr_un_set_path(&sa.un, arg_remote_host); | |
385 | if (salen < 0) { | |
386 | log_error_errno(salen, "Specified address doesn't fit in an AF_UNIX address, refusing: %m"); | |
387 | goto fail; | |
388 | } | |
fb69d709 | 389 | |
15a3e96f | 390 | return connection_start(c, &sa.sa, salen); |
fb69d709 LP |
391 | } |
392 | ||
393 | service = strrchr(arg_remote_host, ':'); | |
394 | if (service) { | |
395 | node = strndupa(arg_remote_host, service - arg_remote_host); | |
313cefa1 | 396 | service++; |
fb69d709 LP |
397 | } else { |
398 | node = arg_remote_host; | |
399 | service = "80"; | |
400 | } | |
401 | ||
402 | log_debug("Looking up address info for %s:%s", node, service); | |
403 | r = sd_resolve_getaddrinfo(c->context->resolve, &c->resolve_query, node, service, &hints, resolve_cb, c); | |
404 | if (r < 0) { | |
da927ba9 | 405 | log_error_errno(r, "Failed to resolve remote host: %m"); |
fb69d709 LP |
406 | goto fail; |
407 | } | |
408 | ||
409 | return 0; | |
410 | ||
411 | fail: | |
412 | connection_free(c); | |
413 | return 0; /* ignore errors, continue serving */ | |
414 | } | |
415 | ||
416 | static int add_connection_socket(Context *context, int fd) { | |
8569a776 LP |
417 | Connection *c; |
418 | int r; | |
419 | ||
420 | assert(context); | |
8569a776 LP |
421 | assert(fd >= 0); |
422 | ||
dc3b8afb | 423 | if (set_size(context->connections) > arg_connections_max) { |
8569a776 | 424 | log_warning("Hit connection limit, refusing connection."); |
03e334a1 | 425 | safe_close(fd); |
8569a776 | 426 | return 0; |
912b54ad DS |
427 | } |
428 | ||
d5099efc | 429 | r = set_ensure_allocated(&context->connections, NULL); |
fb69d709 LP |
430 | if (r < 0) { |
431 | log_oom(); | |
432 | return 0; | |
433 | } | |
912b54ad | 434 | |
8569a776 | 435 | c = new0(Connection, 1); |
fb69d709 LP |
436 | if (!c) { |
437 | log_oom(); | |
438 | return 0; | |
439 | } | |
8569a776 | 440 | |
e633ea1c | 441 | c->context = context; |
8569a776 LP |
442 | c->server_fd = fd; |
443 | c->client_fd = -1; | |
444 | c->server_to_client_buffer[0] = c->server_to_client_buffer[1] = -1; | |
445 | c->client_to_server_buffer[0] = c->client_to_server_buffer[1] = -1; | |
446 | ||
e633ea1c LP |
447 | r = set_put(context->connections, c); |
448 | if (r < 0) { | |
449 | free(c); | |
fb69d709 LP |
450 | log_oom(); |
451 | return 0; | |
8569a776 | 452 | } |
912b54ad | 453 | |
fb69d709 | 454 | return resolve_remote(c); |
40976028 DS |
455 | } |
456 | ||
457 | static int accept_cb(sd_event_source *s, int fd, uint32_t revents, void *userdata) { | |
7b77ed8c | 458 | _cleanup_free_ char *peer = NULL; |
8569a776 LP |
459 | Context *context = userdata; |
460 | int nfd = -1, r; | |
40976028 | 461 | |
8569a776 LP |
462 | assert(s); |
463 | assert(fd >= 0); | |
40976028 | 464 | assert(revents & EPOLLIN); |
8569a776 LP |
465 | assert(context); |
466 | ||
467 | nfd = accept4(fd, NULL, NULL, SOCK_NONBLOCK|SOCK_CLOEXEC); | |
7b77ed8c LP |
468 | if (nfd < 0) { |
469 | if (errno != -EAGAIN) | |
56f64d95 | 470 | log_warning_errno(errno, "Failed to accept() socket: %m"); |
7b77ed8c | 471 | } else { |
366b7db4 | 472 | getpeername_pretty(nfd, true, &peer); |
8569a776 | 473 | log_debug("New connection from %s", strna(peer)); |
40976028 | 474 | |
fb69d709 | 475 | r = add_connection_socket(context, nfd); |
f4bd42aa | 476 | if (r < 0) { |
da927ba9 | 477 | log_error_errno(r, "Failed to accept connection, ignoring: %m"); |
03e334a1 | 478 | safe_close(fd); |
f4bd42aa | 479 | } |
7b77ed8c | 480 | } |
8569a776 | 481 | |
40976028 DS |
482 | r = sd_event_source_set_enabled(s, SD_EVENT_ONESHOT); |
483 | if (r < 0) { | |
da927ba9 | 484 | log_error_errno(r, "Error while re-enabling listener with ONESHOT: %m"); |
fb69d709 | 485 | sd_event_exit(context->event, r); |
40976028 DS |
486 | return r; |
487 | } | |
488 | ||
96c374d0 | 489 | return 1; |
912b54ad DS |
490 | } |
491 | ||
fb69d709 | 492 | static int add_listen_socket(Context *context, int fd) { |
8569a776 LP |
493 | sd_event_source *source; |
494 | int r; | |
495 | ||
496 | assert(context); | |
8569a776 | 497 | assert(fd >= 0); |
912b54ad | 498 | |
d5099efc | 499 | r = set_ensure_allocated(&context->listen, NULL); |
96c374d0 | 500 | if (r < 0) { |
8569a776 LP |
501 | log_oom(); |
502 | return r; | |
503 | } | |
504 | ||
505 | r = sd_is_socket(fd, 0, SOCK_STREAM, 1); | |
f647962d MS |
506 | if (r < 0) |
507 | return log_error_errno(r, "Failed to determine socket type: %m"); | |
8569a776 LP |
508 | if (r == 0) { |
509 | log_error("Passed in socket is not a stream socket."); | |
510 | return -EINVAL; | |
511 | } | |
912b54ad | 512 | |
8569a776 | 513 | r = fd_nonblock(fd, true); |
f647962d MS |
514 | if (r < 0) |
515 | return log_error_errno(r, "Failed to mark file descriptor non-blocking: %m"); | |
912b54ad | 516 | |
fb69d709 | 517 | r = sd_event_add_io(context->event, &source, fd, EPOLLIN, accept_cb, context); |
f647962d MS |
518 | if (r < 0) |
519 | return log_error_errno(r, "Failed to add event source: %m"); | |
912b54ad | 520 | |
8569a776 | 521 | r = set_put(context->listen, source); |
96c374d0 | 522 | if (r < 0) { |
da927ba9 | 523 | log_error_errno(r, "Failed to add source to set: %m"); |
8569a776 | 524 | sd_event_source_unref(source); |
40976028 DS |
525 | return r; |
526 | } | |
527 | ||
528 | /* Set the watcher to oneshot in case other processes are also | |
529 | * watching to accept(). */ | |
8569a776 | 530 | r = sd_event_source_set_enabled(source, SD_EVENT_ONESHOT); |
f647962d MS |
531 | if (r < 0) |
532 | return log_error_errno(r, "Failed to enable oneshot mode: %m"); | |
912b54ad | 533 | |
8569a776 | 534 | return 0; |
912b54ad DS |
535 | } |
536 | ||
37ec0fdd LP |
537 | static int help(void) { |
538 | _cleanup_free_ char *link = NULL; | |
539 | int r; | |
540 | ||
541 | r = terminal_urlify_man("systemd-socket-proxyd", "8", &link); | |
542 | if (r < 0) | |
543 | return log_oom(); | |
544 | ||
601185b4 ZJS |
545 | printf("%1$s [HOST:PORT]\n" |
546 | "%1$s [SOCKET]\n\n" | |
8569a776 | 547 | "Bidirectionally proxy local sockets to another (possibly remote) socket.\n\n" |
23d0fff7 | 548 | " -c --connections-max= Set the maximum number of connections to be accepted\n" |
8cf030b3 | 549 | " -h --help Show this help\n" |
37ec0fdd LP |
550 | " --version Show package version\n" |
551 | "\nSee the %2$s for details.\n" | |
552 | , program_invocation_short_name | |
553 | , link | |
554 | ); | |
555 | ||
556 | return 0; | |
912b54ad DS |
557 | } |
558 | ||
8569a776 | 559 | static int parse_argv(int argc, char *argv[]) { |
912b54ad DS |
560 | |
561 | enum { | |
8cf030b3 LP |
562 | ARG_VERSION = 0x100, |
563 | ARG_IGNORE_ENV | |
912b54ad DS |
564 | }; |
565 | ||
566 | static const struct option options[] = { | |
dc3b8afb DK |
567 | { "connections-max", required_argument, NULL, 'c' }, |
568 | { "help", no_argument, NULL, 'h' }, | |
569 | { "version", no_argument, NULL, ARG_VERSION }, | |
eb9da376 | 570 | {} |
912b54ad DS |
571 | }; |
572 | ||
dc3b8afb | 573 | int c, r; |
912b54ad DS |
574 | |
575 | assert(argc >= 0); | |
576 | assert(argv); | |
577 | ||
dc3b8afb | 578 | while ((c = getopt_long(argc, argv, "c:h", options, NULL)) >= 0) |
912b54ad DS |
579 | |
580 | switch (c) { | |
581 | ||
582 | case 'h': | |
37ec0fdd LP |
583 | return help(); |
584 | ||
585 | case ARG_VERSION: | |
586 | return version(); | |
912b54ad | 587 | |
dc3b8afb DK |
588 | case 'c': |
589 | r = safe_atou(optarg, &arg_connections_max); | |
590 | if (r < 0) { | |
591 | log_error("Failed to parse --connections-max= argument: %s", optarg); | |
592 | return r; | |
593 | } | |
594 | ||
595 | if (arg_connections_max < 1) { | |
596 | log_error("Connection limit is too low."); | |
597 | return -EINVAL; | |
598 | } | |
599 | ||
600 | break; | |
601 | ||
eb9da376 | 602 | case '?': |
912b54ad | 603 | return -EINVAL; |
eb9da376 LP |
604 | |
605 | default: | |
606 | assert_not_reached("Unhandled option"); | |
912b54ad | 607 | } |
912b54ad | 608 | |
8569a776 LP |
609 | if (optind >= argc) { |
610 | log_error("Not enough parameters."); | |
912b54ad DS |
611 | return -EINVAL; |
612 | } | |
613 | ||
8569a776 LP |
614 | if (argc != optind+1) { |
615 | log_error("Too many parameters."); | |
912b54ad DS |
616 | return -EINVAL; |
617 | } | |
618 | ||
8569a776 | 619 | arg_remote_host = argv[optind]; |
912b54ad DS |
620 | return 1; |
621 | } | |
622 | ||
623 | int main(int argc, char *argv[]) { | |
8569a776 LP |
624 | Context context = {}; |
625 | int r, n, fd; | |
912b54ad DS |
626 | |
627 | log_parse_environment(); | |
628 | log_open(); | |
629 | ||
8569a776 | 630 | r = parse_argv(argc, argv); |
912b54ad DS |
631 | if (r <= 0) |
632 | goto finish; | |
633 | ||
fb69d709 | 634 | r = sd_event_default(&context.event); |
8569a776 | 635 | if (r < 0) { |
da927ba9 | 636 | log_error_errno(r, "Failed to allocate event loop: %m"); |
8569a776 LP |
637 | goto finish; |
638 | } | |
912b54ad | 639 | |
fb69d709 LP |
640 | r = sd_resolve_default(&context.resolve); |
641 | if (r < 0) { | |
da927ba9 | 642 | log_error_errno(r, "Failed to allocate resolver: %m"); |
fb69d709 LP |
643 | goto finish; |
644 | } | |
645 | ||
646 | r = sd_resolve_attach_event(context.resolve, context.event, 0); | |
647 | if (r < 0) { | |
da927ba9 | 648 | log_error_errno(r, "Failed to attach resolver: %m"); |
fb69d709 LP |
649 | goto finish; |
650 | } | |
651 | ||
652 | sd_event_set_watchdog(context.event, true); | |
cde93897 | 653 | |
8cf030b3 LP |
654 | n = sd_listen_fds(1); |
655 | if (n < 0) { | |
656 | log_error("Failed to receive sockets from parent."); | |
657 | r = n; | |
658 | goto finish; | |
659 | } else if (n == 0) { | |
660 | log_error("Didn't get any sockets passed in."); | |
661 | r = -EINVAL; | |
662 | goto finish; | |
663 | } | |
664 | ||
665 | for (fd = SD_LISTEN_FDS_START; fd < SD_LISTEN_FDS_START + n; fd++) { | |
fb69d709 | 666 | r = add_listen_socket(&context, fd); |
8569a776 | 667 | if (r < 0) |
96c374d0 | 668 | goto finish; |
912b54ad DS |
669 | } |
670 | ||
fb69d709 | 671 | r = sd_event_loop(context.event); |
912b54ad | 672 | if (r < 0) { |
da927ba9 | 673 | log_error_errno(r, "Failed to run event loop: %m"); |
912b54ad DS |
674 | goto finish; |
675 | } | |
676 | ||
912b54ad | 677 | finish: |
8569a776 LP |
678 | context_free(&context); |
679 | ||
912b54ad DS |
680 | return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS; |
681 | } |