|
@@ -6,6 +6,8 @@
|
|
|
#include "thread.h"
|
|
#include "thread.h"
|
|
|
#include "cfg.h"
|
|
#include "cfg.h"
|
|
|
#include "sqlite_handle.h"
|
|
#include "sqlite_handle.h"
|
|
|
|
|
+#include <net/if.h>
|
|
|
|
|
+#include <ifaddrs.h>
|
|
|
|
|
|
|
|
#if 1
|
|
#if 1
|
|
|
#define LOGD log_d
|
|
#define LOGD log_d
|
|
@@ -17,8 +19,8 @@
|
|
|
#define LOGW printf
|
|
#define LOGW printf
|
|
|
#endif
|
|
#endif
|
|
|
|
|
|
|
|
|
|
+#define SW_PORT_START 99329
|
|
|
|
|
|
|
|
-#define SW_HOST_IP "192.168.0.1"
|
|
|
|
|
#define NETSNMP_DS_APP_DONT_FIX_PDUS 0
|
|
#define NETSNMP_DS_APP_DONT_FIX_PDUS 0
|
|
|
|
|
|
|
|
|
|
|
|
@@ -27,6 +29,9 @@
|
|
|
typedef struct {
|
|
typedef struct {
|
|
|
netswitch_info_t sw;
|
|
netswitch_info_t sw;
|
|
|
netswitch_info_t sw2;
|
|
netswitch_info_t sw2;
|
|
|
|
|
+
|
|
|
|
|
+ int ipflag;
|
|
|
|
|
+ char ipaddr[64];
|
|
|
int port_start;
|
|
int port_start;
|
|
|
}ns_handle_t;
|
|
}ns_handle_t;
|
|
|
static ns_handle_t nsHandle;
|
|
static ns_handle_t nsHandle;
|
|
@@ -233,7 +238,6 @@ static port_info_t* get_port(netswitch_info_t *sw, char *mac)
|
|
|
}
|
|
}
|
|
|
return pt;
|
|
return pt;
|
|
|
}
|
|
}
|
|
|
-
|
|
|
|
|
static char *get_string(netsnmp_variable_list *var)
|
|
static char *get_string(netsnmp_variable_list *var)
|
|
|
{
|
|
{
|
|
|
u_char *buf=NULL;
|
|
u_char *buf=NULL;
|
|
@@ -253,6 +257,55 @@ static char *get_string(netsnmp_variable_list *var)
|
|
|
|
|
|
|
|
return NULL;
|
|
return NULL;
|
|
|
}
|
|
}
|
|
|
|
|
+static int get_ipaddr(int family, char *iface, char *addr)
|
|
|
|
|
+{
|
|
|
|
|
+ int r=-1;
|
|
|
|
|
+ struct ifaddrs *ifap, *ifa;
|
|
|
|
|
+
|
|
|
|
|
+ if (getifaddrs(&ifap) == -1) {
|
|
|
|
|
+ LOGE("___getifaddrs() error\n"); return -1;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ for (ifa = ifap; ifa != NULL; ifa = ifa->ifa_next) {
|
|
|
|
|
+ if (ifa->ifa_addr != NULL && ifa->ifa_addr->sa_family == family) {
|
|
|
|
|
+ if(family==AF_INET) {
|
|
|
|
|
+ struct sockaddr_in *sa,*msk;
|
|
|
|
|
+ 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;
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ else {
|
|
|
|
|
+ struct sockaddr_in6 *sa,*msk;
|
|
|
|
|
+ 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;
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ freeifaddrs(ifap);
|
|
|
|
|
+
|
|
|
|
|
+ return r;
|
|
|
|
|
+}
|
|
|
static void sw_init(netswitch_info_t *sw)
|
|
static void sw_init(netswitch_info_t *sw)
|
|
|
{
|
|
{
|
|
|
int i;
|
|
int i;
|
|
@@ -366,13 +419,13 @@ static void sw_print(char *s, netswitch_info_t *sw)
|
|
|
3. 第三种情况比较特殊,对于net-snmp不熟的也可能会范理解上的错误。
|
|
3. 第三种情况比较特殊,对于net-snmp不熟的也可能会范理解上的错误。
|
|
|
使用snmpget而忘记了添加OID结尾的实例标识符,net-snmp则会报出“No Such Instance currently exists at this OID”的错误。
|
|
使用snmpget而忘记了添加OID结尾的实例标识符,net-snmp则会报出“No Such Instance currently exists at this OID”的错误。
|
|
|
*/
|
|
*/
|
|
|
-static int get_port_start(void)
|
|
|
|
|
|
|
+static int get_port_start(char *ip)
|
|
|
{
|
|
{
|
|
|
int port=-1,val;
|
|
int port=-1,val;
|
|
|
netsnmp_variable_list *var,*pvar;
|
|
netsnmp_variable_list *var,*pvar;
|
|
|
|
|
|
|
|
char *ifEntry="1.3.6.1.2.1.2.2";
|
|
char *ifEntry="1.3.6.1.2.1.2.2";
|
|
|
- var = snmp_getbulk(SNMP_VERSION_2c, "public", SW_HOST_IP, ifEntry);
|
|
|
|
|
|
|
+ var = snmp_getbulk(SNMP_VERSION_2c, "public", ip, ifEntry);
|
|
|
if(var) {
|
|
if(var) {
|
|
|
int id;
|
|
int id;
|
|
|
char *p,*tok="ifIndex.";
|
|
char *p,*tok="ifIndex.";
|
|
@@ -406,21 +459,36 @@ static int get_port_start(void)
|
|
|
}
|
|
}
|
|
|
static int sw_get(netswitch_info_t *sw)
|
|
static int sw_get(netswitch_info_t *sw)
|
|
|
{
|
|
{
|
|
|
- int i,j,find=0;
|
|
|
|
|
|
|
+ int i,j,r,find=0;
|
|
|
char temp[100];
|
|
char temp[100];
|
|
|
netswitch_info_t tmp;
|
|
netswitch_info_t tmp;
|
|
|
netsnmp_variable_list *var,*pvar;
|
|
netsnmp_variable_list *var,*pvar;
|
|
|
ns_handle_t *h=&nsHandle;
|
|
ns_handle_t *h=&nsHandle;
|
|
|
|
|
+
|
|
|
|
|
+ if(h->ipflag==0) {
|
|
|
|
|
+ r = get_ipaddr(AF_INET, "eth0", h->ipaddr);
|
|
|
|
|
+ if(r) {
|
|
|
|
|
+ LOGE("____get ip4 failed\n");
|
|
|
|
|
+
|
|
|
|
|
+ r = get_ipaddr(AF_INET6, "eth0", h->ipaddr);
|
|
|
|
|
+ if(r) {
|
|
|
|
|
+ LOGE("____get ip6 failed\n");
|
|
|
|
|
+ return -1;
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ h->ipflag = 1;
|
|
|
|
|
+ }
|
|
|
|
|
+ //LOGD("___local ip: %s\n", h->ipaddr);
|
|
|
|
|
|
|
|
if(h->port_start==-1) {
|
|
if(h->port_start==-1) {
|
|
|
- h->port_start = get_port_start();
|
|
|
|
|
|
|
+ h->port_start = get_port_start(h->ipaddr);
|
|
|
if(h->port_start==-1) {
|
|
if(h->port_start==-1) {
|
|
|
return -1;
|
|
return -1;
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
char *dot1dTpFdbEntryOid="1.3.6.1.2.1.17.4.3.1";
|
|
char *dot1dTpFdbEntryOid="1.3.6.1.2.1.17.4.3.1";
|
|
|
- var = snmp_getbulk(SNMP_VERSION_2c, "public", SW_HOST_IP, dot1dTpFdbEntryOid);
|
|
|
|
|
|
|
+ var = snmp_getbulk(SNMP_VERSION_2c, "public", h->ipaddr, dot1dTpFdbEntryOid);
|
|
|
if(var) {
|
|
if(var) {
|
|
|
char *tok_mac="Hex-STRING: ", *tok_id="INTEGER: ";
|
|
char *tok_mac="Hex-STRING: ", *tok_id="INTEGER: ";
|
|
|
|
|
|
|
@@ -468,7 +536,7 @@ static int sw_get(netswitch_info_t *sw)
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
char *ipNetToMediaEntryOid="1.3.6.1.2.1.4.22.1";
|
|
char *ipNetToMediaEntryOid="1.3.6.1.2.1.4.22.1";
|
|
|
- var = snmp_getbulk(SNMP_VERSION_2c, "public", SW_HOST_IP, ipNetToMediaEntryOid);
|
|
|
|
|
|
|
+ var = snmp_getbulk(SNMP_VERSION_2c, "public", h->ipaddr, ipNetToMediaEntryOid);
|
|
|
if(var) {
|
|
if(var) {
|
|
|
char *tok_mac="STRING: ", *tok_ip="IpAddress: ";
|
|
char *tok_mac="STRING: ", *tok_ip="IpAddress: ";
|
|
|
|
|
|
|
@@ -516,7 +584,7 @@ static int sw_get(netswitch_info_t *sw)
|
|
|
char *ifEntry="1.3.6.1.2.1.2.2.1.8";
|
|
char *ifEntry="1.3.6.1.2.1.2.2.1.8";
|
|
|
for(i=0; i<SW_PORT_MAX; i++) {
|
|
for(i=0; i<SW_PORT_MAX; i++) {
|
|
|
sprintf(temp, "%s.%d", ifEntry, i+h->port_start);
|
|
sprintf(temp, "%s.%d", ifEntry, i+h->port_start);
|
|
|
- var = snmp_get(SNMP_VERSION_2c, "public", SW_HOST_IP, temp);
|
|
|
|
|
|
|
+ var = snmp_get(SNMP_VERSION_2c, "public", h->ipaddr, temp);
|
|
|
if(var) {
|
|
if(var) {
|
|
|
int id;
|
|
int id;
|
|
|
char *p,*tok_ifo="ifOperStatus.";
|
|
char *p,*tok_ifo="ifOperStatus.";
|
|
@@ -622,7 +690,7 @@ static int sw_alarm(netswitch_evts_t *evts, netswitch_info_t *old, netswitch_inf
|
|
|
|
|
|
|
|
alarm_str[0] = 0;
|
|
alarm_str[0] = 0;
|
|
|
for(i=0; i<SW_PORT_MAX; i++) {
|
|
for(i=0; i<SW_PORT_MAX; i++) {
|
|
|
- if(i==SW_INTER_PORT) {
|
|
|
|
|
|
|
+ if(i<SW_PORT_INDEX_START || i>SW_PORT_INDEX_END) {
|
|
|
continue;
|
|
continue;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
@@ -657,7 +725,7 @@ static void* sw_thread(void *arg)
|
|
|
netswitch_evts_t evts;
|
|
netswitch_evts_t evts;
|
|
|
thread_handle_t *h=(thread_handle_t*)arg;
|
|
thread_handle_t *h=(thread_handle_t*)arg;
|
|
|
ns_handle_t *nh=(ns_handle_t*)h->arg;
|
|
ns_handle_t *nh=(ns_handle_t*)h->arg;
|
|
|
- sqlite3 *db=get_db();;
|
|
|
|
|
|
|
+ sqlite3 *db=get_db();
|
|
|
|
|
|
|
|
dev_switch_init(db, &nh->sw);
|
|
dev_switch_init(db, &nh->sw);
|
|
|
nh->sw2 = nh->sw;
|
|
nh->sw2 = nh->sw;
|
|
@@ -682,7 +750,8 @@ int netswitch_init(void)
|
|
|
#ifdef USE_NETSWITCH
|
|
#ifdef USE_NETSWITCH
|
|
|
ns_handle_t *h=&nsHandle;
|
|
ns_handle_t *h=&nsHandle;
|
|
|
|
|
|
|
|
- h->port_start = -1; //SW_PORT_START
|
|
|
|
|
|
|
+ memset(h, 0, sizeof(ns_handle_t));
|
|
|
|
|
+ h->port_start = -1; //SW_PORT_START
|
|
|
thread_start(THREAD_ID_SWITCH, sw_thread, h, 10*MB, 0);
|
|
thread_start(THREAD_ID_SWITCH, sw_thread, h, 10*MB, 0);
|
|
|
#endif
|
|
#endif
|
|
|
|
|
|