diff options
author | Reyk Floeter <reyk@cvs.openbsd.org> | 2007-12-28 16:59:32 +0000 |
---|---|---|
committer | Reyk Floeter <reyk@cvs.openbsd.org> | 2007-12-28 16:59:32 +0000 |
commit | e67798564e2c2f16e166c1dd27c68911097be333 (patch) | |
tree | 9f32487986c0f8bf37a11f93bf9d5b734353d452 | |
parent | 6f3b6397427423d12b99c79d668ea70e43d4748c (diff) |
split code handling the "structure of management information" and the
"message processing subsystem" into separate files smi.c and mps.c
-rw-r--r-- | usr.sbin/snmpd/Makefile | 4 | ||||
-rw-r--r-- | usr.sbin/snmpd/kroute.c | 14 | ||||
-rw-r--r-- | usr.sbin/snmpd/mib.c | 24 | ||||
-rw-r--r-- | usr.sbin/snmpd/mps.c | 197 | ||||
-rw-r--r-- | usr.sbin/snmpd/parse.y | 6 | ||||
-rw-r--r-- | usr.sbin/snmpd/smi.c | 234 | ||||
-rw-r--r-- | usr.sbin/snmpd/snmpd.c | 4 | ||||
-rw-r--r-- | usr.sbin/snmpd/snmpd.h | 24 | ||||
-rw-r--r-- | usr.sbin/snmpd/snmpe.c | 10 |
9 files changed, 291 insertions, 226 deletions
diff --git a/usr.sbin/snmpd/Makefile b/usr.sbin/snmpd/Makefile index 91bcb0d7700..4e303733c40 100644 --- a/usr.sbin/snmpd/Makefile +++ b/usr.sbin/snmpd/Makefile @@ -1,9 +1,9 @@ -# $OpenBSD: Makefile,v 1.2 2007/12/06 04:07:47 deraadt Exp $ +# $OpenBSD: Makefile,v 1.3 2007/12/28 16:59:31 reyk Exp $ PROG= snmpd MAN= snmpd.8 snmpd.conf.5 SRCS= parse.y ber.c log.c control.c buffer.c imsg.c snmpe.c \ - mps.c mib.c kroute.c snmpd.c + mps.c mib.c smi.c kroute.c snmpd.c LDADD= -levent DPADD= ${LIBEVENT} diff --git a/usr.sbin/snmpd/kroute.c b/usr.sbin/snmpd/kroute.c index 3d598389a3c..0897bc1b0eb 100644 --- a/usr.sbin/snmpd/kroute.c +++ b/usr.sbin/snmpd/kroute.c @@ -1,4 +1,4 @@ -/* $OpenBSD: kroute.c,v 1.3 2007/12/28 15:33:37 reyk Exp $ */ +/* $OpenBSD: kroute.c,v 1.4 2007/12/28 16:59:31 reyk Exp $ */ /* * Copyright (c) 2007 Reyk Floeter <reyk@vantronix.net> @@ -404,7 +404,7 @@ kif_insert(u_short if_index) fatalx("kif_insert: RB_INSERT"); kr_state.ks_nkif++; - kr_state.ks_iflastchange = mps_getticks(); + kr_state.ks_iflastchange = smi_getticks(); return (kif); } @@ -426,7 +426,7 @@ kif_remove(struct kif_node *kif) free(kif); kr_state.ks_nkif--; - kr_state.ks_iflastchange = mps_getticks(); + kr_state.ks_iflastchange = smi_getticks(); return (0); } @@ -439,7 +439,7 @@ kif_clear(void) while ((kif = RB_MIN(kif_tree, &kit)) != NULL) kif_remove(kif); kr_state.ks_nkif = 0; - kr_state.ks_iflastchange = mps_getticks(); + kr_state.ks_iflastchange = smi_getticks(); } struct kif * @@ -456,7 +456,7 @@ kif_update(u_short if_index, int flags, struct if_data *ifd, kif->k.if_flags = flags; bcopy(ifd, &kif->k.if_data, sizeof(struct if_data)); - kif->k.if_ticks = mps_getticks(); + kif->k.if_ticks = smi_getticks(); if (sdl && sdl->sdl_family == AF_LINK) { if (sdl->sdl_nlen >= sizeof(kif->k.if_name)) @@ -983,7 +983,7 @@ dispatch_rtmsg(int fd, short event, void *arg) kr->r.nexthop.s_addr = nexthop.s_addr; kr->r.flags = flags; kr->r.if_index = if_index; - kr->r.ticks = mps_getticks(); + kr->r.ticks = smi_getticks(); rtlabel_unref(kr->r.rtlabel); kr->r.rtlabel = 0; @@ -1009,7 +1009,7 @@ add: kr->r.nexthop.s_addr = nexthop.s_addr; kr->r.flags = flags; kr->r.if_index = if_index; - kr->r.ticks = mps_getticks(); + kr->r.ticks = smi_getticks(); if ((label = (struct sockaddr_rtlabel *) rti_info[RTAX_LABEL]) != NULL) { diff --git a/usr.sbin/snmpd/mib.c b/usr.sbin/snmpd/mib.c index d911268ca3b..11ff6916d91 100644 --- a/usr.sbin/snmpd/mib.c +++ b/usr.sbin/snmpd/mib.c @@ -1,4 +1,4 @@ -/* $OpenBSD: mib.c,v 1.14 2007/12/28 16:27:51 reyk Exp $ */ +/* $OpenBSD: mib.c,v 1.15 2007/12/28 16:59:31 reyk Exp $ */ /* * Copyright (c) 2007 Reyk Floeter <reyk@vantronix.net> @@ -136,11 +136,11 @@ mib_getsys(struct oid *oid, struct ber_oid *o, struct ber_element **elm) case 2: if (so == NULL) so = &sysoid; - mps_oidlen(so); + smi_oidlen(so); *elm = ber_add_oid(*elm, so); break; case 3: - ticks = mps_getticks(); + ticks = smi_getticks(); *elm = ber_add_integer(*elm, ticks); ber_set_header(*elm, BER_CLASS_APPLICATION, SNMP_T_TIMETICKS); break; @@ -186,7 +186,7 @@ mib_sysor(struct oid *oid, struct ber_oid *o, struct ber_element **elm) /* Count MIB root OIDs in the tree */ for (next = NULL; - (next = mps_foreach(next, OID_MIB)) != NULL; nmib++); + (next = smi_foreach(next, OID_MIB)) != NULL; nmib++); /* Get and verify the current row index */ idx = o->bo_id[OIDIDX_sysOREntry]; @@ -195,7 +195,7 @@ mib_sysor(struct oid *oid, struct ber_oid *o, struct ber_element **elm) /* Find the MIB root element for this Id */ for (next = miboid = NULL, nmib = 1; - (next = mps_foreach(next, OID_MIB)) != NULL; nmib++) { + (next = smi_foreach(next, OID_MIB)) != NULL; nmib++) { if (nmib == idx) miboid = next; } @@ -218,7 +218,7 @@ mib_sysor(struct oid *oid, struct ber_oid *o, struct ber_element **elm) * But we use the symbolic OID string for now, it may * help to display names of internal OIDs. */ - mps_oidstring(&miboid->o_id, buf, sizeof(buf)); + smi_oidstring(&miboid->o_id, buf, sizeof(buf)); if ((ptr = strdup(buf)) == NULL) { ber = ber_add_string(ber, miboid->o_name); } else { @@ -720,7 +720,7 @@ mib_ifrcvtable(struct oid *oid, struct ber_oid *o, struct ber_element **elm) idx = 0; o->bo_id[OIDIDX_ifRcvAddressEntry + idx++] = kif->if_index; o->bo_id[OIDIDX_ifRcvAddressEntry + idx] = 0; - mps_oidlen(o); + smi_oidlen(o); /* extend the OID with the lladdr length and octets */ o->bo_id[OIDIDX_ifRcvAddressEntry + idx++] = sizeof(kif->if_lladdr); @@ -1185,21 +1185,21 @@ mib_init(void) /* * MIB declarations (to register the OID names) */ - mps_mibtree(mib_tree); + smi_mibtree(mib_tree); /* * MIB definitions (the implementation) */ /* SNMPv2-MIB */ - mps_mibtree(base_mib); + smi_mibtree(base_mib); /* IF-MIB */ - mps_mibtree(if_mib); + smi_mibtree(if_mib); /* IP-MIB */ - mps_mibtree(ip_mib); + smi_mibtree(ip_mib); /* OPENBSD-MIB */ - mps_mibtree(openbsd_mib); + smi_mibtree(openbsd_mib); } diff --git a/usr.sbin/snmpd/mps.c b/usr.sbin/snmpd/mps.c index 7dcf0f197a2..ba5ddda0743 100644 --- a/usr.sbin/snmpd/mps.c +++ b/usr.sbin/snmpd/mps.c @@ -1,4 +1,4 @@ -/* $OpenBSD: mps.c,v 1.6 2007/12/28 16:27:51 reyk Exp $ */ +/* $OpenBSD: mps.c,v 1.7 2007/12/28 16:59:31 reyk Exp $ */ /* * Copyright (c) 2007 Reyk Floeter <reyk@vantronix.net> @@ -46,32 +46,11 @@ #include "snmpd.h" #include "mib.h" -RB_HEAD(oidtree, oid); -RB_PROTOTYPE(oidtree, oid, o_element, mps_oid_cmp); - extern struct snmpd *env; -struct oidtree mps_oidtree; struct ber_oid * mps_table(struct oid *, struct ber_oid *, struct ber_oid *); -u_long -mps_getticks(void) -{ - struct timeval now, run; - u_long ticks; - - gettimeofday(&now, NULL); - if (timercmp(&now, &env->sc_starttime, <=)) - return (0); - timersub(&now, &env->sc_starttime, &run); - ticks = run.tv_sec * 100; - if (run.tv_usec) - ticks += run.tv_usec / 10000; - - return (ticks); -} - int mps_getstr(struct oid *oid, struct ber_oid *o, struct ber_element **elm) { @@ -131,54 +110,17 @@ mps_getts(struct oid *oid, struct ber_oid *o, struct ber_element **elm) return (0); } -void -mps_oidlen(struct ber_oid *o) -{ - size_t i; - for (i = 0; i < BER_MAX_OID_LEN && o->bo_id[i] != 0; i++); - o->bo_n = i; -} - -char * -mps_oidstring(struct ber_oid *o, char *buf, size_t len) -{ - char str[256]; - struct oid *value, key; - size_t i, lookup = 1; - - bzero(buf, len); - bcopy(o, &key.o_id, sizeof(struct ber_oid)); - key.o_flags |= OID_TABLE; /* do not match wildcards */ - - if (env->sc_flags & SNMPD_F_NONAMES) - lookup = 0; - - for (i = 0; i < o->bo_n; i++) { - key.o_oidlen = i + 1; - if (lookup && - (value = RB_FIND(oidtree, &mps_oidtree, &key)) != NULL) - snprintf(str, sizeof(str), "%s", value->o_name); - else - snprintf(str, sizeof(str), "%d", key.o_oid[i]); - strlcat(buf, str, len); - if (i < (o->bo_n - 1)) - strlcat(buf, ".", len); - } - - return (buf); -} - struct ber_element * mps_getreq(struct ber_element *root, struct ber_oid *o) { struct ber_element *elm = root; struct oid key, *value; - mps_oidlen(o); + smi_oidlen(o); if (o->bo_n > BER_MAX_OID_LEN) return (NULL); bcopy(o, &key.o_id, sizeof(struct ber_oid)); - value = RB_FIND(oidtree, &mps_oidtree, &key); + value = smi_find(&key); if (value == NULL) return (NULL); if (OID_NOTSET(value)) @@ -199,11 +141,11 @@ mps_setreq(struct ber_element *ber, struct ber_oid *o) { struct oid key, *value; - mps_oidlen(o); + smi_oidlen(o); if (o->bo_n > BER_MAX_OID_LEN) return (-1); bcopy(o, &key.o_id, sizeof(struct ber_oid)); - value = RB_FIND(oidtree, &mps_oidtree, &key); + value = smi_find(&key); if (value == NULL) return (-1); if ((value->o_flags & OID_WR) == 0 || @@ -221,11 +163,11 @@ mps_getnextreq(struct ber_element *root, struct ber_oid *o) int ret; struct ber_oid no; - mps_oidlen(o); + smi_oidlen(o); if (o->bo_n > BER_MAX_OID_LEN) return (NULL); bcopy(o, &key.o_id, sizeof(struct ber_oid)); - value = RB_FIND(oidtree, &mps_oidtree, &key); + value = smi_find(&key); if (value == NULL) return (NULL); if (value->o_flags & OID_TABLE) { @@ -244,7 +186,7 @@ mps_getnextreq(struct ber_element *root, struct ber_oid *o) } } for (next = value; next != NULL;) { - next = RB_NEXT(oidtree, &mps_oidtree, next); + next = smi_next(next); if (next == NULL) break; if (!OID_NOTSET(next) && next->o_get != NULL) @@ -275,11 +217,11 @@ mps_set(struct ber_oid *o, void *p, long long len) { struct oid key, *value; - mps_oidlen(o); + smi_oidlen(o); if (o->bo_n > BER_MAX_OID_LEN) return (-1); bcopy(o, &key.o_id, sizeof(struct ber_oid)); - value = RB_FIND(oidtree, &mps_oidtree, &key); + value = smi_find(&key); if (value == NULL) return (-1); if (value->o_data != NULL) @@ -320,7 +262,7 @@ mps_table(struct oid *oid, struct ber_oid *o, struct ber_oid *no) bcopy(&oid->o_id, &b.o_id, sizeof(struct ber_oid)); b.o_oidlen--; b.o_flags |= OID_TABLE; - if (mps_oid_cmp(&a, &b) == 0) { + if (smi_oid_cmp(&a, &b) == 0) { col = oid->o_oid[id]; if (col > o->bo_id[id]) idx = 1; @@ -336,122 +278,7 @@ mps_table(struct oid *oid, struct ber_oid *o, struct ber_oid *no) if (!no->bo_id[subid]) no->bo_id[subid]++; - mps_oidlen(no); + smi_oidlen(no); return (no); } - -void -mps_delete(struct oid *oid) -{ - struct oid key, *value; - - bcopy(&oid->o_id, &key.o_id, sizeof(struct ber_oid)); - if ((value = RB_FIND(oidtree, &mps_oidtree, &key)) != NULL && - value == oid) - RB_REMOVE(oidtree, &mps_oidtree, value); - - if (oid->o_data != NULL) - free(oid->o_data); - if (oid->o_flags & OID_DYNAMIC) { - free(oid->o_name); - free(oid); - } -} - -void -mps_insert(struct oid *oid) -{ - struct oid key, *value; - - if ((oid->o_flags & OID_TABLE) && oid->o_get == NULL) - fatalx("mps_insert: invalid MIB table"); - - bcopy(&oid->o_id, &key.o_id, sizeof(struct ber_oid)); - value = RB_FIND(oidtree, &mps_oidtree, &key); - if (value != NULL) - mps_delete(value); - - RB_INSERT(oidtree, &mps_oidtree, oid); -} - -void -mps_mibtree(struct oid *oids) -{ - struct oid *oid, *decl; - size_t i; - - for (i = 0; oids[i].o_oid[0] != 0; i++) { - oid = &oids[i]; - mps_oidlen(&oid->o_id); - if (oid->o_name != NULL) { - if ((oid->o_flags & OID_TABLE) && oid->o_get == NULL) - fatalx("mps_mibtree: invalid MIB table"); - RB_INSERT(oidtree, &mps_oidtree, oid); - continue; - } - decl = RB_FIND(oidtree, &mps_oidtree, oid); - if (decl == NULL) - fatalx("mps_mibtree: undeclared MIB"); - decl->o_flags = oid->o_flags; - decl->o_get = oid->o_get; - decl->o_set = oid->o_set; - decl->o_val = oid->o_val; - decl->o_data = oid->o_data; - } -} - -int -mps_init(void) -{ - RB_INIT(&mps_oidtree); - mib_init(); - return (0); -} - -struct oid * -mps_foreach(struct oid *oid, u_int flags) -{ - /* - * Traverse the tree of MIBs with the option to check - * for specific OID flags. - */ - if (oid == NULL) { - oid = RB_MIN(oidtree, &mps_oidtree); - if (oid == NULL) - return (NULL); - if (flags == 0 || (oid->o_flags & flags)) - return (oid); - } - for (;;) { - oid = RB_NEXT(oidtree, &mps_oidtree, oid); - if (oid == NULL) - break; - if (flags == 0 || (oid->o_flags & flags)) - return (oid); - } - - return (oid); -} - -long -mps_oid_cmp(struct oid *a, struct oid *b) -{ - size_t i; - - for (i = 0; i < MIN(a->o_oidlen, b->o_oidlen); i++) - if (a->o_oid[i] != b->o_oid[i]) - return (a->o_oid[i] - b->o_oid[i]); - - /* - * Return success if the matched object is a table - * (it will match any sub-elements) - */ - if ((b->o_flags & OID_TABLE) && - (a->o_flags & OID_TABLE) == 0) - return (0); - - return (a->o_oidlen - b->o_oidlen); -} - -RB_GENERATE(oidtree, oid, o_element, mps_oid_cmp); diff --git a/usr.sbin/snmpd/parse.y b/usr.sbin/snmpd/parse.y index fd0f235fed3..5d7ac29fe27 100644 --- a/usr.sbin/snmpd/parse.y +++ b/usr.sbin/snmpd/parse.y @@ -1,4 +1,4 @@ -/* $OpenBSD: parse.y,v 1.5 2007/12/28 15:33:37 reyk Exp $ */ +/* $OpenBSD: parse.y,v 1.6 2007/12/28 16:59:31 reyk Exp $ */ /* * Copyright (c) 2007 Reyk Floeter <reyk@vantronix.net> @@ -240,7 +240,7 @@ mib : OBJECTID oid NAME STRING optwrite objtype { YYERROR; } - mps_oidlen($2); + smi_oidlen($2); bcopy($2, &oid->o_id, sizeof(struct ber_oid)); free($2); oid->o_name = $4; @@ -260,7 +260,7 @@ mib : OBJECTID oid NAME STRING optwrite objtype { if ($5) oid->o_flags |= OID_WR; - mps_insert(oid); + smi_insert(oid); } ; diff --git a/usr.sbin/snmpd/smi.c b/usr.sbin/snmpd/smi.c new file mode 100644 index 00000000000..83f8414a46b --- /dev/null +++ b/usr.sbin/snmpd/smi.c @@ -0,0 +1,234 @@ +/* $OpenBSD: smi.c,v 1.1 2007/12/28 16:59:31 reyk Exp $ */ + +/* + * Copyright (c) 2007 Reyk Floeter <reyk@vantronix.net> + * + * Permission to use, copy, modify, and distribute this software for any + * purpose with or without fee is hereby granted, provided that the above + * copyright notice and this permission notice appear in all copies. + * + * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF + * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. + */ + +#include <sys/queue.h> +#include <sys/param.h> +#include <sys/types.h> +#include <sys/stat.h> +#include <sys/socket.h> +#include <sys/un.h> +#include <sys/tree.h> +#include <sys/sysctl.h> + +#include <net/if.h> +#include <net/if_dl.h> +#include <net/if_arp.h> +#include <net/if_media.h> +#include <net/route.h> +#include <netinet/in.h> +#include <netinet/if_ether.h> +#include <arpa/inet.h> + +#include <stdlib.h> +#include <stdio.h> +#include <errno.h> +#include <event.h> +#include <fcntl.h> +#include <string.h> +#include <unistd.h> +#include <pwd.h> + +#include "snmpd.h" +#include "mib.h" + +extern struct snmpd *env; + +RB_HEAD(oidtree, oid); +RB_PROTOTYPE(oidtree, oid, o_element, smi_oid_cmp); +struct oidtree smi_oidtree; + +u_long +smi_getticks(void) +{ + struct timeval now, run; + u_long ticks; + + gettimeofday(&now, NULL); + if (timercmp(&now, &env->sc_starttime, <=)) + return (0); + timersub(&now, &env->sc_starttime, &run); + ticks = run.tv_sec * 100; + if (run.tv_usec) + ticks += run.tv_usec / 10000; + + return (ticks); +} + +void +smi_oidlen(struct ber_oid *o) +{ + size_t i; + for (i = 0; i < BER_MAX_OID_LEN && o->bo_id[i] != 0; i++); + o->bo_n = i; +} + +char * +smi_oidstring(struct ber_oid *o, char *buf, size_t len) +{ + char str[256]; + struct oid *value, key; + size_t i, lookup = 1; + + bzero(buf, len); + bcopy(o, &key.o_id, sizeof(struct ber_oid)); + key.o_flags |= OID_TABLE; /* do not match wildcards */ + + if (env->sc_flags & SNMPD_F_NONAMES) + lookup = 0; + + for (i = 0; i < o->bo_n; i++) { + key.o_oidlen = i + 1; + if (lookup && + (value = RB_FIND(oidtree, &smi_oidtree, &key)) != NULL) + snprintf(str, sizeof(str), "%s", value->o_name); + else + snprintf(str, sizeof(str), "%d", key.o_oid[i]); + strlcat(buf, str, len); + if (i < (o->bo_n - 1)) + strlcat(buf, ".", len); + } + + return (buf); +} + +void +smi_delete(struct oid *oid) +{ + struct oid key, *value; + + bcopy(&oid->o_id, &key.o_id, sizeof(struct ber_oid)); + if ((value = RB_FIND(oidtree, &smi_oidtree, &key)) != NULL && + value == oid) + RB_REMOVE(oidtree, &smi_oidtree, value); + + if (oid->o_data != NULL) + free(oid->o_data); + if (oid->o_flags & OID_DYNAMIC) { + free(oid->o_name); + free(oid); + } +} + +void +smi_insert(struct oid *oid) +{ + struct oid key, *value; + + if ((oid->o_flags & OID_TABLE) && oid->o_get == NULL) + fatalx("smi_insert: invalid MIB table"); + + bcopy(&oid->o_id, &key.o_id, sizeof(struct ber_oid)); + value = RB_FIND(oidtree, &smi_oidtree, &key); + if (value != NULL) + smi_delete(value); + + RB_INSERT(oidtree, &smi_oidtree, oid); +} + +void +smi_mibtree(struct oid *oids) +{ + struct oid *oid, *decl; + size_t i; + + for (i = 0; oids[i].o_oid[0] != 0; i++) { + oid = &oids[i]; + smi_oidlen(&oid->o_id); + if (oid->o_name != NULL) { + if ((oid->o_flags & OID_TABLE) && oid->o_get == NULL) + fatalx("smi_mibtree: invalid MIB table"); + RB_INSERT(oidtree, &smi_oidtree, oid); + continue; + } + decl = RB_FIND(oidtree, &smi_oidtree, oid); + if (decl == NULL) + fatalx("smi_mibtree: undeclared MIB"); + decl->o_flags = oid->o_flags; + decl->o_get = oid->o_get; + decl->o_set = oid->o_set; + decl->o_val = oid->o_val; + decl->o_data = oid->o_data; + } +} + +int +smi_init(void) +{ + RB_INIT(&smi_oidtree); + mib_init(); + return (0); +} + +struct oid * +smi_find(struct oid *oid) +{ + return (RB_FIND(oidtree, &smi_oidtree, oid)); +} + +struct oid * +smi_next(struct oid *oid) +{ + return (RB_NEXT(oidtree, &smi_oidtree, oid)); +} + +struct oid * +smi_foreach(struct oid *oid, u_int flags) +{ + /* + * Traverse the tree of MIBs with the option to check + * for specific OID flags. + */ + if (oid == NULL) { + oid = RB_MIN(oidtree, &smi_oidtree); + if (oid == NULL) + return (NULL); + if (flags == 0 || (oid->o_flags & flags)) + return (oid); + } + for (;;) { + oid = RB_NEXT(oidtree, &smi_oidtree, oid); + if (oid == NULL) + break; + if (flags == 0 || (oid->o_flags & flags)) + return (oid); + } + + return (oid); +} + +long +smi_oid_cmp(struct oid *a, struct oid *b) +{ + size_t i; + + for (i = 0; i < MIN(a->o_oidlen, b->o_oidlen); i++) + if (a->o_oid[i] != b->o_oid[i]) + return (a->o_oid[i] - b->o_oid[i]); + + /* + * Return success if the matched object is a table + * (it will match any sub-elements) + */ + if ((b->o_flags & OID_TABLE) && + (a->o_flags & OID_TABLE) == 0) + return (0); + + return (a->o_oidlen - b->o_oidlen); +} + +RB_GENERATE(oidtree, oid, o_element, smi_oid_cmp); diff --git a/usr.sbin/snmpd/snmpd.c b/usr.sbin/snmpd/snmpd.c index 292c9d9ae42..d4d41716949 100644 --- a/usr.sbin/snmpd/snmpd.c +++ b/usr.sbin/snmpd/snmpd.c @@ -1,4 +1,4 @@ -/* $OpenBSD: snmpd.c,v 1.1 2007/12/05 09:22:44 reyk Exp $ */ +/* $OpenBSD: snmpd.c,v 1.2 2007/12/28 16:59:31 reyk Exp $ */ /* * Copyright (c) 2007 Reyk Floeter <reyk@vantronix.net> @@ -102,7 +102,7 @@ main(int argc, char *argv[]) int noaction = 0; const char *conffile = CONF_FILE; - mps_init(); + smi_init(); log_init(1); /* log to stderr until daemonized */ diff --git a/usr.sbin/snmpd/snmpd.h b/usr.sbin/snmpd/snmpd.h index fda99a27ef2..78eb9732da5 100644 --- a/usr.sbin/snmpd/snmpd.h +++ b/usr.sbin/snmpd/snmpd.h @@ -1,4 +1,4 @@ -/* $OpenBSD: snmpd.h,v 1.6 2007/12/28 16:27:51 reyk Exp $ */ +/* $OpenBSD: snmpd.h,v 1.7 2007/12/28 16:59:31 reyk Exp $ */ /* * Copyright (c) 2007 Reyk Floeter <reyk@vantronix.net> @@ -380,21 +380,12 @@ struct kif *kr_getnextif(u_short); pid_t snmpe(struct snmpd *, int [2]); /* mps.c */ -int mps_init(void); -u_long mps_getticks(void); -long mps_oid_cmp(struct oid *, struct oid *); -void mps_mibtree(struct oid *); -struct oid *mps_foreach(struct oid *, u_int); -void mps_oidlen(struct ber_oid *); -char *mps_oidstring(struct ber_oid *, char *, size_t); struct ber_element * mps_getreq(struct ber_element *, struct ber_oid *); struct ber_element * mps_getnextreq(struct ber_element *, struct ber_oid *); int mps_setreq(struct ber_element *, struct ber_oid *); int mps_set(struct ber_oid *, void *, long long); -void mps_delete(struct oid *); -void mps_insert(struct oid *); int mps_getstr(struct oid *, struct ber_oid *, struct ber_element **); int mps_setstr(struct oid *, struct ber_oid *, @@ -406,4 +397,17 @@ int mps_setint(struct oid *, struct ber_oid *, int mps_getts(struct oid *, struct ber_oid *, struct ber_element **); +/* smi.c */ +int smi_init(void); +u_long smi_getticks(void); +void smi_mibtree(struct oid *); +struct oid *smi_find(struct oid *); +struct oid *smi_next(struct oid *); +struct oid *smi_foreach(struct oid *, u_int); +void smi_oidlen(struct ber_oid *); +char *smi_oidstring(struct ber_oid *, char *, size_t); +void smi_delete(struct oid *); +void smi_insert(struct oid *); +long smi_oid_cmp(struct oid *, struct oid *); + #endif /* _SNMPD_H */ diff --git a/usr.sbin/snmpd/snmpe.c b/usr.sbin/snmpd/snmpe.c index 92bed095d7b..609ca187f53 100644 --- a/usr.sbin/snmpd/snmpe.c +++ b/usr.sbin/snmpd/snmpe.c @@ -1,4 +1,4 @@ -/* $OpenBSD: snmpe.c,v 1.4 2007/12/27 20:39:32 maja Exp $ */ +/* $OpenBSD: snmpe.c,v 1.5 2007/12/28 16:59:31 reyk Exp $ */ /* * Copyright (c) 2007 Reyk Floeter <reyk@vantronix.net> @@ -117,9 +117,9 @@ snmpe(struct snmpd *x_env, int pipe_parent2snmpe[2]) #endif #ifdef DEBUG - for (oid = NULL; (oid = mps_foreach(oid, 0)) != NULL;) { + for (oid = NULL; (oid = smi_foreach(oid, 0)) != NULL;) { char buf[BUFSIZ]; - mps_oidstring(&oid->o_id, buf, sizeof(buf)); + smi_oidstring(&oid->o_id, buf, sizeof(buf)); log_debug("oid %s", buf); } #endif @@ -437,7 +437,7 @@ snmpe_debug_elements(struct ber_element *root) break; } fprintf(stderr, "oid %s", - mps_oidstring(&o, str, sizeof(str))); + smi_oidstring(&o, str, sizeof(str))); fprintf(stderr, "\n"); break; case BER_TYPE_OCTETSTRING: @@ -631,7 +631,7 @@ snmpe_parse(struct sockaddr_storage *ss, o.bo_n > BER_MAX_OID_LEN) goto varfail; log_debug("snmpe_parse: %s: oid %s", host, - mps_oidstring(&o, buf, sizeof(buf))); + smi_oidstring(&o, buf, sizeof(buf))); break; case 1: c = d = NULL; |