C-style update: Unified comment, retvals in order, remove trailing spaces

Changed function name for `clicon_debug` functions
This commit is contained in:
Olof hagsand 2023-10-22 18:04:47 +02:00
parent 6e314dd96f
commit 62348fc9c7
204 changed files with 6047 additions and 4904 deletions

View file

@ -97,10 +97,10 @@ snmp_common_handler(netsnmp_mib_handler *handler,
clicon_err(OE_UNIX, errno, "cbuf_new");
goto done;
}
oid_cbuf(cb, (*shp)->sh_oid, (*shp)->sh_oidlen);
oid_cbuf(cb, (*shp)->sh_oid, (*shp)->sh_oidlen);
if (oid_eq(requestvb->name, requestvb->name_length,
(*shp)->sh_oid, (*shp)->sh_oidlen) == 0){ /* equal */
clicon_debug(1, "%s \"%s\" %s inclusive:%d %s", __FUNCTION__,
clixon_debug(CLIXON_DBG_DEFAULT, "%s \"%s\" %s inclusive:%d %s", __FUNCTION__,
cbuf_get(cb),
snmp_msg_int2str(reqinfo->mode),
request->inclusive, tablehandler?"table":"scalar");
@ -110,7 +110,7 @@ snmp_common_handler(netsnmp_mib_handler *handler,
oid_cbuf(cb, requestvb->name, requestvb->name_length);
cprintf(cb, ")");
// nhreg->rootoid same as shp
clicon_debug(1, "%s \"%s\" %s inclusive:%d %s", __FUNCTION__,
clixon_debug(CLIXON_DBG_DEFAULT, "%s \"%s\" %s inclusive:%d %s", __FUNCTION__,
cbuf_get(cb),
snmp_msg_int2str(reqinfo->mode),
request->inclusive, tablehandler?"table":"scalar");
@ -122,7 +122,8 @@ snmp_common_handler(netsnmp_mib_handler *handler,
return retval;
}
/*!
/*! scalar return
*
* @param[in] reqinfo Agent transaction request structure
* @param[in] request The netsnmp request info structure.
* @retval 0 OK
@ -138,7 +139,7 @@ snmp_scalar_return(cxobj *xs,
{
int retval = -1;
int asn1type;
char *xmlstr = NULL;
char *xmlstr = NULL;
char *defaultval = NULL;
u_char *snmpval = NULL;
size_t snmplen = 0;
@ -180,7 +181,7 @@ snmp_scalar_return(cxobj *xs,
if ((ret = type_xml2snmp(xmlstr, &asn1type, &snmpval, &snmplen, &reason)) < 0)
goto done;
if (ret == 0){
clicon_debug(1, "%s %s", __FUNCTION__, reason);
clixon_debug(CLIXON_DBG_DEFAULT, "%s %s", __FUNCTION__, reason);
if ((ret = netsnmp_request_set_error(request, SNMP_ERR_WRONGTYPE)) != SNMPERR_SUCCESS){
clicon_err(OE_SNMP, ret, "netsnmp_request_set_error");
goto done;
@ -210,15 +211,16 @@ snmp_scalar_return(cxobj *xs,
}
/*! Scalar handler, set a value to clixon
*
* 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] defaultval Default value
* @param[in] reqinfo Agent transaction request structure
* @param[in] request The netsnmp request info structure.
* @param[in] request The netsnmp request info structure.
* @retval 0 OK
* @retval -1 Error
* @retval -1 Error
*/
static int
snmp_scalar_get(clicon_handle h,
@ -244,7 +246,7 @@ snmp_scalar_get(clicon_handle h,
cxobj *xcache = NULL;
char *body = NULL;
clicon_debug(1, "%s", __FUNCTION__);
clixon_debug(CLIXON_DBG_DEFAULT, "%s", __FUNCTION__);
/* Prepare backend call by constructing namespace context */
if (xml_nsctx_yang(ys, &nsc) < 0)
goto done;
@ -266,7 +268,7 @@ snmp_scalar_get(clicon_handle h,
}
x = xpath_first(xt, nsc, "%s", xpath);
}
/*
/*
* The xml to snmp value conversion is done in two steps:
* 1. From XML to SNMP string, there is a special case for enumeration, and for default value
* 2. From SNMP string to SNMP binary value which invloves parsing
@ -298,7 +300,7 @@ snmp_scalar_get(clicon_handle h,
if ((ret = type_xml2snmp(xmlstr, &asn1type, &snmpval, &snmplen, &reason)) < 0)
goto done;
if (ret == 0){
clicon_debug(1, "%s %s", __FUNCTION__, reason);
clixon_debug(CLIXON_DBG_DEFAULT, "%s %s", __FUNCTION__, reason);
if ((ret = netsnmp_request_set_error(request, SNMP_ERR_WRONGTYPE)) != SNMPERR_SUCCESS){
clicon_err(OE_SNMP, ret, "netsnmp_request_set_error");
goto done;
@ -344,7 +346,7 @@ snmp_yang2xml(cxobj *xtop,
int i;
int ret;
yang_stmt *yspec;
yspec = ys_spec(ys);
if (yang2api_path_fmt(ys, 0, &api_path_fmt) < 0)
goto done;
@ -374,15 +376,16 @@ snmp_yang2xml(cxobj *xtop,
return retval;
}
/*! Scalar handler, get a value from clixon
/*! Scalar handler, get a value from clixon
*
* @param[in] h Clixon handle
* @param[in] ys Yang node
* @param[in] cvk Vector of index/Key variables, if any
* @param[in] valstr0 Pre-set value string, ignore requestvb
* @param[in] reqinfo Agent transaction request structure
* @param[in] request The netsnmp request info structure.
* @param[in] request The netsnmp request info structure.
* @retval 0 OK
* @retval -1 Error
* @retval -1 Error
* @note contains special logic for rowstatus handling
*/
static int
@ -404,13 +407,13 @@ snmp_scalar_set(clicon_handle h,
int asn1_type;
enum operation_type op = OP_MERGE;
clicon_debug(1, "%s", __FUNCTION__);
clixon_debug(CLIXON_DBG_DEFAULT, "%s", __FUNCTION__);
if ((xtop = xml_new(NETCONF_INPUT_CONFIG, NULL, CX_ELMNT)) == NULL)
goto done;
if (snmp_yang2xml(xtop, ys, cvk, &xbot) < 0)
goto done;
if ((xb = xml_new("body", xbot, CX_BODY)) == NULL)
goto done;
goto done;
/* Extended */
if (type_yang2asn1(ys, &asn1_type, 1) < 0)
goto done;
@ -473,7 +476,7 @@ snmp_cache_row_op(clicon_handle h,
cxobj *xbot = NULL;
cxobj *xa;
cxobj *xcache;
if (xml_nsctx_yang(yp, &nsc) < 0)
goto done;
/* Create xpath from yang */
@ -499,7 +502,7 @@ snmp_cache_row_op(clicon_handle h,
if ((cb = cbuf_new()) == NULL){
clicon_err(OE_UNIX, errno, "cbuf_new");
goto done;
}
}
if (clixon_xml2cbuf(cb, xtop, 0, 0, NULL, -1, 0) < 0)
goto done;
if (clicon_rpc_edit_config(h, "candidate", OP_NONE, cbuf_get(cb)) < 0)
@ -552,8 +555,8 @@ snmp_cache_set(clicon_handle h,
yang_stmt *yspec;
int isrowstatus = 0;
cxobj *xcache = NULL;
clicon_debug(1, "%s", __FUNCTION__);
clixon_debug(CLIXON_DBG_DEFAULT, "%s", __FUNCTION__);
if ((yspec = clicon_dbspec_yang(h)) == NULL){
clicon_err(OE_FATAL, 0, "No DB_SPEC");
goto done;
@ -563,7 +566,7 @@ snmp_cache_set(clicon_handle h,
if (snmp_yang2xml(xtop, ys, cvk, &xbot) < 0)
goto done;
if ((xb = xml_new("body", xbot, CX_BODY)) == NULL)
goto done;
goto done;
/* Extended */
if (type_yang2asn1(ys, &asn1_type, 1) < 0)
goto done;
@ -596,8 +599,8 @@ snmp_cache_set(clicon_handle h,
if (snmp_cache_row_op(h, yang_parent_get(ys), cvk, "merge", 1) < 0)
goto done;
}
else if (strcmp(valstr, "destroy") == 0){
clicon_debug(1, "%s %d", __FUNCTION__, rowstatus);
else if (strcmp(valstr, "destroy") == 0){
clixon_debug(CLIXON_DBG_DEFAULT, "%s %d", __FUNCTION__, rowstatus);
/* Dont send delete to backend if notInService(2) */
if (snmp_cache_row_op(h, yang_parent_get(ys), cvk, "delete", rowstatus!=2) < 0)
goto done;
@ -640,7 +643,7 @@ snmp_cache_set(clicon_handle h,
* @param[in] cvk Vector of index/Key variables, if any
* @param[out] rowstatus Enmu rowstatus: 0 invalid, 1 active, etc
* @retval 0 OK
* @retval -1 Error
* @retval -1 Error
*/
static int
snmp_table_rowstatus_get(clicon_handle h,
@ -658,8 +661,8 @@ snmp_table_rowstatus_get(clicon_handle h,
char *body;
char *intstr;
char *reason = NULL;
clicon_debug(1, "%s", __FUNCTION__);
clixon_debug(CLIXON_DBG_DEFAULT, "%s", __FUNCTION__);
/* Prepare backend call by constructing namespace context */
if (xml_nsctx_yang(ys, &nsc) < 0)
goto done;
@ -672,14 +675,14 @@ snmp_table_rowstatus_get(clicon_handle h,
if ((ret = yang_enum2valstr(yrestype, body, &intstr)) < 0)
goto done;
if (ret == 0){
clicon_debug(1, "%s %s invalid or not found", __FUNCTION__, body);
clixon_debug(CLIXON_DBG_DEFAULT, "%s %s invalid or not found", __FUNCTION__, body);
*rowstatus = 0;
}
else {
if ((ret = parse_int32(intstr, rowstatus, &reason)) < 0)
goto done;
if (ret == 0){
clicon_debug(1, "%s parse_int32: %s", __FUNCTION__, reason);
clixon_debug(CLIXON_DBG_DEFAULT, "%s parse_int32: %s", __FUNCTION__, reason);
*rowstatus = 0;
}
}
@ -718,7 +721,7 @@ clixon_snmp_scalar_handler1(netsnmp_mib_handler *handler,
netsnmp_variable_list *requestvb = request->requestvb;
int ret;
clicon_debug(CLIXON_DBG_DETAIL, "%s", __FUNCTION__);
clixon_debug(CLIXON_DBG_DETAIL, "%s", __FUNCTION__);
if (snmp_common_handler(handler, nhreg, reqinfo, request, 0, &sh) < 0)
goto done;
/* see net-snmp/agent/snmp_agent.h / net-snmp/library/snmp.h */
@ -740,7 +743,7 @@ clixon_snmp_scalar_handler1(netsnmp_mib_handler *handler,
if (type_yang2asn1(sh->sh_ys, &asn1_type, 0) < 0)
goto done;
if (requestvb->type != asn1_type){
clicon_debug(1, "%s Expected type:%d, got: %d", __FUNCTION__, requestvb->type, asn1_type);
clixon_debug(CLIXON_DBG_DEFAULT, "%s Expected type:%d, got: %d", __FUNCTION__, requestvb->type, asn1_type);
if ((ret = netsnmp_request_set_error(request, SNMP_ERR_WRONGTYPE)) != SNMPERR_SUCCESS){
clicon_err(OE_SNMP, ret, "netsnmp_request_set_error");
goto ok;
@ -753,7 +756,7 @@ clixon_snmp_scalar_handler1(netsnmp_mib_handler *handler,
if (snmp_scalar_set(sh->sh_h, sh->sh_ys, NULL, NULL, reqinfo, request) < 0)
goto done;
/*
* There does not seem to be a separate validation action and commit does not
* There does not seem to be a separate validation action and commit does not
* return an error.
* Therefore validation is done here directly as well as discard if it fails.
*/
@ -781,13 +784,13 @@ clixon_snmp_scalar_handler1(netsnmp_mib_handler *handler,
break;
case MODE_SET_UNDO: /* 5 */
if (clicon_rpc_discard_changes(sh->sh_h) < 0)
goto done;
goto done;
break;
}
ok:
retval = SNMP_ERR_NOERROR;
done:
clicon_debug(1, "%s retval:%d", __FUNCTION__, retval);
clixon_debug(CLIXON_DBG_DEFAULT, "%s retval:%d", __FUNCTION__, retval);
return retval;
}
@ -797,6 +800,8 @@ clixon_snmp_scalar_handler1(netsnmp_mib_handler *handler,
* @param[in] nhreg Root registration info.
* @param[in] reqinfo Agent transaction request structure
* @param[in] requests The netsnmp request info structure.
* @retval SNMP_ERR_NOERROR OK
* @retval -1 Error
* @see clixon_snmp_table_handler
*/
int
@ -809,7 +814,7 @@ clixon_snmp_scalar_handler(netsnmp_mib_handler *handler,
netsnmp_request_info *req;
int ret;
clicon_debug(1, "%s", __FUNCTION__);
clixon_debug(CLIXON_DBG_DEFAULT, "%s", __FUNCTION__);
for (req = requests; req; req = req->next){
ret = clixon_snmp_scalar_handler1(handler, nhreg, reqinfo, req);
if (ret != SNMP_ERR_NOERROR){
@ -836,9 +841,9 @@ clixon_snmp_scalar_handler(netsnmp_mib_handler *handler,
* @param[in] oidslen OID length of scalar
* @param[in] reqinfo Agent transaction request structure
* @param[in] request The netsnmp request info structure.
* @retval -1 Error
* @retval 0 Object not found
* @retval 1 OK
* @retval 0 Object not found
* @retval -1 Error
*/
static int
snmp_table_get(clicon_handle h,
@ -889,7 +894,7 @@ snmp_table_get(clicon_handle h,
*/
if (yang_extension_value_opt(ys, "smiv2:defval", NULL, &defaultval) < 0)
goto done;
/* Create xpath with right keys from later part of OID
* Inverse of snmp_str2oid
*/
@ -906,12 +911,12 @@ snmp_table_get(clicon_handle h,
oidi = oids+oidtlen+1;
/* Add keys */
for (i=0; i<cvec_len(cvk_val); i++){
cv = cvec_i(cvk_val, i);
cv = cvec_i(cvk_val, i);
if ((yk = yang_find(yt, Y_LEAF, cv_string_get(cv))) == NULL){
clicon_err(OE_YANG, 0, "List key %s not found", cv_string_get(cv));
goto done;
}
if (snmp_oid2str(&oidi, &oidilen, yk, cv) < 0)
if (snmp_oid2str(&oidi, &oidilen, yk, cv) < 0)
goto done;
}
if (oidilen != 0){
@ -938,6 +943,7 @@ snmp_table_get(clicon_handle h,
}
/*! Set value in table
*
* Get yang of leaf from first part of OID
* Create xpath with right keys from later part of OID
* Query clixon if object exists, if so return value
@ -948,9 +954,9 @@ snmp_table_get(clicon_handle h,
* @param[in] reqinfo Agent transaction request structure
* @param[in] request The netsnmp request info structure.
* @param[out] err Error code if failed (retval = 0)
* @retval -1 Error
* @retval 0 Failed, err set
* @retval 1 OK
* @retval 0 Failed, err set
* @retval -1 Error
*/
static int
snmp_table_set(clicon_handle h,
@ -1047,7 +1053,7 @@ snmp_table_set(clicon_handle h,
goto done;
requestvb = request->requestvb;
if (requestvb->type != asn1_type){
clicon_debug(1, "%s Expected type:%d, got: %d", __FUNCTION__, requestvb->type, asn1_type);
clixon_debug(CLIXON_DBG_DEFAULT, "%s Expected type:%d, got: %d", __FUNCTION__, requestvb->type, asn1_type);
if ((ret = netsnmp_request_set_error(request, SNMP_ERR_WRONGTYPE)) != SNMPERR_SUCCESS){
clicon_err(OE_SNMP, ret, "netsnmp_request_set_error");
goto ok;
@ -1069,12 +1075,12 @@ snmp_table_set(clicon_handle h,
oidi = oids+oidtlen+1;
/* Add keys */
for (i=0; i<cvec_len(cvk_val); i++){
cv = cvec_i(cvk_val, i);
cv = cvec_i(cvk_val, i);
if ((yk = yang_find(yt, Y_LEAF, cv_string_get(cv))) == NULL){
clicon_err(OE_YANG, 0, "List key %s not found", cv_string_get(cv));
goto done;
}
if (snmp_oid2str(&oidi, &oidilen, yk, cv) < 0)
if (snmp_oid2str(&oidi, &oidilen, yk, cv) < 0)
goto done;
}
if (oidilen != 0){
@ -1090,7 +1096,7 @@ snmp_table_set(clicon_handle h,
cvk_val,
&rowstatus)) < 0)
goto done;
}
}
else{
/* If no rowstatus object, default to active */
rowstatus = 1;
@ -1152,6 +1158,7 @@ snmp_table_set(clicon_handle h,
}
/*! Find "next" object from oids minus key and return that.
*
* @param[in] h Clixon handle
* @param[in] ylist Yang of table (of list type)
* @param[in] oids OID of ultimate scalar value
@ -1189,12 +1196,12 @@ snmp_table_getnext(clicon_handle h,
size_t oidklen = MAX_OID_LEN;
oid oidnext[MAX_OID_LEN] = {0x7fffffff,}; /* Next oid: start with high value */
size_t oidnextlen = MAX_OID_LEN;
int found = 0;
int found = 0;
cxobj *xnext = NULL;
yang_stmt *ynext = NULL;
cbuf *cb = NULL;
clicon_debug(1, "%s", __FUNCTION__);
clixon_debug(CLIXON_DBG_DEFAULT, "%s", __FUNCTION__);
if ((ys = yang_parent_get(ylist)) == NULL ||
yang_keyword_get(ys) != Y_CONTAINER){
clicon_err(OE_YANG, EINVAL, "ylist parent is not list");
@ -1256,7 +1263,7 @@ snmp_table_getnext(clicon_handle h,
goto done;
}
oid_cbuf(cb, oidnext, oidnextlen);
clicon_debug(1, "%s next: %s", __FUNCTION__, cbuf_get(cb));
clixon_debug(CLIXON_DBG_DEFAULT, "%s next: %s", __FUNCTION__, cbuf_get(cb));
}
retval = found;
done:
@ -1267,7 +1274,7 @@ snmp_table_getnext(clicon_handle h,
if (xt)
xml_free(xt);
if (nsc)
xml_nsctx_free(nsc);
xml_nsctx_free(nsc);
return retval;
}
@ -1277,6 +1284,8 @@ snmp_table_getnext(clicon_handle h,
* @param[in] nhreg Root registration info.
* @param[in] reqinfo Agent transaction request structure
* @param[in] request The netsnmp request info structure.
* @retval 0 OK
* @retval -1 Error
*/
static int
clixon_snmp_table_handler1(netsnmp_mib_handler *handler,
@ -1292,12 +1301,12 @@ clixon_snmp_table_handler1(netsnmp_mib_handler *handler,
int ret;
netsnmp_variable_list *requestvb;
int err = 0;
clicon_debug(CLIXON_DBG_DETAIL, "%s", __FUNCTION__);
clixon_debug(CLIXON_DBG_DETAIL, "%s", __FUNCTION__);
if ((ret = snmp_common_handler(handler, nhreg, reqinfo, request, 1, &sh)) < 0)
goto done;
if (sh->sh_ys == NULL){
clicon_debug(1, "%s Error table not registered", __FUNCTION__);
clixon_debug(CLIXON_DBG_DEFAULT, "%s Error table not registered", __FUNCTION__);
goto ok;
}
requestvb = request->requestvb;
@ -1306,7 +1315,7 @@ clixon_snmp_table_handler1(netsnmp_mib_handler *handler,
/* Create xpath from YANG table OID + 1 + n + cvk/key = requestvb->name
*/
if ((ret = snmp_table_get(sh->sh_h, sh->sh_ys,
sh->sh_oid2, sh->sh_oid2len,
sh->sh_oid2, sh->sh_oid2len,
requestvb->name, requestvb->name_length,
reqinfo, request)) < 0)
goto done;
@ -1315,7 +1324,7 @@ clixon_snmp_table_handler1(netsnmp_mib_handler *handler,
clicon_err(OE_SNMP, ret, "netsnmp_request_set_error");
goto done;
}
clicon_debug(1, "%s Nosuchinstance", __FUNCTION__);
clixon_debug(CLIXON_DBG_DEFAULT, "%s Nosuchinstance", __FUNCTION__);
}
break;
case MODE_GETNEXT: // 161
@ -1330,13 +1339,13 @@ clixon_snmp_table_handler1(netsnmp_mib_handler *handler,
clicon_err(OE_SNMP, ret, "netsnmp_request_set_error");
goto done;
}
clicon_debug(1, "%s No such object", __FUNCTION__);
clixon_debug(CLIXON_DBG_DEFAULT, "%s No such object", __FUNCTION__);
}
break;
case MODE_SET_RESERVE1: // 0
if (!yang_config_ancestor(sh->sh_ys)){
netsnmp_request_set_error(request, SNMP_ERR_NOTWRITABLE);
goto done;;
goto done;
}
// Check types: compare type in requestvb to yang type (or do later)
break;
@ -1352,7 +1361,7 @@ clixon_snmp_table_handler1(netsnmp_mib_handler *handler,
clicon_err(OE_SNMP, ret, "netsnmp_request_set_error");
goto done;
}
clicon_debug(1, "%s Nosuchinstance", __FUNCTION__);
clixon_debug(CLIXON_DBG_DEFAULT, "%s Nosuchinstance", __FUNCTION__);
}
/*
* There does not seem to be a separate validation action and commit does not
@ -1380,7 +1389,7 @@ clixon_snmp_table_handler1(netsnmp_mib_handler *handler,
break;
case MODE_SET_UNDO : // 5
if (clicon_rpc_discard_changes(sh->sh_h) < 0)
goto done;
goto done;
break;
}
ok:
@ -1401,6 +1410,8 @@ clixon_snmp_table_handler1(netsnmp_mib_handler *handler,
* @param[in] nhreg Root registration info.
* @param[in] reqinfo Agent transaction request structure
* @param[in] requests The netsnmp request info structure.
* @retval 0 OK
* @retval -1 Error
* @see clixon_snmp_scalar_handler
*/
int
@ -1413,7 +1424,7 @@ clixon_snmp_table_handler(netsnmp_mib_handler *handler,
netsnmp_request_info *req;
int ret;
clicon_debug(1, "%s", __FUNCTION__);
clixon_debug(CLIXON_DBG_DEFAULT, "%s", __FUNCTION__);
for (req = requests; req; req = req->next){
ret = clixon_snmp_table_handler1(handler, nhreg, reqinfo, req);
if (ret != SNMP_ERR_NOERROR){

View file

@ -217,6 +217,7 @@ yang_type_to_snmp(yang_stmt *ytype,
}
/*! Translate from snmp string to int representation
*
* @note Internal snmpd, maybe find something in netsnmpd?
*/
int
@ -237,8 +238,8 @@ snmp_msg_int2str(int msg)
* @param[in] objid0len Length of first OID vector
* @param[in] objid1 Second OID vector
* @param[in] objid1len Length of second OID vector
* @retval 0 Equal
* @retval !=0 Not equal, see man memcmp
* @retval 0 Equal
* @retval !=0 Not equal, see man memcmp
* Should really be netsnmp lib function, but cant find any?
*/
int
@ -268,6 +269,7 @@ oid_eq(const oid *objid0,
}
/*! Append a second OID to a first
*
* @param[in,out] objid0 First OID vector
* @param[in,out] objid0len Length of first OID vector
* @param[in] objid1 Second OID vector
@ -295,6 +297,7 @@ oid_append(const oid *objid0,
}
/*! Print objid to file
*
* @see fprint_objid but prints symbolic
*/
int
@ -359,7 +362,7 @@ snmp_yang_type_get(yang_stmt *ys,
}
if (yang_path_arg(ys, yang_argument_get(ypath), &yref) < 0)
goto done;
if (yref == NULL){
if (yref == NULL){
clicon_err(OE_YANG, 0, "No referred YANG node found for leafref path %s", yang_argument_get(ypath));
goto done;
}
@ -401,7 +404,7 @@ snmp_yang_type_get(yang_stmt *ys,
* @param[out] exist The extension exists.
* @param[out] value clispec operator (hide/none) - direct pointer into yang, dont free
* @retval 0 OK: Look in exist and value for return value
* @retval -1 Error
* @retval -1 Error
*
* @note This optimizatoin may not work if the unknown statements are augmented in ys.
* @see yang_extension_value for the generic function
@ -443,6 +446,7 @@ yang_extension_value_opt(yang_stmt *ys,
}
/*! Given a YANG node, return SMIv2 oid extension as OID
*
* @param[in] yn Yang node
* @param[out] objid OID vector, assume allocated with MAX_OID_LEN > oidlen
* @param[out] objidlen Length of OID vector on return
@ -461,7 +465,7 @@ yangext_oid_get(yang_stmt *yn,
int exist = 0;
char *oidstr = NULL;
yang_stmt *yref = NULL;
if (yang_keyword_get(yn) == Y_LEAF){
if (snmp_yang_type_get(yn, &yref, NULL, NULL, NULL) < 0)
goto done;
@ -472,7 +476,7 @@ yangext_oid_get(yang_stmt *yn,
if (yang_extension_value_opt(yref, "smiv2:oid", &exist, &oidstr) < 0)
goto done;
if (exist == 0 || oidstr == NULL){
clicon_debug(1, "OID not found as SMIv2 yang extension of %s", yang_argument_get(yref));
clixon_debug(CLIXON_DBG_DEFAULT, "OID not found as SMIv2 yang extension of %s", yang_argument_get(yref));
goto fail;
}
if (snmp_parse_oid(oidstr, objid, objidlen) == NULL){
@ -490,22 +494,23 @@ yangext_oid_get(yang_stmt *yn,
}
/*! Given a YANG node, return 1 if leaf has oid directive in it, otherwise 0
*
* @param[in] yn Yang node
* @retval 1 found
* @retval 0 not found
*/
int
yangext_is_oid_exist(yang_stmt *yn) {
yangext_is_oid_exist(yang_stmt *yn)
{
int exist = 0;
char *oidstr = NULL;
if ((yang_keyword_get(yn) != Y_LEAF) ||
(yang_extension_value_opt(yn, "smiv2:oid", &exist, &oidstr) < 0) ||
(exist == 0) ||
(oidstr == NULL)) {
(yang_extension_value_opt(yn, "smiv2:oid", &exist, &oidstr) < 0) ||
(exist == 0) ||
(oidstr == NULL)) {
return 0;
}
}
else {
return 1;
}
@ -513,6 +518,7 @@ yangext_is_oid_exist(yang_stmt *yn) {
/*! Duplicate clixon snmp handler struct
*
* Use signature of libnetsnmp data_clone field of netsnmp_mib_handler in agent_handler.h
* @param[in] arg
*/
@ -538,6 +544,7 @@ snmp_handle_clone(void *arg)
}
/*! Free clixon snmp handler struct
*
* Use signature of libnetsnmp data_free field of netsnmp_mib_handler in agent_handler.h
* @param[in] arg
*/
@ -564,8 +571,8 @@ snmp_handle_free(void *arg)
* @param[in] ys YANG leaf node
* @param[out] asn1_type ASN.1 type id
* @param[in] extended Special case clixon extended types used in xml<->asn1 data conversions
* @retval 0 OK
* @retval -1 Error
* @retval 0 OK
* @retval -1 Error
* @see type_yang2snmp, yang only
* @note there are some special cases where extended clixon asn1-types are used to convey info
* to type_snmpstr2val, these types are prefixed with CLIXON_ASN_
@ -602,7 +609,7 @@ type_yang2asn1(yang_stmt *ys,
char *display_hint = NULL;
yrp = yang_parent_get(yrestype);
if (yang_extension_value_opt(yrp, "smiv2:display-hint", NULL, &display_hint) < 0)
goto done;
goto done;
/* RFC2578/2579 but maybe all strings with display-hint should use this, eg exist>0? */
if (display_hint &&
@ -628,7 +635,7 @@ type_yang2asn1(yang_stmt *ys,
* @param[out] valstr Clixon/yang/xml string value, free after use)
* @retval 1 OK, and valstr set
* @retval 0 Invalid value or type
* @retval -1 Error
* @retval -1 Error
* @see type_xml2snmp for snmpget
*/
int
@ -648,7 +655,7 @@ type_snmp2xml(yang_stmt *ys,
yang_stmt *yrestype = NULL;
int ret;
clicon_debug(1, "%s", __FUNCTION__);
clixon_debug(CLIXON_DBG_DEFAULT, "%s", __FUNCTION__);
if (valstr == NULL){
clicon_err(OE_UNIX, EINVAL, "valstr is NULL");
goto done;
@ -668,7 +675,7 @@ type_snmp2xml(yang_stmt *ys,
cvtype = cv_str2type(cvstr);
if ((cv = cv_new(cvtype)) == NULL){
clicon_err(OE_UNIX, errno, "cv_new");
goto done;
goto done;
}
switch (*asn1type){
case CLIXON_ASN_ROWSTATUS:
@ -706,7 +713,7 @@ type_snmp2xml(yang_stmt *ys,
break;
case ASN_IPADDRESS:{
struct in_addr addr;
memcpy(&addr.s_addr, requestvb->val.string, 4);
memcpy(&addr.s_addr, requestvb->val.string, 4);
cv_string_set(cv, inet_ntoa(addr));
break;
}
@ -716,7 +723,7 @@ type_snmp2xml(yang_stmt *ys,
break;
case CLIXON_ASN_PHYS_ADDR:
cv_string_set(cv, ether_ntoa((const struct ether_addr *)requestvb->val.string));
*asn1type = ASN_OCTET_STR;
*asn1type = ASN_OCTET_STR;
break;
case ASN_OCTET_STR: // 4
cv_string_set(cv, (char*)requestvb->val.string);
@ -732,7 +739,7 @@ type_snmp2xml(yang_stmt *ys,
}
default:
assert(0); // XXX
clicon_debug(1, "%s %s not supported", __FUNCTION__, cv_type2str(cvtype));
clixon_debug(CLIXON_DBG_DEFAULT, "%s %s not supported", __FUNCTION__, cv_type2str(cvtype));
if ((ret = netsnmp_request_set_error(request, SNMP_ERR_WRONGTYPE)) != SNMPERR_SUCCESS){
clicon_err(OE_SNMP, ret, "netsnmp_request_set_error");
goto done;
@ -746,7 +753,7 @@ type_snmp2xml(yang_stmt *ys,
}
retval = 1;
done:
clicon_debug(CLIXON_DBG_DETAIL, "%s %d", __FUNCTION__, retval);
clixon_debug(CLIXON_DBG_DETAIL, "%s %d", __FUNCTION__, retval);
if (origtype)
free(origtype);
if (cv)
@ -766,7 +773,7 @@ type_snmp2xml(yang_stmt *ys,
* @param[out] xmlstr1 XML string ready for translation
* @retval 1 OK
* @retval 0 Invalid type
* @retval -1 Error
* @retval -1 Error
* @see type_snmp2xml for snmpset
*/
int
@ -793,7 +800,7 @@ type_xml2snmp_pre(char *xmlstr0,
if ((ret = yang_enum2valstr(yrestype, xmlstr0, &str)) < 0)
goto done;
if (ret == 0){
clicon_debug(1, "Invalid enum valstr %s", xmlstr0);
clixon_debug(CLIXON_DBG_DEFAULT, "Invalid enum valstr %s", xmlstr0);
goto fail;
}
}
@ -802,7 +809,7 @@ type_xml2snmp_pre(char *xmlstr0,
* 1) there is no code for ASN_BOOLEAN and
* 2) Truthvalue actually translates to enum true(1)/false(2)
*/
else if (strcmp(restype, "boolean") == 0){
else if (strcmp(restype, "boolean") == 0){
if (strcmp(xmlstr0, "false")==0)
str = "2";
else
@ -819,7 +826,7 @@ type_xml2snmp_pre(char *xmlstr0,
if ((ret = parse_dec64(xmlstr0, cv_dec64_n_get(cv), &num, NULL)) < 0)
goto done;
if (ret == 0){
clicon_debug(1, "Invalid decimal64 valstr %s", xmlstr0);
clixon_debug(CLIXON_DBG_DEFAULT, "Invalid decimal64 valstr %s", xmlstr0);
goto fail;
}
cv_dec64_i_set(cv, num);
@ -835,7 +842,7 @@ type_xml2snmp_pre(char *xmlstr0,
}
retval = 1;
done:
clicon_debug(CLIXON_DBG_DETAIL, "%s %d", __FUNCTION__, retval);
clixon_debug(CLIXON_DBG_DETAIL, "%s %d", __FUNCTION__, retval);
if (cb)
cbuf_free(cb);
return retval;
@ -904,7 +911,7 @@ type_xml2snmp(char *snmpstr,
oid oid1[MAX_OID_LEN] = {0,};
size_t sz1 = MAX_OID_LEN;
if (snmp_parse_oid(snmpstr, oid1, &sz1) == NULL){
clicon_debug(1, "Failed to parse OID %s", snmpstr);
clixon_debug(CLIXON_DBG_DEFAULT, "Failed to parse OID %s", snmpstr);
goto fail;
}
*snmplen = sizeof(oid)*sz1;
@ -960,7 +967,7 @@ type_xml2snmp(char *snmpstr,
}
memset(*snmpval, 0, *snmplen + 1);
if ((eaddr = ether_aton(snmpstr)) == NULL){
clicon_debug(1, "ether_aton(%s)", snmpstr);
clixon_debug(CLIXON_DBG_DEFAULT, "ether_aton(%s)", snmpstr);
goto fail;
}
memcpy(*snmpval, eaddr, sizeof(*eaddr));
@ -980,7 +987,7 @@ type_xml2snmp(char *snmpstr,
}
retval = 1;
done:
clicon_debug(CLIXON_DBG_DETAIL, "%s %d", __FUNCTION__, retval);
clixon_debug(CLIXON_DBG_DETAIL, "%s %d", __FUNCTION__, retval);
return retval;
fail:
retval = 0;
@ -988,6 +995,7 @@ type_xml2snmp(char *snmpstr,
}
/*! Construct an xpath from yang statement, internal fn using cb
*
* Recursively construct it to the top.
* @param[in] ys Yang statement
* @param[in] keyvec Cvec of key values
@ -995,9 +1003,9 @@ type_xml2snmp(char *snmpstr,
* @retval 0 OK
* @retval -1 Error
* @see yang2xpath
*/
*/
static int
snmp_yang2xpath_cb(yang_stmt *ys,
snmp_yang2xpath_cb(yang_stmt *ys,
cvec *keyvec,
cbuf *cb)
{
@ -1006,13 +1014,13 @@ snmp_yang2xpath_cb(yang_stmt *ys,
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));
goto done;
}
if (yp != NULL && /* XXX rm */
yang_keyword_get(yp) != Y_MODULE &&
yang_keyword_get(yp) != Y_MODULE &&
yang_keyword_get(yp) != Y_SUBMODULE){
if (snmp_yang2xpath_cb(yp, keyvec, cb) < 0) /* recursive call */
goto done;
@ -1053,16 +1061,17 @@ snmp_yang2xpath_cb(yang_stmt *ys,
}
/*! Construct an xpath from yang statement, limited to SNMP table translations
*
* Recursively construct it to the top.
* @param[in] ys Yang statement
* @param[in] keyvec Cvec of key values
* @param[out] xpath Malloced xpath string, use free() after use
* @retval 0 OK
* @retval -1 Error
* @retval -1 Error
* @note
* 1. This should really be in a core .c file, like clixon_yang, BUT
* 2. It is far from complete so maybe keep it here as a special case
*/
*/
int
snmp_yang2xpath(yang_stmt *ys,
cvec *keyvec,
@ -1089,18 +1098,21 @@ snmp_yang2xpath(yang_stmt *ys,
}
/*! Translate from xml body string to SMI OID representation
*
* For ints this is one to one, eg 42 -> 42
* But for eg strings this is more complex, eg foo -> 3.6.22.22 (or something,...)
* @param[in] str XML body string
* @param[in] yi Yang statement
* @param[out] objid OID vector
* @param[out] objidlen Length of OID vector
* @retval 0 OK
* @retval -1 Error
*/
int
snmp_str2oid(char *str,
yang_stmt *yi,
oid *objid,
size_t *objidlen)
size_t *objidlen)
{
int retval = -1;
int asn1_type;
@ -1135,12 +1147,15 @@ snmp_str2oid(char *str,
}
/*! Translate from SMI OID representation to name
*
* For ints this is one to one, eg 42 -> 42
* But for eg strings this is more complex, eg foo -> 3.6.22.22 (or something,...)
* @param[in,out] oidi ObjID vector
* @param[in,out] oidilen Length of ObjID vector
* @param[in] yk Yang statement of key
* @param[out] cv CLIgen variable string notation as "x.y.z"
* @retval 0 OK
* @retval -1 Error
* @see rfc2578 Section 7.7
*/
int
@ -1223,7 +1238,7 @@ clixon_snmp_err_cb(void *handle,
{
const char *errstr;
clicon_debug(1, "%s", __FUNCTION__);
clixon_debug(CLIXON_DBG_DEFAULT, "%s", __FUNCTION__);
if (suberr < 0){
if (suberr < -CLIXON_ERR_SNMP_MIB){
switch (suberr+CLIXON_ERR_SNMP_MIB){
@ -1262,9 +1277,9 @@ clixon_snmp_err_cb(void *handle,
* @param[in] cvk_name Vector of list keys
* @param[out] cvk_val Vector of XML key values
* @param[out] objidk OID key part, to be appended to node OID
* @retval -1 Error
* @retval 0 Invalid (not all indexes present)
* @retval 1 OK
* @retval 0 Invalid (not all indexes present)
* @retval -1 Error
* Both cvk_val and cvk_oid can be re-used in successive calls but need to be freed w cvec_free after use
*/
int
@ -1281,7 +1296,7 @@ snmp_xmlkey2val_oid(cxobj *xentry,
cg_var *cv0;
oid objid[MAX_OID_LEN] = {0,};
size_t objidlen = MAX_OID_LEN;
*objidklen = 0;
if (cvk_val){
if (*cvk_val){
@ -1297,11 +1312,11 @@ snmp_xmlkey2val_oid(cxobj *xentry,
}
}
for (i=0; i<cvec_len(cvk_name); i++){
cv0 = cvec_i(cvk_name, i);
cv0 = cvec_i(cvk_name, i);
if ((xi = xml_find_type(xentry, NULL, cv_string_get(cv0), CX_ELMNT)) == NULL)
break;
if (cvk_val){
cv = cvec_i(*cvk_val, i);
cv = cvec_i(*cvk_val, i);
if (cv_string_set(cv, xml_body(xi)) < 0){
clicon_err(OE_UNIX, errno, "cv_string_set");
goto done;
@ -1326,6 +1341,7 @@ snmp_xmlkey2val_oid(cxobj *xentry,
* Peeks into internal lib global variables, may be sensitive to library change
*/
/*! Check if netsnmp is connected
*
* @retval 1 yes, running
* @retval 0 No, not running
* XXX: this peeks into the "main_session" global variable in agent/snmp_agent.c
@ -1335,11 +1351,12 @@ int
clixon_snmp_api_agent_check(void)
{
extern netsnmp_session *main_session;
return (main_session != NULL) ? 1 : 0;
}
/*! Cleanup remaining libnetsnmb memory
*
* XXX: this peeks into the "tclist" global variable in snmplib/parse.c
* Tried to find API function but failed
*/
@ -1347,19 +1364,20 @@ int
clixon_snmp_api_agent_cleanup(void)
{
extern void *tclist;
if (tclist)
free(tclist);
return 0;
}
/*! See if oid is registered
*
* This is good enough for add,
* But for delete a more advanced function is needed
* @see netsnmp_subtree_load
* @retval -1 Error
* @retval 0 Not found
* @retval 1 Found
* @retval 0 Not found
* @retval -1 Error
*/
int
clixon_snmp_api_oid_find(oid *oid0,
@ -1367,7 +1385,7 @@ clixon_snmp_api_oid_find(oid *oid0,
{
int retval = -1;
netsnmp_subtree *tree1 = NULL;
if ((tree1 = netsnmp_subtree_find(oid0, oid0len, NULL, "")) != NULL &&
oid_eq(oid0, oid0len, tree1->name_a, tree1->namelen)==0){
retval = 1;

View file

@ -57,7 +57,7 @@ extern "C" {
#define CLIXON_ASN_EXTRAS 253 /* Special case clixon address >= this */
#define CLIXON_ASN_PHYS_ADDR 253 /* Special case phy-address */
#define CLIXON_ASN_FIXED_STRING 254 /* RFC2578 Sec 7.7: String-valued, fixed-length */
#define CLIXON_ASN_ROWSTATUS 255
#define CLIXON_ASN_ROWSTATUS 255
/*
* Types
@ -70,10 +70,10 @@ struct clixon_snmp_handle {
oid sh_oid[MAX_OID_LEN]; /* OID of registered table (list) */
size_t sh_oidlen;
oid sh_oid2[MAX_OID_LEN]; /* OID of registered container */
size_t sh_oid2len;
size_t sh_oid2len;
char *sh_default; /* MIB default value leaf only */
cvec *sh_cvk_orig; /* Index/Key variable values (original) */
netsnmp_table_registration_info *sh_table_info; /* To mimic table-handler in libnetsnmp code
netsnmp_table_registration_info *sh_table_info; /* To mimic table-handler in libnetsnmp code
* save only to free properly */
};
typedef struct clixon_snmp_handle clixon_snmp_handle;

View file

@ -74,7 +74,7 @@
/* Forward */
static int clixon_snmp_input_cb(int s, void *arg);
/*! Return (hardcoded) pid file
*/
static char*
@ -84,22 +84,24 @@ clicon_snmp_pidfile(clicon_handle h)
}
/*! Signal terminates process
*
* Just set exit flag for proper exit in event loop
*/
static void
clixon_snmp_sig_term(int arg)
{
clicon_log(LOG_NOTICE, "%s: %s: pid: %u Signal %d",
clicon_log(LOG_NOTICE, "%s: %s: pid: %u Signal %d",
__PROGRAM__, __FUNCTION__, getpid(), arg);
/* This should ensure no more accepts or incoming packets are processed because next time eventloop
* is entered, it will terminate.
* However there may be a case of sockets closing rather abruptly for clients
*/
clixon_exit_set(1);
clixon_exit_set(1);
}
/*! Clean and close all state of netconf process (but dont exit).
* Cannot use h after this
/*! Clean and close all state of netconf process (but dont exit).
*
* Cannot use h after this
* @param[in] h Clixon handle
*/
static int
@ -139,10 +141,12 @@ snmp_terminate(clicon_handle h)
/*! Get which sockets are used from SNMP API, the register single sockets into clixon event system
*
* @param[in] h Clixon handle
* @param[in] register if 1 register snmp sockets with event handler. If 0 close and unregister
* @param[in] register If 1 register snmp sockets with event handler. If 0 close and unregister
* @retval 0 OK
* @retval -1 Error
* This is a workaround for netsnmps API usiing fdset:s, instead an fdset is created before calling
* the snmp api
* if you use select(), see snmp_select_info() in snmp_api(3)
* if you use select(), see snmp_select_info() in snmp_api(3)
* snmp_select_info(int *numfds, fd_set *fdset, struct timeval *timeout, int *block)
* @see clixon_snmp_input_cb
*/
@ -166,7 +170,7 @@ clixon_snmp_fdset_register(clicon_handle h,
/* eg 4, 6, 8 */
for (s=0; s<numfds; s++){
if (FD_ISSET(s, &readfds)){
clicon_debug(1, "%s %d", __FUNCTION__, s);
clixon_debug(CLIXON_DBG_DEFAULT, "%s %d", __FUNCTION__, s);
if (regfd){
if (clixon_event_reg_fd(s, clixon_snmp_input_cb, h, "snmp socket") < 0)
goto done;
@ -183,14 +187,17 @@ clixon_snmp_fdset_register(clicon_handle h,
return retval;
}
/*! Callback for single socket
/*! Callback for single socket
*
* This is a workaround for netsnmps API usiing fdset:s, instead an fdset is created before calling
* the snmp api
* @param[in] s Read socket
* @param[in] arg Clixon handle
* @retval 0 OK
* @retval -1 Error
*/
static int
clixon_snmp_input_cb(int s,
clixon_snmp_input_cb(int s,
void *arg)
{
int retval = -1;
@ -198,7 +205,7 @@ clixon_snmp_input_cb(int s,
clicon_handle h = (clicon_handle)arg;
int ret;
clicon_debug(CLIXON_DBG_DETAIL, "%s %d", __FUNCTION__, s);
clixon_debug(CLIXON_DBG_DETAIL, "%s %d", __FUNCTION__, s);
FD_ZERO(&readfds);
FD_SET(s, &readfds);
(void)snmp_read(&readfds);
@ -232,8 +239,11 @@ clixon_snmp_input_cb(int s,
}
/*! Init netsnmp agent connection
*
* @param[in] h Clixon handle
* @param[in] logdst Log destination, see clixon_log.h
* @retval 0 OK
* @retval -1 Error
* @see snmp_terminate
*/
static int
@ -243,7 +253,7 @@ clixon_snmp_init_subagent(clicon_handle h,
int retval = -1;
char *sockpath = NULL;
clicon_debug(1, "%s", __FUNCTION__);
clixon_debug(CLIXON_DBG_DEFAULT, "%s", __FUNCTION__);
if (logdst == CLICON_LOG_SYSLOG)
snmp_enable_calllog();
else
@ -253,11 +263,11 @@ clixon_snmp_init_subagent(clicon_handle h,
/* don't load config and don't load/save persistent file */
netsnmp_ds_set_boolean(NETSNMP_DS_LIBRARY_ID, NETSNMP_DS_LIB_DONT_PERSIST_STATE, 1);
/* don't load persistent file */
netsnmp_ds_set_boolean(NETSNMP_DS_LIBRARY_ID, NETSNMP_DS_LIB_DISABLE_PERSISTENT_LOAD, 1);
netsnmp_ds_set_boolean(NETSNMP_DS_LIBRARY_ID, NETSNMP_DS_LIB_DISABLE_PERSISTENT_LOAD, 1);
/* don't save persistent file */
netsnmp_ds_set_boolean(NETSNMP_DS_LIBRARY_ID, NETSNMP_DS_LIB_DISABLE_PERSISTENT_SAVE, 1);
netsnmp_ds_set_boolean(NETSNMP_DS_LIBRARY_ID, NETSNMP_DS_LIB_DISABLE_PERSISTENT_SAVE, 1);
if (clicon_debug_get())
if (clixon_debug_get())
netsnmp_ds_set_boolean(NETSNMP_DS_APPLICATION_ID, NETSNMP_DS_AGENT_VERBOSE, 1);
if ((sockpath = clicon_option_str(h, "CLICON_SNMP_AGENT_SOCK")) == NULL){
@ -277,7 +287,6 @@ clixon_snmp_init_subagent(clicon_handle h,
clicon_err(OE_DAEMON, 0, "Connection to SNMP agent failed");
goto done;
}
if (set_signal(SIGTERM, clixon_snmp_sig_term, NULL) < 0){
clicon_err(OE_DAEMON, errno, "Setting signal");
goto done;
@ -299,6 +308,7 @@ clixon_snmp_init_subagent(clicon_handle h,
}
/*! Usage help routine
*
* @param[in] h Clixon handle
* @param[in] argv0 command line
*/
@ -344,12 +354,12 @@ main(int argc,
int zap = 0;
int config_dump = 0;
enum format_enum config_dump_format = FORMAT_XML;
/* Create handle */
if ((h = clicon_handle_init()) == NULL)
return -1;
/* In the startup, logs to stderr & debug flag set later */
clicon_log_init(__PROGRAM__, LOG_INFO, logdst);
clicon_log_init(__PROGRAM__, LOG_INFO, logdst);
/* Set username to clixon handle. Use in all communication to backend */
if ((pw = getpwuid(getuid())) == NULL){
@ -382,11 +392,11 @@ main(int argc,
break;
}
/*
/*
* Logs, error and debug to stderr or syslog, set debug level
*/
clicon_log_init(__PROGRAM__, dbg?LOG_DEBUG:LOG_INFO, logdst);
clicon_debug_init(dbg, NULL);
clicon_log_init(__PROGRAM__, dbg?LOG_DEBUG:LOG_INFO, logdst);
clixon_debug_init(dbg, NULL);
/* This is netsnmplib debugging which is quite extensive + only if compiled w debug */
if (dbg > 1)
snmp_set_do_debugging(1);
@ -398,13 +408,10 @@ main(int argc,
clixon_snmp_err_cb /* log fn */
) < 0)
goto done;
yang_init(h);
/* Find, read and parse configfile */
if (clicon_options_main(h) < 0)
goto done;
/* Now rest of options */
optind = 1;
opterr = 0;
@ -455,18 +462,18 @@ main(int argc,
if (pid && pidfile_zapold(pid) < 0)
return -1;
if (lstat(pidfile, &st) == 0)
unlink(pidfile);
unlink(pidfile);
snmp_terminate(h);
exit(0); /* OK */
}
else if (pid){
clicon_err(OE_DAEMON, 0, "Clixon_snmp daemon already running with pid %d\n(Try killing it with %s -z)",
clicon_err(OE_DAEMON, 0, "Clixon_snmp daemon already running with pid %d\n(Try killing it with %s -z)",
pid, argv0);
return -1; /* goto done deletes pidfile */
}
/* Here there is either no old process or we have killed it,.. */
if (lstat(pidfile, &st) == 0)
unlink(pidfile);
unlink(pidfile);
/* Init cligen buffers */
cligen_buflen = clicon_option_int(h, "CLICON_CLI_BUF_START");
@ -488,7 +495,7 @@ main(int argc,
/* Create top-level yang spec and store as option */
if ((yspec = yspec_new()) == NULL)
goto done;
clicon_dbspec_yang_set(h, yspec);
clicon_dbspec_yang_set(h, yspec);
/* Load Yang modules
* 1. Load a yang module as a specific absolute filename */
@ -530,7 +537,7 @@ main(int argc,
goto ok;
}
clicon_option_dump(h, 1);
/* Send hello request to backend to get session-id back
* This is done once at the beginning of the session and then this is
* used by the client, even though new TCP sessions are created for
@ -540,7 +547,6 @@ main(int argc,
if (clicon_hello_req(h, "cl:snmp", NULL, &id) < 0)
goto done;
clicon_session_id_set(h, id);
/* Init snmp as subagent */
if (clixon_snmp_init_subagent(h, logdst) < 0)
goto done;

View file

@ -80,6 +80,7 @@
#include "snmp_handler.h"
/*! Parse smiv2 extensions for YANG leaf
*
* Typical leaf:
* smiv2:oid "1.3.6.1.4.1.8072.2.1.1";
* smiv2:max-access "read-write";
@ -143,7 +144,7 @@ mibyang_leaf_register(clicon_handle h,
if (yang_extension_value_opt(ys, "smiv2:defval", NULL, &default_str) < 0)
goto done;
name = yang_argument_get(ys);
/* Stateless function, just returns ptr */
/* Stateless function, just returns ptr */
if ((handler = netsnmp_create_handler(name, clixon_snmp_scalar_handler)) == NULL){
clicon_err(OE_XML, errno, "netsnmp_create_handler");
goto done;
@ -167,7 +168,7 @@ mibyang_leaf_register(clicon_handle h,
clicon_err(OE_UNIX, errno, "cvec_dup");
goto done;
}
/* Stateless function, just returns ptr */
/* Stateless function, just returns ptr */
if ((nhreg = netsnmp_handler_registration_create(name, handler,
oid1, oid1len,
modes)) == NULL){
@ -180,7 +181,7 @@ mibyang_leaf_register(clicon_handle h,
handler->data_clone = snmp_handle_clone;
handler->data_free = snmp_handle_free;
/*
/*
* XXX: nhreg->agent_data
*/
if ((ret = netsnmp_register_instance(nhreg)) != SNMPERR_SUCCESS){
@ -189,7 +190,7 @@ mibyang_leaf_register(clicon_handle h,
goto done;
}
oid_cbuf(cboid, oid1, oid1len);
clicon_debug(1, "%s register: %s %s", __FUNCTION__, name, cbuf_get(cboid));
clixon_debug(CLIXON_DBG_DEFAULT, "%s register: %s %s", __FUNCTION__, name, cbuf_get(cboid));
ok:
retval = 0;
done:
@ -236,7 +237,7 @@ mibyang_table_register(clicon_handle h,
int asn1type;
yang_stmt *ys;
char *name;
if ((ys = yang_parent_get(ylist)) == NULL ||
yang_keyword_get(ys) != Y_CONTAINER){
clicon_err(OE_YANG, EINVAL, "ylist parent is not list");
@ -247,7 +248,7 @@ mibyang_table_register(clicon_handle h,
* the original and ifXTable for the augmented.
* But the name does not seem to have semantic significance, so I leave it as is.
*/
name = yang_argument_get(ys);
name = yang_argument_get(ys);
/* Userdata to pass around in netsmp callbacks
* XXX: not deallocated
*/
@ -278,7 +279,7 @@ mibyang_table_register(clicon_handle h,
handler->myvoid =(void*)sh;
handler->data_clone = snmp_handle_clone;
handler->data_free = snmp_handle_free;
/* 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");
@ -294,7 +295,7 @@ mibyang_table_register(clicon_handle h,
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\"",
clicon_err(OE_XML, 0, "List statement \"%s\" has no key leaf \"%s\"",
yang_argument_get(ylist), keyname);
goto done;
}
@ -315,19 +316,18 @@ mibyang_table_register(clicon_handle h,
/* Count columns */
yleaf = NULL;
table_info->max_column = 0;
table_info->max_column = 0;
while ((yleaf = yn_each(ylist, yleaf)) != NULL) {
if ((yang_keyword_get(yleaf) != Y_LEAF) || (ret = yangext_is_oid_exist(yleaf)) != 1)
continue;
table_info->max_column++;
table_info->max_column++;
}
if ((ret = netsnmp_register_table(nhreg, table_info)) != SNMPERR_SUCCESS){
clicon_err(OE_SNMP, ret, "netsnmp_register_table");
goto done;
}
sh->sh_table_info = table_info; /* Keep to free at exit */
clicon_debug(1, "%s register: %s %s", __FUNCTION__, name, oidstr);
clixon_debug(CLIXON_DBG_DEFAULT, "%s register: %s %s", __FUNCTION__, name, oidstr);
ok:
retval = 0;
done:
@ -375,7 +375,7 @@ mibyang_list_register(clicon_handle h,
goto done;
if (ret == 0)
goto ok;
if (mibyang_table_register(h, ylist,
if (mibyang_table_register(h, ylist,
oid1, oid1len,
oid2, oid2len,
oidstr) < 0)
@ -446,6 +446,7 @@ mibyang_augment_register(clicon_handle h,
}
/*! Register table sub-oid:s of existing entries in clixon
*
* 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
@ -476,8 +477,8 @@ mibyang_table_poll(clicon_handle h,
int ret;
oid oidk[MAX_OID_LEN] = {0,};
size_t oidklen = MAX_OID_LEN;
clicon_debug(1, "%s", __FUNCTION__);
clixon_debug(CLIXON_DBG_DEFAULT, "%s", __FUNCTION__);
if ((ys = yang_parent_get(ylist)) == NULL ||
yang_keyword_get(ys) != Y_CONTAINER){
clicon_err(OE_YANG, EINVAL, "ylist parent is not list");
@ -509,7 +510,7 @@ mibyang_table_poll(clicon_handle h,
while ((xcol = xml_child_each(xrow, xcol, CX_ELMNT)) != NULL) {
if ((y = xml_spec(xcol)) == NULL)
continue;
if (mibyang_leaf_register(h, y, cvk_val, oidk, oidklen) < 0)
if (mibyang_leaf_register(h, y, cvk_val, oidk, oidklen) < 0)
goto done;
}
}
@ -523,7 +524,7 @@ mibyang_table_poll(clicon_handle h,
if (xt)
xml_free(xt);
if (nsc)
xml_nsctx_free(nsc);
xml_nsctx_free(nsc);
return retval;
}
@ -557,8 +558,8 @@ mibyang_traverse(clicon_handle h,
yang_stmt *yp;
int ret;
static oid zero_oid = 0;
clicon_debug(1, "%s %s", __FUNCTION__, yang_argument_get(yn));
clixon_debug(CLIXON_DBG_DEFAULT, "%s %s", __FUNCTION__, yang_argument_get(yn));
switch(yang_keyword_get(yn)){
case Y_AUGMENT:
if (mibyang_augment_register(h, yn) < 0)
@ -588,7 +589,7 @@ mibyang_traverse(clicon_handle h,
ys = NULL;
while ((ys = yn_each(yn, ys)) != NULL) {
/* augment special case of table */
if (!yang_schemanode(ys) && yang_keyword_get(ys) != Y_AUGMENT)
if (!yang_schemanode(ys) && yang_keyword_get(ys) != Y_AUGMENT)
continue;
if ((ret = mibyang_traverse(h, ys)) < 0)
goto done;
@ -601,7 +602,7 @@ mibyang_traverse(clicon_handle h,
retval = 0;
done:
return retval;
}
}
/*! Init mib-translated yangs and register callbacks by traversing the yang
*
@ -631,7 +632,7 @@ clixon_snmp_traverse_mibyangs(clicon_handle h)
continue;
if ((modname = xml_body(x)) == NULL)
continue;
clicon_debug(1, "%s %s: \"%s\"", __FUNCTION__, xml_name(x), modname);
clixon_debug(CLIXON_DBG_DEFAULT, "%s %s: \"%s\"", __FUNCTION__, xml_name(x), modname);
/* Note, here we assume the Yang is loaded by some other mechanism and
* error if it not found.
* Alternatively, that YANG could be loaded.