SocketAddress.h 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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 <string.h>
  31. #include <sys/socket.h>
  32. #include <sys/un.h>
  33. namespace Core {
  34. class SocketAddress {
  35. public:
  36. enum class Type {
  37. Invalid,
  38. IPv4,
  39. Local
  40. };
  41. SocketAddress() { }
  42. SocketAddress(const IPv4Address& address)
  43. : m_type(Type::IPv4)
  44. , m_ipv4_address(address)
  45. {
  46. }
  47. SocketAddress(const IPv4Address& address, u16 port)
  48. : m_type(Type::IPv4)
  49. , m_ipv4_address(address)
  50. , m_port(port)
  51. {
  52. }
  53. static SocketAddress local(const String& address)
  54. {
  55. SocketAddress addr;
  56. addr.m_type = Type::Local;
  57. addr.m_local_address = address;
  58. return addr;
  59. }
  60. Type type() const { return m_type; }
  61. bool is_valid() const { return m_type != Type::Invalid; }
  62. IPv4Address ipv4_address() const { return m_ipv4_address; }
  63. u16 port() const { return m_port; }
  64. String to_string() const
  65. {
  66. switch (m_type) {
  67. case Type::IPv4:
  68. return String::formatted("{}:{}", m_ipv4_address, m_port);
  69. case Type::Local:
  70. return m_local_address;
  71. default:
  72. return "[SocketAddress]";
  73. }
  74. }
  75. Optional<sockaddr_un> to_sockaddr_un() const
  76. {
  77. VERIFY(type() == Type::Local);
  78. sockaddr_un address;
  79. address.sun_family = AF_LOCAL;
  80. bool fits = m_local_address.copy_characters_to_buffer(address.sun_path, sizeof(address.sun_path));
  81. if (!fits)
  82. return {};
  83. return address;
  84. }
  85. sockaddr_in to_sockaddr_in() const
  86. {
  87. VERIFY(type() == Type::IPv4);
  88. sockaddr_in address {};
  89. address.sin_family = AF_INET;
  90. address.sin_addr.s_addr = m_ipv4_address.to_in_addr_t();
  91. address.sin_port = htons(m_port);
  92. return address;
  93. }
  94. private:
  95. Type m_type { Type::Invalid };
  96. IPv4Address m_ipv4_address;
  97. u16 m_port { 0 };
  98. String m_local_address;
  99. };
  100. }
  101. template<>
  102. struct AK::Formatter<Core::SocketAddress> : Formatter<String> {
  103. void format(FormatBuilder& builder, const Core::SocketAddress& value)
  104. {
  105. return Formatter<String>::format(builder, value.to_string());
  106. }
  107. };