blob: 779d082b49168bbb66a332f7450bdf32631e8863 [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
61// Returns the literal representation of the IPv4 address given in network byte
62// order.
63std::string IPv4AddressToString(uint32_t addr);
64
65// Returns the CIDR representation of an IPv4 address given in network byte
66// order.
67std::string IPv4AddressToCidrString(uint32_t addr, uint32_t prefix_length);
68
Garrick Evans54861622019-07-19 09:05:09 +090069// Returns a string representation of MAC address given.
70std::string MacAddressToString(const MacAddress& addr);
71
Garrick Evans260ff302019-07-25 11:22:50 +090072bool FindFirstIPv6Address(const std::string& ifname, struct in6_addr* address);
73
74bool GenerateRandomIPv6Prefix(struct in6_addr* prefix, int len);
75
76std::ostream& operator<<(std::ostream& stream, const struct in_addr& addr);
77std::ostream& operator<<(std::ostream& stream, const struct in6_addr& addr);
78
Jason Jeremy Iman16f91722020-01-14 09:53:28 +090079// Fold 32-bit into 16 bits.
80uint16_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.
84uint32_t NetChecksum(const void* data, ssize_t len);
85
86uint16_t Ipv4Checksum(const iphdr* ip);
87
88// UDPv4 checksum along with IPv4 pseudo-header is defined in RFC 793,
89// Section 3.1.
90uint16_t Udpv4Checksum(const iphdr* ip, const udphdr* udp);
91
92// ICMPv6 checksum is defined in RFC 8200 Section 8.1
93uint16_t Icmpv6Checksum(const ip6_hdr* ip6, const icmp6_hdr* icmp6);
94
Hugo Benichi2ac4d072019-05-28 14:51:23 +090095} // namespace arc_networkd
96
97#endif // ARC_NETWORK_NET_UTIL_H_