|
@@ -10,6 +10,7 @@
|
|
|
#include "paras.h"
|
|
#include "paras.h"
|
|
|
#include "cfg.h"
|
|
#include "cfg.h"
|
|
|
#include "file.h"
|
|
#include "file.h"
|
|
|
|
|
+#include "thread.h"
|
|
|
#include "sqlite_handle.h"
|
|
#include "sqlite_handle.h"
|
|
|
|
|
|
|
|
#if 0
|
|
#if 0
|
|
@@ -182,6 +183,7 @@ int sys_get_path(char *path, char *name)
|
|
|
#include <net/if.h>
|
|
#include <net/if.h>
|
|
|
#include <ifaddrs.h>
|
|
#include <ifaddrs.h>
|
|
|
#include <netinet/in.h>
|
|
#include <netinet/in.h>
|
|
|
|
|
+#include <linux/if.h>
|
|
|
#include <linux/if_link.h>
|
|
#include <linux/if_link.h>
|
|
|
#include <linux/netlink.h>
|
|
#include <linux/netlink.h>
|
|
|
#include <linux/rtnetlink.h>
|
|
#include <linux/rtnetlink.h>
|
|
@@ -750,7 +752,7 @@ static int set_net(NetworkInfo_t *para, int ipv)
|
|
|
}
|
|
}
|
|
|
free(fbuf);
|
|
free(fbuf);
|
|
|
|
|
|
|
|
- set_dns(para->dns1, para->dns2);
|
|
|
|
|
|
|
+ //set_dns(para->dns1, para->dns2);
|
|
|
|
|
|
|
|
return r;
|
|
return r;
|
|
|
}
|
|
}
|
|
@@ -869,3 +871,123 @@ int sys_get_net(NetworkInfo_t *para, int ipVersion)
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
+#define MAX_BUFFER_SIZE 4096
|
|
|
|
|
+static void* plug_thread(void *arg)
|
|
|
|
|
+{
|
|
|
|
|
+ int nlSock;
|
|
|
|
|
+ struct sockaddr_nl srcAddr;
|
|
|
|
|
+ struct nlmsghdr *nlMsg;
|
|
|
|
|
+ char buffer[MAX_BUFFER_SIZE];
|
|
|
|
|
+ int plug_in=0,plug_out=0;
|
|
|
|
|
+
|
|
|
|
|
+ printf("___plug_thread start\n");
|
|
|
|
|
+
|
|
|
|
|
+ nlSock = socket(PF_NETLINK, SOCK_RAW, NETLINK_ROUTE);
|
|
|
|
|
+ if (nlSock < 0) {
|
|
|
|
|
+ printf("Failed to create Netlink socket");
|
|
|
|
|
+ goto quit;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ memset(&srcAddr, 0, sizeof(srcAddr));
|
|
|
|
|
+ srcAddr.nl_family = AF_NETLINK;
|
|
|
|
|
+ srcAddr.nl_groups = RTMGRP_LINK | RTMGRP_NOTIFY;
|
|
|
|
|
+
|
|
|
|
|
+ if (bind(nlSock, (struct sockaddr *)&srcAddr, sizeof(srcAddr)) < 0) {
|
|
|
|
|
+ perror("Failed to bind Netlink socket");
|
|
|
|
|
+ close(nlSock);
|
|
|
|
|
+ goto quit;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ int flags = fcntl(nlSock, F_GETFL, 0);
|
|
|
|
|
+ fcntl(nlSock, F_SETFL, flags | O_NONBLOCK);
|
|
|
|
|
+
|
|
|
|
|
+ while (1) {
|
|
|
|
|
+ fd_set readSet;
|
|
|
|
|
+ FD_ZERO(&readSet);
|
|
|
|
|
+ FD_SET(nlSock, &readSet);
|
|
|
|
|
+
|
|
|
|
|
+ // 设置超时时间为 1 秒
|
|
|
|
|
+ struct timeval timeout;
|
|
|
|
|
+ timeout.tv_sec = 1;
|
|
|
|
|
+ timeout.tv_usec = 0;
|
|
|
|
|
+
|
|
|
|
|
+ // 监视 Netlink Socket 的可读状态
|
|
|
|
|
+ int result = select(nlSock + 1, &readSet, NULL, NULL, &timeout);
|
|
|
|
|
+ if (result == -1) {
|
|
|
|
|
+ perror("Failed to select");
|
|
|
|
|
+ goto quit;
|
|
|
|
|
+ } else if (result == 0) {
|
|
|
|
|
+ // 超时,没有新数据到达
|
|
|
|
|
+ //printf("Timeout\n");
|
|
|
|
|
+ continue;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ if (FD_ISSET(nlSock, &readSet)) {
|
|
|
|
|
+ // 接收消息
|
|
|
|
|
+ int bytesRead = recv(nlSock, buffer, MAX_BUFFER_SIZE, 0);
|
|
|
|
|
+ if (bytesRead < 0) {
|
|
|
|
|
+ perror("Failed to receive message from Netlink socket");
|
|
|
|
|
+ close(nlSock);
|
|
|
|
|
+ goto quit;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ // 遍历接收到的消息,获取到Netlink的消息头。
|
|
|
|
|
+ nlMsg = (struct nlmsghdr *)buffer;
|
|
|
|
|
+ while (NLMSG_OK(nlMsg, bytesRead)) {
|
|
|
|
|
+ if (nlMsg->nlmsg_type == NLMSG_DONE) {
|
|
|
|
|
+ break;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ if (nlMsg->nlmsg_type == RTM_NEWLINK || nlMsg->nlmsg_type == RTM_DELLINK) {
|
|
|
|
|
+ //NLMSG_DATA()获取完整的ifinfomsg结构体的指针
|
|
|
|
|
+ //根据消息头nlMsg获取到其后面的数据部分ifinfomsg结构体的指针
|
|
|
|
|
+ struct ifinfomsg *ifInfo = (struct ifinfomsg *)NLMSG_DATA(nlMsg);
|
|
|
|
|
+ if (ifInfo->ifi_family == AF_UNSPEC) {
|
|
|
|
|
+ //获取路由信息的属性,以及消息负载的长度,通过这两个参数来判断本次的数据是否有效
|
|
|
|
|
+ struct rtattr *attribute = IFLA_RTA(ifInfo);
|
|
|
|
|
+ int attributeLen = IFLA_PAYLOAD(nlMsg);
|
|
|
|
|
+
|
|
|
|
|
+ while (RTA_OK(attribute, attributeLen)) {
|
|
|
|
|
+ char *ifaceName = (char *)RTA_DATA(attribute);
|
|
|
|
|
+ if ((attribute->rta_type == IFLA_IFNAME) && (strcmp(ifaceName, "eth0")==0)) {
|
|
|
|
|
+
|
|
|
|
|
+ //printf("____Interface: %s \n", ifaceName);
|
|
|
|
|
+ if((ifInfo->ifi_flags & IFF_LOWER_UP)) {
|
|
|
|
|
+ if(plug_in==0) {
|
|
|
|
|
+ system("ifup eth0");
|
|
|
|
|
+ printf("____eth0 plug in\n");
|
|
|
|
|
+ plug_in = 1; plug_out = 0;
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ else {
|
|
|
|
|
+ if(plug_out==0) {
|
|
|
|
|
+ system("ifdown eth0");
|
|
|
|
|
+ printf("____eth0 plug out\n");
|
|
|
|
|
+ plug_in = 0; plug_out = 1;
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ attribute = RTA_NEXT(attribute, attributeLen);
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ nlMsg = NLMSG_NEXT(nlMsg, bytesRead);
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ close(nlSock);
|
|
|
|
|
+
|
|
|
|
|
+quit:
|
|
|
|
|
+ printf("___plug_thread exit\n");
|
|
|
|
|
+ pthread_exit(NULL);
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
|
|
+int sys_init(void)
|
|
|
|
|
+{
|
|
|
|
|
+ //thread_start(THREAD_ID_PLUG, plug_thread, NULL, 4*MB, 0);
|
|
|
|
|
+
|
|
|
|
|
+ return 0;
|
|
|
|
|
+}
|
|
|
|
|
+
|