2020-05-04 21:17:16 +00:00
|
|
|
#pragma once
|
|
|
|
|
2020-05-18 20:07:53 +00:00
|
|
|
#ifndef _WIN32
|
2020-05-04 21:36:08 +00:00
|
|
|
#include <netinet/in.h>
|
2020-05-18 20:07:53 +00:00
|
|
|
#include <arpa/inet.h>
|
|
|
|
#else
|
|
|
|
#include <winsock2.h>
|
|
|
|
#include <ws2tcpip.h>
|
|
|
|
#include <wspiapi.h>
|
|
|
|
extern "C" const char*
|
|
|
|
inet_ntop(int af, const void* src, char* dst, size_t size);
|
|
|
|
extern "C" int
|
|
|
|
inet_pton(int af, const char* src, void* dst);
|
|
|
|
#define inet_aton(x, y) inet_pton(AF_INET, x, y)
|
|
|
|
#endif
|
2020-05-04 21:36:08 +00:00
|
|
|
|
2020-05-04 21:17:16 +00:00
|
|
|
#include <string_view>
|
|
|
|
#include <string>
|
2021-03-09 22:24:35 +00:00
|
|
|
#include "net_int.hpp"
|
2020-05-04 21:17:16 +00:00
|
|
|
|
|
|
|
namespace llarp
|
|
|
|
{
|
2021-02-22 15:01:05 +00:00
|
|
|
struct AddressInfo;
|
|
|
|
|
2020-05-04 21:17:16 +00:00
|
|
|
/// A simple SockAddr wrapper which provides a sockaddr_in (IPv4). Memory management is handled
|
|
|
|
/// in constructor and destructor (if needed) and copying is disabled.
|
|
|
|
struct SockAddr
|
|
|
|
{
|
2020-05-06 20:38:44 +00:00
|
|
|
SockAddr();
|
2020-05-04 21:17:16 +00:00
|
|
|
SockAddr(uint8_t a, uint8_t b, uint8_t c, uint8_t d);
|
|
|
|
SockAddr(uint8_t a, uint8_t b, uint8_t c, uint8_t d, uint16_t port);
|
|
|
|
SockAddr(std::string_view addr);
|
2021-02-24 23:34:42 +00:00
|
|
|
SockAddr(std::string_view addr, uint16_t port);
|
2021-03-02 18:18:22 +00:00
|
|
|
SockAddr(uint32_t ip, uint16_t port);
|
2020-05-04 21:36:08 +00:00
|
|
|
|
2021-02-22 15:01:05 +00:00
|
|
|
SockAddr(const AddressInfo&);
|
|
|
|
|
2020-05-06 20:38:44 +00:00
|
|
|
SockAddr(const SockAddr&);
|
|
|
|
SockAddr&
|
2020-05-08 22:52:00 +00:00
|
|
|
operator=(const SockAddr&);
|
2020-05-06 20:38:44 +00:00
|
|
|
|
|
|
|
SockAddr(const sockaddr& addr);
|
2020-05-08 22:52:00 +00:00
|
|
|
SockAddr&
|
|
|
|
operator=(const sockaddr& addr);
|
|
|
|
|
2020-05-06 20:38:44 +00:00
|
|
|
SockAddr(const sockaddr_in& addr);
|
2020-05-08 22:52:00 +00:00
|
|
|
SockAddr&
|
|
|
|
operator=(const sockaddr_in& addr);
|
|
|
|
|
|
|
|
SockAddr(const sockaddr_in6& addr);
|
|
|
|
SockAddr&
|
|
|
|
operator=(const sockaddr_in6& addr);
|
|
|
|
|
2020-05-11 16:14:07 +00:00
|
|
|
SockAddr(const in6_addr& addr);
|
|
|
|
SockAddr&
|
|
|
|
operator=(const in6_addr& addr);
|
|
|
|
|
2020-05-04 21:36:08 +00:00
|
|
|
operator const sockaddr*() const;
|
2020-10-27 21:34:09 +00:00
|
|
|
operator const sockaddr_in*() const;
|
2020-05-11 15:11:44 +00:00
|
|
|
operator const sockaddr_in6*() const;
|
2020-05-06 20:38:44 +00:00
|
|
|
|
2020-05-11 17:55:36 +00:00
|
|
|
bool
|
|
|
|
operator<(const SockAddr& other) const;
|
|
|
|
|
|
|
|
bool
|
|
|
|
operator==(const SockAddr& other) const;
|
|
|
|
|
2020-05-08 17:23:21 +00:00
|
|
|
void
|
2021-02-24 23:34:42 +00:00
|
|
|
fromString(std::string_view str, bool allow_port = true);
|
2020-05-08 17:23:21 +00:00
|
|
|
|
|
|
|
std::string
|
|
|
|
toString() const;
|
|
|
|
|
|
|
|
/// Returns true if this is an empty SockAddr, defined by having no IP address set. An empty IP
|
|
|
|
/// address with a valid port is still considered empty.
|
|
|
|
///
|
|
|
|
/// @return true if this is empty, false otherwise
|
2020-05-06 20:38:44 +00:00
|
|
|
bool
|
|
|
|
isEmpty() const;
|
|
|
|
|
2020-05-08 17:23:21 +00:00
|
|
|
void
|
|
|
|
setIPv4(uint8_t a, uint8_t b, uint8_t c, uint8_t d);
|
|
|
|
|
2021-02-22 15:01:05 +00:00
|
|
|
/// port is in host order
|
2021-03-02 18:18:22 +00:00
|
|
|
void
|
|
|
|
setIPv4(uint32_t ip);
|
|
|
|
|
2020-05-08 17:23:21 +00:00
|
|
|
void
|
|
|
|
setPort(uint16_t port);
|
|
|
|
|
2021-02-22 15:01:05 +00:00
|
|
|
/// port is in host order
|
2020-05-08 17:23:21 +00:00
|
|
|
uint16_t
|
|
|
|
getPort() const;
|
|
|
|
|
2021-02-16 15:59:18 +00:00
|
|
|
huint128_t
|
|
|
|
asIPv6() const;
|
2021-03-02 18:18:22 +00:00
|
|
|
/// in network order
|
|
|
|
uint32_t
|
|
|
|
getIPv4() const;
|
2021-02-16 15:59:18 +00:00
|
|
|
|
2021-02-22 15:01:05 +00:00
|
|
|
huint32_t
|
|
|
|
asIPv4() const;
|
|
|
|
|
|
|
|
struct Hash
|
|
|
|
{
|
|
|
|
size_t
|
|
|
|
operator()(const SockAddr& addr) const noexcept
|
|
|
|
{
|
|
|
|
const std::hash<uint16_t> port{};
|
|
|
|
const std::hash<huint128_t> ip{};
|
|
|
|
return (port(addr.getPort()) << 3) ^ ip(addr.asIPv6());
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2020-05-06 20:38:44 +00:00
|
|
|
private:
|
2020-05-08 17:23:21 +00:00
|
|
|
bool m_empty = true;
|
|
|
|
sockaddr_in6 m_addr;
|
2020-10-27 21:34:09 +00:00
|
|
|
sockaddr_in m_addr4;
|
2020-05-08 17:23:21 +00:00
|
|
|
|
|
|
|
void
|
|
|
|
init();
|
2020-05-08 22:52:00 +00:00
|
|
|
|
|
|
|
void
|
2021-02-24 23:34:42 +00:00
|
|
|
applyIPv4MapBytes();
|
2020-05-04 21:17:16 +00:00
|
|
|
};
|
2020-05-06 20:38:44 +00:00
|
|
|
|
|
|
|
std::ostream&
|
|
|
|
operator<<(std::ostream& out, const SockAddr& address);
|
|
|
|
|
2020-05-04 21:17:16 +00:00
|
|
|
} // namespace llarp
|