test updates

This commit is contained in:
Thomas Graf 2008-08-20 12:01:50 +02:00
parent 7211a83525
commit 562c5323af
4 changed files with 44 additions and 79 deletions

View 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"; \

View 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;
}
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));
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/addr", &change_cb, &ac)) < 0)
fatal(err, "Unable to add cache route/addr: %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;
}
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;
}

View 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 (hdr == NULL)
fatal(ENOMEM, "Unable to write genl header");
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 ((err = nla_put_u32(msg, CTRL_ATTR_FAMILY_ID, GENL_ID_CTRL)) < 0)
fatal(err, "Unable to add attribute: %s", nl_geterror(err));
if (nl_send_auto_complete(h, msg) < 0) {
nl_perror("nl_send_auto_complete");
return -1;
}
if ((err = nl_send_auto_complete(sock, msg)) < 0)
fatal(err, "Unable to send message: %s", nl_geterror(err));
if (nl_recvmsgs_default(h) < 0) {
nl_perror("nl_recvmsgs_def");
return -1;
}
if ((err = nl_recvmsgs_default(sock)) < 0)
fatal(err, "Unable to receive message: %s", nl_geterror(err));
nlmsg_free(msg);
nl_close(h);
nl_close(sock);
nl_socket_free(sock);
return 0;
}

View 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;