]> git.ipfire.org Git - thirdparty/haproxy.git/commitdiff
MINOR: protocol: add a family lookup
authorWilly Tarreau <w@1wt.eu>
Fri, 9 Aug 2024 18:30:31 +0000 (20:30 +0200)
committerWilly Tarreau <w@1wt.eu>
Wed, 21 Aug 2024 14:46:15 +0000 (16:46 +0200)
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.

include/haproxy/protocol.h
src/protocol.c

index 828093d98241c9c87626adcc99dd352ae116f2bd..a6c50ceb5f2f673677633c38477f90f30a137310 100644 (file)
@@ -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 <proto> */
@@ -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 */
 
 /*
index 69f4595bb23c9353f2106ab58c326350329a0901..516c020d1c077ad4f9ae4bbdd47cb83496283651 100644 (file)
@@ -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);
 }