net.c 15 KB

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