summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--doc/s6-tcpserver4-socketbinder.html15
-rw-r--r--doc/s6-tcpserver6-socketbinder.html15
-rw-r--r--src/conn-tools/s6-tcpserver4-socketbinder.c15
-rw-r--r--src/conn-tools/s6-tcpserver4.c1
-rw-r--r--src/conn-tools/s6-tcpserver6-socketbinder.c15
-rw-r--r--src/conn-tools/s6-tcpserver6.c1
6 files changed, 46 insertions, 16 deletions
diff --git a/doc/s6-tcpserver4-socketbinder.html b/doc/s6-tcpserver4-socketbinder.html
index 4cb75ee..d8c2ba9 100644
--- a/doc/s6-tcpserver4-socketbinder.html
+++ b/doc/s6-tcpserver4-socketbinder.html
@@ -26,7 +26,7 @@ socket to an IPv4 address and port, then executes a program.
<h2> Interface </h2>
<pre>
- s6-tcpserver4-socketbinder [ -d | -D ] [ -b <em>backlog</em> ] <em>ip</em> <em>port</em> <em>prog...</em>
+ s6-tcpserver4-socketbinder [ -d | -D ] [ -b <em>backlog</em> ] [ -M | -m ] <em>ip</em> <em>port</em> <em>prog...</em>
</pre>
<ul>
@@ -47,13 +47,22 @@ even if it has been used not long ago - this is the SO_REUSEADDR flag to
and is generally used with server programs. This is the default. </li>
<li> <tt>-D</tt>&nbsp;: disallow instant rebinding to the same path. </li>
<li> <tt>-b&nbsp;<em>backlog</em></tt>&nbsp;: set a maximum of
-<em>backlog</em> backlog connections on the socket. Extra
-connection attempts will rejected by the kernel. </li>
+<em>backlog</em> backlog connections on the socket - extra
+connection attempts will rejected by the kernel. The default is SOMAXCONN,
+i.e. the maximum number allowed by the system. If <em>backlog</em>
+is 0, then the socket will be created, but it <strong>will not be
+listening</em>. </li>
+ <li> <tt>-M</tt>&nbsp;: create a TCP socket. This is the default. </li>
+ <li> <tt>-m</tt>&nbsp;: create a UDP socket. Note
+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>
</ul>
<h2> Notes </h2>
<ul>
+ <li> The socket is provided <strong>non-blocking</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
diff --git a/doc/s6-tcpserver6-socketbinder.html b/doc/s6-tcpserver6-socketbinder.html
index ea2bc24..d497342 100644
--- a/doc/s6-tcpserver6-socketbinder.html
+++ b/doc/s6-tcpserver6-socketbinder.html
@@ -26,7 +26,7 @@ socket to an IPv6 address and port, then executes a program.
<h2> Interface </h2>
<pre>
- s6-tcpserver6-socketbinder [ -d | -D ] [ -b <em>backlog</em> ] <em>ip</em> <em>port</em> <em>prog...</em>
+ s6-tcpserver6-socketbinder [ -d | -D ] [ -b <em>backlog</em> ] [ -M | -m ] <em>ip</em> <em>port</em> <em>prog...</em>
</pre>
<ul>
@@ -47,13 +47,22 @@ even if it has been used not long ago - this is the SO_REUSEADDR flag to
and is generally used with server programs. This is the default. </li>
<li> <tt>-D</tt>&nbsp;: disallow instant rebinding to the same path. </li>
<li> <tt>-b&nbsp;<em>backlog</em></tt>&nbsp;: set a maximum of
-<em>backlog</em> backlog connections on the socket. Extra
-connection attempts will rejected by the kernel. </li>
+<em>backlog</em> backlog connections on the socket - extra
+connection attempts will rejected by the kernel. The default is SOMAXCONN,
+i.e. the maximum number allowed by the system. If <em>backlog</em>
+is 0, then the socket will be created, but it <strong>will not be
+listening</em>. </li>
+ <li> <tt>-M</tt>&nbsp;: create a TCP socket. This is the default. </li>
+ <li> <tt>-m</tt>&nbsp;: create a UDP socket. Note
+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>
</ul>
<h2> Notes </h2>
<ul>
+ <li> The socket is provided <strong>non-blocking</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
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) ;
diff --git a/src/conn-tools/s6-tcpserver4.c b/src/conn-tools/s6-tcpserver4.c
index 6a85110..7baa66c 100644
--- a/src/conn-tools/s6-tcpserver4.c
+++ b/src/conn-tools/s6-tcpserver4.c
@@ -61,6 +61,7 @@ int main (int argc, char const *const *argv, char const *const *envp)
if (!flagreuse) newargv[m++] = "-D" ;
if (backlog != (unsigned int)-1)
{
+ if (!backlog) backlog = 1 ;
newargv[m++] = "-b" ;
newargv[m++] = fmt + pos ;
pos += uint_fmt(fmt + pos, backlog) ;
diff --git a/src/conn-tools/s6-tcpserver6-socketbinder.c b/src/conn-tools/s6-tcpserver6-socketbinder.c
index 6f020b2..e6c7fd8 100644
--- a/src/conn-tools/s6-tcpserver6-socketbinder.c
+++ b/src/conn-tools/s6-tcpserver6-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-tcpserver6-socketbinder [ -d | -D ] [ -b backlog ] ip6 port prog..."
+#define USAGE "s6-tcpserver6-socketbinder [ -d | -D ] [ -b backlog ] [ -M | -m ] ip6 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[16] ;
uint16 port ;
PROG = "s6-tcpserver6-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 (!ip6_scan(argv[0], ip) || !uint160_scan(argv[1], &port)) dieusage() ;
close(0) ;
- if (socket_tcp6()) strerr_diefu1sys(111, "create socket") ;
+ if (flagudp ? socket_udp6() : socket_tcp6()) 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 (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) ;
diff --git a/src/conn-tools/s6-tcpserver6.c b/src/conn-tools/s6-tcpserver6.c
index 7cffb54..90a6e8f 100644
--- a/src/conn-tools/s6-tcpserver6.c
+++ b/src/conn-tools/s6-tcpserver6.c
@@ -61,6 +61,7 @@ int main (int argc, char const *const *argv, char const *const *envp)
if (!flagreuse) newargv[m++] = "-D" ;
if (backlog != (unsigned int)-1)
{
+ if (!backlog) backlog = 1 ;
newargv[m++] = "-b" ;
newargv[m++] = fmt + pos ;
pos += uint_fmt(fmt + pos, backlog) ;