SocketAddress.h 3.5 KB

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