Socket.cpp 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  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 <LibCore/Notifier.h>
  27. #include <LibCore/Socket.h>
  28. #include <arpa/inet.h>
  29. #include <errno.h>
  30. #include <fcntl.h>
  31. #include <netdb.h>
  32. #include <netinet/in.h>
  33. #include <stdio.h>
  34. #include <stdlib.h>
  35. #include <sys/socket.h>
  36. #include <unistd.h>
  37. //#define CSOCKET_DEBUG
  38. namespace Core {
  39. Socket::Socket(Type type, Object* parent)
  40. : IODevice(parent)
  41. , m_type(type)
  42. {
  43. }
  44. Socket::~Socket()
  45. {
  46. close();
  47. }
  48. bool Socket::connect(const String& hostname, int port)
  49. {
  50. auto* hostent = gethostbyname(hostname.characters());
  51. if (!hostent) {
  52. dbg() << "Socket::connect: Unable to resolve '" << hostname << "'";
  53. return false;
  54. }
  55. IPv4Address host_address((const u8*)hostent->h_addr_list[0]);
  56. #ifdef CSOCKET_DEBUG
  57. dbg() << "Socket::connect: Resolved '" << hostname << "' to " << host_address;
  58. #endif
  59. return connect(host_address, port);
  60. }
  61. void Socket::set_blocking(bool blocking)
  62. {
  63. int flags = fcntl(fd(), F_GETFL, 0);
  64. ASSERT(flags >= 0);
  65. if (blocking)
  66. flags = fcntl(fd(), F_SETFL, flags & ~O_NONBLOCK);
  67. else
  68. flags = fcntl(fd(), F_SETFL, flags | O_NONBLOCK);
  69. ASSERT(flags == 0);
  70. }
  71. bool Socket::connect(const SocketAddress& address, int port)
  72. {
  73. ASSERT(!is_connected());
  74. ASSERT(address.type() == SocketAddress::Type::IPv4);
  75. #ifdef CSOCKET_DEBUG
  76. dbg() << *this << " connecting to " << address << "...";
  77. #endif
  78. ASSERT(port > 0 && port <= 65535);
  79. struct sockaddr_in addr;
  80. memset(&addr, 0, sizeof(addr));
  81. auto ipv4_address = address.ipv4_address();
  82. memcpy(&addr.sin_addr.s_addr, &ipv4_address, sizeof(IPv4Address));
  83. addr.sin_family = AF_INET;
  84. addr.sin_port = htons(port);
  85. m_destination_address = address;
  86. m_destination_port = port;
  87. return common_connect((struct sockaddr*)&addr, sizeof(addr));
  88. }
  89. bool Socket::connect(const SocketAddress& address)
  90. {
  91. ASSERT(!is_connected());
  92. ASSERT(address.type() == SocketAddress::Type::Local);
  93. #ifdef CSOCKET_DEBUG
  94. dbg() << *this << " connecting to " << address << "...";
  95. #endif
  96. sockaddr_un saddr;
  97. saddr.sun_family = AF_LOCAL;
  98. strcpy(saddr.sun_path, address.to_string().characters());
  99. return common_connect((const sockaddr*)&saddr, sizeof(saddr));
  100. }
  101. bool Socket::common_connect(const struct sockaddr* addr, socklen_t addrlen)
  102. {
  103. int rc = ::connect(fd(), addr, addrlen);
  104. if (rc < 0) {
  105. if (errno == EINPROGRESS) {
  106. #ifdef CSOCKET_DEBUG
  107. dbg() << *this << " connection in progress (EINPROGRESS)";
  108. #endif
  109. m_notifier = Notifier::construct(fd(), Notifier::Event::Write, this);
  110. m_notifier->on_ready_to_write = [this] {
  111. #ifdef CSOCKET_DEBUG
  112. dbg() << *this << " connected!";
  113. #endif
  114. m_connected = true;
  115. ensure_read_notifier();
  116. m_notifier->set_event_mask(Notifier::Event::None);
  117. if (on_connected)
  118. on_connected();
  119. };
  120. return true;
  121. }
  122. perror("Socket::common_connect: connect");
  123. return false;
  124. }
  125. #ifdef CSOCKET_DEBUG
  126. dbg() << *this << " connected ok!";
  127. #endif
  128. m_connected = true;
  129. ensure_read_notifier();
  130. if (on_connected)
  131. on_connected();
  132. return true;
  133. }
  134. ByteBuffer Socket::receive(int max_size)
  135. {
  136. auto buffer = read(max_size);
  137. if (eof()) {
  138. dbg() << *this << " connection appears to have closed in receive().";
  139. m_connected = false;
  140. }
  141. return buffer;
  142. }
  143. bool Socket::send(const ByteBuffer& data)
  144. {
  145. int nsent = ::send(fd(), data.data(), data.size(), 0);
  146. if (nsent < 0) {
  147. set_error(errno);
  148. return false;
  149. }
  150. ASSERT(nsent == data.size());
  151. return true;
  152. }
  153. void Socket::did_update_fd(int fd)
  154. {
  155. if (fd < 0) {
  156. m_read_notifier = nullptr;
  157. return;
  158. }
  159. if (m_connected) {
  160. ensure_read_notifier();
  161. } else {
  162. // I don't think it would be right if we updated the fd while not connected *but* while having a notifier..
  163. ASSERT(!m_read_notifier);
  164. }
  165. }
  166. void Socket::ensure_read_notifier()
  167. {
  168. ASSERT(m_connected);
  169. m_read_notifier = Notifier::construct(fd(), Notifier::Event::Read, this);
  170. m_read_notifier->on_ready_to_read = [this] {
  171. if (on_ready_to_read)
  172. on_ready_to_read();
  173. };
  174. }
  175. }