Socket.cpp 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  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. #include <AK/StringBuilder.h>
  27. #include <Kernel/FileSystem/FileDescription.h>
  28. #include <Kernel/Net/IPv4Socket.h>
  29. #include <Kernel/Net/LocalSocket.h>
  30. #include <Kernel/Net/Socket.h>
  31. #include <Kernel/Process.h>
  32. #include <Kernel/UnixTypes.h>
  33. #include <LibC/errno_numbers.h>
  34. //#define SOCKET_DEBUG
  35. namespace Kernel {
  36. KResultOr<NonnullRefPtr<Socket>> Socket::create(int domain, int type, int protocol)
  37. {
  38. switch (domain) {
  39. case AF_LOCAL:
  40. return LocalSocket::create(type & SOCK_TYPE_MASK);
  41. case AF_INET:
  42. return IPv4Socket::create(type & SOCK_TYPE_MASK, protocol);
  43. default:
  44. return KResult(-EAFNOSUPPORT);
  45. }
  46. }
  47. Socket::Socket(int domain, int type, int protocol)
  48. : m_domain(domain)
  49. , m_type(type)
  50. , m_protocol(protocol)
  51. {
  52. auto& process = *Process::current;
  53. m_origin = { process.pid(), process.uid(), process.gid() };
  54. }
  55. Socket::~Socket()
  56. {
  57. }
  58. void Socket::set_setup_state(SetupState new_setup_state)
  59. {
  60. #ifdef SOCKET_DEBUG
  61. dbg() << "Socket{" << this << "} setup state moving from " << to_string(m_setup_state) << " to " << to_string(new_setup_state);
  62. #endif
  63. m_setup_state = new_setup_state;
  64. }
  65. RefPtr<Socket> Socket::accept()
  66. {
  67. LOCKER(m_lock);
  68. if (m_pending.is_empty())
  69. return nullptr;
  70. #ifdef SOCKET_DEBUG
  71. dbg() << "Socket{" << this << "} de-queueing connection";
  72. #endif
  73. auto client = m_pending.take_first();
  74. ASSERT(!client->is_connected());
  75. auto& process = *Process::current;
  76. client->m_acceptor = { process.pid(), process.uid(), process.gid() };
  77. client->m_connected = true;
  78. client->m_role = Role::Accepted;
  79. return client;
  80. }
  81. KResult Socket::queue_connection_from(NonnullRefPtr<Socket> peer)
  82. {
  83. #ifdef SOCKET_DEBUG
  84. dbg() << "Socket{" << this << "} queueing connection";
  85. #endif
  86. LOCKER(m_lock);
  87. if (m_pending.size() >= m_backlog)
  88. return KResult(-ECONNREFUSED);
  89. m_pending.append(peer);
  90. return KSuccess;
  91. }
  92. KResult Socket::setsockopt(int level, int option, const void* value, socklen_t value_size)
  93. {
  94. ASSERT(level == SOL_SOCKET);
  95. switch (option) {
  96. case SO_SNDTIMEO:
  97. if (value_size != sizeof(timeval))
  98. return KResult(-EINVAL);
  99. m_send_timeout = *(const timeval*)value;
  100. return KSuccess;
  101. case SO_RCVTIMEO:
  102. if (value_size != sizeof(timeval))
  103. return KResult(-EINVAL);
  104. m_receive_timeout = *(const timeval*)value;
  105. return KSuccess;
  106. default:
  107. dbg() << "setsockopt(" << option << ") at SOL_SOCKET not implemented.";
  108. return KResult(-ENOPROTOOPT);
  109. }
  110. }
  111. KResult Socket::getsockopt(FileDescription&, int level, int option, void* value, socklen_t* value_size)
  112. {
  113. ASSERT(level == SOL_SOCKET);
  114. switch (option) {
  115. case SO_SNDTIMEO:
  116. if (*value_size < sizeof(timeval))
  117. return KResult(-EINVAL);
  118. *(timeval*)value = m_send_timeout;
  119. *value_size = sizeof(timeval);
  120. return KSuccess;
  121. case SO_RCVTIMEO:
  122. if (*value_size < sizeof(timeval))
  123. return KResult(-EINVAL);
  124. *(timeval*)value = m_receive_timeout;
  125. *value_size = sizeof(timeval);
  126. return KSuccess;
  127. case SO_ERROR:
  128. if (*value_size < sizeof(int))
  129. return KResult(-EINVAL);
  130. dbg() << "getsockopt(SO_ERROR): FIXME!";
  131. *(int*)value = 0;
  132. *value_size = sizeof(int);
  133. return KSuccess;
  134. default:
  135. dbg() << "getsockopt(" << option << ") at SOL_SOCKET not implemented.";
  136. return KResult(-ENOPROTOOPT);
  137. }
  138. }
  139. ssize_t Socket::read(FileDescription& description, u8* buffer, ssize_t size)
  140. {
  141. if (is_shut_down_for_reading())
  142. return 0;
  143. return recvfrom(description, buffer, size, 0, nullptr, 0);
  144. }
  145. ssize_t Socket::write(FileDescription& description, const u8* data, ssize_t size)
  146. {
  147. if (is_shut_down_for_writing())
  148. return -EPIPE;
  149. return sendto(description, data, size, 0, nullptr, 0);
  150. }
  151. KResult Socket::shutdown(int how)
  152. {
  153. if (type() == SOCK_STREAM && !is_connected())
  154. return KResult(-ENOTCONN);
  155. if (m_role == Role::Listener)
  156. return KResult(-ENOTCONN);
  157. if (!m_shut_down_for_writing && (how & SHUT_WR))
  158. shut_down_for_writing();
  159. if (!m_shut_down_for_reading && (how & SHUT_RD))
  160. shut_down_for_reading();
  161. m_shut_down_for_reading |= (how & SHUT_RD) != 0;
  162. m_shut_down_for_writing |= (how & SHUT_WR) != 0;
  163. return KSuccess;
  164. }
  165. }