2020-01-18 09:38:21 +01:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions are met:
|
|
|
|
*
|
|
|
|
* 1. Redistributions of source code must retain the above copyright notice, this
|
|
|
|
* list of conditions and the following disclaimer.
|
|
|
|
*
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright notice,
|
|
|
|
* this list of conditions and the following disclaimer in the documentation
|
|
|
|
* and/or other materials provided with the distribution.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
|
|
|
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
|
|
|
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
|
|
|
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
|
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
|
|
|
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
|
|
|
|
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
|
|
|
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
|
|
|
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
|
2019-04-08 17:19:35 +02:00
|
|
|
#pragma once
|
|
|
|
|
2019-06-18 11:28:48 +02:00
|
|
|
#include <AK/IPv4Address.h>
|
2019-08-05 20:47:30 +10:00
|
|
|
#include <arpa/inet.h>
|
2020-01-01 20:46:26 -06:00
|
|
|
#include <netinet/in.h>
|
2020-03-08 12:05:14 +01:00
|
|
|
#include <string.h>
|
2019-07-26 22:39:16 +02:00
|
|
|
#include <sys/socket.h>
|
|
|
|
#include <sys/un.h>
|
2019-04-08 17:19:35 +02:00
|
|
|
|
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,
|
|
|
|
Local
|
|
|
|
};
|
2019-04-08 17:19:35 +02:00
|
|
|
|
2020-08-23 13:47:52 +02:00
|
|
|
SocketAddress() { }
|
2020-02-02 12:34:39 +01:00
|
|
|
SocketAddress(const IPv4Address& address)
|
2019-04-08 17:19:35 +02:00
|
|
|
: m_type(Type::IPv4)
|
|
|
|
, m_ipv4_address(address)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2020-02-02 12:34:39 +01:00
|
|
|
SocketAddress(const IPv4Address& address, u16 port)
|
2019-08-05 20:47:30 +10:00
|
|
|
: m_type(Type::IPv4)
|
|
|
|
, m_ipv4_address(address)
|
|
|
|
, m_port(port)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2020-02-02 12:34:39 +01:00
|
|
|
static SocketAddress local(const String& address)
|
2019-07-13 19:42:03 +02:00
|
|
|
{
|
2020-02-02 12:34:39 +01:00
|
|
|
SocketAddress addr;
|
2019-07-13 19:42:03 +02:00
|
|
|
addr.m_type = Type::Local;
|
|
|
|
addr.m_local_address = address;
|
|
|
|
return addr;
|
|
|
|
}
|
|
|
|
|
2019-04-08 17:19:35 +02:00
|
|
|
Type type() const { return m_type; }
|
|
|
|
bool is_valid() const { return m_type != Type::Invalid; }
|
|
|
|
IPv4Address ipv4_address() const { return m_ipv4_address; }
|
2019-08-05 20:47:30 +10:00
|
|
|
u16 port() const { return m_port; }
|
2019-04-08 17:19:35 +02:00
|
|
|
|
|
|
|
String to_string() const
|
|
|
|
{
|
|
|
|
switch (m_type) {
|
2019-05-28 11:53:16 +02:00
|
|
|
case Type::IPv4:
|
2019-08-05 20:47:30 +10:00
|
|
|
return String::format("%s:%d", m_ipv4_address.to_string().characters(), m_port);
|
2019-07-13 19:42:03 +02:00
|
|
|
case Type::Local:
|
|
|
|
return m_local_address;
|
2019-05-28 11:53:16 +02:00
|
|
|
default:
|
2020-03-07 11:37:51 +13:00
|
|
|
return "[SocketAddress]";
|
2019-04-08 17:19:35 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-23 13:47:52 +02:00
|
|
|
Optional<sockaddr_un> to_sockaddr_un() const
|
2019-07-26 22:39:16 +02:00
|
|
|
{
|
2021-02-23 20:42:32 +01:00
|
|
|
VERIFY(type() == Type::Local);
|
2019-07-26 22:39:16 +02:00
|
|
|
sockaddr_un address;
|
|
|
|
address.sun_family = AF_LOCAL;
|
2020-08-25 17:32:01 +03:00
|
|
|
bool fits = m_local_address.copy_characters_to_buffer(address.sun_path, sizeof(address.sun_path));
|
|
|
|
if (!fits)
|
2020-08-23 13:47:52 +02:00
|
|
|
return {};
|
2019-07-26 22:39:16 +02:00
|
|
|
return address;
|
|
|
|
}
|
|
|
|
|
2019-08-05 20:47:30 +10:00
|
|
|
sockaddr_in to_sockaddr_in() const
|
|
|
|
{
|
2021-02-23 20:42:32 +01:00
|
|
|
VERIFY(type() == Type::IPv4);
|
2020-08-16 18:29:00 -07:00
|
|
|
sockaddr_in address {};
|
2019-08-05 20:47:30 +10:00
|
|
|
address.sin_family = AF_INET;
|
|
|
|
address.sin_addr.s_addr = m_ipv4_address.to_in_addr_t();
|
|
|
|
address.sin_port = htons(m_port);
|
|
|
|
return address;
|
|
|
|
}
|
|
|
|
|
2019-04-08 17:19:35 +02:00
|
|
|
private:
|
|
|
|
Type m_type { Type::Invalid };
|
|
|
|
IPv4Address m_ipv4_address;
|
2019-08-06 22:58:32 +10:00
|
|
|
u16 m_port { 0 };
|
2019-07-13 19:42:03 +02:00
|
|
|
String 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<>
|
|
|
|
struct AK::Formatter<Core::SocketAddress> : Formatter<String> {
|
|
|
|
void format(FormatBuilder& builder, const Core::SocketAddress& value)
|
|
|
|
{
|
|
|
|
return Formatter<String>::format(builder, value.to_string());
|
|
|
|
}
|
|
|
|
};
|