| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623 |
- #include <sys/types.h>
- #include <sys/stat.h>
- #include <fcntl.h>
- #include <unistd.h>
- #include <string.h>
- #include <limits.h>
- #include "rtdbg.h"
- //#include "sys.h"
- #include "paras.h"
- #include "cfg.h"
- #include "fs.h"
- #include "lwip/netif.h"
- #include "lwip/netifapi.h"
- #include <rtthread.h>
- #include <netif/ethernetif.h>
- #include "paras.h"
- #include "lwip/prot/dhcp.h"
- #include <lwip/ip_addr.h>
- #include "lwip/dns.h"
- #include "net.h"
- #if 1
- #define LOGE LOG_E
- #define LOGW LOG_W
- #define LOGD LOG_D
- #else
- #define LOGD rt_printf
- #define LOGW rt_printf
- #define LOGE rt_printf
- #endif
- #define DFLT_NIC "e0"
- typedef struct{
- struct netif *netif;
- struct netdev *netdev;
- }net_handle_t;
- static net_handle_t handle_net = {0};
- void print_net(char*s, network_data_t *p)
- {
- LOGD("__%s__net.mode: %d\n", s, p->mode);
- LOGD("__%s__net.addr: %s\n", s, p->ip_address);
- LOGD("__%s__net.mask: %s\n", s, p->mask);
- LOGD("__%s__net.gateway: %s\n", s, p->gateway);
- LOGD("__%s__net.dns1: %s\n", s, p->dns1);
- LOGD("__%s__net.dns2: %s\n", s, p->dns2);
- }
- void print_modbus(char*s, modbus_data_t *p)
- {
- LOGD("__%s__mbus.mode: %d\n", s, p->mode);
- LOGD("__%s__mbus.addr: %d\n", s, p->addr);
- LOGD("__%s__mbus.baud: %d\n", s, p->baudrate);
- LOGD("__%s__mbus.path: %s\n", s, p->path);
- }
- static int set_dns(network_data_t *para)
- {
-
- }
- static int set_ipaddr(int family, const char* iface, const char* addr, const char* netmask)
- {
- ip4_addr_t ipaddr4;
- ip6_addr_t ipaddr6;
- // if(family==AF_INET) {
- //ip4_addr_set(&ipaddr4, IP4_ADDR(&ipaddr4, 0xbeef, 0xfeed, 0xdead, 0xbeef));
- // }
- // else {
- //ip6_addr_set(&ipaddr6, IP6_ADDR(&ipaddr6, 0xbeef, 0xfeed, 0xdead, 0xbeef, 0x1337, 0xbabe, 0xbaad, 0xface));
-
- //netif_add_ip6_address(&netif, &ipaddr_v6, &chosen_idx);
- //netif.ip6_addr_state[chosen_idx] = IP6_ADDR_VALID;
- //ip6_addr_assign_zone(ip_2_ip6(&netif.ip6_addr[0]), IP6_UNICAST, &netif);
- //netif_ip6_addr_set_state(&netif, 0, IP6_ADDR_TENTATIVE);
- // }
-
- return 0;
- }
- static int get_ipaddr(int family, char *iface, char *addr, char *netmask)
- {
-
-
- return 0;
- }
- static int set_gateway(int family, const char* gateway)
- {
-
-
- return 0;
- }
- static int get_gateway(int family, const char* gateway)
- {
- return 0;
- }
- static int set_dhcp(int on)
- {
- if(on) {
-
- }
- else {
-
- }
- return 0;
- }
- static int is_number(char *str)
- {
- while (*str) {
- if (!isdigit(*str)) {
- return 0;
- }
- str++;
- }
- return 1;
- }
- static int ip_check(char* addr)
- {
- int flag=0;
- #if 0
- struct in_addr addr4;
- struct in6_addr addr6;
- if (inet_pton(AF_INET, addr, &addr4) == 1) {
- flag = IP_V4;
- } else if (inet_pton(AF_INET6, addr, &addr6) == 1) {
- flag = IP_V6;
- }
- if(flag==IP_ERR) {
- LOGE("____ wrong ip addr: %s\n", addr);
- }
- #endif
- return flag;
- }
- static int is_ipv6_prefix(char *ss)
- {
- // if(is_number(ss)) {
- // int val = atoi(ss);
- // if(val>1 && val<128) {
- // return 1;
- // }
- // }
- return 0;
- }
- static int nw_check(network_data_t *para, int ipv)
- {
- #if 0
- if(ipv==IP_V4) {
- if((ip_check(para->ipaddr)!=IP_V4) || (ip_check(para->mask)!=IP_V4) || (ip_check(para->gateway)!=IP_V4)) {
- return -1;
- }
- }
- else {
- if((ip_check(para->ipaddr)!=IP_V6) || (!is_ipv6_prefix(para->mask)) || (ip_check(para->gateway)!=IP_V6)) {
- return -1;
- }
- }
- #endif
- return 0;
- }
- static int get_net(network_data_t *para, int ipv)
- {
- return 0;
- }
- static int set_net(network_data_t *para, int ipv)
- {
- int r =0;
- #if 0
- int r,ipv2;
- network_data_t info4,info6,*info;
- get_net(&info4, IP_V4);
- get_net(&info6, IP_V6);
- info = ((ipv==IP_V4)?&info4:&info6);
-
- if(para->mode<2 && para->mode!=info->mode) {
- info->mode = para->mode;
- }
-
- ipv2 = ip_check(para->ipaddr);
- if((ipv==ipv2) && strcmp(para->ipaddr, info->ipaddr)) {
- strcpy(info->ipaddr, para->ipaddr);
- }
-
- if(ipv==IP_V4) {
- ipv2 = ip_check(para->mask);
- }
- else {
- ipv2=is_ipv6_prefix(para->mask)?IP_V6:IP_ERR;
- }
- if((ipv==ipv2) && strcmp(para->mask, info->mask)) {
- strcpy(info->mask, para->mask);
- }
- ipv2 = ip_check(para->gateway);
- if((ipv==ipv2) && strcmp(para->gateway, info->gateway)) {
- strcpy(info->gateway, para->gateway);
- }
- #endif
- return r;
- }
- //////////////////////////////////////////////////////////////////////////////
- int sys_set_net(network_data_t *para, int ip_version)
- {
- int r,ipv;
- #if 0
- network_data_t para2;
- r = get_net(¶2, ip_version);
- if(r==0) {
- if(memcmp(para, ¶2, sizeof(network_data_t))==0) {
- LOGW("%s NetworkInfo is same, no need to set\n", DFLT_NIC);
- return -1;
- }
- set_dns(para);
- r = set_net(para, ip_version);
- if(r==0) {
- sys_reboot();
- //nic_restart(DFLT_NIC); exit(0);
- }
- }
- else {
- LOGE("get_net failed\n");
- }
- #endif
- return r;
- }
- int sys_get_net(network_data_t *para, int ip_version)
- {
- int r;
- r = get_net(para, ip_version);
- return r;
- }
- int sys_get_mac(char *mac, int maclen)
- {
- #if 0
- int sock;
- struct ifreq ifr;
- if(!mac || maclen<6) {
- return -1;
- }
- sock = socket(AF_INET, SOCK_DGRAM, 0);
- if (sock == -1){
- return -1;
- }
- strcpy(ifr.ifr_name, "eth0");
- if (ioctl(sock, SIOCGIFHWADDR, &ifr) < 0){
- close(sock); return -1;
- }
- close(sock);
- //uint8_t *p = (uint8_t*)ifr.ifr_hwaddr.sa_data;
- //snprintf(mac, maclen, "%02x%02x%02x%02x%02x%02x", p[0], p[1], p[2], p[3], p[4], p[5]);
- #endif
- return 0;
- }
- #if 0
- int sys_get_chip_id(int *id)
- {
- int sock;
- struct ifreq ifr;
- if(!id) {
- return -1;
- }
-
- return 0;
- }
- #endif
- int sys_ip_check(char *ip)
- {
- return ip_check(ip);
- }
- static void ip_save(network_data_t *data)
- {
- paras_data_t* para = paras_get();
- memcmp(para->netwk.ipaddr,data->ipaddr,strlen(data->ipaddr));
- memcmp(para->netwk.gateway,data->gateway,strlen(data->gateway));
- memcmp(para->netwk.mask,data->mask,strlen(data->mask));
- memcmp(para->netwk.dns1,data->dns1,strlen(data->dns1));
- memcmp(para->netwk.dns2,data->dns2,strlen(data->dns2));
- para->netwk.mode = data->mode;
- paras_save();
- return;
- }
- void net_get_ip(network_data_t *data)
- {
- paras_data_t* para = paras_get();
- if(handle_net.netif && netif_is_up(handle_net.netif))
- {
- char * str = ip4addr_ntoa(netif_ip4_addr(handle_net.netif));
- memcpy(data->ipaddr,str,strlen(str));
- str = ip4addr_ntoa(netif_ip4_netmask(handle_net.netif));
- memcpy(data->mask,str,strlen(str));
- str = ip4addr_ntoa(netif_ip4_gw(handle_net.netif));
- memcpy(data->gateway,str,strlen(str));
- data->mode = para->netwk.mode;
-
- }else
- {
- memcpy(data->dns1,"8.8.8.8",strlen("8.8.8.8"));
- memcpy(data->dns2,"114.114.114.114",strlen("114.114.114.114"));
- memcpy(data->ipaddr,"0.0.0.0",strlen("0.0.0.0"));
- memcpy(data->gateway,"0.0.0.0",strlen("0.0.0.0"));
- memcpy(data->mask,"0.0.0.0",strlen("0.0.0.0"));
- data->mode = para->netwk.mode;
- }
- }
- static void ip_set_static()
- {
- paras_data_t* para = paras_get();
- u32_t ip = ipaddr_addr(para->netwk.ipaddr);
- u32_t gw = ipaddr_addr(para->netwk.gateway);
- u32_t mask = ipaddr_addr(para->netwk.mask);
- netifapi_netif_set_addr(handle_net.netif, (ip4_addr_t*)&ip, (ip4_addr_t*)&mask, (ip4_addr_t*)&gw);
-
- ip_addr_t primary_dns, backup_dns;
-
- ipaddr_aton(para->netwk.dns1, &primary_dns); // 主 DNS
- ipaddr_aton(para->netwk.dns2, &backup_dns); // 备 DNS
-
- dns_setserver(0, &primary_dns); // 索引 0 为主
- dns_setserver(1, &backup_dns); // 索引 1 为备
- netifapi_netif_set_link_down(handle_net.netif);
- usleep(10000);
- netifapi_netif_set_link_up(handle_net.netif);
- usleep(100000);
- }
- int net_ipaddr_set(network_data_t *data)
- {
- paras_data_t* para = paras_get();
-
- if(para->netwk.mode == 0)
- {
- if(data->mode == 1)
- {
- ip_save(data);
- netifapi_dhcp_stop(handle_net.netif);
- ip_set_static();
- LOG_D("mode dhcp 2 static\n");
- }
- }else if(para->netwk.mode == 1)
- {
- if(data->mode == 0)
- {
- netifapi_dhcp_start(handle_net.netif);
- ip_save(data);
- LOG_D("mode static 2 dhcp\n");
- }else if(data->mode == 1)
- {
- ip_save(data);
- ip_set_static();
- LOG_D("mode static 2 static\n");
- }
- }else
- {
- LOG_E("error mode\n");
- }
- }
- int net_init(void)
- {
- //获取网卡设备
- rt_device_t dev = rt_device_find(DFLT_NIC);
- if(dev == RT_NULL)
- {
- LOG_E("___ %s is not exist!!\n",DFLT_NIC);
- goto failed;
- }
- struct eth_device *eth_dev = (struct eth_device*)dev;
- if(eth_dev)
- {
- handle_net.netif = eth_dev->netif;
- }else{
- goto failed;
- }
- // struct netdev *netdev = netdev_get_by_name(DFLT_NIC);
- // if (netdev == RT_NULL)
- // {
- // LOG_E("Netdev 'e0' not found!\n");
- // return -1;
- // }
-
-
- // paras_data_t* para = paras_get();
- //
- // if(para->netwk.mode == 0) //dhcp
- // {
- // //获取dhcp ipaddr and
- // struct dhcp* dhcp = netif_dhcp_data(handle_net.netif);
- // if(!dhcp)
- // {
- // LOG_E("___ %s dhcp failed!!\n",DFLT_NIC);
- // goto failed;
- // }
- //
- // if(dhcp->state == DHCP_STATE_REBINDING)
- // {
- // //dhcp->offered_ip_addr.addr =
- // //filt
- // char * str = ip4addr_ntoa(netif_ip4_addr(handle_net.netif));
- // LOG_D("____%s\n",str);
- // memcpy(para->netwk.ipaddr,str,strlen(str));
- // str = ip4addr_ntoa(netif_ip4_netmask(handle_net.netif));
- // LOG_D("____%s\n",str);
- // memcpy(para->netwk.mask,str,strlen(str));
- //
- // str = ip4addr_ntoa(netif_ip4_gw(handle_net.netif));
- // memcpy(para->netwk.gateway,str,strlen(str));
- // LOG_D("____%s\n",str);
- //
- // }else
- // {
- //
- // }
- //
- // }else if(para->netwk.mode == 1) //static
- // {
- // netifapi_dhcp_stop(handle_net.netif);
- // ip_set_static();
- // }
- //
-
-
- return 0;
- failed:
- return -1;
- }
- #if 0
- ///////////////////////////////////////////////////////////////////////
- #define MAX_BUFFER_SIZE 1024
- static void* plug_thread(void *arg)
- {
- int nlSock;
- struct sockaddr_nl srcAddr;
- struct nlmsghdr *nlMsg;
- char buffer[MAX_BUFFER_SIZE];
- int plug_in=0,plug_out=0;
- thread_handle_t *h=(thread_handle_t*)arg;
- printf("___plug_thread start\n");
- nlSock = socket(PF_NETLINK, SOCK_RAW, NETLINK_ROUTE);
- if (nlSock < 0) {
- printf("Failed to create Netlink socket");
- goto quit;
- }
- memset(&srcAddr, 0, sizeof(srcAddr));
- srcAddr.nl_family = AF_NETLINK;
- srcAddr.nl_groups = RTMGRP_LINK | RTMGRP_NOTIFY;
- if (bind(nlSock, (struct sockaddr *)&srcAddr, sizeof(srcAddr)) < 0) {
- perror("Failed to bind Netlink socket");
- close(nlSock);
- goto quit;
- }
- int flags = fcntl(nlSock, F_GETFL, 0);
- fcntl(nlSock, F_SETFL, flags | O_NONBLOCK);
- while (h->quit==0) {
- fd_set readSet;
- FD_ZERO(&readSet);
- FD_SET(nlSock, &readSet);
- // 设置超时时间为 1 秒
- struct timeval timeout;
- timeout.tv_sec = 1;
- timeout.tv_usec = 0;
- // 监视 Netlink Socket 的可读状态
- int result = select(nlSock + 1, &readSet, NULL, NULL, &timeout);
- if (result == -1) {
- perror("Failed to select");
- goto quit;
- } else if (result == 0) {
- // 超时,没有新数据到达
- //printf("Timeout\n");
- continue;
- }
- if (FD_ISSET(nlSock, &readSet)) {
- // 接收消息
- int bytesRead = recv(nlSock, buffer, MAX_BUFFER_SIZE, 0);
- if (bytesRead < 0) {
- perror("Failed to receive message from Netlink socket");
- close(nlSock);
- goto quit;
- }
- // 遍历接收到的消息,获取到Netlink的消息头。
- nlMsg = (struct nlmsghdr *)buffer;
- while (NLMSG_OK(nlMsg, bytesRead)) {
- if (nlMsg->nlmsg_type == NLMSG_DONE) {
- break;
- }
- if (nlMsg->nlmsg_type == RTM_NEWLINK || nlMsg->nlmsg_type == RTM_DELLINK) {
- //NLMSG_DATA()获取完整的ifinfomsg结构体的指针
- //根据消息头nlMsg获取到其后面的数据部分ifinfomsg结构体的指针
- struct ifinfomsg *ifInfo = (struct ifinfomsg *)NLMSG_DATA(nlMsg);
- if (ifInfo->ifi_family == AF_UNSPEC) {
- //获取路由信息的属性,以及消息负载的长度,通过这两个参数来判断本次的数据是否有效
- struct rtattr *attribute = IFLA_RTA(ifInfo);
- int attributeLen = IFLA_PAYLOAD(nlMsg);
-
- while (RTA_OK(attribute, attributeLen)) {
- char *ifaceName = (char *)RTA_DATA(attribute);
- if ((attribute->rta_type == IFLA_IFNAME) && (strcmp(ifaceName, "eth0")==0)) {
-
- //printf("____Interface: %s \n", ifaceName);
- if((ifInfo->ifi_flags & IFF_LOWER_UP)) {
- if(plug_in==0) {
- system("ifup eth0");
- printf("____eth0 plug in\n");
- plug_in = 1; plug_out = 0;
- }
- }
- else {
- if(plug_out==0) {
- system("ifdown eth0");
- printf("____eth0 plug out\n");
- plug_in = 0; plug_out = 1;
- }
- }
- }
- attribute = RTA_NEXT(attribute, attributeLen);
- }
- }
- }
- nlMsg = NLMSG_NEXT(nlMsg, bytesRead);
- }
- }
- }
- close(nlSock);
- quit:
- printf("___plug_thread exit\n");
- pthread_exit(NULL);
- }
- ///////////////////////////////////////////////////////////
- static void* polling_thread(void *arg)
- {
- thread_handle_t *h=(thread_handle_t*)arg;
- //wdog_init();
- //tmp_create();
- while(h->quit==0) {
-
- sleep(1);
- }
- pthread_exit(NULL);
- }
- int sys_init(void)
- {
- lock_s_init();
- paras_init();
-
- thread_start(THREAD_ID_POLLING, polling_thread, NULL);
- return 0;
- }
- #endif
|