From bb471dd06d7b90736ade28244ff765f5e6ffdcbc Mon Sep 17 00:00:00 2001 From: Willy Tarreau Date: Fri, 9 Aug 2024 21:12:23 +0200 Subject: [PATCH] WIP: create abnsz For now it's the same as abns. We'll need to modify sock_unix_addrcmp(), and a few other ones to support strnlen() when dealing with the \0. Let's check with Tristan's patch for this. --- include/haproxy/protocol-t.h | 3 ++- include/haproxy/sock_unix.h | 1 + include/haproxy/tools.h | 4 +++- src/proto_uxdg.c | 32 ++++++++++++++++++++++++++++ src/proto_uxst.c | 41 ++++++++++++++++++++++++++++++++++++ src/sock_unix.c | 13 ++++++++++++ src/tools.c | 11 +++++++++- 7 files changed, 102 insertions(+), 3 deletions(-) diff --git a/include/haproxy/protocol-t.h b/include/haproxy/protocol-t.h index 9b3b7f5593..2a44e1471b 100644 --- a/include/haproxy/protocol-t.h +++ b/include/haproxy/protocol-t.h @@ -41,7 +41,8 @@ struct connection; #define AF_CUST_SOCKPAIR (AF_MAX + 2) #define AF_CUST_RHTTP_SRV (AF_MAX + 3) #define AF_CUST_ABNS (AF_MAX + 4) -#define AF_CUST_MAX (AF_MAX + 5) +#define AF_CUST_ABNSZ (AF_MAX + 5) +#define AF_CUST_MAX (AF_MAX + 6) /* * Test in case AF_CUST_MAX overflows the sa_family_t (unsigned int) diff --git a/include/haproxy/sock_unix.h b/include/haproxy/sock_unix.h index 641fd823f3..39583e9f9b 100644 --- a/include/haproxy/sock_unix.h +++ b/include/haproxy/sock_unix.h @@ -30,6 +30,7 @@ extern struct proto_fam proto_fam_unix; extern struct proto_fam proto_fam_abns; +extern struct proto_fam proto_fam_abnsz; int sock_unix_addrcmp(const struct sockaddr_storage *a, const struct sockaddr_storage *b); int sock_unix_bind_receiver(struct receiver *rx, char **errmsg); diff --git a/include/haproxy/tools.h b/include/haproxy/tools.h index 91e6ce2d23..5ecf587972 100644 --- a/include/haproxy/tools.h +++ b/include/haproxy/tools.h @@ -713,7 +713,8 @@ static inline int is_addr(const struct sockaddr_storage *addr) /* WT: ideally we should use real_family(addr->ss_family) here, but we * have so few custom addresses that it's simple enough to test them all. */ - if (addr->ss_family == AF_UNIX || addr->ss_family == AF_CUST_ABNS || addr->ss_family == AF_CUST_SOCKPAIR) + if (addr->ss_family == AF_UNIX || addr->ss_family == AF_CUST_ABNS || + addr->ss_family == AF_CUST_ABNSZ || addr->ss_family == AF_CUST_SOCKPAIR) return 1; else return is_inet_addr(addr); @@ -753,6 +754,7 @@ static inline int get_addr_len(const struct sockaddr_storage *addr) return sizeof(struct sockaddr_in6); case AF_UNIX: case AF_CUST_ABNS: + case AF_CUST_ABNSZ: return sizeof(struct sockaddr_un); } return 0; diff --git a/src/proto_uxdg.c b/src/proto_uxdg.c index 89bdc5d6af..034c999951 100644 --- a/src/proto_uxdg.c +++ b/src/proto_uxdg.c @@ -97,8 +97,40 @@ struct protocol proto_abns_dgram = { .nb_receivers = 0, }; +/* Note: must not be declared as its list will be overwritten */ +struct protocol proto_abnsz_dgram = { + .name = "abnsz_dgram", + + /* connection layer */ + .xprt_type = PROTO_TYPE_DGRAM, + .listen = uxdg_bind_listener, + .enable = uxdg_enable_listener, + .disable = uxdg_disable_listener, + .add = default_add_listener, + .unbind = default_unbind_listener, + .suspend = default_suspend_listener, + .resume = default_resume_listener, + + /* binding layer */ + .rx_suspend = uxdg_suspend_receiver, + + /* address family */ + .fam = &proto_fam_abnsz, + + /* socket layer */ + .proto_type = PROTO_TYPE_DGRAM, + .sock_type = SOCK_DGRAM, + .sock_prot = 0, + .rx_enable = sock_enable, + .rx_disable = sock_disable, + .rx_unbind = sock_unbind, + .receivers = LIST_HEAD_INIT(proto_abnsz_dgram.receivers), + .nb_receivers = 0, +}; + INITCALL1(STG_REGISTER, protocol_register, &proto_uxdg); INITCALL1(STG_REGISTER, protocol_register, &proto_abns_dgram); +INITCALL1(STG_REGISTER, protocol_register, &proto_abnsz_dgram); /* This function tries to bind dgram unix socket listener. It may return a warning or * an error message in if the message is at most bytes long diff --git a/src/proto_uxst.c b/src/proto_uxst.c index f6109ab74e..979be28c71 100644 --- a/src/proto_uxst.c +++ b/src/proto_uxst.c @@ -124,8 +124,49 @@ struct protocol proto_abns_stream = { .nb_receivers = 0, }; +/* Note: must not be declared as its list will be overwritten */ +struct protocol proto_abnsz_stream = { + .name = "abnsz_stream", + + /* connection layer */ + .xprt_type = PROTO_TYPE_STREAM, + .listen = uxst_bind_listener, + .enable = uxst_enable_listener, + .disable = uxst_disable_listener, + .add = default_add_listener, + .unbind = default_unbind_listener, + .suspend = default_suspend_listener, + .resume = default_resume_listener, + .accept_conn = sock_accept_conn, + .ctrl_init = sock_conn_ctrl_init, + .ctrl_close = sock_conn_ctrl_close, + .connect = uxst_connect_server, + .drain = sock_drain, + .check_events = sock_check_events, + .ignore_events = sock_ignore_events, + + /* binding layer */ + .rx_suspend = uxst_suspend_receiver, + + /* address family */ + .fam = &proto_fam_abnsz, + + /* socket layer */ + .proto_type = PROTO_TYPE_STREAM, + .sock_type = SOCK_STREAM, + .sock_prot = 0, + .rx_enable = sock_enable, + .rx_disable = sock_disable, + .rx_unbind = sock_unbind, + .rx_listening = sock_accepting_conn, + .default_iocb = sock_accept_iocb, + + .nb_receivers = 0, +}; + INITCALL1(STG_REGISTER, protocol_register, &proto_uxst); INITCALL1(STG_REGISTER, protocol_register, &proto_abns_stream); +INITCALL1(STG_REGISTER, protocol_register, &proto_abnsz_stream); /******************************** * 1) low-level socket functions diff --git a/src/sock_unix.c b/src/sock_unix.c index 13e0c9146d..8785845b31 100644 --- a/src/sock_unix.c +++ b/src/sock_unix.c @@ -63,6 +63,19 @@ struct proto_fam proto_fam_abns = { .get_dst = sock_get_dst, }; +struct proto_fam proto_fam_abnsz = { + .name = "abnsz", + .sock_domain = AF_UNIX, + .sock_family = AF_CUST_ABNSZ, + .real_family = AF_UNIX, + .sock_addrlen = sizeof(struct sockaddr_un), + .l3_addrlen = sizeof(((struct sockaddr_un*)0)->sun_path), + .addrcmp = sock_unix_addrcmp, + .bind = sock_unix_bind_receiver, + .get_src = sock_get_src, + .get_dst = sock_get_dst, +}; + /* PLEASE NOTE for functions below: * * The address family SHOULD always be checked. In some cases a function will diff --git a/src/tools.c b/src/tools.c index 9fa77cfd5d..f68feed962 100644 --- a/src/tools.c +++ b/src/tools.c @@ -1045,6 +1045,11 @@ struct sockaddr_storage *str2sa_range(const char *str, int *port, int *low, int abstract = 1; ss.ss_family = AF_CUST_ABNS; } + else if (strncmp(str2, "abnsz@", 5) == 0) { + str2 += 6; + abstract = 1; + ss.ss_family = AF_CUST_ABNSZ; + } else if (strncmp(str2, "ip@", 3) == 0) { str2 += 3; ss.ss_family = AF_UNSPEC; @@ -1186,7 +1191,7 @@ struct sockaddr_storage *str2sa_range(const char *str, int *port, int *low, int goto out; } } - else if (ss.ss_family == AF_UNIX || ss.ss_family == AF_CUST_ABNS) { + else if (ss.ss_family == AF_UNIX || ss.ss_family == AF_CUST_ABNS || ss.ss_family == AF_CUST_ABNSZ) { struct sockaddr_un *un = (struct sockaddr_un *)&ss; int prefix_path_len; int max_path_len; @@ -1436,6 +1441,7 @@ char * sa2str(const struct sockaddr_storage *addr, int port, int map_ports) break; case AF_UNIX: case AF_CUST_ABNS: + case AF_CUST_ABNSZ: path = ((struct sockaddr_un *)addr)->sun_path; if (path[0] == '\0') { const int max_length = sizeof(struct sockaddr_un) - offsetof(struct sockaddr_un, sun_path) - 1; @@ -1884,6 +1890,7 @@ int addr_to_str(const struct sockaddr_storage *addr, char *str, int size) break; case AF_UNIX: case AF_CUST_ABNS: + case AF_CUST_ABNSZ: memcpy(str, "unix", 5); return addr->ss_family; default: @@ -1922,6 +1929,7 @@ int port_to_str(const struct sockaddr_storage *addr, char *str, int size) break; case AF_UNIX: case AF_CUST_ABNS: + case AF_CUST_ABNSZ: memcpy(str, "unix", 5); return addr->ss_family; default: @@ -6356,6 +6364,7 @@ const char *hash_ipanon(uint32_t scramble, char *ipstring, int hasport) case AF_UNIX: case AF_CUST_ABNS: + case AF_CUST_ABNSZ: return HA_ANON_STR(scramble, ipstring); break;