SNMP: Cleanup snmp IP helper functions, add IPv6 support

This commit is contained in:
Dirk Ziegelmeier 2015-12-29 22:06:23 +01:00
parent 4341df4094
commit 611966de63
3 changed files with 105 additions and 19 deletions

View File

@ -107,7 +107,7 @@ const struct snmp_obj_id* snmp_get_device_enterprise_oid(void)
* @param ip points to output struct
*/
u8_t
snmp_oid_to_ip(const u32_t *oid, ip4_addr_t *ip)
snmp_oid_to_ip4(const u32_t *oid, ip4_addr_t *ip)
{
if((oid[0] > 0xFF) ||
(oid[1] > 0xFF) ||
@ -127,7 +127,7 @@ snmp_oid_to_ip(const u32_t *oid, ip4_addr_t *ip)
* @param ident points to u32_t ident[4] output
*/
void
snmp_ip_to_oid(const ip4_addr_t *ip, u32_t *oid)
snmp_ip4_to_oid(const ip4_addr_t *ip, u32_t *oid)
{
oid[0] = ip4_addr1(ip);
oid[1] = ip4_addr2(ip);
@ -136,6 +136,85 @@ snmp_ip_to_oid(const ip4_addr_t *ip, u32_t *oid)
}
#endif /* LWIP_IPV4 */
#if LWIP_IPV6
/**
* Conversion from oid to lwIP ip_addr
* @param ident points to u32_t ident[4] input
* @param ip points to output struct
*/
u8_t
snmp_oid_to_ip6(const u32_t *oid, ip6_addr_t *ip)
{
if((oid[0] > 0xFF) ||
(oid[1] > 0xFF) ||
(oid[2] > 0xFF) ||
(oid[3] > 0xFF) ||
(oid[4] > 0xFF) ||
(oid[5] > 0xFF) ||
(oid[6] > 0xFF) ||
(oid[7] > 0xFF) ||
(oid[8] > 0xFF) ||
(oid[9] > 0xFF) ||
(oid[10] > 0xFF) ||
(oid[11] > 0xFF) ||
(oid[12] > 0xFF) ||
(oid[13] > 0xFF) ||
(oid[14] > 0xFF) ||
(oid[15] > 0xFF)) {
ip6_addr_set_any(ip);
return 0;
}
ip->addr[0] = (oid[0] << 24) | (oid[1] << 16) | (oid[2] << 8) | (oid[3] << 0);
ip->addr[1] = (oid[4] << 24) | (oid[5] << 16) | (oid[6] << 8) | (oid[7] << 0);
ip->addr[2] = (oid[8] << 24) | (oid[9] << 16) | (oid[10] << 8) | (oid[11] << 0);
ip->addr[3] = (oid[12] << 24) | (oid[13] << 16) | (oid[14] << 8) | (oid[15] << 0);
return 1;
}
/**
* Conversion from lwIP ip_addr to oid
* @param ip points to input struct
* @param ident points to u32_t ident[4] output
*/
void
snmp_ip6_to_oid(const ip6_addr_t *ip, u32_t *oid)
{
oid[0] = (ip->addr[0] & 0xFF000000) >> 24;
oid[1] = (ip->addr[0] & 0x00FF0000) >> 16;
oid[2] = (ip->addr[0] & 0x0000FF00) >> 8;
oid[3] = (ip->addr[0] & 0x000000FF) >> 0;
oid[4] = (ip->addr[1] & 0xFF000000) >> 24;
oid[5] = (ip->addr[1] & 0x00FF0000) >> 16;
oid[6] = (ip->addr[1] & 0x0000FF00) >> 8;
oid[7] = (ip->addr[1] & 0x000000FF) >> 0;
oid[8] = (ip->addr[2] & 0xFF000000) >> 24;
oid[9] = (ip->addr[2] & 0x00FF0000) >> 16;
oid[10] = (ip->addr[2] & 0x0000FF00) >> 8;
oid[11] = (ip->addr[2] & 0x000000FF) >> 0;
oid[12] = (ip->addr[3] & 0xFF000000) >> 24;
oid[13] = (ip->addr[3] & 0x00FF0000) >> 16;
oid[14] = (ip->addr[3] & 0x0000FF00) >> 8;
oid[15] = (ip->addr[3] & 0x000000FF) >> 0;
}
#endif /* LWIP_IPV6 */
#if LWIP_IPV4 || LWIP_IPV6
void
snmp_ip_to_oid(const ip_addr_t *ip, u32_t *oid)
{
if(IP_IS_V6(ip)) {
#if LWIP_IPV6
snmp_ip6_to_oid(ip_2_ip6(ip), oid);
#endif /* LWIP_IPV6 */
} else {
#if LWIP_IPV4
snmp_ip4_to_oid(ip_2_ip4(ip), oid);
#endif /* LWIP_IPV4 */
}
}
#endif /* LWIP_IPV4 || LWIP_IPV6 */
void
snmp_oid_assign(struct snmp_obj_id* target, const u32_t *oid, u8_t oid_len)
{

View File

@ -1302,7 +1302,7 @@ ip_AddrTable_get_cell_value(const u32_t* column, const u32_t* row_oid, const u8_
}
/* get IP from incoming OID */
snmp_oid_to_ip(&row_oid[0], &ip); /* we know it succeeds because of oid_in_range check above */
snmp_oid_to_ip4(&row_oid[0], &ip); /* we know it succeeds because of oid_in_range check above */
/* find netif with requested ip */
netif = netif_list;
@ -1333,7 +1333,7 @@ ip_AddrTable_get_next_cell_instance_and_value(const u32_t* column, struct snmp_o
netif = netif_list;
while (netif != NULL) {
u32_t test_oid[LWIP_ARRAYSIZE(ip_AddrTable_oid_ranges)];
snmp_ip_to_oid(netif_ip4_addr(netif), &test_oid[0]);
snmp_ip4_to_oid(netif_ip4_addr(netif), &test_oid[0]);
/* check generated OID: is it a candidate for the next one? */
snmp_next_oid_check(&state, test_oid, LWIP_ARRAYSIZE(ip_AddrTable_oid_ranges), netif);
@ -1453,7 +1453,7 @@ ip_RouteTable_get_cell_value(const u32_t* column, const u32_t* row_oid, const u8
}
/* get IP and port from incoming OID */
snmp_oid_to_ip(&row_oid[0], &test_ip); /* we know it succeeds because of oid_in_range check above */
snmp_oid_to_ip4(&row_oid[0], &test_ip); /* we know it succeeds because of oid_in_range check above */
/* default route is on default netif */
if(ip4_addr_isany_val(test_ip) && (netif_default != NULL)) {
@ -1492,7 +1492,7 @@ ip_RouteTable_get_next_cell_instance_and_value(const u32_t* column, struct snmp_
/* check default route */
if(netif_default != NULL) {
snmp_ip_to_oid(IP4_ADDR_ANY, &test_oid[0]);
snmp_ip4_to_oid(IP4_ADDR_ANY, &test_oid[0]);
snmp_next_oid_check(&state, test_oid, LWIP_ARRAYSIZE(ip_RouteTable_oid_ranges), netif_default);
}
@ -1504,7 +1504,7 @@ ip_RouteTable_get_next_cell_instance_and_value(const u32_t* column, struct snmp_
/* check generated OID: is it a candidate for the next one? */
if (!ip4_addr_isany_val(dst)) {
snmp_ip_to_oid(&dst, &test_oid[0]);
snmp_ip4_to_oid(&dst, &test_oid[0]);
snmp_next_oid_check(&state, test_oid, LWIP_ARRAYSIZE(ip_RouteTable_oid_ranges), netif);
}
@ -1514,7 +1514,7 @@ ip_RouteTable_get_next_cell_instance_and_value(const u32_t* column, struct snmp_
/* did we find a next one? */
if(state.status == SNMP_NEXT_OID_STATUS_SUCCESS) {
ip4_addr_t dst;
snmp_oid_to_ip(&result_temp[0], &dst);
snmp_oid_to_ip4(&result_temp[0], &dst);
snmp_oid_assign(row_oid, state.next_oid, state.next_oid_len);
/* fill in object properties */
return ip_RouteTable_get_cell_value_core((struct netif*)state.reference, ip4_addr_isany_val(dst), column, value, value_len);
@ -1580,7 +1580,7 @@ ip_NetToMediaTable_get_cell_value(const u32_t* column, const u32_t* row_oid, con
/* get IP from incoming OID */
netif_index = (u8_t)row_oid[0];
snmp_oid_to_ip(&row_oid[1], &ip_in); /* we know it succeeds because of oid_in_range check above */
snmp_oid_to_ip4(&row_oid[1], &ip_in); /* we know it succeeds because of oid_in_range check above */
/* find requested entry */
for(i=0; i<ARP_TABLE_SIZE; i++) {
@ -1620,7 +1620,7 @@ ip_NetToMediaTable_get_next_cell_instance_and_value(const u32_t* column, struct
u32_t test_oid[LWIP_ARRAYSIZE(ip_NetToMediaTable_oid_ranges)];
test_oid[0] = netif_to_num(netif);
snmp_ip_to_oid(ip, &test_oid[1]);
snmp_ip4_to_oid(ip, &test_oid[1]);
/* check generated OID: is it a candidate for the next one? */
snmp_next_oid_check(&state, test_oid, LWIP_ARRAYSIZE(ip_NetToMediaTable_oid_ranges), (void*)(size_t)i);
@ -1882,9 +1882,9 @@ tcp_ConnTable_get_cell_value(const u32_t* column, const u32_t* row_oid, const u8
}
/* get IPs and ports from incoming OID */
snmp_oid_to_ip(&row_oid[0], &local_ip); /* we know it succeeds because of oid_in_range check above */
snmp_oid_to_ip4(&row_oid[0], &local_ip); /* we know it succeeds because of oid_in_range check above */
local_port = (u16_t)row_oid[4];
snmp_oid_to_ip(&row_oid[5], &remote_ip); /* we know it succeeds because of oid_in_range check above */
snmp_oid_to_ip4(&row_oid[5], &remote_ip); /* we know it succeeds because of oid_in_range check above */
remote_port = (u16_t)row_oid[9];
/* find tcp_pcb with requested ips and ports */
@ -1937,18 +1937,18 @@ tcp_ConnTable_get_next_cell_instance_and_value(const u32_t* column, struct snmp_
u32_t test_oid[LWIP_ARRAYSIZE(tcp_ConnTable_oid_ranges)];
if(!IP_IS_V6_VAL(pcb->local_ip)) {
snmp_ip_to_oid(ip_2_ip4(&pcb->local_ip), &test_oid[0]);
snmp_ip4_to_oid(ip_2_ip4(&pcb->local_ip), &test_oid[0]);
test_oid[4] = pcb->local_port;
/* PCBs in state LISTEN are not connected and have no remote_ip or remote_port */
if(pcb->state == LISTEN) {
snmp_ip_to_oid(IP4_ADDR_ANY, &test_oid[5]);
snmp_ip4_to_oid(IP4_ADDR_ANY, &test_oid[5]);
test_oid[9] = 0;
} else {
if(IP_IS_V6_VAL(pcb->remote_ip)) { /* should never happen */
continue;
}
snmp_ip_to_oid(ip_2_ip4(&pcb->remote_ip), &test_oid[5]);
snmp_ip4_to_oid(ip_2_ip4(&pcb->remote_ip), &test_oid[5]);
test_oid[9] = pcb->remote_port;
}
@ -2048,7 +2048,7 @@ udp_Table_get_cell_value(const u32_t* column, const u32_t* row_oid, const u8_t r
}
/* get IP and port from incoming OID */
snmp_oid_to_ip(&row_oid[0], &ip); /* we know it succeeds because of oid_in_range check above */
snmp_oid_to_ip4(&row_oid[0], &ip); /* we know it succeeds because of oid_in_range check above */
port = (u16_t)row_oid[4];
/* find udp_pcb with requested ip and port*/
@ -2083,7 +2083,7 @@ udp_Table_get_next_cell_instance_and_value(const u32_t* column, struct snmp_obj_
u32_t test_oid[LWIP_ARRAYSIZE(udp_Table_oid_ranges)];
if(!IP_IS_V6_VAL(pcb->local_ip)) {
snmp_ip_to_oid(ip_2_ip4(&pcb->local_ip), &test_oid[0]);
snmp_ip4_to_oid(ip_2_ip4(&pcb->local_ip), &test_oid[0]);
test_oid[4] = pcb->local_port;
/* check generated OID: is it a candidate for the next one? */

View File

@ -298,9 +298,16 @@ u8_t snmp_oid_equal(const u32_t *oid1, u8_t oid1_len, const u32_t *oid2, u8_t oi
s8_t snmp_oid_compare(const u32_t *oid1, u8_t oid1_len, const u32_t *oid2, u8_t oid2_len);
#if LWIP_IPV4
u8_t snmp_oid_to_ip(const u32_t *oid, ip4_addr_t *ip);
void snmp_ip_to_oid(const ip4_addr_t *ip, u32_t *oid);
u8_t snmp_oid_to_ip4(const u32_t *oid, ip4_addr_t *ip);
void snmp_ip4_to_oid(const ip4_addr_t *ip, u32_t *oid);
#endif /* LWIP_IPV4 */
#if LWIP_IPV6
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
void snmp_ip_to_oid(const ip_addr_t *ip, u32_t *oid);
#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 */