CSocket.cpp 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  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. fflush(stdout);
  57. int rc = ::connect(fd(), (struct sockaddr*)&addr, sizeof(addr));
  58. if (rc < 0) {
  59. if (errno == EINPROGRESS) {
  60. dbg() << *this << " connection in progress (EINPROGRESS)";
  61. m_notifier = make<CNotifier>(fd(), CNotifier::Event::Write, this);
  62. m_notifier->on_ready_to_write = [this] {
  63. dbg() << *this << " connected!";
  64. m_connected = true;
  65. m_notifier->set_event_mask(CNotifier::Event::None);
  66. if (on_connected)
  67. on_connected();
  68. };
  69. return true;
  70. }
  71. perror("connect");
  72. exit(1);
  73. } else {
  74. dbg() << *this << " connected ok!";
  75. m_connected = true;
  76. if (on_connected)
  77. on_connected();
  78. }
  79. return true;
  80. }
  81. bool CSocket::connect(const CSocketAddress& address)
  82. {
  83. ASSERT(!is_connected());
  84. ASSERT(address.type() == CSocketAddress::Type::Local);
  85. dbg() << *this << " connecting to " << address << "...";
  86. sockaddr_un saddr;
  87. saddr.sun_family = AF_LOCAL;
  88. strcpy(saddr.sun_path, address.to_string().characters());
  89. int rc = ::connect(fd(), (const sockaddr*)&saddr, sizeof(saddr));
  90. if (rc < 0) {
  91. perror("connect");
  92. return false;
  93. }
  94. m_connected = true;
  95. if (on_connected)
  96. on_connected();
  97. return true;
  98. }
  99. ByteBuffer CSocket::receive(int max_size)
  100. {
  101. auto buffer = read(max_size);
  102. if (eof()) {
  103. dbg() << *this << " connection appears to have closed in receive().";
  104. m_connected = false;
  105. }
  106. return buffer;
  107. }
  108. bool CSocket::send(const ByteBuffer& data)
  109. {
  110. int nsent = ::send(fd(), data.pointer(), data.size(), 0);
  111. if (nsent < 0) {
  112. set_error(errno);
  113. return false;
  114. }
  115. ASSERT(nsent == data.size());
  116. return true;
  117. }
  118. void CSocket::did_update_fd(int fd)
  119. {
  120. if (fd < 0) {
  121. m_read_notifier = nullptr;
  122. return;
  123. }
  124. m_read_notifier = make<CNotifier>(fd, CNotifier::Event::Read, this);
  125. m_read_notifier->on_ready_to_read = [this] {
  126. if (on_ready_to_read)
  127. on_ready_to_read();
  128. };
  129. }