]> git.ipfire.org Git - thirdparty/haproxy.git/commitdiff
DEV: tcploop: make it possible to change the target address of a connect()
authorWilly Tarreau <w@1wt.eu>
Tue, 7 Jun 2022 09:46:57 +0000 (11:46 +0200)
committerWilly Tarreau <w@1wt.eu>
Wed, 8 Jun 2022 12:42:15 +0000 (14:42 +0200)
Sometimes it's more convenient to be able to specify where to connect on
the connect() statement, let's make it possible to pass it in argument to
the C command.

dev/tcploop/tcploop.c

index 1ac543b48eee97100e2c1908ffe4a1878c547a1d..5e8b39b0a325f9e232f5662e21ff081281adc25e 100644 (file)
@@ -99,7 +99,7 @@ __attribute__((noreturn)) void usage(int code, const char *arg0)
            "actions :\n"
            "  A[<count>]   : Accepts <count> incoming sockets and closes count-1\n"
            "                 Note: fd=accept(fd)\n"
-           "  C            : Connects to ip:port\n"
+           "  C[[ip]:port] : Connects to ip:port or default ones if unspecified.\n"
            "                 Note: fd=socket,connect(fd)\n"
            "  D            : Disconnect (connect to AF_UNSPEC)\n"
            "  E[<size>]    : Echo this amount of bytes. 0=infinite. unset=any amount.\n"
@@ -236,7 +236,7 @@ void sig_handler(int sig)
 /* converts str in the form [[<ipv4>|<ipv6>|<hostname>]:]port to struct sockaddr_storage.
  * Returns < 0 with err set in case of error.
  */
-int addr_to_ss(char *str, struct sockaddr_storage *ss, struct err_msg *err)
+int addr_to_ss(const char *str, struct sockaddr_storage *ss, struct err_msg *err)
 {
        char *port_str;
        int port;
@@ -418,8 +418,17 @@ int tcp_accept(int sock, const char *arg)
 /* Try to establish a new connection to <sa>. Return the fd or -1 in case of error */
 int tcp_connect(const struct sockaddr_storage *sa, const char *arg)
 {
+       struct sockaddr_storage conn_addr;
        int sock;
 
+       if (arg[1]) {
+               struct err_msg err;
+
+               if (addr_to_ss(arg + 1, &conn_addr, &err) < 0)
+                       die(1, "%s\n", err.msg);
+               sa = &conn_addr;
+       }
+
        sock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
        if (sock < 0)
                return -1;