CSocket.cpp 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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. m_notifier->set_event_mask(CNotifier::Event::None);
  79. if (on_connected)
  80. on_connected();
  81. };
  82. return true;
  83. }
  84. perror("CSocket::common_connect: connect");
  85. return false;
  86. }
  87. dbg() << *this << " connected ok!";
  88. m_connected = true;
  89. if (on_connected)
  90. on_connected();
  91. return true;
  92. }
  93. ByteBuffer CSocket::receive(int max_size)
  94. {
  95. auto buffer = read(max_size);
  96. if (eof()) {
  97. dbg() << *this << " connection appears to have closed in receive().";
  98. m_connected = false;
  99. }
  100. return buffer;
  101. }
  102. bool CSocket::send(const ByteBuffer& data)
  103. {
  104. int nsent = ::send(fd(), data.pointer(), data.size(), 0);
  105. if (nsent < 0) {
  106. set_error(errno);
  107. return false;
  108. }
  109. ASSERT(nsent == data.size());
  110. return true;
  111. }
  112. void CSocket::did_update_fd(int fd)
  113. {
  114. if (fd < 0) {
  115. m_read_notifier = nullptr;
  116. return;
  117. }
  118. m_read_notifier = CNotifier::construct(fd, CNotifier::Event::Read, this);
  119. m_read_notifier->on_ready_to_read = [this] {
  120. if (on_ready_to_read)
  121. on_ready_to_read();
  122. };
  123. }