Serenity Operating System
at portability 113 lines 3.5 kB view raw
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 <sys/socket.h> 33#include <sys/un.h> 34 35namespace Core { 36 37class SocketAddress { 38public: 39 enum class Type { 40 Invalid, 41 IPv4, 42 Local 43 }; 44 45 SocketAddress() {} 46 SocketAddress(const IPv4Address& address) 47 : m_type(Type::IPv4) 48 , m_ipv4_address(address) 49 { 50 } 51 52 SocketAddress(const IPv4Address& address, u16 port) 53 : m_type(Type::IPv4) 54 , m_ipv4_address(address) 55 , m_port(port) 56 { 57 } 58 59 static SocketAddress local(const String& address) 60 { 61 SocketAddress addr; 62 addr.m_type = Type::Local; 63 addr.m_local_address = address; 64 return addr; 65 } 66 67 Type type() const { return m_type; } 68 bool is_valid() const { return m_type != Type::Invalid; } 69 IPv4Address ipv4_address() const { return m_ipv4_address; } 70 u16 port() const { return m_port; } 71 72 String to_string() const 73 { 74 switch (m_type) { 75 case Type::IPv4: 76 return String::format("%s:%d", m_ipv4_address.to_string().characters(), m_port); 77 case Type::Local: 78 return m_local_address; 79 default: 80 return "[CSocketAddress]"; 81 } 82 } 83 84 sockaddr_un to_sockaddr_un() const 85 { 86 ASSERT(type() == Type::Local); 87 sockaddr_un address; 88 address.sun_family = AF_LOCAL; 89 RELEASE_ASSERT(m_local_address.length() < (int)sizeof(address.sun_path)); 90 strcpy(address.sun_path, m_local_address.characters()); 91 return address; 92 } 93 94 sockaddr_in to_sockaddr_in() const 95 { 96 ASSERT(type() == Type::IPv4); 97 sockaddr_in address; 98 address.sin_family = AF_INET; 99 address.sin_addr.s_addr = m_ipv4_address.to_in_addr_t(); 100 address.sin_port = htons(m_port); 101 return address; 102 } 103 104private: 105 Type m_type { Type::Invalid }; 106 IPv4Address m_ipv4_address; 107 u16 m_port { 0 }; 108 String m_local_address; 109}; 110 111const LogStream& operator<<(const LogStream&, const SocketAddress&); 112 113}