blob: 4c4b55806cb38f057f5c870a433e665dd88fb882 [file] [log] [blame]
Hugo Benichi2ac4d072019-05-28 14:51:23 +09001// 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 Evans260ff302019-07-25 11:22:50 +09005#include <arpa/inet.h>
6#include <ifaddrs.h>
7#include <linux/in6.h>
Jason Jeremy Iman16f91722020-01-14 09:53:28 +09008#include <netinet/icmp6.h>
Garrick Evans260ff302019-07-25 11:22:50 +09009#include <netinet/in.h>
Jason Jeremy Iman16f91722020-01-14 09:53:28 +090010#include <netinet/ip.h>
11#include <netinet/ip6.h>
12#include <netinet/udp.h>
Hugo Benichi2ac4d072019-05-28 14:51:23 +090013#include <stdint.h>
Garrick Evans260ff302019-07-25 11:22:50 +090014#include <sys/types.h>
Hugo Benichi2ac4d072019-05-28 14:51:23 +090015
16#include <string>
17
Garrick Evans54861622019-07-19 09:05:09 +090018#include "arc/network/mac_address_generator.h"
19
Hugo Benichi2ac4d072019-05-28 14:51:23 +090020#ifndef ARC_NETWORK_NET_UTIL_H_
21#define ARC_NETWORK_NET_UTIL_H_
22
23namespace arc_networkd {
24
25// Reverses the byte order of the argument.
26constexpr 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.
31constexpr uint16_t Byteswap16(uint16_t x) {
32 return (x >> 8) | (x << 8);
33}
34
35// Constexpr version of ntohl().
36constexpr uint32_t Ntohl(uint32_t x) {
37 return Byteswap32(x);
38}
39
40// Constexpr version of htonl().
41constexpr uint32_t Htonl(uint32_t x) {
42 return Byteswap32(x);
43}
44
45// Constexpr version of ntohs().
46constexpr uint16_t Ntohs(uint16_t x) {
47 return Byteswap16(x);
48}
49
50// Constexpr version of htons().
51constexpr 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.
57constexpr 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
Garrick Evans6f4fa3a2020-02-10 16:15:09 +090061// Returns the netmask in network byte order given a prefixl length.
62uint32_t Ipv4Netmask(uint32_t prefix_len);
63
64// Returns the broadcast address in network byte order for the subnet provided.
65uint32_t Ipv4BroadcastAddr(uint32_t base, uint32_t prefix_len);
66
Hugo Benichi2ac4d072019-05-28 14:51:23 +090067// Returns the literal representation of the IPv4 address given in network byte
68// order.
69std::string IPv4AddressToString(uint32_t addr);
70
71// Returns the CIDR representation of an IPv4 address given in network byte
72// order.
73std::string IPv4AddressToCidrString(uint32_t addr, uint32_t prefix_length);
74
Garrick Evans54861622019-07-19 09:05:09 +090075// Returns a string representation of MAC address given.
76std::string MacAddressToString(const MacAddress& addr);
77
Garrick Evans260ff302019-07-25 11:22:50 +090078bool FindFirstIPv6Address(const std::string& ifname, struct in6_addr* address);
79
80bool GenerateRandomIPv6Prefix(struct in6_addr* prefix, int len);
81
82std::ostream& operator<<(std::ostream& stream, const struct in_addr& addr);
83std::ostream& operator<<(std::ostream& stream, const struct in6_addr& addr);
84
Jason Jeremy Iman16f91722020-01-14 09:53:28 +090085// Fold 32-bit into 16 bits.
86uint16_t FoldChecksum(uint32_t sum);
87
88// RFC 1071: We are doing calculation directly in network order.
89// Note this algorithm works regardless of the endianness of the host.
90uint32_t NetChecksum(const void* data, ssize_t len);
91
92uint16_t Ipv4Checksum(const iphdr* ip);
93
94// UDPv4 checksum along with IPv4 pseudo-header is defined in RFC 793,
95// Section 3.1.
96uint16_t Udpv4Checksum(const iphdr* ip, const udphdr* udp);
97
98// ICMPv6 checksum is defined in RFC 8200 Section 8.1
99uint16_t Icmpv6Checksum(const ip6_hdr* ip6, const icmp6_hdr* icmp6);
100
Hugo Benichi2ac4d072019-05-28 14:51:23 +0900101} // namespace arc_networkd
102
103#endif // ARC_NETWORK_NET_UTIL_H_