From 1e3f94aba1c47b689546ae8ac2e4ba6b1f1774c2 Mon Sep 17 00:00:00 2001 From: Laurent Bercot Date: Wed, 11 Apr 2018 15:09:44 +0000 Subject: Add -B (blocking) option to s6-tcpserver?-socketbinder --- src/conn-tools/s6-tcpserver4-socketbinder.c | 9 ++++++--- src/conn-tools/s6-tcpserver6-socketbinder.c | 9 ++++++--- 2 files changed, 12 insertions(+), 6 deletions(-) (limited to 'src') diff --git a/src/conn-tools/s6-tcpserver4-socketbinder.c b/src/conn-tools/s6-tcpserver4-socketbinder.c index e32eb4f..bcd9ee1 100644 --- a/src/conn-tools/s6-tcpserver4-socketbinder.c +++ b/src/conn-tools/s6-tcpserver4-socketbinder.c @@ -10,7 +10,7 @@ #include #include -#define USAGE "s6-tcpserver4-socketbinder [ -d | -D ] [ -b backlog ] [ -M | -m ] ip4 port prog..." +#define USAGE "s6-tcpserver4-socketbinder [ -d | -D ] [ -b backlog ] [ -M | -m ] [ -B ] ip4 port prog..." #define dieusage() strerr_dieusage(100, USAGE) int main (int argc, char const *const *argv, char const *const *envp) @@ -18,6 +18,7 @@ int main (int argc, char const *const *argv, char const *const *envp) unsigned int backlog = SOMAXCONN ; int flagreuse = 1 ; int flagudp = 0 ; + int flagblocking = 0 ; char ip[4] ; uint16_t port ; PROG = "s6-tcpserver4-socketbinder" ; @@ -25,7 +26,7 @@ int main (int argc, char const *const *argv, char const *const *envp) subgetopt_t l = SUBGETOPT_ZERO ; for (;;) { - int opt = subgetopt_r(argc, argv, "DdMmb:", &l) ; + int opt = subgetopt_r(argc, argv, "DdMmBb:", &l) ; if (opt == -1) break ; switch (opt) { @@ -33,6 +34,7 @@ int main (int argc, char const *const *argv, char const *const *envp) case 'd' : flagreuse = 1 ; break ; case 'M' : flagudp = 0 ; break ; case 'm' : flagudp = 1 ; break ; + case 'B' : flagblocking = 1 ; break ; case 'b' : if (!uint0_scan(l.arg, &backlog)) dieusage() ; break ; default : dieusage() ; } @@ -42,7 +44,8 @@ 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 (flagudp ? socket_udp4() : socket_tcp4()) strerr_diefu1sys(111, "create socket") ; + if (flagudp ? socket_udp4_internal(flagblocking ? 0 : DJBUNIX_FLAG_NB) : socket_tcp4_internal(flagblocking ? 0 : DJBUNIX_FLAG_NB)) + 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 (backlog && socket_listen(0, backlog) < 0) diff --git a/src/conn-tools/s6-tcpserver6-socketbinder.c b/src/conn-tools/s6-tcpserver6-socketbinder.c index 143bfe9..1c4835b 100644 --- a/src/conn-tools/s6-tcpserver6-socketbinder.c +++ b/src/conn-tools/s6-tcpserver6-socketbinder.c @@ -10,7 +10,7 @@ #include #include -#define USAGE "s6-tcpserver6-socketbinder [ -d | -D ] [ -b backlog ] [ -M | -m ] ip6 port prog..." +#define USAGE "s6-tcpserver6-socketbinder [ -d | -D ] [ -b backlog ] [ -M | -m ] [ -B ] ip6 port prog..." #define dieusage() strerr_dieusage(100, USAGE) int main (int argc, char const *const *argv, char const *const *envp) @@ -18,6 +18,7 @@ int main (int argc, char const *const *argv, char const *const *envp) unsigned int backlog = SOMAXCONN ; int flagreuse = 1 ; int flagudp = 0 ; + int flagblocking = 0 ; char ip[16] ; uint16_t port ; PROG = "s6-tcpserver6-socketbinder" ; @@ -25,7 +26,7 @@ int main (int argc, char const *const *argv, char const *const *envp) subgetopt_t l = SUBGETOPT_ZERO ; for (;;) { - int opt = subgetopt_r(argc, argv, "DdMmb:", &l) ; + int opt = subgetopt_r(argc, argv, "DdMmBb:", &l) ; if (opt == -1) break ; switch (opt) { @@ -33,6 +34,7 @@ int main (int argc, char const *const *argv, char const *const *envp) case 'd' : flagreuse = 1 ; break ; case 'M' : flagudp = 0 ; break ; case 'm' : flagudp = 1 ; break ; + case 'B' : flagblocking = 1 ; break ; case 'b' : if (!uint0_scan(l.arg, &backlog)) dieusage() ; break ; default : dieusage() ; } @@ -42,7 +44,8 @@ int main (int argc, char const *const *argv, char const *const *envp) if (argc < 3) dieusage() ; if (!ip6_scan(argv[0], ip) || !uint160_scan(argv[1], &port)) dieusage() ; close(0) ; - if (flagudp ? socket_udp6() : socket_tcp6()) strerr_diefu1sys(111, "create socket") ; + if (flagudp ? socket_udp6_internal(flagblocking ? 0 : DJBUNIX_FLAG_NB) : socket_tcp6_internal(flagblocking ? 0 : DJBUNIX_FLAG_NB)) + strerr_diefu1sys(111, "create socket") ; if ((flagreuse ? socket_bind6_reuse(0, ip, port) : socket_bind6(0, ip, port)) < 0) strerr_diefu5sys(111, "bind to ", argv[0], ":", argv[1], " ") ; if (backlog && socket_listen(0, backlog) < 0) -- cgit v1.2.3