|
|
@@ -5,6 +5,7 @@
|
|
|
#include "elog.h"
|
|
|
#include "thread.h"
|
|
|
#include "cfg.h"
|
|
|
+#include "lock.h"
|
|
|
#include "sqlite_handle.h"
|
|
|
#include <net/if.h>
|
|
|
#include <ifaddrs.h>
|
|
|
@@ -48,23 +49,24 @@ static netsnmp_variable_list* snmp_getx(int version, char *community, char *ip,
|
|
|
netsnmp_variable_list *vars=NULL,*pvar=NULL;
|
|
|
|
|
|
snmp_sess_init(&session);
|
|
|
+ session.timeout = 200000; //us
|
|
|
session.version = version;
|
|
|
session.peername = ip;
|
|
|
session.community = (unsigned char*)community;
|
|
|
session.community_len = strlen(community);
|
|
|
|
|
|
- ss = snmp_open(&session);
|
|
|
- if (ss == NULL) {
|
|
|
- LOGE("___snmp_open failed\n");
|
|
|
- return NULL;
|
|
|
- }
|
|
|
-
|
|
|
oid *poid = snmp_parse_oid(oid_str, name, &namelen);
|
|
|
if(poid==NULL) {
|
|
|
LOGE("___snmp_parse_oid %s failed\n", oid_str);
|
|
|
- goto quit;
|
|
|
+ goto out;
|
|
|
}
|
|
|
|
|
|
+ ss = snmp_open(&session);
|
|
|
+ if (ss == NULL) {
|
|
|
+ LOGE("___snmp_open failed\n");
|
|
|
+ goto out;
|
|
|
+ }
|
|
|
+
|
|
|
pdu = snmp_pdu_create(msgtype);
|
|
|
if(!pdu) {
|
|
|
LOGE("___snmp_pdu_create failed\n");
|
|
|
@@ -77,7 +79,7 @@ static netsnmp_variable_list* snmp_getx(int version, char *community, char *ip,
|
|
|
}
|
|
|
snmp_add_null_var(pdu, name, namelen);
|
|
|
|
|
|
- retry:
|
|
|
+ retry:
|
|
|
status = snmp_synch_response(ss, pdu, &rsp);
|
|
|
if (status == STAT_SUCCESS) {
|
|
|
if (rsp->errstat == SNMP_ERR_NOERROR) {
|
|
|
@@ -105,18 +107,19 @@ static netsnmp_variable_list* snmp_getx(int version, char *community, char *ip,
|
|
|
}
|
|
|
}
|
|
|
} else if (status == STAT_TIMEOUT) {
|
|
|
- LOGE("Timeout: No Response from %s.\n", session.peername);
|
|
|
+ LOGE("snmp_getx timeout: No response from %s\n", session.peername);
|
|
|
} else { /* status == STAT_ERROR */
|
|
|
- snmp_sess_perror("snmpget", ss);
|
|
|
+ snmp_sess_perror("snmp_getx", ss);
|
|
|
}
|
|
|
-
|
|
|
+
|
|
|
if (rsp) {
|
|
|
snmp_free_pdu(rsp);
|
|
|
}
|
|
|
-
|
|
|
+
|
|
|
quit:
|
|
|
snmp_close(ss);
|
|
|
-
|
|
|
+
|
|
|
+out:
|
|
|
return pvar;
|
|
|
}
|
|
|
|
|
|
@@ -346,7 +349,6 @@ static void sw_sync_ip_by_mac(netswitch_info_t *dst, netswitch_info_t *src)
|
|
|
for(i=0; i<SW_PORT_MAX; i++) {
|
|
|
id = get_id_by_mac(dst, &src->port[i]);
|
|
|
if(id>=0) {
|
|
|
- src->port[id] = src->port[i];
|
|
|
src->port[id].id = id;
|
|
|
}
|
|
|
}
|
|
|
@@ -447,7 +449,7 @@ static int get_port_start(char *ip)
|
|
|
}
|
|
|
free(pstr);
|
|
|
|
|
|
- if(port>2) {
|
|
|
+ if(port>1000) {
|
|
|
LOGD("_____start port: %d\n", port);
|
|
|
break;
|
|
|
}
|
|
|
@@ -503,7 +505,7 @@ 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);
|
|
|
@@ -531,7 +533,7 @@ static int sw_get(netswitch_info_t *sw)
|
|
|
char *dot1dTpFdbEntryOid="1.3.6.1.2.1.17.4.3.1";
|
|
|
var = snmp_getbulk(SNMP_VERSION_2c, "public", h->host_ip, dot1dTpFdbEntryOid);
|
|
|
if(var) {
|
|
|
- char *tok_mac="Hex-STRING: ", *tok_id="INTEGER: ";
|
|
|
+ char *tok;
|
|
|
|
|
|
sw_init(&tmp);
|
|
|
|
|
|
@@ -541,25 +543,30 @@ static int sw_get(netswitch_info_t *sw)
|
|
|
if(pstr) {
|
|
|
//LOGD("__1__%s\n", pstr);
|
|
|
|
|
|
- if(strstr(pstr, "2.17.4.3.1.1")) { //mac
|
|
|
- char *p=strstr(pstr, tok_mac);
|
|
|
+ if(strstr(pstr, "2.17.4.3.1.1") || strstr(pstr, "3.6.1.2.1.17.4.3.1.1")) { //mac
|
|
|
+ tok = "Hex-STRING: ";
|
|
|
+ char *p=strstr(pstr, tok);
|
|
|
if(p) {
|
|
|
- p += strlen(tok_mac);
|
|
|
+ p += strlen(tok);
|
|
|
get_mac(p, tmp.port[i].mac);
|
|
|
+ //LOGD("____%d___mac: %s\n", i, tmp.port[i].mac);
|
|
|
+
|
|
|
i++;
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- if(strstr(pstr, "2.17.4.3.1.2")) { //port
|
|
|
- char *p=strstr(pstr, tok_id);
|
|
|
+ if(strstr(pstr, "2.17.4.3.1.2") || strstr(pstr, "3.6.1.2.1.17.4.3.1.2")) { //port
|
|
|
+ tok = "INTEGER: ";
|
|
|
+ char *p=strstr(pstr, tok);
|
|
|
if(p) {
|
|
|
- p += strlen(tok_id);
|
|
|
+ p += strlen(tok);
|
|
|
tmp.port[j].id = atoi(p) - h->port_start;
|
|
|
//LOGD("____%d___id: %s, port_start: %d, id: %d\n", j, p, h->port_start, tmp.port[j].id);
|
|
|
|
|
|
j++;
|
|
|
}
|
|
|
}
|
|
|
+
|
|
|
free(pstr);
|
|
|
}
|
|
|
}
|
|
|
@@ -581,7 +588,7 @@ static int sw_get(netswitch_info_t *sw)
|
|
|
char *ipNetToMediaEntryOid="1.3.6.1.2.1.4.22.1";
|
|
|
var = snmp_getbulk(SNMP_VERSION_2c, "public", h->host_ip, ipNetToMediaEntryOid);
|
|
|
if(var) {
|
|
|
- char *tok_mac="STRING: ", *tok_ip="IpAddress: ";
|
|
|
+ char *tok=NULL;
|
|
|
|
|
|
sw_init(&tmp);
|
|
|
|
|
|
@@ -592,24 +599,40 @@ static int sw_get(netswitch_info_t *sw)
|
|
|
//LOGD("__2__%s\n", pstr);
|
|
|
|
|
|
if(strstr(pstr, "ipNetToMediaPhysAddress")) { //mac addr
|
|
|
- char *p=strstr(pstr, tok_mac);
|
|
|
+ tok = "STRING: ";
|
|
|
+ char *p=strstr(pstr, tok);
|
|
|
+ if(p) {
|
|
|
+ p += strlen(tok);
|
|
|
+ get_mac(p, tmp.port[i].mac);
|
|
|
+ //LOGD("__%d__%s\n", i, tmp.port[i].mac);
|
|
|
+
|
|
|
+ i++;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ else if(strstr(pstr, "3.6.1.2.1.4.22.1.2")) {
|
|
|
+ tok = "Hex-STRING: ";
|
|
|
+ char *p=strstr(pstr, tok);
|
|
|
if(p) {
|
|
|
- p += strlen(tok_mac);
|
|
|
+ p += strlen(tok);
|
|
|
get_mac(p, tmp.port[i].mac);
|
|
|
+ //LOGD("_____%d___mac:%s\n", i, tmp.port[i].mac);
|
|
|
+
|
|
|
i++;
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- if(strstr(pstr, "ipNetToMediaNetAddress")) { //ip addr
|
|
|
- char *p=strstr(pstr, tok_ip);
|
|
|
+ if(strstr(pstr, "ipNetToMediaNetAddress") || strstr(pstr, "3.6.1.2.1.4.22.1.3")) { //ip addr
|
|
|
+ tok = "IpAddress: ";
|
|
|
+ char *p=strstr(pstr, tok);
|
|
|
if(p) {
|
|
|
- p += strlen(tok_ip);
|
|
|
+ p += strlen(tok);
|
|
|
strcpy(tmp.port[j].ip, p);
|
|
|
j++;
|
|
|
|
|
|
//LOGD("__%d__ %s\n", j, p);
|
|
|
}
|
|
|
}
|
|
|
+
|
|
|
free(pstr);
|
|
|
}
|
|
|
}
|
|
|
@@ -630,24 +653,28 @@ static int sw_get(netswitch_info_t *sw)
|
|
|
var = snmp_get(SNMP_VERSION_2c, "public", h->host_ip, temp);
|
|
|
if(var) {
|
|
|
int id;
|
|
|
- char *p,*tok_ifo="ifOperStatus.";
|
|
|
+ char *p,*tok="ifOperStatus.";
|
|
|
|
|
|
for (pvar=var; pvar; pvar=pvar->next_variable) {
|
|
|
char *pstr = get_string(pvar);
|
|
|
if(pstr) {
|
|
|
//LOGD("__3__%s\n", pstr);
|
|
|
|
|
|
- p = strstr(pstr, tok_ifo);
|
|
|
- if(p) { //ip addr
|
|
|
- p += strlen(tok_ifo);
|
|
|
- id = atoi(p)-h->port_start;
|
|
|
- if(strstr(p, "up")) {
|
|
|
- sw->port[id].up = 1;
|
|
|
+ p = strstr(pstr, tok);
|
|
|
+ if(!p) { //ip addr
|
|
|
+ tok = "INTEGER: ";
|
|
|
+ p = strstr(pstr, tok);
|
|
|
+ if(p) {
|
|
|
+ p += strlen(tok);
|
|
|
+ id = atoi(p)-h->port_start;
|
|
|
+ if(strstr(p, "up")) {
|
|
|
+ sw->port[id].up = 1;
|
|
|
+ }
|
|
|
+ else if(strstr(p, "down")) {
|
|
|
+ sw->port[id].up = 0;
|
|
|
+ }
|
|
|
+ j++;
|
|
|
}
|
|
|
- else if(strstr(p, "down")) {
|
|
|
- sw->port[id].up = 0;
|
|
|
- }
|
|
|
- j++;
|
|
|
}
|
|
|
free(pstr);
|
|
|
}
|
|
|
@@ -668,29 +695,36 @@ static int sw_chk(netswitch_info_t *sw1, netswitch_info_t *sw2, netswitch_evts_t
|
|
|
|
|
|
memset(evts, 0, sizeof(netswitch_evts_t));
|
|
|
for(i=0; i<SW_PORT_MAX; i++) {
|
|
|
+
|
|
|
+ if(i<SW_PORT_INDEX_START || i>SW_PORT_INDEX_END) {
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+
|
|
|
+#if 0
|
|
|
if(sw1->port[i].up != sw2->port[i].up) {
|
|
|
if(sw2->port[i].up) {
|
|
|
LOGD("____ port %d plug in\n", i+1);
|
|
|
- evts->evt[i].cab = EVT_CAB_IN;
|
|
|
+ //evts->evt[i].cab = EVT_CAB_IN;
|
|
|
}
|
|
|
else {
|
|
|
LOGD("____ port %d plug out\n", i+1);
|
|
|
- evts->evt[i].cab = EVT_CAB_OUT;
|
|
|
+ //evts->evt[i].cab = EVT_CAB_OUT;
|
|
|
}
|
|
|
- flag = 1;
|
|
|
+ //flag = 1;
|
|
|
+ }
|
|
|
+
|
|
|
+ if(strlen(sw2->port[i].ip) && strcmp(sw1->port[i].ip, sw2->port[i].ip)) {
|
|
|
+ LOGD("____ port %d ip changed from: %s, to: %s\n", i+1, sw1->port[i].ip, sw2->port[i].ip);
|
|
|
+ //evts->evt[i].dev = EVT_IP_CHG;
|
|
|
+ //flag = 1;
|
|
|
}
|
|
|
+#endif
|
|
|
|
|
|
if(strlen(sw2->port[i].mac) && strcmp(sw1->port[i].mac, sw2->port[i].mac)) {
|
|
|
LOGD("____ port %d mac changed from: %s, to: %s\n", i+1, sw1->port[i].mac, sw2->port[i].mac);
|
|
|
evts->evt[i].dev = EVT_MAC_CHG;
|
|
|
flag = 1;
|
|
|
}
|
|
|
-
|
|
|
- if(strlen(sw2->port[i].ip) && strcmp(sw1->port[i].ip, sw2->port[i].ip)) {
|
|
|
- LOGD("____ port %d ip changed from: %s, to: %s\n", i+1, sw1->port[i].ip, sw2->port[i].ip);
|
|
|
- evts->evt[i].dev = EVT_IP_CHG;
|
|
|
- flag = 1;
|
|
|
- }
|
|
|
}
|
|
|
|
|
|
return flag;
|
|
|
@@ -759,7 +793,7 @@ static int sw_alarm(netswitch_evts_t *evts, netswitch_info_t *old, netswitch_inf
|
|
|
snprintf(temp, sizeof(temp), "port[%d] device ip changed from %s to %s\n", i+1, old->port[i].ip, new->port[i].ip);
|
|
|
}
|
|
|
else {
|
|
|
- snprintf(temp, sizeof(temp), "port[%d] device mac changed from %s to %s\n", i+1, old->port[i].mac, new->port[i].mac);
|
|
|
+ //snprintf(temp, sizeof(temp), "port[%d] device mac changed from %s to %s\n", i+1, old->port[i].mac, new->port[i].mac);
|
|
|
}
|
|
|
strcat(alarm_str, temp);
|
|
|
}
|
|
|
@@ -833,7 +867,7 @@ int netswitch_get(netswitch_info_t *info)
|
|
|
if(!info) {
|
|
|
return -1;
|
|
|
}
|
|
|
- *info = h->sw;
|
|
|
+ *info = h->sw2;
|
|
|
#endif
|
|
|
|
|
|
return 0;
|