1 /* 2 * Copyright 2010, Haiku, Inc. All Rights Reserved. 3 * Distributed under the terms of the MIT License. 4 * 5 * Authors: 6 * Atis Elsts, the.kfx@gmail.com 7 */ 8 #ifndef IPV6_UTILS_H 9 #define IPV6_UTILS_H 10 11 12 #include <netinet6/in6.h> 13 #include <netinet/in.h> 14 #include <net_stack.h> 15 16 17 const char *ip6_sprintf(const in6_addr *addr, char *dst, 18 size_t size = INET6_ADDRSTRLEN); 19 20 21 static inline uint16 22 compute_checksum(uint8* _buffer, size_t length) 23 { 24 uint16* buffer = (uint16*)_buffer; 25 uint32 sum = 0; 26 27 while (length >= 2) { 28 sum += *buffer++; 29 length -= 2; 30 } 31 32 return sum; 33 } 34 35 36 static inline uint16 37 ipv6_checksum(const struct in6_addr* source, 38 const struct in6_addr* destination, 39 uint16 length, uint16 protocol, 40 uint16 checksum) 41 { 42 uint32 sum = checksum; 43 44 length = htons(length); 45 protocol = htons(protocol); 46 47 sum += compute_checksum((uint8*)source, sizeof(in6_addr)); 48 sum += compute_checksum((uint8*)destination, sizeof(in6_addr)); 49 sum += compute_checksum((uint8*)&length, sizeof(uint16)); 50 sum += compute_checksum((uint8*)&protocol, sizeof(uint16)); 51 52 while (sum >> 16) 53 sum = (sum & 0xffff) + (sum >> 16); 54 55 return ~(uint16)sum; 56 } 57 58 59 #endif // IPV6_UTILS_H 60