From: martijn Date: Mon, 6 Nov 2023 11:02:57 +0000 (+0000) Subject: Readd the sysORTable based on the new struct appl_agentcap. X-Git-Url: http://artulab.com/gitweb/?a=commitdiff_plain;h=215fd4bc995b53d2f2185d5cbebdc244166b6359;p=openbsd Readd the sysORTable based on the new struct appl_agentcap. OK tb@ --- diff --git a/usr.sbin/snmpd/application.c b/usr.sbin/snmpd/application.c index e91c630a8c4..1f96e9f678f 100644 --- a/usr.sbin/snmpd/application.c +++ b/usr.sbin/snmpd/application.c @@ -1,4 +1,4 @@ -/* $OpenBSD: application.c,v 1.29 2023/11/06 11:00:46 martijn Exp $ */ +/* $OpenBSD: application.c,v 1.30 2023/11/06 11:02:57 martijn Exp $ */ /* * Copyright (c) 2021 Martijn van Duren @@ -34,6 +34,9 @@ #include "snmpe.h" #include "mib.h" +#define OID(...) (struct ber_oid){ { __VA_ARGS__ }, \ + (sizeof((uint32_t []) { __VA_ARGS__ }) / sizeof(uint32_t)) } + TAILQ_HEAD(, appl_context) contexts = TAILQ_HEAD_INITIALIZER(contexts); struct appl_agentcap { @@ -331,6 +334,103 @@ appl_agentcap_free(struct appl_agentcap *cap) free(cap); } +struct ber_element * +appl_sysorlastchange(struct ber_oid *oid) +{ + struct appl_context *ctx; + struct ber_element *value; + + ctx = appl_context(NULL, 0); + value = ober_add_integer(NULL, ctx->ac_agentcap_lastchange); + if (value != NULL) + ober_set_header(value, BER_CLASS_APPLICATION, SNMP_T_TIMETICKS); + else + log_warn("ober_add_integer"); + + return value; +} + +#define SYSORIDX_POS 10 +struct ber_element * +appl_sysortable(struct ber_oid *oid) +{ + struct appl_context *ctx; + struct appl_agentcap *cap; + struct ber_element *value = NULL; + + if (oid->bo_n != SYSORIDX_POS + 1) + goto notfound; + + ctx = appl_context(NULL, 0); + TAILQ_FOREACH(cap, &(ctx->ac_agentcaps), aa_entry) { + if (cap->aa_index == oid->bo_id[SYSORIDX_POS]) + break; + } + if (cap == NULL) + goto notfound; + + if (ober_oid_cmp(&OID(MIB_sysORID), oid) == -2) + value = ober_add_oid(NULL, &(cap->aa_oid)); + else if (ober_oid_cmp(&OID(MIB_sysORDescr), oid) == -2) + value = ober_add_string(NULL, cap->aa_descr); + else if (ober_oid_cmp(&OID(MIB_sysORUpTime), oid) == -2) { + if ((value = ober_add_integer(NULL, cap->aa_uptime)) != NULL) + ober_set_header(value, + BER_CLASS_APPLICATION, SNMP_T_TIMETICKS); + } + if (value == NULL) + log_warn("ober_add_*"); + return value; + + notfound: + if ((value = appl_exception(APPL_EXC_NOSUCHINSTANCE)) == NULL) + log_warn("appl_exception"); + return value; +} + +struct ber_element * +appl_sysortable_getnext(int8_t include, struct ber_oid *oid) +{ + struct appl_context *ctx; + struct appl_agentcap *cap; + struct ber_element *value = NULL; + + if (oid->bo_n < SYSORIDX_POS + 1) { + include = 1; + oid->bo_id[SYSORIDX_POS] = 0; + } else if (oid->bo_n < SYSORIDX_POS + 1) + include = 0; + + ctx = appl_context(NULL, 0); + TAILQ_FOREACH(cap, &(ctx->ac_agentcaps), aa_entry) { + if (cap->aa_index > oid->bo_id[SYSORIDX_POS]) + break; + if (cap->aa_index == oid->bo_id[SYSORIDX_POS] && include) + break; + } + if (cap == NULL) { + value = appl_exception(APPL_EXC_NOSUCHINSTANCE); + goto done; + } + + oid->bo_id[SYSORIDX_POS] = cap->aa_index; + oid->bo_n = SYSORIDX_POS + 1; + + if (ober_oid_cmp(&OID(MIB_sysORID), oid) == -2) + value = ober_add_oid(NULL, &(cap->aa_oid)); + else if (ober_oid_cmp(&OID(MIB_sysORDescr), oid) == -2) + value = ober_add_string(NULL, cap->aa_descr); + else if (ober_oid_cmp(&OID(MIB_sysORUpTime), oid) == -2) { + if ((value = ober_add_integer(NULL, cap->aa_uptime)) != NULL) + ober_set_header(value, + BER_CLASS_APPLICATION, SNMP_T_TIMETICKS); + } + done: + if (value == NULL) + log_warn("ober_add_*"); + return value; +} + enum appl_error appl_region(struct appl_context *ctx, uint32_t timeout, uint8_t priority, struct ber_oid *oid, int instance, int subtree, diff --git a/usr.sbin/snmpd/application.h b/usr.sbin/snmpd/application.h index e873599cf9c..ecbc7dac1e0 100644 --- a/usr.sbin/snmpd/application.h +++ b/usr.sbin/snmpd/application.h @@ -1,4 +1,4 @@ -/* $OpenBSD: application.h,v 1.8 2023/11/06 11:00:46 martijn Exp $ */ +/* $OpenBSD: application.h,v 1.9 2023/11/06 11:02:57 martijn Exp $ */ /* * Copyright (c) 2021 Martijn van Duren @@ -127,6 +127,9 @@ enum appl_error appl_addagentcaps(const char *, struct ber_oid *, const char *, struct appl_backend *); enum appl_error appl_removeagentcaps(const char *, struct ber_oid *, struct appl_backend *); +struct ber_element *appl_sysorlastchange(struct ber_oid *); +struct ber_element *appl_sysortable(struct ber_oid *); +struct ber_element *appl_sysortable_getnext(int8_t, struct ber_oid *); enum appl_error appl_register(const char *, uint32_t, uint8_t, struct ber_oid *, int, int, uint8_t, uint32_t, struct appl_backend *); enum appl_error appl_unregister(const char *, uint8_t, struct ber_oid *, diff --git a/usr.sbin/snmpd/application_internal.c b/usr.sbin/snmpd/application_internal.c index 8a0562c026e..4221dcaabde 100644 --- a/usr.sbin/snmpd/application_internal.c +++ b/usr.sbin/snmpd/application_internal.c @@ -1,4 +1,4 @@ -/* $OpenBSD: application_internal.c,v 1.5 2023/11/06 10:58:13 martijn Exp $ */ +/* $OpenBSD: application_internal.c,v 1.6 2023/11/06 11:02:57 martijn Exp $ */ /* * Copyright (c) 2023 Martijn van Duren @@ -20,6 +20,7 @@ #include #include +#include #include "application.h" #include "log.h" @@ -82,6 +83,15 @@ appl_internal_init(void) appl_internal_object(&OID(MIB_sysName), appl_internal_system, NULL); appl_internal_object(&OID(MIB_sysLocation), appl_internal_system, NULL); appl_internal_object(&OID(MIB_sysServices), appl_internal_system, NULL); + appl_internal_object(&OID(MIB_sysORLastChange), appl_sysorlastchange, + NULL); + + appl_internal_object(&OID(MIB_sysORID), appl_sysortable, + appl_sysortable_getnext); + appl_internal_object(&OID(MIB_sysORDescr), appl_sysortable, + appl_sysortable_getnext); + appl_internal_object(&OID(MIB_sysORUpTime), appl_sysortable, + appl_sysortable_getnext); appl_internal_region(&OID(MIB_snmp)); appl_internal_object(&OID(MIB_snmpInPkts), appl_internal_snmp, NULL);