net.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429
  1. #include <sys/types.h>
  2. #include <sys/stat.h>
  3. #include <fcntl.h>
  4. #include <unistd.h>
  5. #include <string.h>
  6. #include <limits.h>
  7. #include "rtdbg.h"
  8. #include "sys.h"
  9. #include "paras.h"
  10. #include "cfg.h"
  11. #include "fs.h"
  12. #if 1
  13. #define LOGE LOG_E
  14. #define LOGW LOG_W
  15. #define LOGD LOG_D
  16. #else
  17. #define LOGD rt_printf
  18. #define LOGW rt_printf
  19. #define LOGE rt_printf
  20. #endif
  21. #define DFLT_NIC "e0"
  22. ////////////////////////////////////////////////
  23. #include <sys/socket.h>
  24. #include <sys/ioctl.h>
  25. #include <netinet/in.h>
  26. #include <arpa/inet.h>
  27. #include <unistd.h>
  28. #include <lwip/sockets.h>
  29. #include <lwip/ip_addr.h>
  30. #include <lwip/ip6_addr.h>
  31. void print_net(char*s, network_data_t *p)
  32. {
  33. LOGD("__%s__net.mode: %d\n", s, p->mode);
  34. LOGD("__%s__net.addr: %s\n", s, p->ip_address);
  35. LOGD("__%s__net.mask: %s\n", s, p->mask);
  36. LOGD("__%s__net.gateway: %s\n", s, p->gateway);
  37. LOGD("__%s__net.dns1: %s\n", s, p->dns1);
  38. LOGD("__%s__net.dns2: %s\n", s, p->dns2);
  39. }
  40. void print_modbus(char*s, modbus_data_t *p)
  41. {
  42. LOGD("__%s__mbus.mode: %d\n", s, p->mode);
  43. LOGD("__%s__mbus.addr: %d\n", s, p->addr);
  44. LOGD("__%s__mbus.baud: %d\n", s, p->baudrate);
  45. LOGD("__%s__mbus.path: %s\n", s, p->path);
  46. }
  47. static int set_ipaddr(int family, const char* iface, const char* addr, const char* netmask)
  48. {
  49. ip4_addr_t ipaddr4;
  50. ip6_addr_t ipaddr6;
  51. if(family==AF_INET) {
  52. //ip4_addr_set(&ipaddr4, IP4_ADDR(&ipaddr4, 0xbeef, 0xfeed, 0xdead, 0xbeef));
  53. }
  54. else {
  55. //ip6_addr_set(&ipaddr6, IP6_ADDR(&ipaddr6, 0xbeef, 0xfeed, 0xdead, 0xbeef, 0x1337, 0xbabe, 0xbaad, 0xface));
  56. //netif_add_ip6_address(&netif, &ipaddr_v6, &chosen_idx);
  57. //netif.ip6_addr_state[chosen_idx] = IP6_ADDR_VALID;
  58. //ip6_addr_assign_zone(ip_2_ip6(&netif.ip6_addr[0]), IP6_UNICAST, &netif);
  59. //netif_ip6_addr_set_state(&netif, 0, IP6_ADDR_TENTATIVE);
  60. }
  61. return 0;
  62. }
  63. static int get_ipaddr(int family, char *iface, char *addr, char *netmask)
  64. {
  65. return 0;
  66. }
  67. static int set_gateway(int family, const char* gateway)
  68. {
  69. return 0;
  70. }
  71. static int get_gateway(int family, const char* gateway)
  72. {
  73. return 0;
  74. }
  75. static int set_dhcp(int on)
  76. {
  77. if(on) {
  78. }
  79. else {
  80. }
  81. return 0;
  82. }
  83. static int is_number(char *str)
  84. {
  85. while (*str) {
  86. if (!isdigit(*str)) {
  87. return 0;
  88. }
  89. str++;
  90. }
  91. return 1;
  92. }
  93. static int ip_check(char* addr)
  94. {
  95. int flag=IP_ERR;
  96. struct in_addr addr4;
  97. struct in6_addr addr6;
  98. if (inet_pton(AF_INET, addr, &addr4) == 1) {
  99. flag = IP_V4;
  100. } else if (inet_pton(AF_INET6, addr, &addr6) == 1) {
  101. flag = IP_V6;
  102. }
  103. if(flag==IP_ERR) {
  104. LOGE("____ wrong ip addr: %s\n", addr);
  105. }
  106. return flag;
  107. }
  108. static int is_ipv6_prefix(char *ss)
  109. {
  110. if(is_number(ss)) {
  111. int val = atoi(ss);
  112. if(val>1 && val<128) {
  113. return 1;
  114. }
  115. }
  116. return 0;
  117. }
  118. static int nw_check(network_data_t *para, int ipv)
  119. {
  120. if(ipv==IP_V4) {
  121. if((ip_check(para->ipaddr)!=IP_V4) || (ip_check(para->mask)!=IP_V4) || (ip_check(para->gateway)!=IP_V4)) {
  122. return -1;
  123. }
  124. }
  125. else {
  126. if((ip_check(para->ipaddr)!=IP_V6) || (!is_ipv6_prefix(para->mask)) || (ip_check(para->gateway)!=IP_V6)) {
  127. return -1;
  128. }
  129. }
  130. return 0;
  131. }
  132. static int get_net(network_data_t *para, int ipv)
  133. {
  134. return 0;
  135. }
  136. static int set_net(network_data_t *para, int ipv)
  137. {
  138. int r,ipv2;
  139. network_data_t info4,info6,*info;
  140. get_net(&info4, IP_V4);
  141. get_net(&info6, IP_V6);
  142. info = ((ipv==IP_V4)?&info4:&info6);
  143. if(para->mode<2 && para->mode!=info->mode) {
  144. info->mode = para->mode;
  145. }
  146. ipv2 = ip_check(para->ipaddr);
  147. if((ipv==ipv2) && strcmp(para->ipaddr, info->ipaddr)) {
  148. strcpy(info->ipaddr, para->ipaddr);
  149. }
  150. if(ipv==IP_V4) {
  151. ipv2 = ip_check(para->mask);
  152. }
  153. else {
  154. ipv2=is_ipv6_prefix(para->mask)?IP_V6:IP_ERR;
  155. }
  156. if((ipv==ipv2) && strcmp(para->mask, info->mask)) {
  157. strcpy(info->mask, para->mask);
  158. }
  159. ipv2 = ip_check(para->gateway);
  160. if((ipv==ipv2) && strcmp(para->gateway, info->gateway)) {
  161. strcpy(info->gateway, para->gateway);
  162. }
  163. return r;
  164. }
  165. //////////////////////////////////////////////////////////////////////////////
  166. int sys_set_net(network_data_t *para, int ip_version)
  167. {
  168. int r,ipv;
  169. network_data_t para2;
  170. r = get_net(&para2, ip_version);
  171. if(r==0) {
  172. if(memcmp(para, &para2, sizeof(network_data_t))==0) {
  173. LOGW("%s NetworkInfo is same, no need to set\n", DFLT_NIC);
  174. return -1;
  175. }
  176. set_dns(para);
  177. r = set_net(para, ipVersion);
  178. if(r==0) {
  179. sys_reboot();
  180. //nic_restart(DFLT_NIC); exit(0);
  181. }
  182. }
  183. else {
  184. LOGE("get_net failed\n");
  185. }
  186. return r;
  187. }
  188. int sys_get_net(network_data_t *para, int ip_version)
  189. {
  190. int r;
  191. r = get_net(para, ip_version);
  192. return r;
  193. }
  194. int sys_get_mac(char *mac, int maclen)
  195. {
  196. int sock;
  197. struct ifreq ifr;
  198. if(!mac || maclen<6) {
  199. return -1;
  200. }
  201. sock = socket(AF_INET, SOCK_DGRAM, 0);
  202. if (sock == -1){
  203. return -1;
  204. }
  205. strcpy(ifr.ifr_name, "eth0");
  206. if (ioctl(sock, SIOCGIFHWADDR, &ifr) < 0){
  207. close(sock); return -1;
  208. }
  209. close(sock);
  210. //uint8_t *p = (uint8_t*)ifr.ifr_hwaddr.sa_data;
  211. //snprintf(mac, maclen, "%02x%02x%02x%02x%02x%02x", p[0], p[1], p[2], p[3], p[4], p[5]);
  212. return 0;
  213. }
  214. int sys_get_chip_id(int *id)
  215. {
  216. int sock;
  217. struct ifreq ifr;
  218. if(!id) {
  219. return -1;
  220. }
  221. return 0;
  222. }
  223. int sys_ip_check(char *ip)
  224. {
  225. return ip_check(ip);
  226. }
  227. ///////////////////////////////////////////////////////////////////////
  228. #define MAX_BUFFER_SIZE 1024
  229. static void* plug_thread(void *arg)
  230. {
  231. int nlSock;
  232. struct sockaddr_nl srcAddr;
  233. struct nlmsghdr *nlMsg;
  234. char buffer[MAX_BUFFER_SIZE];
  235. int plug_in=0,plug_out=0;
  236. thread_handle_t *h=(thread_handle_t*)arg;
  237. printf("___plug_thread start\n");
  238. nlSock = socket(PF_NETLINK, SOCK_RAW, NETLINK_ROUTE);
  239. if (nlSock < 0) {
  240. printf("Failed to create Netlink socket");
  241. goto quit;
  242. }
  243. memset(&srcAddr, 0, sizeof(srcAddr));
  244. srcAddr.nl_family = AF_NETLINK;
  245. srcAddr.nl_groups = RTMGRP_LINK | RTMGRP_NOTIFY;
  246. if (bind(nlSock, (struct sockaddr *)&srcAddr, sizeof(srcAddr)) < 0) {
  247. perror("Failed to bind Netlink socket");
  248. close(nlSock);
  249. goto quit;
  250. }
  251. int flags = fcntl(nlSock, F_GETFL, 0);
  252. fcntl(nlSock, F_SETFL, flags | O_NONBLOCK);
  253. while (h->quit==0) {
  254. fd_set readSet;
  255. FD_ZERO(&readSet);
  256. FD_SET(nlSock, &readSet);
  257. // 设置超时时间为 1 秒
  258. struct timeval timeout;
  259. timeout.tv_sec = 1;
  260. timeout.tv_usec = 0;
  261. // 监视 Netlink Socket 的可读状态
  262. int result = select(nlSock + 1, &readSet, NULL, NULL, &timeout);
  263. if (result == -1) {
  264. perror("Failed to select");
  265. goto quit;
  266. } else if (result == 0) {
  267. // 超时,没有新数据到达
  268. //printf("Timeout\n");
  269. continue;
  270. }
  271. if (FD_ISSET(nlSock, &readSet)) {
  272. // 接收消息
  273. int bytesRead = recv(nlSock, buffer, MAX_BUFFER_SIZE, 0);
  274. if (bytesRead < 0) {
  275. perror("Failed to receive message from Netlink socket");
  276. close(nlSock);
  277. goto quit;
  278. }
  279. // 遍历接收到的消息,获取到Netlink的消息头。
  280. nlMsg = (struct nlmsghdr *)buffer;
  281. while (NLMSG_OK(nlMsg, bytesRead)) {
  282. if (nlMsg->nlmsg_type == NLMSG_DONE) {
  283. break;
  284. }
  285. if (nlMsg->nlmsg_type == RTM_NEWLINK || nlMsg->nlmsg_type == RTM_DELLINK) {
  286. //NLMSG_DATA()获取完整的ifinfomsg结构体的指针
  287. //根据消息头nlMsg获取到其后面的数据部分ifinfomsg结构体的指针
  288. struct ifinfomsg *ifInfo = (struct ifinfomsg *)NLMSG_DATA(nlMsg);
  289. if (ifInfo->ifi_family == AF_UNSPEC) {
  290. //获取路由信息的属性,以及消息负载的长度,通过这两个参数来判断本次的数据是否有效
  291. struct rtattr *attribute = IFLA_RTA(ifInfo);
  292. int attributeLen = IFLA_PAYLOAD(nlMsg);
  293. while (RTA_OK(attribute, attributeLen)) {
  294. char *ifaceName = (char *)RTA_DATA(attribute);
  295. if ((attribute->rta_type == IFLA_IFNAME) && (strcmp(ifaceName, "eth0")==0)) {
  296. //printf("____Interface: %s \n", ifaceName);
  297. if((ifInfo->ifi_flags & IFF_LOWER_UP)) {
  298. if(plug_in==0) {
  299. system("ifup eth0");
  300. printf("____eth0 plug in\n");
  301. plug_in = 1; plug_out = 0;
  302. }
  303. }
  304. else {
  305. if(plug_out==0) {
  306. system("ifdown eth0");
  307. printf("____eth0 plug out\n");
  308. plug_in = 0; plug_out = 1;
  309. }
  310. }
  311. }
  312. attribute = RTA_NEXT(attribute, attributeLen);
  313. }
  314. }
  315. }
  316. nlMsg = NLMSG_NEXT(nlMsg, bytesRead);
  317. }
  318. }
  319. }
  320. close(nlSock);
  321. quit:
  322. printf("___plug_thread exit\n");
  323. pthread_exit(NULL);
  324. }
  325. ///////////////////////////////////////////////////////////
  326. static void* polling_thread(void *arg)
  327. {
  328. thread_handle_t *h=(thread_handle_t*)arg;
  329. //wdog_init();
  330. //tmp_create();
  331. while(h->quit==0) {
  332. sleep(1);
  333. }
  334. pthread_exit(NULL);
  335. }
  336. int sys_init(void)
  337. {
  338. lock_s_init();
  339. paras_init();
  340. thread_start(THREAD_ID_POLLING, polling_thread, NULL);
  341. return 0;
  342. }