CSocket.cpp 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. #include <LibCore/CNotifier.h>
  2. #include <LibCore/CSocket.h>
  3. #include <arpa/inet.h>
  4. #include <errno.h>
  5. #include <fcntl.h>
  6. #include <netdb.h>
  7. #include <netinet/in.h>
  8. #include <stdio.h>
  9. #include <stdlib.h>
  10. #include <sys/socket.h>
  11. #include <unistd.h>
  12. CSocket::CSocket(Type type, CObject* parent)
  13. : CIODevice(parent)
  14. , m_type(type)
  15. {
  16. }
  17. CSocket::~CSocket()
  18. {
  19. close();
  20. }
  21. bool CSocket::connect(const String& hostname, int port)
  22. {
  23. auto* hostent = gethostbyname(hostname.characters());
  24. if (!hostent) {
  25. dbg() << "CSocket::connect: Unable to resolve '" << hostname << "'";
  26. return false;
  27. }
  28. IPv4Address host_address((const u8*)hostent->h_addr_list[0]);
  29. dbg() << "CSocket::connect: Resolved '" << hostname << "' to " << host_address;
  30. return connect(host_address, port);
  31. }
  32. void CSocket::set_blocking(bool blocking)
  33. {
  34. int flags = fcntl(fd(), F_GETFL, 0);
  35. ASSERT(flags >= 0);
  36. if (blocking)
  37. flags = fcntl(fd(), F_SETFL, flags | O_NONBLOCK);
  38. else
  39. flags = fcntl(fd(), F_SETFL, flags & O_NONBLOCK);
  40. ASSERT(flags >= 0);
  41. }
  42. bool CSocket::connect(const CSocketAddress& address, int port)
  43. {
  44. ASSERT(!is_connected());
  45. ASSERT(address.type() == CSocketAddress::Type::IPv4);
  46. dbg() << *this << " connecting to " << address << "...";
  47. ASSERT(port > 0 && port <= 65535);
  48. struct sockaddr_in addr;
  49. memset(&addr, 0, sizeof(addr));
  50. auto ipv4_address = address.ipv4_address();
  51. memcpy(&addr.sin_addr.s_addr, &ipv4_address, sizeof(IPv4Address));
  52. addr.sin_family = AF_INET;
  53. addr.sin_port = htons(port);
  54. m_destination_address = address;
  55. m_destination_port = port;
  56. return common_connect((struct sockaddr*)&addr, sizeof(addr));
  57. }
  58. bool CSocket::connect(const CSocketAddress& address)
  59. {
  60. ASSERT(!is_connected());
  61. ASSERT(address.type() == CSocketAddress::Type::Local);
  62. dbg() << *this << " connecting to " << address << "...";
  63. sockaddr_un saddr;
  64. saddr.sun_family = AF_LOCAL;
  65. strcpy(saddr.sun_path, address.to_string().characters());
  66. return common_connect((const sockaddr*)&saddr, sizeof(saddr));
  67. }
  68. bool CSocket::common_connect(const struct sockaddr* addr, socklen_t addrlen)
  69. {
  70. int rc = ::connect(fd(), addr, addrlen);
  71. if (rc < 0) {
  72. if (errno == EINPROGRESS) {
  73. dbg() << *this << " connection in progress (EINPROGRESS)";
  74. m_notifier = CNotifier::construct(fd(), CNotifier::Event::Write, this);
  75. m_notifier->on_ready_to_write = [this] {
  76. dbg() << *this << " connected!";
  77. m_connected = true;
  78. ensure_read_notifier();
  79. m_notifier->set_event_mask(CNotifier::Event::None);
  80. if (on_connected)
  81. on_connected();
  82. };
  83. return true;
  84. }
  85. perror("CSocket::common_connect: connect");
  86. return false;
  87. }
  88. dbg() << *this << " connected ok!";
  89. m_connected = true;
  90. ensure_read_notifier();
  91. if (on_connected)
  92. on_connected();
  93. return true;
  94. }
  95. ByteBuffer CSocket::receive(int max_size)
  96. {
  97. auto buffer = read(max_size);
  98. if (eof()) {
  99. dbg() << *this << " connection appears to have closed in receive().";
  100. m_connected = false;
  101. }
  102. return buffer;
  103. }
  104. bool CSocket::send(const ByteBuffer& data)
  105. {
  106. int nsent = ::send(fd(), data.data(), data.size(), 0);
  107. if (nsent < 0) {
  108. set_error(errno);
  109. return false;
  110. }
  111. ASSERT(nsent == data.size());
  112. return true;
  113. }
  114. void CSocket::did_update_fd(int fd)
  115. {
  116. if (fd < 0) {
  117. m_read_notifier = nullptr;
  118. return;
  119. }
  120. if (m_connected) {
  121. ensure_read_notifier();
  122. } else {
  123. // I don't think it would be right if we updated the fd while not connected *but* while having a notifier..
  124. ASSERT(!m_read_notifier);
  125. }
  126. }
  127. void CSocket::ensure_read_notifier()
  128. {
  129. ASSERT(m_connected);
  130. m_read_notifier = CNotifier::construct(fd(), CNotifier::Event::Read, this);
  131. m_read_notifier->on_ready_to_read = [this] {
  132. if (on_ready_to_read)
  133. on_ready_to_read();
  134. };
  135. }