CSocket.cpp 3.1 KB

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