8 #include <libmnl/libmnl.h>
10 #include <linux/if_link.h>
11 #include <linux/rtnetlink.h>
13 int main(
int argc,
char *argv[])
16 char buf[MNL_SOCKET_BUFFER_SIZE];
18 struct ifinfomsg *ifm;
20 unsigned int seq, portid, change = 0, flags = 0;
23 printf(
"Usage: %s [ifname] [up|down]\n", argv[0]);
27 if (strncasecmp(argv[2],
"up", strlen(
"up")) == 0) {
30 }
else if (strncasecmp(argv[2],
"down", strlen(
"down")) == 0) {
34 fprintf(stderr,
"%s is not `up' nor `down'\n", argv[2]);
38 nlh = mnl_nlmsg_put_header(buf);
39 nlh->nlmsg_type = RTM_NEWLINK;
40 nlh->nlmsg_flags = NLM_F_REQUEST | NLM_F_ACK;
41 nlh->nlmsg_seq = seq = time(NULL);
42 ifm = mnl_nlmsg_put_extra_header(nlh,
sizeof(*ifm));
43 ifm->ifi_family = AF_UNSPEC;
44 ifm->ifi_change = change;
45 ifm->ifi_flags = flags;
47 mnl_attr_put_str(nlh, IFLA_IFNAME, argv[1]);
49 nl = mnl_socket_open(NETLINK_ROUTE);
51 perror(
"mnl_socket_open");
55 if (mnl_socket_bind(nl, 0, MNL_SOCKET_AUTOPID) < 0) {
56 perror(
"mnl_socket_bind");
59 portid = mnl_socket_get_portid(nl);
61 mnl_nlmsg_fprintf(stdout, nlh, nlh->nlmsg_len,
62 sizeof(
struct ifinfomsg));
64 if (mnl_socket_sendto(nl, nlh, nlh->nlmsg_len) < 0) {
65 perror(
"mnl_socket_sendto");
69 ret = mnl_socket_recvfrom(nl, buf,
sizeof(buf));
71 perror(
"mnl_socket_recvfrom");
75 ret = mnl_cb_run(buf, ret, seq, portid, NULL, NULL);