From 2e5b097568da6d17d4fde2a3f3d291f940458ee0 Mon Sep 17 00:00:00 2001 From: jwillmartin Date: Thu, 15 Jun 2023 14:00:54 -0400 Subject: [PATCH] renamed: src/v2i-hub/ERVCloudForwardingPlugin/src/SNMPClient.cpp -> src/tmx/TmxUtils/src/SNMPClient.cpp renamed: src/v2i-hub/ERVCloudForwardingPlugin/src/SNMPClient.h -> src/tmx/TmxUtils/src/SNMPClient.h renamed: src/v2i-hub/ERVCloudForwardingPlugin/src/SNMPClientException.h -> src/tmx/TmxUtils/src/SNMPClientException.h modified: src/v2i-hub/ERVCloudForwardingPlugin/CMakeLists.txt modified: src/v2i-hub/ERVCloudForwardingPlugin/manifest.json modified: src/v2i-hub/ERVCloudForwardingPlugin/src/ERVCloudForwardingPlugin.cpp modified: src/v2i-hub/ERVCloudForwardingPlugin/src/ERVCloudForwardingPlugin.h modified: src/v2i-hub/ERVCloudForwardingPlugin/test/SNMPClientTest.cpp modified: src/v2i-hub/ImmediateForwardPlugin/CMakeLists.txt modified: src/v2i-hub/ImmediateForwardPlugin/manifest.json modified: src/v2i-hub/ImmediateForwardPlugin/src/ImmediateForwardPlugin.cpp modified: src/v2i-hub/ImmediateForwardPlugin/src/ImmediateForwardPlugin.h --- .../TmxUtils}/src/SNMPClient.cpp | 127 ++++++++++++------ .../TmxUtils}/src/SNMPClient.h | 23 +++- .../TmxUtils}/src/SNMPClientException.h | 7 +- .../ERVCloudForwardingPlugin/CMakeLists.txt | 2 +- .../ERVCloudForwardingPlugin/manifest.json | 13 +- .../src/ERVCloudForwardingPlugin.cpp | 7 +- .../src/ERVCloudForwardingPlugin.h | 3 +- .../test/SNMPClientTest.cpp | 14 +- .../ImmediateForwardPlugin/CMakeLists.txt | 7 +- .../ImmediateForwardPlugin/manifest.json | 15 ++- .../src/ImmediateForwardPlugin.cpp | 80 ++++++----- .../src/ImmediateForwardPlugin.h | 14 +- 12 files changed, 198 insertions(+), 114 deletions(-) rename src/{v2i-hub/ERVCloudForwardingPlugin => tmx/TmxUtils}/src/SNMPClient.cpp (50%) rename src/{v2i-hub/ERVCloudForwardingPlugin => tmx/TmxUtils}/src/SNMPClient.h (67%) rename src/{v2i-hub/ERVCloudForwardingPlugin => tmx/TmxUtils}/src/SNMPClientException.h (81%) diff --git a/src/v2i-hub/ERVCloudForwardingPlugin/src/SNMPClient.cpp b/src/tmx/TmxUtils/src/SNMPClient.cpp similarity index 50% rename from src/v2i-hub/ERVCloudForwardingPlugin/src/SNMPClient.cpp rename to src/tmx/TmxUtils/src/SNMPClient.cpp index f66962851..a185a06c8 100644 --- a/src/v2i-hub/ERVCloudForwardingPlugin/src/SNMPClient.cpp +++ b/src/tmx/TmxUtils/src/SNMPClient.cpp @@ -1,6 +1,9 @@ #include "SNMPClient.h" -SNMPClient::SNMPClient(const std::string &rsuIP, uint16_t snmp_port, const std::string &securityUser, const std::string &authPassPhrase) +namespace tmx { +namespace utils { + +SNMPClient::SNMPClient(const std::string &rsuIP, uint16_t snmp_port, const std::string &snmp_user, const std::string &securityLevel, const std::string &authPassPhrase) : _snmp_port(snmp_port) , _rsuIP(rsuIP) { @@ -10,15 +13,14 @@ SNMPClient::SNMPClient(const std::string &rsuIP, uint16_t snmp_port, const std:: snmp_sess_init(&session); session.peername = ip_port; session.version = SNMP_VERSION_3; - session.securityName = (char *)securityUser.c_str(); - session.securityNameLen = securityUser.length(); - if (securityUser == "authPrivUser") { + session.securityName = (char *)snmp_user.c_str(); + session.securityNameLen = snmp_user.length(); + if (securityLevel == "authPriv") { session.securityLevel = SNMP_SEC_LEVEL_AUTHPRIV; } - else if (securityUser == "authOnlyUser") { + else if (securityLevel == "authNoPriv") { session.securityLevel = SNMP_SEC_LEVEL_AUTHNOPRIV; } - else session.securityLevel = SNMP_SEC_LEVEL_NOAUTH; session.securityAuthProto = snmp_duplicate_objid(usmHMACSHA1AuthProtocol, USM_AUTH_PROTO_SHA_LEN); session.securityAuthProtoLen = USM_AUTH_PROTO_SHA_LEN; @@ -47,7 +49,6 @@ SNMPClient::SNMPClient(const std::string &rsuIP, uint16_t snmp_port, const std:: std::string SNMPClient::SNMPGet(const std::string &req_oid) { std::string result = ""; - netsnmp_pdu *response; auto pdu = snmp_pdu_create(SNMP_MSG_GET); if (!snmp_parse_oid(req_oid.c_str(), anOID, &anOID_len)) { @@ -88,48 +89,92 @@ std::string SNMPClient::SNMPGet(const std::string &req_oid) return result; } -std::string SNMPClient::SNMPSet(const std::string &req_oid) +bool SNMPClient::SNMPSet(const std::string &oid, int32_t value) { - std::string result = ""; - netsnmp_pdu *response; - auto pdu = snmp_pdu_create(SNMP_MSG_GET); - if (!snmp_parse_oid(req_oid.c_str(), anOID, &anOID_len)) - { - snmp_perror(req_oid.c_str()); - std::string errMsg = "OID could not be created from input:" + req_oid; - throw SNMPClientException(errMsg); - SOCK_CLEANUP; + return SNMPClient::SNMPSet(oid, ASN_INTEGER, (const void *)&value, sizeof(value)); +} + +bool SNMPClient::SNMPSet(const std::string &oid, u_char type, const void *value, size_t len) +{ + bool rc = true; + static int quiet = 0; + int arg; + int count; + int current_name = 0; + int current_type = 0; + int current_value = 0; + char *names[SNMP_MAX_CMDLINE_OIDS]; + char types[SNMP_MAX_CMDLINE_OIDS]; + char *values[SNMP_MAX_CMDLINE_OIDS]; + int status; + int failures = 0; + int exitval = 0; + auto pdu = snmp_pdu_create(SNMP_MSG_SET); + + // if (!snmp_parse_oid(oid.c_str(), anOID, &anOID_len)) + // { + // snmp_perror(oid.c_str()); + // std::string errMsg = "OID could not be created from input:" + oid; + // throw SNMPClientException(errMsg); + // SOCK_CLEANUP; + // } + for (count = 0; count < current_name; count++) { + if (snmp_parse_oid(names[count], anOID, &anOID_len) == NULL) { + snmp_perror(names[count]); + failures++; + } else + if (snmp_add_var + (pdu, anOID, anOID_len, types[count], values[count])) { + snmp_perror(names[count]); + failures++; + } } - snmp_add_null_var(pdu, anOID, anOID_len); - auto status = snmp_synch_response(ss, pdu, &response); - if (!response) - { - throw SNMPClientException("No response for SNMP Get request!"); + + if (failures) { + snmp_close(ss); + SOCK_CLEANUP; + exit(1); } - else if (status == STAT_SUCCESS && response->errstat == SNMP_ERR_NOERROR) - { - // SUCCESS: Return the response as result - for (auto vars = response->variables; vars; vars = vars->next_variable) - { - if (vars->type == ASN_OCTET_STR) - { - result = reinterpret_cast(vars->val.string); + + // snmp_add_null_var(pdu, anOID, anOID_len); + // snmp_pdu_add_variable(pdu, anOID, anOID_len, type, value, len); + + auto status = snmp_synch_response(ss, pdu, &response); + if (status == STAT_SUCCESS) { + if (response->errstat == SNMP_ERR_NOERROR) { + if (!quiet) { + for (vars = response->variables; vars; + vars = vars->next_variable) + print_variable(vars->name, vars->name_length, vars); } - else - { - throw SNMPClientException("Received respones type is not a string"); + } else { + fprintf(stderr, "Error in packet.\nReason: %s\n", + snmp_errstring(response->errstat)); + if (response->errindex != 0) { + fprintf(stderr, "Failed object: "); + for (count = 1, vars = response->variables; + vars && (count != response->errindex); + vars = vars->next_variable, count++); + if (vars) + fprint_objid(stderr, vars->name, vars->name_length); + fprintf(stderr, "\n"); } + exitval = 2; } + } else if (status == STAT_TIMEOUT) { + fprintf(stderr, "Timeout: No Response from %s\n", + session.peername); + exitval = 1; + } else { /* status == STAT_ERROR */ + snmp_sess_perror("snmpset", ss); + exitval = 1; } - else - { - // FAILURE: Print what went wrong! - std::string errMsg = snmp_errstring(response->errstat); - throw SNMPClientException("Error in packet. Reason:" + errMsg); - } + if (response) snmp_free_pdu(response); - return result; + snmp_close(ss); + SOCK_CLEANUP; + return exitval; } int SNMPClient::GetPort() const @@ -147,3 +192,5 @@ SNMPClient::~SNMPClient() fprintf(stdout, "Closing snmp session\n"); snmp_close(ss); } + +}} // namespace tmx::utils diff --git a/src/v2i-hub/ERVCloudForwardingPlugin/src/SNMPClient.h b/src/tmx/TmxUtils/src/SNMPClient.h similarity index 67% rename from src/v2i-hub/ERVCloudForwardingPlugin/src/SNMPClient.h rename to src/tmx/TmxUtils/src/SNMPClient.h index 3e2622468..64dd0c617 100644 --- a/src/v2i-hub/ERVCloudForwardingPlugin/src/SNMPClient.h +++ b/src/tmx/TmxUtils/src/SNMPClient.h @@ -10,11 +10,16 @@ #include #include + +namespace tmx::utils { + class SNMPClient { private: netsnmp_session session; netsnmp_session *ss; + netsnmp_pdu *pdu, *response = NULL; + netsnmp_variable_list *vars; oid anOID[MAX_OID_LEN]; size_t anOID_len = MAX_OID_LEN; int _snmp_port; @@ -26,7 +31,7 @@ class SNMPClient * @param ip RSU IP * @param port SNMP port */ - SNMPClient(const std::string &rsuIP, uint16_t snmp_port, const std::string &securityUser, const std::string &authPassPhrase); + SNMPClient(const std::string &rsuIP, uint16_t snmp_port, const std::string &snmp_user, const std::string &securityLevel, const std::string &authPassPhrase); /** * @brief Send SNMP v3 Get request to an RSU to retrieve data * @param oid OID (Object Identifier) uniquely identify managed objects in a MIB database. Concept refers to: https://en.wikipedia.org/wiki/Management_information_base @@ -34,21 +39,25 @@ class SNMPClient */ std::string SNMPGet(const std::string &oid); /** - * @brief Send SNMP v3 Get request to an RSU to retrieve data - * @param oid OID (Object Identifier) uniquely identify managed objects in a MIB database. Concept refers to: https://en.wikipedia.org/wiki/Management_information_base + * @brief Send SNMP v3 Set request to an RSU to write data + * @param oid OID (Object Identifier) uniquely identify managed objects in a MIB database. * @return std::string identified by the oid. If SNMP response is not string, exit with failure. */ - std::string SNMPSet(const std::string &oid); + bool SNMPSet(const std::string &oid, int32_t value); + bool SNMPSet(const std::string &oid, u_char type, const void *value, size_t len); /** * @brief Retrieve the port used by this SNMP client as an integer. * @return The port as expected in a host integer. */ virtual int GetPort() const; /** - * @brief Retrieve a copy of the rsu ipv4 address. - * @return A string with a copy of the constructor input address. + * @brief Retrieve a copy of the RSU IPv4 address. + * @return std::string with a copy of the constructor input address. */ virtual std::string GetAddress() const; ~SNMPClient(); }; -#endif + +} // namespace tmx::utils + +#endif /* SNMPCLIENT_H_ */ diff --git a/src/v2i-hub/ERVCloudForwardingPlugin/src/SNMPClientException.h b/src/tmx/TmxUtils/src/SNMPClientException.h similarity index 81% rename from src/v2i-hub/ERVCloudForwardingPlugin/src/SNMPClientException.h rename to src/tmx/TmxUtils/src/SNMPClientException.h index f1077e98d..c2effd0b7 100644 --- a/src/v2i-hub/ERVCloudForwardingPlugin/src/SNMPClientException.h +++ b/src/tmx/TmxUtils/src/SNMPClientException.h @@ -2,6 +2,8 @@ #define SNMPCLIENTEXCEPTION_H_ #include +namespace tmx::utils { + class SNMPClientException : public std::exception { private: @@ -15,4 +17,7 @@ class SNMPClientException : public std::exception } ~SNMPClientException() override = default; }; -#endif + +} // namespace tmx::utils + +#endif /* SNMPCLIENTEXCEPTION_H_ */ diff --git a/src/v2i-hub/ERVCloudForwardingPlugin/CMakeLists.txt b/src/v2i-hub/ERVCloudForwardingPlugin/CMakeLists.txt index aa1cca06c..4a74d1b08 100644 --- a/src/v2i-hub/ERVCloudForwardingPlugin/CMakeLists.txt +++ b/src/v2i-hub/ERVCloudForwardingPlugin/CMakeLists.txt @@ -29,7 +29,7 @@ link_directories(${CMAKE_PREFIX_PATH}/lib) # ############ enable_testing() include_directories(${PROJECT_SOURCE_DIR}/src) -add_library(${PROJECT_NAME}_lib src/ERVCloudForwardingWorker.cpp src/SNMPClient.cpp) +add_library(${PROJECT_NAME}_lib src/ERVCloudForwardingWorker.cpp) # src/SNMPClient.cpp) target_link_libraries(${PROJECT_NAME}_lib PUBLIC NemaTode ${TMXAPI_LIBRARIES} ${ASN_J2735_LIBRARIES} tmxutils ${NETSNMPAGENT} ${NETSNMPMIBS} ${NETSNMP} ${NETSNMP_LIBRARIES}) set(BINARY ${PROJECT_NAME}_test) diff --git a/src/v2i-hub/ERVCloudForwardingPlugin/manifest.json b/src/v2i-hub/ERVCloudForwardingPlugin/manifest.json index 16dc0bdf5..5f5de5074 100644 --- a/src/v2i-hub/ERVCloudForwardingPlugin/manifest.json +++ b/src/v2i-hub/ERVCloudForwardingPlugin/manifest.json @@ -44,10 +44,15 @@ "description":"SNMP v3 authentication passphrase" }, { - "key":"SecurityUser", - "default":"authOnlyUser", - "description":"SNMP Security Name" - }, + "key":"SNMPUser", + "default":"dummyUser", + "description":"SNMP v3 user." + }, + { + "key":"SecurityLevel", + "default":"authNoPriv", + "description":"SNMP Access Control. Usage: noAuthNoPriv, authNoPriv, or authPriv." + }, { "key":"GPSOID", "default":"1.0.15628.4.1.8.5.0", diff --git a/src/v2i-hub/ERVCloudForwardingPlugin/src/ERVCloudForwardingPlugin.cpp b/src/v2i-hub/ERVCloudForwardingPlugin/src/ERVCloudForwardingPlugin.cpp index 3bff8b921..c69b9e5e4 100644 --- a/src/v2i-hub/ERVCloudForwardingPlugin/src/ERVCloudForwardingPlugin.cpp +++ b/src/v2i-hub/ERVCloudForwardingPlugin/src/ERVCloudForwardingPlugin.cpp @@ -48,8 +48,8 @@ namespace ERVCloudForwardingPlugin PLOG(logINFO) << "Attempting to register RSU " << attempt << " times." << endl; try { - PLOG(logINFO) << "Create SNMP Client to connect to RSU. RSU IP:" << _rsuIp << ",\tRSU Port:" << _snmpPort << ",\tSecurity Name:" << _securityUser << ",\tAuthentication Passphrase: " << _authPassPhrase << endl; - auto snmpClient = std::make_shared(_rsuIp, _snmpPort, _securityUser, _authPassPhrase); + PLOG(logINFO) << "Create SNMP Client to connect to RSU. RSU IP:" << _rsuIp << ",\tRSU Port:" << _snmpPort << ",\tSecurity Level:" << _securityLevel << "\tSNMP User: " << _snmpUser << ",\tAuthentication Passphrase: " << _authPassPhrase << endl; + auto snmpClient = std::make_shared(_rsuIp, _snmpPort, _snmpUser, _securityLevel, _authPassPhrase); auto gps_sentence = snmpClient->SNMPGet(_GPSOID); auto gps_map = ERVCloudForwardingWorker::ParseGPS(gps_sentence); long latitude = 0; @@ -106,7 +106,8 @@ namespace ERVCloudForwardingPlugin GetConfigValue("WebServicePort", _webPort); GetConfigValue("RSUIp", _rsuIp); GetConfigValue("SNMPPort", _snmpPort); - GetConfigValue("SecurityUser", _securityUser); + GetConfigValue("SecurityLevel", _securityLevel); + GetConfigValue("SNMPUser", _snmpUser); GetConfigValue("AuthPassPhrase", _authPassPhrase); GetConfigValue("GPSOID", _GPSOID); GetConfigValue("RSUName", _rsuName); diff --git a/src/v2i-hub/ERVCloudForwardingPlugin/src/ERVCloudForwardingPlugin.h b/src/v2i-hub/ERVCloudForwardingPlugin/src/ERVCloudForwardingPlugin.h index 730c58aaa..cf3eaa7c7 100644 --- a/src/v2i-hub/ERVCloudForwardingPlugin/src/ERVCloudForwardingPlugin.h +++ b/src/v2i-hub/ERVCloudForwardingPlugin/src/ERVCloudForwardingPlugin.h @@ -55,7 +55,8 @@ namespace ERVCloudForwardingPlugin uint32_t _max_rsu_connection_attempt = 30; // Waiting to start the web service uint32_t _max_web_service_waiting = 10; // unit of second - string _securityUser; + string _snmpUser; + string _securityLevel; string _authPassPhrase; string _GPSOID; const string _CLOUDURL = "http://127.0.0.1:33333"; diff --git a/src/v2i-hub/ERVCloudForwardingPlugin/test/SNMPClientTest.cpp b/src/v2i-hub/ERVCloudForwardingPlugin/test/SNMPClientTest.cpp index 26656bf2d..71de826dc 100644 --- a/src/v2i-hub/ERVCloudForwardingPlugin/test/SNMPClientTest.cpp +++ b/src/v2i-hub/ERVCloudForwardingPlugin/test/SNMPClientTest.cpp @@ -1,6 +1,8 @@ #include #include "SNMPClient.h" +using namespace tmx::utils; + namespace unit_test { class SNMPClientTest : public ::testing::Test @@ -11,20 +13,22 @@ namespace unit_test { const std::string rsu_ip = "127.0.0.1"; uint16_t snmp_port = 161; - std::string securityUser = "dummy"; + std::string snmp_user = "dummy"; + std::string securityLevel = "authPriv"; std::string authPassPhrase = "dummy"; // Error: passphrase chosen is below the length requirements of the USM (min=8). - ASSERT_ANY_THROW(SNMPClient(rsu_ip, snmp_port, securityUser, authPassPhrase)); + ASSERT_ANY_THROW(SNMPClient(rsu_ip, snmp_port, snmp_user, securityLevel, authPassPhrase)); authPassPhrase = "dummydummy"; - ASSERT_NO_THROW(SNMPClient(rsu_ip, snmp_port, securityUser, authPassPhrase)); + ASSERT_NO_THROW(SNMPClient(rsu_ip, snmp_port, snmp_user, securityLevel, authPassPhrase)); } TEST_F(SNMPClientTest, SNMPGet) { const std::string rsu_ip = "127.0.0.1"; uint16_t snmp_port = 161; - std::string securityUser = "dummy"; + std::string snmp_user = "dummy"; + std::string securityLevel = "authPriv"; std::string authPassPhrase = "dummydummy"; - auto snmpClient = SNMPClient(rsu_ip, snmp_port, securityUser, authPassPhrase); + auto snmpClient = SNMPClient(rsu_ip, snmp_port, snmp_user, securityLevel, authPassPhrase); ASSERT_THROW(snmpClient.SNMPGet("1.0.15628.4.1.8.5.0"), SNMPClientException); } } \ No newline at end of file diff --git a/src/v2i-hub/ImmediateForwardPlugin/CMakeLists.txt b/src/v2i-hub/ImmediateForwardPlugin/CMakeLists.txt index 3b6ca7277..3b3e0294d 100644 --- a/src/v2i-hub/ImmediateForwardPlugin/CMakeLists.txt +++ b/src/v2i-hub/ImmediateForwardPlugin/CMakeLists.txt @@ -4,6 +4,9 @@ SET (TMX_PLUGIN_NAME "Immediate Forward") FIND_PACKAGE (XercesC REQUIRED) find_package(CURL REQUIRED) +find_library(NETSNMPAGENT "netsnmpagent") +find_library(NETSNMPMIBS "netsnmpmibs") +find_library(NETSNMP "netsnmp") #SET(CMAKE_BOTAN_SOURCE_DIR {CMAKE_SOURCE_DIR}/../../../ext/botan) #include_directories(${CMAKE_BOTAN_SOURCE_DIR}/build/include) @@ -15,5 +18,5 @@ BuildTmxPlugin () -TARGET_INCLUDE_DIRECTORIES ( ${PROJECT_NAME} PUBLIC ${XercesC_INCLUDE_DIRS} ${CURL_INCLUDE_DIRS}) -TARGET_LINK_LIBRARIES ( ${PROJECT_NAME} tmxutils ${XercesC_LIBRARY} ) #libbotan-2 ) +TARGET_INCLUDE_DIRECTORIES ( ${PROJECT_NAME} PUBLIC ${XercesC_INCLUDE_DIRS} ${CURL_INCLUDE_DIRS} ${NETSNMP_INCLUDE_DIRS}) +TARGET_LINK_LIBRARIES ( ${PROJECT_NAME} tmxutils ${XercesC_LIBRARY} ${NETSNMPAGENT} ${NETSNMPMIBS} ${NETSNMP} ${NETSNMP_LIBRARIES}) #libbotan-2 ) diff --git a/src/v2i-hub/ImmediateForwardPlugin/manifest.json b/src/v2i-hub/ImmediateForwardPlugin/manifest.json index 61d93cc30..9acae12ff 100644 --- a/src/v2i-hub/ImmediateForwardPlugin/manifest.json +++ b/src/v2i-hub/ImmediateForwardPlugin/manifest.json @@ -10,7 +10,7 @@ { "key":"LogLevel", "default":"INFO", - "description":"The log level for this plugin" + "description":"The log level for this plugin." }, { "key": "Messages_Destination_1", @@ -75,12 +75,17 @@ { "key":"AuthPassPhrase", "default":"dummy", - "description":"SNMP v3 authentication passphrase" + "description":"SNMP v3 authentication passphrase." }, { - "key":"SecurityUser", - "default":"authPrivUser", - "description":"SNMP Access Control" + "key":"SNMPUser", + "default":"dummyUser", + "description":"SNMP v3 user." + }, + { + "key":"SecurityLevel", + "default":"authNoPriv", + "description":"SNMP Access Control. Usage: noAuthNoPriv, authNoPriv, or authPriv." } ] } \ No newline at end of file diff --git a/src/v2i-hub/ImmediateForwardPlugin/src/ImmediateForwardPlugin.cpp b/src/v2i-hub/ImmediateForwardPlugin/src/ImmediateForwardPlugin.cpp index 8ec6bc2bf..ba1381538 100644 --- a/src/v2i-hub/ImmediateForwardPlugin/src/ImmediateForwardPlugin.cpp +++ b/src/v2i-hub/ImmediateForwardPlugin/src/ImmediateForwardPlugin.cpp @@ -17,13 +17,11 @@ #include #include #include -#include "UdpClient.h" using namespace boost::algorithm; using namespace boost::property_tree; using namespace std; using namespace tmx; -using namespace tmx::utils; namespace ImmediateForward { @@ -128,11 +126,6 @@ void ImmediateForwardPlugin::UpdateConfigSettings() SetStatus(Key_SkippedNoMessageRoute, _skippedNoMessageRoute); SetStatus(Key_SkippedInvalidUdpClient, _skippedInvalidUdpClient); SetStatus(Key_SkippedSignError, _skippedSignErrorResponse); - - GetConfigValue("EnableSNMP", snmpState, &_mutexUdpClient); - GetConfigValue("SecurityUser", _securityUser); - GetConfigValue("AuthPassPhrase", _authPassPhrase); - } for (uint i = 0; i < _udpClientList.size(); i++) { @@ -168,11 +161,15 @@ bool ImmediateForwardPlugin::UpdateUdpClientFromConfigSettings(uint clientIndex) try { string destinations; - GetConfigValue(udpPortSetting, destinations); - string messages; + GetConfigValue(udpPortSetting, destinations); GetConfigValue(messagesSetting, messages); + GetConfigValue("EnableSNMP", snmpState); + GetConfigValue("SecurityLevel", _securityLevel); + GetConfigValue("SNMPUser", _snmpUser); + GetConfigValue("AuthPassPhrase", _authPassPhrase); + // Take the lock while shared data is accessed. // A lock_guard will unlock when it goes out of scope (even if an exception occurs). lock_guard lock(_mutexUdpClient); @@ -200,11 +197,10 @@ bool ImmediateForwardPlugin::UpdateUdpClientFromConfigSettings(uint clientIndex) continue; if (snmpState == 1) { - string _rsuIp = addr[0]; - string _snmpPort = addr[1]; - PLOG(logINFO) << "Create SNMP Client to connect to RSU. RSU IP:" << _rsuIp << ",\tRSU Port:" << _snmpPort << - ",\tSecurity Name:" << _securityUser << ",\tAuthentication Passphrase: " << _authPassPhrase << endl; - _snmpClient = std::make_shared(_rsuIp, _snmpPort, _securityUser, _authPassPhrase); + _rsuIp = addr[0]; + _snmpPort = stoul(addr[1]); + PLOG(logINFO) << "Create SNMP Client to connect to RSU. RSU IP: " << _rsuIp << ",\tRSU Port: " << _snmpPort << + "\tSNMP User: " << _snmpUser << ",\tSecurity Level: " << _securityLevel << ",\tAuthentication Passphrase: " << _authPassPhrase << endl; } else { @@ -374,8 +370,7 @@ void ImmediateForwardPlugin::SendMessageToRadio(IvpMessage *msg) pclose(pipe); SetStatus(Key_SkippedSignError, ++_skippedSignErrorResponse); PLOG(logERROR) << "Error parsing Messages: " << ex.what(); - return; -; + return; } PLOG(logDEBUG1) << "SCMS Contain response = " << result << std::endl; cJSON *root = cJSON_Parse(result.c_str()); @@ -401,34 +396,47 @@ void ImmediateForwardPlugin::SendMessageToRadio(IvpMessage *msg) } // @SONAR_START@ - os << "Version=0.7" << "\n"; - os << "Type=" << _messageConfigMap[configIndex].SendType << "\n" << "PSID=" << _messageConfigMap[configIndex].Psid << "\n"; - if (_messageConfigMap[configIndex].Channel.empty()) - os << "Priority=7" << "\n" << "TxMode=CONT" << "\n" << "TxChannel=" << msg->dsrcMetadata->channel << "\n"; - else - os << "Priority=7" << "\n" << "TxMode=CONT" << "\n" << "TxChannel=" << _messageConfigMap[configIndex].Channel << "\n"; - os << "TxInterval=0" << "\n" << "DeliveryStart=\n" << "DeliveryStop=\n"; - os << "Signature="<< (signState == 1 ? "True" : "False") << "\n" << "Encryption=False\n"; - os << "Payload=" << payloadbyte << "\n"; - string message = os.str(); - - - - - // Send the message using the configured SNMP client + // Send the message using the configured SNMP client if (snmpState == 1) { - PLOG(logDEBUG2) << _logPrefix << "Sending - TmxType: " << _messageConfigMap[configIndex].TmxType << ", SendType: " << _messageConfigMap[configIndex].SendType - << ", PSID: " << _messageConfigMap[configIndex].Psid << ", Client: " << _messageConfigMap[configIndex].ClientIndex - << ", Channel: " << (_messageConfigMap[configIndex].Channel.empty() ? ::to_string( msg->dsrcMetadata->channel) : _messageConfigMap[configIndex].Channel) - << ", Port: " << _snmpClient->GetPort(); - auto gps_sentence = _snmpClient->SNMPSet(message); + auto _snmpClient = std::make_shared(_rsuIp, _snmpPort, _snmpUser, _securityLevel, _authPassPhrase); + + // os << "1.3.6.1.4.1.1206.4.2.18.4.2.1.2" << " x " << _messageConfigMap[configIndex].Psid; + // if (_messageConfigMap[configIndex].Channel.empty()) + // os << " 1.3.6.1.4.1.1206.4.2.18.4.2.1.3" << " i " << msg->dsrcMetadata->channel; + // else os << " 1.3.6.1.4.1.1206.4.2.18.4.2.1.3" << " i " << _messageConfigMap[configIndex].Channel; + // os << " 1.3.6.1.4.1.1206.4.2.18.4.2.1.4" << " i 1" << " 1.3.6.1.4.1.1206.4.2.18.4.2.1.5" << " i 4" << " 1.3.6.1.4.1.1206.4.2.18.4.2.1.6" << " i 7"; + // if (signState == 1) + // os << "1.3.6.1.4.1.1206.4.2.18.4.2.1.7" << " b 1100"; + // else os << "1.3.6.1.4.1.1206.4.2.18.4.2.1.7" << " b 0000"; + // os << " 1.3.6.1.4.1.1206.4.2.18.4.2.1.8" << " x " << payloadbyte; + + // os << "i 2"; + + string message = "i 2"; + PLOG(logDEBUG2) << _logPrefix << "Sending: " << message + << " to port: " << _snmpClient->GetPort(); + + auto snmp_response = _snmpClient->SNMPSet("iso.0.15628.4.1.99.0", message); + PLOG(logDEBUG) << "Received response: " << snmp_response; } // Send the message using the configured UDP client. else { + os << "Version=0.7" << "\n"; + os << "Type=" << _messageConfigMap[configIndex].SendType << "\n" << "PSID=" << _messageConfigMap[configIndex].Psid << "\n"; + if (_messageConfigMap[configIndex].Channel.empty()) + os << "Priority=7" << "\n" << "TxMode=CONT" << "\n" << "TxChannel=" << msg->dsrcMetadata->channel << "\n"; + else + os << "Priority=7" << "\n" << "TxMode=CONT" << "\n" << "TxChannel=" << _messageConfigMap[configIndex].Channel << "\n"; + os << "TxInterval=0" << "\n" << "DeliveryStart=\n" << "DeliveryStop=\n"; + os << "Signature="<< (signState == 1 ? "True" : "False") << "\n" << "Encryption=False\n"; + os << "Payload=" << payloadbyte << "\n"; + + string message = os.str(); + for (uint i = 0; i < _udpClientList[_messageConfigMap[configIndex].ClientIndex].size(); i++) { //cout << message << endl; diff --git a/src/v2i-hub/ImmediateForwardPlugin/src/ImmediateForwardPlugin.h b/src/v2i-hub/ImmediateForwardPlugin/src/ImmediateForwardPlugin.h index c07086415..d06ed7db4 100644 --- a/src/v2i-hub/ImmediateForwardPlugin/src/ImmediateForwardPlugin.h +++ b/src/v2i-hub/ImmediateForwardPlugin/src/ImmediateForwardPlugin.h @@ -23,6 +23,7 @@ #include #include +using namespace tmx::utils; namespace ImmediateForward { @@ -61,13 +62,15 @@ class ImmediateForwardPlugin : public tmx::utils::PluginClient std::mutex _mutexUdpClient; typedef std::vector svr_list; std::array _udpClientList; - std::shared_ptr _snmpClient; std::vector _messageConfigMap; std::map _messageCountMap; + std::string _rsuIp; + uint16_t _snmpPort; std::string signatureData; std::string url; std::string baseurl; - std::string _securityUser; + std::string _securityLevel; + std::string _snmpUser; std::string _authPassPhrase; unsigned int signState; unsigned int snmpState; @@ -82,13 +85,6 @@ class ImmediateForwardPlugin : public tmx::utils::PluginClient bool _muteDsrc; // @SONAR_START@ - -protected: - void sendSNMP(); - /** - * @brief Send SNMP request to RSU to transmit forwarded message - */ - }; } /* namespace ImmediateForward */