| 1 |
/* This example is placed in the public domain. */ |
| 2 |
#include <stdio.h> |
| 3 |
#include <stdlib.h> |
| 4 |
#include <unistd.h> |
| 5 |
#include <time.h> |
| 6 |
#include <arpa/inet.h> |
| 7 |
|
| 8 |
#include <libmnl/libmnl.h> |
| 9 |
#include <linux/if.h> |
| 10 |
#include <linux/if_link.h> |
| 11 |
#include <linux/rtnetlink.h> |
| 12 |
|
| 13 |
static int data_attr_cb(const struct nlattr *attr, void *data) |
| 14 |
{ |
| 15 |
const struct nlattr **tb = data; |
| 16 |
int type = mnl_attr_get_type(attr); |
| 17 |
|
| 18 |
/* skip unsupported attribute in user-space */ |
| 19 |
if (mnl_attr_type_valid(attr, IFLA_MAX) < 0) |
| 20 |
return MNL_CB_OK; |
| 21 |
|
| 22 |
switch(type) { |
| 23 |
case IFLA_ADDRESS: |
| 24 |
if (mnl_attr_validate(attr, MNL_TYPE_BINARY) < 0) { |
| 25 |
perror("mnl_attr_validate"); |
| 26 |
return MNL_CB_ERROR; |
| 27 |
} |
| 28 |
break; |
| 29 |
case IFLA_MTU: |
| 30 |
if (mnl_attr_validate(attr, MNL_TYPE_U32) < 0) { |
| 31 |
perror("mnl_attr_validate"); |
| 32 |
return MNL_CB_ERROR; |
| 33 |
} |
| 34 |
break; |
| 35 |
case IFLA_IFNAME: |
| 36 |
if (mnl_attr_validate(attr, MNL_TYPE_STRING) < 0) { |
| 37 |
perror("mnl_attr_validate"); |
| 38 |
return MNL_CB_ERROR; |
| 39 |
} |
| 40 |
break; |
| 41 |
} |
| 42 |
tb[type] = attr; |
| 43 |
return MNL_CB_OK; |
| 44 |
} |
| 45 |
|
| 46 |
static int data_cb(const struct nlmsghdr *nlh, void *data) |
| 47 |
{ |
| 48 |
struct nlattr *tb[IFLA_MAX+1] = {}; |
| 49 |
struct ifinfomsg *ifm = mnl_nlmsg_get_payload(nlh); |
| 50 |
|
| 51 |
printf("index=%d type=%d flags=%d family=%d ", |
| 52 |
ifm->ifi_index, ifm->ifi_type, |
| 53 |
ifm->ifi_flags, ifm->ifi_family); |
| 54 |
|
| 55 |
if (ifm->ifi_flags & IFF_RUNNING) |
| 56 |
printf("[RUNNING] "); |
| 57 |
else |
| 58 |
printf("[NOT RUNNING] "); |
| 59 |
|
| 60 |
mnl_attr_parse(nlh, sizeof(*ifm), data_attr_cb, tb); |
| 61 |
if (tb[IFLA_MTU]) { |
| 62 |
printf("mtu=%d ", mnl_attr_get_u32(tb[IFLA_MTU])); |
| 63 |
} |
| 64 |
if (tb[IFLA_IFNAME]) { |
| 65 |
printf("name=%s ", mnl_attr_get_str(tb[IFLA_IFNAME])); |
| 66 |
} |
| 67 |
if (tb[IFLA_ADDRESS]) { |
| 68 |
uint8_t *hwaddr = mnl_attr_get_payload(tb[IFLA_ADDRESS]); |
| 69 |
int i; |
| 70 |
|
| 71 |
printf("hwaddr="); |
| 72 |
for (i=0; i<mnl_attr_get_payload_len(tb[IFLA_ADDRESS]); i++) { |
| 73 |
printf("%.2x", hwaddr[i] & 0xff); |
| 74 |
if (i+1 != mnl_attr_get_payload_len(tb[IFLA_ADDRESS])) |
| 75 |
printf(":"); |
| 76 |
} |
| 77 |
} |
| 78 |
printf("\n"); |
| 79 |
return MNL_CB_OK; |
| 80 |
} |
| 81 |
|
| 82 |
int main(void) |
| 83 |
{ |
| 84 |
char buf[MNL_SOCKET_DUMP_SIZE]; |
| 85 |
unsigned int seq, portid; |
| 86 |
struct mnl_socket *nl; |
| 87 |
struct nlmsghdr *nlh; |
| 88 |
struct rtgenmsg *rt; |
| 89 |
int ret; |
| 90 |
|
| 91 |
nlh = mnl_nlmsg_put_header(buf); |
| 92 |
nlh->nlmsg_type = RTM_GETLINK; |
| 93 |
nlh->nlmsg_flags = NLM_F_REQUEST | NLM_F_DUMP; |
| 94 |
nlh->nlmsg_seq = seq = time(NULL); |
| 95 |
rt = mnl_nlmsg_put_extra_header(nlh, sizeof(struct rtgenmsg)); |
| 96 |
rt->rtgen_family = AF_PACKET; |
| 97 |
|
| 98 |
nl = mnl_socket_open(NETLINK_ROUTE); |
| 99 |
if (nl == NULL) { |
| 100 |
perror("mnl_socket_open"); |
| 101 |
exit(EXIT_FAILURE); |
| 102 |
} |
| 103 |
|
| 104 |
if (mnl_socket_bind(nl, 0, MNL_SOCKET_AUTOPID) < 0) { |
| 105 |
perror("mnl_socket_bind"); |
| 106 |
exit(EXIT_FAILURE); |
| 107 |
} |
| 108 |
portid = mnl_socket_get_portid(nl); |
| 109 |
|
| 110 |
if (mnl_socket_sendto(nl, nlh, nlh->nlmsg_len) < 0) { |
| 111 |
perror("mnl_socket_sendto"); |
| 112 |
exit(EXIT_FAILURE); |
| 113 |
} |
| 114 |
|
| 115 |
ret = mnl_socket_recvfrom(nl, buf, sizeof(buf)); |
| 116 |
while (ret > 0) { |
| 117 |
ret = mnl_cb_run(buf, ret, seq, portid, data_cb, NULL); |
| 118 |
if (ret <= MNL_CB_STOP) |
| 119 |
break; |
| 120 |
ret = mnl_socket_recvfrom(nl, buf, sizeof(buf)); |
| 121 |
} |
| 122 |
if (ret == -1) { |
| 123 |
perror("error"); |
| 124 |
exit(EXIT_FAILURE); |
| 125 |
} |
| 126 |
|
| 127 |
mnl_socket_close(nl); |
| 128 |
|
| 129 |
return 0; |
| 130 |
} |