blob: 7f8022d9e1ee5095d226e578f605fcd93fd4888b [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>
Hugo Benichidcc32392020-02-27 09:14:40 +09008#include <linux/vm_sockets.h>
Jason Jeremy Iman16f91722020-01-14 09:53:28 +09009#include <netinet/icmp6.h>
Garrick Evans260ff302019-07-25 11:22:50 +090010#include <netinet/in.h>
Jason Jeremy Iman16f91722020-01-14 09:53:28 +090011#include <netinet/ip.h>
12#include <netinet/ip6.h>
13#include <netinet/udp.h>
Hugo Benichi2ac4d072019-05-28 14:51:23 +090014#include <stdint.h>
Hugo Benichidcc32392020-02-27 09:14:40 +090015#include <sys/socket.h>
Garrick Evans260ff302019-07-25 11:22:50 +090016#include <sys/types.h>
Hugo Benichidcc32392020-02-27 09:14:40 +090017#include <sys/un.h>
Hugo Benichi2ac4d072019-05-28 14:51:23 +090018
19#include <string>
20
Andreea Costinas29080492020-03-23 09:22:09 +010021#include <brillo/brillo_export.h>
22
Garrick Evans54861622019-07-19 09:05:09 +090023#include "arc/network/mac_address_generator.h"
24
Hugo Benichi2ac4d072019-05-28 14:51:23 +090025#ifndef ARC_NETWORK_NET_UTIL_H_
26#define ARC_NETWORK_NET_UTIL_H_
27
28namespace arc_networkd {
29
30// Reverses the byte order of the argument.
Andreea Costinas29080492020-03-23 09:22:09 +010031BRILLO_EXPORT constexpr uint32_t Byteswap32(uint32_t x) {
Hugo Benichi2ac4d072019-05-28 14:51:23 +090032 return (x >> 24) | (x << 24) | ((x >> 8) & 0xff00) | ((x << 8) & 0xff0000);
33}
34
35// Reverses the byte order of the argument.
Andreea Costinas29080492020-03-23 09:22:09 +010036BRILLO_EXPORT constexpr uint16_t Byteswap16(uint16_t x) {
Hugo Benichi2ac4d072019-05-28 14:51:23 +090037 return (x >> 8) | (x << 8);
38}
39
40// Constexpr version of ntohl().
Andreea Costinas29080492020-03-23 09:22:09 +010041BRILLO_EXPORT constexpr uint32_t Ntohl(uint32_t x) {
Hugo Benichi2ac4d072019-05-28 14:51:23 +090042 return Byteswap32(x);
43}
44
45// Constexpr version of htonl().
Andreea Costinas29080492020-03-23 09:22:09 +010046BRILLO_EXPORT constexpr uint32_t Htonl(uint32_t x) {
Hugo Benichi2ac4d072019-05-28 14:51:23 +090047 return Byteswap32(x);
48}
49
50// Constexpr version of ntohs().
Andreea Costinas29080492020-03-23 09:22:09 +010051BRILLO_EXPORT constexpr uint16_t Ntohs(uint16_t x) {
Hugo Benichi2ac4d072019-05-28 14:51:23 +090052 return Byteswap16(x);
53}
54
55// Constexpr version of htons().
Andreea Costinas29080492020-03-23 09:22:09 +010056BRILLO_EXPORT constexpr uint16_t Htons(uint16_t x) {
Hugo Benichi2ac4d072019-05-28 14:51:23 +090057 return Byteswap16(x);
58}
59
60// Returns the network-byte order int32 representation of the IPv4 address given
61// byte per byte, most significant bytes first.
Andreea Costinas29080492020-03-23 09:22:09 +010062BRILLO_EXPORT constexpr uint32_t Ipv4Addr(uint8_t b0,
63 uint8_t b1,
64 uint8_t b2,
65 uint8_t b3) {
Hugo Benichi2ac4d072019-05-28 14:51:23 +090066 return (b3 << 24) | (b2 << 16) | (b1 << 8) | b0;
67}
68
Garrick Evans6f4fa3a2020-02-10 16:15:09 +090069// Returns the netmask in network byte order given a prefixl length.
Andreea Costinas29080492020-03-23 09:22:09 +010070BRILLO_EXPORT uint32_t Ipv4Netmask(uint32_t prefix_len);
Garrick Evans6f4fa3a2020-02-10 16:15:09 +090071
72// Returns the broadcast address in network byte order for the subnet provided.
Andreea Costinas29080492020-03-23 09:22:09 +010073BRILLO_EXPORT uint32_t Ipv4BroadcastAddr(uint32_t base, uint32_t prefix_len);
Garrick Evans6f4fa3a2020-02-10 16:15:09 +090074
Hugo Benichi2ac4d072019-05-28 14:51:23 +090075// Returns the literal representation of the IPv4 address given in network byte
76// order.
Andreea Costinas29080492020-03-23 09:22:09 +010077BRILLO_EXPORT std::string IPv4AddressToString(uint32_t addr);
Hugo Benichi2ac4d072019-05-28 14:51:23 +090078
79// Returns the CIDR representation of an IPv4 address given in network byte
80// order.
Andreea Costinas29080492020-03-23 09:22:09 +010081BRILLO_EXPORT std::string IPv4AddressToCidrString(uint32_t addr,
82 uint32_t prefix_length);
Hugo Benichi2ac4d072019-05-28 14:51:23 +090083
Garrick Evans54861622019-07-19 09:05:09 +090084// Returns a string representation of MAC address given.
Andreea Costinas29080492020-03-23 09:22:09 +010085BRILLO_EXPORT std::string MacAddressToString(const MacAddress& addr);
Garrick Evans54861622019-07-19 09:05:09 +090086
Andreea Costinas29080492020-03-23 09:22:09 +010087BRILLO_EXPORT bool FindFirstIPv6Address(const std::string& ifname,
88 struct in6_addr* address);
Garrick Evans260ff302019-07-25 11:22:50 +090089
Andreea Costinas29080492020-03-23 09:22:09 +010090BRILLO_EXPORT bool GenerateRandomIPv6Prefix(struct in6_addr* prefix, int len);
Garrick Evans260ff302019-07-25 11:22:50 +090091
Andreea Costinas29080492020-03-23 09:22:09 +010092BRILLO_EXPORT std::ostream& operator<<(std::ostream& stream,
93 const struct in_addr& addr);
94BRILLO_EXPORT std::ostream& operator<<(std::ostream& stream,
95 const struct in6_addr& addr);
96BRILLO_EXPORT std::ostream& operator<<(std::ostream& stream,
97 const struct sockaddr& addr);
98BRILLO_EXPORT std::ostream& operator<<(std::ostream& stream,
99 const struct sockaddr_storage& addr);
100BRILLO_EXPORT std::ostream& operator<<(std::ostream& stream,
101 const struct sockaddr_in& addr);
102BRILLO_EXPORT std::ostream& operator<<(std::ostream& stream,
103 const struct sockaddr_in6& addr);
104BRILLO_EXPORT std::ostream& operator<<(std::ostream& stream,
105 const struct sockaddr_un& addr);
106BRILLO_EXPORT std::ostream& operator<<(std::ostream& stream,
107 const struct sockaddr_vm& addr);
Garrick Evans260ff302019-07-25 11:22:50 +0900108
Jason Jeremy Iman16f91722020-01-14 09:53:28 +0900109// Fold 32-bit into 16 bits.
Andreea Costinas29080492020-03-23 09:22:09 +0100110BRILLO_EXPORT uint16_t FoldChecksum(uint32_t sum);
Jason Jeremy Iman16f91722020-01-14 09:53:28 +0900111
112// RFC 1071: We are doing calculation directly in network order.
113// Note this algorithm works regardless of the endianness of the host.
Andreea Costinas29080492020-03-23 09:22:09 +0100114BRILLO_EXPORT uint32_t NetChecksum(const void* data, ssize_t len);
Jason Jeremy Iman16f91722020-01-14 09:53:28 +0900115
Andreea Costinas29080492020-03-23 09:22:09 +0100116BRILLO_EXPORT uint16_t Ipv4Checksum(const iphdr* ip);
Jason Jeremy Iman16f91722020-01-14 09:53:28 +0900117
118// UDPv4 checksum along with IPv4 pseudo-header is defined in RFC 793,
119// Section 3.1.
Andreea Costinas29080492020-03-23 09:22:09 +0100120BRILLO_EXPORT uint16_t Udpv4Checksum(const iphdr* ip, const udphdr* udp);
Jason Jeremy Iman16f91722020-01-14 09:53:28 +0900121
122// ICMPv6 checksum is defined in RFC 8200 Section 8.1
Andreea Costinas29080492020-03-23 09:22:09 +0100123BRILLO_EXPORT uint16_t Icmpv6Checksum(const ip6_hdr* ip6,
124 const icmp6_hdr* icmp6);
Jason Jeremy Iman16f91722020-01-14 09:53:28 +0900125
Hugo Benichi2ac4d072019-05-28 14:51:23 +0900126} // namespace arc_networkd
127
128#endif // ARC_NETWORK_NET_UTIL_H_