[systemd-devel] [PATCH 10/28] dhcp: Add test function for computing checksum

Zbigniew Jędrzejewski-Szmek zbyszek at in.waw.pl
Wed Nov 13 18:02:42 PST 2013


On Wed, Nov 13, 2013 at 11:22:38PM +0200, Patrik Flykt wrote:
> ---
>  src/dhcp/test-dhcp-client.c |   40 ++++++++++++++++++++++++++++++++++++++++
>  1 file changed, 40 insertions(+)
> 
> diff --git a/src/dhcp/test-dhcp-client.c b/src/dhcp/test-dhcp-client.c
> index 26857b6..94f576a 100644
> --- a/src/dhcp/test-dhcp-client.c
> +++ b/src/dhcp/test-dhcp-client.c
> @@ -22,6 +22,7 @@
>  #include <stdlib.h>
>  #include <assert.h>
>  #include <errno.h>
> +#include <stdio.h>
>  
>  #include "protocol.h"
>  #include "client.h"
> @@ -73,9 +74,48 @@ static void test_request_basic(void)
>          assert(dhcp_client_set_request_option(client, 44) == 0);
>  }
>  
> +static uint16_t client_checksum(void *buf, int len)
> +{
> +        uint32_t sum;
                       = 0;
> +        uint16_t *check;
                        = buf;
> +        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];
> +        }
> +
> +        return ~((sum & 0xffff) + (sum >> 16));
> +}
> +
> +static void test_checksum(void)
> +{
> +        uint8_t buf[20] = {
> +                0x45, 0x00, 0x02, 0x40, 0x00, 0x00, 0x00, 0x00,
> +                0x40, 0x11, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
> +                0xff, 0xff, 0xff, 0xff
> +        };
> +
> +        uint8_t check[2] = {
> +                0x78, 0xae
> +        };
> +
> +        uint16_t *val = (uint16_t *)check;
No need to cast here.

> +
> +        assert(client_checksum(&buf, 20) == *val);
> +}
> +
>  int main(int argc, char *argv[])
>  {
>          test_request_basic();
> +        test_checksum();
>  
>          return 0;
>  }

Zbyszek


More information about the systemd-devel mailing list