From af57bb123f933e8c11a58613d382ae15823206c6 Mon Sep 17 00:00:00 2001 From: Felix Fietkau Date: Mon, 4 Dec 2023 20:20:45 +0100 Subject: [PATCH] socket: add debug callbacks for rx/tx Signed-off-by: Felix Fietkau --- include/netlink/socket.h | 19 +++++++++++++++++++ nl.c | 9 +++++++++ 2 files changed, 28 insertions(+) diff --git a/include/netlink/socket.h b/include/netlink/socket.h index bcb934c..9ddd42c 100644 --- a/include/netlink/socket.h +++ b/include/netlink/socket.h @@ -26,6 +26,8 @@ extern "C" { #define NL_NO_AUTO_ACK (1<<4) struct nl_cb; + +typedef void (*nl_debug_cb)(void *priv, const void *data, size_t len); struct nl_sock { struct sockaddr_nl s_local; @@ -36,6 +38,11 @@ struct nl_sock unsigned int s_seq_expect; int s_flags; struct nl_cb * s_cb; + + nl_debug_cb s_debug_tx_cb; + nl_debug_cb s_debug_rx_cb; + void * s_debug_tx_priv; + void * s_debug_rx_priv; }; @@ -56,6 +63,18 @@ extern void nl_socket_disable_seq_check(struct nl_sock *); extern int nl_socket_set_nonblocking(struct nl_sock *); +static inline void nl_socket_set_tx_debug_cb(struct nl_sock *sk, nl_debug_cb cb, void *priv) +{ + sk->s_debug_tx_cb = cb; + sk->s_debug_tx_priv = priv; +} + +static inline void nl_socket_set_rx_debug_cb(struct nl_sock *sk, nl_debug_cb cb, void *priv) +{ + sk->s_debug_rx_cb = cb; + sk->s_debug_rx_priv = priv; +} + /** * Use next sequence number * @arg sk Netlink socket. diff --git a/nl.c b/nl.c index 32d26a3..fee0529 100644 --- a/nl.c +++ b/nl.c @@ -192,6 +192,9 @@ int nl_sendto(struct nl_sock *sk, void *buf, size_t size) { int ret; + if (sk->s_debug_tx_cb) + sk->s_debug_tx_cb(sk->s_debug_tx_priv, buf, size); + ret = sendto(sk->s_fd, buf, size, 0, (struct sockaddr *) &sk->s_peer, sizeof(sk->s_peer)); if (ret < 0) @@ -227,6 +230,9 @@ int nl_sendmsg(struct nl_sock *sk, struct nl_msg *msg, struct msghdr *hdr) if (nl_cb_call(cb, NL_CB_MSG_OUT, msg) != NL_OK) return 0; + if (sk->s_debug_tx_cb) + sk->s_debug_tx_cb(sk->s_debug_tx_priv, iov.iov_base, iov.iov_len); + ret = sendmsg(sk->s_fd, hdr, 0); if (ret < 0) return -nl_syserr2nlerr(errno); @@ -466,6 +472,9 @@ retry: } } + if (sk->s_debug_rx_cb) + sk->s_debug_rx_cb(sk->s_debug_rx_priv, *buf, n); + free(msg.msg_control); return n; -- 2.30.2