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. if(para->netwk.mode == 0) //dhcp
  353. {
  354. //获取dhcp ipaddr and
  355. struct dhcp* dhcp = netif_dhcp_data(handle_net.netif);
  356. if(!dhcp)
  357. {
  358. LOG_E("___ %s dhcp failed!!\n",DFLT_NIC);
  359. goto failed;
  360. }
  361. if(dhcp->state == DHCP_STATE_REBINDING)
  362. {
  363. //dhcp->offered_ip_addr.addr =
  364. //filt
  365. char * str = ip4addr_ntoa(netif_ip4_addr(handle_net.netif));
  366. LOG_D("____%s\n",str);
  367. memcpy(para->netwk.ipaddr,str,strlen(str));
  368. str = ip4addr_ntoa(netif_ip4_netmask(handle_net.netif));
  369. LOG_D("____%s\n",str);
  370. memcpy(para->netwk.mask,str,strlen(str));
  371. str = ip4addr_ntoa(netif_ip4_gw(handle_net.netif));
  372. memcpy(para->netwk.gateway,str,strlen(str));
  373. LOG_D("____%s\n",str);
  374. }else
  375. {
  376. }
  377. }else if(para->netwk.mode == 1) //static
  378. {
  379. netifapi_dhcp_stop(handle_net.netif);
  380. ip_set_static();
  381. }
  382. return 0;
  383. failed:
  384. return -1;
  385. }
  386. #if 0
  387. ///////////////////////////////////////////////////////////////////////
  388. #define MAX_BUFFER_SIZE 1024
  389. static void* plug_thread(void *arg)
  390. {
  391. int nlSock;
  392. struct sockaddr_nl srcAddr;
  393. struct nlmsghdr *nlMsg;
  394. char buffer[MAX_BUFFER_SIZE];
  395. int plug_in=0,plug_out=0;
  396. thread_handle_t *h=(thread_handle_t*)arg;
  397. printf("___plug_thread start\n");
  398. nlSock = socket(PF_NETLINK, SOCK_RAW, NETLINK_ROUTE);
  399. if (nlSock < 0) {
  400. printf("Failed to create Netlink socket");
  401. goto quit;
  402. }
  403. memset(&srcAddr, 0, sizeof(srcAddr));
  404. srcAddr.nl_family = AF_NETLINK;
  405. srcAddr.nl_groups = RTMGRP_LINK | RTMGRP_NOTIFY;
  406. if (bind(nlSock, (struct sockaddr *)&srcAddr, sizeof(srcAddr)) < 0) {
  407. perror("Failed to bind Netlink socket");
  408. close(nlSock);
  409. goto quit;
  410. }
  411. int flags = fcntl(nlSock, F_GETFL, 0);
  412. fcntl(nlSock, F_SETFL, flags | O_NONBLOCK);
  413. while (h->quit==0) {
  414. fd_set readSet;
  415. FD_ZERO(&readSet);
  416. FD_SET(nlSock, &readSet);
  417. // 设置超时时间为 1 秒
  418. struct timeval timeout;
  419. timeout.tv_sec = 1;
  420. timeout.tv_usec = 0;
  421. // 监视 Netlink Socket 的可读状态
  422. int result = select(nlSock + 1, &readSet, NULL, NULL, &timeout);
  423. if (result == -1) {
  424. perror("Failed to select");
  425. goto quit;
  426. } else if (result == 0) {
  427. // 超时,没有新数据到达
  428. //printf("Timeout\n");
  429. continue;
  430. }
  431. if (FD_ISSET(nlSock, &readSet)) {
  432. // 接收消息
  433. int bytesRead = recv(nlSock, buffer, MAX_BUFFER_SIZE, 0);
  434. if (bytesRead < 0) {
  435. perror("Failed to receive message from Netlink socket");
  436. close(nlSock);
  437. goto quit;
  438. }
  439. // 遍历接收到的消息,获取到Netlink的消息头。
  440. nlMsg = (struct nlmsghdr *)buffer;
  441. while (NLMSG_OK(nlMsg, bytesRead)) {
  442. if (nlMsg->nlmsg_type == NLMSG_DONE) {
  443. break;
  444. }
  445. if (nlMsg->nlmsg_type == RTM_NEWLINK || nlMsg->nlmsg_type == RTM_DELLINK) {
  446. //NLMSG_DATA()获取完整的ifinfomsg结构体的指针
  447. //根据消息头nlMsg获取到其后面的数据部分ifinfomsg结构体的指针
  448. struct ifinfomsg *ifInfo = (struct ifinfomsg *)NLMSG_DATA(nlMsg);
  449. if (ifInfo->ifi_family == AF_UNSPEC) {
  450. //获取路由信息的属性,以及消息负载的长度,通过这两个参数来判断本次的数据是否有效
  451. struct rtattr *attribute = IFLA_RTA(ifInfo);
  452. int attributeLen = IFLA_PAYLOAD(nlMsg);
  453. while (RTA_OK(attribute, attributeLen)) {
  454. char *ifaceName = (char *)RTA_DATA(attribute);
  455. if ((attribute->rta_type == IFLA_IFNAME) && (strcmp(ifaceName, "eth0")==0)) {
  456. //printf("____Interface: %s \n", ifaceName);
  457. if((ifInfo->ifi_flags & IFF_LOWER_UP)) {
  458. if(plug_in==0) {
  459. system("ifup eth0");
  460. printf("____eth0 plug in\n");
  461. plug_in = 1; plug_out = 0;
  462. }
  463. }
  464. else {
  465. if(plug_out==0) {
  466. system("ifdown eth0");
  467. printf("____eth0 plug out\n");
  468. plug_in = 0; plug_out = 1;
  469. }
  470. }
  471. }
  472. attribute = RTA_NEXT(attribute, attributeLen);
  473. }
  474. }
  475. }
  476. nlMsg = NLMSG_NEXT(nlMsg, bytesRead);
  477. }
  478. }
  479. }
  480. close(nlSock);
  481. quit:
  482. printf("___plug_thread exit\n");
  483. pthread_exit(NULL);
  484. }
  485. ///////////////////////////////////////////////////////////
  486. static void* polling_thread(void *arg)
  487. {
  488. thread_handle_t *h=(thread_handle_t*)arg;
  489. //wdog_init();
  490. //tmp_create();
  491. while(h->quit==0) {
  492. sleep(1);
  493. }
  494. pthread_exit(NULL);
  495. }
  496. int sys_init(void)
  497. {
  498. lock_s_init();
  499. paras_init();
  500. thread_start(THREAD_ID_POLLING, polling_thread, NULL);
  501. return 0;
  502. }
  503. #endif