summaryrefslogtreecommitdiff
path: root/examples
diff options
context:
space:
mode:
Diffstat (limited to 'examples')
-rw-r--r--examples/Makefile.am24
-rw-r--r--examples/genl-family-get.c143
-rw-r--r--examples/rtnl-link-dump.c84
-rw-r--r--examples/rtnl-link-event.c70
-rw-r--r--examples/rtnl-link-set.c76
-rw-r--r--examples/rtnl-route-dump.c192
6 files changed, 589 insertions, 0 deletions
diff --git a/examples/Makefile.am b/examples/Makefile.am
new file mode 100644
index 0000000..92c5342
--- /dev/null
+++ b/examples/Makefile.am
@@ -0,0 +1,24 @@
+include $(top_srcdir)/Make_global.am
+
+check_PROGRAMS = rtnl-link-dump rtnl-link-event rtnl-link-set \
+ rtnl-route-dump genl-family-get
+
+rtnl_link_dump_SOURCES = rtnl-link-dump.c
+rtnl_link_dump_LDADD = ../src/libmnl.la
+rtnl_link_dump_LDFLAGS = -dynamic -ldl
+
+rtnl_link_event_SOURCES = rtnl-link-event.c
+rtnl_link_event_LDADD = ../src/libmnl.la
+rtnl_link_event_LDFLAGS = -dynamic -ldl
+
+rtnl_link_set_SOURCES = rtnl-link-set.c
+rtnl_link_set_LDADD = ../src/libmnl.la
+rtnl_link_set_LDFLAGS = -dynamic -ldl
+
+rtnl_route_dump_SOURCES = rtnl-route-dump.c
+rtnl_route_dump_LDADD = ../src/libmnl.la
+rtnl_route_dump_LDFLAGS = -dynamic -ldl
+
+genl_family_get_SOURCES = genl-family-get.c
+genl_family_get_LDADD = ../src/libmnl.la
+genl_family_get_LDFLAGS = -dynamic -ldl
diff --git a/examples/genl-family-get.c b/examples/genl-family-get.c
new file mode 100644
index 0000000..3e741cb
--- /dev/null
+++ b/examples/genl-family-get.c
@@ -0,0 +1,143 @@
+#include <stdio.h>
+#include <stdlib.h>
+#include <unistd.h>
+
+#include <libmnl/libmnl.h>
+#include <linux/genetlink.h>
+
+static void parse_genl_mc_grps(struct nlattr *nested)
+{
+ struct nlattr *pos;
+ int len;
+
+ mnl_attr_for_each_nested(pos, nested, len) {
+ struct nlattr *tb[CTRL_ATTR_MCAST_GRP_MAX+1];
+
+ mnl_attr_parse_nested(pos, tb, CTRL_ATTR_MCAST_GRP_MAX);
+ if (tb[CTRL_ATTR_MCAST_GRP_ID]) {
+ printf("id-0x%x ",
+ mnl_attr_get_u32(tb[CTRL_ATTR_MCAST_GRP_ID]));
+ }
+ if (tb[CTRL_ATTR_MCAST_GRP_NAME]) {
+ printf("name: %s ",
+ mnl_attr_get_str(tb[CTRL_ATTR_MCAST_GRP_NAME]));
+ }
+ printf("\n");
+ }
+}
+
+static void parse_genl_family_ops(struct nlattr *nested)
+{
+ struct nlattr *pos;
+ int len;
+
+ mnl_attr_for_each_nested(pos, nested, len) {
+ struct nlattr *tb[CTRL_ATTR_OP_MAX+1];
+
+ mnl_attr_parse_nested(pos, tb, CTRL_ATTR_OP_MAX);
+ if (tb[CTRL_ATTR_OP_ID]) {
+ printf("id-0x%x ",
+ mnl_attr_get_u32(tb[CTRL_ATTR_OP_ID]));
+ }
+ if (tb[CTRL_ATTR_OP_MAX]) {
+ printf("flags ");
+ }
+ printf("\n");
+ }
+}
+
+static int data_cb(const struct nlmsghdr *nlh, void *data)
+{
+ struct nlattr *tb[CTRL_ATTR_MAX+1];
+ struct genlmsghdr *genl = mnl_nlmsg_get_data(nlh);
+
+ mnl_attr_parse_at_offset(nlh, sizeof(*genl), tb, CTRL_ATTR_MAX);
+ if (tb[CTRL_ATTR_FAMILY_NAME]) {
+ printf("name=%s\t",
+ mnl_attr_get_str(tb[CTRL_ATTR_FAMILY_NAME]));
+ }
+ if (tb[CTRL_ATTR_FAMILY_ID]) {
+ printf("id=%u\t",
+ mnl_attr_get_u16(tb[CTRL_ATTR_FAMILY_ID]));
+ }
+ if (tb[CTRL_ATTR_VERSION]) {
+ printf("version=%u\t",
+ mnl_attr_get_u32(tb[CTRL_ATTR_VERSION]));
+ }
+ if (tb[CTRL_ATTR_HDRSIZE]) {
+ printf("hdrsize=%u\t",
+ mnl_attr_get_u32(tb[CTRL_ATTR_HDRSIZE]));
+ }
+ if (tb[CTRL_ATTR_MAXATTR]) {
+ printf("maxattr=%u\t",
+ mnl_attr_get_u32(tb[CTRL_ATTR_MAXATTR]));
+ }
+ if (tb[CTRL_ATTR_OPS]) {
+ printf("\nops:\n");
+ parse_genl_family_ops(tb[CTRL_ATTR_OPS]);
+ }
+ if (tb[CTRL_ATTR_MCAST_GROUPS]) {
+ printf("\ngrps:\n");
+ parse_genl_mc_grps(tb[CTRL_ATTR_MCAST_GROUPS]);
+ }
+ return MNL_CB_OK;
+}
+
+int main(int argc, char *argv[])
+{
+ struct mnl_socket *nl;
+ char buf[getpagesize()];
+ struct nlmsghdr *nlh;
+ struct genlmsghdr *genl;
+ int ret;
+ unsigned int seq;
+
+ if (argc != 2) {
+ printf("%s [family name]\n", argv[0]);
+ exit(EXIT_FAILURE);
+ }
+
+ nlh = mnl_nlmsg_put_header(buf);
+ nlh->nlmsg_type = GENL_ID_CTRL;
+ nlh->nlmsg_flags = NLM_F_REQUEST | NLM_F_ACK;
+ nlh->nlmsg_seq = seq = time(NULL);
+
+ genl = mnl_nlmsg_put_extra_header(nlh, sizeof(struct genlmsghdr));
+ genl->cmd = CTRL_CMD_GETFAMILY;
+ genl->version = 1;
+
+ mnl_attr_put_u32(nlh, CTRL_ATTR_FAMILY_ID, GENL_ID_CTRL);
+ mnl_attr_put_str_null(nlh, CTRL_ATTR_FAMILY_NAME, 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_sendto(nl, nlh, mnl_nlmsg_get_len(nlh)) < 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, 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;
+}
diff --git a/examples/rtnl-link-dump.c b/examples/rtnl-link-dump.c
new file mode 100644
index 0000000..7cf061d
--- /dev/null
+++ b/examples/rtnl-link-dump.c
@@ -0,0 +1,84 @@
+#include <stdio.h>
+#include <stdlib.h>
+#include <unistd.h>
+
+#include <libmnl/libmnl.h>
+#include <linux/if.h>
+#include <linux/if_link.h>
+#include <linux/rtnetlink.h>
+
+static int data_cb(const struct nlmsghdr *nlh, void *data)
+{
+ struct nlattr *tb[IFLA_MAX+1];
+ struct ifinfomsg *ifm = mnl_nlmsg_get_data(nlh);
+ int len = mnl_nlmsg_get_len(nlh);
+ struct nlattr *attr;
+
+ printf("index=%d type=%d flags=%d family=%d ",
+ ifm->ifi_index, ifm->ifi_type,
+ ifm->ifi_flags, ifm->ifi_family);
+
+ if (ifm->ifi_flags & IFF_RUNNING)
+ printf("[RUNNING] ");
+ else
+ printf("[NOT RUNNING] ");
+
+ mnl_attr_parse_at_offset(nlh, sizeof(*ifm), tb, IFLA_MAX);
+ if (tb[IFLA_MTU]) {
+ 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("\n");
+ return MNL_CB_OK;
+}
+
+int main()
+{
+ struct mnl_socket *nl;
+ char buf[getpagesize()];
+ struct nlmsghdr *nlh;
+ struct rtgenmsg *rt;
+ int ret;
+ unsigned int seq;
+
+ nlh = mnl_nlmsg_put_header(buf);
+ nlh->nlmsg_type = RTM_GETLINK;
+ 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));
+ rt->rtgen_family = AF_PACKET;
+
+ 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);
+ }
+
+ if (mnl_socket_sendto(nl, nlh, mnl_nlmsg_get_len(nlh)) < 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, 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;
+}
diff --git a/examples/rtnl-link-event.c b/examples/rtnl-link-event.c
new file mode 100644
index 0000000..ed5a577
--- /dev/null
+++ b/examples/rtnl-link-event.c
@@ -0,0 +1,70 @@
+#include <stdio.h>
+#include <stdlib.h>
+#include <unistd.h>
+
+#include <libmnl/libmnl.h>
+#include <linux/if.h>
+#include <linux/if_link.h>
+#include <linux/rtnetlink.h>
+
+static int data_cb(const struct nlmsghdr *nlh, void *data)
+{
+ struct nlattr *tb[IFLA_MAX+1];
+ struct ifinfomsg *ifm = mnl_nlmsg_get_data(nlh);
+ int len = mnl_nlmsg_get_len(nlh);
+ struct nlattr *attr;
+
+ printf("index=%d type=%d flags=%d family=%d ",
+ ifm->ifi_index, ifm->ifi_type,
+ ifm->ifi_flags, ifm->ifi_family);
+
+ if (ifm->ifi_flags & IFF_RUNNING)
+ printf("[RUNNING] ");
+ else
+ printf("[NOT RUNNING] ");
+
+ mnl_attr_parse_at_offset(nlh, sizeof(*ifm), tb, IFLA_MAX);
+ if (tb[IFLA_MTU]) {
+ 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("\n");
+ return MNL_CB_OK;
+}
+
+int main()
+{
+ struct mnl_socket *nl;
+ char buf[getpagesize()];
+ struct nlmsghdr *nlh = (struct nlmsghdr *) buf;
+ int ret;
+
+ nl = mnl_socket_open(NETLINK_ROUTE);
+ if (nl == NULL) {
+ perror("mnl_socket_open");
+ exit(EXIT_FAILURE);
+ }
+
+ if (mnl_socket_bind(nl, RTMGRP_LINK, 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, 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;
+}
diff --git a/examples/rtnl-link-set.c b/examples/rtnl-link-set.c
new file mode 100644
index 0000000..36bf355
--- /dev/null
+++ b/examples/rtnl-link-set.c
@@ -0,0 +1,76 @@
+#include <stdio.h>
+#include <stdlib.h>
+#include <unistd.h>
+
+#include <libmnl/libmnl.h>
+#include <linux/if.h>
+#include <linux/if_link.h>
+#include <linux/rtnetlink.h>
+
+int main(int argc, char *argv[])
+{
+ struct mnl_socket *nl;
+ char buf[getpagesize()];
+ struct nlmsghdr *nlh;
+ struct ifinfomsg *ifm;
+ int ret;
+ unsigned int seq, oper;
+
+ if (argc != 3) {
+ printf("Usage: %s [ifname] [up|down]\n", argv[0]);
+ exit(EXIT_FAILURE);
+ }
+
+ if (strncasecmp(argv[2], "up") == 0)
+ oper = IF_OPER_UP;
+ else if (strncasecmp(argv[2], "down") == 0)
+ oper = IF_OPER_DOWN;
+ else {
+ fprintf(stderr, "%s is not `up' nor `down'\n", argv[2]);
+ exit(EXIT_FAILURE);
+ }
+
+ nlh = mnl_nlmsg_put_header(buf);
+ nlh->nlmsg_type = RTM_SETLINK;
+ nlh->nlmsg_flags = NLM_F_REQUEST | NLM_F_ACK;
+ nlh->nlmsg_seq = seq = time(NULL);
+ ifm = mnl_nlmsg_put_extra_header(nlh, sizeof(*ifm));
+ ifm->ifi_family = AF_PACKET;
+
+ mnl_attr_put_u8(nlh, IFLA_OPERSTATE, oper);
+ mnl_attr_put_str(nlh, IFLA_IFNAME, argv[1]);
+
+ 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);
+ }
+
+ mnl_nlmsg_print(nlh);
+
+ if (mnl_socket_sendto(nl, nlh, mnl_nlmsg_get_len(nlh)) < 0) {
+ perror("mnl_socket_send");
+ exit(EXIT_FAILURE);
+ }
+
+ ret = mnl_socket_recvfrom(nl, buf, sizeof(buf));
+ if (ret == -1) {
+ perror("read");
+ exit(EXIT_FAILURE);
+ }
+
+ ret = mnl_cb_run(buf, ret, seq, NULL, NULL);
+ if (ret == -1){
+ perror("callback");
+ exit(EXIT_FAILURE);
+ }
+
+ mnl_socket_close(nl);
+
+ return 0;
+}
diff --git a/examples/rtnl-route-dump.c b/examples/rtnl-route-dump.c
new file mode 100644
index 0000000..e8f3a0c
--- /dev/null
+++ b/examples/rtnl-route-dump.c
@@ -0,0 +1,192 @@
+#include <stdio.h>
+#include <stdlib.h>
+#include <unistd.h>
+
+#include <libmnl/libmnl.h>
+#include <linux/if.h>
+#include <linux/if_link.h>
+#include <linux/rtnetlink.h>
+
+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]) {
+ printf("dst=%s ", inet_ntoa(mnl_attr_get_u32(tb[RTA_DST])));
+ }
+ if (tb[RTA_SRC]) {
+ printf("src=%s ", inet_ntoa(mnl_attr_get_u32(tb[RTA_SRC])));
+ }
+ 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]) {
+ printf("prefsrc=%s ",
+ inet_ntoa(mnl_attr_get_u32(tb[RTA_PREFSRC])));
+ }
+ if (tb[RTA_GATEWAY]) {
+ printf("gw=%s ", inet_ntoa(mnl_attr_get_u32(tb[RTA_GATEWAY])));
+ }
+ if (tb[RTA_METRICS]) {
+ int i;
+ struct nlattr *tbx[RTAX_MAX+1] = {};
+
+ mnl_attr_parse_nested(tb[RTA_METRICS], tbx, RTAX_MAX);
+
+ for (i=0; i<RTAX_MAX; i++) {
+ if (tbx[i]) {
+ printf("metrics[%d]=%u ",
+ mnl_attr_get_u32(tbx[i]));
+ }
+ }
+ }
+ printf("\n");
+}
+
+static int data_cb(const struct nlmsghdr *nlh, void *data)
+{
+ /* parse() ya está inicializando este array, qué hacer ? */
+ struct nlattr *tb[RTA_MAX+1] = {};
+ struct rtmsg *rm = mnl_nlmsg_get_data(nlh);
+ int len = mnl_nlmsg_get_len(nlh);
+ struct nlattr *attr;
+
+ /* 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\n", rm->rtm_flags);
+
+ mnl_attr_parse_at_offset(nlh, sizeof(*rm), tb, RTA_MAX);
+
+ switch(rm->rtm_family) {
+ case AF_INET:
+ attributes_show_ipv4(tb);
+ break;
+ }
+
+ return MNL_CB_OK;
+}
+
+int main()
+{
+ struct mnl_socket *nl;
+ char buf[getpagesize()];
+ struct nlmsghdr *nlh;
+ struct rtmsg *rtm;
+ int ret;
+ unsigned int seq;
+
+ nlh = mnl_nlmsg_put_header(buf);
+ nlh->nlmsg_type = RTM_GETROUTE;
+ nlh->nlmsg_flags = NLM_F_REQUEST | NLM_F_DUMP;
+ nlh->nlmsg_seq = seq = time(NULL);
+ rtm = mnl_nlmsg_put_extra_header(nlh, sizeof(struct rtmsg));
+ rtm->rtm_family = AF_INET;
+
+ 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);
+ }
+
+ if (mnl_socket_sendto(nl, nlh, mnl_nlmsg_get_len(nlh)) < 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, 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;
+}