Browse Source

gnrc udp: simplify gnrc_udp_hdr_build

Directly using 16 bit port numbers instead of casting uint8_t pointers.
pr/gpio
Oleg Hahm 7 years ago
parent
commit
487f6e480f
  1. 13
      examples/gnrc_networking/udp.c
  2. 7
      sys/include/net/gnrc/udp.h
  3. 3
      sys/net/gnrc/application_layer/tftp/gnrc_tftp.c
  4. 3
      sys/net/gnrc/application_layer/zep/gnrc_zep.c
  5. 3
      sys/net/gnrc/conn/udp/gnrc_conn_udp.c
  6. 12
      sys/net/gnrc/netreg/gnrc_netreg.c
  7. 14
      sys/net/gnrc/transport_layer/udp/gnrc_udp.c

13
examples/gnrc_networking/udp.c

@ -34,8 +34,7 @@ static gnrc_netreg_entry_t server = { NULL, GNRC_NETREG_DEMUX_CTX_ALL, KERNEL_PI
static void send(char *addr_str, char *port_str, char *data, unsigned int num,
unsigned int delay)
{
uint8_t port[2];
uint16_t tmp;
uint16_t port;
ipv6_addr_t addr;
/* parse destination address */
@ -44,13 +43,11 @@ static void send(char *addr_str, char *port_str, char *data, unsigned int num,
return;
}
/* parse port */
tmp = (uint16_t)atoi(port_str);
if (tmp == 0) {
port = (uint16_t)atoi(port_str);
if (port == 0) {
puts("Error: unable to parse destination port");
return;
}
port[0] = (uint8_t)tmp;
port[1] = tmp >> 8;
for (unsigned int i = 0; i < num; i++) {
gnrc_pktsnip_t *payload, *udp, *ip;
@ -61,7 +58,7 @@ static void send(char *addr_str, char *port_str, char *data, unsigned int num,
return;
}
/* allocate UDP header, set source port := destination port */
udp = gnrc_udp_hdr_build(payload, port, 2, port, 2);
udp = gnrc_udp_hdr_build(payload, port, port);
if (udp == NULL) {
puts("Error: unable to allocate UDP header");
gnrc_pktbuf_release(payload);
@ -81,7 +78,7 @@ static void send(char *addr_str, char *port_str, char *data, unsigned int num,
return;
}
printf("Success: send %u byte to [%s]:%u\n", (unsigned)payload->size,
addr_str, tmp);
addr_str, port);
xtimer_usleep(delay);
}
}

7
sys/include/net/gnrc/udp.h

@ -71,17 +71,14 @@ int gnrc_udp_calc_csum(gnrc_pktsnip_t *hdr, gnrc_pktsnip_t *pseudo_hdr);
*
* @param[in] payload Payload contained in the UDP packet
* @param[in] src Source port in host byte order
* @param[in] src_len Length of @p src, must be 2
* @param[in] dst Destination port in host byte order
* @param[in] dst_len Length of @p dst, must be 2
*
* @return pointer to the newly created (and allocated) header
* @return NULL on `src == NULL`, `dst == NULL`, `src_len != 2`, `dst_len != 2`
* or on allocation error
*/
gnrc_pktsnip_t *gnrc_udp_hdr_build(gnrc_pktsnip_t *payload,
uint8_t *src, size_t src_len,
uint8_t *dst, size_t dst_len);
gnrc_pktsnip_t *gnrc_udp_hdr_build(gnrc_pktsnip_t *payload, uint16_t src,
uint16_t dst);
/**
* @brief Initialize and start UDP

3
sys/net/gnrc/application_layer/tftp/gnrc_tftp.c

@ -958,8 +958,7 @@ tftp_state _tftp_send(gnrc_pktsnip_t *buf, tftp_context_t *ctxt, size_t len)
/* allocate UDP header, set source port := destination port */
src_port.u16 = ctxt->src_port;
dst_port.u16 = ctxt->dst_port;
udp = gnrc_udp_hdr_build(buf, src_port.u8, sizeof(src_port),
dst_port.u8, sizeof(dst_port));
udp = gnrc_udp_hdr_build(buf, src_port.u16, dst_port.u16);
if (udp == NULL) {
DEBUG("tftp: error unable to allocate UDP header");
gnrc_pktbuf_release(buf);

3
sys/net/gnrc/application_layer/zep/gnrc_zep.c

@ -230,8 +230,7 @@ static int _send(gnrc_netdev_t *netdev, gnrc_pktsnip_t *pkt)
zep = new_pkt->data;
hdr = gnrc_udp_hdr_build(new_pkt, (uint8_t *)(&(dev->src_port)), sizeof(uint16_t),
(uint8_t *)(&(dev->dst_port)), sizeof(uint16_t));
hdr = gnrc_udp_hdr_build(new_pkt, dev->src_port, dev->dst_port);
if (hdr == NULL) {
DEBUG("zep: could not allocate UDP header in pktbuf\n");

3
sys/net/gnrc/conn/udp/gnrc_conn_udp.c

@ -96,8 +96,7 @@ int conn_udp_sendto(const void *data, size_t len, const void *src, size_t src_le
gnrc_pktsnip_t *pkt, *hdr = NULL;
pkt = gnrc_pktbuf_add(NULL, (void *)data, len, GNRC_NETTYPE_UNDEF); /* data will only be copied */
hdr = gnrc_udp_hdr_build(pkt, (uint8_t *)&sport, sizeof(uint16_t), (uint8_t *)&dport,
sizeof(uint16_t));
hdr = gnrc_udp_hdr_build(pkt, sport, dport);
if (hdr == NULL) {
gnrc_pktbuf_release(pkt);
return -ENOMEM;

12
sys/net/gnrc/netreg/gnrc_netreg.c

@ -148,12 +148,20 @@ gnrc_pktsnip_t *gnrc_netreg_hdr_build(gnrc_nettype_t type, gnrc_pktsnip_t *paylo
#ifdef MODULE_GNRC_TCP
case GNRC_NETTYPE_TCP:
return gnrc_tcp_hdr_build(payload, src, src_len, dst, dst_len);
{
uint16_t src_port = *((uint16_t *)src);
uint16_t dst_port = *((uint16_t *)dst);
return gnrc_tcp_hdr_build(payload, src_port, dst_port);
}
#endif
#ifdef MODULE_GNRC_UDP
case GNRC_NETTYPE_UDP:
return gnrc_udp_hdr_build(payload, src, src_len, dst, dst_len);
{
uint16_t src_port = *((uint16_t *)src);
uint16_t dst_port = *((uint16_t *)dst);
return gnrc_udp_hdr_build(payload, src_port, dst_port);
}
#endif
default:

14
sys/net/gnrc/transport_layer/udp/gnrc_udp.c

@ -271,18 +271,12 @@ int gnrc_udp_calc_csum(gnrc_pktsnip_t *hdr, gnrc_pktsnip_t *pseudo_hdr)
return 0;
}
gnrc_pktsnip_t *gnrc_udp_hdr_build(gnrc_pktsnip_t *payload,
uint8_t *src, size_t src_len,
uint8_t *dst, size_t dst_len)
gnrc_pktsnip_t *gnrc_udp_hdr_build(gnrc_pktsnip_t *payload, uint16_t src,
uint16_t dst)
{
gnrc_pktsnip_t *res;
udp_hdr_t *hdr;
/* check parameters */
if (src == NULL || dst == NULL ||
src_len != sizeof(uint16_t) || dst_len != sizeof(uint16_t)) {
return NULL;
}
/* allocate header */
res = gnrc_pktbuf_add(payload, NULL, sizeof(udp_hdr_t), GNRC_NETTYPE_UDP);
if (res == NULL) {
@ -290,8 +284,8 @@ gnrc_pktsnip_t *gnrc_udp_hdr_build(gnrc_pktsnip_t *payload,
}
/* initialize header */
hdr = (udp_hdr_t *)res->data;
hdr->src_port = byteorder_htons(*((uint16_t *)src));
hdr->dst_port = byteorder_htons(*((uint16_t *)dst));
hdr->src_port = byteorder_htons(src);
hdr->dst_port = byteorder_htons(dst);
hdr->checksum = byteorder_htons(0);
return res;
}

Loading…
Cancel
Save