Path: blob/main/usr.sbin/bsnmpd/tools/libbsnmptools/bsnmpmap.c
105658 views
/*-1* Copyright (c) 2006 The FreeBSD Project2* All rights reserved.3*4* Author: Shteryana Shopova <[email protected]>5*6* Redistribution of this software and documentation and use in source and7* binary forms, with or without modification, are permitted provided that8* the following conditions are met:9*10* 1. Redistributions of source code or documentation must retain the above11* copyright notice, this list of conditions and the following disclaimer.12* 2. Redistributions in binary form must reproduce the above copyright13* notice, this list of conditions and the following disclaimer in the14* documentation and/or other materials provided with the distribution.15*16* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND17* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE18* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE19* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE20* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL21* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS22* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)23* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT24* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY25* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF26* SUCH DAMAGE.27*/2829#include <sys/param.h>30#include <sys/queue.h>31#include <sys/uio.h>3233#include <ctype.h>34#include <err.h>35#include <errno.h>36#include <stdio.h>37#include <stdlib.h>38#include <string.h>39#include <syslog.h>40#include <unistd.h>4142#include <bsnmp/asn1.h>43#include <bsnmp/snmp.h>44#include "bsnmptc.h"45#include "bsnmptools.h"4647#define DEBUG if (_bsnmptools_debug) fprintf4849/* Allocate memory and initialize list. */50struct snmp_mappings *51snmp_mapping_init(void)52{53struct snmp_mappings *m;5455if ((m = calloc(1, sizeof(struct snmp_mappings))) == NULL) {56syslog(LOG_ERR, "malloc() failed: %s", strerror(errno));57return (NULL);58}5960return (m);61}6263#define snmp_nodelist mappings->nodelist64#define snmp_intlist mappings->intlist65#define snmp_octlist mappings->octlist66#define snmp_oidlist mappings->oidlist67#define snmp_iplist mappings->iplist68#define snmp_ticklist mappings->ticklist69#define snmp_cntlist mappings->cntlist70#define snmp_gaugelist mappings->gaugelist71#define snmp_cnt64list mappings->cnt64list72#define snmp_enumlist mappings->enumlist73#define snmp_tablelist mappings->tablelist74#define snmp_tclist mappings->tclist7576void77enum_pairs_free(struct enum_pairs *headp)78{79struct enum_pair *e;8081if (headp == NULL)82return;8384while ((e = STAILQ_FIRST(headp)) != NULL) {85STAILQ_REMOVE_HEAD(headp, link);8687if (e->enum_str)88free(e->enum_str);89free(e);90}9192free(headp);93}9495void96snmp_mapping_entryfree(struct snmp_oid2str *entry)97{98if (entry->string)99free(entry->string);100101if (entry->tc == SNMP_TC_OWN)102enum_pairs_free(entry->snmp_enum);103104free(entry);105}106107static void108snmp_mapping_listfree(struct snmp_mapping *headp)109{110struct snmp_oid2str *p;111112while ((p = SLIST_FIRST(headp)) != NULL) {113SLIST_REMOVE_HEAD(headp, link);114115if (p->string)116free(p->string);117118if (p->tc == SNMP_TC_OWN)119enum_pairs_free(p->snmp_enum);120free(p);121}122123SLIST_INIT(headp);124}125126void127snmp_index_listfree(struct snmp_idxlist *headp)128{129struct index *i;130131while ((i = STAILQ_FIRST(headp)) != NULL) {132STAILQ_REMOVE_HEAD(headp, link);133if (i->tc == SNMP_TC_OWN)134enum_pairs_free(i->snmp_enum);135free(i);136}137138STAILQ_INIT(headp);139}140141static void142snmp_mapping_table_listfree(struct snmp_table_index *headp)143{144struct snmp_index_entry *t;145146while ((t = SLIST_FIRST(headp)) != NULL) {147SLIST_REMOVE_HEAD(headp, link);148149if (t->string)150free(t->string);151152snmp_index_listfree(&(t->index_list));153free(t);154}155}156157static void158snmp_enumtc_listfree(struct snmp_enum_tc *headp)159{160struct enum_type *t;161162while ((t = SLIST_FIRST(headp)) != NULL) {163SLIST_REMOVE_HEAD(headp, link);164165if (t->name)166free(t->name);167enum_pairs_free(t->snmp_enum);168free(t);169}170}171172int173snmp_mapping_free(struct snmp_toolinfo *snmptoolctx)174{175if (snmptoolctx == NULL || snmptoolctx->mappings == NULL)176return (-1);177178snmp_mapping_listfree(&snmptoolctx->snmp_nodelist);179snmp_mapping_listfree(&snmptoolctx->snmp_intlist);180snmp_mapping_listfree(&snmptoolctx->snmp_octlist);181snmp_mapping_listfree(&snmptoolctx->snmp_oidlist);182snmp_mapping_listfree(&snmptoolctx->snmp_iplist);183snmp_mapping_listfree(&snmptoolctx->snmp_ticklist);184snmp_mapping_listfree(&snmptoolctx->snmp_cntlist);185snmp_mapping_listfree(&snmptoolctx->snmp_gaugelist);186snmp_mapping_listfree(&snmptoolctx->snmp_cnt64list);187snmp_mapping_listfree(&snmptoolctx->snmp_enumlist);188snmp_mapping_table_listfree(&snmptoolctx->snmp_tablelist);189snmp_enumtc_listfree(&snmptoolctx->snmp_tclist);190free(snmptoolctx->mappings);191192return (0);193}194195static void196snmp_dump_enumpairs(struct enum_pairs *headp)197{198struct enum_pair *entry;199200if (headp == NULL)201return;202203fprintf(stderr,"enums: ");204STAILQ_FOREACH(entry, headp, link)205fprintf(stderr,"%d - %s, ", entry->enum_val,206(entry->enum_str == NULL)?"NULL":entry->enum_str);207208fprintf(stderr,"; ");209}210211void212snmp_dump_oid2str(struct snmp_oid2str *entry)213{214char buf[ASN_OIDSTRLEN];215216if (entry != NULL) {217memset(buf, 0, sizeof(buf));218asn_oid2str_r(&(entry->var), buf);219DEBUG(stderr, "%s - %s - %d - %d - %d", buf, entry->string,220entry->syntax, entry->access, entry->strlen);221snmp_dump_enumpairs(entry->snmp_enum);222DEBUG(stderr,"%s \n", (entry->table_idx == NULL)?"No table":223entry->table_idx->string);224}225}226227static void228snmp_dump_indexlist(struct snmp_idxlist *headp)229{230struct index *entry;231232if (headp == NULL)233return;234235STAILQ_FOREACH(entry, headp, link) {236fprintf(stderr,"%d, ", entry->syntax);237snmp_dump_enumpairs(entry->snmp_enum);238}239240fprintf(stderr,"\n");241}242243/* Initialize the enum pairs list of a oid2str entry. */244struct enum_pairs *245enum_pairs_init(void)246{247struct enum_pairs *snmp_enum;248249if ((snmp_enum = malloc(sizeof(struct enum_pairs))) == NULL) {250syslog(LOG_ERR, "malloc() failed: %s", strerror(errno));251return (NULL);252}253254STAILQ_INIT(snmp_enum);255return (snmp_enum);256}257258/*259* Given a number and string, allocate memory for a (int, string) pair and add260* it to the given oid2str mapping entry's enum pairs list.261*/262int32_t263enum_pair_insert(struct enum_pairs *headp, int32_t enum_val, char *enum_str)264{265struct enum_pair *e_new;266267if ((e_new = calloc(1, sizeof(struct enum_pair))) == NULL) {268syslog(LOG_ERR, "calloc() failed: %s", strerror(errno));269return (-1);270}271272if ((e_new->enum_str = strdup(enum_str)) == NULL) {273syslog(LOG_ERR, "strdup() failed: %s", strerror(errno));274free(e_new);275return (-1);276}277278e_new->enum_val = enum_val;279STAILQ_INSERT_TAIL(headp, e_new, link);280281return (1);282283}284285/*286* Insert an entry in a list - entries are lexicographicaly order by asn_oid.287* Returns 1 on success, -1 if list is not initialized, 0 if a matching oid already288* exists. Error checking is left to calling function.289*/290static int291snmp_mapping_insert(struct snmp_mapping *headp, struct snmp_oid2str *entry)292{293int32_t rc;294struct snmp_oid2str *temp, *prev;295296if (entry == NULL)297return(-1);298299if ((prev = SLIST_FIRST(headp)) == NULL ||300asn_compare_oid(&(entry->var), &(prev->var)) < 0) {301SLIST_INSERT_HEAD(headp, entry, link);302return (1);303} else304rc = -1; /* Make the compiler happy. */305306SLIST_FOREACH(temp, headp, link) {307if ((rc = asn_compare_oid(&(entry->var), &(temp->var))) <= 0)308break;309prev = temp;310rc = -1;311}312313switch (rc) {314case 0:315/* Ops, matching OIDs - hope the rest info also matches. */316if (strncmp(temp->string, entry->string, entry->strlen)) {317syslog(LOG_INFO, "Matching OIDs with different string "318"mappings: old - %s, new - %s", temp->string,319entry->string);320return (-1);321}322/*323* Ok, we have that already.324* As long as the strings match - don't complain.325*/326return (0);327328case 1:329SLIST_INSERT_AFTER(temp, entry, link);330break;331332case -1:333SLIST_INSERT_AFTER(prev, entry, link);334break;335336default:337/* NOTREACHED */338return (-1);339}340341return (1);342}343344int32_t345snmp_node_insert(struct snmp_toolinfo *snmptoolctx, struct snmp_oid2str *entry)346{347if (snmptoolctx != NULL && snmptoolctx->mappings)348return (snmp_mapping_insert(&snmptoolctx->snmp_nodelist,entry));349350return (-1);351}352353static int32_t354snmp_int_insert(struct snmp_toolinfo *snmptoolctx, struct snmp_oid2str *entry)355{356if (snmptoolctx != NULL && snmptoolctx->mappings)357return (snmp_mapping_insert(&snmptoolctx->snmp_intlist,entry));358359return (-1);360}361362static int32_t363snmp_oct_insert(struct snmp_toolinfo *snmptoolctx, struct snmp_oid2str *entry)364{365if (snmptoolctx != NULL && snmptoolctx->mappings)366return (snmp_mapping_insert(&snmptoolctx->snmp_octlist,entry));367368return (-1);369}370371static int32_t372snmp_oid_insert(struct snmp_toolinfo *snmptoolctx, struct snmp_oid2str *entry)373{374if (snmptoolctx != NULL && snmptoolctx->mappings)375return (snmp_mapping_insert(&snmptoolctx->snmp_oidlist,entry));376377return (-1);378}379380static int32_t381snmp_ip_insert(struct snmp_toolinfo *snmptoolctx, struct snmp_oid2str *entry)382{383if (snmptoolctx != NULL && snmptoolctx->mappings)384return (snmp_mapping_insert(&snmptoolctx->snmp_iplist,entry));385386return (-1);387}388389static int32_t390snmp_tick_insert(struct snmp_toolinfo *snmptoolctx, struct snmp_oid2str *entry)391{392if (snmptoolctx != NULL && snmptoolctx->mappings)393return (snmp_mapping_insert(&snmptoolctx->snmp_ticklist,entry));394395return (-1);396}397398static int32_t399snmp_cnt_insert(struct snmp_toolinfo *snmptoolctx, struct snmp_oid2str *entry)400{401if (snmptoolctx != NULL && snmptoolctx->mappings)402return (snmp_mapping_insert(&snmptoolctx->snmp_cntlist,entry));403404return (-1);405}406407static int32_t408snmp_gauge_insert(struct snmp_toolinfo *snmptoolctx, struct snmp_oid2str *entry)409{410if (snmptoolctx != NULL && snmptoolctx->mappings)411return (snmp_mapping_insert(&snmptoolctx->snmp_gaugelist,entry));412413return (-1);414}415416static int32_t417snmp_cnt64_insert(struct snmp_toolinfo *snmptoolctx, struct snmp_oid2str *entry)418{419if (snmptoolctx != NULL && snmptoolctx->mappings)420return (snmp_mapping_insert(&snmptoolctx->snmp_cnt64list,entry));421422return (-1);423}424425int32_t426snmp_enum_insert(struct snmp_toolinfo *snmptoolctx, struct snmp_oid2str *entry)427{428if (snmptoolctx != NULL && snmptoolctx->mappings)429return (snmp_mapping_insert(&snmptoolctx->snmp_enumlist,entry));430431return (-1);432}433434int32_t435snmp_leaf_insert(struct snmp_toolinfo *snmptoolctx, struct snmp_oid2str *entry)436{437switch (entry->syntax) {438case SNMP_SYNTAX_INTEGER:439return (snmp_int_insert(snmptoolctx, entry));440case SNMP_SYNTAX_OCTETSTRING:441return (snmp_oct_insert(snmptoolctx, entry));442case SNMP_SYNTAX_OID:443return (snmp_oid_insert(snmptoolctx, entry));444case SNMP_SYNTAX_IPADDRESS:445return (snmp_ip_insert(snmptoolctx, entry));446case SNMP_SYNTAX_COUNTER:447return (snmp_cnt_insert(snmptoolctx, entry));448case SNMP_SYNTAX_GAUGE:449return (snmp_gauge_insert(snmptoolctx, entry));450case SNMP_SYNTAX_TIMETICKS:451return (snmp_tick_insert(snmptoolctx, entry));452case SNMP_SYNTAX_COUNTER64:453return (snmp_cnt64_insert(snmptoolctx, entry));454default:455break;456}457458return (-1);459}460461static int32_t462snmp_index_insert(struct snmp_idxlist *headp, struct index *idx)463{464if (headp == NULL || idx == NULL)465return (-1);466467STAILQ_INSERT_TAIL(headp, idx, link);468return (1);469}470471int32_t472snmp_syntax_insert(struct snmp_idxlist *headp, struct enum_pairs *enums,473enum snmp_syntax syntax, enum snmp_tc tc)474{475struct index *idx;476477if ((idx = calloc(1, sizeof(struct index))) == NULL) {478syslog(LOG_ERR, "malloc() failed: %s", strerror(errno));479return (-1);480}481482if (snmp_index_insert(headp, idx) < 0) {483free(idx);484return (-1);485}486487idx->syntax = syntax;488idx->snmp_enum = enums;489idx->tc = tc;490491return (1);492}493494int32_t495snmp_table_insert(struct snmp_toolinfo *snmptoolctx,496struct snmp_index_entry *entry)497{498int32_t rc;499struct snmp_index_entry *temp, *prev;500501if (snmptoolctx == NULL || snmptoolctx->mappings == NULL ||502entry == NULL)503return(-1);504505if ((prev = SLIST_FIRST(&snmptoolctx->snmp_tablelist)) == NULL ||506asn_compare_oid(&(entry->var), &(prev->var)) < 0) {507SLIST_INSERT_HEAD(&snmptoolctx->snmp_tablelist, entry, link);508return (1);509} else510rc = -1; /* Make the compiler happy. */511512SLIST_FOREACH(temp, &snmptoolctx->snmp_tablelist, link) {513if ((rc = asn_compare_oid(&(entry->var), &(temp->var))) <= 0)514break;515prev = temp;516rc = -1;517}518519switch (rc) {520case 0:521/* Ops, matching OIDs - hope the rest info also matches. */522if (strncmp(temp->string, entry->string, entry->strlen)) {523syslog(LOG_INFO, "Matching OIDs with different string "524"mapping - old - %s, new - %s", temp->string,525entry->string);526return (-1);527}528return(0);529530case 1:531SLIST_INSERT_AFTER(temp, entry, link);532break;533534case -1:535SLIST_INSERT_AFTER(prev, entry, link);536break;537538default:539/* NOTREACHED */540return (-1);541}542543return (1);544}545546struct enum_type *547snmp_enumtc_init(char *name)548{549struct enum_type *enum_tc;550551if ((enum_tc = calloc(1, sizeof(struct enum_type))) == NULL) {552syslog(LOG_ERR, "malloc() failed: %s", strerror(errno));553return (NULL);554}555556if ((enum_tc->name = strdup(name)) == NULL) {557syslog(LOG_ERR, "malloc() failed: %s", strerror(errno));558free(enum_tc);559return (NULL);560}561562return (enum_tc);563}564565void566snmp_enumtc_free(struct enum_type *tc)567{568if (tc->name)569free(tc->name);570if (tc->snmp_enum)571enum_pairs_free(tc->snmp_enum);572free(tc);573}574575void576snmp_enumtc_insert(struct snmp_toolinfo *snmptoolctx, struct enum_type *entry)577{578if (snmptoolctx == NULL || snmptoolctx->mappings == NULL)579return; /* XXX no error handling? */580581SLIST_INSERT_HEAD(&snmptoolctx->snmp_tclist, entry, link);582}583584struct enum_type *585snmp_enumtc_lookup(struct snmp_toolinfo *snmptoolctx, char *name)586{587struct enum_type *temp;588589if (snmptoolctx == NULL || snmptoolctx->mappings == NULL)590return (NULL);591592SLIST_FOREACH(temp, &snmptoolctx->snmp_tclist, link) {593if (strcmp(temp->name, name) == 0)594return (temp);595}596return (NULL);597}598599static void600snmp_mapping_dumplist(struct snmp_mapping *headp)601{602char buf[ASN_OIDSTRLEN];603struct snmp_oid2str *entry;604605if (headp == NULL)606return;607608SLIST_FOREACH(entry,headp,link) {609memset(buf, 0, sizeof(buf));610asn_oid2str_r(&(entry->var), buf);611fprintf(stderr, "%s - %s - %d - %d - %d", buf, entry->string,612entry->syntax, entry->access ,entry->strlen);613fprintf(stderr," - %s \n", (entry->table_idx == NULL)?614"No table":entry->table_idx->string);615}616}617618static void619snmp_mapping_dumptable(struct snmp_table_index *headp)620{621char buf[ASN_OIDSTRLEN];622struct snmp_index_entry *entry;623624if (headp == NULL)625return;626627SLIST_FOREACH(entry, headp, link) {628memset(buf, 0, sizeof(buf));629asn_oid2str_r(&(entry->var), buf);630fprintf(stderr,"%s - %s - %d - ", buf, entry->string,631entry->strlen);632snmp_dump_indexlist(&(entry->index_list));633}634}635636void637snmp_mapping_dump(struct snmp_toolinfo *snmptoolctx /* int bits */)638{639if (!_bsnmptools_debug)640return;641642if (snmptoolctx == NULL) {643fprintf(stderr,"No snmptool context!\n");644return;645}646647if (snmptoolctx->mappings == NULL) {648fprintf(stderr,"No mappings!\n");649return;650}651652fprintf(stderr,"snmp_nodelist:\n");653snmp_mapping_dumplist(&snmptoolctx->snmp_nodelist);654655fprintf(stderr,"snmp_intlist:\n");656snmp_mapping_dumplist(&snmptoolctx->snmp_intlist);657658fprintf(stderr,"snmp_octlist:\n");659snmp_mapping_dumplist(&snmptoolctx->snmp_octlist);660661fprintf(stderr,"snmp_oidlist:\n");662snmp_mapping_dumplist(&snmptoolctx->snmp_oidlist);663664fprintf(stderr,"snmp_iplist:\n");665snmp_mapping_dumplist(&snmptoolctx->snmp_iplist);666667fprintf(stderr,"snmp_ticklist:\n");668snmp_mapping_dumplist(&snmptoolctx->snmp_ticklist);669670fprintf(stderr,"snmp_cntlist:\n");671snmp_mapping_dumplist(&snmptoolctx->snmp_cntlist);672673fprintf(stderr,"snmp_gaugelist:\n");674snmp_mapping_dumplist(&snmptoolctx->snmp_gaugelist);675676fprintf(stderr,"snmp_cnt64list:\n");677snmp_mapping_dumplist(&snmptoolctx->snmp_cnt64list);678679fprintf(stderr,"snmp_enumlist:\n");680snmp_mapping_dumplist(&snmptoolctx->snmp_enumlist);681682fprintf(stderr,"snmp_tablelist:\n");683snmp_mapping_dumptable(&snmptoolctx->snmp_tablelist);684}685686char *687enum_string_lookup(struct enum_pairs *headp, int32_t enum_val)688{689struct enum_pair *temp;690691if (headp == NULL)692return (NULL);693694STAILQ_FOREACH(temp, headp, link) {695if (temp->enum_val == enum_val)696return (temp->enum_str);697}698699return (NULL);700}701702int32_t703enum_number_lookup(struct enum_pairs *headp, char *e_str)704{705struct enum_pair *tmp;706707if (headp == NULL)708return (-1);709710STAILQ_FOREACH(tmp, headp, link)711if (strncmp(tmp->enum_str, e_str, strlen(tmp->enum_str)) == 0)712return (tmp->enum_val);713714return (-1);715}716717static int32_t718snmp_lookuplist_string(struct snmp_mapping *headp, struct snmp_object *s)719{720struct snmp_oid2str *temp;721722if (headp == NULL)723return (-1);724725SLIST_FOREACH(temp, headp, link)726if (asn_compare_oid(&(temp->var), &(s->val.var)) == 0)727break;728729if ((s->info = temp) == NULL)730return (-1);731732return (1);733}734735/* provided an asn_oid find the corresponding string for it */736static int32_t737snmp_lookup_leaf(struct snmp_mapping *headp, struct snmp_object *s)738{739struct snmp_oid2str *temp;740741if (headp == NULL)742return (-1);743744SLIST_FOREACH(temp,headp,link) {745if ((asn_compare_oid(&(temp->var), &(s->val.var)) == 0) ||746(asn_is_suboid(&(temp->var), &(s->val.var)))) {747s->info = temp;748return (1);749}750}751752return (-1);753}754755int32_t756snmp_lookup_leafstring(struct snmp_toolinfo *snmptoolctx, struct snmp_object *s)757{758if (snmptoolctx == NULL || snmptoolctx->mappings == NULL || s == NULL)759return (-1);760761switch (s->val.syntax) {762case SNMP_SYNTAX_INTEGER:763return (snmp_lookup_leaf(&snmptoolctx->snmp_intlist, s));764case SNMP_SYNTAX_OCTETSTRING:765return (snmp_lookup_leaf(&snmptoolctx->snmp_octlist, s));766case SNMP_SYNTAX_OID:767return (snmp_lookup_leaf(&snmptoolctx->snmp_oidlist, s));768case SNMP_SYNTAX_IPADDRESS:769return (snmp_lookup_leaf(&snmptoolctx->snmp_iplist, s));770case SNMP_SYNTAX_COUNTER:771return (snmp_lookup_leaf(&snmptoolctx->snmp_cntlist, s));772case SNMP_SYNTAX_GAUGE:773return (snmp_lookup_leaf(774&snmptoolctx->snmp_gaugelist, s));775case SNMP_SYNTAX_TIMETICKS:776return (snmp_lookup_leaf(777&snmptoolctx->snmp_ticklist, s));778case SNMP_SYNTAX_COUNTER64:779return (snmp_lookup_leaf(780&snmptoolctx->snmp_cnt64list, s));781case SNMP_SYNTAX_NOSUCHOBJECT:782/* FALLTHROUGH */783case SNMP_SYNTAX_NOSUCHINSTANCE:784/* FALLTHROUGH */785case SNMP_SYNTAX_ENDOFMIBVIEW:786return (snmp_lookup_allstring(snmptoolctx, s));787default:788warnx("Unknown syntax - %d", s->val.syntax);789break;790}791792return (-1);793}794795int32_t796snmp_lookup_enumstring(struct snmp_toolinfo *snmptoolctx, struct snmp_object *s)797{798if (snmptoolctx == NULL || snmptoolctx->mappings == NULL || s == NULL)799return (-1);800801return (snmp_lookuplist_string(&snmptoolctx->snmp_enumlist, s));802}803804int32_t805snmp_lookup_oidstring(struct snmp_toolinfo *snmptoolctx, struct snmp_object *s)806{807if (snmptoolctx == NULL || snmptoolctx->mappings == NULL || s == NULL)808return (-1);809810return (snmp_lookuplist_string(&snmptoolctx->snmp_oidlist, s));811}812813int32_t814snmp_lookup_nodestring(struct snmp_toolinfo *snmptoolctx, struct snmp_object *s)815{816if (snmptoolctx == NULL || snmptoolctx->mappings == NULL || s == NULL)817return (-1);818819return (snmp_lookuplist_string(&snmptoolctx->snmp_nodelist, s));820}821822int32_t823snmp_lookup_allstring(struct snmp_toolinfo *snmptoolctx, struct snmp_object *s)824{825if (snmptoolctx == NULL || snmptoolctx->mappings == NULL)826return (-1);827828if (snmp_lookup_leaf(&snmptoolctx->snmp_intlist, s) > 0)829return (1);830if (snmp_lookup_leaf(&snmptoolctx->snmp_octlist, s) > 0)831return (1);832if (snmp_lookup_leaf(&snmptoolctx->snmp_oidlist, s) > 0)833return (1);834if (snmp_lookup_leaf(&snmptoolctx->snmp_iplist, s) > 0)835return (1);836if (snmp_lookup_leaf(&snmptoolctx->snmp_cntlist, s) > 0)837return (1);838if (snmp_lookup_leaf(&snmptoolctx->snmp_gaugelist, s) > 0)839return (1);840if (snmp_lookup_leaf(&snmptoolctx->snmp_ticklist, s) > 0)841return (1);842if (snmp_lookup_leaf(&snmptoolctx->snmp_cnt64list, s) > 0)843return (1);844if (snmp_lookuplist_string(&snmptoolctx->snmp_enumlist, s) > 0)845return (1);846if (snmp_lookuplist_string(&snmptoolctx->snmp_nodelist, s) > 0)847return (1);848849return (-1);850}851852int32_t853snmp_lookup_nonleaf_string(struct snmp_toolinfo *snmptoolctx,854struct snmp_object *s)855{856if (snmptoolctx == NULL)857return (-1);858859if (snmp_lookuplist_string(&snmptoolctx->snmp_nodelist, s) > 0)860return (1);861if (snmp_lookuplist_string(&snmptoolctx->snmp_enumlist, s) > 0)862return (1);863864return (-1);865}866867static int32_t868snmp_lookup_oidlist(struct snmp_mapping *hp, struct snmp_object *s, char *oid)869{870struct snmp_oid2str *temp;871872if (hp == NULL)873return (-1);874875SLIST_FOREACH(temp, hp, link) {876if (temp->strlen != strlen(oid))877continue;878879if (strncmp(temp->string, oid, temp->strlen))880continue;881882s->val.syntax = temp->syntax;883s->info = temp;884asn_append_oid(&(s->val.var), &(temp->var));885return (1);886}887888return (-1);889}890891static int32_t892snmp_lookup_tablelist(struct snmp_toolinfo *snmptoolctx,893struct snmp_table_index *headp, struct snmp_object *s, char *oid)894{895struct snmp_index_entry *temp;896897if (snmptoolctx == NULL || headp == NULL)898return (-1);899900SLIST_FOREACH(temp, headp, link) {901if (temp->strlen != strlen(oid))902continue;903904if (strncmp(temp->string, oid, temp->strlen))905continue;906907/*908* Another hack here - if we were given a table name909* return the corresponding pointer to it's entry.910* That should not change the reponce we'll get.911*/912s->val.syntax = SNMP_SYNTAX_NULL;913asn_append_oid(&(s->val.var), &(temp->var));914if (snmp_lookup_leaf(&snmptoolctx->snmp_nodelist, s) > 0)915return (1);916else917return (-1);918}919920return (-1);921}922923int32_t924snmp_lookup_oidall(struct snmp_toolinfo *snmptoolctx, struct snmp_object *s,925char *oid)926{927if (snmptoolctx == NULL || s == NULL || oid == NULL)928return (-1);929930if (snmp_lookup_oidlist(&snmptoolctx->snmp_intlist, s, oid) > 0)931return (1);932if (snmp_lookup_oidlist(&snmptoolctx->snmp_octlist, s, oid) > 0)933return (1);934if (snmp_lookup_oidlist(&snmptoolctx->snmp_oidlist, s, oid) > 0)935return (1);936if (snmp_lookup_oidlist(&snmptoolctx->snmp_iplist, s, oid) > 0)937return (1);938if (snmp_lookup_oidlist(&snmptoolctx->snmp_ticklist, s, oid) > 0)939return (1);940if (snmp_lookup_oidlist(&snmptoolctx->snmp_cntlist, s, oid) > 0)941return (1);942if (snmp_lookup_oidlist(&snmptoolctx->snmp_gaugelist, s, oid) > 0)943return (1);944if (snmp_lookup_oidlist(&snmptoolctx->snmp_cnt64list, s, oid) > 0)945return (1);946if (snmp_lookup_oidlist(&snmptoolctx->snmp_nodelist, s, oid) > 0)947return (1);948if (snmp_lookup_tablelist(snmptoolctx, &snmptoolctx->snmp_tablelist,949s, oid) > 0)950return (1);951952return (-1);953}954955int32_t956snmp_lookup_enumoid(struct snmp_toolinfo *snmptoolctx, struct snmp_object *s,957char *oid)958{959if (snmptoolctx == NULL || s == NULL)960return (-1);961962return (snmp_lookup_oidlist(&snmptoolctx->snmp_enumlist, s, oid));963}964965int32_t966snmp_lookup_oid(struct snmp_toolinfo *snmptoolctx, struct snmp_object *s,967char *oid)968{969if (snmptoolctx == NULL || s == NULL)970return (-1);971972switch (s->val.syntax) {973case SNMP_SYNTAX_INTEGER:974return (snmp_lookup_oidlist(&snmptoolctx->snmp_intlist,975s, oid));976case SNMP_SYNTAX_OCTETSTRING:977return (snmp_lookup_oidlist(&snmptoolctx->snmp_octlist,978s, oid));979case SNMP_SYNTAX_OID:980return (snmp_lookup_oidlist(&snmptoolctx->snmp_oidlist,981s, oid));982case SNMP_SYNTAX_IPADDRESS:983return (snmp_lookup_oidlist(&snmptoolctx->snmp_iplist,984s, oid));985case SNMP_SYNTAX_COUNTER:986return (snmp_lookup_oidlist(&snmptoolctx->snmp_cntlist,987s, oid));988case SNMP_SYNTAX_GAUGE:989return (snmp_lookup_oidlist(&snmptoolctx->snmp_gaugelist,990s, oid));991case SNMP_SYNTAX_TIMETICKS:992return (snmp_lookup_oidlist(&snmptoolctx->snmp_ticklist,993s, oid));994case SNMP_SYNTAX_COUNTER64:995return (snmp_lookup_oidlist(&snmptoolctx->snmp_cnt64list,996s, oid));997case SNMP_SYNTAX_NULL:998return (snmp_lookup_oidlist(&snmptoolctx->snmp_nodelist,999s, oid));1000default:1001warnx("Unknown syntax - %d", s->val.syntax);1002break;1003}10041005return (-1);1006}100710081009