diff options
author | Laurent Bercot <ska-skaware@skarnet.org> | 2016-09-10 15:01:14 +0000 |
---|---|---|
committer | Laurent Bercot <ska-skaware@skarnet.org> | 2016-09-10 15:01:14 +0000 |
commit | c4fe75fd62ad8b53e98472874c07182f74832ecb (patch) | |
tree | 97edcae6128b12d3053a25b5b858bbfdde7bc757 /src/conn-tools/s6-tcpserver4-socketbinder.c | |
parent | 5d417a186a73712b22faae4d1b9654c095342aaa (diff) | |
download | s6-networking-c4fe75fd62ad8b53e98472874c07182f74832ecb.tar.xz |
Add options to s6-tcpserver4-socketbinder and s6-tcpserver6-socketbinder to create UDP sockets; also to not listen(). (Suggested by Daniel Kahn Gillmor.)
Diffstat (limited to 'src/conn-tools/s6-tcpserver4-socketbinder.c')
-rw-r--r-- | src/conn-tools/s6-tcpserver4-socketbinder.c | 15 |
1 files changed, 10 insertions, 5 deletions
diff --git a/src/conn-tools/s6-tcpserver4-socketbinder.c b/src/conn-tools/s6-tcpserver4-socketbinder.c index 86f5390..5550820 100644 --- a/src/conn-tools/s6-tcpserver4-socketbinder.c +++ b/src/conn-tools/s6-tcpserver4-socketbinder.c @@ -1,6 +1,8 @@ /* ISC license. */ +#include <sys/types.h> #include <unistd.h> +#include <sys/socket.h> #include <skalibs/uint16.h> #include <skalibs/uint.h> #include <skalibs/sgetopt.h> @@ -9,13 +11,14 @@ #include <skalibs/djbunix.h> #include <skalibs/socket.h> -#define USAGE "s6-tcpserver4-socketbinder [ -d | -D ] [ -b backlog ] ip4 port prog..." +#define USAGE "s6-tcpserver4-socketbinder [ -d | -D ] [ -b backlog ] [ -M | -m ] ip4 port prog..." #define dieusage() strerr_dieusage(100, USAGE) int main (int argc, char const *const *argv, char const *const *envp) { - unsigned int backlog = 20 ; + unsigned int backlog = SOMAXCONN ; int flagreuse = 1 ; + int flagudp = 0 ; char ip[4] ; uint16 port ; PROG = "s6-tcpserver4-socketbinder" ; @@ -23,12 +26,14 @@ int main (int argc, char const *const *argv, char const *const *envp) subgetopt_t l = SUBGETOPT_ZERO ; for (;;) { - register int opt = subgetopt_r(argc, argv, "Ddb:", &l) ; + register int opt = subgetopt_r(argc, argv, "DdMmb:", &l) ; if (opt == -1) break ; switch (opt) { case 'D' : flagreuse = 0 ; break ; case 'd' : flagreuse = 1 ; break ; + case 'M' : flagudp = 0 ; break ; + case 'm' : flagudp = 1 ; break ; case 'b' : if (!uint0_scan(l.arg, &backlog)) dieusage() ; break ; default : dieusage() ; } @@ -38,10 +43,10 @@ int main (int argc, char const *const *argv, char const *const *envp) if (argc < 3) dieusage() ; if (!ip4_scan(argv[0], ip) || !uint160_scan(argv[1], &port)) dieusage() ; close(0) ; - if (socket_tcp4()) strerr_diefu1sys(111, "create socket") ; + if (flagudp ? socket_udp4() : socket_tcp4()) strerr_diefu1sys(111, "create socket") ; if ((flagreuse ? socket_bind4_reuse(0, ip, port) : socket_bind4(0, ip, port)) < 0) strerr_diefu5sys(111, "bind to ", argv[0], ":", argv[1], " ") ; - if (socket_listen(0, backlog) < 0) + if (backlog && socket_listen(0, backlog) < 0) strerr_diefu5sys(111, "listen to ", argv[0], ":", argv[1], " ") ; pathexec_run(argv[2], argv + 2, envp) ; |