/* * Creates and allocates a clone of the input PDU, * but does NOT copy the variables. * This function should be used with another function, * such as _copy_pdu_vars. * * Returns a pointer to the cloned PDU if successful. * Returns 0 if failure. */ static netsnmp_pdu * _clone_pdu_header(netsnmp_pdu *pdu) { netsnmp_pdu *newpdu; struct snmp_secmod_def *sptr; newpdu = (netsnmp_pdu *) malloc(sizeof(netsnmp_pdu)); if (!newpdu) return 0; memmove(newpdu, pdu, sizeof(netsnmp_pdu)); /* * reset copied pointers if copy fails */ newpdu->variables = 0; newpdu->enterprise = 0; newpdu->community = 0; newpdu->securityEngineID = 0; newpdu->securityName = 0; newpdu->contextEngineID = 0; newpdu->contextName = 0; newpdu->transport_data = 0; /* * copy buffers individually. If any copy fails, all are freed. */ if (snmp_clone_mem((void **) &newpdu->enterprise, pdu->enterprise, sizeof(oid) * pdu->enterprise_length) || snmp_clone_mem((void **) &newpdu->community, pdu->community, pdu->community_len) || snmp_clone_mem((void **) &newpdu->contextEngineID, pdu->contextEngineID, pdu->contextEngineIDLen) || snmp_clone_mem((void **) &newpdu->securityEngineID, pdu->securityEngineID, pdu->securityEngineIDLen) || snmp_clone_mem((void **) &newpdu->contextName, pdu->contextName, pdu->contextNameLen) || snmp_clone_mem((void **) &newpdu->securityName, pdu->securityName, pdu->securityNameLen) || snmp_clone_mem((void **) &newpdu->transport_data, pdu->transport_data, pdu->transport_data_length)) { snmp_free_pdu(newpdu); return 0; } if ((sptr = find_sec_mod(newpdu->securityModel)) != NULL && sptr->pdu_clone != NULL) { /* * call security model if it needs to know about this */ (*sptr->pdu_clone) (pdu, newpdu); } return newpdu; }
/* * send_trap_to_sess: sends a trap to a session but assumes that the * pdu is constructed correctly for the session type. */ void send_trap_to_sess(netsnmp_session * sess, netsnmp_pdu *template_pdu) { netsnmp_pdu *pdu; int result; char tmp[SPRINT_MAX_LEN]; int len; if (!sess || !template_pdu) return; DEBUGMSGTL(("trap", "sending trap type=%d, version=%d\n", template_pdu->command, sess->version)); #ifndef NETSNMP_DISABLE_SNMPV1 if (sess->version == SNMP_VERSION_1 && (template_pdu->command != SNMP_MSG_TRAP)) return; /* Skip v1 sinks for v2 only traps */ if (sess->version != SNMP_VERSION_1 && (template_pdu->command == SNMP_MSG_TRAP)) return; /* Skip v2+ sinks for v1 only traps */ #endif template_pdu->version = sess->version; pdu = snmp_clone_pdu(template_pdu); pdu->sessid = sess->sessid; /* AgentX only ? */ if ( template_pdu->command == SNMP_MSG_INFORM #ifdef USING_AGENTX_PROTOCOL_MODULE || template_pdu->command == AGENTX_MSG_NOTIFY #endif ) { result = snmp_async_send(sess, pdu, &handle_inform_response, NULL); } else { if ((sess->version == SNMP_VERSION_3) && (pdu->command == SNMP_MSG_TRAP2) && (pdu->securityEngineIDLen == 0)) { len = snmpv3_get_engineID(tmp, sizeof(tmp)); memdup(&pdu->securityEngineID, tmp, len); pdu->securityEngineIDLen = len; } result = snmp_send(sess, pdu); } if (result == 0) { snmp_sess_perror("snmpd: send_trap", sess); snmp_free_pdu(pdu); } else { snmp_increment_statistic(STAT_SNMPOUTTRAPS); snmp_increment_statistic(STAT_SNMPOUTPKTS); } }
/** Get current from power supply */ double getCurrentMeasurement(HSNMP m_sessp, int channel) { double value; struct snmp_pdu* pdu = snmp_pdu_create(SNMP_MSG_GET); // prepare get-request pdu // for(each GET request to one crate) { snmp_add_null_var(pdu,oidOutputMeasurementCurrent[channel],lengthOutputMeasurementCurrent[channel]); // generate request data // } // endfor struct snmp_pdu* response; int status = snmp_sess_synch_response(m_sessp,pdu,&response); /* * Process the response. */ if (status == STAT_SUCCESS && response->errstat == SNMP_ERR_NOERROR) { /* * SUCCESS: Print the result variables */ struct variable_list *vars; // debug print //for(vars = response->variables; vars; vars = vars->next_variable) // print_variable(vars->name, vars->name_length, vars); /* manipuate the information ourselves */ for(vars = response->variables; vars; vars = vars->next_variable) { if (vars->type == ASN_OPAQUE_FLOAT) { // 0x78 value = *vars->val.floatVal; } else if (vars->type == ASN_OPAQUE_DOUBLE) { // 0x79 value = *vars->val.doubleVal; } else if(vars->type == ASN_INTEGER) { // 0x02 value = (double)*vars->val.integer; } } } else { /* * FAILURE: print what went wrong! */ if (status == STAT_SUCCESS) fprintf(stderr, "Error in packet\nReason: %s\n", snmp_errstring(response->errstat)); else snmp_sess_perror("snmpget",snmp_sess_session(m_sessp)); return 0; } snmp_free_pdu(response); return value; }
int handle_subagent_set_response(int op, netsnmp_session * session, int reqid, netsnmp_pdu *pdu, void *magic) { netsnmp_session *retsess; struct agent_netsnmp_set_info *asi; if (op != NETSNMP_CALLBACK_OP_RECEIVED_MESSAGE || magic == NULL) { return 1; } DEBUGMSGTL(("agentx/subagent", "handling agentx subagent set response (mode=%d,req=0x%x," "trans=0x%x,sess=0x%x)\n", pdu->command, pdu->reqid,pdu->transid, pdu->sessid)); pdu = snmp_clone_pdu(pdu); asi = (struct agent_netsnmp_set_info *) magic; retsess = asi->sess; asi->errstat = pdu->errstat; if (asi->mode == SNMP_MSG_INTERNAL_SET_RESERVE1) { /* * reloop for RESERVE2 mode, an internal only agent mode */ /* * XXX: check exception statuses of reserve1 first */ if (!pdu->errstat) { asi->mode = pdu->command = SNMP_MSG_INTERNAL_SET_RESERVE2; snmp_async_send(agentx_callback_sess, pdu, handle_subagent_set_response, asi); DEBUGMSGTL(("agentx/subagent", " going from RESERVE1 -> RESERVE2\n")); return 1; } } else { if (asi->mode == SNMP_MSG_INTERNAL_SET_FREE || asi->mode == SNMP_MSG_INTERNAL_SET_UNDO || asi->mode == SNMP_MSG_INTERNAL_SET_COMMIT) { free_set_vars(retsess, pdu); } pdu->variables = NULL; /* the variables were added by us */ } netsnmp_assert(retsess != NULL); pdu->command = AGENTX_MSG_RESPONSE; pdu->version = retsess->version; if (!snmp_send(retsess, pdu)) { snmp_free_pdu(pdu); } DEBUGMSGTL(("agentx/subagent", " FINISHED\n")); return 1; }
/** Write RampUp to power supply */ double setOutputRampUp(HSNMP m_sessp,int channel,double value) { struct snmp_pdu* pdu = snmp_pdu_create(SNMP_MSG_SET); // prepare set-request pdu pdu->community = (u_char*)strdup(writeCommunity); pdu->community_len = strlen(writeCommunity); // for(each SET request to one crate) { float v = (float) value; snmp_pdu_add_variable(pdu,oidOutputRampUp[channel],lengthOutputRampUp[channel],ASN_OPAQUE_FLOAT,(u_char*)&v,sizeof(v)); // } // endfor struct snmp_pdu* response; int status = snmp_sess_synch_response(m_sessp,pdu,&response); /* * Process the response. */ if (status == STAT_SUCCESS && response->errstat == SNMP_ERR_NOERROR) { /* * SUCCESS: Print the result variables */ struct variable_list *vars; // debug print //for(vars = response->variables; vars; vars = vars->next_variable) // print_variable(vars->name, vars->name_length, vars); /* manipuate the information ourselves */ for(vars = response->variables; vars; vars = vars->next_variable) { if (vars->type == ASN_OPAQUE_FLOAT) { // 0x78 value = *vars->val.floatVal; } else if (vars->type == ASN_OPAQUE_DOUBLE) { // 0x79 value = *vars->val.doubleVal; } else if(vars->type == ASN_INTEGER) { // 0x02 value = (double)*vars->val.integer; } } } else { /* * FAILURE: print what went wrong! */ if (status == STAT_SUCCESS) fprintf(stderr, "Error in packet\nReason: %s\n", snmp_errstring(response->errstat)); else snmp_sess_perror("snmpget",snmp_sess_session(m_sessp)); return 0; } snmp_free_pdu(response); return value; }
static void NotifyingEntry(UNUSED tState self) { netsnmp_pdu* act = snmp_clone_pdu(pdu); if(act) { act->sessid = session; act->transid = 0; act->reqid = ++packetid; if(snmp_sess_send(sessp, act) == 0) snmp_free_pdu(act); } }
int powernet_snmp_kill_ups_power(UPSINFO *ups) { /* Was 1} change submitted by Kastus Shchuka ([email protected]) 10Dec03 */ oid upsBasicControlConserveBattery[] = { 1, 3, 6, 1, 4, 1, 318, 1, 1, 1, 6, 1, 1, 0 }; struct snmp_ups_internal_data *Sid = (struct snmp_ups_internal_data *)ups->driver_internal_data; struct snmp_session *s = &Sid->session; struct snmp_session *peer; struct snmp_pdu *request, *response; int status; /* * Set up the SET request. */ request = snmp_pdu_create(SNMP_MSG_SET); /* * Set upsBasicControlConserveBattery variable (INTEGER) to * turnOffUpsToConserveBattery(2) value. Will turn on the UPS only * when power returns. */ if (snmp_add_var(request, upsBasicControlConserveBattery, sizeof(upsBasicControlConserveBattery) / sizeof(oid), 'i', "2")) { return 0; } peer = snmp_open(s); if (!peer) { Dmsg0(0, "Can not open the SNMP connection.\n"); return 0; } status = snmp_synch_response(peer, request, &response); if (status != STAT_SUCCESS) { Dmsg0(0, "Unable to communicate with UPS.\n"); return 0; } if (response->errstat != SNMP_ERR_NOERROR) { Dmsg1(0, "Unable to kill UPS power: can not set SNMP variable (%d).\n", response->errstat); return 0; } if (response) snmp_free_pdu(response); snmp_close(peer); return 1; }
QVariant QSnmp::get(const QString &oid) { struct snmp_pdu *pdu; struct snmp_pdu *response; // oid anOID[MAX_OID_LEN]; u_long anOID[MAX_OID_LEN]; size_t anOID_len = MAX_OID_LEN; int status; struct variable_list *vars; pdu = snmp_pdu_create(SNMP_MSG_GET); read_objid(oid.toUtf8().constData(), anOID, &anOID_len); snmp_add_null_var(pdu, anOID, anOID_len); status = snmp_synch_response(ss, pdu, &response); if (status != STAT_SUCCESS || response->errstat != SNMP_ERR_NOERROR) { qDebug("QSnmp: failed to get an oid"); if (response) snmp_free_pdu(response); return QVariant(); } for(vars = response->variables; vars; vars = vars->next_variable) { if (vars->type == ASN_OCTET_STR) { // string QByteArray res(vars->val_len, '\0'); memcpy(res.data(), vars->val.string, vars->val_len); snmp_free_pdu(response); return res; } if (vars->type == ASN_INTEGER) { int res = (int)*vars->val.integer; snmp_free_pdu(response); return res; } print_variable(vars->name, vars->name_length, vars); qDebug("QSnmp: unknown var type %d", vars->type); } snmp_free_pdu(response); return QVariant(); }
bool Session::getVariable(const QString &oid, QVariant &retvar, uint32_t &status) { QMutexLocker ml(&m_mutex); struct snmp_pdu *response = NULL; if ( ! m_session ) { status = 0xFFFFFFFF; return false; } // Buffer requestd for net-snmp library u_long anOID[MAX_OID_LEN]; size_t anOID_len = MAX_OID_LEN; // Prepare the PDU for a GET command struct snmp_pdu *pdu = snmp_pdu_create(SNMP_MSG_GET); get_node(oid.latin1(), anOID, &anOID_len); snmp_add_null_var(pdu, anOID, anOID_len); status = snmp_synch_response(m_session, pdu, &response); //! @todo Error handling should be changed in a more OO way. if ( status != STAT_SUCCESS ) { snmp_sess_perror("snmpget", m_session); return false; } if ( response->errstat != SNMP_ERR_NOERROR ) { kdWarning() << "Error in packet: " << snmp_errstring(response->errstat) << endl; snmp_free_pdu(response); return false; } retvar = snmpvarToVariant(response->variables); snmp_free_pdu(response); return true; }
netsnmp_variable_list * collect(netsnmp_session * ss, netsnmp_pdu *pdu, oid * base, size_t base_length) { netsnmp_pdu *response; int running = 1; netsnmp_variable_list *saved = NULL, **vlpp = &saved; int status; while (running) { /* * gotta catch em all, gotta catch em all! */ status = snmp_synch_response(ss, pdu, &response); if (status != STAT_SUCCESS || !response) { snmp_sess_perror("snmpdf", ss); exit(1); } if (response->errstat != SNMP_ERR_NOERROR) { fprintf(stderr, "snmpdf: Error in packet: %s\n", snmp_errstring(response->errstat)); exit(1); } if (response && snmp_oid_compare(response->variables->name, SNMP_MIN(base_length, response->variables-> name_length), base, base_length) != 0) running = 0; else { /* * get response */ *vlpp = response->variables; (*vlpp)->next_variable = NULL; /* shouldn't be any, but just in case */ /* * create the next request */ pdu = snmp_pdu_create(SNMP_MSG_GETNEXT); snmp_add_null_var(pdu, (*vlpp)->name, (*vlpp)->name_length); /* * finish loop setup */ vlpp = &((*vlpp)->next_variable); response->variables = NULL; /* ahh, forget about it */ } snmp_free_pdu(response); } return saved; }
int collect_mem(netsnmp_session *ss, struct memStats *mem) { netsnmp_pdu *pdu; netsnmp_pdu *response; int status; int ret = 0; pdu = snmp_pdu_create(SNMP_MSG_GET); add(pdu, "UCD-SNMP-MIB:memTotalSwap.0", NULL, 0); add(pdu, "UCD-SNMP-MIB:memAvailSwap.0", NULL, 0); add(pdu, "UCD-SNMP-MIB:memTotalReal.0", NULL, 0); add(pdu, "UCD-SNMP-MIB:memAvailReal.0", NULL, 0); add(pdu, "UCD-SNMP-MIB:memShared.0", NULL, 0); add(pdu, "UCD-SNMP-MIB:memBuffer.0", NULL, 0); add(pdu, "UCD-SNMP-MIB:memCached.0", NULL, 0); status = snmp_synch_response(ss, pdu, &response); memset(mem, 0, sizeof(*mem)); if (status != STAT_SUCCESS || !response || response->errstat != SNMP_ERR_NOERROR) { goto out; } else { netsnmp_variable_list *vlp = response->variables; if (vlp->type == SNMP_NOSUCHOBJECT) goto out; mem->totalSwap = *vlp->val.integer; vlp = vlp->next_variable; if (vlp->type == SNMP_NOSUCHOBJECT) goto out; mem->availSwap = *vlp->val.integer; vlp = vlp->next_variable; if (vlp->type == SNMP_NOSUCHOBJECT) goto out; mem->totalReal = *vlp->val.integer; vlp = vlp->next_variable; if (vlp->type == SNMP_NOSUCHOBJECT) goto out; mem->availReal = *vlp->val.integer; vlp = vlp->next_variable; ret = 1; if (vlp->type == SNMP_NOSUCHOBJECT) goto out; mem->shared = *vlp->val.integer; vlp = vlp->next_variable; if (vlp->type == SNMP_NOSUCHOBJECT) goto out; mem->buffer = *vlp->val.integer; vlp = vlp->next_variable; if (vlp->type == SNMP_NOSUCHOBJECT) goto out; mem->cached = *vlp->val.integer; } out: if (response) snmp_free_pdu(response); return ret; }
/* * Copy some or all variables from source PDU to target PDU. * This function consolidates many of the needs of PDU variables: * Clone PDU : copy all the variables. * Split PDU : skip over some variables to copy other variables. * Fix PDU : remove variable associated with error index. * * Designed to work with _clone_pdu_header. * * If drop_err is set, drop any variable associated with errindex. * If skip_count is set, skip the number of variable in pdu's list. * While copy_count is greater than zero, copy pdu variables to newpdu. * * If an error occurs, newpdu is freed and pointer is set to 0. * * Returns a pointer to the cloned PDU if successful. * Returns 0 if failure. */ static netsnmp_pdu * _copy_pdu_vars(netsnmp_pdu *pdu, /* source PDU */ netsnmp_pdu *newpdu, /* target PDU */ int drop_err, /* !=0 drop errored variable */ int skip_count, /* !=0 number of variables to skip */ int copy_count) { /* !=0 number of variables to copy */ netsnmp_variable_list *var, *oldvar; int ii, copied, drop_idx; if (!newpdu) return 0; /* where is PDU to copy to ? */ if (drop_err) drop_idx = pdu->errindex - skip_count; else drop_idx = 0; var = pdu->variables; while (var && (skip_count-- > 0)) /* skip over pdu variables */ var = var->next_variable; oldvar = 0; ii = 0; copied = 0; if (pdu->flags & UCD_MSG_FLAG_FORCE_PDU_COPY) copied = 1; /* We're interested in 'empty' responses too */ newpdu->variables = _copy_varlist(var, drop_idx, copy_count); if (newpdu->variables) copied = 1; #if ALSO_TEMPORARILY_DISABLED /* * Error if bad errindex or if target PDU has no variables copied */ if ((drop_err && (ii < pdu->errindex)) #if TEMPORARILY_DISABLED /* * SNMPv3 engineID probes are allowed to be empty. * See the comment in snmp_api.c for further details */ || copied == 0 #endif ) { snmp_free_pdu(newpdu); return 0; } #endif return newpdu; }
int agentx_unregister( struct snmp_session *ss, oid start[], size_t startlen, int priority, int range_subid, oid range_ubound) { struct snmp_pdu *pdu, *response; if (! IS_AGENTX_VERSION( ss->version )) return 0; DEBUGMSGTL(("agentx/subagent","unregistering: ")); DEBUGMSGOID(("agentx/subagent", start, startlen)); DEBUGMSG(("agentx/subagent","\n")); pdu = snmp_pdu_create(AGENTX_MSG_UNREGISTER); if ( pdu == NULL ) return 0; pdu->time = 0; pdu->priority = priority; pdu->sessid = ss->sessid; pdu->range_subid = range_subid; if ( range_subid ) { snmp_pdu_add_variable( pdu, start, startlen, ASN_OBJECT_ID, (u_char *)start, startlen); pdu->variables->val.objid[ range_subid-1 ] = range_ubound; } else snmp_add_null_var( pdu, start, startlen); if ( agentx_synch_response(ss, pdu, &response) != STAT_SUCCESS ) return 0; if ( response->errstat != SNMP_ERR_NOERROR ) { snmp_free_pdu(response); return 0; } snmp_free_pdu(response); DEBUGMSGTL(("agentx/subagent","unregistered\n")); return 1; }
static netsnmp_pdu* pdu_create_opt_context(int command, const char* context, size_t len) { netsnmp_pdu* res = snmp_pdu_create(command); if (res) if (context) { if (snmp_clone_mem((void**)&res->contextName, context, len)) { snmp_free_pdu(res); res = NULL; } else res->contextNameLen = len; } return res; }
bool QSnmp::set(const QString &oid, int val) { struct snmp_pdu *pdu; struct snmp_pdu *response = NULL; // oid anOID[MAX_OID_LEN]; u_long anOID[MAX_OID_LEN]; size_t anOID_len = MAX_OID_LEN; int status; pdu = snmp_pdu_create(SNMP_MSG_SET); read_objid(oid.toUtf8().constData(), anOID, &anOID_len); snmp_add_var(pdu, anOID, anOID_len, 'i', QByteArray::number(val).constData()); //snmp_add_var(pdu, anOID, anOID_len, ASN_INTEGER, QByteArray::number(val).constData()); status = snmp_synch_response(ss, pdu, &response); if (status != STAT_SUCCESS || response->errstat != SNMP_ERR_NOERROR) { qDebug("QSnmp: failed to set an oid"); if (response) snmp_free_pdu(response); return false; } if (response) snmp_free_pdu(response); return true; }
static int send_pdu(netsnmp_session *ss, netsnmp_pdu *pdu, int kind, int link_id) { int status; netsnmp_callback cb; cb = kind == SNMP_LINK_UP ? link_up_cb : link_down_cb; status = snmp_async_send(ss, pdu, cb, (void *) link_id); if (status == 0) { snmp_log(LOG_ERR, "Failed to send async request.\n"); snmp_free_pdu(pdu); } return status; }
static void ClosingEntry(UNUSED tState self) { /* CLOSE pdu->errstat */ netsnmp_pdu* act = pdu_create_opt_context(AGENTX_MSG_CLOSE, context, contextLen); if(act) { act->sessid = session; act->transid = 0; act->reqid = ++packetid; act->errstat = AGENTX_CLOSE_SHUTDOWN; if(snmp_sess_send(sessp, act) == 0) snmp_free_pdu(act); } }
static void OpeningEntry(UNUSED tState self) { netsnmp_pdu* act = pdu_create_opt_context(AGENTX_MSG_OPEN, context, contextLen); if(act) { act->sessid = 0; act->transid = 0; act->reqid = ++packetid; act->time = 0; snmp_pdu_add_variable(act, NULL, 0, ASN_OCTET_STR, NULL, 0); if(snmp_sess_send(sessp, act) == 0) snmp_free_pdu(act); } }
static void send_agentx_error (netsnmp_session * session, netsnmp_pdu * pdu, int errstat, int errindex) { pdu = snmp_clone_pdu (pdu); pdu->command = AGENTX_MSG_RESPONSE; pdu->version = session->version; pdu->errstat = errstat; pdu->errindex = errindex; snmp_free_varbind (pdu->variables); pdu->variables = NULL; DEBUGMSGTL (("agentx/subagent", "Sending AgentX response error stat %d idx %d\n", errstat, errindex)); if (!snmp_send (session, pdu)) { snmp_free_pdu (pdu); } }
/* * response handler */ int asynch_response(int operation, struct snmp_session *sp, int reqid, struct snmp_pdu *pdu, void *magic) { struct session *host = (struct session *)magic; struct snmp_pdu *req; struct oid *op; if (operation == NETSNMP_CALLBACK_OP_RECEIVED_MESSAGE) { if (print_result(STAT_SUCCESS, host->sess, pdu)) { /* host->current_oid++; */ /* send next GET (if any) */ op = host->current_oid; op++; while(op->hostname) { /* printf("[%s] [%s]\n",op->hostname, host->current_oid->hostname); */ if(strcmp(op->hostname,host->current_oid->hostname)==0) { host->current_oid = op; break; } op++; } if (op->hostname && host->current_oid->Name) { req = snmp_pdu_create(SNMP_MSG_GET); snmp_add_null_var(req, host->current_oid->Oid, host->current_oid->OidLen); if (snmp_send(host->sess, req)) return 1; else { snmp_perror("snmp_send"); snmp_free_pdu(req); } } else { /* printf("No more OIDs for [%s]\n", host->current_oid->hostname); */ } } } else print_result(STAT_TIMEOUT, host->sess, pdu); /* something went wrong (or end of variables) * this host not active any more */ active_hosts--; return 1; }
char *session_query(struct snmp_session *ss, struct snmp_pdu *pdu) { struct snmp_pdu *response; struct variable_list *vars; int status; char buf[SPRINT_MAX_LEN]; char *rbuffer=NULL; /* Send the Request */ status = snmp_synch_response(ss, pdu, &response); /* Process the response */ if (status == STAT_SUCCESS && response->errstat == SNMP_ERR_NOERROR ) { /* Success: Print the results */ /* for (vars=response->variables; vars; vars=vars->next_variable) { */ vars=response->variables; if (vars!=NULL && vars->type <ASN_LONG_LEN) { if (vars->type == ASN_INTEGER) { sprintf(buf,"%ld",*vars->val.integer); rbuffer=malloc(sizeof(char)*(strlen(buf)+1)); memset(rbuffer,'\0',strlen(buf)+1); strncpy(rbuffer,buf,strlen(buf)); } else { snprint_variable(buf, sizeof (buf), vars->name, vars->name_length, vars); rbuffer=malloc(sizeof(char)*(strlen(buf)+1)); memset(rbuffer,'\0',strlen(buf)+1); strncpy(rbuffer,buf,strlen(buf)); } } else rbuffer = NULL; } else { /* Failure: print what went wrong */ if (status == STAT_SUCCESS) fprintf(stderr,"Error in packet.\nReason: %s\n", snmp_errstring(response->errstat)); else snmp_sess_perror("snmpget ", ss); } /* Clean up */ if (response) snmp_free_pdu(response); // printf("Result : %s\n",rbuffer); return rbuffer; }
void snmp_get(void *precord) { snmpRecord *psnmp = (snmpRecord *)precord; /* SNMP_INFO *gsnmpInfo = (SNMP_INFO*)psnmp->dpvt; */ printf("snmpget()****** Message:%s, version: %s, ip: %s, name: %s, securityname: %s, authpass: %s, privpass: %s, secuauth: %s, secupriv: %s\n", snmpinfo->msg, snmpinfo->ss.version, snmpinfo->ss.peername, snmpinfo->username, snmpinfo->ss.securityName, snmpinfo->authpass, snmpinfo->privpass, snmpinfo->ss.securityAuthKey, snmpinfo->ss.securityPrivKey); printf("oids: %s\n", psnmp->oids); if (!(snmpinfo->sess = snmp_open(&snmpinfo->ss))) { snmp_perror("snmp_open"); }; snmpinfo->getreq = snmp_pdu_create(SNMP_MSG_GET); /* send the first GET */ if (! snmpinfo->getreq) { snmp_close(snmpinfo->sess); /* cleanup */ } snmp_add_null_var(snmpinfo->getreq, snmpinfo->oid_info.Oid, snmpinfo->oid_info.OidLen); /* int snmp_async_send(netsnmp_session *, netsnmp_pdu *, netsnmp_callback, void *) */ /* int snmp_send(netsnmp_session *, netsnmp_pdu *) */ /* if (snmp_async_send(snmpinfo->sess, snmpinfo->getreq, asynch_response, NULL)) */ /* { */ /* hosts++; */ /* } else { */ /* snmp_perror("snmp_get->async_Send Error "); */ /* snmp_free_pdu(snmpinfo->getreq); */ /* } */ if (snmp_send(snmpinfo->sess, snmpinfo->getreq)) { hosts++; } else { snmp_perror("snmp_get->Send Error "); snmp_free_pdu(snmpinfo->getreq); } active_hosts(); snmp_close(snmpinfo->sess); /* cleanup */ }
char *session_set( struct snmp_session *ss,const char *name, const char *value ) { struct snmp_pdu *pdu, *response; oid anOID[MAX_OID_LEN]; size_t anOID_len = MAX_OID_LEN; struct variable_list *vars; int status; char buf[SPRINT_MAX_LEN]; char *rbuffer=NULL; /* create PDU for request */ pdu = snmp_pdu_create(SNMP_MSG_SET); get_node(name, anOID, &anOID_len); snmp_add_var(pdu, anOID, anOID_len,'i',value); // snmp_add_null_var(pdu, anOID, anOID_len); /* Send the Request */ status = snmp_synch_response(ss, pdu, &response); /* Process the response */ if (status == STAT_SUCCESS && response->errstat == SNMP_ERR_NOERROR) { /* Success: Print the results */ /* for (vars=response->variables; vars; vars=vars->next_variable) { */ vars=response->variables; if (vars!=NULL) { snprint_variable(buf, sizeof (buf), vars->name, vars->name_length, vars); rbuffer=malloc(sizeof(char)*(strlen(buf)+1)); memset(rbuffer,'\0',strlen(buf)+1); strncpy(rbuffer,buf,strlen(buf)); } else rbuffer = NULL; } else { /* Failure: print what went wrong */ if (status == STAT_SUCCESS) fprintf(stderr,"Error in packet.\nReason: %s\n", snmp_errstring(response->errstat)); else snmp_sess_perror("snmpset", ss); } /* Clean up */ /* if (pdu) snmp_free_pdu(pdu); */ if (response) snmp_free_pdu(response); return rbuffer; }
void QtNetSNMP::QSNMPCore::snmpoperation(SNMPPDUType type, SNMPVersion version, const QString& community, const QString& agent, QVector<QSNMPObject *>& objs, unsigned short nrepeaters, unsigned short mrepetitions) throw(QSNMPException) { SNMPSession *session; SNMPPDU *requestPDU; SNMPPDU *responsePDU; session = createSession(version, community, agent); requestPDU = createPDU(type, objs, nrepeaters, mrepetitions); responsePDU = sendPDU(session, requestPDU); if(type != SNMPPDUSet) processResponse(responsePDU, objs); snmp_free_pdu(responsePDU); snmp_close(session); SOCK_CLEANUP; // Free resources on Win32. (No effect on Unix systems) }
static void _cs_snmp_rrp_faulty_event(char *nodename, uint32_t nodeid, uint32_t iface_no, const char *state) { int ret; char csysuptime[20]; static oid snmptrap_oid[] = { 1,3,6,1,6,3,1,1,4,1,0 }; static oid sysuptime_oid[] = { 1,3,6,1,2,1,1,3,0 }; time_t now = time (NULL); netsnmp_pdu *trap_pdu; netsnmp_session *session = snmp_init (snmp_manager); if (session == NULL) { qb_log(LOG_NOTICE, "Failed to init SNMP session."); return ; } trap_pdu = snmp_pdu_create (SNMP_MSG_TRAP2); if (!trap_pdu) { qb_log(LOG_NOTICE, "Failed to create SNMP notification."); return ; } /* send uptime */ sprintf (csysuptime, "%ld", now); snmp_add_var (trap_pdu, sysuptime_oid, sizeof (sysuptime_oid) / sizeof (oid), 't', csysuptime); snmp_add_var (trap_pdu, snmptrap_oid, sizeof (snmptrap_oid) / sizeof (oid), 'o', SNMP_OID_TRAPS_RRP); /* Add extries to the trap */ add_field (trap_pdu, ASN_OCTET_STR, SNMP_OID_OBJECT_NODE_NAME, (void*)nodename, strlen (nodename)); add_field (trap_pdu, ASN_UNSIGNED, SNMP_OID_OBJECT_NODE_ID, (void*)&nodeid, sizeof (nodeid)); add_field (trap_pdu, ASN_INTEGER, SNMP_OID_OBJECT_RRP_IFACE_NO, (void*)&iface_no, sizeof (iface_no)); add_field (trap_pdu, ASN_OCTET_STR, SNMP_OID_OBJECT_RRP_STATUS, (void*)state, strlen (state)); /* Send and cleanup */ ret = snmp_send (session, trap_pdu); if (ret == 0) { /* error */ qb_log(LOG_ERR, "Could not send SNMP trap"); snmp_free_pdu (trap_pdu); } }
/* * send_trap_to_sess: sends a trap to a session but assumes that the * pdu is constructed correctly for the session type. */ void send_trap_to_sess(netsnmp_session * sess, netsnmp_pdu *template_pdu) { netsnmp_pdu *pdu; int result; if (!sess || !template_pdu) return; DEBUGMSGTL(("trap", "sending trap type=%d, version=%d\n", template_pdu->command, sess->version)); #ifndef DISABLE_SNMPV1 if (sess->version == SNMP_VERSION_1 && (template_pdu->command != SNMP_MSG_TRAP)) return; /* Skip v1 sinks for v2 only traps */ #endif template_pdu->version = sess->version; pdu = snmp_clone_pdu(template_pdu); pdu->sessid = sess->sessid; /* AgentX only ? */ if ( template_pdu->command == SNMP_MSG_INFORM #ifdef USING_AGENTX_PROTOCOL_MODULE || template_pdu->command == AGENTX_MSG_NOTIFY #endif ) { result = snmp_async_send(sess, pdu, &handle_inform_response, NULL); } else { result = snmp_send(sess, pdu); } if (result == 0) { snmp_sess_perror("snmpd: send_trap", sess); snmp_free_pdu(pdu); } else { snmp_increment_statistic(STAT_SNMPOUTTRAPS); snmp_increment_statistic(STAT_SNMPOUTPKTS); } }
static void snmp_get_and_print(netsnmp_session * ss, oid * theoid, size_t theoid_len) { netsnmp_pdu *pdu, *response; netsnmp_variable_list *vars; int status; pdu = snmp_pdu_create(SNMP_MSG_GET); snmp_add_null_var(pdu, theoid, theoid_len); status = snmp_synch_response(ss, pdu, &response); if (status == STAT_SUCCESS && response->errstat == SNMP_ERR_NOERROR) { for (vars = response->variables; vars; vars = vars->next_variable) { numprinted++; print_variable(vars->name, vars->name_length, vars); } } if (response) { snmp_free_pdu(response); } }
int agentx_close_session( struct snmp_session *ss, int why ) { struct snmp_pdu *pdu, *response; DEBUGMSGTL(("agentx/subagent","closing session\n")); if (! IS_AGENTX_VERSION( ss->version )) return 0; pdu = snmp_pdu_create(AGENTX_MSG_CLOSE); if ( pdu == NULL ) return 0; pdu->time = 0; pdu->errstat = why; pdu->sessid = ss->sessid; (void) agentx_synch_response(ss, pdu, &response); snmp_free_pdu(response); DEBUGMSGTL(("agentx/subagent","closed\n")); return 1; }
/* * Trap handler for forwarding to another destination */ int forward_handler( netsnmp_pdu *pdu, netsnmp_transport *transport, netsnmp_trapd_handler *handler) { netsnmp_session session, *ss; netsnmp_pdu *pdu2; char buf[BUFSIZ], *cp; DEBUGMSGTL(( "snmptrapd", "forward_handler (%s)\n", handler->token)); snmp_sess_init( &session ); if (strchr( handler->token, ':') == NULL) { snprintf( buf, BUFSIZ, "%s:%d", handler->token, SNMP_TRAP_PORT); cp = buf; } else { cp = handler->token; } session.peername = cp; session.version = pdu->version; ss = snmp_open( &session ); if (!ss) return NETSNMPTRAPD_HANDLER_FAIL; /* XXX: wjh we should be caching sessions here and not always reopening a session. It's very ineffecient, especially with v3 INFORMS which may require engineID probing */ pdu2 = snmp_clone_pdu(pdu); if (pdu2->transport_data) { free(pdu2->transport_data); pdu2->transport_data = NULL; pdu2->transport_data_length = 0; } if (!snmp_send( ss, pdu2 )) { snmp_sess_perror("Forward failed", ss); snmp_free_pdu(pdu2); } snmp_close( ss ); return NETSNMPTRAPD_HANDLER_OK; }
int main(int argc, char ** argv) { struct snmp_session session; struct snmp_session *sess_handle; struct snmp_pdu *pdu; struct snmp_pdu *response; struct variable_list *vars; oid id_oid[MAX_OID_LEN]; oid serial_oid[MAX_OID_LEN]; size_t id_len = MAX_OID_LEN; size_t serial_len = MAX_OID_LEN; int status; struct tree * mib_tree; /*********************/ if(argv[1] == NULL){ printf("Please supply a hostname\n"); exit(1); } init_snmp("Main check"); snmp_sess_init( &session ); session.version = SNMP_VERSION_1; session.community = "public"; session.community_len = strlen(session.community); session.peername = argv[1]; sess_handle = snmp_open(&session); add_mibdir("."); mib_tree = read_mib("mibs/SNMPv2-MIB.txt"); pdu = snmp_pdu_create(SNMP_MSG_GET); read_objid("SNMPv2-MIB::sysDescr.0", id_oid, &id_len); snmp_add_null_var(pdu, id_oid, id_len); read_objid("SNMPv2-MIB::sysObjectID.0", serial_oid, &serial_len); snmp_add_null_var(pdu, serial_oid, serial_len); status = snmp_synch_response(sess_handle, pdu, &response); for(vars = response->variables; vars; vars = vars->next_variable) print_value(vars->name, vars->name_length, vars); snmp_free_pdu(response); snmp_close(sess_handle); return (0); }