From: Willy Tarreau Date: Thu, 10 Jul 2025 12:43:28 +0000 (+0200) Subject: CLEANUP: server: add server_find_by_addr() X-Git-Tag: v3.3-dev4~89 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=616c10f6087ddb409438d2a46320c99a5477d46c;p=thirdparty%2Fhaproxy.git CLEANUP: server: add server_find_by_addr() Server lookup by address requires locking and manipulation of the tree from user code. Let's provide server_find_by_addr() which does that for us. --- diff --git a/include/haproxy/server.h b/include/haproxy/server.h index b1b1588eb..15d4c7e0c 100644 --- a/include/haproxy/server.h +++ b/include/haproxy/server.h @@ -61,6 +61,7 @@ const char *srv_update_check_addr_port(struct server *s, const char *addr, const const char *srv_update_agent_addr_port(struct server *s, const char *addr, const char *port); struct server *server_find_by_id_unique(struct proxy *bk, int id, uint32_t rid); struct server *server_find_by_name(struct proxy *px, const char *name); +struct server *server_find_by_addr(struct proxy *px, const char *addr); struct server *server_find(struct proxy *bk, const char *name); struct server *server_find_unique(struct proxy *bk, const char *name, uint32_t rid); struct server *server_find_best_match(struct proxy *bk, char *name, int id, int *diff); diff --git a/src/server.c b/src/server.c index f92787483..c77ff5e48 100644 --- a/src/server.c +++ b/src/server.c @@ -3994,6 +3994,23 @@ struct server *server_find_by_name(struct proxy *px, const char *name) return cursrv; } +/* + * This function returns the server with a matching address within selected + * proxy, or NULL if not found. The proxy lock is taken for reads during this + * operation since we don't want the address to change under us. + */ +struct server *server_find_by_addr(struct proxy *px, const char *addr) +{ + struct ebpt_node *node; + struct server *cursrv; + + HA_RWLOCK_RDLOCK(PROXY_LOCK, &px->lock); + node = ebis_lookup(&px->used_server_addr, addr); + cursrv = node ? container_of(node, struct server, addr_node) : NULL; + HA_RWLOCK_RDUNLOCK(PROXY_LOCK, &px->lock); + return cursrv; +} + /* Returns a pointer to the first server matching either name , or id * if starts with a '#'. NULL is returned if no match is found. * the lookup is performed in the backend