Use copies of private libnfnetlink and libmnl structs to move required info from one to the other. Move (now) common code in nfq_open() and nfq_open_nfnl() to static fill_nfnl_subsys_handle(). Signed-off-by: Duncan Roe <duncan_roe@xxxxxxxxxxxxxxx> --- src/libnetfilter_queue.c | 64 ++++++++++++++++++++-------------------- 1 file changed, 32 insertions(+), 32 deletions(-) diff --git a/src/libnetfilter_queue.c b/src/libnetfilter_queue.c index 2aba68d..03c56ca 100644 --- a/src/libnetfilter_queue.c +++ b/src/libnetfilter_queue.c @@ -403,6 +403,27 @@ int nfq_fd(struct nfq_handle *h) * @{ */ +static bool fill_nfnl_subsys_handle(struct nfq_handle *h) +{ + struct nfnl_callback pkt_cb = { + .call = __nfq_rcv_pkt, + .attr_count = NFQA_MAX, + }; + + /* Fill in nfnl subsys handle with code adapted from libnfnetlink */ + h->nfnlssh = &h->nfnlh->subsys[NFNL_SUBSYS_QUEUE]; + h->nfnlssh->cb = calloc(NFQNL_MSG_MAX, sizeof(*(h->nfnlssh->cb))); + if (!h->nfnlssh->cb) + return false; + h->nfnlssh->nfnlh = h->nfnlh; + h->nfnlssh->cb_count = NFQNL_MSG_MAX; + h->nfnlssh->subscriptions = 0; + h->nfnlssh->subsys_id = NFNL_SUBSYS_QUEUE; + pkt_cb.data = h; + memcpy(&h->nfnlssh->cb[NFQNL_MSG_PACKET], &pkt_cb, sizeof(pkt_cb)); + return true; +} + /** * nfq_open - open a nfqueue handler * @@ -416,10 +437,6 @@ int nfq_fd(struct nfq_handle *h) EXPORT_SYMBOL struct nfq_handle *nfq_open(void) { - struct nfnl_callback pkt_cb = { - .call = __nfq_rcv_pkt, - .attr_count = NFQA_MAX, - }; struct nfq_handle *h = malloc(sizeof(*h)); if (!h) @@ -442,22 +459,11 @@ struct nfq_handle *nfq_open(void) h->nfnlh->fd = h->nl->fd; h->nfnlh->local = h->nl->addr; h->nfnlh->peer.nl_family = AF_NETLINK; - //h->nfnlh->seq = time(NULL); h->nfnlh->rcv_buffer_size = NFNL_BUFFSIZE; - /* Fill in nfnl subsys handle with code adapted from libnfnetlink */ - h->nfnlssh = &h->nfnlh->subsys[NFNL_SUBSYS_QUEUE]; - h->nfnlssh->cb = calloc(NFQNL_MSG_MAX, sizeof(*(h->nfnlssh->cb))); - if (!h->nfnlssh->cb) + if (!fill_nfnl_subsys_handle(h)) goto err_close; - h->nfnlssh->nfnlh = h->nfnlh; - h->nfnlssh->cb_count = NFQNL_MSG_MAX; - h->nfnlssh->subscriptions = 0; - h->nfnlssh->subsys_id = NFNL_SUBSYS_QUEUE; - pkt_cb.data = h; - memcpy(&h->nfnlssh->cb[NFQNL_MSG_PACKET], &pkt_cb, sizeof(pkt_cb)); - return h; err_close: @@ -473,6 +479,7 @@ err_free: * @} */ +#define NFNL_F_SEQTRACK_ENABLED (1 << 0) /** * nfq_open_nfnl - open a nfqueue handler from a existing nfnetlink handler * \param nfnlh Netfilter netlink connection handle obtained by calling nfnl_open() @@ -486,12 +493,7 @@ err_free: EXPORT_SYMBOL struct nfq_handle *nfq_open_nfnl(struct nfnl_handle *nfnlh) { - struct nfnl_callback pkt_cb = { - .call = __nfq_rcv_pkt, - .attr_count = NFQA_MAX, - }; struct nfq_handle *h; - int err; h = malloc(sizeof(*h)); if (!h) @@ -499,24 +501,22 @@ struct nfq_handle *nfq_open_nfnl(struct nfnl_handle *nfnlh) memset(h, 0, sizeof(*h)); h->nfnlh = nfnlh; + h->nfnlh->seq = 0; + h->nfnlh->flags &= ~NFNL_F_SEQTRACK_ENABLED; - h->nfnlssh = nfnl_subsys_open(h->nfnlh, NFNL_SUBSYS_QUEUE, - NFQNL_MSG_MAX, 0); - if (!h->nfnlssh) { - /* FIXME: nfq_errno */ + h->nl = malloc(sizeof(*h->nl)); + if (!h->nl) goto out_free; - } + memset(h->nl, 0, sizeof(*h->nl)); + h->nl->fd = h->nfnlh->fd; + h->nl->addr = h->nfnlh->local; - pkt_cb.data = h; - err = nfnl_callback_register(h->nfnlssh, NFQNL_MSG_PACKET, &pkt_cb); - if (err < 0) { - nfq_errno = err; + if (!fill_nfnl_subsys_handle(h)) goto out_close; - } return h; out_close: - nfnl_subsys_close(h->nfnlssh); + mnl_socket_close(h->nl); out_free: free(h); return NULL; -- 2.35.8