Bladeren bron

1、针对anderson,将interfaces改为dhcp
2、修改交换机ip获取为动态方式

guohui 2 jaren geleden
bovenliggende
commit
542e6eac1f
4 gewijzigde bestanden met toevoegingen van 39 en 30 verwijderingen
  1. 2 2
      pro/config/cfg.h
  2. 1 5
      pro/src/json_handle.c
  3. 34 23
      pro/src/netswitch.c
  4. 2 0
      pub

+ 2 - 2
pro/config/cfg.h

@@ -49,11 +49,11 @@
     //#define USE_SMTP
     #define USE_NETSWITCH
 
+    #define SW_STATIC_PORT_START
     #define SW_PORT_INDEX_START         0
     #define SW_PORT_INDEX_END           21
 
-    #define SW_STATIC_IP
-    #define SW_STATIC_PORT_START
+    //#define SW_STATIC_IP
 
     #ifdef SW_STATIC_IP
         #define SW_HOST_IP              "192.168.234.1"

+ 1 - 5
pro/src/json_handle.c

@@ -1139,14 +1139,10 @@ char* json_to_system_config_info(void)
     // 添加基本键值对
     int nLanguageID=__globalDeviceManage._globalDevInfo.product_language+1;
     cJSON_AddNumberToObject(data_field, "systemLanguage", nLanguageID);
+    
     //返回websockt
-    // cJSON_AddStringToObject(data_field,"webSocket","ws://192.168.2.132:6785/websocket/"); 
     char strIPInfo[64] = "ws://192.168.1.132:6785/websocket/";
-#ifdef USE_NETSWITCH
-    sprintf(strIPInfo,"ws://%s:6785/websocket/", SW_PDU_IP);
-#else
     sprintf(strIPInfo,"ws://%s:6785/websocket/", getLocalIpAddress("eth0"));
-#endif
     cJSON_AddStringToObject(data_field,"webSocket",strIPInfo); 
     
     get_base64(&logo, NULL);

+ 34 - 23
pro/src/netswitch.c

@@ -8,6 +8,7 @@
 #include "thread.h"
 #include "cfg.h"
 #include "lock.h"
+#include "sys.h"
 #include "sqlite_handle.h"
 #include "smtp.h"
 
@@ -35,6 +36,8 @@ typedef struct {
 
     int              ipflag;
     char             host_ip[64];
+    char             local_ip[64];
+
     int              port_start;
     
     pthread_mutex_t  mutex;
@@ -267,9 +270,10 @@ static char *get_string(netsnmp_variable_list *var)
     
     return NULL;
 }
-static int get_ipaddr(int family, char *iface, char *addr)
+static int get_ipaddr(int family, char *iface, char *host, char *local)
 {
     int r=-1;
+    char temp[100];
     struct ifaddrs *ifap, *ifa;
 
     if (getifaddrs(&ifap) == -1) {
@@ -283,14 +287,15 @@ static int get_ipaddr(int family, char *iface, char *addr)
                 sa = (struct sockaddr_in *)  ifa->ifa_addr;
                 msk = (struct sockaddr_in *) ifa->ifa_netmask;
 
-                if(addr) {
-                    char *p = (char*)inet_ntop(family, &sa->sin_addr, addr, INET6_ADDRSTRLEN);
-                    if(p) {
-                        //printf("___ipv4: %s\n", p);
-                        if(strcmp(addr, "127.0.0.1") && strcmp(addr, "172.17.0.1")) {
-                            p = strrchr(p, '.'); p[1] = '1'; p[2] = 0;
-                            r = 0; break;
-                        }
+                char *p = (char*)inet_ntop(family, &sa->sin_addr, temp, INET6_ADDRSTRLEN);
+                if(p) {
+                    //printf("___ipv4: %s\n", p);
+
+                    if(strcmp(p, "127.0.0.1") && strcmp(p, "172.17.0.1")) {
+                        strcpy(host, p); strcpy(local, p);
+
+                        p = strrchr(host, '.'); p[1] = '1'; p[2] = 0;
+                        r = 0; break;
                     }
                 }
             }
@@ -299,14 +304,15 @@ static int get_ipaddr(int family, char *iface, char *addr)
                 sa = (struct sockaddr_in6 *)ifa->ifa_addr;
                 msk = (struct sockaddr_in6 *)ifa->ifa_netmask;
                 
-                if(addr) {
-                    char *p = (char*)inet_ntop(family, &sa->sin6_addr, addr, INET6_ADDRSTRLEN);
-                    if(p) {
-                        //printf("___ipv6: %s\n", p);
-                        if(strcmp(addr, "::1")) {
-                            p = strrchr(p, ':'); p[1] = '1'; p[2] = 0;
-                            r = 0; break;
-                        }
+                char *p = (char*)inet_ntop(family, &sa->sin6_addr, temp, INET6_ADDRSTRLEN);
+                if(p) {
+                    //printf("___ipv6: %s\n", p);
+
+                    if(strcmp(p, "::1")) {
+                        strcpy(host, p); strcpy(local, p);
+
+                        p = strrchr(host, ':'); p[1] = '1'; p[2] = 0;
+                        r = 0; break;
                     }
                 }
             }
@@ -515,23 +521,30 @@ static int sw_get(netswitch_info_t *sw)
         strcpy(h->host_ip, SW_HOST_IP);
         //get_hostnet(hnet, h->host_ip);
         //sprintf(temp, "ip addr add dev eth0 %s/24; ip route add %s/24 via %s", SW_PDU_IP, hnet, h->host_ip);
-        set_ip(SW_PDU_IP); set_gw(h->host_ip);
+        //set_ip(SW_PDU_IP); set_gw(h->host_ip);
         h->ipflag = 1;
 #else
-        r = get_ipaddr(AF_INET, "eth0", h->host_ip);
+        r = get_ipaddr(AF_INET, "eth0", h->host_ip, h->local_ip);
         if(r) {
             LOGE("____get ip4 failed\n");
 
-            r = get_ipaddr(AF_INET6, "eth0", h->host_ip);
+            r = get_ipaddr(AF_INET6, "eth0", h->host_ip, h->local_ip);
             if(r) {
                 LOGE("____get ip6 failed\n");
                 return -1;
             }
         }
+
+        if((p=strrchr(h->local_ip, '.')) || (p=strrchr(h->local_ip, ':'))) {
+            if(strstr(p, "132")==0) {
+                p[1] = 0; strcat(p, "132");
+                set_ip(h->local_ip); set_gw(h->host_ip);
+            }
+        }
         h->ipflag = 1;
 #endif
     }
-    //LOGD("___local ip: %s\n", h->host_ip);
+    LOGD("___netswitch ip: %s\n", h->host_ip);
 
     if(h->port_start==-1) {
         h->port_start = get_port_start(h->host_ip);
@@ -540,8 +553,6 @@ static int sw_get(netswitch_info_t *sw)
         }
     }
 
-    //system("/root/run/app/snmp/bin/snmpbulkget -v 2c -c public 192.168.234.1 1.3.6.1.2.1.17.4.3.1");
-
 #if 1
     char *dot1dTpFdbEntryOid="1.3.6.1.2.1.17.4.3.1";
     var = snmp_getbulk(SNMP_VERSION_2c, "public", h->host_ip, dot1dTpFdbEntryOid);

+ 2 - 0
pub

@@ -26,6 +26,7 @@ function do_copy()
 {
     echo do_copy ...
     
+    rm -f $curdir/output/t113i/etc/network/interfaces
     mkdir -p $curdir/output/t113i/root/run/app/image
     if [ $cust -eq 1 ]; then
         rm -rf  $curdir/output/t113i/root/run/web
@@ -78,6 +79,7 @@ function do_copy()
         rm -rf  $curdir/output/t113i/root/run/web
         cp -rf $custdir/web/anderson  $curdir/output/t113i/root/run/web
         
+        cp -f $custdir/etc/interfaces $curdir/output/t113i/etc/network/
         cp -f $custdir/db/ac_i3_o3_english.db $curdir/output/t113i/root/run/app/pdu_dat.db
         cp -f $custdir/logo/logo_neuter.png $curdir/output/t113i/root/run/app/image/logo.png
     elif [ $cust -eq 9 ]; then