#include #include #include #include // snmp API #include #include #include #include #include #include #include extern int snmp_errno; snmp_session* m_Session; int SnmpCallback (int operation, struct snmp_session* session, int reqid, struct snmp_pdu* pdu, void* magic) { printf("CSnmp::SnmpCallback\n"); // TODO return 1; // successful completion } int main(int argc, char *argv[]) { oid BaseOid[] = {1,3,6,1,4,1,4329,1}; char AgentIpAddress[] = "192.168.184.200"; snmp_session snmp_session_settings; snmp_session_settings.community = 0; // default snmp_session_settings.community_len = SNMP_DEFAULT_COMMUNITY_LEN; // default snmp_session_settings.retries = 0; // no retries snmp_session_settings.timeout = SNMP_DEFAULT_TIMEOUT; snmp_session_settings.peername = AgentIpAddress; // agent ip address 192.168.184.200 snmp_session_settings.remote_port = 161; // UDP Port at agent snmp_session_settings.local_port = 162; // local UDP port for traps snmp_session_settings.authenticator = NULL; // not authentication used snmp_session_settings.callback = (int (*)())(SnmpCallback); // callback for async answers and traps snmp_session_settings.callback_magic = NULL; snmp_session_settings.version = SNMP_VERSION_2; snmp_session_settings.srcParty = BaseOid; snmp_session_settings.srcPartyLen = sizeof(BaseOid)/sizeof(oid); snmp_session_settings.dstParty = BaseOid; snmp_session_settings.dstPartyLen = sizeof(BaseOid)/sizeof(oid); snmp_session_settings.context = BaseOid; snmp_session_settings.contextLen = sizeof(BaseOid)/sizeof(oid); m_Session = snmp_open(&snmp_session_settings); if(m_Session == NULL) { printf("Error %d in snmp_open\n", snmp_errno); } else { printf("snmp_open done!\n"); } long Value = 0; oid AnyOID[] = {1,3,6,1,4,1,4329,1,2,4}; variable_list new_variable; new_variable.name = &AnyOID[0]; new_variable.name_length = sizeof(AnyOID); new_variable.next_variable = NULL; new_variable.type = ASN_INTEGER; new_variable.val.integer = &Value; new_variable.val_len = sizeof(Value); ipaddr theIpAddr; memset(&theIpAddr, 0, sizeof(theIpAddr)); theIpAddr.sin_len = sizeof(theIpAddr); theIpAddr.sin_family = AF_INET; theIpAddr.sin_port = htons((uint16_t)162); theIpAddr.sin_addr.s_addr = __IPADDR(0xC0A4B801); //192.168.184.1 // create a new pdu struct struct snmp_pdu * new_snmp_pdu = snmp_pdu_create (GET_REQ_MSG); // fill the pdu struct new_snmp_pdu->version = m_Session->version; new_snmp_pdu->address = theIpAddr; new_snmp_pdu->srcParty = m_Session->srcParty; new_snmp_pdu->srcPartyLen = m_Session->srcPartyLen; new_snmp_pdu->dstParty = m_Session->dstParty; new_snmp_pdu->dstPartyLen = m_Session->dstPartyLen; new_snmp_pdu->context = m_Session->context; new_snmp_pdu->contextLen = m_Session->contextLen; new_snmp_pdu->community = m_Session->community; new_snmp_pdu->community_len = m_Session->community_len; // new_snmp_pdu->command = allready filed new_snmp_pdu->reqid = 1; new_snmp_pdu->agent_addr = theIpAddr; // new_snmp_pdu->trap_type :don't know // new_snmp_pdu->specific_type :don't know // new_snmp_pdu->time :use default new_snmp_pdu->variables = &new_variable; int returnvalue = snmp_send( m_Session, new_snmp_pdu ); if(returnvalue == 0) { printf("Error %d in snmp_send\n", snmp_errno); } else { printf("snmp_send done!\n"); } if(m_Session != NULL) { // close snmp session if(snmp_close(m_Session) == 0) { printf("Error %d in snmp_close\n", snmp_errno); } else { printf("snmp_close done!\n"); } } return EXIT_SUCCESS; }