DEV: tcploop: add minimal support for unix sockets
Since the tool permits to pass an FD bound for listening, it's convenient
to test haproxy's "bind fd@". Let's add support for UNIX sockets the same
way. -U needs to be passed to change the default address family, and the
address must contain a "/".
E.g.
$ dev/tcploop/tcploop -U /tmp/ux L Xi ./haproxy -f fd1.cfg
diff --git a/dev/tcploop/tcploop.c b/dev/tcploop/tcploop.c
index d8e9f0f..c091f10 100644
--- a/dev/tcploop/tcploop.c
+++ b/dev/tcploop/tcploop.c
@@ -32,6 +32,7 @@
#include <sys/stat.h>
#include <sys/time.h>
#include <sys/ioctl.h>
+#include <sys/un.h>
#include <sys/wait.h>
#ifdef __linux__
@@ -110,6 +111,7 @@
"options :\n"
" -v : verbose\n"
" -u : use UDP instead of TCP (limited)\n"
+ " -U : use UNIX instead of TCP (limited, addr must have one '/')\n"
" -t|-tt|-ttt : show time (msec / relative / absolute)\n"
" -e : use epoll instead of poll on Linux\n"
"actions :\n"
@@ -261,6 +263,14 @@
memset(ss, 0, sizeof(*ss));
+ /* if there's a slash it's a unix socket */
+ if (strchr(str, '/')) {
+ ((struct sockaddr_un *)ss)->sun_family = AF_UNIX;
+ strncpy(((struct sockaddr_un *)ss)->sun_path, str, sizeof(((struct sockaddr_un *)ss)->sun_path) - 1);
+ ((struct sockaddr_un *)ss)->sun_path[sizeof(((struct sockaddr_un *)ss)->sun_path)] = 0;
+ return 0;
+ }
+
/* look for the addr/port delimiter, it's the last colon. If there's no
* colon, it's 0:<port>.
*/
@@ -375,11 +385,11 @@
}
/* Create a new TCP socket for either listening or connecting */
-int tcp_socket()
+int tcp_socket(sa_family_t fam)
{
int sock;
- sock = socket(AF_INET, sock_type, sock_proto);
+ sock = socket(fam, sock_type, sock_proto);
if (sock < 0) {
perror("socket()");
return -1;
@@ -405,7 +415,7 @@
if (sock < 0) {
- sock = tcp_socket();
+ sock = tcp_socket(sa->ss_family);
if (sock < 0)
return sock;
}
@@ -513,7 +523,7 @@
}
if (sock < 0) {
- sock = tcp_socket();
+ sock = tcp_socket(sa->ss_family);
if (sock < 0)
return sock;
}
@@ -835,6 +845,9 @@
sock_type = SOCK_DGRAM;
sock_proto = IPPROTO_UDP;
}
+ else if (strcmp(argv[0], "-U") == 0) {
+ sock_proto = 0;
+ }
else if (strcmp(argv[0], "--") == 0)
break;
else