next_variable is always NULL in GET-NEXT requests
Matteo Bini <[email protected]> Thu, 22 Aug 2024 18:15:20 +0200
| Newsgroups | gmane.network.net-snmp.user |
|---|---|
| Message-ID | <[email protected]> |
This is a multipart message in MIME format.
------_=_5791982b33e13013197daf7a_=_
Content-Type: text/plain; charset=UTF-8
Content-Disposition: inline
Content-Transfer-Encoding: quoted-printable
Hello Net-SNMP community,
first of all thank you for this amazing free software package!
I'm writing an application that relies on SNMP to exchange data.
Following the documentation and the code samples I've written the client
part that handles GET-NEXT requests, that I will use to display SNMP
tables.
However I'm puzzled by a behaviour difference from snmpgetnext: when I
receive the PDU response, the next_variable of the variable_list struct
is always NULL. However this is not the case, looking at the code for
snmpgetnext.
apps/snmpgetnext.c:189
> status =3D snmp_synch_response(ss, pdu, &response);
> if (status =3D=3D STAT_SUCCESS) {
> if (response->errstat =3D=3D SNMP_ERR_NOERROR) {
> for (vars =3D response->variables; vars;
> vars =3D vars->next_variable)
> print_variable(vars->name, vars->name_length, vars);
This not a big deal, since the answer is correct, but it's not where I
would expect it to be. I read it from the variables pointer, instead of
the next_variable one.
I'm using Net-SNMP version 5.9.3 on Debian GNU/Linux.
I've attached a sample of my code.
Thank you for your input.
--
Matteo Bini
------_=_5791982b33e13013197daf7a_=_
Content-Disposition: attachment; filename=mysnmpgetnext.cpp
Content-Type: text/plain
Content-Transfer-Encoding: quoted-printable
SnmpResponse *SnmpClient::Request(const SnmpRequestType req, const std::str=
ing objid, const SnmpOidType type, const std::string value)
{
void *data;
oid name[MAX_OID_LEN] =3D { 0 };
size_t name_length;
netsnmp_pdu *request, *response;
int pdu_type;
SnmpResponse *retVal =3D NULL;
int status;
char *str;
netsnmp_variable_list *var;
char var_objid[MAX_TEXT_OBJID_SIZE];
response =3D NULL;
if (!session && !Connect()) {
return new SnmpResponse(objid, this->error);
}
if (req =3D=3D SNMP_REQUEST_SET)
pdu_type =3D SNMP_MSG_SET;
else if (req =3D=3D SNMP_REQUEST_GETNEXT)
pdu_type =3D SNMP_MSG_GETNEXT;
else
pdu_type =3D SNMP_MSG_GET;
request =3D snmp_pdu_create(pdu_type);
name_length =3D MAX_OID_LEN;
if (!read_objid(objid.c_str(), name, &name_length)) {
return new SnmpResponse(objid, "Can't read OID " + objid + ".");
}
if (req =3D=3D SNMP_REQUEST_SET)
snmp_add_var(request, name, name_length, type, value.c_str());
else
snmp_add_null_var(request, name, name_length);
status =3D snmp_synch_response(session, request, &response);
if (status =3D=3D STAT_SUCCESS && response->errstat =3D=3D SNMP_ERR_NOERRO=
R && (var =3D response->variables)) {
/* Why is next_variable always NULL?
if (var && req =3D=3D SNMP_REQUEST_GETNEXT) {
var =3D var->next_variable;
}
*/
if (var) {
if (req =3D=3D SNMP_REQUEST_GETNEXT) {
if (snprint_objid(var_objid, MAX_TEXT_OBJID_SIZE, var->name, var->name_=
length) =3D=3D -1) {
throw std::length_error("objid buffer needs more than " + std::to_stri=
ng(MAX_TEXT_OBJID_SIZE) + " bytes.");
}
} else {
if (objid.length() >=3D MAX_TEXT_OBJID_SIZE)
throw std::length_error("objid buffer needs more than " + std::to_stri=
ng(MAX_TEXT_OBJID_SIZE) + " bytes.");
strcpy(var_objid, objid.c_str());
}
if (var->val_len > 0) {
data =3D malloc(sizeof(u_char) * (var->val_len + 1));
memcpy(data, (void *) var->val.bitstring, var->val_len);
str =3D (char *) data;
str[var->val_len] =3D '\0';
retVal =3D new SnmpResponse(std::string(var_objid), data, var->val_len,=
var->type);
} else if (var->val_len =3D=3D 0) {
retVal =3D new SnmpResponse(std::string(var_objid));
} else {
retVal =3D new SnmpResponse(std::string(var_objid), "No data " + objid =
+ ".");
}
}
} else {
if (status =3D=3D STAT_SUCCESS)
retVal =3D new SnmpResponse(snmp_errstring(response->errstat));
else if (status =3D=3D STAT_TIMEOUT)
retVal =3D new SnmpResponse("Timeout: no response from " + peerName + ".=
");
else
retVal =3D new SnmpResponse("Request generic error.");
}
if (response)
snmp_free_pdu(response);
return retVal;
}
------_=_5791982b33e13013197daf7a_=_
Content-Type: text/plain; charset="us-ascii"
MIME-Version: 1.0
Content-Transfer-Encoding: 7bit
Content-Disposition: inline
------_=_5791982b33e13013197daf7a_=_
Content-Type: text/plain; charset="us-ascii"
MIME-Version: 1.0
Content-Transfer-Encoding: 7bit
Content-Disposition: inline
_______________________________________________
Net-snmp-users mailing list
[email protected]
Please see the following page to unsubscribe or change other options:
https://lists.sourceforge.net/lists/listinfo/net-snmp-users
------_=_5791982b33e13013197daf7a_=_--