CSocket.cpp 3.4 KB

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