From 321d3a1b42b6a9c3962137e428695a976fd8d181 Mon Sep 17 00:00:00 2001 From: carloleo Date: Thu, 4 Jul 2024 21:42:49 +0200 Subject: [PATCH 1/9] include/SNMP.h declared new callback collecting traps src/SNMP.cpp added new session to receive traps --- include/SNMP.h | 3 +++ src/SNMP.cpp | 47 ++++++++++++++++++++++++++++++++++++++++++++--- 2 files changed, 47 insertions(+), 3 deletions(-) diff --git a/include/SNMP.h b/include/SNMP.h index 2c14dd8e9a7f..4eec23d3427c 100644 --- a/include/SNMP.h +++ b/include/SNMP.h @@ -51,6 +51,9 @@ class SNMPSession { }; #endif +int read_snmp_trap(int operation, struct snmp_session *sp, int reqid, + struct snmp_pdu *pdu, void *magic); + class SNMP { private: u_int snmp_version; diff --git a/src/SNMP.cpp b/src/SNMP.cpp index 728e9a43e3d8..fb9275dbd91f 100644 --- a/src/SNMP.cpp +++ b/src/SNMP.cpp @@ -48,8 +48,33 @@ SNMP::SNMP() { batch_mode = false; #ifdef HAVE_LIBSNMP init_snmp("ntopng"); + //trap management + //instantiate necessary resources. Perhaps, this snippet can be place better. + const char* port = "upd:162"; + const char* application = "ntopng snmp trap"; + netsnmp_transport *snmpTransport = netsnmp_transport_open_server(application,port); + if(snmpTransport == NULL){ + snmp_perror("netsnmp_transport_open_server "); + return; + } + //trap session + SNMPSession* trap_session = new SNMPSession; + //TODO: check if other fields need to be populated + snmp_sess_init(&trap_session->session); + trap_session->session.callback = read_snmp_trap; + trap_session->session.callback_magic = (void *) snmpTransport; + trap_session->session.authenticator = NULL; + netsnmp_session* rc = snmp_add(&trap_session->session,snmpTransport, NULL, NULL); + if (rc == NULL) { + snmp_perror("FAILURE: "); + netsnmp_transport_free(snmpTransport); + } + trap_session->session_ptr = snmp_sess_open(&trap_session->session); + //add session + this->sessions.push_back(trap_session); + printf("TRAP SESSION OK "); //debug + #endif - getbulk_max_num_repetitions = 10; } @@ -280,10 +305,11 @@ void SNMP::send_snmpv1v2c_request(char *agent_host, char *community, return; } } else { - if(sessions.size() == 0) { + //because of adding the snmptrap session inside the constructor + if((sessions.size() - 1) == 0) { goto create_snmp_session; } else { - snmpSession = sessions.at(0); + snmpSession = sessions.at(1); //the first session is going to be the trap session } } @@ -1121,3 +1147,18 @@ int SNMP::snmp_get_fctn(lua_State *vm, snmp_pdu_primitive pduType, return (snmp_read_response(vm, timeout)); } } +//call back printing trap +int read_snmp_trap(int operation, struct snmp_session *sp, int reqid, + struct snmp_pdu *pdu, void *magic){ + switch (operation){ + case SNMP_MSG_TRAP: + case SNMP_MSG_TRAP2: + printf("trap type %l specific type %l",pdu->trap_type,pdu->specific_type); + + break; + default: + printf("Invalid operation %d",operation); + break; + } + return 1; +} \ No newline at end of file From 1c95a8df2780095a7e2bf42a10fb14e5c97fa52d Mon Sep 17 00:00:00 2001 From: carloleo Date: Fri, 5 Jul 2024 00:28:33 +0200 Subject: [PATCH 2/9] - fixed snmp transport creation - added log - fixed typo --- src/SNMP.cpp | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/src/SNMP.cpp b/src/SNMP.cpp index fb9275dbd91f..532b8afd8372 100644 --- a/src/SNMP.cpp +++ b/src/SNMP.cpp @@ -49,9 +49,11 @@ SNMP::SNMP() { #ifdef HAVE_LIBSNMP init_snmp("ntopng"); //trap management - //instantiate necessary resources. Perhaps, this snippet can be place better. + //instantiate necessary resources. Perhaps, this snippet can be placed better. const char* port = "upd:162"; const char* application = "ntopng snmp trap"; + //this needs to be called so that the snmp library can initialize the supported transports list + netsnmp_tdomain_init(); netsnmp_transport *snmpTransport = netsnmp_transport_open_server(application,port); if(snmpTransport == NULL){ snmp_perror("netsnmp_transport_open_server "); @@ -72,7 +74,7 @@ SNMP::SNMP() { trap_session->session_ptr = snmp_sess_open(&trap_session->session); //add session this->sessions.push_back(trap_session); - printf("TRAP SESSION OK "); //debug + ntop->getTrace()->traceEvent(TRACE_DEBUGGING,__FILE__,__LINE__,"SNMP TRAP SESSION OK"); #endif getbulk_max_num_repetitions = 10; @@ -1147,17 +1149,16 @@ int SNMP::snmp_get_fctn(lua_State *vm, snmp_pdu_primitive pduType, return (snmp_read_response(vm, timeout)); } } -//call back printing trap +//callback printing trap int read_snmp_trap(int operation, struct snmp_session *sp, int reqid, struct snmp_pdu *pdu, void *magic){ switch (operation){ case SNMP_MSG_TRAP: case SNMP_MSG_TRAP2: - printf("trap type %l specific type %l",pdu->trap_type,pdu->specific_type); - + ntop->getTrace()->traceEvent(TRACE_DEBUGGING, __FILE__,__LINE__,"trap type %ld specific type %ld", pdu->trap_type, pdu->specific_type); break; - default: - printf("Invalid operation %d",operation); + default: + ntop->getTrace()->traceEvent(TRACE_DEBUGGING,__FILE__,__LINE__,"Invalid operation %d",operation); break; } return 1; From 841976f6805c8fd4537634746f4c2e3557573b4a Mon Sep 17 00:00:00 2001 From: carloleo Date: Sat, 6 Jul 2024 18:57:42 +0200 Subject: [PATCH 3/9] fix trap parsing --- src/SNMP.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/SNMP.cpp b/src/SNMP.cpp index 532b8afd8372..17aa12c13329 100644 --- a/src/SNMP.cpp +++ b/src/SNMP.cpp @@ -1152,7 +1152,7 @@ int SNMP::snmp_get_fctn(lua_State *vm, snmp_pdu_primitive pduType, //callback printing trap int read_snmp_trap(int operation, struct snmp_session *sp, int reqid, struct snmp_pdu *pdu, void *magic){ - switch (operation){ + switch (pdu->command){ case SNMP_MSG_TRAP: case SNMP_MSG_TRAP2: ntop->getTrace()->traceEvent(TRACE_DEBUGGING, __FILE__,__LINE__,"trap type %ld specific type %ld", pdu->trap_type, pdu->specific_type); From d514a27a53f01595c522345e4bfdcf586efab71b Mon Sep 17 00:00:00 2001 From: carloleo Date: Tue, 9 Jul 2024 01:01:31 +0200 Subject: [PATCH 4/9] SNMP.h added method to handle trap SNMP.cpp printed oid and mib in the new method --- include/SNMP.h | 1 + src/SNMP.cpp | 24 +++++++++++++++++++++++- 2 files changed, 24 insertions(+), 1 deletion(-) diff --git a/include/SNMP.h b/include/SNMP.h index 4eec23d3427c..74b8d895a81b 100644 --- a/include/SNMP.h +++ b/include/SNMP.h @@ -115,6 +115,7 @@ class SNMP { int getnext(lua_State *vm, bool skip_first_param); int getnextbulk(lua_State *vm, bool skip_first_param); int set(lua_State *vm, bool skip_first_param); + void handle_trap(struct snmp_pdu*pdu); }; #endif /* _SNMP_H_ */ diff --git a/src/SNMP.cpp b/src/SNMP.cpp index 17aa12c13329..ac99fe82d56d 100644 --- a/src/SNMP.cpp +++ b/src/SNMP.cpp @@ -64,7 +64,7 @@ SNMP::SNMP() { //TODO: check if other fields need to be populated snmp_sess_init(&trap_session->session); trap_session->session.callback = read_snmp_trap; - trap_session->session.callback_magic = (void *) snmpTransport; + trap_session->session.callback_magic = (void *) this; trap_session->session.authenticator = NULL; netsnmp_session* rc = snmp_add(&trap_session->session,snmpTransport, NULL, NULL); if (rc == NULL) { @@ -1149,13 +1149,35 @@ int SNMP::snmp_get_fctn(lua_State *vm, snmp_pdu_primitive pduType, return (snmp_read_response(vm, timeout)); } } + +void SNMP::handle_trap(struct snmp_pdu*pdu){ + netsnmp_variable_list *variable; + char oid[MAX_OID_LEN] = {'\0'}; + tree *mib; + variable = pdu->variables; + while (variable) { + // fill oid with an human readable oid + if(snprint_objid(oid, sizeof(oid), variable->name_loc, variable->name_length)) + printf("oid: %s\n", oid); + //now let's figure out the mib + mib = get_tree(variable->name_loc, variable->name_length, get_tree_head()); + while(mib && mib->next_peer) + mib = mib->next_peer; + + printf("mib %s\n",mib->label); + variable = variable->next_variable; + } + +} //callback printing trap int read_snmp_trap(int operation, struct snmp_session *sp, int reqid, struct snmp_pdu *pdu, void *magic){ + SNMP *s = (SNMP *)magic; switch (pdu->command){ case SNMP_MSG_TRAP: case SNMP_MSG_TRAP2: ntop->getTrace()->traceEvent(TRACE_DEBUGGING, __FILE__,__LINE__,"trap type %ld specific type %ld", pdu->trap_type, pdu->specific_type); + s->handle_trap(pdu); break; default: ntop->getTrace()->traceEvent(TRACE_DEBUGGING,__FILE__,__LINE__,"Invalid operation %d",operation); From 838ed6d2f01392aced9db93513174f5ebad25749 Mon Sep 17 00:00:00 2001 From: carloleo Date: Thu, 11 Jul 2024 22:11:44 +0200 Subject: [PATCH 5/9] SNMP.h defined macro SNMP.cpp refactored code so that collectsTrap starts trap collection added dedicated select for traps removed trap session from vector --- include/SNMP.h | 10 ++++- src/SNMP.cpp | 115 ++++++++++++++++++++++++++++++++++--------------- 2 files changed, 89 insertions(+), 36 deletions(-) diff --git a/include/SNMP.h b/include/SNMP.h index 53fa75ab9925..a352ff724017 100644 --- a/include/SNMP.h +++ b/include/SNMP.h @@ -49,6 +49,10 @@ class SNMPSession { SNMPSession(); ~SNMPSession(); }; + +#define TRAP_PORT "udp:162" +#define APPLICATION "ntopng snmp trap" + #endif int read_snmp_trap(int operation, struct snmp_session *sp, int reqid, @@ -62,6 +66,10 @@ class SNMP { std::vector sessions; /* Variables below are used for the async check */ lua_State *vm; + SNMPSession *trap_session; + netsnmp_transport *snmpTransport; + netsnmp_session* rc; + bool cease_collecting_trap; #else int udp_sock; u_int32_t request_id; @@ -87,7 +95,7 @@ class SNMP { SNMP(); ~SNMP(); - void collectTraps(); + void collectTraps(struct timeval timeout); #ifdef HAVE_LIBSNMP void handle_async_response(struct snmp_pdu *pdu, const char *agent_ip); diff --git a/src/SNMP.cpp b/src/SNMP.cpp index 3c0bc3807840..1e2c3b92fa33 100644 --- a/src/SNMP.cpp +++ b/src/SNMP.cpp @@ -47,35 +47,8 @@ SNMP::SNMP() { if(trace_new_delete) ntop->getTrace()->traceEvent(TRACE_NORMAL, "[new] %s", __FILE__); batch_mode = false; #ifdef HAVE_LIBSNMP - init_snmp("ntopng"); - //trap management - //instantiate necessary resources. Perhaps, this snippet can be placed better. - const char* port = "upd:162"; - const char* application = "ntopng snmp trap"; - //this needs to be called so that the snmp library can initialize the supported transports list - netsnmp_tdomain_init(); - netsnmp_transport *snmpTransport = netsnmp_transport_open_server(application,port); - if(snmpTransport == NULL){ - snmp_perror("netsnmp_transport_open_server "); - return; - } - //trap session - SNMPSession* trap_session = new SNMPSession; - //TODO: check if other fields need to be populated - snmp_sess_init(&trap_session->session); - trap_session->session.callback = read_snmp_trap; - trap_session->session.callback_magic = (void *) this; - trap_session->session.authenticator = NULL; - netsnmp_session* rc = snmp_add(&trap_session->session,snmpTransport, NULL, NULL); - if (rc == NULL) { - snmp_perror("FAILURE: "); - netsnmp_transport_free(snmpTransport); - } - trap_session->session_ptr = snmp_sess_open(&trap_session->session); - //add session - this->sessions.push_back(trap_session); - ntop->getTrace()->traceEvent(TRACE_DEBUGGING,__FILE__,__LINE__,"SNMP TRAP SESSION OK"); - + init_snmp("ntopng"); + printf("********************************************************************************************************"); #endif getbulk_max_num_repetitions = 10; } @@ -84,6 +57,9 @@ SNMP::SNMP() { SNMP::~SNMP() { for (unsigned int i = 0; i < sessions.size(); i++) delete sessions.at(i); + if(trap_session) delete trap_session; + if(snmpTransport) free(snmpTransport); + if(rc) free(rc); } /* ******************************* */ @@ -307,11 +283,10 @@ void SNMP::send_snmpv1v2c_request(char *agent_host, char *community, return; } } else { - //because of adding the snmptrap session inside the constructor - if((sessions.size() - 1) == 0) { + if((sessions.size()) == 0) { goto create_snmp_session; } else { - snmpSession = sessions.at(1); //the first session is going to be the trap session + snmpSession = sessions.at(0); } } @@ -1152,8 +1127,42 @@ int SNMP::snmp_get_fctn(lua_State *vm, snmp_pdu_primitive pduType, /* ******************************************* */ -void SNMP::collectTraps() { - /* TODO */ +void SNMP::collectTraps(struct timeval timeout) { + //this needs to be called so that the snmp library can initialize the supported transports list + netsnmp_tdomain_init(); + ntop->getTrace()->traceEvent(TRACE_INFO, __FILE__,__LINE__,"********************AOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOO\n"); + this->snmpTransport = netsnmp_transport_open_server(APPLICATION,TRAP_PORT); + if(this->snmpTransport == NULL){ + snmp_perror("netsnmp_transport_open_server "); + perror("trap: "); + return; + } + //trap session + this->trap_session = new SNMPSession; + snmp_sess_init(&this->trap_session->session); + this->trap_session->session.callback = read_snmp_trap; + this->trap_session->session.callback_magic = (void *) this; + this->trap_session->session.authenticator = NULL; + this->rc = snmp_add(&trap_session->session,this->snmpTransport, NULL, NULL); + if (this->rc == NULL) { + snmp_perror("FAILURE: "); + netsnmp_transport_free(snmpTransport); + } + //trap_session->session_ptr = snmp_sess_open(&trap_session->session); + int numfds; + fd_set fdset; + struct timeval tvp; + int count, block = 1; + this->cease_collecting_trap = false; + numfds = 0; + FD_ZERO(&fdset); + tvp.tv_sec = timeout.tv_sec, tvp.tv_usec = 0; + snmp_select_info(&numfds,&fdset,&tvp,&block); + count = select(numfds, &fdset, NULL, NULL, &tvp); + printf("count %d\n", count); + if (count > 0) + snmp_read(&fdset); + } void SNMP::handle_trap(struct snmp_pdu*pdu){ @@ -1169,8 +1178,44 @@ void SNMP::handle_trap(struct snmp_pdu*pdu){ mib = get_tree(variable->name_loc, variable->name_length, get_tree_head()); while(mib && mib->next_peer) mib = mib->next_peer; - printf("mib %s\n",mib->label); + switch (variable->type) { + case ASN_INTEGER: + printf("INTEGER: %ld\n", *variable->val.integer); + break; + case ASN_OCTET_STR: + printf("STRING: %.*s\n", (int)variable->val_len, variable->val.string); + break; + case ASN_OBJECT_ID: + printf("OID: "); + for (size_t i = 0; i < variable->val_len / sizeof(oid); i++) { + printf("%s%lu", i ? "." : "", variable->val.objid[i]); + } + printf("\n"); + break; + case ASN_BIT_STR: + printf("BIT STRING: "); + for (size_t i = 0; i < variable->val_len; i++) { + printf("%02x ", variable->val.bitstring[i]); + } + printf("\n"); + break; + case ASN_COUNTER64: + printf("COUNTER64 high: %lu\n", variable->val.counter64->high); + printf("COUNTER64 low: %lu\n", variable->val.counter64->low); + break; +#ifdef NETSNMP_WITH_OPAQUE_SPECIAL_TYPES + case ASN_OPAQUE_FLOAT: + printf("FLOAT: %f\n", *variable->val.floatVal); + break; + case ASN_OPAQUE_DOUBLE: + printf("DOUBLE: %f\n", *variable->val.doubleVal); + break; +#endif + default: + printf("Unknown or unhandled ASN type: %u\n", variable->type); + break; + } variable = variable->next_variable; } From ce11adba6784fa0d94548095a98ba8acdccc5d16 Mon Sep 17 00:00:00 2001 From: carloleo Date: Mon, 15 Jul 2024 22:51:35 +0200 Subject: [PATCH 6/9] SNMP.cpp - added loop to receive trap continuously - added comments and error logs SNMP.h - added method to stop collecting trap - changed type of timeout parameter --- include/SNMP.h | 9 ++++++-- src/SNMP.cpp | 56 ++++++++++++++++++++++++++++++-------------------- 2 files changed, 41 insertions(+), 24 deletions(-) diff --git a/include/SNMP.h b/include/SNMP.h index a352ff724017..0f2e0b33c755 100644 --- a/include/SNMP.h +++ b/include/SNMP.h @@ -66,10 +66,11 @@ class SNMP { std::vector sessions; /* Variables below are used for the async check */ lua_State *vm; + //trap variables SNMPSession *trap_session; netsnmp_transport *snmpTransport; netsnmp_session* rc; - bool cease_collecting_trap; + volatile bool cease_collecting_trap; #else int udp_sock; u_int32_t request_id; @@ -95,7 +96,7 @@ class SNMP { SNMP(); ~SNMP(); - void collectTraps(struct timeval timeout); + #ifdef HAVE_LIBSNMP void handle_async_response(struct snmp_pdu *pdu, const char *agent_ip); @@ -126,6 +127,10 @@ class SNMP { int getnextbulk(lua_State *vm, bool skip_first_param); int set(lua_State *vm, bool skip_first_param); void handle_trap(struct snmp_pdu*pdu); + //call to commence trap collection + void collectTraps(int timeout); + //call to cease trap collection + void cease_trap_collection(); }; #endif /* _SNMP_H_ */ diff --git a/src/SNMP.cpp b/src/SNMP.cpp index 1e2c3b92fa33..058a333863e2 100644 --- a/src/SNMP.cpp +++ b/src/SNMP.cpp @@ -1127,14 +1127,11 @@ int SNMP::snmp_get_fctn(lua_State *vm, snmp_pdu_primitive pduType, /* ******************************************* */ -void SNMP::collectTraps(struct timeval timeout) { - //this needs to be called so that the snmp library can initialize the supported transports list - netsnmp_tdomain_init(); - ntop->getTrace()->traceEvent(TRACE_INFO, __FILE__,__LINE__,"********************AOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOO\n"); +void SNMP::collectTraps(int timeout) { this->snmpTransport = netsnmp_transport_open_server(APPLICATION,TRAP_PORT); if(this->snmpTransport == NULL){ + ntop->getTrace()->traceEvent(TRACE_ERROR,__FILE__,__LINE__,"opening snmp transport failed\n"); snmp_perror("netsnmp_transport_open_server "); - perror("trap: "); return; } //trap session @@ -1145,23 +1142,29 @@ void SNMP::collectTraps(struct timeval timeout) { this->trap_session->session.authenticator = NULL; this->rc = snmp_add(&trap_session->session,this->snmpTransport, NULL, NULL); if (this->rc == NULL) { + ntop->getTrace()->traceEvent(TRACE_ERROR,__FILE__,__LINE__,"adding snmp trap session failed\n"); snmp_perror("FAILURE: "); netsnmp_transport_free(snmpTransport); + + return; } - //trap_session->session_ptr = snmp_sess_open(&trap_session->session); int numfds; fd_set fdset; struct timeval tvp; int count, block = 1; this->cease_collecting_trap = false; numfds = 0; - FD_ZERO(&fdset); - tvp.tv_sec = timeout.tv_sec, tvp.tv_usec = 0; - snmp_select_info(&numfds,&fdset,&tvp,&block); - count = select(numfds, &fdset, NULL, NULL, &tvp); - printf("count %d\n", count); + tvp.tv_sec = timeout; tvp.tv_usec = 0; + while(!this->cease_collecting_trap){ + FD_ZERO(&fdset); + snmp_select_info(&numfds,&fdset,&tvp,&block); + count = select(numfds, &fdset, NULL, NULL, &tvp); + printf("count %d\n", count); if (count > 0) - snmp_read(&fdset); + snmp_read(&fdset); //cause callback execution + if(tvp.tv_sec == 0) + tvp.tv_sec = timeout; + } } @@ -1179,60 +1182,69 @@ void SNMP::handle_trap(struct snmp_pdu*pdu){ while(mib && mib->next_peer) mib = mib->next_peer; printf("mib %s\n",mib->label); + //managed types being in netsnmp_vardata switch (variable->type) { case ASN_INTEGER: - printf("INTEGER: %ld\n", *variable->val.integer); + printf("integer: %ld\n", *variable->val.integer); break; case ASN_OCTET_STR: - printf("STRING: %.*s\n", (int)variable->val_len, variable->val.string); + printf("string: %.*s\n", (int)variable->val_len, variable->val.string); break; case ASN_OBJECT_ID: - printf("OID: "); + printf("oid: "); for (size_t i = 0; i < variable->val_len / sizeof(oid); i++) { printf("%s%lu", i ? "." : "", variable->val.objid[i]); } printf("\n"); break; case ASN_BIT_STR: - printf("BIT STRING: "); + printf("bit string: "); for (size_t i = 0; i < variable->val_len; i++) { printf("%02x ", variable->val.bitstring[i]); } printf("\n"); break; case ASN_COUNTER64: - printf("COUNTER64 high: %lu\n", variable->val.counter64->high); - printf("COUNTER64 low: %lu\n", variable->val.counter64->low); + printf("counter64 high: %lu\n", variable->val.counter64->high); + printf("counter64 low: %lu\n", variable->val.counter64->low); break; #ifdef NETSNMP_WITH_OPAQUE_SPECIAL_TYPES case ASN_OPAQUE_FLOAT: - printf("FLOAT: %f\n", *variable->val.floatVal); + printf("float: %f\n", *variable->val.floatVal); break; case ASN_OPAQUE_DOUBLE: - printf("DOUBLE: %f\n", *variable->val.doubleVal); + printf("double: %f\n", *variable->val.doubleVal); break; #endif default: - printf("Unknown or unhandled ASN type: %u\n", variable->type); + printf("Unmanaged ASN type: %u\n", variable->type); break; } + //go to next variable variable = variable->next_variable; } } + +void SNMP::cease_trap_collection(){ + this->cease_collecting_trap = true; +} //callback printing trap int read_snmp_trap(int operation, struct snmp_session *sp, int reqid, struct snmp_pdu *pdu, void *magic){ SNMP *s = (SNMP *)magic; + int ret = 0; switch (pdu->command){ case SNMP_MSG_TRAP: case SNMP_MSG_TRAP2: ntop->getTrace()->traceEvent(TRACE_DEBUGGING, __FILE__,__LINE__,"trap type %ld specific type %ld", pdu->trap_type, pdu->specific_type); + //call SNMP method to have reference to lua state s->handle_trap(pdu); break; default: ntop->getTrace()->traceEvent(TRACE_DEBUGGING,__FILE__,__LINE__,"Invalid operation %d",operation); + ret = 1; break; } - return 1; + return ret; } \ No newline at end of file From 5dca6a107bf957af3c0606b879fdfd9884001109 Mon Sep 17 00:00:00 2001 From: carloleo Date: Tue, 16 Jul 2024 00:22:38 +0200 Subject: [PATCH 7/9] removed debug ptintf --- src/SNMP.cpp | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/SNMP.cpp b/src/SNMP.cpp index 058a333863e2..e0ce718a4e4a 100644 --- a/src/SNMP.cpp +++ b/src/SNMP.cpp @@ -48,7 +48,6 @@ SNMP::SNMP() { batch_mode = false; #ifdef HAVE_LIBSNMP init_snmp("ntopng"); - printf("********************************************************************************************************"); #endif getbulk_max_num_repetitions = 10; } @@ -283,7 +282,7 @@ void SNMP::send_snmpv1v2c_request(char *agent_host, char *community, return; } } else { - if((sessions.size()) == 0) { + if(sessions.size() == 0) { goto create_snmp_session; } else { snmpSession = sessions.at(0); From 8cf3f453a04417fc0759b64f3298ebf1234cc98a Mon Sep 17 00:00:00 2001 From: carloleo Date: Tue, 16 Jul 2024 22:59:37 +0200 Subject: [PATCH 8/9] SNMP.cpp fixed memory deallocation SNMP.h added space to force new version --- include/SNMP.h | 2 +- src/SNMP.cpp | 14 +++++++++----- 2 files changed, 10 insertions(+), 6 deletions(-) diff --git a/include/SNMP.h b/include/SNMP.h index 0f2e0b33c755..a607040fa709 100644 --- a/include/SNMP.h +++ b/include/SNMP.h @@ -67,7 +67,7 @@ class SNMP { /* Variables below are used for the async check */ lua_State *vm; //trap variables - SNMPSession *trap_session; + SNMPSession *trap_session; netsnmp_transport *snmpTransport; netsnmp_session* rc; volatile bool cease_collecting_trap; diff --git a/src/SNMP.cpp b/src/SNMP.cpp index e0ce718a4e4a..3cbf690c2f44 100644 --- a/src/SNMP.cpp +++ b/src/SNMP.cpp @@ -57,8 +57,8 @@ SNMP::SNMP() { SNMP::~SNMP() { for (unsigned int i = 0; i < sessions.size(); i++) delete sessions.at(i); if(trap_session) delete trap_session; - if(snmpTransport) free(snmpTransport); - if(rc) free(rc); + if(snmpTransport) netsnmp_transport_free(snmpTransport); + if(rc) netsnmp_free(rc); } /* ******************************* */ @@ -1134,7 +1134,11 @@ void SNMP::collectTraps(int timeout) { return; } //trap session - this->trap_session = new SNMPSession; + this->trap_session = new (std::nothrow) SNMPSession; + if(this->trap_session == NULL){ + ntop->getTrace()->traceEvent(TRACE_ERROR,__FILE__,__LINE__,"new trap session failed\n"); + netsnmp_transport_free(this->snmpTransport); + } snmp_sess_init(&this->trap_session->session); this->trap_session->session.callback = read_snmp_trap; this->trap_session->session.callback_magic = (void *) this; @@ -1143,8 +1147,8 @@ void SNMP::collectTraps(int timeout) { if (this->rc == NULL) { ntop->getTrace()->traceEvent(TRACE_ERROR,__FILE__,__LINE__,"adding snmp trap session failed\n"); snmp_perror("FAILURE: "); - netsnmp_transport_free(snmpTransport); - + netsnmp_transport_free(this->snmpTransport); + delete this->trap_session; return; } int numfds; From 6a37cd77c44bd24dcc31b2d911714e6c249461f6 Mon Sep 17 00:00:00 2001 From: carloleo Date: Sun, 21 Jul 2024 19:57:15 +0200 Subject: [PATCH 9/9] fixed preprocessing --- include/SNMP.h | 17 ++-- src/SNMP.cpp | 238 +++++++++++++++++++++++++------------------------ 2 files changed, 129 insertions(+), 126 deletions(-) diff --git a/include/SNMP.h b/include/SNMP.h index a607040fa709..2eb75c5c65f7 100644 --- a/include/SNMP.h +++ b/include/SNMP.h @@ -50,13 +50,13 @@ class SNMPSession { ~SNMPSession(); }; +//constant for trap collection #define TRAP_PORT "udp:162" #define APPLICATION "ntopng snmp trap" - -#endif - +//callback int read_snmp_trap(int operation, struct snmp_session *sp, int reqid, struct snmp_pdu *pdu, void *magic); +#endif class SNMP { private: @@ -113,6 +113,12 @@ class SNMP { char *oid[SNMP_MAX_NUM_OIDS], char value_types[SNMP_MAX_NUM_OIDS], char *values[SNMP_MAX_NUM_OIDS], bool _batch_mode); + // + void handle_trap(struct snmp_pdu*pdu); + //call to commence trap collection + void collectTraps(int timeout); + //call to cease trap collection + void cease_trap_collection(); #endif void send_snmpv1v2c_request(char *agent_host, char *community, snmp_pdu_primitive pduType, u_int version, @@ -126,11 +132,6 @@ class SNMP { int getnext(lua_State *vm, bool skip_first_param); int getnextbulk(lua_State *vm, bool skip_first_param); int set(lua_State *vm, bool skip_first_param); - void handle_trap(struct snmp_pdu*pdu); - //call to commence trap collection - void collectTraps(int timeout); - //call to cease trap collection - void cease_trap_collection(); }; #endif /* _SNMP_H_ */ diff --git a/src/SNMP.cpp b/src/SNMP.cpp index 3cbf690c2f44..3551507dff93 100644 --- a/src/SNMP.cpp +++ b/src/SNMP.cpp @@ -1125,129 +1125,131 @@ int SNMP::snmp_get_fctn(lua_State *vm, snmp_pdu_primitive pduType, } /* ******************************************* */ +#ifdef HAVE_LIBSNMP -void SNMP::collectTraps(int timeout) { - this->snmpTransport = netsnmp_transport_open_server(APPLICATION,TRAP_PORT); - if(this->snmpTransport == NULL){ - ntop->getTrace()->traceEvent(TRACE_ERROR,__FILE__,__LINE__,"opening snmp transport failed\n"); - snmp_perror("netsnmp_transport_open_server "); - return; - } - //trap session - this->trap_session = new (std::nothrow) SNMPSession; - if(this->trap_session == NULL){ - ntop->getTrace()->traceEvent(TRACE_ERROR,__FILE__,__LINE__,"new trap session failed\n"); - netsnmp_transport_free(this->snmpTransport); - } - snmp_sess_init(&this->trap_session->session); - this->trap_session->session.callback = read_snmp_trap; - this->trap_session->session.callback_magic = (void *) this; - this->trap_session->session.authenticator = NULL; - this->rc = snmp_add(&trap_session->session,this->snmpTransport, NULL, NULL); - if (this->rc == NULL) { - ntop->getTrace()->traceEvent(TRACE_ERROR,__FILE__,__LINE__,"adding snmp trap session failed\n"); - snmp_perror("FAILURE: "); - netsnmp_transport_free(this->snmpTransport); - delete this->trap_session; + void SNMP::collectTraps(int timeout) { + this->snmpTransport = netsnmp_transport_open_server(APPLICATION,TRAP_PORT); + if(this->snmpTransport == NULL){ + ntop->getTrace()->traceEvent(TRACE_ERROR,__FILE__,__LINE__,"opening snmp transport failed\n"); + snmp_perror("netsnmp_transport_open_server "); return; + } + //trap session + this->trap_session = new (std::nothrow) SNMPSession; + if(this->trap_session == NULL){ + ntop->getTrace()->traceEvent(TRACE_ERROR,__FILE__,__LINE__,"new trap session failed\n"); + netsnmp_transport_free(this->snmpTransport); + } + snmp_sess_init(&this->trap_session->session); + this->trap_session->session.callback = read_snmp_trap; + this->trap_session->session.callback_magic = (void *) this; + this->trap_session->session.authenticator = NULL; + this->rc = snmp_add(&trap_session->session,this->snmpTransport, NULL, NULL); + if (this->rc == NULL) { + ntop->getTrace()->traceEvent(TRACE_ERROR,__FILE__,__LINE__,"adding snmp trap session failed\n"); + snmp_perror("FAILURE: "); + netsnmp_transport_free(this->snmpTransport); + delete this->trap_session; + return; + } + int numfds; + fd_set fdset; + struct timeval tvp; + int count, block = 1; + this->cease_collecting_trap = false; + numfds = 0; + tvp.tv_sec = timeout; tvp.tv_usec = 0; + while(!this->cease_collecting_trap){ + FD_ZERO(&fdset); + snmp_select_info(&numfds,&fdset,&tvp,&block); + count = select(numfds, &fdset, NULL, NULL, &tvp); + printf("count %d\n", count); + if (count > 0) + snmp_read(&fdset); //cause callback execution + if(tvp.tv_sec == 0) + tvp.tv_sec = timeout; + } + } - int numfds; - fd_set fdset; - struct timeval tvp; - int count, block = 1; - this->cease_collecting_trap = false; - numfds = 0; - tvp.tv_sec = timeout; tvp.tv_usec = 0; - while(!this->cease_collecting_trap){ - FD_ZERO(&fdset); - snmp_select_info(&numfds,&fdset,&tvp,&block); - count = select(numfds, &fdset, NULL, NULL, &tvp); - printf("count %d\n", count); - if (count > 0) - snmp_read(&fdset); //cause callback execution - if(tvp.tv_sec == 0) - tvp.tv_sec = timeout; - } - -} -void SNMP::handle_trap(struct snmp_pdu*pdu){ - netsnmp_variable_list *variable; - char oid[MAX_OID_LEN] = {'\0'}; - tree *mib; - variable = pdu->variables; - while (variable) { - // fill oid with an human readable oid - if(snprint_objid(oid, sizeof(oid), variable->name_loc, variable->name_length)) - printf("oid: %s\n", oid); - //now let's figure out the mib - mib = get_tree(variable->name_loc, variable->name_length, get_tree_head()); - while(mib && mib->next_peer) - mib = mib->next_peer; - printf("mib %s\n",mib->label); - //managed types being in netsnmp_vardata - switch (variable->type) { - case ASN_INTEGER: - printf("integer: %ld\n", *variable->val.integer); - break; - case ASN_OCTET_STR: - printf("string: %.*s\n", (int)variable->val_len, variable->val.string); - break; - case ASN_OBJECT_ID: - printf("oid: "); - for (size_t i = 0; i < variable->val_len / sizeof(oid); i++) { - printf("%s%lu", i ? "." : "", variable->val.objid[i]); - } - printf("\n"); - break; - case ASN_BIT_STR: - printf("bit string: "); - for (size_t i = 0; i < variable->val_len; i++) { - printf("%02x ", variable->val.bitstring[i]); - } - printf("\n"); - break; - case ASN_COUNTER64: - printf("counter64 high: %lu\n", variable->val.counter64->high); - printf("counter64 low: %lu\n", variable->val.counter64->low); - break; -#ifdef NETSNMP_WITH_OPAQUE_SPECIAL_TYPES - case ASN_OPAQUE_FLOAT: - printf("float: %f\n", *variable->val.floatVal); - break; - case ASN_OPAQUE_DOUBLE: - printf("double: %f\n", *variable->val.doubleVal); - break; -#endif - default: - printf("Unmanaged ASN type: %u\n", variable->type); - break; + void SNMP::handle_trap(struct snmp_pdu*pdu){ + netsnmp_variable_list *variable; + char oid[MAX_OID_LEN] = {'\0'}; + tree *mib; + variable = pdu->variables; + while (variable) { + // fill oid with an human readable oid + if(snprint_objid(oid, sizeof(oid), variable->name_loc, variable->name_length)) + printf("oid: %s\n", oid); + //now let's figure out the mib + mib = get_tree(variable->name_loc, variable->name_length, get_tree_head()); + while(mib && mib->next_peer) + mib = mib->next_peer; + printf("mib %s\n",mib->label); + //managed types being in netsnmp_vardata + switch (variable->type) { + case ASN_INTEGER: + printf("integer: %ld\n", *variable->val.integer); + break; + case ASN_OCTET_STR: + printf("string: %.*s\n", (int)variable->val_len, variable->val.string); + break; + case ASN_OBJECT_ID: + printf("oid: "); + for (size_t i = 0; i < variable->val_len / sizeof(oid); i++) { + printf("%s%lu", i ? "." : "", variable->val.objid[i]); + } + printf("\n"); + break; + case ASN_BIT_STR: + printf("bit string: "); + for (size_t i = 0; i < variable->val_len; i++) { + printf("%02x ", variable->val.bitstring[i]); + } + printf("\n"); + break; + case ASN_COUNTER64: + printf("counter64 high: %lu\n", variable->val.counter64->high); + printf("counter64 low: %lu\n", variable->val.counter64->low); + break; + #ifdef NETSNMP_WITH_OPAQUE_SPECIAL_TYPES + case ASN_OPAQUE_FLOAT: + printf("float: %f\n", *variable->val.floatVal); + break; + case ASN_OPAQUE_DOUBLE: + printf("double: %f\n", *variable->val.doubleVal); + break; + #endif + default: + printf("Unmanaged ASN type: %u\n", variable->type); + break; + } + //go to next variable + variable = variable->next_variable; } - //go to next variable - variable = variable->next_variable; + } - -} -void SNMP::cease_trap_collection(){ - this->cease_collecting_trap = true; -} -//callback printing trap -int read_snmp_trap(int operation, struct snmp_session *sp, int reqid, - struct snmp_pdu *pdu, void *magic){ - SNMP *s = (SNMP *)magic; - int ret = 0; - switch (pdu->command){ - case SNMP_MSG_TRAP: - case SNMP_MSG_TRAP2: - ntop->getTrace()->traceEvent(TRACE_DEBUGGING, __FILE__,__LINE__,"trap type %ld specific type %ld", pdu->trap_type, pdu->specific_type); - //call SNMP method to have reference to lua state - s->handle_trap(pdu); - break; - default: - ntop->getTrace()->traceEvent(TRACE_DEBUGGING,__FILE__,__LINE__,"Invalid operation %d",operation); - ret = 1; - break; + void SNMP::cease_trap_collection(){ + this->cease_collecting_trap = true; + } + //callback printing trap + int read_snmp_trap(int operation, struct snmp_session *sp, int reqid, + struct snmp_pdu *pdu, void *magic){ + SNMP *s = (SNMP *)magic; + int ret = 0; + switch (pdu->command){ + case SNMP_MSG_TRAP: + case SNMP_MSG_TRAP2: + ntop->getTrace()->traceEvent(TRACE_DEBUGGING, __FILE__,__LINE__,"trap type %ld specific type %ld", pdu->trap_type, pdu->specific_type); + //call SNMP method to have reference to lua state + s->handle_trap(pdu); + break; + default: + ntop->getTrace()->traceEvent(TRACE_DEBUGGING,__FILE__,__LINE__,"Invalid operation %d",operation); + ret = 1; + break; + } + return ret; } - return ret; -} \ No newline at end of file +#endif \ No newline at end of file