From: Amaury Denoyelle Date: Thu, 1 Dec 2022 16:46:45 +0000 (+0100) Subject: MINOR: tools: add port for ipcmp as optional criteria X-Git-Tag: v2.8-dev1~196 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=21e611dc894550293250cbacebc8e301ae90b124;p=thirdparty%2Fhaproxy.git MINOR: tools: add port for ipcmp as optional criteria Complete ipcmp() function with a new argument . If this argument is true, the function will compare port values besides IP addresses and return true only if both are identical. This commit will simplify QUIC connection migration detection. As such, it should be backported to 2.7. --- diff --git a/include/haproxy/tools.h b/include/haproxy/tools.h index c3549918cb..b2133b827c 100644 --- a/include/haproxy/tools.h +++ b/include/haproxy/tools.h @@ -775,11 +775,13 @@ extern void v4tov6(struct in6_addr *sin6_addr, struct in_addr *sin_addr); */ extern int v6tov4(struct in_addr *sin_addr, struct in6_addr *sin6_addr); -/* compare two struct sockaddr_storage and return: +/* compare two struct sockaddr_storage, including port if is true, + * and return: * 0 (true) if the addr is the same in both * 1 (false) if the addr is not the same in both + * -1 (unable) if one of the addr is not AF_INET* */ -int ipcmp(struct sockaddr_storage *ss1, struct sockaddr_storage *ss2); +int ipcmp(struct sockaddr_storage *ss1, struct sockaddr_storage *ss2, int check_port); /* compare a struct sockaddr_storage to a struct net_addr and return : * 0 (true) if is matching diff --git a/src/server.c b/src/server.c index 51ca0cdf3b..5938e35473 100644 --- a/src/server.c +++ b/src/server.c @@ -3314,7 +3314,7 @@ const char *srv_update_addr_port(struct server *s, const char *addr, const char /* applying ADDR changes if required and allowed * ipcmp returns 0 when both ADDR are the same */ - if (ipcmp(&s->addr, &sa) == 0) { + if (ipcmp(&s->addr, &sa, 0) == 0) { chunk_appendf(msg, "no need to change the addr"); goto port; } diff --git a/src/tools.c b/src/tools.c index a392fe573b..1f4fedaaaa 100644 --- a/src/tools.c +++ b/src/tools.c @@ -3323,12 +3323,13 @@ int v6tov4(struct in_addr *sin_addr, struct in6_addr *sin6_addr) return 0; } -/* compare two struct sockaddr_storage and return: +/* compare two struct sockaddr_storage, including port if is true, + * and return: * 0 (true) if the addr is the same in both * 1 (false) if the addr is not the same in both * -1 (unable) if one of the addr is not AF_INET* */ -int ipcmp(struct sockaddr_storage *ss1, struct sockaddr_storage *ss2) +int ipcmp(struct sockaddr_storage *ss1, struct sockaddr_storage *ss2, int check_port) { if ((ss1->ss_family != AF_INET) && (ss1->ss_family != AF_INET6)) return -1; @@ -3341,13 +3342,15 @@ int ipcmp(struct sockaddr_storage *ss1, struct sockaddr_storage *ss2) switch (ss1->ss_family) { case AF_INET: - return memcmp(&((struct sockaddr_in *)ss1)->sin_addr, + return (memcmp(&((struct sockaddr_in *)ss1)->sin_addr, &((struct sockaddr_in *)ss2)->sin_addr, - sizeof(struct in_addr)) != 0; + sizeof(struct in_addr)) != 0) || + (check_port && get_net_port(ss1) != get_net_port(ss2)); case AF_INET6: - return memcmp(&((struct sockaddr_in6 *)ss1)->sin6_addr, + return (memcmp(&((struct sockaddr_in6 *)ss1)->sin6_addr, &((struct sockaddr_in6 *)ss2)->sin6_addr, - sizeof(struct in6_addr)) != 0; + sizeof(struct in6_addr)) != 0) || + (check_port && get_net_port(ss1) != get_net_port(ss2)); } return 1;