]> git.ipfire.org Git - thirdparty/haproxy.git/commitdiff
MINOR: protocol: support a generic way to call getsockopt() on a connection
authorWilly Tarreau <w@1wt.eu>
Wed, 24 Dec 2025 15:54:15 +0000 (16:54 +0100)
committerWilly Tarreau <w@1wt.eu>
Wed, 24 Dec 2025 17:38:51 +0000 (18:38 +0100)
It's regularly needed to call getsockopt() on a connection, but each
time the calling code has to do all the job by itself. This commit adds
a "get_opt()" callback on the protocol struct, that directly calls
getsockopt() on the connection's FD. A generic implementation for
standard sockets is provided, though QUIC would likely require a
different approach, or maybe a mapping. Due to the overlap between
IP/TCP/socket option values, it is necessary for the caller to indicate
both the level and the option. An abstraction of the level could be
done, but the caller would nonetheless have to know the optname, which
is generally defined in the same include files. So for now we'll
consider that this callback is only for very specific use.

The levels and optnames are purposely passed as signed ints so that it
is possible to further extend the API by using negative levels for
internal namespaces.

include/haproxy/protocol-t.h
include/haproxy/sock.h
src/sock.c

index 2a44e1471bc380e39a46264b34be100f9439b78f..882539ebfa3eac7dc0023f9b3f65f7760e22b640 100644 (file)
@@ -160,6 +160,7 @@ struct protocol {
        /* default I/O handler */
        void (*default_iocb)(int fd);                   /* generic I/O handler (typically accept callback) */
        int (*get_info)(struct connection *conn, long long int *info, int info_num);       /* Callback to get connection level statistical counters */
+       int (*get_opt)(const struct connection *conn, int level, int optname, void *buf, int size); /* getsockopt(level:optname) into buf:size */
 
        uint flags;                                     /* flags describing protocol support (PROTO_F_*) */
        uint nb_receivers;                              /* number of receivers (under proto_lock) */
index 9aa0147517118e4dbb42561b4fe514a57216529f..0642f7797938950f068c594452161a46181fbf2e 100644 (file)
@@ -46,6 +46,7 @@ struct connection *sock_accept_conn(struct listener *l, int *status);
 void sock_accept_iocb(int fd);
 void sock_conn_ctrl_init(struct connection *conn);
 void sock_conn_ctrl_close(struct connection *conn);
+int sock_conn_get_opt(const struct connection *conn, int level, int optname, void *buf, int size);
 void sock_conn_iocb(int fd);
 int sock_conn_check(struct connection *conn);
 int sock_drain(struct connection *conn);
index bda07024eb1f1e740ab77e007c6916e94a7caa9e..c1e88413fc37414dd3912a25820e3249465274bc 100644 (file)
@@ -905,6 +905,23 @@ void sock_conn_ctrl_close(struct connection *conn)
        conn->handle.fd = DEAD_FD_MAGIC;
 }
 
+/* call getsockopt() for <level> and <optname> on connection <conn>'s socket,
+ * store the result in <buf> for at most <size> bytes, and return the number
+ * of bytes read on success (which may be zero). Returns < 0 on error.
+ * Note that the recommended way to use the level is to pass IPPROTO_TCP for
+ * TCP_*, IPPROTO_UDP for UDP_*, IPPROTO_IP for IP_*, IPPROTO_IPV6 for IPV6_*,
+ * and SOL_SOCKET for UNIX sockets.
+ */
+int sock_conn_get_opt(const struct connection *conn, int level, int optname, void *buf, int size)
+{
+       socklen_t opt_len = size;
+
+       if (getsockopt(conn->handle.fd, level, optname, buf, &opt_len) == -1)
+               return -1;
+
+       return opt_len;
+}
+
 /* This is the callback which is set when a connection establishment is pending
  * and we have nothing to send. It may update the FD polling status to indicate
  * !READY. It returns 0 if it fails in a fatal way or needs to poll to go