SNMP frontend, new preli table handling code
Skipped data set and use table-helper only Specialized SNMP error handling
This commit is contained in:
parent
1a2edb7a2a
commit
b2be7e9e1b
15 changed files with 750 additions and 363 deletions
|
|
@ -59,43 +59,107 @@
|
|||
#include "snmp_lib.h"
|
||||
#include "snmp_handler.h"
|
||||
|
||||
/*! SNMP table operation handlre
|
||||
static int
|
||||
snmp_common_handler(netsnmp_mib_handler *handler,
|
||||
netsnmp_handler_registration *nhreg,
|
||||
netsnmp_agent_request_info *reqinfo,
|
||||
netsnmp_request_info *requests,
|
||||
clixon_snmp_handle **shp,
|
||||
int tablehandler)
|
||||
{
|
||||
int retval = -1;
|
||||
netsnmp_variable_list *requestvb; /* sub of requests */
|
||||
clixon_snmp_handle *sh;
|
||||
char oidstr0[MAX_OID_LEN*2] = {0,};
|
||||
char oidstr1[MAX_OID_LEN*2] = {0,};
|
||||
char oidstr2[MAX_OID_LEN*2] = {0,};
|
||||
|
||||
requestvb = requests->requestvb;
|
||||
if (snprint_objid(oidstr0, sizeof(oidstr0),
|
||||
requestvb->name, requestvb->name_length) < 0){
|
||||
clicon_err(OE_XML, 0, "snprint_objid buffer too small");
|
||||
}
|
||||
if ((sh = (clixon_snmp_handle*)handler->myvoid) != NULL){
|
||||
if (snprint_objid(oidstr1, sizeof(oidstr1),
|
||||
nhreg->rootoid, nhreg->rootoid_len) < 0){
|
||||
clicon_err(OE_XML, 0, "snprint_objid buffer too small");
|
||||
goto done;
|
||||
}
|
||||
if (snprint_objid(oidstr2, sizeof(oidstr2),
|
||||
sh->sh_oid, sh->sh_oidlen) < 0){
|
||||
clicon_err(OE_XML, 0, "snprint_objid buffer too small");
|
||||
goto done;
|
||||
}
|
||||
if (shp)
|
||||
*shp = sh;
|
||||
if (strcmp(oidstr0, oidstr2) == 0)
|
||||
clicon_debug(1, "%s \"%s\" %s inclusive:%d %s", __FUNCTION__,
|
||||
oidstr2,
|
||||
snmp_msg_int2str(reqinfo->mode),
|
||||
requests->inclusive, tablehandler?"table":"");
|
||||
else
|
||||
clicon_debug(1, "%s \"%s\"/\"%s\" %s inclusive:%d %s", __FUNCTION__,
|
||||
oidstr2, oidstr0,
|
||||
snmp_msg_int2str(reqinfo->mode),
|
||||
requests->inclusive, tablehandler?"table":"");
|
||||
}
|
||||
else{
|
||||
assert(0);
|
||||
}
|
||||
|
||||
retval = 0;
|
||||
done:
|
||||
return retval;
|
||||
}
|
||||
|
||||
/*! SNMP table operation handler
|
||||
* Callorder: 161,160,.... 0, 1,2,3, 160,161,...
|
||||
* see https://net-snmp.sourceforge.io/dev/agent/data_set_8c-example.html#_a0
|
||||
*
|
||||
* see table_array.[ch] simplify the task of
|
||||
* writing a table handler for the net-snmp agent when the data being
|
||||
* accessed is in an oid sorted form and must be accessed externally.
|
||||
*
|
||||
* netsnmp_table_build_oid_from_index()
|
||||
*
|
||||
* table_container.[ch]
|
||||
*
|
||||
* build_new_oid
|
||||
* XXX Check expected return values for these netsnmp handler functions
|
||||
*/
|
||||
int
|
||||
snmp_table_handler(netsnmp_mib_handler *handler,
|
||||
netsnmp_handler_registration *nhreg,
|
||||
netsnmp_agent_request_info *reqinfo,
|
||||
netsnmp_request_info *requests)
|
||||
clixon_snmp_table_handler(netsnmp_mib_handler *handler,
|
||||
netsnmp_handler_registration *nhreg,
|
||||
netsnmp_agent_request_info *reqinfo,
|
||||
netsnmp_request_info *requests)
|
||||
{
|
||||
int retval = SNMP_ERR_GENERR;
|
||||
clixon_snmp_handle *sh;
|
||||
netsnmp_table_data_set *table;
|
||||
yang_stmt *ys;
|
||||
clicon_handle h;
|
||||
yang_stmt *ylist;
|
||||
int retval = -1;
|
||||
clixon_snmp_handle *sh = NULL;
|
||||
cvec *nsc = NULL;
|
||||
cxobj *xt = NULL;
|
||||
cbuf *cb = NULL;
|
||||
int ret;
|
||||
|
||||
clicon_debug(1, "%s %s %s", __FUNCTION__,
|
||||
handler->handler_name,
|
||||
snmp_msg_int2str(reqinfo->mode));
|
||||
sh = (clixon_snmp_handle*)nhreg->my_reg_void;
|
||||
ys = sh->sh_ys;
|
||||
h = sh->sh_h;
|
||||
table = sh->sh_table;
|
||||
|
||||
if ((ylist = yang_find(ys, Y_LIST, NULL)) == NULL)
|
||||
goto ok;
|
||||
|
||||
if (clixon_table_create(table, ys, h) < 0)
|
||||
goto done;
|
||||
|
||||
if ((ret = snmp_common_handler(handler, nhreg, reqinfo, requests, &sh, 1)) < 0)
|
||||
goto done;
|
||||
switch(reqinfo->mode){
|
||||
case MODE_GETNEXT: // 160
|
||||
case MODE_GETNEXT:{ // 160
|
||||
#ifdef NOTYET
|
||||
char *oidstr = NULL;
|
||||
oid oid1[MAX_OID_LEN] = {0,};
|
||||
size_t sz1 = MAX_OID_LEN;
|
||||
|
||||
oidstr = ".1.3.6.1.2.1.2.2.1.1.1";
|
||||
if (snmp_parse_oid(oidstr, oid1, &sz1) == NULL){
|
||||
clicon_err(OE_XML, errno, "snmp_parse_oid");
|
||||
goto done;
|
||||
}
|
||||
if (snmp_set_var_objid(requests->requestvb, oid1, sz1) != 0){
|
||||
clicon_err(OE_XML, 0, "snmp_set_var_objid");
|
||||
goto done;
|
||||
}
|
||||
#endif
|
||||
}
|
||||
break;
|
||||
case MODE_GET: // 160
|
||||
case MODE_SET_RESERVE1:
|
||||
|
|
@ -105,10 +169,9 @@ snmp_table_handler(netsnmp_mib_handler *handler,
|
|||
break;
|
||||
|
||||
}
|
||||
ok:
|
||||
// ok:
|
||||
retval = SNMP_ERR_NOERROR;
|
||||
|
||||
done:
|
||||
done:
|
||||
if (xt)
|
||||
xml_free(xt);
|
||||
if (cb)
|
||||
|
|
@ -119,11 +182,19 @@ done:
|
|||
}
|
||||
|
||||
/*! Scalar handler, set a value to clixon
|
||||
* get xpath: see yang2api_path_fmt / api_path2xpath
|
||||
* get xpath: see yang2api_path_fmt / api_path2xpath
|
||||
* @param[in] h Clixon handle
|
||||
* @param[in] ys Yang node
|
||||
* @param[in] cvk Vector of index/Key variables, if any
|
||||
* @param[in] requestvb SNMP variables
|
||||
* @param[in] defaultval
|
||||
* @param[in] reqinfo
|
||||
* @param[in] requests
|
||||
*/
|
||||
static int
|
||||
snmp_scalar_get(clicon_handle h,
|
||||
yang_stmt *ys,
|
||||
cvec *cvk,
|
||||
netsnmp_variable_list *requestvb,
|
||||
char *defaultval,
|
||||
netsnmp_agent_request_info *reqinfo,
|
||||
|
|
@ -146,7 +217,7 @@ snmp_scalar_get(clicon_handle h,
|
|||
if (xml_nsctx_yang(ys, &nsc) < 0)
|
||||
goto done;
|
||||
/* Create xpath from yang (XXX works not for lists) */
|
||||
if (yang2xpath(ys, &xpath) < 0)
|
||||
if (yang2xpath(ys, cvk, &xpath) < 0)
|
||||
goto done;
|
||||
/* Do the backend call */
|
||||
if (clicon_rpc_get(h, xpath, nsc, CONTENT_ALL, -1, &xt) < 0)
|
||||
|
|
@ -163,8 +234,12 @@ snmp_scalar_get(clicon_handle h,
|
|||
*/
|
||||
if ((x = xpath_first(xt, nsc, "%s", xpath)) != NULL){
|
||||
assert(xml_spec(x) == ys);
|
||||
if (type_xml2snmpstr(xml_body(x), ys, &snmpstr) < 0)
|
||||
if ((ret = type_xml2snmpstr(xml_body(x), ys, &snmpstr)) < 0)
|
||||
goto done;
|
||||
if (ret == 0){
|
||||
netsnmp_set_request_error(reqinfo, requests, SNMP_ERR_WRONGVALUE);
|
||||
goto ok;
|
||||
}
|
||||
}
|
||||
else if (defaultval != NULL){
|
||||
if ((snmpstr = strdup(defaultval)) == NULL){
|
||||
|
|
@ -178,7 +253,6 @@ snmp_scalar_get(clicon_handle h,
|
|||
}
|
||||
if (type_yang2asn1(ys, &asn1type) < 0)
|
||||
goto done;
|
||||
|
||||
if ((ret = type_snmpstr2val(snmpstr, asn1type, &snmpval, &snmplen, &reason)) < 0)
|
||||
goto done;
|
||||
if (ret == 0){
|
||||
|
|
@ -189,10 +263,8 @@ snmp_scalar_get(clicon_handle h,
|
|||
/* see snmplib/snmp_client. somewhat indirect
|
||||
*/
|
||||
requestvb->type = asn1type; // ASN_NULL on input
|
||||
if (snmp_set_var_value(requestvb,
|
||||
snmpval,
|
||||
snmplen) != 0){
|
||||
clicon_err(OE_SNMP, 0, "snmp_set_var_value");
|
||||
if ((ret = snmp_set_var_value(requestvb, snmpval, snmplen)) != SNMPERR_SUCCESS){
|
||||
clicon_err(OE_SNMP, ret, "snmp_set_var_value");
|
||||
goto done;
|
||||
}
|
||||
ok:
|
||||
|
|
@ -282,49 +354,20 @@ snmp_scalar_set(clicon_handle h,
|
|||
*
|
||||
*/
|
||||
int
|
||||
snmp_scalar_handler(netsnmp_mib_handler *handler,
|
||||
netsnmp_handler_registration *nhreg,
|
||||
netsnmp_agent_request_info *reqinfo,
|
||||
netsnmp_request_info *requests)
|
||||
clixon_snmp_scalar_handler(netsnmp_mib_handler *handler,
|
||||
netsnmp_handler_registration *nhreg,
|
||||
netsnmp_agent_request_info *reqinfo,
|
||||
netsnmp_request_info *requests)
|
||||
{
|
||||
int retval = -1;
|
||||
clixon_snmp_handle *sh;
|
||||
clixon_snmp_handle *sh = NULL;
|
||||
yang_stmt *ys;
|
||||
int asn1_type;
|
||||
netsnmp_variable_list *requestvb; /* sub of requests */
|
||||
|
||||
/*
|
||||
* can be used to pass information on a per-pdu basis from a
|
||||
* helper to the later handlers
|
||||
netsnmp_agent_request_info *reqinfo,
|
||||
netsnmp_data_list *agent_data;
|
||||
netsnmp_free_agent_data_set()
|
||||
*/
|
||||
requestvb = requests->requestvb;
|
||||
if (0)
|
||||
fprintf(stderr, "%s %s %s\n", __FUNCTION__,
|
||||
handler->handler_name,
|
||||
snmp_msg_int2str(reqinfo->mode)
|
||||
);
|
||||
netsnmp_variable_list *requestvb = requests->requestvb;
|
||||
|
||||
if (0)
|
||||
fprintf(stderr, "inclusive:%d\n",
|
||||
requests->inclusive
|
||||
);
|
||||
clicon_debug(1, "%s %s %s %d", __FUNCTION__,
|
||||
handler->handler_name,
|
||||
snmp_msg_int2str(reqinfo->mode),
|
||||
requests->inclusive);
|
||||
sh = (clixon_snmp_handle*)nhreg->my_reg_void;
|
||||
if (snmp_common_handler(handler, nhreg, reqinfo, requests, &sh, 0) < 0)
|
||||
goto done;
|
||||
ys = sh->sh_ys;
|
||||
// fprint_objid(stderr, nhreg->rootoid, nhreg->rootoid_len);
|
||||
assert(sh->sh_oidlen == requestvb->name_length);
|
||||
assert(requestvb->name_length == nhreg->rootoid_len);
|
||||
assert(snmp_oid_compare(sh->sh_oid, sh->sh_oidlen,
|
||||
requestvb->name, requestvb->name_length) == 0);
|
||||
assert(snmp_oid_compare(requestvb->name, requestvb->name_length,
|
||||
nhreg->rootoid, nhreg->rootoid_len) == 0);
|
||||
|
||||
#if 0 /* If oid match fails */
|
||||
netsnmp_set_request_error(reqinfo, requests,
|
||||
SNMP_NOSUCHOBJECT);
|
||||
|
|
@ -333,7 +376,8 @@ snmp_scalar_handler(netsnmp_mib_handler *handler,
|
|||
/* see net-snmp/agent/snmp_agent.h / net-snmp/library/snmp.h */
|
||||
switch (reqinfo->mode) {
|
||||
case MODE_GET: /* 160 */
|
||||
if (snmp_scalar_get(sh->sh_h, ys, requestvb, sh->sh_default, reqinfo, requests) < 0)
|
||||
if (snmp_scalar_get(sh->sh_h, ys, sh->sh_cvk,
|
||||
requestvb, sh->sh_default, reqinfo, requests) < 0)
|
||||
goto done;
|
||||
break;
|
||||
case MODE_GETNEXT: /* 161 */
|
||||
|
|
|
|||
|
|
@ -43,14 +43,14 @@ extern "C" {
|
|||
/*
|
||||
* Prototypes
|
||||
*/
|
||||
int snmp_table_handler(netsnmp_mib_handler *handler,
|
||||
netsnmp_handler_registration *nhreg,
|
||||
netsnmp_agent_request_info *reqinfo,
|
||||
netsnmp_request_info *requests);
|
||||
int snmp_scalar_handler(netsnmp_mib_handler *handler,
|
||||
netsnmp_handler_registration *nhreg,
|
||||
netsnmp_agent_request_info *reqinfo,
|
||||
netsnmp_request_info *requests);
|
||||
int clixon_snmp_table_handler(netsnmp_mib_handler *handler,
|
||||
netsnmp_handler_registration *nhreg,
|
||||
netsnmp_agent_request_info *reqinfo,
|
||||
netsnmp_request_info *requests);
|
||||
int clixon_snmp_scalar_handler(netsnmp_mib_handler *handler,
|
||||
netsnmp_handler_registration *nhreg,
|
||||
netsnmp_agent_request_info *reqinfo,
|
||||
netsnmp_request_info *requests);
|
||||
|
||||
#endif /* _SNMP_HANDLER_H_ */
|
||||
|
||||
|
|
|
|||
|
|
@ -100,6 +100,7 @@ static const map_str2int snmp_type_map[] = {
|
|||
{"string", ASN_OCTET_STR}, // 4
|
||||
{"enumeration", ASN_INTEGER}, // 2 special case
|
||||
{"uint32", ASN_GAUGE}, // 0x42 / 66
|
||||
{"uint32", ASN_COUNTER}, // 0x41 / 65
|
||||
{"uint64", ASN_COUNTER64}, // 0x46 / 70
|
||||
{"boolean", ASN_INTEGER}, // 2 special case -> enumeration
|
||||
{NULL, -1}
|
||||
|
|
@ -112,8 +113,8 @@ static const map_str2int snmp_msg_map[] = {
|
|||
{"MODE_SET_RESERVE2", MODE_SET_RESERVE2},
|
||||
{"MODE_SET_ACTION", MODE_SET_ACTION},
|
||||
{"MODE_SET_COMMIT", MODE_SET_COMMIT},
|
||||
{"MODE_GET", MODE_GET},
|
||||
{"MODE_GETNEXT", MODE_GETNEXT},
|
||||
{"MODE_GET", MODE_GET}, // 160
|
||||
{"MODE_GETNEXT", MODE_GETNEXT}, // 161
|
||||
{NULL, -1}
|
||||
};
|
||||
|
||||
|
|
@ -132,6 +133,17 @@ snmp_msg_int2str(int msg)
|
|||
return clicon_int2str(snmp_msg_map, msg);
|
||||
}
|
||||
|
||||
/*! Free clixon snmp handler struct
|
||||
*/
|
||||
int
|
||||
snmp_handle_free(clixon_snmp_handle *sh)
|
||||
{
|
||||
if (sh->sh_cvk)
|
||||
cvec_free(sh->sh_cvk);
|
||||
free(sh);
|
||||
return 0;
|
||||
}
|
||||
|
||||
/*! Translate from YANG to SNMP asn1.1 type ids (not value)
|
||||
*
|
||||
* @param[in] ys YANG leaf node
|
||||
|
|
@ -149,20 +161,40 @@ type_yang2asn1(yang_stmt *ys,
|
|||
char *restype; /* resolved type */
|
||||
char *origtype=NULL; /* original type */
|
||||
int at;
|
||||
yang_stmt *ypath;
|
||||
yang_stmt *yref;
|
||||
|
||||
/* Get yang type of leaf and trasnslate to ASN.1 */
|
||||
if (yang_type_get(ys, &origtype, &yrestype, NULL, NULL, NULL, NULL, NULL) < 0)
|
||||
goto done;
|
||||
restype = yrestype?yang_argument_get(yrestype):NULL;
|
||||
/* Special case: leafref, find original type */
|
||||
if (strcmp(restype, "leafref")==0){
|
||||
if ((ypath = yang_find(yrestype, Y_PATH, NULL)) == NULL){
|
||||
clicon_err(OE_YANG, 0, "No path in leafref");
|
||||
goto done;
|
||||
}
|
||||
if (yang_path_arg(ys, yang_argument_get(ypath), &yref) < 0)
|
||||
goto done;
|
||||
if (yang_type_get(yref, &origtype, &yrestype, NULL, NULL, NULL, NULL, NULL) < 0)
|
||||
goto done;
|
||||
restype = yrestype?yang_argument_get(yrestype):NULL;
|
||||
}
|
||||
/* Special case: counter32, maps to same resolved type as gauge32 */
|
||||
if (strcmp(origtype, "counter32")==0){
|
||||
at = ASN_COUNTER;
|
||||
}
|
||||
else if (strcmp(origtype, "object-identifier-128")==0){
|
||||
at = ASN_OBJECT_ID;
|
||||
}
|
||||
/* translate to asn.1 */
|
||||
if ((at = clicon_str2int(snmp_type_map, restype)) < 0){
|
||||
else if ((at = clicon_str2int(snmp_type_map, restype)) < 0){
|
||||
clicon_err(OE_YANG, 0, "No snmp translation for YANG %s type:%s",
|
||||
yang_argument_get(ys), restype);
|
||||
goto done;
|
||||
}
|
||||
if (asn1_type)
|
||||
*asn1_type = at;
|
||||
clicon_debug(1, "%s type:%s", __FUNCTION__, restype);
|
||||
retval = 0;
|
||||
done:
|
||||
return retval;
|
||||
|
|
@ -281,6 +313,9 @@ type_snmp2xml(yang_stmt *ys,
|
|||
/*! Given xml value and YANG,m return corresponding malloced snmp string
|
||||
* There is a special case for enumeration which is integer in snmp, string in YANG
|
||||
* @param[in] xmlstr
|
||||
* @retval 1 OK
|
||||
* @retval 0 Invalid type
|
||||
* @retval -1 Error
|
||||
* @see type_snmp2xml for snmpset
|
||||
*/
|
||||
int
|
||||
|
|
@ -294,6 +329,7 @@ type_xml2snmpstr(char *xmlstr,
|
|||
char *restype; /* resolved type */
|
||||
char *origtype=NULL; /* original type */
|
||||
char *str = NULL;
|
||||
int ret;
|
||||
|
||||
if (snmpstr == NULL){
|
||||
clicon_err(OE_UNIX, EINVAL, "snmpstr");
|
||||
|
|
@ -304,8 +340,10 @@ type_xml2snmpstr(char *xmlstr,
|
|||
goto done;
|
||||
restype = yrestype?yang_argument_get(yrestype):NULL;
|
||||
if (strcmp(restype, "enumeration") == 0){ /* special case for enum */
|
||||
if (yang_enum2valstr(yrestype, xmlstr, &str) < 0)
|
||||
if ((ret = yang_enum2valstr(yrestype, xmlstr, &str)) < 0)
|
||||
goto done;
|
||||
if (ret == 0)
|
||||
goto fail;
|
||||
}
|
||||
/* special case for bool: although smidump translates TruthValue to boolean
|
||||
* and there is an ASN_BOOLEAN constant:
|
||||
|
|
@ -325,10 +363,13 @@ type_xml2snmpstr(char *xmlstr,
|
|||
clicon_err(OE_UNIX, errno, "strdup");
|
||||
goto done;
|
||||
}
|
||||
retval = 0;
|
||||
retval = 1;
|
||||
done:
|
||||
clicon_debug(1, "%s %d", __FUNCTION__, retval);
|
||||
clicon_debug(2, "%s %d", __FUNCTION__, retval);
|
||||
return retval;
|
||||
fail:
|
||||
retval = 0;
|
||||
goto done;
|
||||
}
|
||||
|
||||
/*! Given snmp string value (as translated frm XML) parse into snmp value
|
||||
|
|
@ -368,6 +409,7 @@ type_snmpstr2val(char *snmpstr,
|
|||
if (ret == 0)
|
||||
goto fail;
|
||||
break;
|
||||
case ASN_COUNTER: // 0x41
|
||||
case ASN_GAUGE: // 0x42
|
||||
*snmplen = 4;
|
||||
if ((*snmpval = malloc(*snmplen)) == NULL){
|
||||
|
|
@ -380,6 +422,7 @@ type_snmpstr2val(char *snmpstr,
|
|||
goto fail;
|
||||
|
||||
break;
|
||||
case ASN_OBJECT_ID: // 6
|
||||
case ASN_OCTET_STR: // 4
|
||||
*snmplen = strlen(snmpstr)+1;
|
||||
if ((*snmpval = (u_char*)strdup((snmpstr))) == NULL){
|
||||
|
|
@ -410,7 +453,7 @@ type_snmpstr2val(char *snmpstr,
|
|||
}
|
||||
retval = 1;
|
||||
done:
|
||||
clicon_debug(1, "%s %d", __FUNCTION__, retval);
|
||||
clicon_debug(2, "%s %d", __FUNCTION__, retval);
|
||||
return retval;
|
||||
fail:
|
||||
retval = 0;
|
||||
|
|
@ -419,20 +462,23 @@ type_snmpstr2val(char *snmpstr,
|
|||
|
||||
/*! Construct an xpath from yang statement, internal fn using cb
|
||||
* Recursively construct it to the top.
|
||||
* @param[in] ys Yang statement
|
||||
* @param[out] cb xpath as cbuf
|
||||
* @retval 0 OK
|
||||
* @retval -1 Error
|
||||
* @param[in] ys Yang statement
|
||||
* @param[in] keyvec Array of [name,val]s as a cvec of key name and values
|
||||
* @param[out] cb xpath as cbuf
|
||||
* @retval 0 OK
|
||||
* @retval -1 Error
|
||||
* @see yang2xpath
|
||||
*/
|
||||
static int
|
||||
yang2xpath_cb(yang_stmt *ys,
|
||||
cvec *keyvec,
|
||||
cbuf *cb)
|
||||
{
|
||||
yang_stmt *yp; /* parent */
|
||||
int i;
|
||||
cvec *cvk = NULL; /* vector of index keys */
|
||||
int retval = -1;
|
||||
char *prefix = NULL;
|
||||
|
||||
if ((yp = yang_parent_get(ys)) == NULL){
|
||||
clicon_err(OE_YANG, EINVAL, "yang expected parent %s", yang_argument_get(ys));
|
||||
|
|
@ -441,31 +487,34 @@ yang2xpath_cb(yang_stmt *ys,
|
|||
if (yp != NULL && /* XXX rm */
|
||||
yang_keyword_get(yp) != Y_MODULE &&
|
||||
yang_keyword_get(yp) != Y_SUBMODULE){
|
||||
|
||||
if (yang2xpath_cb(yp, cb) < 0) /* recursive call */
|
||||
if (yang2xpath_cb(yp, keyvec, cb) < 0) /* recursive call */
|
||||
goto done;
|
||||
if (yang_keyword_get(yp) != Y_CHOICE && yang_keyword_get(yp) != Y_CASE){
|
||||
cprintf(cb, "/");
|
||||
}
|
||||
}
|
||||
prefix = yang_find_myprefix(ys);
|
||||
if (yang_keyword_get(ys) != Y_CHOICE && yang_keyword_get(ys) != Y_CASE){
|
||||
cprintf(cb, "%s:", yang_find_myprefix(ys));
|
||||
if (prefix)
|
||||
cprintf(cb, "%s:", prefix);
|
||||
cprintf(cb, "%s", yang_argument_get(ys));
|
||||
}
|
||||
switch (yang_keyword_get(ys)){
|
||||
case Y_LIST: // XXX not xpaths
|
||||
case Y_LIST:
|
||||
cvk = yang_cvec_get(ys); /* Use Y_LIST cache, see ys_populate_list() */
|
||||
if (cvec_len(cvk))
|
||||
cprintf(cb, "=");
|
||||
/* Iterate over individual keys */
|
||||
assert(keyvec && cvec_len(cvk) == cvec_len(keyvec));
|
||||
for (i=0; i<cvec_len(cvk); i++){
|
||||
if (i)
|
||||
cprintf(cb, ",");
|
||||
cprintf(cb, "%%s");
|
||||
cprintf(cb, "[");
|
||||
if (prefix)
|
||||
cprintf(cb, "%s:", prefix);
|
||||
cprintf(cb, "%s='%s']",
|
||||
cv_string_get(cvec_i(cvk, i)),
|
||||
cv_string_get(cvec_i(keyvec, i)));
|
||||
}
|
||||
break;
|
||||
case Y_LEAF_LIST:
|
||||
cprintf(cb, "=%%s");
|
||||
assert(0); // NYI
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
|
|
@ -478,6 +527,7 @@ yang2xpath_cb(yang_stmt *ys,
|
|||
/*! Construct an xpath from yang statement
|
||||
* Recursively construct it to the top.
|
||||
* @param[in] ys Yang statement
|
||||
* @param[in] keyvec Array of [name,val]s as a cvec of key name and values
|
||||
* @param[out] xpath Malloced xpath string, use free() after use
|
||||
* @retval 0 OK
|
||||
* @retval -1 Error
|
||||
|
|
@ -487,6 +537,7 @@ yang2xpath_cb(yang_stmt *ys,
|
|||
*/
|
||||
int
|
||||
yang2xpath(yang_stmt *ys,
|
||||
cvec *keyvec,
|
||||
char **xpath)
|
||||
{
|
||||
int retval = -1;
|
||||
|
|
@ -496,7 +547,7 @@ yang2xpath(yang_stmt *ys,
|
|||
clicon_err(OE_UNIX, errno, "cbuf_new");
|
||||
goto done;
|
||||
}
|
||||
if (yang2xpath_cb(ys, cb) < 0)
|
||||
if (yang2xpath_cb(ys, keyvec, cb) < 0)
|
||||
goto done;
|
||||
if (xpath && (*xpath = strdup(cbuf_get(cb))) == NULL){
|
||||
clicon_err(OE_UNIX, errno, "strdup");
|
||||
|
|
@ -509,25 +560,42 @@ yang2xpath(yang_stmt *ys,
|
|||
return retval;
|
||||
}
|
||||
|
||||
#ifdef NOTUSED
|
||||
/*!
|
||||
* @param[in] ys Yang statement
|
||||
* @param[out] xpath Malloced xpath string, use free() after use
|
||||
* @retval 0 OK
|
||||
* @retval -1 Error
|
||||
*/
|
||||
int
|
||||
clixon_table_create(netsnmp_table_data_set *table, yang_stmt *ys, clicon_handle h)
|
||||
clixon_table_create(netsnmp_table_data_set *table0,
|
||||
yang_stmt *ys,
|
||||
clicon_handle h)
|
||||
{
|
||||
cvec *nsc = NULL;
|
||||
cxobj *xt = NULL;
|
||||
cxobj *xerr;
|
||||
char *xpath;
|
||||
cxobj *xtable;
|
||||
cxobj *xe;
|
||||
cxobj *xleaf;
|
||||
int i;
|
||||
char *valstr;
|
||||
netsnmp_table_row *row, *tmprow;
|
||||
int retval = -1;
|
||||
int retval = -1;
|
||||
cvec *nsc = NULL;
|
||||
cxobj *xt = NULL;
|
||||
cxobj *xerr;
|
||||
char *xpath;
|
||||
cxobj *xtable;
|
||||
cxobj *xe;
|
||||
cxobj *xleaf;
|
||||
char *valstr;
|
||||
netsnmp_table_data *table;
|
||||
netsnmp_table_row *row;
|
||||
netsnmp_table_row *tmprow;
|
||||
int i;
|
||||
int ret;
|
||||
cvec *keyvec = NULL;
|
||||
|
||||
if (table0 == NULL || (table = table0->table) == NULL){
|
||||
clicon_err(OE_UNIX, EINVAL, "table0 /->table is NULL");
|
||||
goto done;
|
||||
}
|
||||
if (xml_nsctx_yang(ys, &nsc) < 0)
|
||||
goto done;
|
||||
|
||||
if (yang2xpath(ys, &xpath) < 0)
|
||||
if (yang2xpath(ys, keyvec, &xpath) < 0)
|
||||
goto done;
|
||||
|
||||
if (clicon_rpc_get(h, xpath, nsc, CONTENT_ALL, -1, &xt) < 0)
|
||||
|
|
@ -538,40 +606,77 @@ clixon_table_create(netsnmp_table_data_set *table, yang_stmt *ys, clicon_handle
|
|||
goto done;
|
||||
}
|
||||
|
||||
#if 1
|
||||
/* Boils down to snmp_varlist_add_variable */
|
||||
if (snmp_varlist_add_variable(&table->indexes_template,
|
||||
NULL,
|
||||
0,
|
||||
ASN_OCTET_STR,
|
||||
NULL,
|
||||
0) == NULL){
|
||||
clicon_err(OE_XML, errno, "snmp_varlist_add_variable");
|
||||
goto done;
|
||||
}
|
||||
if ((ret = netsnmp_table_set_add_default_row(table0, 2, ASN_OCTET_STR, 1, NULL, 0)) != SNMPERR_SUCCESS){
|
||||
clicon_err(OE_SNMP, ret, "netsnmp_table_set_add_default_row");
|
||||
goto done;
|
||||
}
|
||||
if ((ret = netsnmp_table_set_add_default_row(table0, 3, ASN_OCTET_STR, 1, NULL, 0)) != SNMPERR_SUCCESS){
|
||||
clicon_err(OE_SNMP, ret, "netsnmp_table_set_add_default_row");
|
||||
goto done;
|
||||
}
|
||||
#else
|
||||
netsnmp_table_dataset_add_index(table, ASN_OCTET_STR);
|
||||
netsnmp_table_set_multi_add_default_row(table, 2, ASN_OCTET_STR, 1, NULL, 0, 3, ASN_OCTET_STR, 1, NULL, 0, 0);
|
||||
|
||||
netsnmp_table_set_multi_add_default_row(table0, 2, ASN_OCTET_STR, 1, NULL, 0, 3, ASN_OCTET_STR, 1, NULL, 0, 0);
|
||||
#endif
|
||||
if ((xtable = xpath_first(xt, nsc, "%s", xpath)) != NULL) {
|
||||
for (tmprow = table->table->first_row; tmprow; tmprow = tmprow->next)
|
||||
netsnmp_table_dataset_remove_and_delete_row(table, tmprow);
|
||||
for (tmprow = table->first_row; tmprow; tmprow = tmprow->next)
|
||||
netsnmp_table_dataset_remove_and_delete_row(table0, tmprow);
|
||||
|
||||
xe = NULL; /* Loop thru entries in table */
|
||||
while ((xe = xml_child_each(xtable, xe, CX_ELMNT)) != NULL) {
|
||||
row = netsnmp_create_table_data_row();
|
||||
if ((row = netsnmp_create_table_data_row()) == NULL){
|
||||
clicon_err(OE_UNIX, errno, "netsnmp_create_table_data_row");
|
||||
goto done;
|
||||
}
|
||||
xleaf = NULL; /* Loop thru leafs in entry */
|
||||
i = 1; /* tableindex start at 1 */
|
||||
while ((xleaf = xml_child_each(xe, xleaf, CX_ELMNT)) != NULL) {
|
||||
valstr = xml_body(xleaf);
|
||||
if (i == 1) // Assume first netry is key XXX should check YANG
|
||||
if (i == 1){ // Assume first entry is key XXX should check YANG
|
||||
#if 1
|
||||
if ((snmp_varlist_add_variable(&row->indexes, NULL, 0, ASN_OCTET_STR, (const u_char *)valstr, strlen(valstr))) == NULL){
|
||||
clicon_err(OE_XML, errno, "snmp_varlist_add_variable");
|
||||
goto done;
|
||||
}
|
||||
#else
|
||||
netsnmp_table_row_add_index(row, ASN_OCTET_STR, valstr, strlen(valstr));
|
||||
#endif
|
||||
}
|
||||
else{
|
||||
netsnmp_set_row_column(row, i, ASN_OCTET_STR, valstr, strlen(valstr));
|
||||
netsnmp_mark_row_column_writable(row, i, 1);
|
||||
if ((ret = netsnmp_set_row_column(row, i, ASN_OCTET_STR, valstr, strlen(valstr))) != SNMPERR_SUCCESS){
|
||||
clicon_err(OE_SNMP, ret, "netsnmp_set_row_column");
|
||||
goto done;
|
||||
}
|
||||
if ((ret = netsnmp_mark_row_column_writable(row, i, 1)) != SNMPERR_SUCCESS){
|
||||
clicon_err(OE_SNMP, ret, "netsnmp_set_row_column");
|
||||
goto done;
|
||||
}
|
||||
}
|
||||
i++;
|
||||
}
|
||||
|
||||
netsnmp_table_dataset_add_row(table, row);
|
||||
netsnmp_table_dataset_add_row(table0, row);
|
||||
}
|
||||
}
|
||||
|
||||
retval = 1;
|
||||
retval = 0;
|
||||
|
||||
done:
|
||||
if (xt)
|
||||
xml_free(xt);
|
||||
if (nsc)
|
||||
xml_nsctx_free(nsc);
|
||||
|
||||
return retval;
|
||||
}
|
||||
#endif
|
||||
|
|
|
|||
|
|
@ -51,7 +51,7 @@ struct clixon_snmp_handle {
|
|||
oid sh_oid[MAX_OID_LEN]; /* OID for debug, may be removed? */
|
||||
size_t sh_oidlen;
|
||||
char *sh_default; /* MIB default value leaf only */
|
||||
netsnmp_table_data_set *sh_table; /* table struct, table only */
|
||||
cvec *sh_cvk; /* Index/Key variables */
|
||||
};
|
||||
typedef struct clixon_snmp_handle clixon_snmp_handle;
|
||||
|
||||
|
|
@ -60,6 +60,7 @@ typedef struct clixon_snmp_handle clixon_snmp_handle;
|
|||
*/
|
||||
int snmp_access_str2int(char *modes_str);
|
||||
const char *snmp_msg_int2str(int msg);
|
||||
int snmp_handle_free(clixon_snmp_handle *sh);
|
||||
int type_yang2asn1(yang_stmt *ys, int *asn1_type);
|
||||
int type_snmp2xml(yang_stmt *ys,
|
||||
netsnmp_variable_list *requestvb,
|
||||
|
|
@ -67,10 +68,8 @@ int type_snmp2xml(yang_stmt *ys,
|
|||
netsnmp_request_info *requests,
|
||||
char **valstr);
|
||||
int type_xml2snmpstr(char *xmlstr, yang_stmt *ys, char **snmpstr);
|
||||
|
||||
int type_snmpstr2val(char *snmpstr, int asn1type, u_char **snmpval, size_t *snmplen, char **reason);
|
||||
int yang2xpath(yang_stmt *ys, char **xpath);
|
||||
int clixon_table_create(netsnmp_table_data_set *table, yang_stmt *ys, clicon_handle h);
|
||||
int yang2xpath(yang_stmt *ys, cvec *keyvec, char **xpath);
|
||||
|
||||
#endif /* _SNMP_LIB_H_ */
|
||||
|
||||
|
|
|
|||
|
|
@ -98,7 +98,7 @@ clixon_snmp_input_cb(int s,
|
|||
fd_set readfds;
|
||||
// clicon_handle h = (clicon_handle)arg;
|
||||
|
||||
clicon_debug(1, "%s", __FUNCTION__);
|
||||
clicon_debug(2, "%s", __FUNCTION__);
|
||||
FD_ZERO(&readfds);
|
||||
FD_SET(s, &readfds);
|
||||
snmp_read(&readfds);
|
||||
|
|
@ -128,7 +128,7 @@ clixon_snmp_fdset_register(clicon_handle h)
|
|||
|
||||
FD_ZERO(&readfds);
|
||||
if ((nr = snmp_sess_select_info(NULL, &numfds, &readfds, &timeout, &block)) < 0){
|
||||
clicon_err(OE_SNMP, errno, "snmp_select_error");
|
||||
clicon_err(OE_XML, errno, "snmp_select_error");
|
||||
goto done;
|
||||
}
|
||||
for (i=0; i<numfds; i++){
|
||||
|
|
@ -163,7 +163,7 @@ clixon_snmp_subagent(clicon_handle h,
|
|||
netsnmp_ds_set_boolean(NETSNMP_DS_APPLICATION_ID, NETSNMP_DS_AGENT_ROLE, 1);
|
||||
|
||||
if ((sockpath = clicon_option_str(h, "CLICON_SNMP_AGENT_SOCK")) == NULL){
|
||||
clicon_err(OE_SNMP, 0, "CLICON_SNMP_AGENT_SOCK not set");
|
||||
clicon_err(OE_XML, 0, "CLICON_SNMP_AGENT_SOCK not set");
|
||||
goto done;
|
||||
}
|
||||
/* XXX: This should be configurable. */
|
||||
|
|
@ -196,6 +196,36 @@ clixon_snmp_subagent(clicon_handle h,
|
|||
return retval;
|
||||
}
|
||||
|
||||
/* Specialized SNMP error category log/err callback
|
||||
*
|
||||
* This function displays all negative SNMP errors on the form SNMPERR_* that are not SNMPERR_SUCCESS(=0)
|
||||
* There are also positive SNMP errors on the form SNMP_ERR_* which are not properly handled below
|
||||
* @param[in] handle Application-specific handle
|
||||
* @param[in] suberr Application-specific handle
|
||||
* @param[out] cb Read log/error string into this buffer
|
||||
* @note Some SNMP API functions sometimes returns NULL/ptr or other return values that do not fall into
|
||||
* this category, then OE_SNMP should NOT be used.
|
||||
*/
|
||||
static int
|
||||
clixon_snmp_err_cb(void *handle,
|
||||
int suberr,
|
||||
cbuf *cb)
|
||||
{
|
||||
const char *errstr;
|
||||
|
||||
clicon_debug(1, "%s", __FUNCTION__);
|
||||
if (suberr < 0){
|
||||
if ((errstr = snmp_api_errstring(suberr)) == NULL)
|
||||
cprintf(cb, "unknown error %d", suberr);
|
||||
else
|
||||
cprintf(cb, "%s", errstr);
|
||||
}
|
||||
else{ /* See eg SNMP_ERR_* in snmp.h for positive error numbers, are they applicable here? */
|
||||
cprintf(cb, "unknown error %d", suberr);
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
/*! Clean and close all state of netconf process (but dont exit).
|
||||
* Cannot use h after this
|
||||
* @param[in] h Clixon handle
|
||||
|
|
@ -315,6 +345,15 @@ main(int argc,
|
|||
clicon_log_init(__PROGRAM__, dbg?LOG_DEBUG:LOG_INFO, logdst);
|
||||
clicon_debug_init(dbg, NULL);
|
||||
|
||||
/*
|
||||
* Register error category and error/log callbacks for netsnmp special error handling
|
||||
*/
|
||||
if (clixon_err_cat_reg(OE_SNMP, /* category */
|
||||
h, /* handle (can be NULL) */
|
||||
clixon_snmp_err_cb /* log fn */
|
||||
) < 0)
|
||||
goto done;
|
||||
|
||||
yang_init(h);
|
||||
|
||||
/* Find, read and parse configfile */
|
||||
|
|
@ -433,6 +472,9 @@ main(int argc,
|
|||
if (clicon_nsctx_global_set(h, nsctx_global) < 0)
|
||||
goto done;
|
||||
|
||||
if (dbg)
|
||||
clicon_option_dump(h, dbg);
|
||||
|
||||
/* Get session id from backend hello */
|
||||
clicon_session_id_set(h, getpid());
|
||||
|
||||
|
|
@ -445,15 +487,17 @@ main(int argc,
|
|||
goto done;
|
||||
clicon_session_id_set(h, id);
|
||||
|
||||
|
||||
|
||||
/* Init snmp as subagent */
|
||||
if (clixon_snmp_subagent(h, logdst) < 0)
|
||||
goto done;
|
||||
|
||||
/* Init and traverse mib-translated yangs and register callbacks */
|
||||
if (clixon_snmp_traverse_mibyangs(h) < 0)
|
||||
goto done;
|
||||
|
||||
if (dbg)
|
||||
clicon_option_dump(h, dbg);
|
||||
|
||||
/* Write pid-file */
|
||||
if (pidfile_write(pidfile) < 0)
|
||||
goto done;
|
||||
|
|
|
|||
|
|
@ -81,91 +81,6 @@
|
|||
|
||||
#define IETF_YANG_SMIV2_NS "urn:ietf:params:xml:ns:yang:ietf-yang-smiv2"
|
||||
|
||||
/*! Parse smiv2 extensions for YANG container/list
|
||||
*
|
||||
* Typical table:
|
||||
* container x {
|
||||
* smiv2:oid "1.3.6.1.4.1.8072.2.2.1";
|
||||
* list y{
|
||||
*
|
||||
* }
|
||||
* }
|
||||
* @param[in] h Clixon handle
|
||||
* @param[in] ys Mib-Yang node
|
||||
* @retval 0 OK
|
||||
* @retval -1 Error
|
||||
*/
|
||||
static int
|
||||
mib_yang_table(clicon_handle h,
|
||||
yang_stmt *ys)
|
||||
{
|
||||
int retval = SNMP_ERR_GENERR;
|
||||
netsnmp_handler_registration *nhreg;
|
||||
netsnmp_table_data_set *table;
|
||||
char *oidstr = NULL;
|
||||
oid oid1[MAX_OID_LEN] = {0,};
|
||||
size_t sz1 = MAX_OID_LEN;
|
||||
char *name;
|
||||
clixon_snmp_handle *sh;
|
||||
int ret;
|
||||
|
||||
|
||||
/* Get OID from parent container */
|
||||
if (yang_extension_value(ys, "oid", IETF_YANG_SMIV2_NS, NULL, &oidstr) < 0)
|
||||
goto done;
|
||||
if (oidstr == NULL)
|
||||
goto ok;
|
||||
if (snmp_parse_oid(oidstr, oid1, &sz1) == NULL){
|
||||
clicon_err(OE_SNMP, 0, "snmp_parse_oid");
|
||||
goto done;
|
||||
}
|
||||
name = yang_argument_get(ys);
|
||||
|
||||
if ((table = netsnmp_create_table_data_set(name)) == NULL){
|
||||
clicon_err(OE_SNMP, errno, "netsnmp_create_table_data_set");
|
||||
goto done;
|
||||
}
|
||||
/* Userdata to pass around in netsmp callbacks
|
||||
* XXX: not deallocated
|
||||
*/
|
||||
if ((sh = malloc(sizeof(*sh))) == NULL){
|
||||
clicon_err(OE_UNIX, errno, "malloc");
|
||||
goto done;
|
||||
}
|
||||
memset(sh, 0, sizeof(*sh));
|
||||
sh->sh_h = h;
|
||||
sh->sh_ys = ys;
|
||||
sh->sh_table = table;
|
||||
memcpy(sh->sh_oid, oid1, sizeof(oid1));
|
||||
sh->sh_oidlen = sz1;
|
||||
|
||||
if (clixon_table_create(table, ys, h) < 0)
|
||||
goto done;
|
||||
|
||||
if ((nhreg = netsnmp_create_handler_registration(name,
|
||||
snmp_table_handler,
|
||||
oid1,
|
||||
sz1,
|
||||
HANDLER_CAN_RWRITE)) == NULL){
|
||||
clicon_err(OE_SNMP, errno, "netsnmp_create_handler_registration");
|
||||
goto done;
|
||||
}
|
||||
|
||||
nhreg->my_reg_void = sh;
|
||||
|
||||
if ((ret = netsnmp_register_table_data_set(nhreg, table, NULL)) != SNMPERR_SUCCESS){
|
||||
//XXX err return? clicon_err(OE_SNMP, ret, "netsnmp_register_table_data_set");
|
||||
// goto done;
|
||||
|
||||
}
|
||||
(void)netsnmp_register_auto_data_table(table, NULL);
|
||||
|
||||
clicon_debug(1, "%s %s registered", __FUNCTION__, oidstr);
|
||||
ok:
|
||||
retval = 0;
|
||||
done:
|
||||
return retval;
|
||||
}
|
||||
|
||||
/*! Parse smiv2 extensions for YANG leaf
|
||||
* Typical leaf:
|
||||
|
|
@ -174,12 +89,14 @@ mib_yang_table(clicon_handle h,
|
|||
* smiv2:defval "42"; (optional)
|
||||
* @param[in] h Clixon handle
|
||||
* @param[in] ys Mib-Yang node
|
||||
* @param[in] cvk Vector of key/index values. NB: not for scalars, only tables
|
||||
* @retval 0 OK
|
||||
* @retval -1 Error
|
||||
*/
|
||||
static int
|
||||
mib_yang_leaf(clicon_handle h,
|
||||
yang_stmt *ys)
|
||||
yang_stmt *ys,
|
||||
cvec *cvk)
|
||||
{
|
||||
int retval = -1;
|
||||
netsnmp_handler_registration *nhreg = NULL;
|
||||
|
|
@ -193,23 +110,36 @@ mib_yang_leaf(clicon_handle h,
|
|||
int modes;
|
||||
char *name;
|
||||
clixon_snmp_handle *sh;
|
||||
cg_var *cvi;
|
||||
cbuf *cboid = NULL;
|
||||
|
||||
/* Get OID from leaf */
|
||||
if (yang_extension_value(ys, "oid", IETF_YANG_SMIV2_NS, NULL, &oidstr) < 0)
|
||||
goto done;
|
||||
if (oidstr == NULL)
|
||||
goto ok;
|
||||
if (snmp_parse_oid(oidstr, oid1, &sz1) == NULL){
|
||||
clicon_err(OE_SNMP, 0, "snmp_parse_oid");
|
||||
/* Append sub-keys to original oidstr, use cligen-buf
|
||||
*/
|
||||
if ((cboid = cbuf_new()) == NULL){
|
||||
clicon_err(OE_UNIX, errno, "cbuf_new");
|
||||
goto done;
|
||||
}
|
||||
cprintf(cboid, "%s", oidstr);
|
||||
cvi = NULL;
|
||||
while ((cvi = cvec_each(cvk, cvi)) != NULL)
|
||||
cprintf(cboid, ".%s", cv_string_get(cvi));
|
||||
if (snmp_parse_oid(cbuf_get(cboid), oid1, &sz1) == NULL){
|
||||
clicon_err(OE_XML, 0, "snmp_parse_oid(%s)", cbuf_get(cboid));
|
||||
// goto done;
|
||||
goto ok; // XXX skip
|
||||
}
|
||||
if (yang_extension_value(ys, "max-access", IETF_YANG_SMIV2_NS, NULL, &modes_str) < 0)
|
||||
goto done;
|
||||
#if 1 /* Sanity check of types */
|
||||
/* Sanity check of types */
|
||||
if (type_yang2asn1(ys, NULL) < 0)
|
||||
goto done;
|
||||
#endif
|
||||
/* Get modes (access) read-only, read-write, not-accessible, oaccessible-for-notify
|
||||
|
||||
/* Get modes (access) read-only, read-write, not-accessible, accessible-for-notify
|
||||
*/
|
||||
if (modes_str == NULL)
|
||||
goto ok;
|
||||
|
|
@ -222,8 +152,8 @@ mib_yang_leaf(clicon_handle h,
|
|||
|
||||
name = yang_argument_get(ys);
|
||||
|
||||
if ((handler = netsnmp_create_handler(name, snmp_scalar_handler)) == NULL){
|
||||
clicon_err(OE_SNMP, errno, "netsnmp_create_handler");
|
||||
if ((handler = netsnmp_create_handler(name, clixon_snmp_scalar_handler)) == NULL){
|
||||
clicon_err(OE_XML, errno, "netsnmp_create_handler");
|
||||
goto done;
|
||||
}
|
||||
|
||||
|
|
@ -240,29 +170,249 @@ mib_yang_leaf(clicon_handle h,
|
|||
memcpy(sh->sh_oid, oid1, sizeof(oid1));
|
||||
sh->sh_oidlen = sz1;
|
||||
sh->sh_default = default_str;
|
||||
if (cvk &&
|
||||
(sh->sh_cvk = cvec_dup(cvk)) == NULL){
|
||||
clicon_err(OE_UNIX, errno, "cvec_dup");
|
||||
goto done;
|
||||
}
|
||||
if ((nhreg = netsnmp_handler_registration_create(name, handler,
|
||||
oid1, sz1,
|
||||
modes)) == NULL){
|
||||
clicon_err(OE_SNMP, errno, "netsnmp_handler_registration_create");
|
||||
oid1, sz1,
|
||||
modes)) == NULL){
|
||||
clicon_err(OE_XML, errno, "netsnmp_handler_registration_create");
|
||||
netsnmp_handler_free(handler);
|
||||
goto done;
|
||||
}
|
||||
nhreg->my_reg_void =(void*)sh;
|
||||
handler->myvoid =(void*)sh;
|
||||
/*
|
||||
* XXX: nhreg->agent_data
|
||||
*/
|
||||
if ((ret = netsnmp_register_instance(nhreg)) < 0){
|
||||
if ((ret = netsnmp_register_instance(nhreg)) != SNMPERR_SUCCESS){
|
||||
/* XXX Failures are MIB_REGISTRATION_FAILED and MIB_DUPLICATE_REGISTRATION. */
|
||||
clicon_err(OE_SNMP, ret, "netsnmp_register_instance");
|
||||
goto done;
|
||||
}
|
||||
clicon_debug(1, "%s %s registered", __FUNCTION__, cbuf_get(cboid));
|
||||
ok:
|
||||
retval = 0;
|
||||
done:
|
||||
if (cboid)
|
||||
cbuf_free(cboid);
|
||||
return retval;
|
||||
}
|
||||
|
||||
/*! Parse smiv2 extensions for YANG container/list
|
||||
*
|
||||
* Typical table:
|
||||
* container x {
|
||||
* smiv2:oid "1.3.6.1.4.1.8072.2.2.1";
|
||||
* list y{
|
||||
*
|
||||
* }
|
||||
* }
|
||||
* @param[in] h Clixon handle
|
||||
* @param[in] ys Mib-Yang node (container)
|
||||
* @param[in] yl Mib-Yang node (list)
|
||||
* @retval 0 OK
|
||||
* @retval -1 Error
|
||||
*/
|
||||
static int
|
||||
mib_yang_table(clicon_handle h,
|
||||
yang_stmt *ys,
|
||||
yang_stmt *ylist)
|
||||
{
|
||||
int retval = -1;
|
||||
netsnmp_handler_registration *nhreg;
|
||||
char *oidstr = NULL;
|
||||
oid oid1[MAX_OID_LEN] = {0,};
|
||||
size_t sz1 = MAX_OID_LEN;
|
||||
char *name;
|
||||
clixon_snmp_handle *sh;
|
||||
int ret;
|
||||
netsnmp_mib_handler *handler;
|
||||
netsnmp_table_registration_info *table_info=NULL;
|
||||
cvec *cvk = NULL; /* vector of index keys */
|
||||
cg_var *cvi;
|
||||
char *keyname;
|
||||
yang_stmt *yleaf;
|
||||
int asn1type;
|
||||
|
||||
/* Get OID from parent container */
|
||||
if (yang_extension_value(ys, "oid", IETF_YANG_SMIV2_NS, NULL, &oidstr) < 0)
|
||||
goto done;
|
||||
if (oidstr == NULL)
|
||||
goto ok;
|
||||
if (snmp_parse_oid(oidstr, oid1, &sz1) == NULL){
|
||||
clicon_err(OE_XML, errno, "snmp_parse_oid");
|
||||
goto done;
|
||||
}
|
||||
name = yang_argument_get(ys);
|
||||
|
||||
/* Userdata to pass around in netsmp callbacks
|
||||
* XXX: not deallocated
|
||||
*/
|
||||
if ((sh = malloc(sizeof(*sh))) == NULL){
|
||||
clicon_err(OE_UNIX, errno, "malloc");
|
||||
goto done;
|
||||
}
|
||||
memset(sh, 0, sizeof(*sh));
|
||||
sh->sh_h = h;
|
||||
sh->sh_ys = ys;
|
||||
|
||||
memcpy(sh->sh_oid, oid1, sizeof(oid1));
|
||||
sh->sh_oidlen = sz1;
|
||||
|
||||
if ((handler = netsnmp_create_handler(name, clixon_snmp_table_handler)) == NULL){
|
||||
clicon_err(OE_XML, errno, "netsnmp_create_handler");
|
||||
goto done;
|
||||
}
|
||||
if ((nhreg = netsnmp_handler_registration_create(name, handler,
|
||||
oid1, sz1,
|
||||
HANDLER_CAN_RWRITE)) == NULL){
|
||||
clicon_err(OE_XML, errno, "netsnmp_handler_registration_create");
|
||||
netsnmp_handler_free(handler);
|
||||
goto done;
|
||||
}
|
||||
handler->myvoid =(void*)sh;
|
||||
/* See netsnmp_register_table_data_set */
|
||||
if ((table_info = SNMP_MALLOC_TYPEDEF(netsnmp_table_registration_info)) == NULL){
|
||||
clicon_err(OE_UNIX, errno, "SNMP_MALLOC_TYPEDEF");
|
||||
goto done;
|
||||
}
|
||||
/* Keys, go through keys */
|
||||
if ((cvk = yang_cvec_get(ylist)) == NULL){
|
||||
clicon_err(OE_YANG, 0, "No keys");
|
||||
goto done;
|
||||
}
|
||||
cvi = NULL;
|
||||
/* Iterate over individual keys */
|
||||
while ((cvi = cvec_each(cvk, cvi)) != NULL) {
|
||||
keyname = cv_string_get(cvi);
|
||||
if ((yleaf = yang_find(ylist, Y_LEAF, keyname)) == NULL){
|
||||
clicon_err(OE_XML, 0, "List statement \"%s\" has no key leaf \"%s\"",
|
||||
yang_argument_get(ylist), keyname);
|
||||
goto done;
|
||||
}
|
||||
if (type_yang2asn1(yleaf, &asn1type) < 0)
|
||||
// goto done;
|
||||
goto ok; // XXX skip
|
||||
if (snmp_varlist_add_variable(&table_info->indexes,
|
||||
NULL, // oid name
|
||||
0, // oid len
|
||||
asn1type,
|
||||
NULL, // value
|
||||
0) == NULL){
|
||||
clicon_err(OE_XML, errno, "snmp_varlist_add_variable");
|
||||
goto done;
|
||||
}
|
||||
}
|
||||
table_info->min_column = cvec_len(cvk);
|
||||
|
||||
/* Count columns */
|
||||
yleaf = NULL;
|
||||
table_info->max_column = 0;
|
||||
while ((yleaf = yn_each(ylist, yleaf)) != NULL) {
|
||||
if (yang_keyword_get(yleaf) == Y_LEAF)
|
||||
table_info->max_column++;
|
||||
}
|
||||
if ((ret = netsnmp_register_table(nhreg, table_info)) != SNMPERR_SUCCESS){
|
||||
clicon_err(OE_SNMP, ret, "netsnmp_register_table");
|
||||
goto done;
|
||||
}
|
||||
clicon_debug(1, "%s %s registered", __FUNCTION__, oidstr);
|
||||
ok:
|
||||
retval = 0;
|
||||
done:
|
||||
return retval;
|
||||
}
|
||||
|
||||
|
||||
/*! Register table sub-oid:s
|
||||
* This assumes a table contains a set of keys and a list of leafs only
|
||||
* The function makes a query to the datastore and registers all table entries that
|
||||
* currently exists. This means it registers for a static table. If new rows or columns
|
||||
* are created or deleted this will not change the OID registration.
|
||||
* That is, the table registration is STATIC
|
||||
* @param[in] h Clixon handle
|
||||
* @param[in] ys Mib-Yang node (container)
|
||||
* @param[in] ylist Mib-Yang node (list)
|
||||
* @retval 0 OK
|
||||
* @retval -1 Error
|
||||
*/
|
||||
static int
|
||||
mib_traverse_table(clicon_handle h,
|
||||
yang_stmt *ys,
|
||||
yang_stmt *ylist)
|
||||
{
|
||||
int retval = -1;
|
||||
cvec *nsc = NULL;
|
||||
char *xpath;
|
||||
cxobj *xt = NULL;
|
||||
cxobj *xerr;
|
||||
cxobj *xtable;
|
||||
cxobj *xrow;
|
||||
cxobj *xcol;
|
||||
yang_stmt *y;
|
||||
cvec *cvk0;
|
||||
cg_var *cv0;
|
||||
cvec *cvk = NULL; /* vector of index keys */
|
||||
cg_var *cv;
|
||||
int i;
|
||||
cxobj *xi;
|
||||
|
||||
if (xml_nsctx_yang(ys, &nsc) < 0)
|
||||
goto done;
|
||||
if (yang2xpath(ys, NULL, &xpath) < 0)
|
||||
goto done;
|
||||
if (clicon_rpc_get(h, xpath, nsc, CONTENT_ALL, -1, &xt) < 0)
|
||||
goto done;
|
||||
if ((xerr = xpath_first(xt, NULL, "/rpc-error")) != NULL){
|
||||
clixon_netconf_error(xerr, "clicon_rpc_get", NULL);
|
||||
goto done;
|
||||
}
|
||||
if ((xtable = xpath_first(xt, nsc, "%s", xpath)) != NULL) {
|
||||
/* Make a clone of key-list, but replace names with values */
|
||||
if ((cvk0 = yang_cvec_get(ylist)) == NULL){
|
||||
clicon_err(OE_YANG, 0, "No keys");
|
||||
goto done;
|
||||
}
|
||||
xrow = NULL;
|
||||
while ((xrow = xml_child_each(xtable, xrow, CX_ELMNT)) != NULL) {
|
||||
if (cvk){
|
||||
cvec_free(cvk);
|
||||
cvk = NULL;
|
||||
}
|
||||
if ((cvk = cvec_dup(cvk0)) == NULL){
|
||||
clicon_err(OE_UNIX, errno, "cvec_dup");
|
||||
goto done;
|
||||
}
|
||||
for (i=0; i<cvec_len(cvk0); i++){
|
||||
cv0 = cvec_i(cvk0, i);
|
||||
cv = cvec_i(cvk, i);
|
||||
if ((xi = xml_find_type(xrow, NULL, cv_string_get(cv0), CX_ELMNT)) == NULL)
|
||||
break;
|
||||
cv_string_set(cv, xml_body(xi));
|
||||
}
|
||||
if (i<cvec_len(cvk0))
|
||||
continue; /* skip row, not all indexes */
|
||||
xcol = NULL;
|
||||
while ((xcol = xml_child_each(xrow, xcol, CX_ELMNT)) != NULL) {
|
||||
if ((y = xml_spec(xcol)) == NULL)
|
||||
continue;
|
||||
if (mib_yang_leaf(h, y, cvk) < 0)
|
||||
goto done;
|
||||
}
|
||||
}
|
||||
}
|
||||
retval = 0;
|
||||
done:
|
||||
if (cvk)
|
||||
cvec_free(cvk);
|
||||
if (xt)
|
||||
xml_free(xt);
|
||||
if (nsc)
|
||||
xml_nsctx_free(nsc);
|
||||
return retval;
|
||||
}
|
||||
|
||||
/*! Traverse mib-yang tree, identify scalars and tables, register OID and callbacks
|
||||
*
|
||||
* The tree is traversed depth-first, which at least guarantees that a parent is
|
||||
|
|
@ -278,10 +428,11 @@ mib_yang_leaf(clicon_handle h,
|
|||
* smiv2:alias "netSnmpExamples" {
|
||||
* smiv2:oid "1.3.6.1.4.1.8072.2";
|
||||
|
||||
* @param[in] h Clixon handle
|
||||
* @param[in] yn yang node
|
||||
* @retval 0 OK, all nodes traversed
|
||||
* @retval -1 Error, aborted at first error encounter
|
||||
* @param[in] h Clixon handle
|
||||
* @param[in] yn yang node
|
||||
* @param[in] table Yang node is within table
|
||||
* @retval 0 OK, all nodes traversed
|
||||
* @retval -1 Error, aborted at first error encounter
|
||||
*/
|
||||
static int
|
||||
mib_traverse(clicon_handle h,
|
||||
|
|
@ -291,12 +442,10 @@ mib_traverse(clicon_handle h,
|
|||
yang_stmt *ys = NULL;
|
||||
yang_stmt *yp;
|
||||
int ret;
|
||||
enum rfc_6020 keyw;
|
||||
|
||||
keyw = yang_keyword_get(yn);
|
||||
switch(keyw){
|
||||
switch(yang_keyword_get(yn)){
|
||||
case Y_LEAF:
|
||||
if (mib_yang_leaf(h, yn) < 0)
|
||||
if (mib_yang_leaf(h, yn, NULL) < 0)
|
||||
goto done;
|
||||
break;
|
||||
case Y_CONTAINER: /* See list case */
|
||||
|
|
@ -304,9 +453,12 @@ mib_traverse(clicon_handle h,
|
|||
case Y_LIST: /* If parent is container -> identify as table */
|
||||
yp = yang_parent_get(yn);
|
||||
if (yang_keyword_get(yp) == Y_CONTAINER){
|
||||
if (mib_yang_table(h, yp) < 0)
|
||||
/* Specialize table traversal */
|
||||
if (mib_yang_table(h, yp, yn) < 0)
|
||||
goto done;
|
||||
goto ok; /* Dont traverse child leafs further */
|
||||
if (mib_traverse_table(h, yp, yn) < 0)
|
||||
goto done;
|
||||
goto ok;
|
||||
}
|
||||
break;
|
||||
default:
|
||||
|
|
@ -314,15 +466,16 @@ mib_traverse(clicon_handle h,
|
|||
}
|
||||
/* Traverse data nodes in tree (module is special case */
|
||||
ys = NULL;
|
||||
if (yang_schemanode(yn) || keyw == Y_MODULE|| keyw == Y_SUBMODULE)
|
||||
while ((ys = yn_each(yn, ys)) != NULL) {
|
||||
if ((ret = mib_traverse(h, ys)) < 0)
|
||||
goto done;
|
||||
if (ret > 0){
|
||||
retval = ret;
|
||||
goto done;
|
||||
}
|
||||
while ((ys = yn_each(yn, ys)) != NULL) {
|
||||
if (!yang_schemanode(ys))
|
||||
continue;
|
||||
if ((ret = mib_traverse(h, ys)) < 0)
|
||||
goto done;
|
||||
if (ret > 0){
|
||||
retval = ret;
|
||||
goto done;
|
||||
}
|
||||
}
|
||||
ok:
|
||||
retval = 0;
|
||||
done:
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue