From: Roy Marples Date: Wed, 19 Nov 2008 15:01:20 +0000 (+0000) Subject: Zeroing each field is cheaper than using memset/memcpy. X-Git-Tag: v5.0.0~174 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=0b10229bc6ae7eadcc84a3c5e5710aac11c4f163;p=thirdparty%2Fdhcpcd.git Zeroing each field is cheaper than using memset/memcpy. --- diff --git a/net.c b/net.c index 291f40cc..45dab802 100644 --- a/net.c +++ b/net.c @@ -638,8 +638,6 @@ valid_udp_packet(const uint8_t *data) { struct udp_dhcp_packet packet; uint16_t bytes, udpsum; - struct in_addr dest, source; - int retval = 0; memcpy(&packet, data, sizeof(packet)); if (checksum(&packet.ip, sizeof(packet.ip)) != 0) { @@ -648,22 +646,21 @@ valid_udp_packet(const uint8_t *data) } bytes = ntohs(packet.ip.ip_len); - packet.ip.ip_sum = 0; - memcpy(&source, &packet.ip.ip_src, sizeof(packet.ip.ip_src)); - memcpy(&dest, &packet.ip.ip_dst, sizeof(packet.ip.ip_dst)); - memset(&packet.ip, 0, sizeof(packet.ip)); udpsum = packet.udp.uh_sum; packet.udp.uh_sum = 0; - - packet.ip.ip_p = IPPROTO_UDP; - memcpy(&packet.ip.ip_src, &source, sizeof(packet.ip.ip_src)); - memcpy(&packet.ip.ip_dst, &dest, sizeof(packet.ip.ip_dst)); + packet.ip.ip_hl = 0; + packet.ip.ip_v = 0; + packet.ip.ip_tos = 0; packet.ip.ip_len = packet.udp.uh_ulen; + packet.ip.ip_id = 0; + packet.ip.ip_off = 0; + packet.ip.ip_ttl = 0; + packet.ip.ip_sum = 0; if (udpsum && checksum(&packet, bytes) != udpsum) { errno = EINVAL; - retval = -1; + return -1; } - return retval; + return 0; }