[systemd-devel] [PATCH 1/4] libsystemd-network: Export checksum function to test case
Patrik Flykt
patrik.flykt at linux.intel.com
Thu Mar 13 04:01:49 PDT 2014
Remove identical checksum function implementation from the test
case code.
---
src/libsystemd-network/dhcp-internal.h | 2 ++
src/libsystemd-network/dhcp-packet.c | 10 +++++-----
src/libsystemd-network/test-dhcp-client.c | 30 +++---------------------------
3 files changed, 10 insertions(+), 32 deletions(-)
diff --git a/src/libsystemd-network/dhcp-internal.h b/src/libsystemd-network/dhcp-internal.h
index 3c3e1f6..064b13b 100644
--- a/src/libsystemd-network/dhcp-internal.h
+++ b/src/libsystemd-network/dhcp-internal.h
@@ -48,6 +48,8 @@ int dhcp_option_parse(DHCPMessage *message, size_t len,
int dhcp_message_init(DHCPMessage *message, uint8_t op, uint32_t xid, uint8_t type,
uint8_t **opt, size_t *optlen);
+uint16_t dhcp_packet_checksum(void *buf, int len);
+
void dhcp_packet_append_ip_headers(DHCPPacket *packet, be32_t source_addr,
uint16_t source, be32_t destination_addr,
uint16_t destination, uint16_t len);
diff --git a/src/libsystemd-network/dhcp-packet.c b/src/libsystemd-network/dhcp-packet.c
index 418a977..bed942f 100644
--- a/src/libsystemd-network/dhcp-packet.c
+++ b/src/libsystemd-network/dhcp-packet.c
@@ -69,7 +69,7 @@ int dhcp_message_init(DHCPMessage *message, uint8_t op, uint32_t xid,
return 0;
}
-static uint16_t dhcp_checksum(void *buf, int len) {
+uint16_t dhcp_packet_checksum(void *buf, int len) {
uint32_t sum;
uint16_t *check;
int i;
@@ -109,11 +109,11 @@ void dhcp_packet_append_ip_headers(DHCPPacket *packet, be32_t source_addr,
packet->udp.len = htobe16(len - DHCP_IP_SIZE);
packet->ip.check = packet->udp.len;
- packet->udp.check = dhcp_checksum(&packet->ip.ttl, len - 8);
+ packet->udp.check = dhcp_packet_checksum(&packet->ip.ttl, len - 8);
packet->ip.ttl = IPDEFTTL;
packet->ip.check = 0;
- packet->ip.check = dhcp_checksum(&packet->ip, DHCP_IP_SIZE);
+ packet->ip.check = dhcp_packet_checksum(&packet->ip, DHCP_IP_SIZE);
}
int dhcp_packet_verify_headers(DHCPPacket *packet, size_t len, bool checksum) {
@@ -150,7 +150,7 @@ int dhcp_packet_verify_headers(DHCPPacket *packet, size_t len, bool checksum) {
return -EINVAL;
}
- if (dhcp_checksum(&packet->ip, hdrlen)) {
+ if (dhcp_packet_checksum(&packet->ip, hdrlen)) {
log_dhcp_client(client, "ignoring packet: invalid IP checksum");
return -EINVAL;
}
@@ -175,7 +175,7 @@ int dhcp_packet_verify_headers(DHCPPacket *packet, size_t len, bool checksum) {
packet->ip.check = packet->udp.len;
packet->ip.ttl = 0;
- if (dhcp_checksum(&packet->ip.ttl,
+ if (dhcp_packet_checksum(&packet->ip.ttl,
be16toh(packet->udp.len) + 12)) {
log_dhcp_client(client, "ignoring packet: invalid UDP checksum");
return -EINVAL;
diff --git a/src/libsystemd-network/test-dhcp-client.c b/src/libsystemd-network/test-dhcp-client.c
index 8061e5f..cfc75ae 100644
--- a/src/libsystemd-network/test-dhcp-client.c
+++ b/src/libsystemd-network/test-dhcp-client.c
@@ -102,30 +102,6 @@ static void test_request_basic(sd_event *e)
assert_se(sd_dhcp_client_set_request_option(client, 33) == -EEXIST);
}
-static uint16_t client_checksum(void *buf, int len)
-{
- uint32_t sum;
- uint16_t *check;
- int i;
- uint8_t *odd;
-
- sum = 0;
- check = buf;
-
- for (i = 0; i < len / 2 ; i++)
- sum += check[i];
-
- if (len & 0x01) {
- odd = buf;
- sum += odd[len - 1];
- }
-
- while (sum >> 16)
- sum = (sum & 0xffff) + (sum >> 16);
-
- return ~sum;
-}
-
static void test_checksum(void)
{
uint8_t buf[20] = {
@@ -137,7 +113,7 @@ static void test_checksum(void)
if (verbose)
printf("* %s\n", __FUNCTION__);
- assert_se(client_checksum(&buf, 20) == be16toh(0x78ae));
+ assert_se(dhcp_packet_checksum(&buf, 20) == be16toh(0x78ae));
}
static int check_options(uint8_t code, uint8_t len, const uint8_t *option,
@@ -173,13 +149,13 @@ int dhcp_network_send_raw_socket(int s, const union sockaddr_union *link,
discover->ip.ttl = 0;
discover->ip.check = discover->udp.len;
- udp_check = ~client_checksum(&discover->ip.ttl, len - 8);
+ udp_check = ~dhcp_packet_checksum(&discover->ip.ttl, len - 8);
assert_se(udp_check == 0xffff);
discover->ip.ttl = IPDEFTTL;
discover->ip.check = ip_check;
- ip_check = ~client_checksum(&discover->ip, sizeof(discover->ip));
+ ip_check = ~dhcp_packet_checksum(&discover->ip, sizeof(discover->ip));
assert_se(ip_check == 0xffff);
assert_se(discover->dhcp.xid);
--
1.8.5.2
More information about the systemd-devel
mailing list