0neblock / Arduino_SNMP

SNMP Agent built with Arduino
MIT License
77 stars 30 forks source link

specificTrap and UDP ports ..... #25

Closed Nourbakhsh-Rad closed 2 years ago

Nourbakhsh-Rad commented 2 years ago

Please modify (and add) the followings, Thanks ...

////// SNMP_Agent.h short _AgentUDPport = 161;

void setUDPport(short port){
    _AgentUDPport = port;
}

////// SNMP_Agent.cpp

bool SNMPAgent::restartUDP() {
    for(auto udp : _udp){
        udp->stop();
        udp->begin(_AgentUDPport);
    }
    return true;
}

////// SNMPTrap.h short specificTrap = 1; short _TrapUDPport = 162;

void setUDPport(short port){
    _TrapUDPport = port;
}
bool sendTo(const IPAddress& ip, bool skipBuild = false){
    bool buildStatus = true;
    if(!skipBuild) {
        buildStatus = this->buildForSending();
    }

    if(!_udp){
        return false;
    }

    if(!this->packet){
        return false;
    }

    if(!buildStatus){
        SNMP_LOGW("Failed Building packet..");
        return false;
    }

    uint8_t _packetBuffer[MAX_SNMP_PACKET_LENGTH] = {0};
    int length = packet->serialise(_packetBuffer, MAX_SNMP_PACKET_LENGTH);

    if(length <= 0) return false;

    _udp->beginPacket(ip, _TrapUDPport);
    _udp->write(_packetBuffer, length);
    return _udp->endPacket();
}

////// add to defs.h

#define RFC1213_OID_sysDescr            (char*)(".1.3.6.1.2.1.1.1.0")
#define RFC1213_OID_sysObjectID         (char*)(".1.3.6.1.2.1.1.2.0")
#define RFC1213_OID_sysUpTime           (char*)(".1.3.6.1.2.1.1.3.0")
#define RFC1213_OID_sysContact          (char*)(".1.3.6.1.2.1.1.4.0")
#define RFC1213_OID_sysName         (char*)(".1.3.6.1.2.1.1.5.0")
#define RFC1213_OID_sysLocation         (char*)(".1.3.6.1.2.1.1.6.0")
#define RFC1213_OID_sysServices         (char*)(".1.3.6.1.2.1.1.7.0")

typedef struct RFC1213SystemStruct
{
char*           sysDescr;           /* .1.3.6.1.2.1.1.1.0 */
char*           sysObjectID;            /* .1.3.6.1.2.1.1.2.0 */
uint32_t        sysUpTime;          /* .1.3.6.1.2.1.1.3.0 */
char*           sysContact;         /* .1.3.6.1.2.1.1.4.0 */
char*           sysName;            /* .1.3.6.1.2.1.1.5.0 */
char*           sysLocation;            /* .1.3.6.1.2.1.1.6.0 */
int32_t         sysServices;            /* .1.3.6.1.2.1.1.7.0 */
} RFC1213_list;
0neblock commented 2 years ago

Hi @Nourbakhsh-Rad, would you like to open a PR that has these changes, and I can merge them in. or would you like me to make the changes?

Nourbakhsh-Rad commented 2 years ago

Hi @0neblock , I appriciate you make this changes, thanks a lot ....

and this change for example (ESP32_SNMP.ino)

#if defined (ESP8266)
    #include <ESP8266WiFi.h>        // ESP8266 Core WiFi Library         
#else
    #include <WiFi.h>               // ESP32 Core WiFi Library    
#endif