From 3f69d23e0e4181ee91332da6060d943f82c698a0 Mon Sep 17 00:00:00 2001 From: Pablo Neira Ayuso Date: Tue, 19 Mar 2013 13:32:04 +0100 Subject: examples: add genl-group-events Signed-off-by: Pablo Neira Ayuso --- examples/genl/Makefile.am | 6 +++- examples/genl/genl-group-events.c | 63 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 68 insertions(+), 1 deletion(-) create mode 100644 examples/genl/genl-group-events.c (limited to 'examples/genl') diff --git a/examples/genl/Makefile.am b/examples/genl/Makefile.am index deaf18d..b4b7954 100644 --- a/examples/genl/Makefile.am +++ b/examples/genl/Makefile.am @@ -1,6 +1,10 @@ include $(top_srcdir)/Make_global.am -check_PROGRAMS = genl-family-get +check_PROGRAMS = genl-family-get \ + genl-group-events genl_family_get_SOURCES = genl-family-get.c genl_family_get_LDADD = ../../src/libmnl.la + +genl_group_events_SOURCES = genl-group-events.c +genl_group_events_LDADD = ../../src/libmnl.la diff --git a/examples/genl/genl-group-events.c b/examples/genl/genl-group-events.c new file mode 100644 index 0000000..d5f0a18 --- /dev/null +++ b/examples/genl/genl-group-events.c @@ -0,0 +1,63 @@ +/* This example is placed in the public domain. */ +#include +#include +#include +#include + +#include +#include + +static int group; + +static int data_cb(const struct nlmsghdr *nlh, void *data) +{ + printf("received event type=%d from genetlink group %d\n", + nlh->nlmsg_type, group); + return MNL_CB_OK; +} + +int main(int argc, char *argv[]) +{ + struct mnl_socket *nl; + char buf[MNL_SOCKET_BUFFER_SIZE]; + int ret; + + if (argc != 2) { + printf("%s [group]\n", argv[0]); + exit(EXIT_FAILURE); + } + group = atoi(argv[1]); + + nl = mnl_socket_open(NETLINK_GENERIC); + if (nl == NULL) { + perror("mnl_socket_open"); + exit(EXIT_FAILURE); + } + + if (mnl_socket_bind(nl, 0, MNL_SOCKET_AUTOPID) < 0) { + perror("mnl_socket_bind"); + exit(EXIT_FAILURE); + } + + if (mnl_socket_setsockopt(nl, NETLINK_ADD_MEMBERSHIP, &group, + sizeof(int)) < 0) { + perror("mnl_socket_setsockopt"); + exit(EXIT_FAILURE); + } + + ret = mnl_socket_recvfrom(nl, buf, sizeof(buf)); + while (ret > 0) { + ret = mnl_cb_run(buf, ret, 0, 0, data_cb, NULL); + if (ret <= 0) + break; + ret = mnl_socket_recvfrom(nl, buf, sizeof(buf)); + } + if (ret == -1) { + perror("error"); + exit(EXIT_FAILURE); + } + + mnl_socket_close(nl); + + return 0; +} -- cgit v1.2.3 From 73b9805968e430d4328f5eca78574b6c0987f2cf Mon Sep 17 00:00:00 2001 From: Ken-ichirou MATSUZAWA Date: Sat, 7 Dec 2013 20:23:10 +0900 Subject: examples: fix trivial error message Signed-off-by: Ken-ichirou MATSUZAWA Signed-off-by: Florian Westphal --- examples/genl/genl-family-get.c | 2 +- examples/netfilter/nf-log.c | 12 ++++++------ examples/netfilter/nf-queue.c | 14 +++++++------- examples/netfilter/nfct-create-batch.c | 4 ++-- examples/netfilter/nfct-daemon.c | 4 ++-- examples/netfilter/nfct-dump.c | 4 ++-- examples/rtnl/rtnl-addr-dump.c | 2 +- examples/rtnl/rtnl-link-dump.c | 4 ++-- examples/rtnl/rtnl-link-dump2.c | 4 ++-- examples/rtnl/rtnl-link-dump3.c | 2 +- examples/rtnl/rtnl-link-event.c | 2 +- examples/rtnl/rtnl-link-set.c | 6 +++--- examples/rtnl/rtnl-route-add.c | 2 +- examples/rtnl/rtnl-route-dump.c | 4 ++-- examples/rtnl/rtnl-route-event.c | 2 +- 15 files changed, 34 insertions(+), 34 deletions(-) (limited to 'examples/genl') diff --git a/examples/genl/genl-family-get.c b/examples/genl/genl-family-get.c index 50f7ea3..ba8de12 100644 --- a/examples/genl/genl-family-get.c +++ b/examples/genl/genl-family-get.c @@ -219,7 +219,7 @@ int main(int argc, char *argv[]) portid = mnl_socket_get_portid(nl); if (mnl_socket_sendto(nl, nlh, nlh->nlmsg_len) < 0) { - perror("mnl_socket_send"); + perror("mnl_socket_sendto"); exit(EXIT_FAILURE); } diff --git a/examples/netfilter/nf-log.c b/examples/netfilter/nf-log.c index a862912..901bd80 100644 --- a/examples/netfilter/nf-log.c +++ b/examples/netfilter/nf-log.c @@ -39,14 +39,14 @@ static int parse_attr_cb(const struct nlattr *attr, void *data) case NFULA_TIMESTAMP: if (mnl_attr_validate2(attr, MNL_TYPE_UNSPEC, sizeof(struct nfulnl_msg_packet_timestamp)) < 0) { - perror("mnl_attr_validate"); + perror("mnl_attr_validate2"); return MNL_CB_ERROR; } break; case NFULA_HWADDR: if (mnl_attr_validate2(attr, MNL_TYPE_UNSPEC, sizeof(struct nfulnl_msg_packet_hw)) < 0) { - perror("mnl_attr_validate"); + perror("mnl_attr_validate2"); return MNL_CB_ERROR; } break; @@ -174,28 +174,28 @@ int main(int argc, char *argv[]) nlh = nflog_build_cfg_pf_request(buf, NFULNL_CFG_CMD_PF_UNBIND); if (mnl_socket_sendto(nl, nlh, nlh->nlmsg_len) < 0) { - perror("mnl_socket_send"); + perror("mnl_socket_sendto"); exit(EXIT_FAILURE); } nlh = nflog_build_cfg_pf_request(buf, NFULNL_CFG_CMD_PF_BIND); if (mnl_socket_sendto(nl, nlh, nlh->nlmsg_len) < 0) { - perror("mnl_socket_send"); + perror("mnl_socket_sendto"); exit(EXIT_FAILURE); } nlh = nflog_build_cfg_request(buf, NFULNL_CFG_CMD_BIND, qnum); if (mnl_socket_sendto(nl, nlh, nlh->nlmsg_len) < 0) { - perror("mnl_socket_send"); + perror("mnl_socket_sendto"); exit(EXIT_FAILURE); } nlh = nflog_build_cfg_params(buf, NFULNL_COPY_PACKET, 0xFFFF, qnum); if (mnl_socket_sendto(nl, nlh, nlh->nlmsg_len) < 0) { - perror("mnl_socket_send"); + perror("mnl_socket_sendto"); exit(EXIT_FAILURE); } diff --git a/examples/netfilter/nf-queue.c b/examples/netfilter/nf-queue.c index c66611f..6b7c30e 100644 --- a/examples/netfilter/nf-queue.c +++ b/examples/netfilter/nf-queue.c @@ -39,14 +39,14 @@ static int parse_attr_cb(const struct nlattr *attr, void *data) case NFQA_TIMESTAMP: if (mnl_attr_validate2(attr, MNL_TYPE_UNSPEC, sizeof(struct nfqnl_msg_packet_timestamp)) < 0) { - perror("mnl_attr_validate"); + perror("mnl_attr_validate2"); return MNL_CB_ERROR; } break; case NFQA_HWADDR: if (mnl_attr_validate2(attr, MNL_TYPE_UNSPEC, sizeof(struct nfqnl_msg_packet_hw)) < 0) { - perror("mnl_attr_validate"); + perror("mnl_attr_validate2"); return MNL_CB_ERROR; } break; @@ -188,28 +188,28 @@ int main(int argc, char *argv[]) nlh = nfq_build_cfg_pf_request(buf, NFQNL_CFG_CMD_PF_UNBIND); if (mnl_socket_sendto(nl, nlh, nlh->nlmsg_len) < 0) { - perror("mnl_socket_send"); + perror("mnl_socket_sendto"); exit(EXIT_FAILURE); } nlh = nfq_build_cfg_pf_request(buf, NFQNL_CFG_CMD_PF_BIND); if (mnl_socket_sendto(nl, nlh, nlh->nlmsg_len) < 0) { - perror("mnl_socket_send"); + perror("mnl_socket_sendto"); exit(EXIT_FAILURE); } nlh = nfq_build_cfg_request(buf, NFQNL_CFG_CMD_BIND, queue_num); if (mnl_socket_sendto(nl, nlh, nlh->nlmsg_len) < 0) { - perror("mnl_socket_send"); + perror("mnl_socket_sendto"); exit(EXIT_FAILURE); } nlh = nfq_build_cfg_params(buf, NFQNL_COPY_PACKET, 0xFFFF, queue_num); if (mnl_socket_sendto(nl, nlh, nlh->nlmsg_len) < 0) { - perror("mnl_socket_send"); + perror("mnl_socket_sendto"); exit(EXIT_FAILURE); } @@ -230,7 +230,7 @@ int main(int argc, char *argv[]) id = ret - MNL_CB_OK; nlh = nfq_build_verdict(buf, id, queue_num, NF_ACCEPT); if (mnl_socket_sendto(nl, nlh, nlh->nlmsg_len) < 0) { - perror("mnl_socket_send"); + perror("mnl_socket_sendto"); exit(EXIT_FAILURE); } diff --git a/examples/netfilter/nfct-create-batch.c b/examples/netfilter/nfct-create-batch.c index dd6623f..40cd2f6 100644 --- a/examples/netfilter/nfct-create-batch.c +++ b/examples/netfilter/nfct-create-batch.c @@ -87,7 +87,7 @@ send_batch(struct mnl_socket *nl, struct mnl_nlmsg_batch *b, int portid) ret = mnl_socket_sendto(nl, mnl_nlmsg_batch_head(b), len); if (ret == -1) { - perror("mnl_socket_recvfrom"); + perror("mnl_socket_sendto"); exit(EXIT_FAILURE); } @@ -116,7 +116,7 @@ send_batch(struct mnl_socket *nl, struct mnl_nlmsg_batch *b, int portid) NULL, NULL, cb_ctl_array, MNL_ARRAY_SIZE(cb_ctl_array)); if (ret == -1) { - perror("mnl_cb_run"); + perror("mnl_cb_run2"); exit(EXIT_FAILURE); } diff --git a/examples/netfilter/nfct-daemon.c b/examples/netfilter/nfct-daemon.c index 5258537..a9b93db 100644 --- a/examples/netfilter/nfct-daemon.c +++ b/examples/netfilter/nfct-daemon.c @@ -87,7 +87,7 @@ static int parse_ip_cb(const struct nlattr *attr, void *data) case CTA_IP_V6_DST: if (mnl_attr_validate2(attr, MNL_TYPE_BINARY, sizeof(struct in6_addr)) < 0) { - perror("mnl_attr_validate"); + perror("mnl_attr_validate2"); return MNL_CB_ERROR; } break; @@ -322,7 +322,7 @@ int main(int argc, char *argv[]) /* ... request a fresh dump of the table from kernel */ ret = mnl_socket_sendto(nl, nlh, nlh->nlmsg_len); if (ret == -1) { - perror("mnl_socket_recvfrom"); + perror("mnl_socket_sendto"); return -1; } tv.tv_sec = secs; diff --git a/examples/netfilter/nfct-dump.c b/examples/netfilter/nfct-dump.c index ce05bbb..25ccf2c 100644 --- a/examples/netfilter/nfct-dump.c +++ b/examples/netfilter/nfct-dump.c @@ -66,7 +66,7 @@ static int parse_ip_cb(const struct nlattr *attr, void *data) case CTA_IP_V6_DST: if (mnl_attr_validate2(attr, MNL_TYPE_BINARY, sizeof(struct in6_addr)) < 0) { - perror("mnl_attr_validate"); + perror("mnl_attr_validate2"); return MNL_CB_ERROR; } break; @@ -292,7 +292,7 @@ int main(void) ret = mnl_socket_sendto(nl, nlh, nlh->nlmsg_len); if (ret == -1) { - perror("mnl_socket_recvfrom"); + perror("mnl_socket_sendto"); exit(EXIT_FAILURE); } portid = mnl_socket_get_portid(nl); diff --git a/examples/rtnl/rtnl-addr-dump.c b/examples/rtnl/rtnl-addr-dump.c index 156563b..6b4e52e 100644 --- a/examples/rtnl/rtnl-addr-dump.c +++ b/examples/rtnl/rtnl-addr-dump.c @@ -111,7 +111,7 @@ int main(int argc, char *argv[]) portid = mnl_socket_get_portid(nl); if (mnl_socket_sendto(nl, nlh, nlh->nlmsg_len) < 0) { - perror("mnl_socket_send"); + perror("mnl_socket_sendto"); exit(EXIT_FAILURE); } diff --git a/examples/rtnl/rtnl-link-dump.c b/examples/rtnl/rtnl-link-dump.c index f717cea..f5d6312 100644 --- a/examples/rtnl/rtnl-link-dump.c +++ b/examples/rtnl/rtnl-link-dump.c @@ -34,7 +34,7 @@ static int data_attr_cb(const struct nlattr *attr, void *data) break; case IFLA_IFNAME: if (mnl_attr_validate(attr, MNL_TYPE_STRING) < 0) { - perror("mnl_attr_validate2"); + perror("mnl_attr_validate"); return MNL_CB_ERROR; } break; @@ -108,7 +108,7 @@ int main(void) portid = mnl_socket_get_portid(nl); if (mnl_socket_sendto(nl, nlh, nlh->nlmsg_len) < 0) { - perror("mnl_socket_send"); + perror("mnl_socket_sendto"); exit(EXIT_FAILURE); } diff --git a/examples/rtnl/rtnl-link-dump2.c b/examples/rtnl/rtnl-link-dump2.c index 78f31a8..b3ca3fa 100644 --- a/examples/rtnl/rtnl-link-dump2.c +++ b/examples/rtnl/rtnl-link-dump2.c @@ -25,7 +25,7 @@ static int data_attr_cb(const struct nlattr *attr, void *data) break; case IFLA_IFNAME: if (mnl_attr_validate(attr, MNL_TYPE_STRING) < 0) { - perror("mnl_attr_validate2"); + perror("mnl_attr_validate"); return MNL_CB_ERROR; } printf("name=%s ", mnl_attr_get_str(attr)); @@ -81,7 +81,7 @@ int main(void) portid = mnl_socket_get_portid(nl); if (mnl_socket_sendto(nl, nlh, nlh->nlmsg_len) < 0) { - perror("mnl_socket_send"); + perror("mnl_socket_sendto"); exit(EXIT_FAILURE); } diff --git a/examples/rtnl/rtnl-link-dump3.c b/examples/rtnl/rtnl-link-dump3.c index a6dce6f..2521214 100644 --- a/examples/rtnl/rtnl-link-dump3.c +++ b/examples/rtnl/rtnl-link-dump3.c @@ -81,7 +81,7 @@ int main(void) portid = mnl_socket_get_portid(nl); if (mnl_socket_sendto(nl, nlh, nlh->nlmsg_len) < 0) { - perror("mnl_socket_send"); + perror("mnl_socket_sendto"); exit(EXIT_FAILURE); } diff --git a/examples/rtnl/rtnl-link-event.c b/examples/rtnl/rtnl-link-event.c index 90bb8e5..123fb88 100644 --- a/examples/rtnl/rtnl-link-event.c +++ b/examples/rtnl/rtnl-link-event.c @@ -26,7 +26,7 @@ static int data_attr_cb(const struct nlattr *attr, void *data) break; case IFLA_IFNAME: if (mnl_attr_validate(attr, MNL_TYPE_STRING) < 0) { - perror("mnl_attr_validate2"); + perror("mnl_attr_validate"); return MNL_CB_ERROR; } break; diff --git a/examples/rtnl/rtnl-link-set.c b/examples/rtnl/rtnl-link-set.c index dad8856..6086c37 100644 --- a/examples/rtnl/rtnl-link-set.c +++ b/examples/rtnl/rtnl-link-set.c @@ -62,19 +62,19 @@ int main(int argc, char *argv[]) sizeof(struct ifinfomsg)); if (mnl_socket_sendto(nl, nlh, nlh->nlmsg_len) < 0) { - perror("mnl_socket_send"); + perror("mnl_socket_sendto"); exit(EXIT_FAILURE); } ret = mnl_socket_recvfrom(nl, buf, sizeof(buf)); if (ret == -1) { - perror("read"); + perror("mnl_socket_recvfrom"); exit(EXIT_FAILURE); } ret = mnl_cb_run(buf, ret, seq, portid, NULL, NULL); if (ret == -1){ - perror("callback"); + perror("mnl_cb_run"); exit(EXIT_FAILURE); } diff --git a/examples/rtnl/rtnl-route-add.c b/examples/rtnl/rtnl-route-add.c index 6d070b8..97578cd 100644 --- a/examples/rtnl/rtnl-route-add.c +++ b/examples/rtnl/rtnl-route-add.c @@ -105,7 +105,7 @@ int main(int argc, char *argv[]) portid = mnl_socket_get_portid(nl); if (mnl_socket_sendto(nl, nlh, nlh->nlmsg_len) < 0) { - perror("mnl_socket_send"); + perror("mnl_socket_sendto"); exit(EXIT_FAILURE); } diff --git a/examples/rtnl/rtnl-route-dump.c b/examples/rtnl/rtnl-route-dump.c index 33cb2df..9829e36 100644 --- a/examples/rtnl/rtnl-route-dump.c +++ b/examples/rtnl/rtnl-route-dump.c @@ -182,7 +182,7 @@ static int data_ipv6_attr_cb(const struct nlattr *attr, void *data) case RTA_GATEWAY: if (mnl_attr_validate2(attr, MNL_TYPE_BINARY, sizeof(struct in6_addr)) < 0) { - perror("mnl_attr_validate"); + perror("mnl_attr_validate2"); return MNL_CB_ERROR; } break; @@ -330,7 +330,7 @@ int main(int argc, char *argv[]) portid = mnl_socket_get_portid(nl); if (mnl_socket_sendto(nl, nlh, nlh->nlmsg_len) < 0) { - perror("mnl_socket_send"); + perror("mnl_socket_sendto"); exit(EXIT_FAILURE); } diff --git a/examples/rtnl/rtnl-route-event.c b/examples/rtnl/rtnl-route-event.c index badba2d..8a836f8 100644 --- a/examples/rtnl/rtnl-route-event.c +++ b/examples/rtnl/rtnl-route-event.c @@ -178,7 +178,7 @@ static int data_ipv6_attr_cb(const struct nlattr *attr, void *data) case RTA_GATEWAY: if (mnl_attr_validate2(attr, MNL_TYPE_BINARY, sizeof(struct in6_addr)) < 0) { - perror("mnl_attr_validate"); + perror("mnl_attr_validate2"); return MNL_CB_ERROR; } break; -- cgit v1.2.3