net.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623
  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. #include "lwip/netif.h"
  13. #include "lwip/netifapi.h"
  14. #include <rtthread.h>
  15. #include <netif/ethernetif.h>
  16. #include "paras.h"
  17. #include "lwip/prot/dhcp.h"
  18. #include <lwip/ip_addr.h>
  19. #include "lwip/dns.h"
  20. #include "net.h"
  21. #if 1
  22. #define LOGE LOG_E
  23. #define LOGW LOG_W
  24. #define LOGD LOG_D
  25. #else
  26. #define LOGD rt_printf
  27. #define LOGW rt_printf
  28. #define LOGE rt_printf
  29. #endif
  30. #define DFLT_NIC "e0"
  31. typedef struct{
  32. struct netif *netif;
  33. struct netdev *netdev;
  34. }net_handle_t;
  35. static net_handle_t handle_net = {0};
  36. void print_net(char*s, network_data_t *p)
  37. {
  38. LOGD("__%s__net.mode: %d\n", s, p->mode);
  39. LOGD("__%s__net.addr: %s\n", s, p->ip_address);
  40. LOGD("__%s__net.mask: %s\n", s, p->mask);
  41. LOGD("__%s__net.gateway: %s\n", s, p->gateway);
  42. LOGD("__%s__net.dns1: %s\n", s, p->dns1);
  43. LOGD("__%s__net.dns2: %s\n", s, p->dns2);
  44. }
  45. void print_modbus(char*s, modbus_data_t *p)
  46. {
  47. LOGD("__%s__mbus.mode: %d\n", s, p->mode);
  48. LOGD("__%s__mbus.addr: %d\n", s, p->addr);
  49. LOGD("__%s__mbus.baud: %d\n", s, p->baudrate);
  50. LOGD("__%s__mbus.path: %s\n", s, p->path);
  51. }
  52. static int set_dns(network_data_t *para)
  53. {
  54. }
  55. static int set_ipaddr(int family, const char* iface, const char* addr, const char* netmask)
  56. {
  57. ip4_addr_t ipaddr4;
  58. ip6_addr_t ipaddr6;
  59. // if(family==AF_INET) {
  60. //ip4_addr_set(&ipaddr4, IP4_ADDR(&ipaddr4, 0xbeef, 0xfeed, 0xdead, 0xbeef));
  61. // }
  62. // else {
  63. //ip6_addr_set(&ipaddr6, IP6_ADDR(&ipaddr6, 0xbeef, 0xfeed, 0xdead, 0xbeef, 0x1337, 0xbabe, 0xbaad, 0xface));
  64. //netif_add_ip6_address(&netif, &ipaddr_v6, &chosen_idx);
  65. //netif.ip6_addr_state[chosen_idx] = IP6_ADDR_VALID;
  66. //ip6_addr_assign_zone(ip_2_ip6(&netif.ip6_addr[0]), IP6_UNICAST, &netif);
  67. //netif_ip6_addr_set_state(&netif, 0, IP6_ADDR_TENTATIVE);
  68. // }
  69. return 0;
  70. }
  71. static int get_ipaddr(int family, char *iface, char *addr, char *netmask)
  72. {
  73. return 0;
  74. }
  75. static int set_gateway(int family, const char* gateway)
  76. {
  77. return 0;
  78. }
  79. static int get_gateway(int family, const char* gateway)
  80. {
  81. return 0;
  82. }
  83. static int set_dhcp(int on)
  84. {
  85. if(on) {
  86. }
  87. else {
  88. }
  89. return 0;
  90. }
  91. static int is_number(char *str)
  92. {
  93. while (*str) {
  94. if (!isdigit(*str)) {
  95. return 0;
  96. }
  97. str++;
  98. }
  99. return 1;
  100. }
  101. static int ip_check(char* addr)
  102. {
  103. int flag=0;
  104. #if 0
  105. struct in_addr addr4;
  106. struct in6_addr addr6;
  107. if (inet_pton(AF_INET, addr, &addr4) == 1) {
  108. flag = IP_V4;
  109. } else if (inet_pton(AF_INET6, addr, &addr6) == 1) {
  110. flag = IP_V6;
  111. }
  112. if(flag==IP_ERR) {
  113. LOGE("____ wrong ip addr: %s\n", addr);
  114. }
  115. #endif
  116. return flag;
  117. }
  118. static int is_ipv6_prefix(char *ss)
  119. {
  120. // if(is_number(ss)) {
  121. // int val = atoi(ss);
  122. // if(val>1 && val<128) {
  123. // return 1;
  124. // }
  125. // }
  126. return 0;
  127. }
  128. static int nw_check(network_data_t *para, int ipv)
  129. {
  130. #if 0
  131. if(ipv==IP_V4) {
  132. if((ip_check(para->ipaddr)!=IP_V4) || (ip_check(para->mask)!=IP_V4) || (ip_check(para->gateway)!=IP_V4)) {
  133. return -1;
  134. }
  135. }
  136. else {
  137. if((ip_check(para->ipaddr)!=IP_V6) || (!is_ipv6_prefix(para->mask)) || (ip_check(para->gateway)!=IP_V6)) {
  138. return -1;
  139. }
  140. }
  141. #endif
  142. return 0;
  143. }
  144. static int get_net(network_data_t *para, int ipv)
  145. {
  146. return 0;
  147. }
  148. static int set_net(network_data_t *para, int ipv)
  149. {
  150. int r =0;
  151. #if 0
  152. int r,ipv2;
  153. network_data_t info4,info6,*info;
  154. get_net(&info4, IP_V4);
  155. get_net(&info6, IP_V6);
  156. info = ((ipv==IP_V4)?&info4:&info6);
  157. if(para->mode<2 && para->mode!=info->mode) {
  158. info->mode = para->mode;
  159. }
  160. ipv2 = ip_check(para->ipaddr);
  161. if((ipv==ipv2) && strcmp(para->ipaddr, info->ipaddr)) {
  162. strcpy(info->ipaddr, para->ipaddr);
  163. }
  164. if(ipv==IP_V4) {
  165. ipv2 = ip_check(para->mask);
  166. }
  167. else {
  168. ipv2=is_ipv6_prefix(para->mask)?IP_V6:IP_ERR;
  169. }
  170. if((ipv==ipv2) && strcmp(para->mask, info->mask)) {
  171. strcpy(info->mask, para->mask);
  172. }
  173. ipv2 = ip_check(para->gateway);
  174. if((ipv==ipv2) && strcmp(para->gateway, info->gateway)) {
  175. strcpy(info->gateway, para->gateway);
  176. }
  177. #endif
  178. return r;
  179. }
  180. //////////////////////////////////////////////////////////////////////////////
  181. int sys_set_net(network_data_t *para, int ip_version)
  182. {
  183. int r,ipv;
  184. #if 0
  185. network_data_t para2;
  186. r = get_net(&para2, ip_version);
  187. if(r==0) {
  188. if(memcmp(para, &para2, sizeof(network_data_t))==0) {
  189. LOGW("%s NetworkInfo is same, no need to set\n", DFLT_NIC);
  190. return -1;
  191. }
  192. set_dns(para);
  193. r = set_net(para, ip_version);
  194. if(r==0) {
  195. sys_reboot();
  196. //nic_restart(DFLT_NIC); exit(0);
  197. }
  198. }
  199. else {
  200. LOGE("get_net failed\n");
  201. }
  202. #endif
  203. return r;
  204. }
  205. int sys_get_net(network_data_t *para, int ip_version)
  206. {
  207. int r;
  208. r = get_net(para, ip_version);
  209. return r;
  210. }
  211. int sys_get_mac(char *mac, int maclen)
  212. {
  213. #if 0
  214. int sock;
  215. struct ifreq ifr;
  216. if(!mac || maclen<6) {
  217. return -1;
  218. }
  219. sock = socket(AF_INET, SOCK_DGRAM, 0);
  220. if (sock == -1){
  221. return -1;
  222. }
  223. strcpy(ifr.ifr_name, "eth0");
  224. if (ioctl(sock, SIOCGIFHWADDR, &ifr) < 0){
  225. close(sock); return -1;
  226. }
  227. close(sock);
  228. //uint8_t *p = (uint8_t*)ifr.ifr_hwaddr.sa_data;
  229. //snprintf(mac, maclen, "%02x%02x%02x%02x%02x%02x", p[0], p[1], p[2], p[3], p[4], p[5]);
  230. #endif
  231. return 0;
  232. }
  233. #if 0
  234. int sys_get_chip_id(int *id)
  235. {
  236. int sock;
  237. struct ifreq ifr;
  238. if(!id) {
  239. return -1;
  240. }
  241. return 0;
  242. }
  243. #endif
  244. int sys_ip_check(char *ip)
  245. {
  246. return ip_check(ip);
  247. }
  248. static void ip_save(network_data_t *data)
  249. {
  250. paras_data_t* para = paras_get();
  251. memcmp(para->netwk.ipaddr,data->ipaddr,strlen(data->ipaddr));
  252. memcmp(para->netwk.gateway,data->gateway,strlen(data->gateway));
  253. memcmp(para->netwk.mask,data->mask,strlen(data->mask));
  254. memcmp(para->netwk.dns1,data->dns1,strlen(data->dns1));
  255. memcmp(para->netwk.dns2,data->dns2,strlen(data->dns2));
  256. para->netwk.mode = data->mode;
  257. paras_save();
  258. return;
  259. }
  260. void net_get_ip(network_data_t *data)
  261. {
  262. paras_data_t* para = paras_get();
  263. if(handle_net.netif && netif_is_up(handle_net.netif))
  264. {
  265. char * str = ip4addr_ntoa(netif_ip4_addr(handle_net.netif));
  266. memcpy(data->ipaddr,str,strlen(str));
  267. str = ip4addr_ntoa(netif_ip4_netmask(handle_net.netif));
  268. memcpy(data->mask,str,strlen(str));
  269. str = ip4addr_ntoa(netif_ip4_gw(handle_net.netif));
  270. memcpy(data->gateway,str,strlen(str));
  271. data->mode = para->netwk.mode;
  272. }else
  273. {
  274. memcpy(data->dns1,"8.8.8.8",strlen("8.8.8.8"));
  275. memcpy(data->dns2,"114.114.114.114",strlen("114.114.114.114"));
  276. memcpy(data->ipaddr,"0.0.0.0",strlen("0.0.0.0"));
  277. memcpy(data->gateway,"0.0.0.0",strlen("0.0.0.0"));
  278. memcpy(data->mask,"0.0.0.0",strlen("0.0.0.0"));
  279. data->mode = para->netwk.mode;
  280. }
  281. }
  282. static void ip_set_static()
  283. {
  284. paras_data_t* para = paras_get();
  285. u32_t ip = ipaddr_addr(para->netwk.ipaddr);
  286. u32_t gw = ipaddr_addr(para->netwk.gateway);
  287. u32_t mask = ipaddr_addr(para->netwk.mask);
  288. netifapi_netif_set_addr(handle_net.netif, (ip4_addr_t*)&ip, (ip4_addr_t*)&mask, (ip4_addr_t*)&gw);
  289. ip_addr_t primary_dns, backup_dns;
  290. ipaddr_aton(para->netwk.dns1, &primary_dns); // 主 DNS
  291. ipaddr_aton(para->netwk.dns2, &backup_dns); // 备 DNS
  292. dns_setserver(0, &primary_dns); // 索引 0 为主
  293. dns_setserver(1, &backup_dns); // 索引 1 为备
  294. netifapi_netif_set_link_down(handle_net.netif);
  295. usleep(10000);
  296. netifapi_netif_set_link_up(handle_net.netif);
  297. usleep(100000);
  298. }
  299. int net_ipaddr_set(network_data_t *data)
  300. {
  301. paras_data_t* para = paras_get();
  302. if(para->netwk.mode == 0)
  303. {
  304. if(data->mode == 1)
  305. {
  306. ip_save(data);
  307. netifapi_dhcp_stop(handle_net.netif);
  308. ip_set_static();
  309. LOG_D("mode dhcp 2 static\n");
  310. }
  311. }else if(para->netwk.mode == 1)
  312. {
  313. if(data->mode == 0)
  314. {
  315. netifapi_dhcp_start(handle_net.netif);
  316. ip_save(data);
  317. LOG_D("mode static 2 dhcp\n");
  318. }else if(data->mode == 1)
  319. {
  320. ip_save(data);
  321. ip_set_static();
  322. LOG_D("mode static 2 static\n");
  323. }
  324. }else
  325. {
  326. LOG_E("error mode\n");
  327. }
  328. }
  329. int net_init(void)
  330. {
  331. //获取网卡设备
  332. rt_device_t dev = rt_device_find(DFLT_NIC);
  333. if(dev == RT_NULL)
  334. {
  335. LOG_E("___ %s is not exist!!\n",DFLT_NIC);
  336. goto failed;
  337. }
  338. struct eth_device *eth_dev = (struct eth_device*)dev;
  339. if(eth_dev)
  340. {
  341. handle_net.netif = eth_dev->netif;
  342. }else{
  343. goto failed;
  344. }
  345. // struct netdev *netdev = netdev_get_by_name(DFLT_NIC);
  346. // if (netdev == RT_NULL)
  347. // {
  348. // LOG_E("Netdev 'e0' not found!\n");
  349. // return -1;
  350. // }
  351. // paras_data_t* para = paras_get();
  352. //
  353. // if(para->netwk.mode == 0) //dhcp
  354. // {
  355. // //获取dhcp ipaddr and
  356. // struct dhcp* dhcp = netif_dhcp_data(handle_net.netif);
  357. // if(!dhcp)
  358. // {
  359. // LOG_E("___ %s dhcp failed!!\n",DFLT_NIC);
  360. // goto failed;
  361. // }
  362. //
  363. // if(dhcp->state == DHCP_STATE_REBINDING)
  364. // {
  365. // //dhcp->offered_ip_addr.addr =
  366. // //filt
  367. // char * str = ip4addr_ntoa(netif_ip4_addr(handle_net.netif));
  368. // LOG_D("____%s\n",str);
  369. // memcpy(para->netwk.ipaddr,str,strlen(str));
  370. // str = ip4addr_ntoa(netif_ip4_netmask(handle_net.netif));
  371. // LOG_D("____%s\n",str);
  372. // memcpy(para->netwk.mask,str,strlen(str));
  373. //
  374. // str = ip4addr_ntoa(netif_ip4_gw(handle_net.netif));
  375. // memcpy(para->netwk.gateway,str,strlen(str));
  376. // LOG_D("____%s\n",str);
  377. //
  378. // }else
  379. // {
  380. //
  381. // }
  382. //
  383. // }else if(para->netwk.mode == 1) //static
  384. // {
  385. // netifapi_dhcp_stop(handle_net.netif);
  386. // ip_set_static();
  387. // }
  388. //
  389. return 0;
  390. failed:
  391. return -1;
  392. }
  393. #if 0
  394. ///////////////////////////////////////////////////////////////////////
  395. #define MAX_BUFFER_SIZE 1024
  396. static void* plug_thread(void *arg)
  397. {
  398. int nlSock;
  399. struct sockaddr_nl srcAddr;
  400. struct nlmsghdr *nlMsg;
  401. char buffer[MAX_BUFFER_SIZE];
  402. int plug_in=0,plug_out=0;
  403. thread_handle_t *h=(thread_handle_t*)arg;
  404. printf("___plug_thread start\n");
  405. nlSock = socket(PF_NETLINK, SOCK_RAW, NETLINK_ROUTE);
  406. if (nlSock < 0) {
  407. printf("Failed to create Netlink socket");
  408. goto quit;
  409. }
  410. memset(&srcAddr, 0, sizeof(srcAddr));
  411. srcAddr.nl_family = AF_NETLINK;
  412. srcAddr.nl_groups = RTMGRP_LINK | RTMGRP_NOTIFY;
  413. if (bind(nlSock, (struct sockaddr *)&srcAddr, sizeof(srcAddr)) < 0) {
  414. perror("Failed to bind Netlink socket");
  415. close(nlSock);
  416. goto quit;
  417. }
  418. int flags = fcntl(nlSock, F_GETFL, 0);
  419. fcntl(nlSock, F_SETFL, flags | O_NONBLOCK);
  420. while (h->quit==0) {
  421. fd_set readSet;
  422. FD_ZERO(&readSet);
  423. FD_SET(nlSock, &readSet);
  424. // 设置超时时间为 1 秒
  425. struct timeval timeout;
  426. timeout.tv_sec = 1;
  427. timeout.tv_usec = 0;
  428. // 监视 Netlink Socket 的可读状态
  429. int result = select(nlSock + 1, &readSet, NULL, NULL, &timeout);
  430. if (result == -1) {
  431. perror("Failed to select");
  432. goto quit;
  433. } else if (result == 0) {
  434. // 超时,没有新数据到达
  435. //printf("Timeout\n");
  436. continue;
  437. }
  438. if (FD_ISSET(nlSock, &readSet)) {
  439. // 接收消息
  440. int bytesRead = recv(nlSock, buffer, MAX_BUFFER_SIZE, 0);
  441. if (bytesRead < 0) {
  442. perror("Failed to receive message from Netlink socket");
  443. close(nlSock);
  444. goto quit;
  445. }
  446. // 遍历接收到的消息,获取到Netlink的消息头。
  447. nlMsg = (struct nlmsghdr *)buffer;
  448. while (NLMSG_OK(nlMsg, bytesRead)) {
  449. if (nlMsg->nlmsg_type == NLMSG_DONE) {
  450. break;
  451. }
  452. if (nlMsg->nlmsg_type == RTM_NEWLINK || nlMsg->nlmsg_type == RTM_DELLINK) {
  453. //NLMSG_DATA()获取完整的ifinfomsg结构体的指针
  454. //根据消息头nlMsg获取到其后面的数据部分ifinfomsg结构体的指针
  455. struct ifinfomsg *ifInfo = (struct ifinfomsg *)NLMSG_DATA(nlMsg);
  456. if (ifInfo->ifi_family == AF_UNSPEC) {
  457. //获取路由信息的属性,以及消息负载的长度,通过这两个参数来判断本次的数据是否有效
  458. struct rtattr *attribute = IFLA_RTA(ifInfo);
  459. int attributeLen = IFLA_PAYLOAD(nlMsg);
  460. while (RTA_OK(attribute, attributeLen)) {
  461. char *ifaceName = (char *)RTA_DATA(attribute);
  462. if ((attribute->rta_type == IFLA_IFNAME) && (strcmp(ifaceName, "eth0")==0)) {
  463. //printf("____Interface: %s \n", ifaceName);
  464. if((ifInfo->ifi_flags & IFF_LOWER_UP)) {
  465. if(plug_in==0) {
  466. system("ifup eth0");
  467. printf("____eth0 plug in\n");
  468. plug_in = 1; plug_out = 0;
  469. }
  470. }
  471. else {
  472. if(plug_out==0) {
  473. system("ifdown eth0");
  474. printf("____eth0 plug out\n");
  475. plug_in = 0; plug_out = 1;
  476. }
  477. }
  478. }
  479. attribute = RTA_NEXT(attribute, attributeLen);
  480. }
  481. }
  482. }
  483. nlMsg = NLMSG_NEXT(nlMsg, bytesRead);
  484. }
  485. }
  486. }
  487. close(nlSock);
  488. quit:
  489. printf("___plug_thread exit\n");
  490. pthread_exit(NULL);
  491. }
  492. ///////////////////////////////////////////////////////////
  493. static void* polling_thread(void *arg)
  494. {
  495. thread_handle_t *h=(thread_handle_t*)arg;
  496. //wdog_init();
  497. //tmp_create();
  498. while(h->quit==0) {
  499. sleep(1);
  500. }
  501. pthread_exit(NULL);
  502. }
  503. int sys_init(void)
  504. {
  505. lock_s_init();
  506. paras_init();
  507. thread_start(THREAD_ID_POLLING, polling_thread, NULL);
  508. return 0;
  509. }
  510. #endif