From 5d1c78ead5abcbad23e1af69e3234e3b92d0ae9b Mon Sep 17 00:00:00 2001 From: Olof hagsand Date: Thu, 9 Jun 2022 13:14:36 +0200 Subject: [PATCH] SNMP: refactor type translation using orig-table --- apps/snmp/snmp_lib.c | 69 ++++++++++++++++++++------------------------ apps/snmp/snmp_lib.h | 28 +++++++++--------- 2 files changed, 46 insertions(+), 51 deletions(-) diff --git a/apps/snmp/snmp_lib.c b/apps/snmp/snmp_lib.c index c4d68d41..a6c82243 100644 --- a/apps/snmp/snmp_lib.c +++ b/apps/snmp/snmp_lib.c @@ -94,8 +94,6 @@ static const map_str2int snmp_access_map[] = { /* Map between clixon and ASN.1 types. * @see net-snmp/library/asn1.h * @see union netsnmp_vardata in net-snmp/types.h - * XXX not complete - * XXX TimeTicks */ static const map_str2int snmp_type_map[] = { {"int32", ASN_INTEGER}, // 2 @@ -109,8 +107,27 @@ static const map_str2int snmp_type_map[] = { {NULL, -1} }; -#define CLIXON_ASN_PHYS_ADDR 0x4242 /* Special case phy-address */ -#define CLIXON_ASN_ADMIN_STRING 0x4243 /* Special case SnmpAdminString */ +//#define CLIXON_ASN_PHYS_ADDR 0x4242 /* Special case phy-address */ +//#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 */ @@ -238,40 +255,14 @@ type_yang2asn1(yang_stmt *ys, 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; + /* Translate to asn.1 + * First try original type, first type + */ + 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 || - 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 */ + /* Then try fully resolved type */ 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); @@ -364,6 +355,10 @@ type_snmp2xml(yang_stmt *ys, case ASN_GAUGE: // 0x42 cv_uint32_set(cv, *requestvb->val.integer); break; + case CLIXON_ASN_ADMIN_STRING: // XXX + case CLIXON_ASN_PHYS_ADDR: // XXX + assert(0); + break; case ASN_OCTET_STR: // 4 cv_string_set(cv, (char*)requestvb->val.string); break; diff --git a/apps/snmp/snmp_lib.h b/apps/snmp/snmp_lib.h index 1e4bfe2f..98939681 100644 --- a/apps/snmp/snmp_lib.h +++ b/apps/snmp/snmp_lib.h @@ -67,23 +67,23 @@ typedef struct clixon_snmp_handle clixon_snmp_handle; /* * Prototypes */ -int snmp_access_str2int(char *modes_str); +int snmp_access_str2int(char *modes_str); const char *snmp_msg_int2str(int msg); void *snmp_handle_clone(void *arg); void snmp_handle_free(void *arg); -int type_yang2asn1(yang_stmt *ys, int *asn1_type, int extended); -int type_snmp2xml(yang_stmt *ys, - netsnmp_variable_list *requestvb, - netsnmp_agent_request_info *reqinfo, - netsnmp_request_info *requests, - char **valstr); -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 yang2xpath(yang_stmt *ys, cvec *keyvec, char **xpath); -int snmp_body2oid(cxobj *xi, cg_var *cv); -int snmp_agent_check(void); -int snmp_agent_cleanup(void); -int clixon_snmp_err_cb(void *handle, int suberr, cbuf *cb); +int type_yang2asn1(yang_stmt *ys, int *asn1_type, int extended); +int type_snmp2xml(yang_stmt *ys, + netsnmp_variable_list *requestvb, + netsnmp_agent_request_info *reqinfo, + netsnmp_request_info *requests, + char **valstr); +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 yang2xpath(yang_stmt *ys, cvec *keyvec, char **xpath); +int snmp_body2oid(cxobj *xi, cg_var *cv); +int snmp_agent_check(void); +int snmp_agent_cleanup(void); +int clixon_snmp_err_cb(void *handle, int suberr, cbuf *cb); #endif /* _SNMP_LIB_H_ */