2020-01-18 09:38:21 +01:00
|
|
|
/*
|
2024-10-04 13:19:50 +02:00
|
|
|
* Copyright (c) 2018-2020, Andreas Kling <andreas@ladybird.org>
|
2022-02-26 09:09:45 -07:00
|
|
|
* Copyright (c) 2022, the SerenityOS developers.
|
2020-01-18 09:38:21 +01:00
|
|
|
*
|
2021-04-22 01:24:48 -07:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-01-18 09:38:21 +01:00
|
|
|
*/
|
|
|
|
|
|
2019-04-08 17:19:35 +02:00
|
|
|
#pragma once
|
|
|
|
|
|
2019-06-18 11:28:48 +02:00
|
|
|
#include <AK/IPv4Address.h>
|
2024-06-30 21:51:58 +03:00
|
|
|
#include <AK/IPv6Address.h>
|
2024-11-29 22:13:25 +05:00
|
|
|
|
2025-11-30 15:13:45 -05:00
|
|
|
#if defined(AK_OS_WINDOWS)
|
|
|
|
|
constexpr inline int SOCK_STREAM = 1;
|
|
|
|
|
constexpr inline int SOCK_DGRAM = 2;
|
|
|
|
|
|
|
|
|
|
using ADDRESS_FAMILY = unsigned short;
|
|
|
|
|
constexpr inline ADDRESS_FAMILY AF_LOCAL = 1;
|
|
|
|
|
|
|
|
|
|
# include <afunix.h>
|
|
|
|
|
#else
|
2024-11-29 22:13:25 +05:00
|
|
|
# include <arpa/inet.h>
|
|
|
|
|
# include <netinet/in.h>
|
|
|
|
|
# include <sys/socket.h>
|
|
|
|
|
# include <sys/un.h>
|
|
|
|
|
#endif
|
2019-04-08 17:19:35 +02:00
|
|
|
|
2025-11-30 15:13:45 -05:00
|
|
|
struct sockaddr_in;
|
|
|
|
|
struct sockaddr_in6;
|
|
|
|
|
struct sockaddr_un;
|
|
|
|
|
|
2020-02-02 12:34:39 +01:00
|
|
|
namespace Core {
|
|
|
|
|
|
|
|
|
|
class SocketAddress {
|
2019-04-08 17:19:35 +02:00
|
|
|
public:
|
2019-06-07 17:13:23 +02:00
|
|
|
enum class Type {
|
2019-05-28 11:53:16 +02:00
|
|
|
Invalid,
|
|
|
|
|
IPv4,
|
2024-06-30 21:51:58 +03:00
|
|
|
IPv6,
|
2019-05-28 11:53:16 +02:00
|
|
|
Local
|
|
|
|
|
};
|
2019-04-08 17:19:35 +02:00
|
|
|
|
2025-11-30 15:13:45 -05:00
|
|
|
SocketAddress();
|
|
|
|
|
SocketAddress(IPv4Address const& address);
|
|
|
|
|
SocketAddress(IPv6Address const& address);
|
|
|
|
|
SocketAddress(IPv4Address const& address, u16 port);
|
|
|
|
|
SocketAddress(IPv6Address const& address, u16 port);
|
2019-08-05 20:47:30 +10:00
|
|
|
|
2025-11-30 15:13:45 -05:00
|
|
|
static SocketAddress local(ByteString const& address);
|
2019-07-13 19:42:03 +02:00
|
|
|
|
2019-04-08 17:19:35 +02:00
|
|
|
Type type() const { return m_type; }
|
|
|
|
|
bool is_valid() const { return m_type != Type::Invalid; }
|
2024-06-30 21:51:58 +03:00
|
|
|
|
|
|
|
|
IPv4Address ipv4_address() const { return m_ip_address.get<IPv4Address>(); }
|
|
|
|
|
IPv6Address ipv6_address() const { return m_ip_address.get<IPv6Address>(); }
|
2019-08-05 20:47:30 +10:00
|
|
|
u16 port() const { return m_port; }
|
2019-04-08 17:19:35 +02:00
|
|
|
|
2025-11-30 15:13:45 -05:00
|
|
|
ByteString to_byte_string() const;
|
2019-07-26 22:39:16 +02:00
|
|
|
|
2025-11-30 15:13:45 -05:00
|
|
|
Optional<sockaddr_un> to_sockaddr_un() const;
|
|
|
|
|
sockaddr_in6 to_sockaddr_in6() const;
|
|
|
|
|
sockaddr_in to_sockaddr_in() const;
|
2019-08-05 20:47:30 +10:00
|
|
|
|
2023-07-13 15:30:13 -04:00
|
|
|
bool operator==(SocketAddress const& other) const = default;
|
|
|
|
|
bool operator!=(SocketAddress const& other) const = default;
|
|
|
|
|
|
2019-04-08 17:19:35 +02:00
|
|
|
private:
|
|
|
|
|
Type m_type { Type::Invalid };
|
2024-06-30 21:51:58 +03:00
|
|
|
|
|
|
|
|
Variant<IPv4Address, IPv6Address> m_ip_address = IPv4Address();
|
|
|
|
|
|
2019-08-06 22:58:32 +10:00
|
|
|
u16 m_port { 0 };
|
2023-12-16 17:49:34 +03:30
|
|
|
ByteString m_local_address;
|
2019-04-08 17:19:35 +02:00
|
|
|
};
|
2019-07-14 11:02:40 +02:00
|
|
|
|
2020-02-02 12:34:39 +01:00
|
|
|
}
|
2021-01-15 21:46:23 +01:00
|
|
|
|
|
|
|
|
template<>
|
2023-12-16 17:49:34 +03:30
|
|
|
struct AK::Formatter<Core::SocketAddress> : Formatter<ByteString> {
|
2021-11-16 01:15:21 +01:00
|
|
|
ErrorOr<void> format(FormatBuilder& builder, Core::SocketAddress const& value)
|
2021-01-15 21:46:23 +01:00
|
|
|
{
|
2023-12-16 17:49:34 +03:30
|
|
|
return Formatter<ByteString>::format(builder, value.to_byte_string());
|
2021-01-15 21:46:23 +01:00
|
|
|
}
|
|
|
|
|
};
|