CSocket.cpp 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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. }
  61. dbg() << *this << " connected ok!";
  62. m_connected = true;
  63. return true;
  64. }
  65. bool CSocket::connect(const CSocketAddress& address)
  66. {
  67. ASSERT(!is_connected());
  68. ASSERT(address.type() == CSocketAddress::Type::Local);
  69. dbg() << *this << " connecting to " << address << "...";
  70. sockaddr_un saddr;
  71. saddr.sun_family = AF_LOCAL;
  72. strcpy(saddr.sun_path, address.to_string().characters());
  73. int rc = ::connect(fd(), (const sockaddr*)&saddr, sizeof(saddr));
  74. if (rc < 0) {
  75. perror("connect");
  76. return false;
  77. }
  78. m_connected = true;
  79. return true;
  80. }
  81. ByteBuffer CSocket::receive(int max_size)
  82. {
  83. auto buffer = read(max_size);
  84. if (eof()) {
  85. dbg() << *this << " connection appears to have closed in receive().";
  86. m_connected = false;
  87. }
  88. return buffer;
  89. }
  90. bool CSocket::send(const ByteBuffer& data)
  91. {
  92. int nsent = ::send(fd(), data.pointer(), data.size(), 0);
  93. if (nsent < 0) {
  94. set_error(nsent);
  95. return false;
  96. }
  97. ASSERT(nsent == data.size());
  98. return true;
  99. }