Skip to content

Commit

Permalink
modified: src/v2i-hub/ERVCloudForwardingPlugin/src/SNMPClient.cpp
Browse files Browse the repository at this point in the history
	modified:   src/v2i-hub/ERVCloudForwardingPlugin/src/SNMPClient.h
	modified:   src/v2i-hub/ImmediateForwardPlugin/src/ImmediateForwardPlugin.cpp
	modified:   src/v2i-hub/ImmediateForwardPlugin/src/ImmediateForwardPlugin.h
  • Loading branch information
jwillmartin committed May 12, 2023
1 parent 747a678 commit 47a1dc3
Show file tree
Hide file tree
Showing 4 changed files with 88 additions and 7 deletions.
60 changes: 58 additions & 2 deletions src/v2i-hub/ERVCloudForwardingPlugin/src/SNMPClient.cpp
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
#include "SNMPClient.h"

SNMPClient::SNMPClient(const std::string &rsu_ip, uint16_t snmp_port, const std::string &securityUser, const std::string &authPassPhrase)
SNMPClient::SNMPClient(const std::string &rsuIP, uint16_t snmp_port, const std::string &securityUser, const std::string &authPassPhrase)
: _snmp_port(snmp_port)
, _rsuIP(rsuIP)
{
std::string ip_port_string = rsu_ip + ":" + std::to_string(snmp_port);
std::string ip_port_string = rsuIP + ":" + std::to_string(snmp_port);
char *ip_port = &ip_port_string[0];
init_snmp("snmpclient");
snmp_sess_init(&session);
Expand Down Expand Up @@ -86,6 +88,60 @@ std::string SNMPClient::SNMPGet(const std::string &req_oid)
return result;
}

std::string SNMPClient::SNMPSet(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))
{
snmp_perror(req_oid.c_str());
std::string errMsg = "OID could not be created from input:" + req_oid;
throw SNMPClientException(errMsg);
SOCK_CLEANUP;
}
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!");
}
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<char *>(vars->val.string);
}
else
{
throw SNMPClientException("Received respones type is not a string");
}
}
}
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;
}

int SNMPClient::GetPort() const
{
return _snmp_port;
}

std::string SNMPClient::GetAddress() const
{
return _rsuIP;
}

SNMPClient::~SNMPClient()
{
fprintf(stdout, "Closing snmp session\n");
Expand Down
20 changes: 19 additions & 1 deletion src/v2i-hub/ERVCloudForwardingPlugin/src/SNMPClient.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,20 +17,38 @@ class SNMPClient
netsnmp_session *ss;
oid anOID[MAX_OID_LEN];
size_t anOID_len = MAX_OID_LEN;
int _snmp_port;
std::string _rsuIP;

public:
/**
* @brief Construct a new SNMPClient object
* @param ip RSU IP
* @param port SNMP port
*/
SNMPClient(const std::string &rsu_ip, uint16_t snmp_port, const std::string &securityUser, const std::string &authPassPhrase);
SNMPClient(const std::string &rsuIP, uint16_t snmp_port, const std::string &securityUser, 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
* @return std::string identified by the oid. If SNMP response is not string, exit with failure.
*/
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
* @return std::string identified by the oid. If SNMP response is not string, exit with failure.
*/
std::string SNMPSet(const std::string &oid);
/**
* @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.
*/
virtual std::string GetAddress() const;
~SNMPClient();
};
#endif
14 changes: 10 additions & 4 deletions src/v2i-hub/ImmediateForwardPlugin/src/ImmediateForwardPlugin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -202,8 +202,9 @@ bool ImmediateForwardPlugin::UpdateUdpClientFromConfigSettings(uint clientIndex)
{
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;
auto snmpClient = std::make_shared<SNMPClient>(_rsuIp, _snmpPort, _securityUser, _authPassPhrase);
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<SNMPClient>(_rsuIp, _snmpPort, _securityUser, _authPassPhrase);
}
else
{
Expand Down Expand Up @@ -416,10 +417,15 @@ void ImmediateForwardPlugin::SendMessageToRadio(IvpMessage *msg)


// Send the message using the configured SNMP client
if (signState == 1)
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);
}

// Send the message using the configured UDP client.
else
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ class ImmediateForwardPlugin : public tmx::utils::PluginClient
std::mutex _mutexUdpClient;
typedef std::vector<tmx::utils::UdpClient *> svr_list;
std::array<svr_list, 4> _udpClientList;
std::shared_ptr<SNMPClient> _snmpClient;
std::vector<MessageConfig> _messageConfigMap;
std::map<std::string, int> _messageCountMap;
std::string signatureData;
Expand Down

0 comments on commit 47a1dc3

Please sign in to comment.