]> granicus.if.org Git - libnl/commitdiff
test updates
authorThomas Graf <tgr@plip.localdomain>
Wed, 20 Aug 2008 10:01:50 +0000 (12:01 +0200)
committerThomas Graf <tgr@plip.localdomain>
Wed, 20 Aug 2008 10:01:50 +0000 (12:01 +0200)
tests/Makefile
tests/test-cache-mngr.c
tests/test-genl.c
tests/test-nf-cache-mngr.c

index b5cec347bb7de8c6543b27309da9951c0e0ccf37..168eeeb695931c6d2cdddd110adcbd76f9c03bf9 100644 (file)
@@ -23,7 +23,7 @@ $(TOOLS): ../src/utils.o
 
 test-%: test-%.c
        @echo "  LD $@"; \
-       $(CC) $(CFLAGS) -o $@ $< $(LDFLAGS)
+       $(CC) $(CFLAGS) -o $@ $< $(LDFLAGS) -lnl-genl
 
 clean:
        @echo "  CLEAN src"; \
index 9a3da2d71fc8e22dea291f2064af1a70374ca46a..777bce89fbc94682c59d644eb64941c128836d20 100644 (file)
@@ -7,7 +7,7 @@ static void change_cb(struct nl_cache *cache, struct nl_object *obj,
                      int action)
 {
        struct nl_dump_params dp = {
-               .dp_type = NL_DUMP_BRIEF,
+               .dp_type = NL_DUMP_LINE,
                .dp_fd = stdout,
        };
 
@@ -30,55 +30,42 @@ int main(int argc, char *argv[])
 {
        struct nl_cache_mngr *mngr;
        struct nl_cache *lc, *nc, *ac, *rc;
-       struct nl_handle *handle;
+       struct nl_sock *sock;
+       int err;
 
        signal(SIGINT, sigint);
 
-       nltool_init(argc, argv);
+       sock = nlt_alloc_socket();
+       err = nl_cache_mngr_alloc(sock, NETLINK_ROUTE, NL_AUTO_PROVIDE, &mngr);
+       if (err < 0)
+               fatal(err, "Unable to allocate cache manager: %s",
+                     nl_geterror(err));
 
-       handle = nltool_alloc_handle();
+       if ((err = nl_cache_mngr_add(mngr, "route/link", &change_cb, &lc)) < 0)
+               fatal(err, "Unable to add cache route/link: %s",
+                     nl_geterror(err));
 
-       mngr = nl_cache_mngr_alloc(handle, NETLINK_ROUTE, NL_AUTO_PROVIDE);
-       if (!mngr) {
-               nl_perror("nl_cache_mngr_alloc");
-               return -1;
-       }
-
-       lc = nl_cache_mngr_add(mngr, "route/link", &change_cb);
-       if (lc == NULL) {
-               nl_perror("nl_cache_mngr_add(route/link");
-               return -1;
-       }
+       if ((err = nl_cache_mngr_add(mngr, "route/neigh", &change_cb, &nc)) < 0)
+               fatal(err, "Unable to add cache route/neigh: %s",
+                     nl_geterror(err));
 
-       nc = nl_cache_mngr_add(mngr, "route/neigh", &change_cb);
-       if (nc == NULL) {
-               nl_perror("nl_cache_mngr_add(route/neigh");
-               return -1;
-       }
+       if ((err = nl_cache_mngr_add(mngr, "route/addr", &change_cb, &ac)) < 0)
+               fatal(err, "Unable to add cache route/addr: %s",
+                     nl_geterror(err));
 
-       ac = nl_cache_mngr_add(mngr, "route/addr", &change_cb);
-       if (ac == NULL) {
-               nl_perror("nl_cache_mngr_add(route/addr");
-               return -1;
-       }
-
-       rc = nl_cache_mngr_add(mngr, "route/route", &change_cb);
-       if (rc == NULL) {
-               nl_perror("nl_cache_mngr_add(route/route");
-               return -1;
-       }
+       if ((err = nl_cache_mngr_add(mngr, "route/route", &change_cb, &rc)) < 0)
+               fatal(err, "Unable to add cache route/route: %s",
+                     nl_geterror(err));
 
        while (!quit) {
                int err = nl_cache_mngr_poll(mngr, 5000);
-               if (err < 0 && err != -EINTR) {
-                       nl_perror("nl_cache_mngr_poll()");
-                       return -1;
-               }
+               if (err < 0 && err != -NLE_INTR)
+                       fatal(err, "Polling failed: %s", nl_geterror(err));
 
        }
 
        nl_cache_mngr_free(mngr);
-       nl_handle_destroy(handle);
+       nl_socket_free(sock);
 
        return 0;
 }
index e44b3fb579c5ab84298e1222b5edbbdae298516e..8bf60c5c626e9521e281ad65f83f2f19dbabaee3 100644 (file)
@@ -2,55 +2,35 @@
 
 int main(int argc, char *argv[])
 {
-       struct nl_handle *h;
+       struct nl_sock *sock;
        struct nl_msg *msg;
        void *hdr;
+       int err;
 
-       if (nltool_init(argc, argv) < 0)
-               return -1;
-
-       h = nltool_alloc_handle();
-       if (!h) {
-               nl_perror("nl_handle_alloc");
-               return -1;
-       }
-
-       if (genl_connect(h) < 0) {
-               nl_perror("genl_connect");
-               return -1;
-       }
+       sock = nlt_alloc_socket();
+       nlt_connect(sock, NETLINK_GENERIC);
 
        msg = nlmsg_alloc();
-       if (msg == NULL) {
-               nl_perror("nlmsg_alloc");
-               return -1;
-       }
+       if (msg == NULL)
+               fatal(NLE_NOMEM, "Unable to allocate netlink message");
 
        hdr = genlmsg_put(msg, NL_AUTO_PID, NL_AUTO_SEQ, GENL_ID_CTRL,
                          0, 0, CTRL_CMD_GETFAMILY, 1);
-       if (hdr == NULL) {
-               nl_perror("genlmsg_put");
-               return -1;
-       }
-
-       if (nla_put_u32(msg, CTRL_ATTR_FAMILY_ID, GENL_ID_CTRL) < 0) {
-               nl_perror("nla_put_u32(CTRL_ATTR_FAMILY_ID)");
-               return -1;
-       }
-
-       if (nl_send_auto_complete(h, msg) < 0) {
-               nl_perror("nl_send_auto_complete");
-               return -1;
-       }
-
-       if (nl_recvmsgs_default(h) < 0) {
-               nl_perror("nl_recvmsgs_def");
-               return -1;
-       }
+       if (hdr == NULL)
+               fatal(ENOMEM, "Unable to write genl header");
 
-       nlmsg_free(msg);
+       if ((err = nla_put_u32(msg, CTRL_ATTR_FAMILY_ID, GENL_ID_CTRL)) < 0)
+               fatal(err, "Unable to add attribute: %s", nl_geterror(err));
 
-       nl_close(h);
+       if ((err = nl_send_auto_complete(sock, msg)) < 0)
+               fatal(err, "Unable to send message: %s", nl_geterror(err));
+
+       if ((err = nl_recvmsgs_default(sock)) < 0)
+               fatal(err, "Unable to receive message: %s", nl_geterror(err));
+
+       nlmsg_free(msg);
+       nl_close(sock);
+       nl_socket_free(sock);
 
        return 0;
 }
index 86cbabb017e3c5de0eb3fb270211fd9947a6895b..05485bf639367c97ca867eae8e24f37a9ba625bf 100644 (file)
@@ -12,7 +12,7 @@ static void change_cb(struct nl_cache *cache, struct nl_object *obj,
        if (!nl_addr_cmp(hack, nfnl_ct_get_src(ct, 1)) ||
            !nl_addr_cmp(hack, nfnl_ct_get_dst(ct, 1))) {
                struct nl_dump_params dp = {
-                       .dp_type = NL_DUMP_BRIEF,
+                       .dp_type = NL_DUMP_LINE,
                        .dp_fd = stdout,
                };
 
@@ -24,14 +24,12 @@ static void change_cb(struct nl_cache *cache, struct nl_object *obj,
 int main(int argc, char *argv[])
 {
        struct nl_cache_mngr *mngr;
-       struct nl_handle *handle;
+       struct nl_sock *sock;
        struct nl_cache *ct;
 
-       nltool_init(argc, argv);
+       sock = nlt_socket_alloc();
 
-       handle = nltool_alloc_handle();
-
-       mngr = nl_cache_mngr_alloc(handle, NETLINK_NETFILTER, NL_AUTO_PROVIDE);
+       mngr = nl_cache_mngr_alloc(sock, NETLINK_NETFILTER, NL_AUTO_PROVIDE);
        if (!mngr) {
                nl_perror("nl_cache_mngr_alloc");
                return -1;