diff options
author | Alex Xu (Hello71) <alex_y_xu@yahoo.ca> | 2016-07-06 20:35:16 -0400 |
---|---|---|
committer | Alex Xu (Hello71) <alex_y_xu@yahoo.ca> | 2016-07-06 20:35:16 -0400 |
commit | e6cec2f2553a4187a09d92f3a79c0cc0b5049b79 (patch) | |
tree | 24b2e9b9c53d2628d48857695cdd2b29d55c9e31 /src/checksum.c | |
parent | c68c67ce7d88dcfc9db29e572d60e1c43ddb2519 (diff) | |
download | udpastcp-e6cec2f2553a4187a09d92f3a79c0cc0b5049b79.tar.xz udpastcp-e6cec2f2553a4187a09d92f3a79c0cc0b5049b79.zip |
Add IPv4 support.
Diffstat (limited to 'src/checksum.c')
-rw-r--r-- | src/checksum.c | 16 |
1 files changed, 16 insertions, 0 deletions
diff --git a/src/checksum.c b/src/checksum.c index 5c6415a..53d4e5a 100644 --- a/src/checksum.c +++ b/src/checksum.c @@ -33,6 +33,7 @@ #include <assert.h> #include <endian.h> +#include <netinet/in.h> #include <stdint.h> #include "checksum.h" @@ -127,3 +128,18 @@ uint16_t csum_partial(const void *buff, int len, uint16_t wsum) result += 1; return result; } + +uint16_t csum_sockaddr_partial(const struct sockaddr *addr, int incl_port, uint16_t wsum) +{ + if (incl_port) + wsum = csum_partial(&((struct sockaddr_in *)addr)->sin_port, sizeof(in_port_t), wsum); + + switch (addr->sa_family) { + case AF_INET: + return csum_partial(&((struct sockaddr_in *)addr)->sin_addr, sizeof(struct in_addr), wsum); + case AF_INET6: + return csum_partial(&((struct sockaddr_in6 *)addr)->sin6_addr, sizeof(struct in6_addr), wsum); + default: + abort(); + } +} |