Specify dependencies libs explicitly.

Such setup is better for cross compilation. Also changed Makefile to
properly make the project with these libraries.
This commit is contained in:
Vadim Vetrov
2024-07-22 22:46:16 +03:00
parent 4a4519cbac
commit 822266b74b
131 changed files with 17984 additions and 18 deletions

12
deps/libmnl/examples/rtnl/.gitignore vendored Normal file
View File

@@ -0,0 +1,12 @@
/rtnl-addr-add
/rtnl-addr-dump
/rtnl-link-can
/rtnl-link-dump
/rtnl-link-dump2
/rtnl-link-dump3
/rtnl-link-event
/rtnl-link-set
/rtnl-neigh-dump
/rtnl-route-event
/rtnl-route-add
/rtnl-route-dump

48
deps/libmnl/examples/rtnl/Makefile.am vendored Normal file
View File

@@ -0,0 +1,48 @@
include $(top_srcdir)/Make_global.am
check_PROGRAMS = rtnl-addr-add \
rtnl-addr-dump \
rtnl-link-can \
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-neigh-dump
rtnl_addr_add_SOURCES = rtnl-addr-add.c
rtnl_addr_add_LDADD = ../../src/libmnl.la
rtnl_link_can_SOURCES = rtnl-link-can.c
rtnl_link_can_LDADD = ../../src/libmnl.la
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
rtnl_link_dump2_SOURCES = rtnl-link-dump2.c
rtnl_link_dump2_LDADD = ../../src/libmnl.la
rtnl_link_dump3_SOURCES = rtnl-link-dump3.c
rtnl_link_dump3_LDADD = ../../src/libmnl.la
rtnl_route_add_SOURCES = rtnl-route-add.c
rtnl_route_add_LDADD = ../../src/libmnl.la
rtnl_link_event_SOURCES = rtnl-link-event.c
rtnl_link_event_LDADD = ../../src/libmnl.la
rtnl_link_set_SOURCES = rtnl-link-set.c
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
rtnl_neigh_dump_SOURCES = rtnl-neigh-dump.c
rtnl_neigh_dump_LDADD = ../../src/libmnl.la

View File

@@ -0,0 +1,119 @@
/* This example is placed in the public domain. */
#include <netinet/in.h>
#include <arpa/inet.h>
#include <time.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <strings.h>
#include <net/if.h>
#include <libmnl/libmnl.h>
#include <linux/if_link.h>
#include <linux/rtnetlink.h>
int main(int argc, char *argv[])
{
struct mnl_socket *nl;
char buf[MNL_SOCKET_BUFFER_SIZE];
struct nlmsghdr *nlh;
struct ifaddrmsg *ifm;
uint32_t seq, portid;
union {
in_addr_t ip;
struct in6_addr ip6;
} addr;
int ret, family = AF_INET;
uint32_t prefix;
int iface;
if (argc <= 3) {
printf("Usage: %s iface destination cidr\n", argv[0]);
printf("Example: %s eth0 10.0.1.12 32\n", argv[0]);
printf(" %s eth0 ffff::10.0.1.12 128\n", argv[0]);
exit(EXIT_FAILURE);
}
iface = if_nametoindex(argv[1]);
if (iface == 0) {
perror("if_nametoindex");
exit(EXIT_FAILURE);
}
if (!inet_pton(AF_INET, argv[2], &addr)) {
if (!inet_pton(AF_INET6, argv[2], &addr)) {
perror("inet_pton");
exit(EXIT_FAILURE);
}
family = AF_INET6;
}
if (sscanf(argv[3], "%u", &prefix) == 0) {
perror("sscanf");
exit(EXIT_FAILURE);
}
nlh = mnl_nlmsg_put_header(buf);
nlh->nlmsg_type = RTM_NEWADDR;
nlh->nlmsg_flags = NLM_F_REQUEST | NLM_F_CREATE | NLM_F_REPLACE | NLM_F_ACK;
nlh->nlmsg_seq = seq = time(NULL);
ifm = mnl_nlmsg_put_extra_header(nlh, sizeof(struct ifaddrmsg));
ifm->ifa_family = family;
ifm->ifa_prefixlen = prefix;
ifm->ifa_flags = IFA_F_PERMANENT;
ifm->ifa_scope = RT_SCOPE_UNIVERSE;
ifm->ifa_index = iface;
/*
* The exact meaning of IFA_LOCAL and IFA_ADDRESS depend
* on the address family being used and the device type.
* For broadcast devices (like the interfaces we use),
* for IPv4 we specify both and they are used interchangeably.
* For IPv6, only IFA_ADDRESS needs to be set.
*/
if (family == AF_INET) {
mnl_attr_put_u32(nlh, IFA_LOCAL, addr.ip);
mnl_attr_put_u32(nlh, IFA_ADDRESS, addr.ip);
} else {
mnl_attr_put(nlh, IFA_ADDRESS, sizeof(struct in6_addr), &addr);
}
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_sendto");
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;
}

View File

@@ -0,0 +1,133 @@
/* This example is placed in the public domain. */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <time.h>
#include <arpa/inet.h>
#include <libmnl/libmnl.h>
#include <linux/if.h>
#include <linux/if_link.h>
#include <linux/rtnetlink.h>
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[IFA_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[])
{
char buf[MNL_SOCKET_DUMP_SIZE];
unsigned int seq, portid;
struct mnl_socket *nl;
struct nlmsghdr *nlh;
struct rtgenmsg *rt;
int ret;
if (argc != 2) {
fprintf(stderr, "Usage: %s <inet|inet6>\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_sendto");
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;
}

View File

@@ -0,0 +1,452 @@
/* This example is placed in the public domain. */
#include <errno.h>
#include <limits.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <time.h>
#include <libmnl/libmnl.h>
#include <linux/can/netlink.h>
#include <linux/if.h>
#include <linux/if_link.h>
#include <linux/rtnetlink.h>
static void incomplete_command(void) __attribute__((noreturn));
#define NEXT_ARG() \
do { \
if (argc <= 0) incomplete_command(); \
argv++; \
argc--; \
} while (0)
static void duparg2(const char *key, const char *arg)
{
fprintf(stderr,
"Error: either \"%s\" is duplicate, or \"%s\" is a garbage.\n",
key, arg);
exit(-1);
}
static void incomplete_command(void)
{
fprintf(stderr, "Command line is not complete. Try option \"help\"\n");
exit(EXIT_FAILURE);
}
/* Returns false if 'prefix' is a not empty prefix of 'string'.
*/
static bool matches(const char *prefix, const char *string)
{
if (!*prefix)
return true;
while (*string && *prefix == *string) {
prefix++;
string++;
}
return !!*prefix;
}
static int get_u16(__u16 *val, const char *arg, int base)
{
unsigned long res;
char *ptr;
if (!arg || !*arg)
return -1;
res = strtoul(arg, &ptr, base);
/* empty string or trailing non-digits */
if (!ptr || ptr == arg || *ptr)
return -1;
/* overflow */
if (res == ULONG_MAX && errno == ERANGE)
return -1;
if (res > 0xFFFFUL)
return -1;
*val = res;
return 0;
}
static int get_u32(__u32 *val, const char *arg, int base)
{
unsigned long res;
char *ptr;
if (!arg || !*arg)
return -1;
res = strtoul(arg, &ptr, base);
/* empty string or trailing non-digits */
if (!ptr || ptr == arg || *ptr)
return -1;
/* overflow */
if (res == ULONG_MAX && errno == ERANGE)
return -1;
/* in case UL > 32 bits */
if (res > 0xFFFFFFFFUL)
return -1;
*val = res;
return 0;
}
static int get_float(float *val, const char *arg)
{
float res;
char *ptr;
if (!arg || !*arg)
return -1;
res = strtof(arg, &ptr);
if (!ptr || ptr == arg || *ptr)
return -1;
*val = res;
return 0;
}
static void set_ctrlmode(char *name, char *arg,
struct can_ctrlmode *cm, __u32 flags)
{
if (strcmp(arg, "on") == 0) {
cm->flags |= flags;
} else if (strcmp(arg, "off") != 0) {
fprintf(stderr,
"Error: argument of \"%s\" must be \"on\" or \"off\", not \"%s\"\n",
name, arg);
exit(EXIT_FAILURE);
}
cm->mask |= flags;
}
static void invarg(const char *msg, const char *arg)
{
fprintf(stderr, "Error: argument \"%s\" is wrong: %s\n", arg, msg);
exit(-1);
}
static void print_usage(FILE *f)
{
fprintf(f,
"Usage: ip link set DEVICE type can\n"
"\t[ bitrate BITRATE [ sample-point SAMPLE-POINT] ] |\n"
"\t[ tq TQ prop-seg PROP_SEG phase-seg1 PHASE-SEG1\n \t phase-seg2 PHASE-SEG2 [ sjw SJW ] ]\n"
"\n"
"\t[ dbitrate BITRATE [ dsample-point SAMPLE-POINT] ] |\n"
"\t[ dtq TQ dprop-seg PROP_SEG dphase-seg1 PHASE-SEG1\n \t dphase-seg2 PHASE-SEG2 [ dsjw SJW ] ]\n"
"\n"
"\t[ loopback { on | off } ]\n"
"\t[ listen-only { on | off } ]\n"
"\t[ triple-sampling { on | off } ]\n"
"\t[ one-shot { on | off } ]\n"
"\t[ berr-reporting { on | off } ]\n"
"\t[ fd { on | off } ]\n"
"\t[ fd-non-iso { on | off } ]\n"
"\t[ presume-ack { on | off } ]\n"
"\t[ cc-len8-dlc { on | off } ]\n"
"\n"
"\t[ restart-ms TIME-MS ]\n"
"\t[ restart ]\n"
"\n"
"\t[ termination { 0..65535 } ]\n"
"\n"
"\tWhere: BITRATE := { 1..1000000 }\n"
"\t SAMPLE-POINT := { 0.000..0.999 }\n"
"\t TQ := { NUMBER }\n"
"\t PROP-SEG := { 1..8 }\n"
"\t PHASE-SEG1 := { 1..8 }\n"
"\t PHASE-SEG2 := { 1..8 }\n"
"\t SJW := { 1..4 }\n"
"\t RESTART-MS := { 0 | NUMBER }\n"
);
}
static void usage(void)
{
print_usage(stderr);
}
static int iplink_set_can_parse(int argc, char **argv, struct nlmsghdr *nlh)
{
struct can_bittiming bt = {}, dbt = {};
struct can_ctrlmode cm = {};
while (argc > 0) {
if (matches(*argv, "bitrate") == 0) {
NEXT_ARG();
if (get_u32(&bt.bitrate, *argv, 0))
invarg("invalid \"bitrate\" value\n", *argv);
} else if (matches(*argv, "sample-point") == 0) {
float sp;
NEXT_ARG();
if (get_float(&sp, *argv))
invarg("invalid \"sample-point\" value\n",
*argv);
bt.sample_point = (__u32)(sp * 1000);
} else if (matches(*argv, "tq") == 0) {
NEXT_ARG();
if (get_u32(&bt.tq, *argv, 0))
invarg("invalid \"tq\" value\n", *argv);
} else if (matches(*argv, "prop-seg") == 0) {
NEXT_ARG();
if (get_u32(&bt.prop_seg, *argv, 0))
invarg("invalid \"prop-seg\" value\n", *argv);
} else if (matches(*argv, "phase-seg1") == 0) {
NEXT_ARG();
if (get_u32(&bt.phase_seg1, *argv, 0))
invarg("invalid \"phase-seg1\" value\n", *argv);
} else if (matches(*argv, "phase-seg2") == 0) {
NEXT_ARG();
if (get_u32(&bt.phase_seg2, *argv, 0))
invarg("invalid \"phase-seg2\" value\n", *argv);
} else if (matches(*argv, "sjw") == 0) {
NEXT_ARG();
if (get_u32(&bt.sjw, *argv, 0))
invarg("invalid \"sjw\" value\n", *argv);
} else if (matches(*argv, "dbitrate") == 0) {
NEXT_ARG();
if (get_u32(&dbt.bitrate, *argv, 0))
invarg("invalid \"dbitrate\" value\n", *argv);
} else if (matches(*argv, "dsample-point") == 0) {
float sp;
NEXT_ARG();
if (get_float(&sp, *argv))
invarg("invalid \"dsample-point\" value\n", *argv);
dbt.sample_point = (__u32)(sp * 1000);
} else if (matches(*argv, "dtq") == 0) {
NEXT_ARG();
if (get_u32(&dbt.tq, *argv, 0))
invarg("invalid \"dtq\" value\n", *argv);
} else if (matches(*argv, "dprop-seg") == 0) {
NEXT_ARG();
if (get_u32(&dbt.prop_seg, *argv, 0))
invarg("invalid \"dprop-seg\" value\n", *argv);
} else if (matches(*argv, "dphase-seg1") == 0) {
NEXT_ARG();
if (get_u32(&dbt.phase_seg1, *argv, 0))
invarg("invalid \"dphase-seg1\" value\n", *argv);
} else if (matches(*argv, "dphase-seg2") == 0) {
NEXT_ARG();
if (get_u32(&dbt.phase_seg2, *argv, 0))
invarg("invalid \"dphase-seg2\" value\n", *argv);
} else if (matches(*argv, "dsjw") == 0) {
NEXT_ARG();
if (get_u32(&dbt.sjw, *argv, 0))
invarg("invalid \"dsjw\" value\n", *argv);
} else if (matches(*argv, "loopback") == 0) {
NEXT_ARG();
set_ctrlmode("loopback", *argv, &cm,
CAN_CTRLMODE_LOOPBACK);
} else if (matches(*argv, "listen-only") == 0) {
NEXT_ARG();
set_ctrlmode("listen-only", *argv, &cm,
CAN_CTRLMODE_LISTENONLY);
} else if (matches(*argv, "triple-sampling") == 0) {
NEXT_ARG();
set_ctrlmode("triple-sampling", *argv, &cm,
CAN_CTRLMODE_3_SAMPLES);
} else if (matches(*argv, "one-shot") == 0) {
NEXT_ARG();
set_ctrlmode("one-shot", *argv, &cm,
CAN_CTRLMODE_ONE_SHOT);
} else if (matches(*argv, "berr-reporting") == 0) {
NEXT_ARG();
set_ctrlmode("berr-reporting", *argv, &cm,
CAN_CTRLMODE_BERR_REPORTING);
} else if (matches(*argv, "fd") == 0) {
NEXT_ARG();
set_ctrlmode("fd", *argv, &cm,
CAN_CTRLMODE_FD);
} else if (matches(*argv, "fd-non-iso") == 0) {
NEXT_ARG();
set_ctrlmode("fd-non-iso", *argv, &cm,
CAN_CTRLMODE_FD_NON_ISO);
} else if (matches(*argv, "presume-ack") == 0) {
NEXT_ARG();
set_ctrlmode("presume-ack", *argv, &cm,
CAN_CTRLMODE_PRESUME_ACK);
#if defined(CAN_CTRLMODE_CC_LEN8_DLC)
} else if (matches(*argv, "cc-len8-dlc") == 0) {
NEXT_ARG();
set_ctrlmode("cc-len8-dlc", *argv, &cm,
CAN_CTRLMODE_CC_LEN8_DLC);
#endif
} else if (matches(*argv, "restart") == 0) {
__u32 val = 1;
mnl_attr_put(nlh, IFLA_CAN_RESTART, sizeof(val), &val);
} else if (matches(*argv, "restart-ms") == 0) {
__u32 val;
NEXT_ARG();
if (get_u32(&val, *argv, 0))
invarg("invalid \"restart-ms\" value\n", *argv);
mnl_attr_put(nlh, IFLA_CAN_RESTART_MS, sizeof(val), &val);
} else if (matches(*argv, "termination") == 0) {
__u16 val;
NEXT_ARG();
if (get_u16(&val, *argv, 0))
invarg("invalid \"termination\" value\n",
*argv);
mnl_attr_put(nlh, IFLA_CAN_TERMINATION, sizeof(val), &val);
} else {
fprintf(stderr, "unknown option \"%s\"\n", *argv);
usage();
return -1;
}
NEXT_ARG();
}
if (bt.bitrate || bt.tq)
mnl_attr_put(nlh, IFLA_CAN_BITTIMING, sizeof(bt), &bt);
if (cm.mask)
mnl_attr_put(nlh, IFLA_CAN_CTRLMODE, sizeof(cm), &cm);
return 0;
}
int main(int argc, char *argv[])
{
char buf[MNL_SOCKET_BUFFER_SIZE];
struct mnl_socket *nl;
struct nlmsghdr *nlh;
struct ifinfomsg *ifm;
int ret;
unsigned int seq, portid;
struct nlattr *linkinfo, *data;
const char *signatures[] = {
"ip", "link", "set", ""
};
char *type = NULL;
char *dev = NULL;
int i;
NEXT_ARG();
for (i = 0; argc > 0 && signatures[i][0];) {
if (matches(*argv, signatures[i]))
incomplete_command();
NEXT_ARG();
i++;
}
if (argc == 0)
incomplete_command();
nlh = mnl_nlmsg_put_header(buf);
nlh->nlmsg_type = RTM_NEWLINK;
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_UNSPEC;
ifm->ifi_change = 0;
ifm->ifi_flags = 0;
while (argc > 0) {
if (matches(*argv, "up") == 0) {
ifm->ifi_change |= IFF_UP;
ifm->ifi_flags |= IFF_UP;
} else if (matches(*argv, "down") == 0) {
ifm->ifi_change |= IFF_UP;
ifm->ifi_flags &= ~IFF_UP;
} else if (matches(*argv, "type") == 0) {
NEXT_ARG();
type = *argv;
NEXT_ARG();
break;
} else if (matches(*argv, "help") == 0) {
usage();
exit(EXIT_FAILURE);
} else {
if (matches(*argv, "dev") == 0)
NEXT_ARG();
if (dev)
duparg2("dev", *argv);
dev = *argv;
}
NEXT_ARG();
}
if (dev)
mnl_attr_put_str(nlh, IFLA_IFNAME, dev);
if (type) {
if (matches(type, "can")) {
fprintf(stderr, "unknown type \"%s\"\n", type);
usage();
exit(EXIT_FAILURE);
}
linkinfo = mnl_attr_nest_start(nlh, IFLA_LINKINFO);
mnl_attr_put_str(nlh, IFLA_INFO_KIND, "can");
data = mnl_attr_nest_start(nlh, IFLA_INFO_DATA);
if (iplink_set_can_parse(argc, argv, nlh))
return -1;
mnl_attr_nest_end(nlh, data);
mnl_attr_nest_end(nlh, linkinfo);
}
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);
mnl_nlmsg_fprintf(stdout, nlh, nlh->nlmsg_len,
sizeof(struct ifinfomsg));
if (mnl_socket_sendto(nl, nlh, nlh->nlmsg_len) < 0) {
perror("mnl_socket_sendto");
exit(EXIT_FAILURE);
}
ret = mnl_socket_recvfrom(nl, buf, sizeof(buf));
if (ret == -1) {
perror("mnl_socket_recvfrom");
exit(EXIT_FAILURE);
}
ret = mnl_cb_run(buf, ret, seq, portid, NULL, NULL);
if (ret == -1) {
perror("mnl_cb_run");
exit(EXIT_FAILURE);
}
mnl_socket_close(nl);
return 0;
}

View File

@@ -0,0 +1,130 @@
/* This example is placed in the public domain. */
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <time.h>
#include <arpa/inet.h>
#include <libmnl/libmnl.h>
#include <linux/if.h>
#include <linux/if_link.h>
#include <linux/rtnetlink.h>
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, IFLA_MAX) < 0)
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");
return MNL_CB_ERROR;
}
break;
case IFLA_IFNAME:
if (mnl_attr_validate(attr, MNL_TYPE_STRING) < 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 ifinfomsg *ifm = mnl_nlmsg_get_payload(nlh);
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(nlh, sizeof(*ifm), data_attr_cb, tb);
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]));
}
if (tb[IFLA_ADDRESS]) {
uint8_t *hwaddr = mnl_attr_get_payload(tb[IFLA_ADDRESS]);
int i;
printf("hwaddr=");
for (i=0; i<mnl_attr_get_payload_len(tb[IFLA_ADDRESS]); i++) {
printf("%.2x", hwaddr[i] & 0xff);
if (i+1 != mnl_attr_get_payload_len(tb[IFLA_ADDRESS]))
printf(":");
}
}
printf("\n");
return MNL_CB_OK;
}
int main(void)
{
char buf[MNL_SOCKET_DUMP_SIZE];
unsigned int seq, portid;
struct mnl_socket *nl;
struct nlmsghdr *nlh;
struct rtgenmsg *rt;
int ret;
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);
}
portid = mnl_socket_get_portid(nl);
if (mnl_socket_sendto(nl, nlh, nlh->nlmsg_len) < 0) {
perror("mnl_socket_sendto");
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;
}

View File

@@ -0,0 +1,103 @@
/* This example is placed in the public domain. */
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <time.h>
#include <libmnl/libmnl.h>
#include <linux/if.h>
#include <linux/if_link.h>
#include <linux/rtnetlink.h>
static int data_attr_cb(const struct nlattr *attr, void *data)
{
/* skip unsupported attribute in user-space */
if (mnl_attr_type_valid(attr, IFLA_MAX) < 0)
return MNL_CB_OK;
switch(mnl_attr_get_type(attr)) {
case IFLA_MTU:
if (mnl_attr_validate(attr, MNL_TYPE_U32) < 0) {
perror("mnl_attr_validate");
return MNL_CB_ERROR;
}
printf("mtu=%d ", mnl_attr_get_u32(attr));
break;
case IFLA_IFNAME:
if (mnl_attr_validate(attr, MNL_TYPE_STRING) < 0) {
perror("mnl_attr_validate");
return MNL_CB_ERROR;
}
printf("name=%s ", mnl_attr_get_str(attr));
break;
}
return MNL_CB_OK;
}
static int data_cb(const struct nlmsghdr *nlh, void *data)
{
struct ifinfomsg *ifm = mnl_nlmsg_get_payload(nlh);
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(nlh, sizeof(*ifm), data_attr_cb, NULL);
printf("\n");
return MNL_CB_OK;
}
int main(void)
{
char buf[MNL_SOCKET_DUMP_SIZE];
unsigned int seq, portid;
struct mnl_socket *nl;
struct nlmsghdr *nlh;
struct rtgenmsg *rt;
int ret;
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);
}
portid = mnl_socket_get_portid(nl);
if (mnl_socket_sendto(nl, nlh, nlh->nlmsg_len) < 0) {
perror("mnl_socket_sendto");
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;
}

View File

@@ -0,0 +1,103 @@
/* This example is placed in the public domain. */
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <time.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 ifinfomsg *ifm = mnl_nlmsg_get_payload(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_for_each(attr, nlh, sizeof(*ifm)) {
int type = mnl_attr_get_type(attr);
/* skip unsupported attribute in user-space */
if (mnl_attr_type_valid(attr, IFLA_MAX) < 0)
continue;
switch(type) {
case IFLA_MTU:
if (mnl_attr_validate(attr, MNL_TYPE_U32) < 0) {
perror("mnl_attr_validate");
return MNL_CB_ERROR;
}
printf("mtu=%d ", mnl_attr_get_u32(attr));
break;
case IFLA_IFNAME:
if (mnl_attr_validate(attr, MNL_TYPE_STRING) < 0) {
perror("mnl_attr_validate");
return MNL_CB_ERROR;
}
printf("name=%s ", mnl_attr_get_str(attr));
break;
}
}
printf("\n");
return MNL_CB_OK;
}
int main(void)
{
char buf[MNL_SOCKET_DUMP_SIZE];
unsigned int seq, portid;
struct mnl_socket *nl;
struct nlmsghdr *nlh;
struct rtgenmsg *rt;
int ret;
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);
}
portid = mnl_socket_get_portid(nl);
if (mnl_socket_sendto(nl, nlh, nlh->nlmsg_len) < 0) {
perror("mnl_socket_sendto");
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;
}

View File

@@ -0,0 +1,95 @@
/* This example is placed in the public domain. */
#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_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, IFLA_MAX) < 0)
return MNL_CB_OK;
switch(type) {
case IFLA_MTU:
if (mnl_attr_validate(attr, MNL_TYPE_U32) < 0) {
perror("mnl_attr_validate");
return MNL_CB_ERROR;
}
break;
case IFLA_IFNAME:
if (mnl_attr_validate(attr, MNL_TYPE_STRING) < 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 ifinfomsg *ifm = mnl_nlmsg_get_payload(nlh);
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(nlh, sizeof(*ifm), data_attr_cb, tb);
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(void)
{
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_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, 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;
}

View File

@@ -0,0 +1,84 @@
/* This example is placed in the public domain. */
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <time.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[MNL_SOCKET_BUFFER_SIZE];
struct nlmsghdr *nlh;
struct ifinfomsg *ifm;
int ret;
unsigned int seq, portid, change = 0, flags = 0;
if (argc != 3) {
printf("Usage: %s [ifname] [up|down]\n", argv[0]);
exit(EXIT_FAILURE);
}
if (strncasecmp(argv[2], "up", strlen("up")) == 0) {
change |= IFF_UP;
flags |= IFF_UP;
} else if (strncasecmp(argv[2], "down", strlen("down")) == 0) {
change |= IFF_UP;
flags &= ~IFF_UP;
} 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_NEWLINK;
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_UNSPEC;
ifm->ifi_change = change;
ifm->ifi_flags = flags;
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);
}
portid = mnl_socket_get_portid(nl);
mnl_nlmsg_fprintf(stdout, nlh, nlh->nlmsg_len,
sizeof(struct ifinfomsg));
if (mnl_socket_sendto(nl, nlh, nlh->nlmsg_len) < 0) {
perror("mnl_socket_sendto");
exit(EXIT_FAILURE);
}
ret = mnl_socket_recvfrom(nl, buf, sizeof(buf));
if (ret == -1) {
perror("mnl_socket_recvfrom");
exit(EXIT_FAILURE);
}
ret = mnl_cb_run(buf, ret, seq, portid, NULL, NULL);
if (ret == -1){
perror("mnl_cb_run");
exit(EXIT_FAILURE);
}
mnl_socket_close(nl);
return 0;
}

View File

@@ -0,0 +1,158 @@
/* This example is placed in the public domain. */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <time.h>
#include <arpa/inet.h>
#include <libmnl/libmnl.h>
#include <linux/if.h>
#include <linux/if_link.h>
#include <linux/rtnetlink.h>
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, NDA_MAX) < 0)
return MNL_CB_OK;
switch(type) {
case NDA_DST:
case NDA_LLADDR:
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[NDA_MAX + 1] = {};
struct ndmsg *ndm = mnl_nlmsg_get_payload(nlh);
printf("index=%d family=%d ", ndm->ndm_ifindex, ndm->ndm_family);
mnl_attr_parse(nlh, sizeof(*ndm), data_attr_cb, tb);
printf("dst=");
if (tb[NDA_DST]) {
void *addr = mnl_attr_get_payload(tb[NDA_DST]);
char out[INET6_ADDRSTRLEN];
if (inet_ntop(ndm->ndm_family, addr, out, sizeof(out)))
printf("%s ", out);
}
mnl_attr_parse(nlh, sizeof(*ndm), data_attr_cb, tb);
printf("lladdr=");
if (tb[NDA_LLADDR]) {
void *addr = mnl_attr_get_payload(tb[NDA_LLADDR]);
unsigned char lladdr[6] = {0};
if (memcpy(&lladdr, addr, 6))
printf("%02x:%02x:%02x:%02x:%02x:%02x ",
lladdr[0], lladdr[1], lladdr[2],
lladdr[3], lladdr[4], lladdr[5]);
}
printf("state=");
switch(ndm->ndm_state) {
case NUD_INCOMPLETE:
printf("incomplete ");
break;
case NUD_REACHABLE:
printf("reachable ");
break;
case NUD_STALE:
printf("stale ");
break;
case NUD_DELAY:
printf("delay ");
break;
case NUD_PROBE:
printf("probe ");
break;
case NUD_FAILED:
printf("failed ");
break;
case NUD_NOARP:
printf("noarp ");
break;
case NUD_PERMANENT:
printf("permanent ");
break;
default:
printf("%d ", ndm->ndm_state);
break;
}
printf("\n");
return MNL_CB_OK;
}
int main(int argc, char *argv[])
{
char buf[MNL_SOCKET_DUMP_SIZE];
unsigned int seq, portid;
struct mnl_socket *nl;
struct nlmsghdr *nlh;
struct ndmsg *nd;
int ret;
if (argc != 2) {
fprintf(stderr, "Usage: %s <inet|inet6>\n", argv[0]);
exit(EXIT_FAILURE);
}
nlh = mnl_nlmsg_put_header(buf);
nlh->nlmsg_type = RTM_GETNEIGH;
nlh->nlmsg_flags = NLM_F_REQUEST | NLM_F_DUMP;
nlh->nlmsg_seq = seq = time(NULL);
nd = mnl_nlmsg_put_extra_header(nlh, sizeof(struct ndmsg));
if (strcmp(argv[1], "inet") == 0)
nd->ndm_family = AF_INET;
else if (strcmp(argv[1], "inet6") == 0)
nd->ndm_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_sendto");
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;
}

View File

@@ -0,0 +1,127 @@
/* This example is placed in the public domain. */
#include <netinet/in.h>
#include <arpa/inet.h>
#include <time.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <strings.h>
#include <net/if.h>
#include <libmnl/libmnl.h>
#include <linux/if_link.h>
#include <linux/rtnetlink.h>
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);
}
iface = if_nametoindex(argv[1]);
if (iface == 0) {
perror("if_nametoindex");
exit(EXIT_FAILURE);
}
if (!inet_pton(AF_INET, argv[2], &dst)) {
if (!inet_pton(AF_INET6, argv[2], &dst)) {
perror("inet_pton");
exit(EXIT_FAILURE);
}
family = AF_INET6;
}
if (sscanf(argv[3], "%u", &prefix) == 0) {
perror("sscanf");
exit(EXIT_FAILURE);
}
if (argc == 5 && !inet_pton(family, argv[4], &gw)) {
perror("inet_pton");
exit(EXIT_FAILURE);
}
nlh = mnl_nlmsg_put_header(buf);
nlh->nlmsg_type = RTM_NEWROUTE;
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 = family;
rtm->rtm_dst_len = prefix;
rtm->rtm_src_len = 0;
rtm->rtm_tos = 0;
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;
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) {
if (family == AF_INET)
mnl_attr_put_u32(nlh, RTA_GATEWAY, gw.ip);
else {
mnl_attr_put(nlh, RTA_GATEWAY, sizeof(struct in6_addr),
&gw.ip6);
}
}
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_sendto");
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;
}

View File

@@ -0,0 +1,356 @@
/* This example is placed in the public domain. */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <time.h>
#include <arpa/inet.h>
#include <libmnl/libmnl.h>
#include <linux/if.h>
#include <linux/if_link.h>
#include <linux/rtnetlink.h>
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;
if (mnl_attr_validate(attr, MNL_TYPE_U32) < 0) {
perror("mnl_attr_validate");
return MNL_CB_ERROR;
}
tb[mnl_attr_get_type(attr)] = attr;
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_PRIORITY]) {
printf("prio=%u ", mnl_attr_get_u32(tb[RTA_PRIORITY]));
}
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; i<RTAX_MAX; i++) {
if (tbx[i]) {
printf("metrics[%d]=%u ",
i, mnl_attr_get_u32(tbx[i]));
}
}
}
}
/* like inet_ntoa(), not reentrant */
static const char *inet6_ntoa(struct in6_addr in6)
{
static char buf[INET6_ADDRSTRLEN];
return inet_ntop(AF_INET6, &in6.s6_addr, buf, sizeof(buf));
}
static void attributes_show_ipv6(struct nlattr *tb[])
{
if (tb[RTA_TABLE]) {
printf("table=%u ", mnl_attr_get_u32(tb[RTA_TABLE]));
}
if (tb[RTA_DST]) {
struct in6_addr *addr = mnl_attr_get_payload(tb[RTA_DST]);
printf("dst=%s ", inet6_ntoa(*addr));
}
if (tb[RTA_SRC]) {
struct in6_addr *addr = mnl_attr_get_payload(tb[RTA_SRC]);
printf("src=%s ", inet6_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 in6_addr *addr = mnl_attr_get_payload(tb[RTA_PREFSRC]);
printf("prefsrc=%s ", inet6_ntoa(*addr));
}
if (tb[RTA_GATEWAY]) {
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] = {};
mnl_attr_parse_nested(tb[RTA_METRICS], data_attr_cb2, tbx);
for (i=0; i<RTAX_MAX; i++) {
if (tbx[i]) {
printf("metrics[%d]=%u ",
i, mnl_attr_get_u32(tbx[i]));
}
}
}
}
static int data_ipv4_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, RTA_MAX) < 0)
return MNL_CB_OK;
switch(type) {
case RTA_TABLE:
case RTA_DST:
case RTA_SRC:
case RTA_OIF:
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;
}
break;
case RTA_METRICS:
if (mnl_attr_validate(attr, MNL_TYPE_NESTED) < 0) {
perror("mnl_attr_validate");
return MNL_CB_ERROR;
}
break;
}
tb[type] = attr;
return MNL_CB_OK;
}
static int data_ipv6_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, RTA_MAX) < 0)
return MNL_CB_OK;
switch(type) {
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;
}
break;
case RTA_DST:
case RTA_SRC:
case RTA_PREFSRC:
case RTA_GATEWAY:
if (mnl_attr_validate2(attr, MNL_TYPE_BINARY,
sizeof(struct in6_addr)) < 0) {
perror("mnl_attr_validate2");
return MNL_CB_ERROR;
}
break;
case RTA_METRICS:
if (mnl_attr_validate(attr, MNL_TYPE_NESTED) < 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[RTA_MAX+1] = {};
struct rtmsg *rm = mnl_nlmsg_get_payload(nlh);
/* 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[])
{
char buf[MNL_SOCKET_DUMP_SIZE];
unsigned int seq, portid;
struct mnl_socket *nl;
struct nlmsghdr *nlh;
struct rtmsg *rtm;
int ret;
if (argc != 2) {
fprintf(stderr, "Usage: %s <inet|inet6>\n", argv[0]);
exit(EXIT_FAILURE);
}
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));
if (strcmp(argv[1], "inet") == 0)
rtm->rtm_family = AF_INET;
else if (strcmp(argv[1], "inet6") == 0)
rtm->rtm_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_sendto");
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;
}

View File

@@ -0,0 +1,341 @@
/* This example is placed in the public domain. */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <time.h>
#include <arpa/inet.h>
#include <libmnl/libmnl.h>
#include <linux/if.h>
#include <linux/if_link.h>
#include <linux/rtnetlink.h>
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;
if (mnl_attr_validate(attr, MNL_TYPE_U32) < 0) {
perror("mnl_attr_validate");
return MNL_CB_ERROR;
}
tb[mnl_attr_get_type(attr)] = attr;
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_PRIORITY]) {
printf("prio=%u ", mnl_attr_get_u32(tb[RTA_PRIORITY]));
}
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; i<RTAX_MAX; i++) {
if (tbx[i]) {
printf("metrics[%d]=%u ",
i, mnl_attr_get_u32(tbx[i]));
}
}
}
}
/* like inet_ntoa(), not reentrant */
static const char *inet6_ntoa(struct in6_addr in6)
{
static char buf[INET6_ADDRSTRLEN];
return inet_ntop(AF_INET6, &in6.s6_addr, buf, sizeof(buf));
}
static void attributes_show_ipv6(struct nlattr *tb[])
{
if (tb[RTA_TABLE]) {
printf("table=%u ", mnl_attr_get_u32(tb[RTA_TABLE]));
}
if (tb[RTA_DST]) {
struct in6_addr *addr = mnl_attr_get_payload(tb[RTA_DST]);
printf("dst=%s ", inet6_ntoa(*addr));
}
if (tb[RTA_SRC]) {
struct in6_addr *addr = mnl_attr_get_payload(tb[RTA_SRC]);
printf("src=%s ", inet6_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 in6_addr *addr = mnl_attr_get_payload(tb[RTA_PREFSRC]);
printf("prefsrc=%s ", inet6_ntoa(*addr));
}
if (tb[RTA_GATEWAY]) {
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] = {};
mnl_attr_parse_nested(tb[RTA_METRICS], data_attr_cb2, tbx);
for (i=0; i<RTAX_MAX; i++) {
if (tbx[i]) {
printf("metrics[%d]=%u ",
i, mnl_attr_get_u32(tbx[i]));
}
}
}
}
static int data_ipv4_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, RTA_MAX) < 0)
return MNL_CB_OK;
switch(type) {
case RTA_TABLE:
case RTA_DST:
case RTA_SRC:
case RTA_OIF:
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;
}
break;
case RTA_METRICS:
if (mnl_attr_validate(attr, MNL_TYPE_NESTED) < 0) {
perror("mnl_attr_validate");
return MNL_CB_ERROR;
}
break;
}
tb[type] = attr;
return MNL_CB_OK;
}
static int data_ipv6_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, RTA_MAX) < 0)
return MNL_CB_OK;
switch(type) {
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;
}
break;
case RTA_DST:
case RTA_SRC:
case RTA_PREFSRC:
case RTA_GATEWAY:
if (mnl_attr_validate2(attr, MNL_TYPE_BINARY,
sizeof(struct in6_addr)) < 0) {
perror("mnl_attr_validate2");
return MNL_CB_ERROR;
}
break;
case RTA_METRICS:
if (mnl_attr_validate(attr, MNL_TYPE_NESTED) < 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[RTA_MAX+1] = {};
struct rtmsg *rm = mnl_nlmsg_get_payload(nlh);
switch(nlh->nlmsg_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;
}