Skip to content

Commit

Permalink
Add -B (blocking) option to s6-tcpserver?-socketbinder
Browse files Browse the repository at this point in the history
  • Loading branch information
skarnet committed Apr 11, 2018
1 parent dfaa5da commit 1e3f94a
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 13 deletions.
33 changes: 30 additions & 3 deletions .gitignore
@@ -1,5 +1,32 @@
/config.mak
/src/include/s6-networking/config.h
*.o
*.a
*.lo
*.so
*.so.*
/libs6net.a.xyzzy
/libs6net.so.xyzzy
/libsbearssl.a.xyzzy
/libsbearssl.so.xyzzy
/libstls.a.xyzzy
/libstls.so.xyzzy
/minidentd
/s6-clockadd
/s6-clockview
/s6-getservbyname
/s6-ident-client
/s6-sntpclock
/s6-taiclock
/s6-taiclockd
/s6-tcpclient
/s6-tcpserver
/s6-tcpserver-access
/s6-tcpserver4
/s6-tcpserver4-socketbinder
/s6-tcpserver4d
/s6-tcpserver6
/s6-tcpserver6-socketbinder
/s6-tcpserver6d
/s6-tlsc
/s6-tlsclient
/s6-tlsd
/s6-tlsserver
/src/minidentd/mgetuid.c
5 changes: 3 additions & 2 deletions doc/s6-tcpserver4-socketbinder.html
Expand Up @@ -26,7 +26,7 @@ <h1> The <tt>s6-tcpserver4-socketbinder</tt> program </h1>
<h2> Interface </h2>

<pre>
s6-tcpserver4-socketbinder [ -d | -D ] [ -b <em>backlog</em> ] [ -M | -m ] <em>ip</em> <em>port</em> <em>prog...</em>
s6-tcpserver4-socketbinder [ -d | -D ] [ -b <em>backlog</em> ] [ -M | -m ] [ -B ] <em>ip</em> <em>port</em> <em>prog...</em>
</pre>

<ul>
Expand Down Expand Up @@ -57,12 +57,13 @@ <h2> Options </h2>
that by default UDP sockets are not connection-mode, and <tt>listen()</tt>
will fail - so you should always give the <tt>-b0</tt> option to
s6-tcpserver4-socketbinder along with <tt>-m</tt>. </li>
<li> <tt>-B</tt>&nbsp;: create a blocking socket. Default is non-blocking. </li>
</ul>

<h2> Notes </h2>

<ul>
<li> The socket is provided <strong>non-blocking</strong>. </li>
<li> The socket is provided <strong>non-blocking by default</strong>. </li>
<li> s6-tcpserver4-socketbinder is part of a set of basic blocks used to
build a flexible TCP/IPv4 super-server. It normally should be given a
command line crafted to make it execute into
Expand Down
5 changes: 3 additions & 2 deletions doc/s6-tcpserver6-socketbinder.html
Expand Up @@ -26,7 +26,7 @@ <h1> The <tt>s6-tcpserver6-socketbinder</tt> program </h1>
<h2> Interface </h2>

<pre>
s6-tcpserver6-socketbinder [ -d | -D ] [ -b <em>backlog</em> ] [ -M | -m ] <em>ip</em> <em>port</em> <em>prog...</em>
s6-tcpserver6-socketbinder [ -d | -D ] [ -b <em>backlog</em> ] [ -M | -m ] [ -B ] <em>ip</em> <em>port</em> <em>prog...</em>
</pre>

<ul>
Expand Down Expand Up @@ -57,12 +57,13 @@ <h2> Options </h2>
that by default UDP sockets are not connection-mode, and <tt>listen()</tt>
will fail - so you should always give the <tt>-b0</tt> option to
s6-tcpserver6-socketbinder along with <tt>-m</tt>. </li>
<li> <tt>-B</tt>&nbsp;: create a blocking socket. Default is non-blocking. </li>
</ul>

<h2> Notes </h2>

<ul>
<li> The socket is provided <strong>non-blocking</strong>. </li>
<li> The socket is provided <strong>non-blocking by default</strong>. </li>
<li> s6-tcpserver6-socketbinder is part of a set of basic blocks used to
build a flexible TCP/IPv6 super-server. It normally should be given a
command line crafted to make it execute into
Expand Down
9 changes: 6 additions & 3 deletions src/conn-tools/s6-tcpserver4-socketbinder.c
Expand Up @@ -10,29 +10,31 @@
#include <skalibs/djbunix.h>
#include <skalibs/socket.h>

#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)
{
unsigned int backlog = SOMAXCONN ;
int flagreuse = 1 ;
int flagudp = 0 ;
int flagblocking = 0 ;
char ip[4] ;
uint16_t port ;
PROG = "s6-tcpserver4-socketbinder" ;
{
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)
{
case 'D' : flagreuse = 0 ; break ;
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() ;
}
Expand All @@ -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)
Expand Down
9 changes: 6 additions & 3 deletions src/conn-tools/s6-tcpserver6-socketbinder.c
Expand Up @@ -10,29 +10,31 @@
#include <skalibs/djbunix.h>
#include <skalibs/socket.h>

#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)
{
unsigned int backlog = SOMAXCONN ;
int flagreuse = 1 ;
int flagudp = 0 ;
int flagblocking = 0 ;
char ip[16] ;
uint16_t port ;
PROG = "s6-tcpserver6-socketbinder" ;
{
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)
{
case 'D' : flagreuse = 0 ; break ;
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() ;
}
Expand All @@ -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)
Expand Down

0 comments on commit 1e3f94a

Please sign in to comment.