Hugo Benichi | 2ac4d07 | 2019-05-28 14:51:23 +0900 | [diff] [blame] | 1 | // Copyright 2019 The Chromium OS Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
Garrick Evans | 260ff30 | 2019-07-25 11:22:50 +0900 | [diff] [blame] | 5 | #include <arpa/inet.h> |
| 6 | #include <ifaddrs.h> |
| 7 | #include <linux/in6.h> |
Jason Jeremy Iman | 16f9172 | 2020-01-14 09:53:28 +0900 | [diff] [blame] | 8 | #include <netinet/icmp6.h> |
Garrick Evans | 260ff30 | 2019-07-25 11:22:50 +0900 | [diff] [blame] | 9 | #include <netinet/in.h> |
Jason Jeremy Iman | 16f9172 | 2020-01-14 09:53:28 +0900 | [diff] [blame] | 10 | #include <netinet/ip.h> |
| 11 | #include <netinet/ip6.h> |
| 12 | #include <netinet/udp.h> |
Hugo Benichi | 2ac4d07 | 2019-05-28 14:51:23 +0900 | [diff] [blame] | 13 | #include <stdint.h> |
Garrick Evans | 260ff30 | 2019-07-25 11:22:50 +0900 | [diff] [blame] | 14 | #include <sys/types.h> |
Hugo Benichi | 2ac4d07 | 2019-05-28 14:51:23 +0900 | [diff] [blame] | 15 | |
| 16 | #include <string> |
| 17 | |
Garrick Evans | 5486162 | 2019-07-19 09:05:09 +0900 | [diff] [blame] | 18 | #include "arc/network/mac_address_generator.h" |
| 19 | |
Hugo Benichi | 2ac4d07 | 2019-05-28 14:51:23 +0900 | [diff] [blame] | 20 | #ifndef ARC_NETWORK_NET_UTIL_H_ |
| 21 | #define ARC_NETWORK_NET_UTIL_H_ |
| 22 | |
| 23 | namespace arc_networkd { |
| 24 | |
| 25 | // Reverses the byte order of the argument. |
| 26 | constexpr uint32_t Byteswap32(uint32_t x) { |
| 27 | return (x >> 24) | (x << 24) | ((x >> 8) & 0xff00) | ((x << 8) & 0xff0000); |
| 28 | } |
| 29 | |
| 30 | // Reverses the byte order of the argument. |
| 31 | constexpr uint16_t Byteswap16(uint16_t x) { |
| 32 | return (x >> 8) | (x << 8); |
| 33 | } |
| 34 | |
| 35 | // Constexpr version of ntohl(). |
| 36 | constexpr uint32_t Ntohl(uint32_t x) { |
| 37 | return Byteswap32(x); |
| 38 | } |
| 39 | |
| 40 | // Constexpr version of htonl(). |
| 41 | constexpr uint32_t Htonl(uint32_t x) { |
| 42 | return Byteswap32(x); |
| 43 | } |
| 44 | |
| 45 | // Constexpr version of ntohs(). |
| 46 | constexpr uint16_t Ntohs(uint16_t x) { |
| 47 | return Byteswap16(x); |
| 48 | } |
| 49 | |
| 50 | // Constexpr version of htons(). |
| 51 | constexpr uint16_t Htons(uint16_t x) { |
| 52 | return Byteswap16(x); |
| 53 | } |
| 54 | |
| 55 | // Returns the network-byte order int32 representation of the IPv4 address given |
| 56 | // byte per byte, most significant bytes first. |
| 57 | constexpr uint32_t Ipv4Addr(uint8_t b0, uint8_t b1, uint8_t b2, uint8_t b3) { |
| 58 | return (b3 << 24) | (b2 << 16) | (b1 << 8) | b0; |
| 59 | } |
| 60 | |
| 61 | // Returns the literal representation of the IPv4 address given in network byte |
| 62 | // order. |
| 63 | std::string IPv4AddressToString(uint32_t addr); |
| 64 | |
| 65 | // Returns the CIDR representation of an IPv4 address given in network byte |
| 66 | // order. |
| 67 | std::string IPv4AddressToCidrString(uint32_t addr, uint32_t prefix_length); |
| 68 | |
Garrick Evans | 5486162 | 2019-07-19 09:05:09 +0900 | [diff] [blame] | 69 | // Returns a string representation of MAC address given. |
| 70 | std::string MacAddressToString(const MacAddress& addr); |
| 71 | |
Garrick Evans | 260ff30 | 2019-07-25 11:22:50 +0900 | [diff] [blame] | 72 | bool FindFirstIPv6Address(const std::string& ifname, struct in6_addr* address); |
| 73 | |
| 74 | bool GenerateRandomIPv6Prefix(struct in6_addr* prefix, int len); |
| 75 | |
| 76 | std::ostream& operator<<(std::ostream& stream, const struct in_addr& addr); |
| 77 | std::ostream& operator<<(std::ostream& stream, const struct in6_addr& addr); |
| 78 | |
Jason Jeremy Iman | 16f9172 | 2020-01-14 09:53:28 +0900 | [diff] [blame] | 79 | // Fold 32-bit into 16 bits. |
| 80 | uint16_t FoldChecksum(uint32_t sum); |
| 81 | |
| 82 | // RFC 1071: We are doing calculation directly in network order. |
| 83 | // Note this algorithm works regardless of the endianness of the host. |
| 84 | uint32_t NetChecksum(const void* data, ssize_t len); |
| 85 | |
| 86 | uint16_t Ipv4Checksum(const iphdr* ip); |
| 87 | |
| 88 | // UDPv4 checksum along with IPv4 pseudo-header is defined in RFC 793, |
| 89 | // Section 3.1. |
| 90 | uint16_t Udpv4Checksum(const iphdr* ip, const udphdr* udp); |
| 91 | |
| 92 | // ICMPv6 checksum is defined in RFC 8200 Section 8.1 |
| 93 | uint16_t Icmpv6Checksum(const ip6_hdr* ip6, const icmp6_hdr* icmp6); |
| 94 | |
Hugo Benichi | 2ac4d07 | 2019-05-28 14:51:23 +0900 | [diff] [blame] | 95 | } // namespace arc_networkd |
| 96 | |
| 97 | #endif // ARC_NETWORK_NET_UTIL_H_ |