Let appl_report() collect its own metrics. This simplifies the interface
authormartijn <martijn@openbsd.org>
Wed, 8 Nov 2023 20:02:52 +0000 (20:02 +0000)
committermartijn <martijn@openbsd.org>
Wed, 8 Nov 2023 20:02:52 +0000 (20:02 +0000)
and gives us a free report-pdu log line in debug mode.

OK tb@

usr.sbin/snmpd/application.c
usr.sbin/snmpd/smi.h

index 5435acd..4004e62 100644 (file)
@@ -1,4 +1,4 @@
-/*     $OpenBSD: application.c,v 1.33 2023/11/08 19:59:46 martijn Exp $        */
+/*     $OpenBSD: application.c,v 1.34 2023/11/08 20:02:52 martijn Exp $        */
 
 /*
  * Copyright (c) 2021 Martijn van Duren <martijn@openbsd.org>
@@ -77,6 +77,7 @@ struct appl_request_upstream {
        struct appl_context *aru_ctx;
        struct snmp_message *aru_statereference;
        enum snmp_pdutype aru_requesttype;
+       enum snmp_pdutype aru_responsetype;
        int32_t aru_requestid; /* upstream requestid */
        int32_t aru_transactionid; /* RFC 2741 section 6.1 */
        uint16_t aru_nonrepeaters;
@@ -151,8 +152,7 @@ int appl_varbind_valid(struct appl_varbind *, struct appl_varbind_internal *,
 int appl_error_valid(enum appl_error, enum snmp_pdutype);
 int appl_varbind_backend(struct appl_varbind_internal *);
 void appl_varbind_error(struct appl_varbind_internal *, enum appl_error);
-void appl_report(struct snmp_message *, int32_t, struct ber_oid *,
-    struct ber_element *);
+void appl_report(struct snmp_message *, int32_t, struct ber_oid *);
 void appl_pdu_log(struct appl_backend *, enum snmp_pdutype, int32_t, uint16_t,
     uint16_t, struct appl_varbind *);
 void ober_oid_nextsibling(struct ber_oid *);
@@ -854,8 +854,7 @@ appl_processpdu(struct snmp_message *statereference, const char *ctxname,
 {
        struct appl_context *ctx;
        struct appl_request_upstream *ureq;
-       struct ber_oid oid;
-       struct ber_element *value, *varbind, *varbindlist;
+       struct ber_element *varbind, *varbindlist;
        long long nonrepeaters, maxrepetitions;
        static uint32_t transactionid;
        int32_t requestid;
@@ -867,12 +866,9 @@ appl_processpdu(struct snmp_message *statereference, const char *ctxname,
 
        /* RFC 3413, section 3.2, processPDU, item 5, final bullet */
        if ((ctx = appl_context(ctxname, 0)) == NULL) {
-               oid = BER_OID(MIB_snmpUnknownContexts, 0);
                snmp_target_mib.snmp_unknowncontexts++;
-               if ((value = ober_add_integer(NULL,
-                   snmp_target_mib.snmp_unknowncontexts)) == NULL)
-                       fatal("ober_add_integer");
-               appl_report(statereference, requestid, &oid, value);
+               appl_report(statereference, requestid,
+                   &OID(MIB_snmpUnknownContexts, 0));
                return;
        }
 
@@ -883,6 +879,7 @@ appl_processpdu(struct snmp_message *statereference, const char *ctxname,
        ureq->aru_statereference = statereference;
        ureq->aru_transactionid = transactionid++;
        ureq->aru_requesttype = pdu->be_type;
+       ureq->aru_responsetype = SNMP_C_RESPONSE;
        ureq->aru_requestid = requestid;
        ureq->aru_error = APPL_ERROR_NOERROR;
        ureq->aru_index = 0;
@@ -1257,7 +1254,7 @@ appl_request_upstream_reply(struct appl_request_upstream *ureq)
        }
 
        ureq->aru_vblist[i - 1].avi_varbind.av_next = NULL;
-       appl_pdu_log(NULL, SNMP_C_RESPONSE, ureq->aru_requestid,
+       appl_pdu_log(NULL, ureq->aru_responsetype, ureq->aru_requestid,
            ureq->aru_error, ureq->aru_index,
            &(ureq->aru_vblist[0].avi_varbind));
 
@@ -1272,7 +1269,7 @@ appl_request_upstream_reply(struct appl_request_upstream *ureq)
                        varbindlist = varbind;
        }
 
-       snmpe_send(ureq->aru_statereference, SNMP_C_RESPONSE,
+       snmpe_send(ureq->aru_statereference, ureq->aru_responsetype,
            ureq->aru_requestid, ureq->aru_error, ureq->aru_index, varbindlist);
        ureq->aru_statereference = NULL;
        appl_request_upstream_free(ureq);
@@ -1690,20 +1687,41 @@ appl_varbind_error(struct appl_varbind_internal *avi, enum appl_error error)
 }
 
 void
-appl_report(struct snmp_message *msg, int32_t requestid, struct ber_oid *oid,
-    struct ber_element *value)
+appl_report(struct snmp_message *statereference, int32_t requestid,
+    struct ber_oid *oid)
 {
-       struct ber_element *varbind;
-
-       varbind = ober_printf_elements(NULL, "{Oe}", oid, value);
-       if (varbind == NULL) {
-               log_warn("%"PRId32": ober_printf_elements", requestid);
-               ober_free_elements(value);
-               snmp_msgfree(msg);
-               return;
-       }
+       struct appl_request_upstream *ureq;
 
-       snmpe_send(msg, SNMP_C_REPORT, requestid, 0, 0, varbind);
+       if ((ureq = calloc(1, sizeof(*ureq))) == NULL)
+               fatal("malloc");
+       ureq->aru_ctx = appl_context(NULL, 0);
+       ureq->aru_statereference = statereference;
+       ureq->aru_requesttype = SNMP_C_GETREQ;
+       ureq->aru_responsetype = SNMP_C_REPORT;
+       ureq->aru_requestid = requestid;
+       ureq->aru_transactionid = 0;
+       ureq->aru_nonrepeaters = 0;
+       ureq->aru_maxrepetitions = 0;
+       if ((ureq->aru_vblist = calloc(1, sizeof(*ureq->aru_vblist))) == NULL)
+               fatal("malloc");
+       ureq->aru_varbindlen = 1;
+       ureq->aru_error = APPL_ERROR_NOERROR;
+       ureq->aru_index = 0;
+       ureq->aru_locked = 0;
+       ureq->aru_pduversion = SNMP_V3;
+
+       ureq->aru_vblist[0].avi_state = APPL_VBSTATE_NEW;
+       ureq->aru_vblist[0].avi_varbind.av_oid = *oid;
+       ureq->aru_vblist[0].avi_varbind.av_value = NULL;
+       ureq->aru_vblist[0].avi_varbind.av_next = NULL;
+       ureq->aru_vblist[0].avi_origid = *oid;
+       ureq->aru_vblist[0].avi_index = 1;
+       ureq->aru_vblist[0].avi_request_upstream = ureq;
+       ureq->aru_vblist[0].avi_request_downstream = NULL;
+       ureq->aru_vblist[0].avi_next = NULL;
+       ureq->aru_vblist[0].avi_sub = NULL;
+       
+       appl_request_upstream_resolve(ureq);
 }
 
 struct ber_element *
index 69bbca8..980dd7d 100644 (file)
@@ -1,4 +1,4 @@
-/*     $OpenBSD: smi.h,v 1.2 2022/06/30 09:42:19 martijn Exp $ */
+/*     $OpenBSD: smi.h,v 1.3 2023/11/08 20:02:52 martijn Exp $ */
 
 /*
  * Copyright (c) 2021 Martijn van Duren <martijn@openbsd.org>
@@ -19,9 +19,6 @@
 #include <ber.h>
 #include <stdint.h>
 
-#define BER_OID(...) (struct ber_oid) { { __VA_ARGS__ }, \
-    (sizeof((u_int32_t []) { __VA_ARGS__ }) / sizeof(u_int32_t)) }
-
 char   *smi_oid2string(struct ber_oid *, char *, size_t, size_t);
 u_long  smi_getticks(void);
 char   *smi_print_element(struct ber_element *);