diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/conn-tools/deps-exe/s6-tcpserver-socketbinder (renamed from src/conn-tools/deps-exe/s6-tcpserver4) | 0 | ||||
-rw-r--r-- | src/conn-tools/deps-exe/s6-tcpserver4d | 2 | ||||
-rw-r--r-- | src/conn-tools/deps-exe/s6-tcpserver6 | 2 | ||||
-rw-r--r-- | src/conn-tools/deps-exe/s6-tcpserver6-socketbinder | 2 | ||||
-rw-r--r-- | src/conn-tools/deps-exe/s6-tcpserver6d | 2 | ||||
-rw-r--r-- | src/conn-tools/deps-exe/s6-tcpserverd (renamed from src/conn-tools/deps-exe/s6-tcpserver4-socketbinder) | 0 | ||||
-rw-r--r-- | src/conn-tools/s6-tcpserver-access.c | 18 | ||||
-rw-r--r-- | src/conn-tools/s6-tcpserver-socketbinder.c (renamed from src/conn-tools/s6-tcpserver4-socketbinder.c) | 15 | ||||
-rw-r--r-- | src/conn-tools/s6-tcpserver.c | 18 | ||||
-rw-r--r-- | src/conn-tools/s6-tcpserver4.c | 131 | ||||
-rw-r--r-- | src/conn-tools/s6-tcpserver6-socketbinder.c | 58 | ||||
-rw-r--r-- | src/conn-tools/s6-tcpserver6.c | 131 | ||||
-rw-r--r-- | src/conn-tools/s6-tcpserver6d.c | 374 | ||||
-rw-r--r-- | src/conn-tools/s6-tcpserverd.c (renamed from src/conn-tools/s6-tcpserver4d.c) | 194 |
14 files changed, 121 insertions, 826 deletions
diff --git a/src/conn-tools/deps-exe/s6-tcpserver4 b/src/conn-tools/deps-exe/s6-tcpserver-socketbinder index 19869b2..19869b2 100644 --- a/src/conn-tools/deps-exe/s6-tcpserver4 +++ b/src/conn-tools/deps-exe/s6-tcpserver-socketbinder diff --git a/src/conn-tools/deps-exe/s6-tcpserver4d b/src/conn-tools/deps-exe/s6-tcpserver4d deleted file mode 100644 index 19869b2..0000000 --- a/src/conn-tools/deps-exe/s6-tcpserver4d +++ /dev/null @@ -1,2 +0,0 @@ --lskarnet -${SOCKET_LIB} diff --git a/src/conn-tools/deps-exe/s6-tcpserver6 b/src/conn-tools/deps-exe/s6-tcpserver6 deleted file mode 100644 index 19869b2..0000000 --- a/src/conn-tools/deps-exe/s6-tcpserver6 +++ /dev/null @@ -1,2 +0,0 @@ --lskarnet -${SOCKET_LIB} diff --git a/src/conn-tools/deps-exe/s6-tcpserver6-socketbinder b/src/conn-tools/deps-exe/s6-tcpserver6-socketbinder deleted file mode 100644 index 19869b2..0000000 --- a/src/conn-tools/deps-exe/s6-tcpserver6-socketbinder +++ /dev/null @@ -1,2 +0,0 @@ --lskarnet -${SOCKET_LIB} diff --git a/src/conn-tools/deps-exe/s6-tcpserver6d b/src/conn-tools/deps-exe/s6-tcpserver6d deleted file mode 100644 index 19869b2..0000000 --- a/src/conn-tools/deps-exe/s6-tcpserver6d +++ /dev/null @@ -1,2 +0,0 @@ --lskarnet -${SOCKET_LIB} diff --git a/src/conn-tools/deps-exe/s6-tcpserver4-socketbinder b/src/conn-tools/deps-exe/s6-tcpserverd index 19869b2..19869b2 100644 --- a/src/conn-tools/deps-exe/s6-tcpserver4-socketbinder +++ b/src/conn-tools/deps-exe/s6-tcpserverd diff --git a/src/conn-tools/s6-tcpserver-access.c b/src/conn-tools/s6-tcpserver-access.c index addf66b..ddd6072 100644 --- a/src/conn-tools/s6-tcpserver-access.c +++ b/src/conn-tools/s6-tcpserver-access.c @@ -127,6 +127,14 @@ int main (int argc, char const *const *argv) char const *x ; char tmp[protolen + 11] ; memcpy(tmp, proto, protolen) ; + memcpy(tmp + protolen, "LOCALIP", 8) ; + x = getenv(tmp) ; + if (!x) strerr_dienotset(100, tmp) ; + if (!ip46_scan(x, &localip)) strerr_dieinvalid(100, tmp) ; + memcpy(tmp + protolen + 5, "PORT", 5) ; + x = getenv(tmp) ; + if (!x) strerr_dienotset(100, tmp) ; + if (!uint160_scan(x, &localport)) strerr_dieinvalid(100, tmp) ; memcpy(tmp + protolen, "REMOTEIP", 9) ; x = getenv(tmp) ; if (!x) strerr_dienotset(100, tmp) ; @@ -180,17 +188,8 @@ int main (int argc, char const *const *argv) { char const *x = 0 ; char idbuf[S6NET_IDENT_ID_SIZE] ; - char fmt[IP46_FMT] ; char tmp[protolen + 11] ; - if (socket_local46(0, &localip, &localport) < 0) - strerr_diefu1sys(111, "socket_local") ; - fmt[ip46_fmt(fmt, &localip)] = 0 ; memcpy(tmp, proto, protolen) ; - memcpy(tmp + protolen, "LOCALIP", 8) ; - if (!env_addmodif(&modifs, tmp, fmt)) dienomem() ; - fmt[uint16_fmt(fmt, localport)] = 0 ; - memcpy(tmp + protolen + 5, "PORT", 5) ; - if (!env_addmodif(&modifs, tmp, fmt)) dienomem() ; memcpy(tmp + protolen, "REMOTEINFO", 11) ; if (flagident) { @@ -215,7 +214,6 @@ int main (int argc, char const *const *argv) } else x = idbuf ; } - if (!env_addmodif(&modifs, tmp, x)) dienomem() ; } diff --git a/src/conn-tools/s6-tcpserver4-socketbinder.c b/src/conn-tools/s6-tcpserver-socketbinder.c index aadb3d9..bc40ef4 100644 --- a/src/conn-tools/s6-tcpserver4-socketbinder.c +++ b/src/conn-tools/s6-tcpserver-socketbinder.c @@ -11,9 +11,10 @@ #include <skalibs/fmtscan.h> #include <skalibs/strerr.h> #include <skalibs/socket.h> +#include <skalibs/ip46.h> #include <skalibs/exec.h> -#define USAGE "s6-tcpserver4-socketbinder [ -d | -D ] [ -b backlog ] [ -M | -m ] [ -B ] ip4 port prog..." +#define USAGE "s6-tcpserver-socketbinder [ -d | -D ] [ -b backlog ] [ -M | -m ] [ -B ] ip port prog..." #define dieusage() strerr_dieusage(100, USAGE) int main (int argc, char const *const *argv) @@ -22,9 +23,9 @@ int main (int argc, char const *const *argv) int flagreuse = 1 ; int flagudp = 0 ; unsigned int flags = O_NONBLOCK ; - char ip[4] ; + ip46 ip ; uint16_t port ; - PROG = "s6-tcpserver4-socketbinder" ; + PROG = "s6-tcpserver-socketbinder" ; { subgetopt l = SUBGETOPT_ZERO ; for (;;) @@ -45,13 +46,13 @@ int main (int argc, char const *const *argv) argc -= l.ind ; argv += l.ind ; } if (argc < 3) dieusage() ; - if (!ip4_scan(argv[0], ip) || !uint160_scan(argv[1], &port)) dieusage() ; + if (!ip46_scan(argv[0], &ip) || !uint160_scan(argv[1], &port)) dieusage() ; close(0) ; - if (flagudp ? socket_udp4_internal(flags) : socket_tcp4_internal(flags)) + if (flagudp ? socket_udp46_internal(ip46_is6(&ip), flags) : socket_tcp46_internal(ip46_is6(&ip), flags)) strerr_diefu1sys(111, "create socket") ; - if ((flagreuse ? socket_bind4_reuse(0, ip, port) : socket_bind4(0, ip, port)) < 0) + if ((flagreuse ? socket_bind46_reuse(0, &ip, port) : socket_bind46(0, &ip, port)) == -1) strerr_diefu5sys(111, "bind to ", argv[0], ":", argv[1], " ") ; - if (backlog && socket_listen(0, backlog) < 0) + if (backlog && socket_listen(0, backlog) == -1) strerr_diefu5sys(111, "listen to ", argv[0], ":", argv[1], " ") ; xexec(argv+2) ; diff --git a/src/conn-tools/s6-tcpserver.c b/src/conn-tools/s6-tcpserver.c index 435fe0d..edc13ea 100644 --- a/src/conn-tools/s6-tcpserver.c +++ b/src/conn-tools/s6-tcpserver.c @@ -13,7 +13,7 @@ #include <s6-networking/config.h> -#define USAGE "s6-tcpserver [ -q | -Q | -v ] [ -4 | -6 ] [ -d | -D ] [ -1 ] [ -c maxconn ] [ -C localmaxconn ] [ -b backlog ] [ -G gid,gid,... ] [ -g gid ] [ -u uid ] [ -U ] ip port prog..." +#define USAGE "s6-tcpserver [ -q | -Q | -v ] [ -d | -D ] [ -1 ] [ -c maxconn ] [ -C localmaxconn ] [ -b backlog ] [ -G gid,gid,... ] [ -g gid ] [ -u uid ] [ -U ] ip port prog..." #define dieusage() strerr_dieusage(100, USAGE) int main (int argc, char const *const *argv) @@ -29,13 +29,12 @@ int main (int argc, char const *const *argv) unsigned int maxconn = 0 ; unsigned int localmaxconn = 0 ; unsigned int backlog = (unsigned int)-1 ; - unsigned int what = 0 ; PROG = "s6-tcpserver" ; { subgetopt l = SUBGETOPT_ZERO ; for (;;) { - int opt = subgetopt_r(argc, argv, "qQvDd1U46c:C:b:u:g:G:", &l) ; + int opt = subgetopt_r(argc, argv, "qQvDd1Uc:C:b:u:g:G:", &l) ; if (opt == -1) break ; switch (opt) { @@ -44,8 +43,6 @@ int main (int argc, char const *const *argv) case 'v' : verbosity = 2 ; break ; case 'D' : flagreuse = 0 ; break ; case 'd' : flagreuse = 1 ; break ; - case '4' : what = 4 ; break ; - case '6' : what = 6 ; break ; case 'c' : if (!uint0_scan(l.arg, &maxconn)) dieusage() ; if (!maxconn) maxconn = 1 ; break ; case 'C' : if (!uint0_scan(l.arg, &localmaxconn)) dieusage() ; if (!localmaxconn) localmaxconn = 1 ; break ; case 'b' : if (!uint0_scan(l.arg, &backlog)) dieusage() ; break ; @@ -61,19 +58,12 @@ int main (int argc, char const *const *argv) if (argc < 3) dieusage() ; } - if (!what) - { - ip46 ip ; - if (!ip46_scan(argv[0], &ip)) dieusage() ; - what = ip46_is6(&ip) ? 6 : 4 ; - } - { size_t pos = 0 ; unsigned int m = 0 ; char fmt[UINT_FMT * 3 + UID_FMT + GID_FMT * (NGROUPS_MAX + 1)] ; char const *newargv[23 + argc] ; - newargv[m++] = what == 6 ? S6_NETWORKING_BINPREFIX "s6-tcpserver6-socketbinder" : S6_NETWORKING_BINPREFIX "s6-tcpserver4-socketbinder" ; + newargv[m++] = S6_NETWORKING_BINPREFIX "s6-tcpserver-socketbinder" ; if (!flagreuse) newargv[m++] = "-D" ; if (backlog != (unsigned int)-1) { @@ -112,7 +102,7 @@ int main (int argc, char const *const *argv) } newargv[m++] = "--" ; } - newargv[m++] = what == 6 ? S6_NETWORKING_BINPREFIX "s6-tcpserver6d" : S6_NETWORKING_BINPREFIX "s6-tcpserver4d" ; + newargv[m++] = S6_NETWORKING_BINPREFIX "s6-tcpserverd" ; if (!verbosity) newargv[m++] = "-v0" ; else if (verbosity == 2) newargv[m++] = "-v2" ; if (flag1) newargv[m++] = "-1" ; diff --git a/src/conn-tools/s6-tcpserver4.c b/src/conn-tools/s6-tcpserver4.c deleted file mode 100644 index eb828a4..0000000 --- a/src/conn-tools/s6-tcpserver4.c +++ /dev/null @@ -1,131 +0,0 @@ -/* ISC license. */ - -#include <sys/types.h> -#include <limits.h> - -#include <skalibs/types.h> -#include <skalibs/sgetopt.h> -#include <skalibs/strerr.h> -#include <skalibs/exec.h> - -#include <s6/config.h> - -#include <s6-networking/config.h> - -#define USAGE "s6-tcpserver4 [ -v verbosity ] [ -d | -D ] [ -1 ] [ -c maxconn ] [ -C localmaxconn ] [ -b backlog ] [ -G gid,gid,... ] [ -g gid ] [ -u uid ] [ -U ] ip port prog..." -#define dieusage() strerr_dieusage(100, USAGE) - -int main (int argc, char const *const *argv) -{ - unsigned int verbosity = 1 ; - int flag1 = 0 ; - int flagU = 0 ; - int flagreuse = 1 ; - uid_t uid = 0 ; - gid_t gid = 0 ; - gid_t gids[NGROUPS_MAX] ; - size_t gidn = (size_t)-1 ; - unsigned int maxconn = 0 ; - unsigned int localmaxconn = 0 ; - unsigned int backlog = (unsigned int)-1 ; - PROG = "s6-tcpserver4" ; - { - subgetopt l = SUBGETOPT_ZERO ; - for (;;) - { - int opt = subgetopt_r(argc, argv, "Dd1Uv:c:C:b:u:g:G:", &l) ; - if (opt == -1) break ; - switch (opt) - { - case 'D' : flagreuse = 0 ; break ; - case 'd' : flagreuse = 1 ; break ; - case 'v' : if (!uint0_scan(l.arg, &verbosity)) dieusage() ; break ; - case 'c' : if (!uint0_scan(l.arg, &maxconn)) dieusage() ; if (!maxconn) maxconn = 1 ; break ; - case 'C' : if (!uint0_scan(l.arg, &localmaxconn)) dieusage() ; if (!localmaxconn) localmaxconn = 1 ; break ; - case 'b' : if (!uint0_scan(l.arg, &backlog)) dieusage() ; break ; - case 'u' : if (!uid0_scan(l.arg, &uid)) dieusage() ; break ; - case 'g' : if (!gid0_scan(l.arg, &gid)) dieusage() ; break ; - case 'G' : if (!gid_scanlist(gids, NGROUPS_MAX, l.arg, &gidn) && *l.arg) dieusage() ; break ; - case '1' : flag1 = 1 ; break ; - case 'U' : flagU = 1 ; uid = 0 ; gid = 0 ; gidn = (size_t)-1 ; break ; - default : dieusage() ; - } - } - argc -= l.ind ; argv += l.ind ; - if (argc < 3) dieusage() ; - } - - { - size_t pos = 0 ; - unsigned int m = 0 ; - char fmt[UINT_FMT * 4 + UID_FMT + GID_FMT * (NGROUPS_MAX + 1)] ; - char const *newargv[24 + argc] ; - newargv[m++] = S6_NETWORKING_BINPREFIX "s6-tcpserver4-socketbinder" ; - 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) ; - fmt[pos++] = 0 ; - } - newargv[m++] = "--" ; - newargv[m++] = *argv++ ; - newargv[m++] = *argv++ ; - if (flagU || uid || gid || gidn != (size_t)-1) - { - newargv[m++] = S6_EXTBINPREFIX "s6-applyuidgid" ; - if (flagU) newargv[m++] = "-Uz" ; - if (uid) - { - newargv[m++] = "-u" ; - newargv[m++] = fmt + pos ; - pos += uid_fmt(fmt + pos, uid) ; - fmt[pos++] = 0 ; - } - if (gid) - { - newargv[m++] = "-g" ; - newargv[m++] = fmt + pos ; - pos += gid_fmt(fmt + pos, gid) ; - fmt[pos++] = 0 ; - } - if (gidn != (size_t)-1) - { - newargv[m++] = "-G" ; - newargv[m++] = fmt + pos ; - pos += gid_fmtlist(fmt + pos, gids, gidn) ; - fmt[pos++] = 0 ; - } - newargv[m++] = "--" ; - } - newargv[m++] = S6_NETWORKING_BINPREFIX "s6-tcpserver4d" ; - if (verbosity != 1) - { - newargv[m++] = "-v" ; - newargv[m++] = fmt + pos ; - pos += uint_fmt(fmt + pos, verbosity) ; - fmt[pos++] = 0 ; - } - if (flag1) newargv[m++] = "-1" ; - if (maxconn) - { - newargv[m++] = "-c" ; - newargv[m++] = fmt + pos ; - pos += uint_fmt(fmt + pos, maxconn) ; - fmt[pos++] = 0 ; - } - if (localmaxconn) - { - newargv[m++] = "-C" ; - newargv[m++] = fmt + pos ; - pos += uint_fmt(fmt + pos, localmaxconn) ; - fmt[pos++] = 0 ; - } - newargv[m++] = "--" ; - while (*argv) newargv[m++] = *argv++ ; - newargv[m++] = 0 ; - xexec(newargv) ; - } -} diff --git a/src/conn-tools/s6-tcpserver6-socketbinder.c b/src/conn-tools/s6-tcpserver6-socketbinder.c deleted file mode 100644 index b1c7dba..0000000 --- a/src/conn-tools/s6-tcpserver6-socketbinder.c +++ /dev/null @@ -1,58 +0,0 @@ -/* ISC license. */ - -#include <skalibs/nonposix.h> -#include <stdint.h> -#include <unistd.h> -#include <fcntl.h> -#include <sys/socket.h> - -#include <skalibs/types.h> -#include <skalibs/sgetopt.h> -#include <skalibs/fmtscan.h> -#include <skalibs/strerr.h> -#include <skalibs/socket.h> -#include <skalibs/exec.h> - -#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) -{ - unsigned int backlog = SOMAXCONN ; - int flagreuse = 1 ; - int flagudp = 0 ; - unsigned int flags = O_NONBLOCK ; - char ip[16] ; - uint16_t port ; - PROG = "s6-tcpserver6-socketbinder" ; - { - subgetopt l = SUBGETOPT_ZERO ; - for (;;) - { - 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' : flags = 0 ; break ; - case 'b' : if (!uint0_scan(l.arg, &backlog)) dieusage() ; break ; - default : dieusage() ; - } - } - argc -= l.ind ; argv += l.ind ; - } - if (argc < 3) dieusage() ; - if (!ip6_scan(argv[0], ip) || !uint160_scan(argv[1], &port)) dieusage() ; - close(0) ; - if (flagudp ? socket_udp6_internal(flags) : socket_tcp6_internal(flags)) - 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) - strerr_diefu5sys(111, "listen to ", argv[0], ":", argv[1], " ") ; - - xexec(argv+2) ; -} diff --git a/src/conn-tools/s6-tcpserver6.c b/src/conn-tools/s6-tcpserver6.c deleted file mode 100644 index 491d7e0..0000000 --- a/src/conn-tools/s6-tcpserver6.c +++ /dev/null @@ -1,131 +0,0 @@ -/* ISC license. */ - -#include <sys/types.h> -#include <limits.h> - -#include <skalibs/types.h> -#include <skalibs/sgetopt.h> -#include <skalibs/strerr.h> -#include <skalibs/exec.h> - -#include <s6/config.h> - -#include <s6-networking/config.h> - -#define USAGE "s6-tcpserver6 [ -v verbosity ] [ -d | -D ] [ -1 ] [ -c maxconn ] [ -C localmaxconn ] [ -b backlog ] [ -G gid,gid,... ] [ -g gid ] [ -u uid ] [ -U ] ip port prog..." -#define dieusage() strerr_dieusage(100, USAGE) - -int main (int argc, char const *const *argv) -{ - unsigned int verbosity = 1 ; - int flag1 = 0 ; - int flagU = 0 ; - int flagreuse = 1 ; - uid_t uid = 0 ; - gid_t gid = 0 ; - gid_t gids[NGROUPS_MAX] ; - size_t gidn = (size_t)-1 ; - unsigned int maxconn = 0 ; - unsigned int localmaxconn = 0 ; - unsigned int backlog = (unsigned int)-1 ; - PROG = "s6-tcpserver6" ; - { - subgetopt l = SUBGETOPT_ZERO ; - for (;;) - { - int opt = subgetopt_r(argc, argv, "Dd1Uv:c:C:b:u:g:G:", &l) ; - if (opt == -1) break ; - switch (opt) - { - case 'D' : flagreuse = 0 ; break ; - case 'd' : flagreuse = 1 ; break ; - case 'v' : if (!uint0_scan(l.arg, &verbosity)) dieusage() ; break ; - case 'c' : if (!uint0_scan(l.arg, &maxconn)) dieusage() ; if (!maxconn) maxconn = 1 ; break ; - case 'C' : if (!uint0_scan(l.arg, &localmaxconn)) dieusage() ; if (!localmaxconn) localmaxconn = 1 ; break ; - case 'b' : if (!uint0_scan(l.arg, &backlog)) dieusage() ; break ; - case 'u' : if (!uid0_scan(l.arg, &uid)) dieusage() ; break ; - case 'g' : if (!gid0_scan(l.arg, &gid)) dieusage() ; break ; - case 'G' : if (!gid_scanlist(gids, NGROUPS_MAX, l.arg, &gidn) && *l.arg) dieusage() ; break ; - case '1' : flag1 = 1 ; break ; - case 'U' : flagU = 1 ; uid = 0 ; gid = 0 ; gidn = (size_t)-1 ; break ; - default : dieusage() ; - } - } - argc -= l.ind ; argv += l.ind ; - if (argc < 3) dieusage() ; - } - - { - size_t pos = 0 ; - unsigned int m = 0 ; - char fmt[UINT_FMT * 4 + UID_FMT + GID_FMT * (NGROUPS_MAX + 1)] ; - char const *newargv[24 + argc] ; - newargv[m++] = S6_NETWORKING_BINPREFIX "s6-tcpserver6-socketbinder" ; - 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) ; - fmt[pos++] = 0 ; - } - newargv[m++] = "--" ; - newargv[m++] = *argv++ ; - newargv[m++] = *argv++ ; - if (flagU || uid || gid || gidn != (size_t)-1) - { - newargv[m++] = S6_EXTBINPREFIX "s6-applyuidgid" ; - if (flagU) newargv[m++] = "-Uz" ; - if (uid) - { - newargv[m++] = "-u" ; - newargv[m++] = fmt + pos ; - pos += uid_fmt(fmt + pos, uid) ; - fmt[pos++] = 0 ; - } - if (gid) - { - newargv[m++] = "-g" ; - newargv[m++] = fmt + pos ; - pos += gid_fmt(fmt + pos, gid) ; - fmt[pos++] = 0 ; - } - if (gidn != (size_t)-1) - { - newargv[m++] = "-G" ; - newargv[m++] = fmt + pos ; - pos += gid_fmtlist(fmt + pos, gids, gidn) ; - fmt[pos++] = 0 ; - } - newargv[m++] = "--" ; - } - newargv[m++] = S6_NETWORKING_BINPREFIX "s6-tcpserver6d" ; - if (verbosity != 1) - { - newargv[m++] = "-v" ; - newargv[m++] = fmt + pos ; - pos += uint_fmt(fmt + pos, verbosity) ; - fmt[pos++] = 0 ; - } - if (flag1) newargv[m++] = "-1" ; - if (maxconn) - { - newargv[m++] = "-c" ; - newargv[m++] = fmt + pos ; - pos += uint_fmt(fmt + pos, maxconn) ; - fmt[pos++] = 0 ; - } - if (localmaxconn) - { - newargv[m++] = "-C" ; - newargv[m++] = fmt + pos ; - pos += uint_fmt(fmt + pos, localmaxconn) ; - fmt[pos++] = 0 ; - } - newargv[m++] = "--" ; - while (*argv) newargv[m++] = *argv++ ; - newargv[m++] = 0 ; - xexec(newargv) ; - } -} diff --git a/src/conn-tools/s6-tcpserver6d.c b/src/conn-tools/s6-tcpserver6d.c deleted file mode 100644 index 4168d91..0000000 --- a/src/conn-tools/s6-tcpserver6d.c +++ /dev/null @@ -1,374 +0,0 @@ -/* ISC license. */ - -#include <string.h> -#include <stdint.h> -#include <sys/stat.h> -#include <sys/wait.h> -#include <errno.h> -#include <unistd.h> -#include <fcntl.h> -#include <signal.h> - -#include <skalibs/posixplz.h> -#include <skalibs/allreadwrite.h> -#include <skalibs/types.h> -#include <skalibs/sgetopt.h> -#include <skalibs/strerr.h> -#include <skalibs/fmtscan.h> -#include <skalibs/env.h> -#include <skalibs/cspawn.h> -#include <skalibs/djbunix.h> -#include <skalibs/sig.h> -#include <skalibs/selfpipe.h> -#include <skalibs/iopause.h> -#include <skalibs/socket.h> - -#define ABSOLUTE_MAXCONN 16384 - -#define USAGE "s6-tcpserver6d [ -v verbosity ] [ -1 ] [ -c maxconn ] [ -C localmaxconn ] prog..." - -typedef struct pidip_s pidip_t, *pidip_t_ref ; -struct pidip_s -{ - pid_t pid ; - char ip[16] ; -} ; - -typedef struct ipnum_s ipnum_t, *ipnum_t_ref ; -struct ipnum_s -{ - char ip[16] ; - unsigned int num ; -} ; - -static unsigned int maxconn = 40 ; -static unsigned int localmaxconn = 40 ; -static unsigned int verbosity = 1 ; -static int cont = 1 ; -static pidip_t *pidip = 0 ; -static unsigned int numconn = 0 ; -static ipnum_t *ipnum = 0 ; -static unsigned int iplen = 0 ; - -static char fmtmaxconn[UINT_FMT+1] = "/" ; -static char fmtlocalmaxconn[UINT_FMT+1] = "/" ; - -static inline void dieusage () -{ - strerr_dieusage(100, USAGE) ; -} - -static inline void X (void) -{ - strerr_dief1x(101, "internal inconsistency. Please submit a bug-report.") ; -} - -static inline unsigned int lookup_pid (pid_t pid) -{ - unsigned int i = 0 ; - for (; i < numconn ; i++) if (pid == pidip[i].pid) break ; - return i ; -} - -static inline unsigned int lookup_ip (char const *ip) -{ - unsigned int i = 0 ; - for (; i < iplen ; i++) if (!memcmp(ip, ipnum[i].ip, 16)) break ; - return i ; -} - -static inline void log_start (void) -{ - strerr_warni1x("starting") ; -} - -static inline void log_exit (void) -{ - strerr_warni1x("exiting") ; -} - -static void log_status (void) -{ - char fmt[UINT_FMT] ; - fmt[uint_fmt(fmt, numconn)] = 0 ; - strerr_warni3x("status: ", fmt, fmtmaxconn) ; -} - -static inline void log_deny (char const *ip, uint16_t port, unsigned int num) -{ - char fmtip[IP6_FMT] ; - char fmtport[UINT16_FMT] ; - char fmtnum[UINT_FMT] ; - fmtip[ip6_fmt(fmtip, ip)] = 0 ; - fmtport[uint16_fmt(fmtport, port)] = 0 ; - fmtnum[uint_fmt(fmtnum, num)] = 0 ; - strerr_warni7sys("deny ", fmtip, " port ", fmtport, " count ", fmtnum, fmtlocalmaxconn) ; -} - -static inline void log_accept (pid_t pid, char const *ip, uint16_t port, unsigned int num) -{ - char fmtipport[IP6_FMT + UINT16_FMT + 6] ; - char fmtpid[PID_FMT] ; - char fmtnum[UINT_FMT] ; - size_t n ; - n = ip6_fmt(fmtipport, ip) ; - memcpy(fmtipport + n, " port ", 6) ; n += 6 ; - n += uint16_fmt(fmtipport + n, port) ; - fmtipport[n] = 0 ; - fmtnum[uint_fmt(fmtnum, num)] = 0 ; - fmtpid[pid_fmt(fmtpid, pid)] = 0 ; - strerr_warni7x("allow ", fmtipport, " pid ", fmtpid, " count ", fmtnum, fmtlocalmaxconn) ; -} - -static inline void log_close (pid_t pid, char const *ip, int w) -{ - char fmtpid[PID_FMT] ; - char fmtip[IP6_FMT] = "?" ; - char fmtw[UINT_FMT] ; - fmtpid[pid_fmt(fmtpid, pid)] = 0 ; - fmtip[ip6_fmt(fmtip, ip)] = 0 ; - fmtw[uint_fmt(fmtw, WIFSIGNALED(w) ? WTERMSIG(w) : WEXITSTATUS(w))] = 0 ; - strerr_warni6x("end pid ", fmtpid, " ip ", fmtip, WIFSIGNALED(w) ? " signal " : " exitcode ", fmtw) ; -} - -static void killthem (int sig) -{ - unsigned int i = 0 ; - for (; i < numconn ; i++) kill(pidip[i].pid, sig) ; -} - -static inline void wait_children (void) -{ - for (;;) - { - unsigned int i ; - int w ; - pid_t pid = wait_nohang(&w) ; - if (pid < 0) - if (errno != ECHILD) strerr_diefu1sys(111, "wait_nohang") ; - else break ; - else if (!pid) break ; - i = lookup_pid(pid) ; - if (i < numconn) /* it's one of ours ! */ - { - unsigned int j = lookup_ip(pidip[i].ip) ; - if (j >= iplen) X() ; - if (!--ipnum[j].num) ipnum[j] = ipnum[--iplen] ; - --numconn ; - if (verbosity >= 2) - { - log_close(pid, pidip[i].ip, w) ; - log_status() ; - } - pidip[i] = pidip[numconn] ; - } - } -} - -static inline void handle_signals (void) -{ - for (;;) switch (selfpipe_read()) - { - case -1 : strerr_diefu1sys(111, "read selfpipe") ; - case 0 : return ; - case SIGCHLD : wait_children() ; break ; - case SIGTERM : - { - if (verbosity >= 2) - strerr_warni3x("received ", "SIGTERM,", " quitting") ; - cont = 0 ; - break ; - } - case SIGHUP : - { - if (verbosity >= 2) - strerr_warni5x("received ", "SIGHUP,", " sending ", "SIGTERM+SIGCONT", " to all connections") ; - killthem(SIGTERM) ; - killthem(SIGCONT) ; - break ; - } - case SIGQUIT : - { - if (verbosity >= 2) - strerr_warni6x("received ", "SIGQUIT,", " sending ", "SIGTERM+SIGCONT", " to all connections", " and quitting") ; - cont = 0 ; - killthem(SIGTERM) ; - killthem(SIGCONT) ; - break ; - } - case SIGABRT : - { - if (verbosity >= 2) - strerr_warni6x("received ", "SIGABRT,", " sending ", "SIGKILL", " to all connections", " and quitting") ; - cont = 0 ; - killthem(SIGKILL) ; - break ; - } - default : X() ; - } -} - -static inline void new_connection (int s, char const *ip, uint16_t port, char const *const *argv, char const *const *envp, size_t envlen) -{ - pid_t pid ; - unsigned int i = lookup_ip(ip) ; - unsigned int num = (i < iplen) ? ipnum[i].num : 0 ; - size_t m = 0 ; - char fmt[47 + IP6_FMT + UINT16_FMT + UINT_FMT] ; - - if (num >= localmaxconn) - { - log_deny(ip, port, num) ; - return ; - } - - memcpy(fmt + m, "PROTO=TCP\0TCPREMOTEIP=", 22) ; m += 22 ; - m += ip6_fmt(fmt + m, ip) ; fmt[m++] = 0 ; - memcpy(fmt + m, "TCPREMOTEPORT=", 14) ; m += 14 ; - m += uint16_fmt(fmt + m, port) ; fmt[m++] = 0 ; - memcpy(fmt + m, "TCPCONNNUM=", 11) ; m += 11 ; - m += uint_fmt(fmt + m, num) ; fmt[m++] = 0 ; - - { - cspawn_fileaction fa[2] = - { - [0] = { .type = CSPAWN_FA_MOVE, .x = { .fd2 = { [0] = 0, [1] = s } } }, - [1] = { .type = CSPAWN_FA_COPY, .x = { .fd2 = { [0] = 1, [1] = 0 } } } - } ; - char const *newenvp[envlen + 5] ; - env_mergen(newenvp, envlen + 5, envp, envlen, fmt, m, 4) ; - pid = cspawn(argv[0], argv, newenvp, CSPAWN_FLAGS_SELFPIPE_FINISH, fa, 2) ; - } - if (!pid) - { - if (verbosity) strerr_warnwu2sys("spawn ", argv[0]) ; - return ; - } - - if (i < iplen) ipnum[i].num = num + 1 ; - else - { - memcpy(ipnum[iplen].ip, ip, 16) ; - ipnum[iplen++].num = 1 ; - } - pidip[numconn].pid = pid ; - memcpy(pidip[numconn++].ip, ip, 16) ; - if (verbosity >= 2) - { - log_accept(pid, ip, port, ipnum[i].num) ; - log_status() ; - } -} - -int main (int argc, char const *const *argv) -{ - iopause_fd x[2] = { { .events = IOPAUSE_READ }, { .fd = 0, .events = IOPAUSE_READ | IOPAUSE_EXCEPT } } ; - PROG = "s6-tcpserver6d" ; - { - subgetopt l = SUBGETOPT_ZERO ; - int flag1 = 0 ; - for (;;) - { - int opt = subgetopt_r(argc, argv, "1c:C:v:", &l) ; - if (opt == -1) break ; - switch (opt) - { - case '1' : flag1 = 1 ; break ; - case 'c' : if (!uint0_scan(l.arg, &maxconn)) dieusage() ; break ; - case 'C' : if (!uint0_scan(l.arg, &localmaxconn)) dieusage() ; break ; - case 'v' : if (!uint0_scan(l.arg, &verbosity)) dieusage() ; break ; - default : dieusage() ; - } - } - argc -= l.ind ; argv += l.ind ; - if (!argc || !*argv[0]) dieusage() ; - { - struct stat st ; - if (fstat(0, &st) < 0) strerr_diefu1sys(111, "fstat stdin") ; - if (!S_ISSOCK(st.st_mode)) strerr_dief1x(100, "stdin is not a socket") ; - } - if (coe(0) < 0) strerr_diefu1sys(111, "make socket close-on-exec") ; - if (flag1) - { - if (fcntl(1, F_GETFD) < 0) - strerr_dief1sys(100, "called with option -1 but stdout said") ; - } - else close(1) ; - if (!maxconn) maxconn = 1 ; - if (maxconn > ABSOLUTE_MAXCONN) maxconn = ABSOLUTE_MAXCONN ; - if (localmaxconn > maxconn) localmaxconn = maxconn ; - - x[0].fd = selfpipe_init() ; - if (x[0].fd == -1) strerr_diefu1sys(111, "create selfpipe") ; - if (!sig_altignore(SIGPIPE)) strerr_diefu1sys(111, "ignore SIGPIPE") ; - { - sigset_t set ; - sigemptyset(&set) ; - sigaddset(&set, SIGCHLD) ; - sigaddset(&set, SIGTERM) ; - sigaddset(&set, SIGHUP) ; - sigaddset(&set, SIGQUIT) ; - sigaddset(&set, SIGABRT) ; - if (!selfpipe_trapset(&set)) strerr_diefu1sys(111, "trap signals") ; - } - if (flag1) - { - uint16_t port ; - uint16_t m = 0 ; - char ip[16] ; - char fmtport[UINT16_FMT] ; - if (socket_local6(0, ip, &port) == -1) - { - if (verbosity) strerr_warnwu1sys("socket_local6") ; - } - else m = uint16_fmt(fmtport, port) ; - fmtport[m++] = '\n' ; - allwrite(1, fmtport, m) ; - fd_close(1) ; - } - fmtlocalmaxconn[1+uint_fmt(fmtlocalmaxconn+1, localmaxconn)] = 0 ; - if (verbosity >= 2) - { - fmtmaxconn[1+uint_fmt(fmtmaxconn+1, maxconn)] = 0 ; - log_start() ; - log_status() ; - } - } - - { - pidip_t pidip_inyostack[maxconn] ; - ipnum_t ipnum_inyostack[maxconn] ; - size_t envlen = env_len((char const *const *)environ) ; - pidip = pidip_inyostack ; ipnum = ipnum_inyostack ; - - while (cont) - { - if (iopause_g(x, 1 + (numconn < maxconn), 0) < 0) - strerr_diefu1sys(111, "iopause") ; - - if (x[0].revents & IOPAUSE_EXCEPT) strerr_dief1x(111, "trouble with selfpipe") ; - if (x[0].revents & IOPAUSE_READ) { handle_signals() ; continue ; } - if (numconn < maxconn) - { - if (x[1].revents & IOPAUSE_EXCEPT) strerr_dief1x(111, "trouble with socket") ; - if (x[1].revents & IOPAUSE_READ) - { - char ip[16] ; - uint16_t port ; - int fd = socket_accept6(x[1].fd, ip, &port) ; - if (fd < 0) - { - if (verbosity) strerr_warnwu1sys("accept") ; - } - else - { - new_connection(fd, ip, port, argv, (char const *const *)environ, envlen) ; - fd_close(fd) ; - } - } - } - } - } - if (verbosity >= 2) log_exit() ; - return 0 ; -} diff --git a/src/conn-tools/s6-tcpserver4d.c b/src/conn-tools/s6-tcpserverd.c index 5982816..5a15dd9 100644 --- a/src/conn-tools/s6-tcpserver4d.c +++ b/src/conn-tools/s6-tcpserverd.c @@ -13,11 +13,11 @@ #include <skalibs/uint16.h> #include <skalibs/uint32.h> #include <skalibs/types.h> +#include <skalibs/ip46.h> #include <skalibs/allreadwrite.h> #include <skalibs/sgetopt.h> #include <skalibs/strerr.h> #include <skalibs/fmtscan.h> -#include <skalibs/diuint32.h> #include <skalibs/env.h> #include <skalibs/cspawn.h> #include <skalibs/djbunix.h> @@ -30,7 +30,13 @@ #define ABSOLUTE_MAXCONN 16384 -#define USAGE "s6-tcpserver4d [ -v verbosity ] [ -1 ] [ -c maxconn ] [ -C localmaxconn ] prog..." +#define USAGE "s6-tcpserverd [ -v verbosity ] [ -1 ] [ -c maxconn ] [ -C localmaxconn ] prog..." + +static uint32_t maxconn = 40 ; +static uint32_t localmaxconn = 40 ; +static uint32_t verbosity = 1 ; +static int cont = 1 ; +static int is6 ; typedef struct pidi_s pidi, *pidi_ref ; struct pidi_s @@ -39,21 +45,18 @@ struct pidi_s uint32_t i ; } ; -static uint32_t maxconn = 40 ; -static uint32_t localmaxconn = 40 ; -static uint32_t verbosity = 1 ; -static int cont = 1 ; - static genset *pidis ; #define PIDI(i) genset_p(pidi, pidis, (i)) #define numconn genset_n(pidis) +static avltreen *by_pid ; + static genset *ipnums ; -#define IPNUM(i) genset_p(diuint32, ipnums, (i)) +#define IP(i) (genset_p(char, ipnums, (i)) + 4) +#define NUMP(i) ((uint32_t *)genset_p(char, ipnums, (i))) static avltreen *by_ip ; -static avltreen *by_pid ; -static char fmtmaxconn[UINT32_FMT+1] = "/" ; -static char fmtlocalmaxconn[UINT32_FMT+1] = "/" ; +static char fmtmaxconn[UINT32_FMT + 1] = "/" ; +static char fmtlocalmaxconn[UINT32_FMT + 1] = "/" ; static inline void dieusage () @@ -68,8 +71,7 @@ static inline void X (void) static void *bypid_dtok (uint32_t d, void *aux) { - genset *g = aux ; - return &genset_p(pidi, g, d)->pid ; + return &genset_p(pidi, (genset *)aux, d)->pid ; } static int bypid_cmp (void const *a, void const *b, void *aux) @@ -82,21 +84,18 @@ static int bypid_cmp (void const *a, void const *b, void *aux) static void *byip_dtok (uint32_t d, void *aux) { - genset *g = aux ; - return &genset_p(diuint32, g, d)->left ; + return genset_p(char, (genset *)aux, d) + 4 ; } static int byip_cmp (void const *a, void const *b, void *aux) { (void)aux ; - uint32_t aa = *(uint32_t const *)a ; - uint32_t bb = *(uint32_t const *)b ; - return aa < bb ? -1 : aa > bb ; + return memcmp(a, b, is6 ? 16 : 4) ; } -static inline void log_start (void) +static inline void log_start (char const *fmtip, char const *fmtport) { - strerr_warni1x("starting") ; + strerr_warni4x("starting - bound to ip ", fmtip, " port ", fmtport) ; } static inline void log_exit (void) @@ -111,41 +110,37 @@ static void log_status (void) strerr_warni3x("status: ", fmt, fmtmaxconn) ; } -static inline void log_deny (uint32_t ip, uint16_t port, uint32_t num) +static inline void log_deny (char const *ip, uint16_t port, uint32_t num) { - char fmtip[UINT32_FMT] ; + char fmtip[IP46_FMT] ; char fmtport[UINT16_FMT] ; char fmtnum[UINT32_FMT] ; - fmtip[ip4_fmtu32(fmtip, ip)] = 0 ; + fmtip[is6 ? ip6_fmt(fmtip, ip) : ip4_fmt(fmtip, ip)] = 0 ; fmtport[uint16_fmt(fmtport, port)] = 0 ; fmtnum[uint32_fmt(fmtnum, num)] = 0 ; strerr_warni7sys("deny ", fmtip, ":", fmtport, " count ", fmtnum, fmtlocalmaxconn) ; } -static inline void log_accept (pid_t pid, uint32_t ip, uint16_t port, uint32_t num) +static inline void log_accept (pid_t pid, char const *ip, uint16_t port, uint32_t num) { - char fmtipport[IP4_FMT + UINT16_FMT + 1] ; - char fmtpid[PID_FMT] ; + char fmtip[IP46_FMT] ; + char fmtport[UINT16_FMT] ; char fmtnum[UINT32_FMT] ; - size_t n ; - n = ip4_fmtu32(fmtipport, ip) ; - fmtipport[n++] = ':' ; - n += uint16_fmt(fmtipport + n, port) ; - fmtipport[n] = 0 ; + char fmtpid[PID_FMT] ; + fmtip[is6 ? ip6_fmt(fmtip, ip) : ip4_fmt(fmtip, ip)] = 0 ; + fmtport[uint16_fmt(fmtport, port)] = 0 ; fmtnum[uint32_fmt(fmtnum, num)] = 0 ; fmtpid[pid_fmt(fmtpid, pid)] = 0 ; - strerr_warni7x("allow ", fmtipport, " pid ", fmtpid, " count ", fmtnum, fmtlocalmaxconn) ; + strerr_warni9x("allow ", fmtip, ":", fmtport, " pid ", fmtpid, " count ", fmtnum, fmtlocalmaxconn) ; } -static inline void log_close (pid_t pid, uint32_t ip, int w) +static inline void log_close (pid_t pid, char const *ip, int w) { char fmtpid[PID_FMT] ; - char fmtip[IP4_FMT] = "?" ; char fmtw[UINT_FMT] ; fmtpid[pid_fmt(fmtpid, pid)] = 0 ; - fmtip[ip4_fmtu32(fmtip, ip)] = 0 ; fmtw[uint_fmt(fmtw, WIFSIGNALED(w) ? WTERMSIG(w) : WEXITSTATUS(w))] = 0 ; - strerr_warni6x("end pid ", fmtpid, " ip ", fmtip, WIFSIGNALED(w) ? " signal " : " exitcode ", fmtw) ; + strerr_warni4x("end pid ", fmtpid, WIFSIGNALED(w) ? " signal " : " exitcode ", fmtw) ; } static int killthem_iter (void *data, void *aux) @@ -173,12 +168,13 @@ static inline void wait_children (void) if (avltreen_search(by_pid, &pid, &d)) { uint32_t i = PIDI(d)->i ; - uint32_t ip = IPNUM(i)->left ; + char ip[SKALIBS_IP_SIZE] ; + memcpy(ip, IP(i), is6 ? 16 : 4) ; avltreen_delete(by_pid, &pid) ; genset_delete(pidis, d) ; - if (!--IPNUM(i)->right) + if (!--*NUMP(i)) { - avltreen_delete(by_ip, &ip) ; + avltreen_delete(by_ip, ip) ; genset_delete(ipnums, i) ; } if (verbosity >= 2) @@ -207,7 +203,7 @@ static inline void handle_signals (void) case SIGHUP : { if (verbosity >= 2) - strerr_warni5x("received ", "SIGHUP,", " sending ", "SIGTERM+SIGCONT", " to all connections") ; + strerr_warni5x("received ", "SIGHUP,", " sending ", "SIGTERM and SIGCONT", " to all connections") ; killthem(SIGTERM) ; killthem(SIGCONT) ; break ; @@ -215,7 +211,7 @@ static inline void handle_signals (void) case SIGQUIT : { if (verbosity >= 2) - strerr_warni6x("received ", "SIGQUIT,", " sending ", "SIGTERM+SIGCONT", " to all connections", " and quitting") ; + strerr_warni6x("received ", "SIGQUIT,", " sending ", "SIGTERM and SIGCONT", " to all connections", " and quitting") ; cont = 0 ; killthem(SIGTERM) ; killthem(SIGCONT) ; @@ -233,28 +229,25 @@ static inline void handle_signals (void) } } -static inline void new_connection (int s, uint32_t ip, uint16_t port, char const *const *argv, char const *const *envp, size_t envlen) +static inline void new_connection (int s, char const *ip, uint16_t port, char const *const *argv, char const *const *envp, char *modifs, size_t m, size_t envlen) { - size_t m = 0 ; pid_t pid ; uint32_t d ; - uint32_t num = 0 ; - char fmt[47 + IP4_FMT + UINT16_FMT + UINT_FMT] ; - - if (avltreen_search(by_ip, &ip, &d)) num = IPNUM(d)->right ; + uint32_t num = avltreen_search(by_ip, ip, &d) ? *NUMP(d) : 0 ; if (num >= localmaxconn) { log_deny(ip, port, num) ; return ; } - memcpy(fmt + m, "PROTO=TCP\0TCPREMOTEIP=", 22) ; m += 22 ; - m += ip4_fmtu32(fmt + m, ip) ; - fmt[m++] = 0 ; - memcpy(fmt + m, "TCPREMOTEPORT=", 14) ; m += 14 ; - m += uint16_fmt(fmt + m, port) ; fmt[m++] = 0 ; - memcpy(fmt + m, "TCPCONNNUM=", 11) ; m += 11 ; - m += uint_fmt(fmt + m, num) ; fmt[m++] = 0 ; + m += is6 ? ip6_fmt(modifs + m, ip) : ip4_fmt(modifs + m, ip) ; + modifs[m++] = 0 ; + memcpy(modifs + m, "TCPREMOTEPORT=", 14) ; m += 14 ; + m += uint16_fmt(modifs + m, port) ; + modifs[m++] = 0 ; + memcpy(modifs + m, "TCPCONNNUM=", 11) ; m += 11 ; + m += uint32_fmt(modifs + m, num) ; + modifs[m++] = 0 ; { cspawn_fileaction fa[2] = @@ -262,22 +255,22 @@ static inline void new_connection (int s, uint32_t ip, uint16_t port, char const [0] = { .type = CSPAWN_FA_MOVE, .x = { .fd2 = { [0] = 0, [1] = s } } }, [1] = { .type = CSPAWN_FA_COPY, .x = { .fd2 = { [0] = 1, [1] = 0 } } } } ; - char const *newenvp[envlen + 5] ; - env_mergen(newenvp, envlen + 5, envp, envlen, fmt, m, 4) ; + char const *newenvp[envlen + 7] ; + env_mergen(newenvp, envlen + 7, envp, envlen, modifs, m, 6) ; pid = cspawn(argv[0], argv, newenvp, CSPAWN_FLAGS_SELFPIPE_FINISH, fa, 2) ; - } - if (!pid) - { - if (verbosity) strerr_warnwu2sys("spawn ", argv[0]) ; - return ; + if (!pid) + { + if (verbosity) strerr_warnwu2sys("spawn ", argv[0]) ; + return ; + } } - if (num) IPNUM(d)->right++ ; + if (num) (*NUMP(d))++ ; else { d = genset_new(ipnums) ; - IPNUM(d)->left = ip ; - IPNUM(d)->right = 1 ; + *NUMP(d) = 1 ; + memcpy(IP(d), ip, is6 ? 16 : 4) ; avltreen_insert(by_ip, d) ; } @@ -287,7 +280,7 @@ static inline void new_connection (int s, uint32_t ip, uint16_t port, char const avltreen_insert(by_pid, num) ; if (verbosity >= 2) { - log_accept(pid, ip, port, IPNUM(d)->right) ; + log_accept(pid, ip, port, *NUMP(d)) ; log_status() ; } } @@ -296,7 +289,7 @@ int main (int argc, char const *const *argv) { iopause_fd x[2] = { { .events = IOPAUSE_READ }, { .fd = 0, .events = IOPAUSE_READ | IOPAUSE_EXCEPT } } ; int flag1 = 0 ; - PROG = "s6-tcpserver4d" ; + PROG = "s6-tcpserverd" ; { subgetopt l = SUBGETOPT_ZERO ; for (;;) @@ -348,7 +341,8 @@ int main (int argc, char const *const *argv) } { - diuint32 ipnum_storage[maxconn] ; + /* Yo dawg, I herd u like stack allocations */ + char ipnum_storage[maxconn * (is6 ? 20 : 8)] ; uint32_t ipnum_freelist[maxconn] ; avlnode byip_storage[maxconn] ; uint32_t byip_freelist[maxconn] ; @@ -361,8 +355,12 @@ int main (int argc, char const *const *argv) avltreen byip_info ; avltreen bypid_info ; size_t envlen = env_len((char const *const *)environ) ; + size_t m = 21 ; + char ip[SKALIBS_IP_SIZE] ; + uint16_t port ; + char modifs[sizeof("PROTO=TCP TCPLOCALIP= TCPLOCALPORT= TCPREMOTEIP= TCPREMOTEPORT= TCPCONNNUM=") + 2 * (IP46_FMT + UINT16_FMT) + UINT32_FMT] = "PROTO=TCP\0TCPLOCALIP=" ; - GENSET_init(&ipnum_info, diuint32, ipnum_storage, ipnum_freelist, maxconn) ; + genset_init(&ipnum_info, ipnum_storage, ipnum_freelist, is6 ? 20 : 8, maxconn) ; GENSET_init(&pidi_info, pidi, pidi_storage, pidi_freelist, maxconn) ; avltreen_init(&byip_info, byip_storage, byip_freelist, maxconn, &byip_dtok, &byip_cmp, &ipnum_info) ; avltreen_init(&bypid_info, bypid_storage, bypid_freelist, maxconn, &bypid_dtok, &bypid_cmp, &pidi_info) ; @@ -371,51 +369,61 @@ int main (int argc, char const *const *argv) by_ip = &byip_info ; by_pid = &bypid_info ; - if (verbosity >= 2) - { - fmtmaxconn[1+uint32_fmt(fmtmaxconn+1, maxconn)] = 0 ; - log_start() ; - log_status() ; - } - - if (flag1) { - uint16_t port ; - uint16_t m = 0 ; - char ip[4] ; + size_t iplen, portlen ; + char fmtip[IP4_FMT] ; char fmtport[UINT16_FMT] ; - if (socket_local4(0, ip, &port) == -1) + ip46 loc ; + if (socket_local46(0, &loc, &port) == -1) + strerr_diefu1sys(111, "get local socket information") ; + is6 = ip46_is6(&loc) ; + memcpy(ip, loc.ip, is6 ? 16 : 4) ; + iplen = is6 ? ip6_fmt(fmtip, ip) : ip4_fmt(fmtip, ip) ; + portlen = uint16_fmt(fmtport, port) ; + memcpy(modifs + m, fmtip, iplen) ; m += iplen ; + memcpy(modifs + m, "\0TCPLOCALPORT=", 14) ; m += 14 ; + memcpy(modifs + m, fmtport, portlen) ; m += portlen ; + memcpy(modifs + m, "\0TCPREMOTEIP=", 13) ; m += 13 ; + + if (verbosity >= 2) { - if (verbosity) strerr_warnwu1sys("socket_local4") ; + fmtmaxconn[1 + uint32_fmt(fmtmaxconn+1, maxconn)] = 0 ; + log_start(fmtip, fmtport) ; + log_status() ; + } + + if (flag1) + { + fmtport[portlen] = '\n' ; + allwrite(1, fmtport, portlen + 1) ; + close(1) ; } - else m = uint16_fmt(fmtport, port) ; - fmtport[m++] = '\n' ; - allwrite(1, fmtport, m) ; - close(1) ; } while (cont) { - if (iopause_g(x, 1 + (numconn < maxconn), 0) < 0) strerr_diefu1sys(111, "iopause") ; + if (iopause_g(x, 1 + (numconn < maxconn), 0) == -1) + strerr_diefu1sys(111, "iopause") ; + if (x[0].revents & IOPAUSE_EXCEPT) strerr_dief1x(111, "trouble with selfpipe") ; - if (x[0].revents & IOPAUSE_READ) { handle_signals() ; continue ; } + if (x[0].revents & IOPAUSE_READ) + { + handle_signals() ; + continue ; + } if (numconn < maxconn) { if (x[1].revents & IOPAUSE_EXCEPT) strerr_dief1x(111, "trouble with socket") ; if (x[1].revents & IOPAUSE_READ) { - char packedip[4] ; - uint16_t port ; - int fd = socket_accept4(x[1].fd, packedip, &port) ; + int fd = is6 ? socket_accept6(x[1].fd, ip, &port) : socket_accept4(x[1].fd, ip, &port) ; if (fd == -1) { if (verbosity) strerr_warnwu1sys("accept") ; } else { - uint32_t ip ; - uint32_unpack_big(packedip, &ip) ; - new_connection(fd, ip, port, argv, (char const *const *)environ, envlen) ; + new_connection(fd, ip, port, argv, (char const *const *)environ, modifs, m, envlen) ; fd_close(fd) ; } } |