PlainConnection.cpp 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. #include "PlainConnection.h"
  2. #include <cstring>
  3. #include <netinet/tcp.h>
  4. #include <errno.h>
  5. #include "Logger.h"
  6. PlainConnection::PlainConnection()
  7. {
  8. this->apSock = -1;
  9. };
  10. PlainConnection::~PlainConnection()
  11. {
  12. closeSocket();
  13. };
  14. void PlainConnection::connectToAp(std::string apAddress)
  15. {
  16. struct addrinfo h, *airoot, *ai;
  17. std::string hostname = apAddress.substr(0, apAddress.find(":"));
  18. std::string portStr = apAddress.substr(apAddress.find(":") + 1, apAddress.size());
  19. memset(&h, 0, sizeof(h));
  20. h.ai_family = AF_INET;
  21. h.ai_socktype = SOCK_STREAM;
  22. h.ai_protocol = IPPROTO_IP;
  23. // Lookup host
  24. if (getaddrinfo(hostname.c_str(), portStr.c_str(), &h, &airoot))
  25. {
  26. CSPOT_LOG(error, "getaddrinfo failed");
  27. }
  28. // find the right ai, connect to server
  29. for (ai = airoot; ai; ai = ai->ai_next)
  30. {
  31. if (ai->ai_family != AF_INET && ai->ai_family != AF_INET6)
  32. continue;
  33. this->apSock = socket(ai->ai_family,
  34. ai->ai_socktype, ai->ai_protocol);
  35. if (this->apSock < 0)
  36. continue;
  37. if (connect(this->apSock,
  38. (struct sockaddr *)ai->ai_addr,
  39. ai->ai_addrlen) != -1)
  40. {
  41. struct timeval tv;
  42. tv.tv_sec = 3;
  43. tv.tv_usec = 0;
  44. setsockopt(this->apSock, SOL_SOCKET, SO_RCVTIMEO, (const char *)&tv, sizeof tv);
  45. setsockopt(this->apSock, SOL_SOCKET, SO_SNDTIMEO, (const char *)&tv, sizeof tv);
  46. int flag = 1;
  47. setsockopt(this->apSock, /* socket affected */
  48. IPPROTO_TCP, /* set option at TCP level */
  49. TCP_NODELAY, /* name of option */
  50. (char *)&flag, /* the cast is historical cruft */
  51. sizeof(int)); /* length of option value */
  52. break;
  53. }
  54. close(this->apSock);
  55. apSock = -1;
  56. throw std::runtime_error("Can't connect to spotify servers");
  57. }
  58. freeaddrinfo(airoot);
  59. CSPOT_LOG(debug, "Connected to spotify server");
  60. }
  61. std::vector<uint8_t> PlainConnection::recvPacket()
  62. {
  63. // Read packet size
  64. auto sizeData = readBlock(4);
  65. uint32_t packetSize = ntohl(extract<uint32_t>(sizeData, 0));
  66. // Read actual data
  67. auto data = readBlock(packetSize - 4);
  68. sizeData.insert(sizeData.end(), data.begin(), data.end());
  69. return sizeData;
  70. }
  71. std::vector<uint8_t> PlainConnection::sendPrefixPacket(const std::vector<uint8_t> &prefix, const std::vector<uint8_t> &data)
  72. {
  73. // Calculate full packet length
  74. uint32_t actualSize = prefix.size() + data.size() + sizeof(uint32_t);
  75. // Packet structure [PREFIX] + [SIZE] + [DATA]
  76. auto sizeRaw = pack<uint32_t>(htonl(actualSize));
  77. sizeRaw.insert(sizeRaw.begin(), prefix.begin(), prefix.end());
  78. sizeRaw.insert(sizeRaw.end(), data.begin(), data.end());
  79. // Actually write it to the server
  80. writeBlock(sizeRaw);
  81. return sizeRaw;
  82. }
  83. std::vector<uint8_t> PlainConnection::readBlock(size_t size)
  84. {
  85. std::vector<uint8_t> buf(size);
  86. unsigned int idx = 0;
  87. ssize_t n;
  88. int retries = 0;
  89. // printf("START READ\n");
  90. while (idx < size)
  91. {
  92. READ:
  93. if ((n = recv(this->apSock, &buf[idx], size - idx, 0)) <= 0)
  94. {
  95. switch (errno)
  96. {
  97. case EAGAIN:
  98. case ETIMEDOUT:
  99. if (timeoutHandler())
  100. {
  101. CSPOT_LOG(error, "Connection lost, will need to reconnect...");
  102. throw std::runtime_error("Reconnection required");
  103. }
  104. goto READ;
  105. case EINTR:
  106. break;
  107. default:
  108. if (retries++ > 4) throw std::runtime_error("Error in read");
  109. goto READ;
  110. }
  111. }
  112. idx += n;
  113. }
  114. // printf("FINISH READ\n");
  115. return buf;
  116. }
  117. size_t PlainConnection::writeBlock(const std::vector<uint8_t> &data)
  118. {
  119. unsigned int idx = 0;
  120. ssize_t n;
  121. // printf("START WRITE\n");
  122. int retries = 0;
  123. while (idx < data.size())
  124. {
  125. WRITE:
  126. if ((n = send(this->apSock, &data[idx], data.size() - idx < 64 ? data.size() - idx : 64, 0)) <= 0)
  127. {
  128. switch (errno)
  129. {
  130. case EAGAIN:
  131. case ETIMEDOUT:
  132. if (timeoutHandler())
  133. {
  134. throw std::runtime_error("Reconnection required");
  135. }
  136. goto WRITE;
  137. case EINTR:
  138. break;
  139. default:
  140. if (retries++ > 4) throw std::runtime_error("Error in write");
  141. }
  142. }
  143. idx += n;
  144. }
  145. return data.size();
  146. }
  147. void PlainConnection::closeSocket()
  148. {
  149. if (this->apSock < 0) return;
  150. CSPOT_LOG(info, "Closing socket...");
  151. shutdown(this->apSock, SHUT_RDWR);
  152. close(this->apSock);
  153. this->apSock = -1;
  154. }