From 7412a0e74ea16a714704b1ba08c057e1f189f218 Mon Sep 17 00:00:00 2001 From: Dirk Ziegelmeier Date: Sat, 2 Jan 2016 21:29:01 +0100 Subject: [PATCH] SNMP: Move common MIB code into snmp_core.c helper functions --- src/apps/snmp/snmp_core.c | 52 +++++++++++++++ src/apps/snmp/snmp_mib2.c | 106 +++++++----------------------- src/include/lwip/apps/snmp_core.h | 5 +- 3 files changed, 78 insertions(+), 85 deletions(-) diff --git a/src/apps/snmp/snmp_core.c b/src/apps/snmp/snmp_core.c index 2eecd3e1..935b6b89 100644 --- a/src/apps/snmp/snmp_core.c +++ b/src/apps/snmp/snmp_core.c @@ -200,6 +200,25 @@ snmp_ip6_to_oid(const ip6_addr_t *ip, u32_t *oid) #endif /* LWIP_IPV6 */ #if LWIP_IPV4 || LWIP_IPV6 +/** + * Convert to InetAddressType+InetAddress+InetPortNumber + * @param ip + * @param port + * @param oid + * @return OID length + */ +u8_t +snmp_ip_port_to_oid(const ip_addr_t *ip, u16_t port, u32_t *oid) +{ + u8_t index; + + index = snmp_ip_to_oid(ip, oid); + oid[index] = port; + index++; + + return index; +} + /** * Convert to InetAddressType+InetAddress * @param ip @@ -279,6 +298,39 @@ snmp_oid_to_ip(const u32_t *oid, u8_t oid_len, ip_addr_t *ip) return 0; } } + +/** + * Convert from InetAddressType+InetAddress+InetPortNumber to ip_addr_t and u16_t + * @param oid + * @param oid_len + * @param ip + * @param port + * @return Parsed OID length + */ +u8_t +snmp_oid_to_ip_port(const u32_t *oid, u8_t oid_len, ip_addr_t *ip, u16_t *port) +{ + u8_t index = 0; + + /* InetAddressType + InetAddress */ + index += snmp_oid_to_ip(&oid[index], oid_len-index, ip); + if(index == 0) { + return 0; + } + + /* InetPortNumber */ + if(oid_len < (index+1)) { + return 0; + } + if(oid[index] > 0xffff) { + return 0; + } + *port = (u16_t)oid[index]; + index++; + + return index; +} + #endif /* LWIP_IPV4 || LWIP_IPV6 */ void diff --git a/src/apps/snmp/snmp_mib2.c b/src/apps/snmp/snmp_mib2.c index cf27e9bf..c9b8f939 100644 --- a/src/apps/snmp/snmp_mib2.c +++ b/src/apps/snmp/snmp_mib2.c @@ -2079,40 +2079,22 @@ tcp_ConnectionTable_get_cell_value(const u32_t* column, const u32_t* row_oid, u8 LWIP_UNUSED_ARG(value_len); - /* tcpConnectionLocalAddressType + tcpConnectionLocalAddress */ - index += snmp_oid_to_ip(&row_oid[index], row_oid_len-index, &local_ip); + /* tcpConnectionLocalAddressType + tcpConnectionLocalAddress + tcpConnectionLocalPort */ + index += snmp_oid_to_ip_port(&row_oid[index], row_oid_len-index, &local_ip, &local_port); if(index == 0) { return SNMP_ERR_NOSUCHINSTANCE; } - - /* tcpConnectionLocalPort */ - if(row_oid_len < (index+1)) { - return SNMP_ERR_NOSUCHINSTANCE; - } - if(row_oid[index] > 0xffff) { - return SNMP_ERR_NOSUCHINSTANCE; - } - local_port = (u16_t)row_oid[index]; - index++; - /* tcpConnectionRemAddressType + tcpConnectionRemAddress */ - index += snmp_oid_to_ip(&row_oid[index], row_oid_len-index, &remote_ip); + /* tcpConnectionRemAddressType + tcpConnectionRemAddress + tcpConnectionRemPort */ + index += snmp_oid_to_ip_port(&row_oid[index], row_oid_len-index, &remote_ip, &remote_port); if(index == 0) { return SNMP_ERR_NOSUCHINSTANCE; } - /* tcpConnectionRemPort */ - if(row_oid_len < (index+1)) { - return SNMP_ERR_NOSUCHINSTANCE; - } - if(row_oid[index] > 0xffff) { - return SNMP_ERR_NOSUCHINSTANCE; - } - remote_port = (u16_t)row_oid[index]; - /* find tcp_pcb with requested ip and port*/ for(i=0; ilocal_ip) && (local_port == pcb->local_port) && @@ -2148,21 +2130,16 @@ tcp_ConnectionTable_get_next_cell_instance_and_value(const u32_t* column, struct /* iterate over all possible OIDs to find the next one */ for(i=0; ilocal_ip, &test_oid[index]); + /* tcpConnectionLocalAddressType + tcpConnectionLocalAddress + tcpConnectionLocalPort */ + index += snmp_ip_port_to_oid(&pcb->local_ip, pcb->local_port, &test_oid[index]); - test_oid[index] = pcb->local_port; /* tcpConnectionLocalPort */ - index++; - - /* tcpConnectionRemAddressType + tcpConnectionRemAddress */ - index += snmp_ip_to_oid(&pcb->remote_ip, &test_oid[index]); - - test_oid[index] = pcb->remote_port; /* tcpConnectionRemPort */ - index++; + /* tcpConnectionRemAddressType + tcpConnectionRemAddress + tcpConnectionRemPort */ + index += snmp_ip_port_to_oid(&pcb->remote_ip, pcb->remote_port, &test_oid[index]); /* check generated OID: is it a candidate for the next one? */ snmp_next_oid_check(&state, test_oid, index, pcb); @@ -2209,21 +2186,11 @@ tcp_ListenerTable_get_cell_value(const u32_t* column, const u32_t* row_oid, u8_t LWIP_UNUSED_ARG(value_len); - /* tcpListenerLocalAddressType + tcpListenerLocalAddress */ - index += snmp_oid_to_ip(&row_oid[index], row_oid_len-index, &local_ip); + /* tcpListenerLocalAddressType + tcpListenerLocalAddress + tcpListenerLocalPort */ + index += snmp_oid_to_ip_port(&row_oid[index], row_oid_len-index, &local_ip, &local_port); if(index == 0) { return SNMP_ERR_NOSUCHINSTANCE; } - - /* tcpListenerLocalPort */ - if(row_oid_len < (index+1)) { - return SNMP_ERR_NOSUCHINSTANCE; - } - if(row_oid[index] > 0xffff) { - return SNMP_ERR_NOSUCHINSTANCE; - } - local_port = (u16_t)row_oid[index]; - index++; /* find tcp_pcb with requested ip and port*/ pcb = tcp_listen_pcbs.listen_pcbs; @@ -2259,11 +2226,8 @@ tcp_ListenerTable_get_next_cell_instance_and_value(const u32_t* column, struct s u8_t index = 0; u32_t test_oid[LWIP_ARRAYSIZE(result_temp)]; - /* tcpListenerLocalAddressType + tcpListenerLocalAddress */ - index += snmp_ip_to_oid(&pcb->local_ip, &test_oid[index]); - - test_oid[index] = pcb->local_port; /* tcpListenerLocalPort */ - index++; + /* tcpListenerLocalAddressType + tcpListenerLocalAddress + tcpListenerLocalPort */ + index += snmp_ip_port_to_oid(&pcb->local_ip, pcb->local_port, &test_oid[index]); /* check generated OID: is it a candidate for the next one? */ snmp_next_oid_check(&state, test_oid, index, NULL); @@ -2347,38 +2311,18 @@ udp_endpointTable_get_cell_value(const u32_t* column, const u32_t* row_oid, u8_t LWIP_UNUSED_ARG(value_len); - /* udpEndpointLocalAddressType + udpEndpointLocalAddress */ - index += snmp_oid_to_ip(&row_oid[index], row_oid_len-index, &local_ip); + /* udpEndpointLocalAddressType + udpEndpointLocalAddress + udpEndpointLocalPort */ + index += snmp_oid_to_ip_port(&row_oid[index], row_oid_len-index, &local_ip, &local_port); if(index == 0) { return SNMP_ERR_NOSUCHINSTANCE; } - /* udpEndpointLocalPort */ - if(row_oid_len < (index+1)) { - return SNMP_ERR_NOSUCHINSTANCE; - } - if(row_oid[index] > 0xffff) { - return SNMP_ERR_NOSUCHINSTANCE; - } - local_port = (u16_t)row_oid[index]; - index++; - - /* udpEndpointRemoteAddressType + udpEndpointRemoteAddress */ - index += snmp_oid_to_ip(&row_oid[index], row_oid_len-index, &remote_ip); + /* udpEndpointRemoteAddressType + udpEndpointRemoteAddress + udpEndpointRemotePort */ + index += snmp_oid_to_ip_port(&row_oid[index], row_oid_len-index, &remote_ip, &remote_port); if(index == 0) { return SNMP_ERR_NOSUCHINSTANCE; } - /* udpEndpointRemotePort */ - if(row_oid_len < (index+1)) { - return SNMP_ERR_NOSUCHINSTANCE; - } - if(row_oid[index] > 0xffff) { - return SNMP_ERR_NOSUCHINSTANCE; - } - remote_port = (u16_t)row_oid[index]; - index++; - /* udpEndpointInstance */ if(row_oid_len < (index+1)) { return SNMP_ERR_NOSUCHINSTANCE; @@ -2426,17 +2370,11 @@ udp_endpointTable_get_next_cell_instance_and_value(const u32_t* column, struct s u32_t test_oid[LWIP_ARRAYSIZE(result_temp)]; u8_t index = 0; - /* udpEndpointLocalAddressType + udpEndpointLocalAddress */ - index += snmp_ip_to_oid(&pcb->local_ip, &test_oid[index]); + /* udpEndpointLocalAddressType + udpEndpointLocalAddress + udpEndpointLocalPort */ + index += snmp_ip_port_to_oid(&pcb->local_ip, pcb->local_port, &test_oid[index]); - test_oid[index] = pcb->local_port; /* udpEndpointLocalPort */ - index++; - - /* udpEndpointRemoteAddressType + udpEndpointRemoteAddress */ - index += snmp_ip_to_oid(&pcb->remote_ip, &test_oid[index]); - - test_oid[index] = pcb->remote_port; /* udpEndpointRemotePort */ - index++; + /* udpEndpointRemoteAddressType + udpEndpointRemoteAddress + udpEndpointRemotePort */ + index += snmp_ip_port_to_oid(&pcb->remote_ip, pcb->remote_port, &test_oid[index]); test_oid[index] = 0; /* udpEndpointInstance */ index++; diff --git a/src/include/lwip/apps/snmp_core.h b/src/include/lwip/apps/snmp_core.h index bfbc440a..c1ed2657 100644 --- a/src/include/lwip/apps/snmp_core.h +++ b/src/include/lwip/apps/snmp_core.h @@ -306,8 +306,11 @@ u8_t snmp_oid_to_ip6(const u32_t *oid, ip6_addr_t *ip); void snmp_ip6_to_oid(const ip6_addr_t *ip, u32_t *oid); #endif /* LWIP_IPV6 */ #if LWIP_IPV4 || LWIP_IPV6 -u8_t snmp_oid_to_ip(const u32_t *oid, u8_t oid_len, ip_addr_t *ip); u8_t snmp_ip_to_oid(const ip_addr_t *ip, u32_t *oid); +u8_t snmp_ip_port_to_oid(const ip_addr_t *ip, u16_t port, u32_t *oid); + +u8_t snmp_oid_to_ip(const u32_t *oid, u8_t oid_len, ip_addr_t *ip); +u8_t snmp_oid_to_ip_port(const u32_t *oid, u8_t oid_len, ip_addr_t *ip, u16_t *port); #endif /* LWIP_IPV4 || LWIP_IPV6 */ u8_t snmp_set_test_ok(struct snmp_node_instance* instance, u16_t value_len, void* value); /* generic function which can be used if test is always successful */