00001
00002
00003
00004
00005
00006
00007 #include <stdio.h>
00008 #include <stdlib.h>
00009 #include <unistd.h>
00010 #include <string.h>
00011 #include <time.h>
00012
00013 #include <libmnl/libmnl.h>
00014 #include <linux/if.h>
00015 #include <linux/if_link.h>
00016 #include <linux/rtnetlink.h>
00017
00018 int main(int argc, char *argv[])
00019 {
00020 struct mnl_socket *nl;
00021 char buf[MNL_SOCKET_BUFFER_SIZE];
00022 struct nlmsghdr *nlh;
00023 struct ifinfomsg *ifm;
00024 int ret;
00025 unsigned int seq, portid, change = 0, flags = 0;
00026
00027 if (argc != 3) {
00028 printf("Usage: %s [ifname] [up|down]\n", argv[0]);
00029 exit(EXIT_FAILURE);
00030 }
00031
00032 if (strncasecmp(argv[2], "up", strlen("up")) == 0) {
00033 change |= IFF_UP;
00034 flags |= IFF_UP;
00035 } else if (strncasecmp(argv[2], "down", strlen("down")) == 0) {
00036 change |= IFF_UP;
00037 flags &= ~IFF_UP;
00038 } else {
00039 fprintf(stderr, "%s is not `up' nor `down'\n", argv[2]);
00040 exit(EXIT_FAILURE);
00041 }
00042
00043 nlh = mnl_nlmsg_put_header(buf);
00044 nlh->nlmsg_type = RTM_NEWLINK;
00045 nlh->nlmsg_flags = NLM_F_REQUEST | NLM_F_ACK;
00046 nlh->nlmsg_seq = seq = time(NULL);
00047 ifm = mnl_nlmsg_put_extra_header(nlh, sizeof(*ifm));
00048 ifm->ifi_family = AF_UNSPEC;
00049 ifm->ifi_change = change;
00050 ifm->ifi_flags = flags;
00051
00052 mnl_attr_put_str(nlh, IFLA_IFNAME, argv[1]);
00053
00054 nl = mnl_socket_open(NETLINK_ROUTE);
00055 if (nl == NULL) {
00056 perror("mnl_socket_open");
00057 exit(EXIT_FAILURE);
00058 }
00059
00060 if (mnl_socket_bind(nl, 0, MNL_SOCKET_AUTOPID) < 0) {
00061 perror("mnl_socket_bind");
00062 exit(EXIT_FAILURE);
00063 }
00064 portid = mnl_socket_get_portid(nl);
00065
00066 mnl_nlmsg_fprintf(stdout, nlh, nlh->nlmsg_len,
00067 sizeof(struct ifinfomsg));
00068
00069 if (mnl_socket_sendto(nl, nlh, nlh->nlmsg_len) < 0) {
00070 perror("mnl_socket_send");
00071 exit(EXIT_FAILURE);
00072 }
00073
00074 ret = mnl_socket_recvfrom(nl, buf, sizeof(buf));
00075 if (ret == -1) {
00076 perror("read");
00077 exit(EXIT_FAILURE);
00078 }
00079
00080 ret = mnl_cb_run(buf, ret, seq, portid, NULL, NULL);
00081 if (ret == -1){
00082 perror("callback");
00083 exit(EXIT_FAILURE);
00084 }
00085
00086 mnl_socket_close(nl);
00087
00088 return 0;
00089 }