blob: 9e0c9aa2d409a54d3c66dbd78c39d3c9efecef5f [file] [log] [blame]
// Copyright 2019 The ChromiumOS Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef PATCHPANEL_NET_UTIL_H_
#define PATCHPANEL_NET_UTIL_H_
#include <ifaddrs.h>
#include <linux/if_packet.h>
#include <linux/in6.h>
#include <linux/vm_sockets.h>
#include <net/if.h>
#include <net/route.h>
#include <netinet/icmp6.h>
#include <netinet/in.h>
#include <netinet/ip.h>
#include <netinet/ip6.h>
#include <netinet/udp.h>
#include <stdint.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <sys/un.h>
#include <iostream>
#include <string>
#include <base/strings/stringprintf.h>
#include <base/sys_byteorder.h>
#include <brillo/brillo_export.h>
#include <chromeos/net-base/ipv4_address.h>
namespace patchpanel {
// Adds a positive offset of the IPv4Address.
net_base::IPv4Address AddOffset(const net_base::IPv4Address& addr,
uint32_t offset);
BRILLO_EXPORT void SetSockaddrIn(struct sockaddr* sockaddr,
const net_base::IPv4Address& addr);
BRILLO_EXPORT std::ostream& operator<<(std::ostream& stream,
const struct in_addr& addr);
BRILLO_EXPORT std::ostream& operator<<(std::ostream& stream,
const struct in6_addr& addr);
BRILLO_EXPORT std::ostream& operator<<(std::ostream& stream,
const struct sockaddr& addr);
BRILLO_EXPORT std::ostream& operator<<(std::ostream& stream,
const struct sockaddr_storage& addr);
BRILLO_EXPORT std::ostream& operator<<(std::ostream& stream,
const struct sockaddr_in& addr);
BRILLO_EXPORT std::ostream& operator<<(std::ostream& stream,
const struct sockaddr_in6& addr);
BRILLO_EXPORT std::ostream& operator<<(std::ostream& stream,
const struct sockaddr_un& addr);
BRILLO_EXPORT std::ostream& operator<<(std::ostream& stream,
const struct sockaddr_vm& addr);
BRILLO_EXPORT std::ostream& operator<<(std::ostream& stream,
const struct sockaddr_ll& addr);
BRILLO_EXPORT std::ostream& operator<<(std::ostream& stream,
const struct rtentry& route);
BRILLO_EXPORT std::ostream& operator<<(std::ostream& stream,
const struct in6_rtmsg& route);
// Fold 32-bit into 16 bits.
BRILLO_EXPORT uint16_t FoldChecksum(uint32_t sum);
// RFC 1071: We are doing calculation directly in network order.
// Note this algorithm works regardless of the endianness of the host.
BRILLO_EXPORT uint32_t NetChecksum(const void* data, size_t len);
BRILLO_EXPORT uint16_t Ipv4Checksum(const iphdr* ip);
// UDPv4 checksum along with IPv4 pseudo-header is defined in RFC 793,
// Section 3.1.
BRILLO_EXPORT uint16_t Udpv4Checksum(const uint8_t* udp_packet, size_t len);
// ICMPv6 checksum is defined in RFC 8200 Section 8.1
BRILLO_EXPORT uint16_t Icmpv6Checksum(const uint8_t* icmp6_packet, size_t len);
// Returns true if multicast forwarding should be enabled for this interface.
BRILLO_EXPORT bool IsMulticastInterface(const std::string& ifname);
// Populates interface request |ifr| with given |ifname|.
BRILLO_EXPORT void FillInterfaceRequest(std::string_view ifname,
struct ifreq* ifr);
} // namespace patchpanel
#endif // PATCHPANEL_NET_UTIL_H_