net.c 15 KB

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