Last active
August 30, 2016 17:26
-
-
Save tiebingzhang/aafc2953b430d5586bd1135cad85100f to your computer and use it in GitHub Desktop.
C code to detect link connected/disconnected using RTNETLINK
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <stdlib.h> | |
#include <string.h> | |
#include <asm/types.h> | |
#include <sys/socket.h> | |
#include <linux/netlink.h> | |
#include <linux/rtnetlink.h> | |
#include <net/if.h> | |
int main(){ | |
int fd; | |
struct sockaddr_nl sa; | |
struct ifaddrmsg *rtmp; | |
struct rtattr *rtatp; | |
int rtattrlen; | |
struct ifinfomsg *rtif; | |
char ifname[IF_NAMESIZE]; | |
int len; | |
char buf[4096]; | |
struct iovec iov = { buf, sizeof(buf) }; | |
struct nlmsghdr *nh; | |
memset(&sa, 0, sizeof(sa)); | |
sa.nl_family = AF_NETLINK; | |
sa.nl_groups = RTMGRP_LINK | RTMGRP_IPV4_IFADDR; | |
fd = socket(AF_NETLINK, SOCK_RAW, NETLINK_ROUTE); | |
bind(fd, (struct sockaddr *) &sa, sizeof(sa)); | |
while(1){ | |
struct msghdr msg = { &sa, sizeof(sa), &iov, 1, NULL, 0, 0 }; | |
len = recvmsg(fd, &msg, 0); | |
for (nh = (struct nlmsghdr *) buf; NLMSG_OK (nh, len); nh = NLMSG_NEXT (nh, len)) { | |
/* The end of multipart message. */ | |
if (nh->nlmsg_type == NLMSG_DONE){ | |
printf("got msg done\n"); | |
break; | |
} | |
if (nh->nlmsg_type == NLMSG_ERROR){ | |
printf("got msg error\n"); | |
continue; | |
} | |
if (nh->nlmsg_type < RTM_NEWADDR){ | |
rtif=(struct ifinfomsg *)NLMSG_DATA(nh); | |
printf("Interface %s is %s\n",if_indextoname(rtif->ifi_index,ifname), | |
(rtif->ifi_flags&IFF_RUNNING)?"Connected":"Disconnected"); | |
} | |
} | |
} | |
return 0; | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment