#include <sys/socket.h> #include <linux/types.h> #include <linux/netlink.h> #include <linux/rtnetlink.h> int main(void) { int nl_socket; if((nl_socket = socket(PF_NETLINK,SOCK_DGRAM,NETLINK_ROUTE)) == -1) exit(1); struct sockaddr_nl addr; memset(&addr,0,sizeof(addr)); addr.nl_family = PF_NETLINK; addr.nl_groups = RTMGRP_IPV4_IFADDR; addr.nl_pid=0; if(bind(nl_socket,(struct sockaddr *)&addr,sizeof(addr)) == -1) { close(nl_socket); exit(1); } return 0; }
http://blog.csdn.net/luckyapple1028/article/details/50839395
http://blog.csdn.net/luckyapple1028/article/details/50936563
https://www.ibm.com/developerworks/cn/linux/1305_wanghz_ddns/
#include <stdio.h> #include <stdlib.h> #include <string.h> #include <unistd.h> #include <sys/socket.h> #include <sys/select.h> #include <sys/time.h> #include <linux/types.h> #include <linux/netlink.h> #include <linux/rtnetlink.h> #include <linux/if.h> #include <linux/if_ether.h> #define MAX_MSG_SIZE 1024 void handle_newaddr(void) { system("/usrsbin/mDNSResponder -f /var/rendezvous.conf -v 2"); } void receive_netlink_message(int sock) { int ret,length; fd_set nl_rset; struct nlmsghdr *nl; struct timeval read_timeout = {10, 0}; struct iovec iov; // 使用 iovec 进行接收 struct msghdr msg = {NULL, 0, &iov, 1, NULL, 0, 0}; // 初始化 msghdr nl = NULL; if ((nl = (struct nlmsghdr *) malloc(MAX_MSG_SIZE)) == NULL ) return; iov.iov_base = (void *)nl; // 封装 nlmsghdr iov.iov_len = MAX_MSG_SIZE; // 指定长度 FD_ZERO(&nl_rset); FD_SET(sock, &nl_rset); while(1) { ret=select(sock+1,&nl_rset,NULL, NULL,&read_timeout); printf("===============ret=%d=============== ",ret); if(ret == -1) continue; else if (ret == 0) continue; else { length = recvmsg(sock,&msg,0); if(length <= 0) { free(nl); break; } switch(nl->nlmsg_type) { case RTM_NEWADDR: handle_newaddr(); break; default: printf("Unknown netlink message type : %d", nl->nlmsg_type); } } } return ; } int main(void) { int nl_socket; struct sockaddr_nl addr; if((nl_socket = socket(PF_NETLINK,SOCK_DGRAM,NETLINK_ROUTE)) == -1) exit(1); memset(&addr,0,sizeof(addr)); addr.nl_family = PF_NETLINK; addr.nl_groups = RTMGRP_IPV4_IFADDR; addr.nl_pid=0; if(bind(nl_socket,(struct sockaddr *)&addr,sizeof(addr)) == -1) { close(nl_socket); exit(1); } while(1) { printf("111111111111111111111111111 "); receive_netlink_message(nl_socket); } return 0; }
https://www.cnblogs.com/oracleloyal/p/5333276.html
https://www.ibm.com/developerworks/cn/linux/1305_wanghz_ddns/
http://blog.csdn.net/hansel/article/details/38088645
http://blog.csdn.net/gt945/article/details/45315911
网线热插拔
#include <sys/types.h> #include <sys/socket.h> #include <asm/types.h> #include <linux/netlink.h> #include <linux/rtnetlink.h> #include <unistd.h> #include <stdlib.h> #include <stdio.h> #include <sys/ioctl.h> #include <linux/if.h> #include <string.h> #define BUFLEN 20480 int main(int argc, char *argv[]) { int fd, retval; char buf[BUFLEN] = {0}; int len = BUFLEN; struct sockaddr_nl addr; struct nlmsghdr *nh; struct ifinfomsg *ifinfo; struct rtattr *attr; fd = socket(AF_NETLINK, SOCK_RAW, NETLINK_ROUTE); setsockopt(fd, SOL_SOCKET, SO_RCVBUF, &len, sizeof(len)); memset(&addr, 0, sizeof(addr)); addr.nl_family = AF_NETLINK; addr.nl_groups = RTNLGRP_LINK; bind(fd, (struct sockaddr*)&addr, sizeof(addr)); while ((retval = read(fd, buf, BUFLEN)) > 0) { for (nh = (struct nlmsghdr *)buf; NLMSG_OK(nh, retval); nh = NLMSG_NEXT(nh, retval)) { if (nh->nlmsg_type == NLMSG_DONE) break; else if (nh->nlmsg_type == NLMSG_ERROR) return -1; else if (nh->nlmsg_type != RTM_NEWLINK) continue; ifinfo = NLMSG_DATA(nh); printf("%u: %s", ifinfo->ifi_index, (ifinfo->ifi_flags & IFF_LOWER_UP) ? "up" : "down" ); attr = (struct rtattr*)(((char*)nh) + NLMSG_SPACE(sizeof(*ifinfo))); len = nh->nlmsg_len - NLMSG_SPACE(sizeof(*ifinfo)); for (; RTA_OK(attr, len); attr = RTA_NEXT(attr, len)) { if (attr->rta_type == IFLA_IFNAME) { printf(" %s", (char*)RTA_DATA(attr)); break; } } printf(" "); } } return 0; }
http://blog.csdn.net/qq123386926/article/details/50695725