From: Willy Tarreau Date: Fri, 9 Aug 2024 18:30:31 +0000 (+0200) Subject: MINOR: protocol: add a family lookup X-Git-Tag: v3.1-dev6~6 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=ba4a416c66cee8f33bc4d877868e69470901ebad;p=thirdparty%2Fhaproxy.git MINOR: protocol: add a family lookup At plenty of places we have access to an address family which may include some custom addresses but we cannot simply convert them to the real families without performing some random protocol lookups. Let's simply add a proto_fam table like we have for the protocols. The protocols could even be indexed there, but for now it's not worth it. --- diff --git a/include/haproxy/protocol.h b/include/haproxy/protocol.h index 828093d982..a6c50ceb5f 100644 --- a/include/haproxy/protocol.h +++ b/include/haproxy/protocol.h @@ -28,6 +28,7 @@ /* [AF][sock_dgram][ctrl_dgram] */ extern struct protocol *__protocol_by_family[AF_CUST_MAX][PROTO_NUM_TYPES][2]; +extern const struct proto_fam *__proto_fam_by_family[AF_CUST_MAX]; __decl_thread(extern HA_SPINLOCK_T proto_lock); /* Registers the protocol */ @@ -101,6 +102,17 @@ static inline struct protocol *protocol_lookup(int family, enum proto_type proto return NULL; } +/* returns the proto_fam that matches ss_family. This supports custom address + * families so it is suitable for use with ss_family as found in various config + * element addresses. + */ +static inline const struct proto_fam *proto_fam_lookup(int ss_family) +{ + if (ss_family >= 0 && ss_family < AF_CUST_MAX) + return __proto_fam_by_family[ss_family]; + return NULL; +} + #endif /* _HAPROXY_PROTOCOL_H */ /* diff --git a/src/protocol.c b/src/protocol.c index 69f4595bb2..516c020d1c 100644 --- a/src/protocol.c +++ b/src/protocol.c @@ -28,6 +28,7 @@ /* List head of all registered protocols */ static struct list protocols = LIST_HEAD_INIT(protocols); struct protocol *__protocol_by_family[AF_CUST_MAX][PROTO_NUM_TYPES][2] __read_mostly = { }; +const struct proto_fam *__proto_fam_by_family[AF_CUST_MAX] = { }; /* This is the global spinlock we may need to register/unregister listeners or * protocols. Its main purpose is in fact to serialize the rare stop/deinit() @@ -48,6 +49,7 @@ void protocol_register(struct protocol *proto) __protocol_by_family[sock_family] [proto->proto_type] [proto->xprt_type == PROTO_TYPE_DGRAM] = proto; + __proto_fam_by_family[sock_family] = proto->fam; HA_SPIN_UNLOCK(PROTO_LOCK, &proto_lock); }