From efcfbadb265ced5289f64c76bff777f20565a13e Mon Sep 17 00:00:00 2001 From: Pablo Neira Ayuso Date: Wed, 24 Apr 2013 01:03:33 +0200 Subject: examples: rtnl-route-add improvements (including IPv6 support) Now, both IPv4 and IPv6 are supported simultaneously in one single file. While at it: * I moved the declarations to the beginning of the main function. * Renamed mask by prefix, which seems more appropriate to me. * Use RTPROTO_STATIC, as the route has been added by the administrator. * Set NLM_F_ACK, so we get a report from the kernel about our query and treat reply from the kernel. * Stricter argc checking. * Use perror instead of printf for error reporting. Signed-off-by: Pablo Neira Ayuso --- examples/rtnl/rtnl-route-add.c | 81 +++++++++++++++++++++++++++++------------- 1 file changed, 57 insertions(+), 24 deletions(-) (limited to 'examples/rtnl') diff --git a/examples/rtnl/rtnl-route-add.c b/examples/rtnl/rtnl-route-add.c index 6d166da..dd810c9 100644 --- a/examples/rtnl/rtnl-route-add.c +++ b/examples/rtnl/rtnl-route-add.c @@ -14,63 +14,83 @@ int main(int argc, char *argv[]) { + struct mnl_socket *nl; + char buf[MNL_SOCKET_BUFFER_SIZE]; + struct nlmsghdr *nlh; + struct rtmsg *rtm; + uint32_t prefix, seq, portid; + union { + in_addr_t ip; + struct in6_addr ip6; + } dst; + union { + in_addr_t ip; + struct in6_addr ip6; + } gw; + int iface, ret, family = AF_INET; + if (argc <= 3) { printf("Usage: %s iface destination cidr [gateway]\n", argv[0]); printf("Example: %s eth0 10.0.1.12 32 10.0.1.11\n", argv[0]); + printf(" %s eth0 ffff::10.0.1.12 128 fdff::1\n", argv[0]); exit(EXIT_FAILURE); } - int iface; iface = if_nametoindex(argv[1]); if (iface == 0) { - printf("Bad interface name\n"); + perror("if_nametoindex"); exit(EXIT_FAILURE); } - in_addr_t dst; if (!inet_pton(AF_INET, argv[2], &dst)) { - printf("Bad destination\n"); - exit(EXIT_FAILURE); + if (!inet_pton(AF_INET6, argv[2], &dst)) { + perror("inet_pton"); + exit(EXIT_FAILURE); + } + family = AF_INET6; } - uint32_t mask; - if (sscanf(argv[3], "%u", &mask) == 0) { - printf("Bad CIDR\n"); + if (sscanf(argv[3], "%u", &prefix) == 0) { + perror("sscanf"); exit(EXIT_FAILURE); } - in_addr_t gw; - if (argc >= 5 && !inet_pton(AF_INET, argv[4], &gw)) { - printf("Bad gateway\n"); + if (argc == 5 && !inet_pton(family, argv[4], &gw)) { + perror("inet_pton"); exit(EXIT_FAILURE); } - struct mnl_socket *nl; - char buf[MNL_SOCKET_BUFFER_SIZE]; - struct nlmsghdr *nlh; - struct rtmsg *rtm; - nlh = mnl_nlmsg_put_header(buf); nlh->nlmsg_type = RTM_NEWROUTE; - nlh->nlmsg_flags = NLM_F_REQUEST | NLM_F_CREATE; - nlh->nlmsg_seq = time(NULL); + nlh->nlmsg_flags = NLM_F_REQUEST | NLM_F_CREATE | NLM_F_ACK; + nlh->nlmsg_seq = seq = time(NULL); rtm = mnl_nlmsg_put_extra_header(nlh, sizeof(struct rtmsg)); - rtm->rtm_family = AF_INET; - rtm->rtm_dst_len = mask; + rtm->rtm_family = family; + rtm->rtm_dst_len = prefix; rtm->rtm_src_len = 0; rtm->rtm_tos = 0; - rtm->rtm_protocol = RTPROT_BOOT; + rtm->rtm_protocol = RTPROT_STATIC; rtm->rtm_table = RT_TABLE_MAIN; rtm->rtm_type = RTN_UNICAST; /* is there any gateway? */ rtm->rtm_scope = (argc == 4) ? RT_SCOPE_LINK : RT_SCOPE_UNIVERSE; rtm->rtm_flags = 0; - mnl_attr_put_u32(nlh, RTA_DST, dst); + if (family == AF_INET) + mnl_attr_put_u32(nlh, RTA_DST, dst.ip); + else + mnl_attr_put(nlh, RTA_DST, sizeof(struct in6_addr), &dst); + mnl_attr_put_u32(nlh, RTA_OIF, iface); - if (argc >= 5) - mnl_attr_put_u32(nlh, RTA_GATEWAY, gw); + if (argc == 5) { + if (family == AF_INET) + mnl_attr_put_u32(nlh, RTA_GATEWAY, gw.ip); + else { + mnl_attr_put(nlh, RTA_DST, sizeof(struct in6_addr), + &gw.ip6); + } + } nl = mnl_socket_open(NETLINK_ROUTE); if (nl == NULL) { @@ -82,12 +102,25 @@ int main(int argc, char *argv[]) perror("mnl_socket_bind"); exit(EXIT_FAILURE); } + portid = mnl_socket_get_portid(nl); if (mnl_socket_sendto(nl, nlh, nlh->nlmsg_len) < 0) { perror("mnl_socket_send"); exit(EXIT_FAILURE); } + ret = mnl_socket_recvfrom(nl, buf, sizeof(buf)); + if (ret < 0) { + perror("mnl_socket_recvfrom"); + exit(EXIT_FAILURE); + } + + ret = mnl_cb_run(buf, ret, seq, portid, NULL, NULL); + if (ret < 0) { + perror("mnl_cb_run"); + exit(EXIT_FAILURE); + } + mnl_socket_close(nl); return 0; -- cgit v1.2.3 From 7a965af1cbe9a9c42935c00657af78efd29db380 Mon Sep 17 00:00:00 2001 From: Pablo Neira Ayuso Date: Wed, 24 Apr 2013 12:36:24 +0200 Subject: examples: rtnl-route-event: add example to listen to IPv4/IPv6 routes Derived from rtnl-route-dump.c Signed-off-by: Pablo Neira Ayuso --- examples/rtnl/rtnl-route-event.c | 330 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 330 insertions(+) create mode 100644 examples/rtnl/rtnl-route-event.c (limited to 'examples/rtnl') diff --git a/examples/rtnl/rtnl-route-event.c b/examples/rtnl/rtnl-route-event.c new file mode 100644 index 0000000..16d0563 --- /dev/null +++ b/examples/rtnl/rtnl-route-event.c @@ -0,0 +1,330 @@ +/* This example is placed in the public domain. */ +#include +#include +#include +#include +#include +#include + +#include +#include +#include +#include + +static int data_attr_cb2(const struct nlattr *attr, void *data) +{ + /* skip unsupported attribute in user-space */ + if (mnl_attr_type_valid(attr, RTAX_MAX) < 0) + return MNL_CB_OK; + + if (mnl_attr_validate(attr, MNL_TYPE_U32) < 0) { + perror("mnl_attr_validate"); + return MNL_CB_ERROR; + } + return MNL_CB_OK; +} + +static void attributes_show_ipv4(struct nlattr *tb[]) +{ + if (tb[RTA_TABLE]) { + printf("table=%u ", mnl_attr_get_u32(tb[RTA_TABLE])); + } + if (tb[RTA_DST]) { + struct in_addr *addr = mnl_attr_get_payload(tb[RTA_DST]); + printf("dst=%s ", inet_ntoa(*addr)); + } + if (tb[RTA_SRC]) { + struct in_addr *addr = mnl_attr_get_payload(tb[RTA_SRC]); + printf("src=%s ", inet_ntoa(*addr)); + } + if (tb[RTA_OIF]) { + printf("oif=%u ", mnl_attr_get_u32(tb[RTA_OIF])); + } + if (tb[RTA_FLOW]) { + printf("flow=%u ", mnl_attr_get_u32(tb[RTA_FLOW])); + } + if (tb[RTA_PREFSRC]) { + struct in_addr *addr = mnl_attr_get_payload(tb[RTA_PREFSRC]); + printf("prefsrc=%s ", inet_ntoa(*addr)); + } + if (tb[RTA_GATEWAY]) { + struct in_addr *addr = mnl_attr_get_payload(tb[RTA_GATEWAY]); + printf("gw=%s ", inet_ntoa(*addr)); + } + if (tb[RTA_METRICS]) { + int i; + struct nlattr *tbx[RTAX_MAX+1] = {}; + + mnl_attr_parse_nested(tb[RTA_METRICS], data_attr_cb2, tbx); + + for (i=0; inlmsg_type) { + case RTM_NEWROUTE: + printf("[NEW] "); + break; + case RTM_DELROUTE: + printf("[DEL] "); + break; + } + + /* protocol family = AF_INET | AF_INET6 */ + printf("family=%u ", rm->rtm_family); + + /* destination CIDR, eg. 24 or 32 for IPv4 */ + printf("dst_len=%u ", rm->rtm_dst_len); + + /* source CIDR */ + printf("src_len=%u ", rm->rtm_src_len); + + /* type of service (TOS), eg. 0 */ + printf("tos=%u ", rm->rtm_tos); + + /* table id: + * RT_TABLE_UNSPEC = 0 + * + * ... user defined values ... + * + * RT_TABLE_COMPAT = 252 + * RT_TABLE_DEFAULT = 253 + * RT_TABLE_MAIN = 254 + * RT_TABLE_LOCAL = 255 + * RT_TABLE_MAX = 0xFFFFFFFF + * + * Synonimous attribute: RTA_TABLE. + */ + printf("table=%u ", rm->rtm_table); + + /* type: + * RTN_UNSPEC = 0 + * RTN_UNICAST = 1 + * RTN_LOCAL = 2 + * RTN_BROADCAST = 3 + * RTN_ANYCAST = 4 + * RTN_MULTICAST = 5 + * RTN_BLACKHOLE = 6 + * RTN_UNREACHABLE = 7 + * RTN_PROHIBIT = 8 + * RTN_THROW = 9 + * RTN_NAT = 10 + * RTN_XRESOLVE = 11 + * __RTN_MAX = 12 + */ + printf("type=%u ", rm->rtm_type); + + /* scope: + * RT_SCOPE_UNIVERSE = 0 : everywhere in the universe + * + * ... user defined values ... + * + * RT_SCOPE_SITE = 200 + * RT_SCOPE_LINK = 253 : destination attached to link + * RT_SCOPE_HOST = 254 : local address + * RT_SCOPE_NOWHERE = 255 : not existing destination + */ + printf("scope=%u ", rm->rtm_scope); + + /* protocol: + * RTPROT_UNSPEC = 0 + * RTPROT_REDIRECT = 1 + * RTPROT_KERNEL = 2 : route installed by kernel + * RTPROT_BOOT = 3 : route installed during boot + * RTPROT_STATIC = 4 : route installed by administrator + * + * Values >= RTPROT_STATIC are not interpreted by kernel, they are + * just user-defined. + */ + printf("proto=%u ", rm->rtm_protocol); + + /* flags: + * RTM_F_NOTIFY = 0x100: notify user of route change + * RTM_F_CLONED = 0x200: this route is cloned + * RTM_F_EQUALIZE = 0x400: Multipath equalizer: NI + * RTM_F_PREFIX = 0x800: Prefix addresses + */ + printf("flags=%x", rm->rtm_flags); + + switch(rm->rtm_family) { + case AF_INET: + mnl_attr_parse(nlh, sizeof(*rm), data_ipv4_attr_cb, tb); + attributes_show_ipv4(tb); + break; + case AF_INET6: + mnl_attr_parse(nlh, sizeof(*rm), data_ipv6_attr_cb, tb); + attributes_show_ipv6(tb); + break; + } + printf("\n"); + + return MNL_CB_OK; +} + +int main(int argc, char *argv[]) +{ + struct mnl_socket *nl; + char buf[MNL_SOCKET_BUFFER_SIZE]; + int ret; + + nl = mnl_socket_open(NETLINK_ROUTE); + if (nl == NULL) { + perror("mnl_socket_open"); + exit(EXIT_FAILURE); + } + + if (mnl_socket_bind(nl, RTMGRP_IPV4_ROUTE | RTMGRP_IPV6_ROUTE, + MNL_SOCKET_AUTOPID) < 0) { + perror("mnl_socket_bind"); + 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 <= MNL_CB_STOP) + 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 cf813753e8738c781e33de955f9733a27c65229f Mon Sep 17 00:00:00 2001 From: Pablo Neira Ayuso Date: Wed, 24 Apr 2013 13:02:15 +0200 Subject: examples: rtnl-route-event: update Makefile.am So you can compile rtnl-route-event via `make check' Signed-off-by: Pablo Neira Ayuso --- examples/rtnl/Makefile.am | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) (limited to 'examples/rtnl') diff --git a/examples/rtnl/Makefile.am b/examples/rtnl/Makefile.am index 3cc8995..abe3e9b 100644 --- a/examples/rtnl/Makefile.am +++ b/examples/rtnl/Makefile.am @@ -4,7 +4,8 @@ check_PROGRAMS = rtnl-link-dump rtnl-link-dump2 rtnl-link-dump3 \ rtnl-link-event \ rtnl-link-set \ rtnl-route-add \ - rtnl-route-dump + rtnl-route-dump \ + rtnl-route-event rtnl_link_dump_SOURCES = rtnl-link-dump.c rtnl_link_dump_LDADD = ../../src/libmnl.la @@ -26,3 +27,6 @@ rtnl_link_set_LDADD = ../../src/libmnl.la rtnl_route_dump_SOURCES = rtnl-route-dump.c rtnl_route_dump_LDADD = ../../src/libmnl.la + +rtnl_route_event_SOURCES = rtnl-route-event.c +rtnl_route_event_LDADD = ../../src/libmnl.la -- cgit v1.2.3 From 3b8a8e462ee3ed33a95c784b6c77c33377210ca3 Mon Sep 17 00:00:00 2001 From: Pablo Neira Ayuso Date: Thu, 25 Apr 2013 15:19:58 +0200 Subject: examples: rtnl-route-add: fix typo in IPv6 support Signed-off-by: Pablo Neira Ayuso --- examples/rtnl/rtnl-route-add.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'examples/rtnl') diff --git a/examples/rtnl/rtnl-route-add.c b/examples/rtnl/rtnl-route-add.c index dd810c9..6d070b8 100644 --- a/examples/rtnl/rtnl-route-add.c +++ b/examples/rtnl/rtnl-route-add.c @@ -87,7 +87,7 @@ int main(int argc, char *argv[]) if (family == AF_INET) mnl_attr_put_u32(nlh, RTA_GATEWAY, gw.ip); else { - mnl_attr_put(nlh, RTA_DST, sizeof(struct in6_addr), + mnl_attr_put(nlh, RTA_GATEWAY, sizeof(struct in6_addr), &gw.ip6); } } -- cgit v1.2.3 From 050d592eee6c96ae8e08c6dfefe42f10685f0718 Mon Sep 17 00:00:00 2001 From: Pablo Neira Ayuso Date: Thu, 2 May 2013 17:24:57 +0200 Subject: examples: rtnl-route-dump: display also metric/priority If present in the route message, otherwise simply skip it. Signed-off-by: Pablo Neira Ayuso --- examples/rtnl/rtnl-route-dump.c | 3 +++ 1 file changed, 3 insertions(+) (limited to 'examples/rtnl') diff --git a/examples/rtnl/rtnl-route-dump.c b/examples/rtnl/rtnl-route-dump.c index a798515..59e0a9d 100644 --- a/examples/rtnl/rtnl-route-dump.c +++ b/examples/rtnl/rtnl-route-dump.c @@ -51,6 +51,9 @@ static void attributes_show_ipv4(struct nlattr *tb[]) struct in_addr *addr = mnl_attr_get_payload(tb[RTA_GATEWAY]); printf("gw=%s ", inet_ntoa(*addr)); } + if (tb[RTA_PRIORITY]) { + printf("prio=%u ", mnl_attr_get_u32(tb[RTA_PRIORITY])); + } if (tb[RTA_METRICS]) { int i; struct nlattr *tbx[RTAX_MAX+1] = {}; -- cgit v1.2.3 From e2af6ce3d5f97600e8c953612608523a4ff727bd Mon Sep 17 00:00:00 2001 From: Pablo Neira Ayuso Date: Mon, 27 May 2013 20:30:49 +0200 Subject: examples: rtnl-link-dump: display HW address --- examples/rtnl/rtnl-link-dump.c | 20 +++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) (limited to 'examples/rtnl') diff --git a/examples/rtnl/rtnl-link-dump.c b/examples/rtnl/rtnl-link-dump.c index 159f3b6..f717cea 100644 --- a/examples/rtnl/rtnl-link-dump.c +++ b/examples/rtnl/rtnl-link-dump.c @@ -3,6 +3,7 @@ #include #include #include +#include #include #include @@ -19,6 +20,12 @@ static int data_attr_cb(const struct nlattr *attr, void *data) return MNL_CB_OK; switch(type) { + case IFLA_ADDRESS: + if (mnl_attr_validate(attr, MNL_TYPE_BINARY) < 0) { + perror("mnl_attr_validate"); + return MNL_CB_ERROR; + } + break; case IFLA_MTU: if (mnl_attr_validate(attr, MNL_TYPE_U32) < 0) { perror("mnl_attr_validate"); @@ -55,7 +62,18 @@ static int data_cb(const struct nlmsghdr *nlh, void *data) printf("mtu=%d ", mnl_attr_get_u32(tb[IFLA_MTU])); } if (tb[IFLA_IFNAME]) { - printf("name=%s", mnl_attr_get_str(tb[IFLA_IFNAME])); + printf("name=%s ", mnl_attr_get_str(tb[IFLA_IFNAME])); + } + if (tb[IFLA_ADDRESS]) { + uint8_t *hwaddr = mnl_attr_get_payload(tb[IFLA_ADDRESS]); + int i; + + printf("hwaddr="); + for (i=0; i Date: Mon, 27 May 2013 20:54:08 +0200 Subject: examples: add rtnl-addr-dump Signed-off-by: Pablo Neira Ayuso --- examples/rtnl/Makefile.am | 6 +- examples/rtnl/rtnl-addr-dump.c | 133 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 138 insertions(+), 1 deletion(-) create mode 100644 examples/rtnl/rtnl-addr-dump.c (limited to 'examples/rtnl') diff --git a/examples/rtnl/Makefile.am b/examples/rtnl/Makefile.am index abe3e9b..24769b6 100644 --- a/examples/rtnl/Makefile.am +++ b/examples/rtnl/Makefile.am @@ -1,12 +1,16 @@ include $(top_srcdir)/Make_global.am -check_PROGRAMS = rtnl-link-dump rtnl-link-dump2 rtnl-link-dump3 \ +check_PROGRAMS = rtnl-addr-dump \ + rtnl-link-dump rtnl-link-dump2 rtnl-link-dump3 \ rtnl-link-event \ rtnl-link-set \ rtnl-route-add \ rtnl-route-dump \ rtnl-route-event +rtnl_addr_dump_SOURCES = rtnl-addr-dump.c +rtnl_addr_dump_LDADD = ../../src/libmnl.la + rtnl_link_dump_SOURCES = rtnl-link-dump.c rtnl_link_dump_LDADD = ../../src/libmnl.la diff --git a/examples/rtnl/rtnl-addr-dump.c b/examples/rtnl/rtnl-addr-dump.c new file mode 100644 index 0000000..156563b --- /dev/null +++ b/examples/rtnl/rtnl-addr-dump.c @@ -0,0 +1,133 @@ +/* This example is placed in the public domain. */ +#include +#include +#include +#include +#include +#include + +#include +#include +#include +#include + +static int data_attr_cb(const struct nlattr *attr, void *data) +{ + const struct nlattr **tb = data; + int type = mnl_attr_get_type(attr); + + /* skip unsupported attribute in user-space */ + if (mnl_attr_type_valid(attr, IFA_MAX) < 0) + return MNL_CB_OK; + + switch(type) { + case IFA_ADDRESS: + if (mnl_attr_validate(attr, MNL_TYPE_BINARY) < 0) { + perror("mnl_attr_validate"); + return MNL_CB_ERROR; + } + break; + } + tb[type] = attr; + return MNL_CB_OK; +} + +static int data_cb(const struct nlmsghdr *nlh, void *data) +{ + struct nlattr *tb[IFLA_MAX+1] = {}; + struct ifaddrmsg *ifa = mnl_nlmsg_get_payload(nlh); + + printf("index=%d family=%d ", ifa->ifa_index, ifa->ifa_family); + + mnl_attr_parse(nlh, sizeof(*ifa), data_attr_cb, tb); + printf("addr="); + if (tb[IFA_ADDRESS]) { + void *addr = mnl_attr_get_payload(tb[IFA_ADDRESS]); + char out[INET6_ADDRSTRLEN]; + + if (inet_ntop(ifa->ifa_family, addr, out, sizeof(out))) + printf("%s ", out); + } + printf("scope="); + switch(ifa->ifa_scope) { + case 0: + printf("global "); + break; + case 200: + printf("site "); + break; + case 253: + printf("link "); + break; + case 254: + printf("host "); + break; + case 255: + printf("nowhere "); + break; + default: + printf("%d ", ifa->ifa_scope); + break; + } + + printf("\n"); + return MNL_CB_OK; +} + +int main(int argc, char *argv[]) +{ + struct mnl_socket *nl; + char buf[MNL_SOCKET_BUFFER_SIZE]; + struct nlmsghdr *nlh; + struct rtgenmsg *rt; + int ret; + unsigned int seq, portid; + + if (argc != 2) { + fprintf(stderr, "Usage: %s \n", argv[0]); + exit(EXIT_FAILURE); + } + + nlh = mnl_nlmsg_put_header(buf); + nlh->nlmsg_type = RTM_GETADDR; + nlh->nlmsg_flags = NLM_F_REQUEST | NLM_F_DUMP; + nlh->nlmsg_seq = seq = time(NULL); + rt = mnl_nlmsg_put_extra_header(nlh, sizeof(struct rtgenmsg)); + if (strcmp(argv[1], "inet") == 0) + rt->rtgen_family = AF_INET; + else if (strcmp(argv[1], "inet6") == 0) + rt->rtgen_family = AF_INET6; + + nl = mnl_socket_open(NETLINK_ROUTE); + 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); + } + portid = mnl_socket_get_portid(nl); + + if (mnl_socket_sendto(nl, nlh, nlh->nlmsg_len) < 0) { + perror("mnl_socket_send"); + exit(EXIT_FAILURE); + } + + ret = mnl_socket_recvfrom(nl, buf, sizeof(buf)); + while (ret > 0) { + ret = mnl_cb_run(buf, ret, seq, portid, data_cb, NULL); + if (ret <= MNL_CB_STOP) + 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 c7a66dd8c1cc25889fdb91c5ef92e92d55119497 Mon Sep 17 00:00:00 2001 From: Ken-ichirou MATSUZAWA Date: Sat, 7 Dec 2013 20:21:34 +0900 Subject: examples: set attr table Florian adviced rtnl-route-event.c has same problem Signed-off-by: Ken-ichirou MATSUZAWA Signed-off-by: Florian Westphal --- examples/rtnl/rtnl-route-dump.c | 4 ++++ examples/rtnl/rtnl-route-event.c | 4 ++++ 2 files changed, 8 insertions(+) (limited to 'examples/rtnl') diff --git a/examples/rtnl/rtnl-route-dump.c b/examples/rtnl/rtnl-route-dump.c index 59e0a9d..33cb2df 100644 --- a/examples/rtnl/rtnl-route-dump.c +++ b/examples/rtnl/rtnl-route-dump.c @@ -13,6 +13,8 @@ static int data_attr_cb2(const struct nlattr *attr, void *data) { + const struct nlattr **tb = data; + /* skip unsupported attribute in user-space */ if (mnl_attr_type_valid(attr, RTAX_MAX) < 0) return MNL_CB_OK; @@ -21,6 +23,8 @@ static int data_attr_cb2(const struct nlattr *attr, void *data) perror("mnl_attr_validate"); return MNL_CB_ERROR; } + + tb[mnl_attr_get_type(attr)] = attr; return MNL_CB_OK; } diff --git a/examples/rtnl/rtnl-route-event.c b/examples/rtnl/rtnl-route-event.c index 16d0563..badba2d 100644 --- a/examples/rtnl/rtnl-route-event.c +++ b/examples/rtnl/rtnl-route-event.c @@ -13,6 +13,8 @@ static int data_attr_cb2(const struct nlattr *attr, void *data) { + const struct nlattr **tb = data; + /* skip unsupported attribute in user-space */ if (mnl_attr_type_valid(attr, RTAX_MAX) < 0) return MNL_CB_OK; @@ -21,6 +23,8 @@ static int data_attr_cb2(const struct nlattr *attr, void *data) perror("mnl_attr_validate"); return MNL_CB_ERROR; } + + tb[mnl_attr_get_type(attr)] = attr; return MNL_CB_OK; } -- 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/rtnl') 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 From 60dfea927d1c6b94239243b828ea9656a48601e9 Mon Sep 17 00:00:00 2001 From: Ken-ichirou MATSUZAWA Date: Sat, 7 Dec 2013 20:29:23 +0900 Subject: examples: made sub functions the same show entry in oneline and RTA_PRIORITY Signed-off-by: Ken-ichirou MATSUZAWA Signed-off-by: Florian Westphal --- examples/rtnl/rtnl-route-dump.c | 10 +++++++--- examples/rtnl/rtnl-route-event.c | 13 ++++++++++--- 2 files changed, 17 insertions(+), 6 deletions(-) (limited to 'examples/rtnl') diff --git a/examples/rtnl/rtnl-route-dump.c b/examples/rtnl/rtnl-route-dump.c index 9829e36..17da80b 100644 --- a/examples/rtnl/rtnl-route-dump.c +++ b/examples/rtnl/rtnl-route-dump.c @@ -71,7 +71,6 @@ static void attributes_show_ipv4(struct nlattr *tb[]) } } } - printf("\n"); } /* like inet_ntoa(), not reentrant */ @@ -109,6 +108,9 @@ static void attributes_show_ipv6(struct nlattr *tb[]) struct in6_addr *addr = mnl_attr_get_payload(tb[RTA_GATEWAY]); printf("gw=%s ", inet6_ntoa(*addr)); } + if (tb[RTA_PRIORITY]) { + printf("prio=%u ", mnl_attr_get_u32(tb[RTA_PRIORITY])); + } if (tb[RTA_METRICS]) { int i; struct nlattr *tbx[RTAX_MAX+1] = {}; @@ -122,7 +124,6 @@ static void attributes_show_ipv6(struct nlattr *tb[]) } } } - printf("\n"); } static int data_ipv4_attr_cb(const struct nlattr *attr, void *data) @@ -142,6 +143,7 @@ static int data_ipv4_attr_cb(const struct nlattr *attr, void *data) case RTA_FLOW: case RTA_PREFSRC: case RTA_GATEWAY: + case RTA_PRIORITY: if (mnl_attr_validate(attr, MNL_TYPE_U32) < 0) { perror("mnl_attr_validate"); return MNL_CB_ERROR; @@ -171,6 +173,7 @@ static int data_ipv6_attr_cb(const struct nlattr *attr, void *data) case RTA_TABLE: case RTA_OIF: case RTA_FLOW: + case RTA_PRIORITY: if (mnl_attr_validate(attr, MNL_TYPE_U32) < 0) { perror("mnl_attr_validate"); return MNL_CB_ERROR; @@ -276,7 +279,7 @@ static int data_cb(const struct nlmsghdr *nlh, void *data) * RTM_F_EQUALIZE = 0x400: Multipath equalizer: NI * RTM_F_PREFIX = 0x800: Prefix addresses */ - printf("flags=%x\n", rm->rtm_flags); + printf("flags=%x ", rm->rtm_flags); switch(rm->rtm_family) { case AF_INET: @@ -289,6 +292,7 @@ static int data_cb(const struct nlmsghdr *nlh, void *data) break; } + printf("\n"); return MNL_CB_OK; } diff --git a/examples/rtnl/rtnl-route-event.c b/examples/rtnl/rtnl-route-event.c index 8a836f8..6cad9f0 100644 --- a/examples/rtnl/rtnl-route-event.c +++ b/examples/rtnl/rtnl-route-event.c @@ -55,6 +55,9 @@ static void attributes_show_ipv4(struct nlattr *tb[]) struct in_addr *addr = mnl_attr_get_payload(tb[RTA_GATEWAY]); printf("gw=%s ", inet_ntoa(*addr)); } + if (tb[RTA_PRIORITY]) { + printf("prio=%u ", mnl_attr_get_u32(tb[RTA_PRIORITY])); + } if (tb[RTA_METRICS]) { int i; struct nlattr *tbx[RTAX_MAX+1] = {}; @@ -105,6 +108,9 @@ static void attributes_show_ipv6(struct nlattr *tb[]) struct in6_addr *addr = mnl_attr_get_payload(tb[RTA_GATEWAY]); printf("gw=%s ", inet6_ntoa(*addr)); } + if (tb[RTA_PRIORITY]) { + printf("prio=%u ", mnl_attr_get_u32(tb[RTA_PRIORITY])); + } if (tb[RTA_METRICS]) { int i; struct nlattr *tbx[RTAX_MAX+1] = {}; @@ -118,7 +124,6 @@ static void attributes_show_ipv6(struct nlattr *tb[]) } } } - printf("\n"); } static int data_ipv4_attr_cb(const struct nlattr *attr, void *data) @@ -138,6 +143,7 @@ static int data_ipv4_attr_cb(const struct nlattr *attr, void *data) case RTA_FLOW: case RTA_PREFSRC: case RTA_GATEWAY: + case RTA_PRIORITY: if (mnl_attr_validate(attr, MNL_TYPE_U32) < 0) { perror("mnl_attr_validate"); return MNL_CB_ERROR; @@ -167,6 +173,7 @@ static int data_ipv6_attr_cb(const struct nlattr *attr, void *data) case RTA_TABLE: case RTA_OIF: case RTA_FLOW: + case RTA_PRIORITY: if (mnl_attr_validate(attr, MNL_TYPE_U32) < 0) { perror("mnl_attr_validate"); return MNL_CB_ERROR; @@ -281,7 +288,7 @@ static int data_cb(const struct nlmsghdr *nlh, void *data) * RTM_F_EQUALIZE = 0x400: Multipath equalizer: NI * RTM_F_PREFIX = 0x800: Prefix addresses */ - printf("flags=%x", rm->rtm_flags); + printf("flags=%x ", rm->rtm_flags); switch(rm->rtm_family) { case AF_INET: @@ -293,8 +300,8 @@ static int data_cb(const struct nlmsghdr *nlh, void *data) attributes_show_ipv6(tb); break; } - printf("\n"); + printf("\n"); return MNL_CB_OK; } -- cgit v1.2.3