From 932d8d4fc4f991341c641bf38b8e3865f2e71943 Mon Sep 17 00:00:00 2001 From: Pablo Neira Ayuso Date: Fri, 24 Dec 2021 16:25:20 +0100 Subject: conntrack: add nfct_mnl_talk() and nfct_mnl_recv() helper functions Add helper function to consolidate nfct_mnl_dump() and nfct_mnl_get(). Signed-off-by: Pablo Neira Ayuso --- src/conntrack.c | 54 ++++++++++++++++++++++++++++++++++-------------------- 1 file changed, 34 insertions(+), 20 deletions(-) (limited to 'src') diff --git a/src/conntrack.c b/src/conntrack.c index 5bd3cb5..067ae41 100644 --- a/src/conntrack.c +++ b/src/conntrack.c @@ -2440,20 +2440,11 @@ static void nfct_mnl_socket_close(void) mnl_socket_close(sock.mnl); } -static int -nfct_mnl_dump(uint16_t subsys, uint16_t type, mnl_cb_t cb, - struct ct_cmd *cmd, const struct nfct_filter_dump *filter_dump) +static int nfct_mnl_recv(const struct nlmsghdr *nlh, mnl_cb_t cb, void *data) { - uint8_t family = cmd ? cmd->family : AF_UNSPEC; char buf[MNL_SOCKET_BUFFER_SIZE]; - struct nlmsghdr *nlh; int res; - nlh = nfct_mnl_nlmsghdr_put(buf, subsys, type, family); - - if (filter_dump) - nfct_nlmsg_build_filter(nlh, filter_dump); - res = mnl_socket_sendto(sock.mnl, nlh, nlh->nlmsg_len); if (res < 0) return res; @@ -2461,7 +2452,7 @@ nfct_mnl_dump(uint16_t subsys, uint16_t type, mnl_cb_t cb, res = mnl_socket_recvfrom(sock.mnl, buf, sizeof(buf)); while (res > 0) { res = mnl_cb_run(buf, res, nlh->nlmsg_seq, sock.portid, - cb, cmd); + cb, data); if (res <= MNL_CB_STOP) break; @@ -2472,23 +2463,46 @@ nfct_mnl_dump(uint16_t subsys, uint16_t type, mnl_cb_t cb, } static int -nfct_mnl_get(uint16_t subsys, uint16_t type, mnl_cb_t cb, uint8_t family) +nfct_mnl_dump(uint16_t subsys, uint16_t type, mnl_cb_t cb, + struct ct_cmd *cmd, const struct nfct_filter_dump *filter_dump) { + uint8_t family = cmd ? cmd->family : AF_UNSPEC; char buf[MNL_SOCKET_BUFFER_SIZE]; struct nlmsghdr *nlh; - int res; nlh = nfct_mnl_nlmsghdr_put(buf, subsys, type, family); - res = mnl_socket_sendto(sock.mnl, nlh, nlh->nlmsg_len); - if (res < 0) - return res; + if (filter_dump) + nfct_nlmsg_build_filter(nlh, filter_dump); - res = mnl_socket_recvfrom(sock.mnl, buf, sizeof(buf)); - if (res < 0) - return res; + return nfct_mnl_recv(nlh, cb, cmd); +} + +static int nfct_mnl_talk(const struct nlmsghdr *nlh, mnl_cb_t cb) +{ + char buf[MNL_SOCKET_BUFFER_SIZE]; + int ret; + + ret = mnl_socket_sendto(sock.mnl, nlh, nlh->nlmsg_len); + if (ret < 0) + return ret; + + ret = mnl_socket_recvfrom(sock.mnl, buf, sizeof(buf)); + if (ret < 0) + return ret; + + return mnl_cb_run(buf, ret, nlh->nlmsg_seq, sock.portid, cb, NULL); +} + +static int +nfct_mnl_get(uint16_t subsys, uint16_t type, mnl_cb_t cb, uint8_t family) +{ + char buf[MNL_SOCKET_BUFFER_SIZE]; + struct nlmsghdr *nlh; + + nlh = nfct_mnl_nlmsghdr_put(buf, subsys, type, family); - return mnl_cb_run(buf, res, nlh->nlmsg_seq, sock.portid, cb, NULL); + return nfct_mnl_talk(nlh, cb); } #define UNKNOWN_STATS_NUM 4 -- cgit v1.2.3