-/* $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 <martijn@openbsd.org>
#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 {
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,
-/* $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 <martijn@openbsd.org>
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 *,
-/* $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 <martijn@openbsd.org>
#include <event.h>
#include <stdlib.h>
+#include <unistd.h>
#include "application.h"
#include "log.h"
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);