]> git.ipfire.org Git - thirdparty/haproxy.git/commitdiff
MINOR: listener: implement bind_conf_find_by_name()
authorWilliam Lallemand <wlallemand@haproxy.com>
Wed, 29 Oct 2025 10:43:33 +0000 (11:43 +0100)
committerWilliam Lallemand <wlallemand@haproxy.com>
Thu, 30 Oct 2025 09:37:42 +0000 (10:37 +0100)
Returns a pointer to the first bind_conf matching <name> in a frontend
<front>.

When name is prefixed by a @ (@<filename>:<linenum>), it tries to look
for the corresponding filename and line of the configuration file.

NULL is returned if no match is found.

include/haproxy/listener.h
src/listener.c

index d8aea274b6bb37d45acd178209a0f119e4b8d48a..cc32703c1ecde33001fcef765ed11b9b6a939d2f 100644 (file)
@@ -258,6 +258,12 @@ static inline uint accept_queue_ring_len(const struct accept_queue_ring *ring)
        return len;
 }
 
+/* Returns a pointer to the first bind_conf matching either name <name>, or
+ * filename:linenum in <name> if <name> begins with a '@'. NULL is returned if
+ * no match is found.
+ */
+struct bind_conf *bind_conf_find_by_name(struct proxy *front, const char *name);
+
 #endif /* _HAPROXY_LISTENER_H */
 
 /*
index 3bb4cf86216060c0e1387c0d81868e8fc2509d22..5c81fe0807e14e6a8503aaceecf3f7351ed06b02 100644 (file)
@@ -16,6 +16,8 @@
 #include <string.h>
 #include <unistd.h>
 
+#include <import/ist.h>
+
 #include <haproxy/acl.h>
 #include <haproxy/api.h>
 #include <haproxy/activity.h>
@@ -1944,6 +1946,63 @@ int bind_generate_guid(struct bind_conf *bind_conf)
        return 0;
 }
 
+/* Returns a pointer to the first bind_conf matching either name <name>, or
+ * filename:linenum <name> if <name> begins with a '@'. NULL is returned if no
+ * match is found.
+ */
+struct bind_conf *bind_conf_find_by_name(struct proxy *front, const char *name)
+{
+       struct bind_conf *bind_conf = NULL;
+       int linenum = -1;
+       struct ist filename = IST_NULL;
+
+       if (*name == '@') {
+               /* handle @filename:linenum */
+               char *endptr = NULL;
+               const char *p = name + 1;
+
+               filename.ptr = (char *)p;
+
+               while (*p != '\0' && *p != ':')
+                       p++;
+
+               filename.len = p - filename.ptr;
+
+               if (*p)
+                       p++;
+
+               linenum = strtol(p, &endptr, 10);
+               if (*endptr != '\0' || endptr == p) {
+                       linenum = -1;
+                       /* parsing error */
+               }
+               /* we're not using name for the next steps */
+               name = NULL;
+       }
+
+       /* loop on binds */
+       list_for_each_entry(bind_conf, &front->conf.bind, by_fe) {
+
+               /* if we are using a name, look at the name of the first listener,
+                * because bind_parse_name() sets the name on all listeners */
+               if (name) {
+                       struct listener *l;
+
+                       list_for_each_entry(l, &bind_conf->listeners, by_bind) {
+                               if (l->name && strcmp(l->name, name) == 0)
+                                       return bind_conf;
+                               /* we just need the first one */
+                               break;
+                       }
+               } else {
+                       if ((strncmp(bind_conf->file, filename.ptr, filename.len) == 0)
+                           && bind_conf->line == linenum)
+                               return bind_conf;
+               }
+       }
+       return NULL;
+}
+
 /*
  * Registers the bind keyword list <kwl> as a list of valid keywords for next
  * parsing sessions.