1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 | #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/
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 | #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===============\n" ,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\n" ); 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("\n"); } } return 0; }
http://blog.csdn.net/qq123386926/article/details/50695725
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· 终于写完轮子一部分:tcp代理 了,记录一下
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 别再用vector<bool>了!Google高级工程师:这可能是STL最大的设计失误
· 单元测试从入门到精通
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理