Set TODO_SNMP_MSG_EVENT to 1. Moved some more code from snmp_recv() to snmp_event_service().

This commit is contained in:
christiaans 2006-09-22 08:05:17 +00:00
parent a5532ffd4d
commit d0f279ece4

View File

@ -50,7 +50,7 @@
#include "lwip/snmp_msg.h" #include "lwip/snmp_msg.h"
#include "lwip/snmp_structs.h" #include "lwip/snmp_structs.h"
#define TODO_SNMP_MSG_EVENT 0 #define TODO_SNMP_MSG_EVENT 1
/* public (non-static) constants */ /* public (non-static) constants */
/** SNMP v1 == 0 */ /** SNMP v1 == 0 */
@ -104,22 +104,18 @@ snmp_init(void)
#if TODO_SNMP_MSG_EVENT #if TODO_SNMP_MSG_EVENT
/** /**
* called for each variable binding (also for the fist one) * Handle a single internal or external event.
* Called for each variable binding (also for the fist one).
* *
* @param request_id identifies requests from 0 to (SNMP_CONCURRENT_REQUESTS-1) * @param request_id identifies requests from 0 to (SNMP_CONCURRENT_REQUESTS-1)
* @param pm points to a private message used for * @param msg_ps points to the assosicated message process state
*/ */
void void
snmp_msg_event(u8_t request_id) snmp_msg_event_service(u8_t request_id, struct snmp_msg_pstat *msg_ps)
{ {
struct snmp_msg_pstat *msg_ps;
struct mib_node *mn; struct mib_node *mn;
struct snmp_name_ptr np; struct snmp_name_ptr np;
if (request_id < SNMP_CONCURRENT_REQUESTS)
{
msg_ps = &msg_input_list[request_id];
LWIP_DEBUGF(SNMP_MSG_DEBUG, ("snmp_msg_event: msg_ps->state==%"U16_F"\n",(u16_t)msg_ps->state)); LWIP_DEBUGF(SNMP_MSG_DEBUG, ("snmp_msg_event: msg_ps->state==%"U16_F"\n",(u16_t)msg_ps->state));
if (msg_ps->state == SNMP_MSG_DEMUX) if (msg_ps->state == SNMP_MSG_DEMUX)
{ {
@ -130,7 +126,6 @@ snmp_msg_event(u8_t request_id)
else else
{ {
msg_ps->vb_ptr = msg_ps->vb_ptr->next; msg_ps->vb_ptr = msg_ps->vb_ptr->next;
msg_ps->vb_idx += 1;
} }
if (msg_ps->rt == SNMP_ASN1_PDU_GET_REQ) if (msg_ps->rt == SNMP_ASN1_PDU_GET_REQ)
{ {
@ -174,6 +169,58 @@ snmp_msg_event(u8_t request_id)
/* search failed, object id points to unknown object (nosuchname) */ /* search failed, object id points to unknown object (nosuchname) */
mn = NULL; mn = NULL;
} }
if (mn != NULL)
{
struct snmp_varbind *vb;
/* allocate output varbind */
vb = (struct snmp_varbind *)mem_malloc(sizeof(struct snmp_varbind));
LWIP_ASSERT("vb != NULL",vb != NULL);
if (vb != NULL)
{
vb->next = NULL;
vb->prev = NULL;
/* move name from invb to outvb */
vb->ident = msg_ps->vb_ptr->ident;
vb->ident_len = msg_ps->vb_ptr->ident_len;
/* ensure this memory is refereced once only */
msg_ps->vb_ptr->ident = NULL;
msg_ps->vb_ptr->ident_len = 0;
vb->value_type = object_def.asn_type;
vb->value_len = object_def.v_len;
if (vb->value_len > 0)
{
vb->value = mem_malloc(vb->value_len);
LWIP_ASSERT("vb->value != NULL",vb->value != NULL);
if (vb->value != NULL)
{
mn->get_value(&object_def, vb->value_len, vb->value);
snmp_varbind_tail_add(&msg_ps->outvb, vb);
}
else
{
LWIP_DEBUGF(SNMP_MSG_DEBUG, ("snmp_msg_event: couldn't allocate variable space\n"));
msg_ps->vb_ptr->ident = vb->ident;
msg_ps->vb_ptr->ident_len = vb->ident_len;
mem_free(vb);
/** @todo end varbind loop!! */
}
}
else
{
/* vb->value_len == 0, empty value (e.g. empty string) */
vb->value = NULL;
snmp_varbind_tail_add(&msg_ps->outvb, vb);
}
}
else
{
LWIP_DEBUGF(SNMP_MSG_DEBUG, ("snmp_msg_event: couldn't allocate outvb space\n"));
/** @todo end varbind loop!! */
}
}
msg_ps->state = SNMP_MSG_DEMUX; msg_ps->state = SNMP_MSG_DEMUX;
} }
} }
@ -191,6 +238,86 @@ snmp_msg_event(u8_t request_id)
msg_ps->state = SNMP_MSG_EMPTY; msg_ps->state = SNMP_MSG_EMPTY;
} }
} }
else if (msg_ps->rt == SNMP_ASN1_PDU_GET_NEXT_REQ)
{
struct snmp_obj_id oid;
if (snmp_iso_prefix_expand(msg_ps->vb_ptr->ident_len, msg_ps->vb_ptr->ident, &oid))
{
if (msg_ps->vb_ptr->ident_len > 3)
{
/* can offset ident_len and ident */
mn = snmp_expand_tree((struct mib_node*)&internet,
msg_ps->vb_ptr->ident_len - 4,
msg_ps->vb_ptr->ident + 4, &oid);
}
else
{
/* can't offset ident_len -4, ident + 4 */
mn = snmp_expand_tree((struct mib_node*)&internet, 0, NULL, &oid);
}
}
else
{
mn = NULL;
}
if (mn != NULL)
{
if (mn->node_type == MIB_NODE_EX)
{
/* external object */
struct mib_external_node *en = (struct mib_external_node*)mn;
msg_ps->state = SNMP_MSG_EXTERNAL_GET_OBJDEF;
/* save en && args in msg_ps!! */
msg_ps->ext_mib_node = en;
msg_ps->ext_name_ptr = np;
en->get_object_def_q(en->addr_inf, request_id, np.ident_len, np.ident);
}
else
{
/* internal object */
struct obj_def object_def;
struct snmp_varbind *vb;
msg_ps->state = SNMP_MSG_INTERNAL;
mn->get_object_def(1, &oid.id[oid.len - 1], &object_def);
vb = snmp_varbind_alloc(&oid, object_def.asn_type, object_def.v_len);
if (vb != NULL)
{
mn->get_value(&object_def, object_def.v_len, vb->value);
snmp_varbind_tail_add(&msg_ps->outvb, vb);
}
else
{
LWIP_DEBUGF(SNMP_MSG_DEBUG, ("snmp_recv couldn't allocate outvb space\n"));
/** @todo end varbind loop!! */
}
msg_ps->state = SNMP_MSG_DEMUX;
}
}
if (mn == NULL)
{
/* mn == NULL, noSuchName */
snmp_varbind_list_free(&msg_ps->outvb);
msg_ps->outvb = msg_ps->invb;
msg_ps->invb.head = NULL;
msg_ps->invb.tail = NULL;
msg_ps->invb.count = 0;
msg_ps->error_status = SNMP_ES_NOSUCHNAME;
msg_ps->error_index = 1 + msg_ps->vb_idx;
snmp_send_response(msg_ps);
msg_ps->state = SNMP_MSG_EMPTY;
}
}
else if (msg_ps->rt == SNMP_ASN1_PDU_SET_REQ)
{
/** @todo actual SNMP SET */
}
} }
else if (msg_ps->state == SNMP_MSG_EXTERNAL_GET_OBJDEF) else if (msg_ps->state == SNMP_MSG_EXTERNAL_GET_OBJDEF)
{ {
@ -205,14 +332,15 @@ snmp_msg_event(u8_t request_id)
if (msg_ps->ext_object_def.instance != MIB_OBJECT_NONE) if (msg_ps->ext_object_def.instance != MIB_OBJECT_NONE)
{ {
if (msg_ps->rt == SNMP_ASN1_PDU_GET_REQ) if ((msg_ps->rt == SNMP_ASN1_PDU_GET_REQ) ||
(msg_ps->rt == SNMP_ASN1_PDU_GET_NEXT_REQ))
{ {
msg_ps->state = SNMP_MSG_EXTERNAL_GET_VALUE; msg_ps->state = SNMP_MSG_EXTERNAL_GET_VALUE;
en->get_value_q(request_id, &msg_ps->ext_object_def); en->get_value_q(request_id, &msg_ps->ext_object_def);
} }
else else
{ {
/** @todo other SNMP requests */ /** @todo SNMP SET */
} }
} }
else else
@ -244,6 +372,7 @@ snmp_msg_event(u8_t request_id)
/* allocate output varbind */ /* allocate output varbind */
vb = (struct snmp_varbind *)mem_malloc(sizeof(struct snmp_varbind)); vb = (struct snmp_varbind *)mem_malloc(sizeof(struct snmp_varbind));
LWIP_ASSERT("vb != NULL",vb != NULL);
if (vb != NULL) if (vb != NULL)
{ {
vb->next = NULL; vb->next = NULL;
@ -258,20 +387,35 @@ snmp_msg_event(u8_t request_id)
vb->value_type = msg_ps->ext_object_def.asn_type; vb->value_type = msg_ps->ext_object_def.asn_type;
vb->value_len = msg_ps->ext_object_def.v_len; vb->value_len = msg_ps->ext_object_def.v_len;
vb->value = mem_malloc(msg_ps->ext_object_def.v_len); if (vb->value_len > 0)
{
vb->value = mem_malloc(vb->value_len);
LWIP_ASSERT("vb->value != NULL",vb->value != NULL);
if (vb->value != NULL) if (vb->value != NULL)
{ {
en->get_value_a(request_id, &msg_ps->ext_object_def, msg_ps->ext_object_def.v_len, vb->value); en->get_value_a(request_id, &msg_ps->ext_object_def, vb->value_len, vb->value);
snmp_varbind_tail_add(&msg_ps->outvb, vb); snmp_varbind_tail_add(&msg_ps->outvb, vb);
} }
else else
{ {
LWIP_DEBUGF(SNMP_MSG_DEBUG, ("snmp_msg_event: no variable space\n")); LWIP_DEBUGF(SNMP_MSG_DEBUG, ("snmp_msg_event: no variable space\n"));
msg_ps->vb_ptr->ident = vb->ident;
msg_ps->vb_ptr->ident_len = vb->ident_len;
mem_free(vb);
/** @todo end varbind loop */
}
}
else
{
/* vb->value_len == 0, empty value (e.g. empty string) */
vb->value = NULL;
snmp_varbind_tail_add(&msg_ps->outvb, vb);
} }
} }
else else
{ {
LWIP_DEBUGF(SNMP_MSG_DEBUG, ("snmp_msg_event: no outvb space\n")); LWIP_DEBUGF(SNMP_MSG_DEBUG, ("snmp_msg_event: no outvb space\n"));
/** @todo end varbind loop!! */
} }
} }
else else
@ -282,18 +426,21 @@ snmp_msg_event(u8_t request_id)
} }
else if (msg_ps->state == SNMP_MSG_EXTERNAL_SET_TEST) else if (msg_ps->state == SNMP_MSG_EXTERNAL_SET_TEST)
{ {
} }
else if (msg_ps->state == SNMP_MSG_EXTERNAL_SET_VALUE) else if (msg_ps->state == SNMP_MSG_EXTERNAL_SET_VALUE)
{ {
} }
/* if next state is demuxing AND we demuxed all input varbinds, /* if next state is demuxing AND we demuxed all input varbinds,
we can make a reply and set the state to SNMP_MSG_EMPTY. */ we can make a reply and set the state to SNMP_MSG_EMPTY. */
if ((msg_ps->state == SNMP_MSG_DEMUX) && if (msg_ps->state == SNMP_MSG_DEMUX)
((msg_ps->vb_idx + 1) == msg_ps->invb.count)) {
if (msg_ps->vb_idx < msg_ps->invb.count)
{
msg_ps->vb_idx += 1;
}
if (msg_ps->vb_idx == msg_ps->invb.count)
{ {
err_t err_ret; err_t err_ret;
@ -314,7 +461,46 @@ snmp_msg_event(u8_t request_id)
snmp_varbind_list_free(&msg_ps->outvb); snmp_varbind_list_free(&msg_ps->outvb);
msg_ps->state = SNMP_MSG_EMPTY; msg_ps->state = SNMP_MSG_EMPTY;
} }
}
}
/**
* Handle one internal or external event.
* Called for one async event. (recv external/private answer)
*
* @param request_id identifies requests from 0 to (SNMP_CONCURRENT_REQUESTS-1)
*/
void
snmp_msg_event(u8_t request_id)
{
struct snmp_msg_pstat *msg_ps;
if (request_id < SNMP_CONCURRENT_REQUESTS)
{
msg_ps = &msg_input_list[request_id];
snmp_msg_event_service(request_id, msg_ps);
}
}
/**
* Handle internal or external events until external object is found.
* Called for each async event. (recv snmp input, recv external/private answer)
*
* @param request_id identifies requests from 0 to (SNMP_CONCURRENT_REQUESTS-1)
*/
void
snmp_msg_event_loop(u8_t request_id)
{
struct snmp_msg_pstat *msg_ps;
if (request_id < SNMP_CONCURRENT_REQUESTS)
{
msg_ps = &msg_input_list[request_id];
/* iterate over input varbind list and change msg_ps->state */
while (msg_ps->state == SNMP_MSG_DEMUX)
{
snmp_msg_event_service(request_id, msg_ps);
}
} }
} }
#endif #endif
@ -389,8 +575,10 @@ snmp_recv(void *arg, struct udp_pcb *pcb, struct pbuf *p, struct ip_addr *addr,
err_ret = snmp_pdu_dec_varbindlist(p, varbind_ofs, &varbind_ofs, msg_ps); err_ret = snmp_pdu_dec_varbindlist(p, varbind_ofs, &varbind_ofs, msg_ps);
if ((err_ret == ERR_OK) && (msg_ps->invb.count > 0)) if ((err_ret == ERR_OK) && (msg_ps->invb.count > 0))
{ {
#if !TODO_SNMP_MSG_EVENT
struct mib_node *mn; struct mib_node *mn;
struct obj_def object_def; struct obj_def object_def;
#endif
/* we've decoded the incoming message, release input msg now */ /* we've decoded the incoming message, release input msg now */
pbuf_free(p); pbuf_free(p);
@ -405,12 +593,10 @@ snmp_recv(void *arg, struct udp_pcb *pcb, struct pbuf *p, struct ip_addr *addr,
LWIP_DEBUGF(SNMP_MSG_DEBUG, ("snmp_recv varbind cnt=%"U16_F"\n",(u16_t)msg_ps->invb.count)); LWIP_DEBUGF(SNMP_MSG_DEBUG, ("snmp_recv varbind cnt=%"U16_F"\n",(u16_t)msg_ps->invb.count));
#if TODO_SNMP_MSG_EVENT #if TODO_SNMP_MSG_EVENT
/* handle input events (and demux as much as possible in one go) */ /* handle input event and demux as much as possible in one go */
while (msg_ps->state == SNMP_MSG_DEMUX)
{ snmp_msg_event_loop(req_idx);
/** iterate over invb and change msg_ps->state */
snmp_msg_event(req_idx);
}
#else #else
if (msg_ps->rt == SNMP_ASN1_PDU_GET_REQ) if (msg_ps->rt == SNMP_ASN1_PDU_GET_REQ)
{ {
@ -448,6 +634,7 @@ snmp_recv(void *arg, struct udp_pcb *pcb, struct pbuf *p, struct ip_addr *addr,
/* allocate output varbind */ /* allocate output varbind */
vb = (struct snmp_varbind *)mem_malloc(sizeof(struct snmp_varbind)); vb = (struct snmp_varbind *)mem_malloc(sizeof(struct snmp_varbind));
LWIP_ASSERT("vb != NULL",vb != NULL);
if (vb != NULL) if (vb != NULL)
{ {
vb->next = NULL; vb->next = NULL;
@ -462,20 +649,32 @@ snmp_recv(void *arg, struct udp_pcb *pcb, struct pbuf *p, struct ip_addr *addr,
vb->value_type = object_def.asn_type; vb->value_type = object_def.asn_type;
vb->value_len = object_def.v_len; vb->value_len = object_def.v_len;
vb->value = mem_malloc(object_def.v_len); if (vb->value_len > 0)
{
vb->value = mem_malloc(vb->value_len);
LWIP_ASSERT("vb->value != NULL",vb->value != NULL);
if (vb->value != NULL) if (vb->value != NULL)
{ {
mn->get_value(&object_def, object_def.v_len, vb->value); mn->get_value(&object_def, vb->value_len, vb->value);
snmp_varbind_tail_add(&msg_ps->outvb, vb); snmp_varbind_tail_add(&msg_ps->outvb, vb);
} }
else else
{ {
LWIP_DEBUGF(SNMP_MSG_DEBUG, ("snmp_recv couldn't allocate variable space")); LWIP_DEBUGF(SNMP_MSG_DEBUG, ("snmp_recv couldn't allocate variable space\n"));
msg_ps->vb_ptr->ident = vb->ident;
msg_ps->vb_ptr->ident_len = vb->ident_len;
mem_free(vb);
} }
} }
else else
{ {
LWIP_DEBUGF(SNMP_MSG_DEBUG, ("snmp_recv couldn't allocate outvb space")); vb->value = NULL;
snmp_varbind_tail_add(&msg_ps->outvb, vb);
}
}
else
{
LWIP_DEBUGF(SNMP_MSG_DEBUG, ("snmp_recv couldn't allocate outvb space\n"));
} }
} }
else else
@ -528,7 +727,7 @@ snmp_recv(void *arg, struct udp_pcb *pcb, struct pbuf *p, struct ip_addr *addr,
} }
else else
{ {
LWIP_DEBUGF(SNMP_MSG_DEBUG, ("snmp_recv couldn't allocate outvb space")); LWIP_DEBUGF(SNMP_MSG_DEBUG, ("snmp_recv couldn't allocate outvb space\n"));
} }
} }
else else
@ -638,13 +837,13 @@ snmp_recv(void *arg, struct udp_pcb *pcb, struct pbuf *p, struct ip_addr *addr,
snmp_varbind_list_free(&msg_ps->invb); snmp_varbind_list_free(&msg_ps->invb);
snmp_varbind_list_free(&msg_ps->outvb); snmp_varbind_list_free(&msg_ps->outvb);
msg_ps->state = SNMP_MSG_EMPTY; msg_ps->state = SNMP_MSG_EMPTY;
#endif /* disable */ #endif /* TODO_SNMP_MSG_EVENT */
} }
else else
{ {
/* varbind-list decode failed, or varbind list empty (silly cmd for agent) */ /* varbind-list decode failed, or varbind list empty (silly cmd for agent) */
pbuf_free(p); pbuf_free(p);
LWIP_DEBUGF(SNMP_MSG_DEBUG, ("snmp_pdu_dec_varbindlist() failed")); LWIP_DEBUGF(SNMP_MSG_DEBUG, ("snmp_pdu_dec_varbindlist() failed\n"));
msg_ps->error_status = SNMP_ES_GENERROR; msg_ps->error_status = SNMP_ES_GENERROR;
msg_ps->error_index = 0; msg_ps->error_index = 0;
msg_ps->outvb.head = NULL; msg_ps->outvb.head = NULL;
@ -659,7 +858,7 @@ snmp_recv(void *arg, struct udp_pcb *pcb, struct pbuf *p, struct ip_addr *addr,
{ {
/* header check failed! */ /* header check failed! */
pbuf_free(p); pbuf_free(p);
LWIP_DEBUGF(SNMP_MSG_DEBUG, ("snmp_pdu_header_check() failed")); LWIP_DEBUGF(SNMP_MSG_DEBUG, ("snmp_pdu_header_check() failed\n"));
} }
} }
else else
@ -1076,6 +1275,7 @@ snmp_varbind_alloc(struct snmp_obj_id *oid, u8_t type, u8_t len)
struct snmp_varbind *vb; struct snmp_varbind *vb;
vb = (struct snmp_varbind *)mem_malloc(sizeof(struct snmp_varbind)); vb = (struct snmp_varbind *)mem_malloc(sizeof(struct snmp_varbind));
LWIP_ASSERT("vb != NULL",vb != NULL);
if (vb != NULL) if (vb != NULL)
{ {
u8_t i; u8_t i;
@ -1088,6 +1288,7 @@ snmp_varbind_alloc(struct snmp_obj_id *oid, u8_t type, u8_t len)
{ {
/* allocate array of s32_t for our object identifier */ /* allocate array of s32_t for our object identifier */
vb->ident = (s32_t*)mem_malloc(sizeof(s32_t) * i); vb->ident = (s32_t*)mem_malloc(sizeof(s32_t) * i);
LWIP_ASSERT("vb->ident != NULL",vb->ident != NULL);
if (vb->ident == NULL) if (vb->ident == NULL)
{ {
mem_free(vb); mem_free(vb);
@ -1110,6 +1311,7 @@ snmp_varbind_alloc(struct snmp_obj_id *oid, u8_t type, u8_t len)
{ {
/* allocate raw bytes for our object value */ /* allocate raw bytes for our object value */
vb->value = mem_malloc(len); vb->value = mem_malloc(len);
LWIP_ASSERT("vb->value != NULL",vb->value != NULL);
if (vb->value == NULL) if (vb->value == NULL)
{ {
if (vb->ident != NULL) if (vb->ident != NULL)