Serenity Operating System
1/*
2 * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
7 *
8 * 1. Redistributions of source code must retain the above copyright notice, this
9 * list of conditions and the following disclaimer.
10 *
11 * 2. Redistributions in binary form must reproduce the above copyright notice,
12 * this list of conditions and the following disclaimer in the documentation
13 * and/or other materials provided with the distribution.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
16 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
18 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
21 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
22 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
23 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
24 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25 */
26
27#pragma once
28
29#include <AK/IPv4Address.h>
30#include <arpa/inet.h>
31#include <netinet/in.h>
32#include <string.h>
33#include <sys/socket.h>
34#include <sys/un.h>
35
36namespace Core {
37
38class SocketAddress {
39public:
40 enum class Type {
41 Invalid,
42 IPv4,
43 Local
44 };
45
46 SocketAddress() {}
47 SocketAddress(const IPv4Address& address)
48 : m_type(Type::IPv4)
49 , m_ipv4_address(address)
50 {
51 }
52
53 SocketAddress(const IPv4Address& address, u16 port)
54 : m_type(Type::IPv4)
55 , m_ipv4_address(address)
56 , m_port(port)
57 {
58 }
59
60 static SocketAddress local(const String& address)
61 {
62 SocketAddress addr;
63 addr.m_type = Type::Local;
64 addr.m_local_address = address;
65 return addr;
66 }
67
68 Type type() const { return m_type; }
69 bool is_valid() const { return m_type != Type::Invalid; }
70 IPv4Address ipv4_address() const { return m_ipv4_address; }
71 u16 port() const { return m_port; }
72
73 String to_string() const
74 {
75 switch (m_type) {
76 case Type::IPv4:
77 return String::format("%s:%d", m_ipv4_address.to_string().characters(), m_port);
78 case Type::Local:
79 return m_local_address;
80 default:
81 return "[SocketAddress]";
82 }
83 }
84
85 sockaddr_un to_sockaddr_un() const
86 {
87 ASSERT(type() == Type::Local);
88 sockaddr_un address;
89 address.sun_family = AF_LOCAL;
90 RELEASE_ASSERT(m_local_address.length() < (int)sizeof(address.sun_path));
91 strcpy(address.sun_path, m_local_address.characters());
92 return address;
93 }
94
95 sockaddr_in to_sockaddr_in() const
96 {
97 ASSERT(type() == Type::IPv4);
98 sockaddr_in address;
99 address.sin_family = AF_INET;
100 address.sin_addr.s_addr = m_ipv4_address.to_in_addr_t();
101 address.sin_port = htons(m_port);
102 return address;
103 }
104
105private:
106 Type m_type { Type::Invalid };
107 IPv4Address m_ipv4_address;
108 u16 m_port { 0 };
109 String m_local_address;
110};
111
112const LogStream& operator<<(const LogStream&, const SocketAddress&);
113
114}