From faa62625739a98f0c20e63bb6e7b2b1fbc43bfaa Mon Sep 17 00:00:00 2001 From: Vojtech Vilimek Date: Thu, 19 Oct 2023 16:20:37 +0200 Subject: [PATCH] SNMP: Code improvements --- proto/snmp/snmp.c | 13 ++------ proto/snmp/subagent.c | 69 +++++++------------------------------------ proto/snmp/subagent.h | 2 +- 3 files changed, 14 insertions(+), 70 deletions(-) diff --git a/proto/snmp/snmp.c b/proto/snmp/snmp.c index bde36779..6c5ed239 100644 --- a/proto/snmp/snmp.c +++ b/proto/snmp/snmp.c @@ -252,13 +252,9 @@ snmp_start_locked(struct object_lock *lock) } snmp_log("opening socket"); + /* Try opening the socket, schedule a retry on fail */ if (sk_open(s) < 0) - { - // TODO rather set the startup timer, then reset whole SNMP proto - log(L_ERR "Cannot open listening socket"); - snmp_down(p); - // TODO go back to SNMP_INIT and try reconnecting after timeout - } + tm_set(p->startup_timer, current_time() + p->timeout S); } /* this function is internal and shouldn't be used outside the snmp module */ @@ -307,10 +303,7 @@ static void snmp_stop_timeout(timer *t) { snmp_log("stop timer triggered"); - - struct snmp_proto *p = t->data; - - snmp_down(p); + snmp_down(t->data); } static void diff --git a/proto/snmp/subagent.c b/proto/snmp/subagent.c index 47ec574d..1ace2c43 100644 --- a/proto/snmp/subagent.c +++ b/proto/snmp/subagent.c @@ -38,7 +38,7 @@ static struct oid *search_mib(struct snmp_proto *p, const struct oid *o_start, c u32 snmp_internet[] = { SNMP_ISO, SNMP_ORG, SNMP_DOD, SNMP_INTERNET }; -static const char * const snmp_errs[] = { +static const char * const snmp_errs[] UNUSED = { #define SNMP_ERR_SHIFT 256 [AGENTX_RES_OPEN_FAILED - SNMP_ERR_SHIFT] = "Open failed", [AGENTX_RES_NOT_OPEN - SNMP_ERR_SHIFT] = "Not open", @@ -55,7 +55,7 @@ static const char * const snmp_errs[] = { [AGENTX_RES_PROCESSING_ERR - SNMP_ERR_SHIFT] = "Processing error", }; -static const char * const snmp_pkt_type[] = { +static const char * const snmp_pkt_type[] UNUSED = { [AGENTX_OPEN_PDU] = "Open-PDU", [AGENTX_CLOSE_PDU] = "Close-PDU", [AGENTX_REGISTER_PDU] = "Register-PDU", @@ -108,13 +108,7 @@ open_pdu(struct snmp_proto *p, struct oid *oid) c.buffer = snmp_put_str(c.buffer, cf->description); uint s = update_packet_size(p, buf, c.buffer); - int ret = sk_send(sk, s); - if (ret > 0) - snmp_log("sk_send OK!"); - else if (ret == 0) - snmp_log("sk_send sleep"); - else - snmp_log("sk_send error"); + sk_send(sk, s); #undef TIMEOUT_SIZE } @@ -184,13 +178,7 @@ snmp_notify_pdu(struct snmp_proto *p, struct oid *oid, void *data, uint size, in ADVANCE(c.buffer, c.size, size); uint s = update_packet_size(p, sk->tbuf, c.buffer); - int ret = sk_send(sk, s); - if (ret > 0) - snmp_log("sk_send OK!"); - else if (ret == 0) - snmp_log("sk_send sleep"); - else - snmp_log("sk_send error"); + sk_send(sk, s); #undef TRAP0_HEADER_SIZE #undef UPTIME_SIZE @@ -289,13 +277,7 @@ un_register_pdu(struct snmp_proto *p, struct oid *oid, uint len, uint index, u8 uint s = update_packet_size(p, buf, c.buffer); //snmp_log("sending (un)register %s", snmp_pkt_type[type]); - int ret = sk_send(sk, s); - if (ret > 0) - snmp_log("sk_send OK!"); - else if (ret == 0) - snmp_log("sk_send sleep"); - else - snmp_log("sk_send error"); + sk_send(sk, s); } /* Register-PDU */ @@ -343,13 +325,7 @@ close_pdu(struct snmp_proto *p, u8 reason) uint s = update_packet_size(p, buf, c.buffer); //snmp_log("preparing to sk_send() (close)"); - int ret = sk_send(sk, s); - if (ret > 0) - snmp_log("sk_send OK!"); - else if (ret == 0) - snmp_log("sk_send sleep"); - else - snmp_log("sk_send error"); + sk_send(sk, s); } #if 0 @@ -394,13 +370,7 @@ addagentcaps_pdu(struct snmp_proto *p, struct oid *cap, char *descr, // make a note in the snmp_proto structure //int ret = sk_send(sk, buf - sk->tbuf); - int ret = sk_send(sk, buf - sk->tpos); - if (ret == 0) - snmp_log("sk_send sleep"); - else if (ret < 0) - snmp_log("sk_send err"); - else - log(L_INFO, "sk_send ok !!"); + sk_send(sk, buf - sk->tpos); } static void UNUSED @@ -438,14 +408,7 @@ removeagentcaps_pdu(struct snmp_proto *p, struct oid *cap, struct agentx_context // update state in snmp_proto structure - //int ret = sk_send(sk, buf - sk->tbuf); - int ret = sk_send(sk, buf - sk->tpos); - if (ret == 0) - snmp_log("sk_send sleep"); - else if (ret < 0) - snmp_log("sk_send err"); - else - log(L_INFO, "sk_send ok !!"); + sk_send(sk, buf - sk->tpos); } #endif @@ -1024,13 +987,7 @@ send:; //snmp_log("sending response to Get-PDU, GetNext-PDU or GetBulk-PDU request (size %u)...", s); /* We send the message in TX-buffer. */ - int ret = sk_send(sk, s); - if (ret > 0) - snmp_log("sk_send OK!"); - else if (ret == 0) - snmp_log("sk_send sleep"); - else - snmp_log("sk_send error"); + sk_send(sk, s); // TODO think through the error state mb_free(o_start); @@ -1173,13 +1130,7 @@ snmp_ping(struct snmp_proto *p) /* sending only header -> pkt - buf */ uint s = update_packet_size(p, sk->tpos, c.buffer); - int ret = sk_send(sk, s); - if (ret > 0) - snmp_log("sk_send OK!"); - else if (ret == 0) - snmp_log("sk_send sleep"); - else - snmp_log("sk_send error"); + sk_send(sk, s); } static inline int diff --git a/proto/snmp/subagent.h b/proto/snmp/subagent.h index dac6f7f9..937b2de9 100644 --- a/proto/snmp/subagent.h +++ b/proto/snmp/subagent.h @@ -122,7 +122,7 @@ enum snmp_search_res { buf += 4 + snmp_str_size_from_len(length); }) #define SNMP_LOAD_CONTEXT(hdr, buf, cont, cont_len) ({ \ - if ((hdr)->flags & AGENTX_NON_DEFAULT_CONTEXT) \ + if (cont = NULL, cont_len = 0, (hdr)->flags & AGENTX_NON_DEFAULT_CONTEXT) \ LOAD_STR((buf), (cont), (cont_len), \ (hdr)->flags & AGENTX_NETWORK_BYTE_ORDER); })