diff --git a/src/dhcp.c b/src/dhcp.c index c5e824c..5755ad6 100644 --- a/src/dhcp.c +++ b/src/dhcp.c @@ -40,7 +40,7 @@ #include "nk/log.h" #include "nk/io.h" #include "nk/random.h" -#include "nk/net_checksum.h" +#include "nk/net_checksum16.h" #include "dhcp.h" #include "state.h" @@ -133,7 +133,7 @@ static ssize_t send_dhcp_unicast(struct client_state_t cs[static 1], // Returns 1 if IP checksum is correct, otherwise 0. static int ip_checksum(struct ip_udp_dhcp_packet packet[static 1]) { - return net_checksum161c(&packet->ip, sizeof packet->ip) == 0; + return net_checksum16(&packet->ip, sizeof packet->ip) == 0; } // Returns 1 if UDP checksum is correct, otherwise 0. @@ -146,11 +146,11 @@ static int udp_checksum(struct ip_udp_dhcp_packet packet[static 1]) .tot_len = packet->udp.len, }; uint16_t udpcs = - net_checksum161c(&packet->udp, + net_checksum16(&packet->udp, min_size_t(ntohs(packet->udp.len), sizeof *packet - sizeof(struct iphdr))); - uint16_t hdrcs = net_checksum161c(&ph, sizeof ph); - uint16_t cs = net_checksum161c_add(udpcs, hdrcs); + uint16_t hdrcs = net_checksum16(&ph, sizeof ph); + uint16_t cs = net_checksum16_add(udpcs, hdrcs); return cs == 0; } @@ -285,10 +285,10 @@ static ssize_t send_dhcp_raw(struct dhcpmsg payload[static 1]) iudmsg.udp.dest = htons(DHCP_SERVER_PORT); iudmsg.udp.len = htons(ud_len); iudmsg.udp.check = 0; - uint16_t udpcs = net_checksum161c(&iudmsg.udp, ud_len); - uint16_t phcs = net_checksum161c(&ph, sizeof ph); - iudmsg.udp.check = net_checksum161c_add(udpcs, phcs); - iudmsg.ip.check = net_checksum161c(&iudmsg.ip, sizeof iudmsg.ip); + uint16_t udpcs = net_checksum16(&iudmsg.udp, ud_len); + uint16_t phcs = net_checksum16(&ph, sizeof ph); + iudmsg.udp.check = net_checksum16_add(udpcs, phcs); + iudmsg.ip.check = net_checksum16(&iudmsg.ip, sizeof iudmsg.ip); struct sockaddr_ll da = { .sll_family = AF_PACKET, diff --git a/src/nk/net_checksum.h b/src/nk/net_checksum16.h similarity index 77% rename from src/nk/net_checksum.h rename to src/nk/net_checksum16.h index e983ef6..bf94abe 100644 --- a/src/nk/net_checksum.h +++ b/src/nk/net_checksum16.h @@ -1,5 +1,5 @@ -#ifndef NCMLIB_NET_CHECKSUM_H -#define NCMLIB_NET_CHECKSUM_H +#ifndef NCMLIB_NET_CHECKSUM16_H +#define NCMLIB_NET_CHECKSUM16_H // RFC 1071 is still a good reference. @@ -9,7 +9,7 @@ // representation, fold the carry bits that have spilled into the upper // 16-bits of the 32-bit unsigned value back into the 16-bit ones-complement // binary value. -static inline uint16_t net_checksum161c_foldcarry(uint32_t v) +static inline uint16_t net_checksum16_foldcarry(uint32_t v) { v = (v >> 16) + (v & 0xffff); v += v >> 16; @@ -20,7 +20,7 @@ static inline uint16_t net_checksum161c_foldcarry(uint32_t v) // the binary value returned, when stored to memory, will match // the result on big endian; if the numeric value returned // must match big endian results, then call ntohs() on the result. -static uint16_t net_checksum161c(const void *buf, size_t size) +static uint16_t net_checksum16(const void *buf, size_t size) { const char *b = (const char *)buf; const char *bend = b + size; @@ -37,17 +37,17 @@ static uint16_t net_checksum161c(const void *buf, size_t size) memcpy(&t, b, 2); sum += t; } - return ~net_checksum161c_foldcarry(sum + sumo); + return ~net_checksum16_foldcarry(sum + sumo); } // For two sequences of bytes A and B that return checksums CS(A) and CS(B), // this function will calculate the checksum CS(AB) of the concatenated value // AB given the checksums of the individual parts CS(A) and CS(B). -static inline uint16_t net_checksum161c_add(uint16_t a, uint16_t b) +static inline uint16_t net_checksum16_add(uint16_t a, uint16_t b) { const uint32_t A = a; const uint32_t B = b; - return ~net_checksum161c_foldcarry((~A & 0xffffu) + (~B & 0xffffu)); + return ~net_checksum16_foldcarry((~A & 0xffffu) + (~B & 0xffffu)); } #endif