From 18ff854e121dcd8bf97587080da4195dbf3f1749 Mon Sep 17 00:00:00 2001 From: Olof hagsand Date: Sat, 28 May 2022 15:13:53 +0200 Subject: [PATCH] SNMP frontend: special case for SnmpAdminString with not null-terminated string --- apps/snmp/snmp_lib.c | 17 ++++++++++++++++- test/test_snmp_set.sh | 2 +- 2 files changed, 17 insertions(+), 2 deletions(-) diff --git a/apps/snmp/snmp_lib.c b/apps/snmp/snmp_lib.c index fc84954a..8aaec720 100644 --- a/apps/snmp/snmp_lib.c +++ b/apps/snmp/snmp_lib.c @@ -107,7 +107,9 @@ static const map_str2int snmp_type_map[] = { {"boolean", ASN_INTEGER}, // 2 special case -> enumeration {NULL, -1} }; -#define CLIXON_ASN_PHYS_ADDR 0x4242 + +#define CLIXON_ASN_PHYS_ADDR 0x4242 /* Special case phy-address */ +#define CLIXON_ASN_ADMIN_STRING 0x4243 /* Special case SnmpAdminString */ /* Map between SNMP message / mode str and int form */ @@ -155,6 +157,8 @@ snmp_handle_free(clixon_snmp_handle *sh) * @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_ */ int type_yang2asn1(yang_stmt *ys, @@ -200,6 +204,9 @@ type_yang2asn1(yang_stmt *ys, else if (strcmp(origtype, "phys-address")==0){ at = CLIXON_ASN_PHYS_ADDR; /* Clixon extended string type */ } + else if (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){ @@ -498,6 +505,14 @@ type_snmpstr2val(char *snmpstr, *asn1type = ASN_OCTET_STR; break; } + case CLIXON_ASN_ADMIN_STRING: /* OCTET-STRING with decrement length */ + *snmplen = strlen(snmpstr); + if ((*snmpval = (u_char*)strdup((snmpstr))) == NULL){ + clicon_err(OE_UNIX, errno, "strdup"); + goto done; + } + *asn1type = ASN_OCTET_STR; + break; default: assert(0); } diff --git a/test/test_snmp_set.sh b/test/test_snmp_set.sh index 627ba8b9..a5fc5ae1 100755 --- a/test/test_snmp_set.sh +++ b/test/test_snmp_set.sh @@ -150,7 +150,7 @@ VALUE="foo bar" TYPE=STRING # SnmpAdminString new "Get $NAME default" -expectpart "$($snmpget $OID)" 0 "$OID = $TYPE: So long, and thanks for all the fish!." +expectpart "$($snmpget $OID)" 0 "$OID = $TYPE: So long, and thanks for all the fish!" new "Set $NAME $VALUE" expectpart "$($snmpset $OID s "$VALUE")" 0 "$OID = $TYPE: $VALUE"