Added some defensive programming

This commit is contained in:
Adam Dunkels 2011-08-31 20:53:48 +02:00
parent 2b245cf850
commit 81e098d2da

View file

@ -46,23 +46,25 @@ extern u16_t uip_slen;
#include <string.h> #include <string.h>
/*---------------------------------------------------------------------------*/ /*---------------------------------------------------------------------------*/
void void
uip_udp_packet_send(struct uip_udp_conn *c, const void *data, int len) uip_udp_packet_send(struct uip_udp_conn *c, const void *data, int len)
{ {
#if UIP_UDP #if UIP_UDP
uip_udp_conn = c; if(data != NULL) {
uip_slen = len; uip_udp_conn = c;
memcpy(&uip_buf[UIP_LLH_LEN + UIP_IPUDPH_LEN], data, len > UIP_BUFSIZE? UIP_BUFSIZE: len); uip_slen = len;
uip_process(UIP_UDP_SEND_CONN); memcpy(&uip_buf[UIP_LLH_LEN + UIP_IPUDPH_LEN], data,
#if UIP_CONF_IPV6 //math len > UIP_BUFSIZE? UIP_BUFSIZE: len);
tcpip_ipv6_output(); uip_process(UIP_UDP_SEND_CONN);
#if UIP_CONF_IPV6
tcpip_ipv6_output();
#else #else
if(uip_len > 0) { if(uip_len > 0) {
tcpip_output(); tcpip_output();
} }
#endif #endif
}
uip_slen = 0; uip_slen = 0;
#endif /* UIP_UDP */ #endif /* UIP_UDP */
} }
@ -74,18 +76,20 @@ uip_udp_packet_sendto(struct uip_udp_conn *c, const void *data, int len,
uip_ipaddr_t curaddr; uip_ipaddr_t curaddr;
uint16_t curport; uint16_t curport;
/* Save current IP addr/port. */ if(toaddr != NULL) {
uip_ipaddr_copy(&curaddr, &c->ripaddr); /* Save current IP addr/port. */
curport = c->rport; uip_ipaddr_copy(&curaddr, &c->ripaddr);
curport = c->rport;
/* Load new IP addr/port */ /* Load new IP addr/port */
uip_ipaddr_copy(&c->ripaddr, toaddr); uip_ipaddr_copy(&c->ripaddr, toaddr);
c->rport = toport; c->rport = toport;
uip_udp_packet_send(c, data, len); uip_udp_packet_send(c, data, len);
/* Restore old IP addr/port */ /* Restore old IP addr/port */
uip_ipaddr_copy(&c->ripaddr, &curaddr); uip_ipaddr_copy(&c->ripaddr, &curaddr);
c->rport = curport; c->rport = curport;
}
} }
/*---------------------------------------------------------------------------*/ /*---------------------------------------------------------------------------*/