SNMP: refactor type translation using orig-table

This commit is contained in:
Olof hagsand 2022-06-09 13:14:36 +02:00
parent 8727e93911
commit 5d1c78ead5
2 changed files with 46 additions and 51 deletions

View file

@ -94,8 +94,6 @@ static const map_str2int snmp_access_map[] = {
/* Map between clixon and ASN.1 types. /* Map between clixon and ASN.1 types.
* @see net-snmp/library/asn1.h * @see net-snmp/library/asn1.h
* @see union netsnmp_vardata in net-snmp/types.h * @see union netsnmp_vardata in net-snmp/types.h
* XXX not complete
* XXX TimeTicks
*/ */
static const map_str2int snmp_type_map[] = { static const map_str2int snmp_type_map[] = {
{"int32", ASN_INTEGER}, // 2 {"int32", ASN_INTEGER}, // 2
@ -109,8 +107,27 @@ static const map_str2int snmp_type_map[] = {
{NULL, -1} {NULL, -1}
}; };
#define CLIXON_ASN_PHYS_ADDR 0x4242 /* Special case phy-address */ //#define CLIXON_ASN_PHYS_ADDR 0x4242 /* Special case phy-address */
#define CLIXON_ASN_ADMIN_STRING 0x4243 /* Special case SnmpAdminString */ //#define CLIXON_ASN_ADMIN_STRING 0x4243 /* Special case SnmpAdminString */
#define CLIXON_ASN_PHYS_ADDR 253 /* Special case phy-address */
#define CLIXON_ASN_ADMIN_STRING 254 /* Special case SnmpAdminString */
/* Map between clixon "orig" resolved type and ASN.1 types.
*/
static const map_str2int snmp_orig_map[] = {
{"counter32", ASN_COUNTER}, // 0x41 / 65
{"object-identifier-128", ASN_OBJECT_ID}, // 6
{"AutonomousType", ASN_OBJECT_ID}, // 6
{"DateAndTime", ASN_OCTET_STR}, // 4
{"UUIDorZero", ASN_OCTET_STR}, // 4
{"binary", ASN_OCTET_STR}, // 4
{"timeticks", ASN_TIMETICKS}, // 0x43 / 67
{"timestamp", ASN_TIMETICKS}, // 0x43 / 67
{"InetAddress", ASN_IPADDRESS}, // 0x40 / 64
{"phys-address", CLIXON_ASN_PHYS_ADDR}, /* Clixon extended string type */
{"SnmpAdminString", CLIXON_ASN_ADMIN_STRING}, /* cf extension display-type 255T? */
{NULL, -1}
};
/* Map between SNMP message / mode str and int form /* Map between SNMP message / mode str and int form
*/ */
@ -238,40 +255,14 @@ type_yang2asn1(yang_stmt *ys,
goto done; goto done;
restype = yrestype?yang_argument_get(yrestype):NULL; restype = yrestype?yang_argument_get(yrestype):NULL;
} }
/* Special case: counter32, maps to same resolved type as gauge32 */ /* Translate to asn.1
if (strcmp(origtype, "counter32")==0){ * First try original type, first type
at = ASN_COUNTER; */
if ((at = clicon_str2int(snmp_orig_map, origtype)) >= 0 &&
(extended || (at != CLIXON_ASN_PHYS_ADDR && at != CLIXON_ASN_ADMIN_STRING))){
;
} }
else if (strcmp(origtype, "object-identifier-128") == 0 || /* Then try fully resolved type */
strcmp(origtype, "AutonomousType") == 0){
at = ASN_OBJECT_ID;
}
else if (strcmp(origtype, "binary")==0){
at = ASN_OCTET_STR;
}
else if (strcmp(origtype, "timeticks")==0){
at = ASN_TIMETICKS;
}
else if (strcmp(origtype, "DateAndTime")==0) {
at = ASN_OCTET_STR;
}
else if (strcmp(origtype, "UUIDorZero")==0) {
at = ASN_OCTET_STR;
}
else if (strcmp(origtype, "timestamp")==0){
at = ASN_TIMETICKS;
}
else if (strcmp(origtype, "InetAddress")==0){
at = ASN_IPADDRESS;
}
else if (extended && strcmp(origtype, "phys-address")==0){
at = CLIXON_ASN_PHYS_ADDR; /* Clixon extended string type */
}
else if (extended && strcmp(origtype, "SnmpAdminString")==0){
at = CLIXON_ASN_ADMIN_STRING; /* cf extension display-type 255T? */
}
/* translate to asn.1 */
else 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", clicon_err(OE_YANG, 0, "No snmp translation for YANG %s type:%s",
yang_argument_get(ys), restype); yang_argument_get(ys), restype);
@ -364,6 +355,10 @@ type_snmp2xml(yang_stmt *ys,
case ASN_GAUGE: // 0x42 case ASN_GAUGE: // 0x42
cv_uint32_set(cv, *requestvb->val.integer); cv_uint32_set(cv, *requestvb->val.integer);
break; break;
case CLIXON_ASN_ADMIN_STRING: // XXX
case CLIXON_ASN_PHYS_ADDR: // XXX
assert(0);
break;
case ASN_OCTET_STR: // 4 case ASN_OCTET_STR: // 4
cv_string_set(cv, (char*)requestvb->val.string); cv_string_set(cv, (char*)requestvb->val.string);
break; break;

View file

@ -67,23 +67,23 @@ typedef struct clixon_snmp_handle clixon_snmp_handle;
/* /*
* Prototypes * Prototypes
*/ */
int snmp_access_str2int(char *modes_str); int snmp_access_str2int(char *modes_str);
const char *snmp_msg_int2str(int msg); const char *snmp_msg_int2str(int msg);
void *snmp_handle_clone(void *arg); void *snmp_handle_clone(void *arg);
void snmp_handle_free(void *arg); void snmp_handle_free(void *arg);
int type_yang2asn1(yang_stmt *ys, int *asn1_type, int extended); int type_yang2asn1(yang_stmt *ys, int *asn1_type, int extended);
int type_snmp2xml(yang_stmt *ys, int type_snmp2xml(yang_stmt *ys,
netsnmp_variable_list *requestvb, netsnmp_variable_list *requestvb,
netsnmp_agent_request_info *reqinfo, netsnmp_agent_request_info *reqinfo,
netsnmp_request_info *requests, netsnmp_request_info *requests,
char **valstr); char **valstr);
int type_xml2snmp_pre(char *xmlstr, yang_stmt *ys, char **snmpstr); int type_xml2snmp_pre(char *xmlstr, yang_stmt *ys, char **snmpstr);
int type_xml2snmp(char *snmpstr, int *asn1type, u_char **snmpval, size_t *snmplen, char **reason); int type_xml2snmp(char *snmpstr, int *asn1type, u_char **snmpval, size_t *snmplen, char **reason);
int yang2xpath(yang_stmt *ys, cvec *keyvec, char **xpath); int yang2xpath(yang_stmt *ys, cvec *keyvec, char **xpath);
int snmp_body2oid(cxobj *xi, cg_var *cv); int snmp_body2oid(cxobj *xi, cg_var *cv);
int snmp_agent_check(void); int snmp_agent_check(void);
int snmp_agent_cleanup(void); int snmp_agent_cleanup(void);
int clixon_snmp_err_cb(void *handle, int suberr, cbuf *cb); int clixon_snmp_err_cb(void *handle, int suberr, cbuf *cb);
#endif /* _SNMP_LIB_H_ */ #endif /* _SNMP_LIB_H_ */