diff options
author | Thomas Graf <tgr@lsx.localdomain> | 2008-05-22 22:02:02 (GMT) |
---|---|---|
committer | Thomas Graf <tgr@lsx.localdomain> | 2008-05-22 22:02:02 (GMT) |
commit | 10cf5a586c149fdb7e2639000dbfae5e6f8522a5 (patch) | |
tree | 399a15d767fd15b85c92be2e70b748645eeb983e /src/nf-queue.c | |
parent | 337fbd24cad1f5cf9c8b4287a75f2c69f088adce (diff) | |
download | libnl-10cf5a586c149fdb7e2639000dbfae5e6f8522a5.zip libnl-10cf5a586c149fdb7e2639000dbfae5e6f8522a5.tar.gz libnl-10cf5a586c149fdb7e2639000dbfae5e6f8522a5.tar.bz2 |
New set of libnl tools
Converts all tools to the API changes and improves the useability by
introducing regular options and long options.
Diffstat (limited to 'src/nf-queue.c')
-rw-r--r-- | src/nf-queue.c | 107 |
1 files changed, 27 insertions, 80 deletions
diff --git a/src/nf-queue.c b/src/nf-queue.c index 1420273..4e17f70 100644 --- a/src/nf-queue.c +++ b/src/nf-queue.c @@ -9,16 +9,10 @@ * Copyright (c) 2007, 2008 Patrick McHardy <kaber@trash.net> */ -#include <sys/types.h> -#include <linux/netfilter.h> -#include <linux/netfilter/nfnetlink_queue.h> -#include "utils.h" -#include <netlink/netfilter/nfnl.h> -#include <netlink/netfilter/queue.h> -#include <netlink/netfilter/queue_msg.h> +#include "queue-utils.h" -static struct nl_handle *nfnlh; +static struct nl_sock *nf_sock; static void obj_input(struct nl_object *obj, void *arg) { @@ -31,7 +25,7 @@ static void obj_input(struct nl_object *obj, void *arg) nfnl_queue_msg_set_verdict(msg, NF_ACCEPT); nl_object_dump(obj, &dp); - nfnl_queue_msg_send_verdict(nfnlh, msg); + nfnl_queue_msg_send_verdict(nf_sock, msg); } static int event_input(struct nl_msg *msg, void *arg) @@ -45,7 +39,7 @@ static int event_input(struct nl_msg *msg, void *arg) int main(int argc, char *argv[]) { - struct nl_handle *rtnlh; + struct nl_sock *rt_sock; struct nl_cache *link_cache; struct nfnl_queue *queue; enum nfnl_queue_copy_mode copy_mode; @@ -53,16 +47,9 @@ int main(int argc, char *argv[]) int err = 1; int family; - if (nltool_init(argc, argv) < 0) - return -1; - - nfnlh = nltool_alloc_handle(); - if (nfnlh == NULL) - return -1; - - nl_disable_sequence_check(nfnlh); - - nl_socket_modify_cb(nfnlh, NL_CB_VALID, NL_CB_CUSTOM, event_input, NULL); + nf_sock = nlt_alloc_socket(); + nl_disable_sequence_check(nf_sock); + nl_socket_modify_cb(nf_sock, NL_CB_VALID, NL_CB_CUSTOM, event_input, NULL); if ((argc > 1 && !strcasecmp(argv[1], "-h")) || argc < 3) { printf("Usage: nf-queue family group [ copy_mode ] " @@ -70,38 +57,24 @@ int main(int argc, char *argv[]) return 2; } - if (nfnl_connect(nfnlh) < 0) { - fprintf(stderr, "%s\n", nl_geterror()); - goto errout; - } + nlt_connect(nf_sock, NETLINK_NETFILTER); - family = nl_str2af(argv[1]); - if (family == AF_UNSPEC) { - fprintf(stderr, "Unknown family: %s\n", argv[1]); - goto errout; - } + if ((family = nl_str2af(argv[1])) == AF_UNSPEC) + fatal(NLE_INVAL, "Unknown family \"%s\"", argv[1]); - nfnl_queue_pf_unbind(nfnlh, family); - if (nfnl_queue_pf_bind(nfnlh, family) < 0) { - fprintf(stderr, "%s\n", nl_geterror()); - goto errout; - } - - queue = nfnl_queue_alloc(); - if (queue == NULL) { - fprintf(stderr, "%s\n", nl_geterror()); - goto errout; - } + nfnl_queue_pf_unbind(nf_sock, family); + if ((err = nfnl_queue_pf_bind(nf_sock, family)) < 0) + fatal(err, "Unable to bind logger: %s", nl_geterror(err)); + queue = nlt_alloc_queue(); nfnl_queue_set_group(queue, atoi(argv[2])); copy_mode = NFNL_QUEUE_COPY_PACKET; if (argc > 3) { copy_mode = nfnl_queue_str2copy_mode(argv[3]); - if (copy_mode < 0) { - fprintf(stderr, "%s\n", nl_geterror()); - goto errout; - } + if (copy_mode < 0) + fatal(copy_mode, "Unable to parse copy mode \"%s\": %s", + argv[3], nl_geterror(copy_mode)); } nfnl_queue_set_copy_mode(queue, copy_mode); @@ -110,27 +83,12 @@ int main(int argc, char *argv[]) copy_range = atoi(argv[4]); nfnl_queue_set_copy_range(queue, copy_range); - if (nfnl_queue_create(nfnlh, queue) < 0) { - fprintf(stderr, "%s\n", nl_geterror()); - goto errout; - } + if ((err = nfnl_queue_create(nf_sock, queue)) < 0) + fatal(err, "Unable to bind queue: %s", nl_geterror(err)); - rtnlh = nltool_alloc_handle(); - if (rtnlh == NULL) { - goto errout_close; - } - - if (nl_connect(rtnlh, NETLINK_ROUTE) < 0) { - fprintf(stderr, "%s\n", nl_geterror()); - goto errout; - } - - if ((link_cache = rtnl_link_alloc_cache(rtnlh)) == NULL) { - fprintf(stderr, "%s\n", nl_geterror()); - goto errout_close; - } - - nl_cache_mngt_provide(link_cache); + rt_sock = nlt_alloc_socket(); + nlt_connect(rt_sock, NETLINK_ROUTE); + link_cache = nlt_alloc_link_cache(rt_sock); while (1) { fd_set rfds; @@ -138,10 +96,10 @@ int main(int argc, char *argv[]) FD_ZERO(&rfds); - maxfd = nffd = nl_socket_get_fd(nfnlh); + maxfd = nffd = nl_socket_get_fd(nf_sock); FD_SET(nffd, &rfds); - rtfd = nl_socket_get_fd(rtnlh); + rtfd = nl_socket_get_fd(rt_sock); FD_SET(rtfd, &rfds); if (maxfd < rtfd) maxfd = rtfd; @@ -151,22 +109,11 @@ int main(int argc, char *argv[]) if (retval) { if (FD_ISSET(nffd, &rfds)) - nl_recvmsgs_default(nfnlh); + nl_recvmsgs_default(nf_sock); if (FD_ISSET(rtfd, &rfds)) - nl_recvmsgs_default(rtnlh); + nl_recvmsgs_default(rt_sock); } } - nl_cache_mngt_unprovide(link_cache); - nl_cache_free(link_cache); - - nfnl_queue_put(queue); - - nl_close(rtnlh); - nl_handle_destroy(rtnlh); -errout_close: - nl_close(nfnlh); - nl_handle_destroy(nfnlh); -errout: - return err; + return 0; } |