client.cc 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366
  1. /* vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
  2. *
  3. * Data Differential YATL (i.e. libtest) library
  4. *
  5. * Copyright (C) 2012 Data Differential, http://datadifferential.com/
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions are
  9. * met:
  10. *
  11. * * Redistributions of source code must retain the above copyright
  12. * notice, this list of conditions and the following disclaimer.
  13. *
  14. * * Redistributions in binary form must reproduce the above
  15. * copyright notice, this list of conditions and the following disclaimer
  16. * in the documentation and/or other materials provided with the
  17. * distribution.
  18. *
  19. * * The names of its contributors may not be used to endorse or
  20. * promote products derived from this software without specific prior
  21. * written permission.
  22. *
  23. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  24. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  25. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  26. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  27. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  28. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  29. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  30. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  31. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  32. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  33. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  34. *
  35. */
  36. #include "libtest/yatlcon.h"
  37. #include <libtest/common.h>
  38. #include <sys/types.h>
  39. #include <sys/socket.h>
  40. #include <unistd.h>
  41. #include <string>
  42. namespace libtest {
  43. SimpleClient::SimpleClient(const std::string& hostname_, in_port_t port_) :
  44. _is_connected(false),
  45. _hostname(hostname_),
  46. _port(port_),
  47. sock_fd(INVALID_SOCKET),
  48. requested_message(1)
  49. {
  50. }
  51. bool SimpleClient::ready(int event_)
  52. {
  53. struct pollfd fds[1];
  54. fds[0].fd= sock_fd;
  55. fds[0].events= event_;
  56. fds[0].revents= 0;
  57. int timeout= 5000;
  58. if (_is_connected == false)
  59. {
  60. timeout= timeout * 30;
  61. }
  62. int ready_fds= poll(fds, 1, timeout);
  63. if (ready_fds == -1)
  64. {
  65. _error= strerror(errno);
  66. return false;
  67. }
  68. else if (ready_fds == 1)
  69. {
  70. if (fds[0].revents & (POLLERR | POLLHUP | POLLNVAL))
  71. {
  72. int err;
  73. socklen_t len= sizeof (err);
  74. // We replace errno with err if getsockopt() passes, but err has been
  75. // set.
  76. if (getsockopt(fds[0].fd, SOL_SOCKET, SO_ERROR, &err, &len) == 0)
  77. {
  78. // We check the value to see what happened wth the socket.
  79. if (err == 0)
  80. {
  81. _error= "getsockopt() returned no error but poll() indicated one existed";
  82. return false;
  83. }
  84. errno= err;
  85. }
  86. _error= strerror(errno);
  87. return false;
  88. }
  89. _is_connected= true;
  90. if (fds[0].revents & event_)
  91. {
  92. return true;
  93. }
  94. }
  95. fatal_assert(ready_fds == 0);
  96. _error= "TIMEOUT";
  97. return false;
  98. }
  99. struct addrinfo* SimpleClient::lookup()
  100. {
  101. struct addrinfo *ai= NULL;
  102. struct addrinfo hints;
  103. memset(&hints, 0, sizeof(struct addrinfo));
  104. hints.ai_socktype= SOCK_STREAM;
  105. hints.ai_protocol= IPPROTO_TCP;
  106. libtest::vchar_t service;
  107. service.resize(NI_MAXSERV);
  108. (void)snprintf(&service[0], service.size(), "%d", _port);
  109. int getaddrinfo_error;
  110. if ((getaddrinfo_error= getaddrinfo(_hostname.c_str(), &service[0], &hints, &ai)) != 0)
  111. {
  112. if (getaddrinfo_error != EAI_SYSTEM)
  113. {
  114. _error= gai_strerror(getaddrinfo_error);
  115. return NULL;
  116. }
  117. else
  118. {
  119. _error= strerror(getaddrinfo_error);
  120. return NULL;
  121. }
  122. }
  123. return ai;
  124. }
  125. SimpleClient::~SimpleClient()
  126. {
  127. close_socket();
  128. }
  129. void SimpleClient::close_socket()
  130. {
  131. if (sock_fd != INVALID_SOCKET)
  132. {
  133. close(sock_fd);
  134. sock_fd= INVALID_SOCKET;
  135. }
  136. }
  137. bool SimpleClient::instance_connect()
  138. {
  139. _is_connected= false;
  140. struct addrinfo *ai;
  141. if ((ai= lookup()))
  142. {
  143. {
  144. struct addrinfo* address_info_next= ai;
  145. while (address_info_next and sock_fd == INVALID_SOCKET)
  146. {
  147. if ((sock_fd= socket(address_info_next->ai_family, address_info_next->ai_socktype, address_info_next->ai_protocol)) != SOCKET_ERROR)
  148. {
  149. if (connect(sock_fd, address_info_next->ai_addr, address_info_next->ai_addrlen) == SOCKET_ERROR)
  150. {
  151. close_socket();
  152. _error= strerror(errno);
  153. }
  154. }
  155. else
  156. {
  157. fatal_message(strerror(errno));
  158. }
  159. address_info_next= address_info_next->ai_next;
  160. }
  161. freeaddrinfo(ai);
  162. }
  163. if (sock_fd == INVALID_SOCKET)
  164. {
  165. fatal_assert(_error.size());
  166. }
  167. return bool(sock_fd != INVALID_SOCKET);
  168. }
  169. return false;
  170. }
  171. bool SimpleClient::is_valid()
  172. {
  173. _error.clear();
  174. if (sock_fd == INVALID_SOCKET)
  175. {
  176. return instance_connect();
  177. }
  178. return true;
  179. }
  180. bool SimpleClient::message(const char* ptr, const size_t len)
  181. {
  182. if (is_valid())
  183. {
  184. if (ready(POLLOUT))
  185. {
  186. off_t offset= 0;
  187. do
  188. {
  189. ssize_t nw= send(sock_fd, ptr + offset, len - offset, MSG_NOSIGNAL);
  190. if (nw == -1)
  191. {
  192. if (errno != EINTR)
  193. {
  194. _error= strerror(errno);
  195. return false;
  196. }
  197. }
  198. else
  199. {
  200. offset += nw;
  201. }
  202. } while (offset < ssize_t(len));
  203. return true;
  204. }
  205. }
  206. fatal_assert(_error.size());
  207. return false;
  208. }
  209. bool SimpleClient::send_message(const std::string& arg)
  210. {
  211. if (message(arg.c_str(), arg.size()) == true)
  212. {
  213. return message("\r\n", 2);
  214. }
  215. return false;
  216. }
  217. bool SimpleClient::send_data(const libtest::vchar_t& message_, libtest::vchar_t& response_)
  218. {
  219. requested_message++;
  220. if (message(&message_[0], message_.size()))
  221. {
  222. return response(response_);
  223. }
  224. return false;
  225. }
  226. bool SimpleClient::send_message(const std::string& message_, std::string& response_)
  227. {
  228. requested_message++;
  229. if (send_message(message_))
  230. {
  231. return response(response_);
  232. }
  233. return false;
  234. }
  235. bool SimpleClient::response(libtest::vchar_t& response_)
  236. {
  237. response_.clear();
  238. if (is_valid())
  239. {
  240. if (ready(POLLIN))
  241. {
  242. bool more= true;
  243. char buffer[2];
  244. buffer[1]= 0;
  245. do
  246. {
  247. ssize_t nr= recv(sock_fd, buffer, 1, MSG_NOSIGNAL);
  248. if (nr == -1)
  249. {
  250. if (errno != EINTR)
  251. {
  252. _error= strerror(errno);
  253. return false;
  254. }
  255. }
  256. else if (nr == 0)
  257. {
  258. close_socket();
  259. more= false;
  260. }
  261. else
  262. {
  263. response_.reserve(response_.size() + nr +1);
  264. fatal_assert(nr == 1);
  265. if (buffer[0] == '\n')
  266. {
  267. more= false;
  268. }
  269. response_.insert(response_.end(), buffer, buffer +nr);
  270. }
  271. } while (more);
  272. return response_.size();
  273. }
  274. }
  275. fatal_assert(_error.size());
  276. return false;
  277. }
  278. bool SimpleClient::response(std::string& response_)
  279. {
  280. response_.clear();
  281. if (is_valid())
  282. {
  283. if (ready(POLLIN))
  284. {
  285. bool more= true;
  286. char buffer[2];
  287. buffer[1]= 0;
  288. do
  289. {
  290. ssize_t nr= recv(sock_fd, buffer, 1, MSG_NOSIGNAL);
  291. if (nr == -1)
  292. {
  293. if (errno != EINTR)
  294. {
  295. _error= strerror(errno);
  296. return false;
  297. }
  298. }
  299. else if (nr == 0)
  300. {
  301. close_socket();
  302. more= false;
  303. }
  304. else
  305. {
  306. fatal_assert(nr == 1);
  307. if (buffer[0] == '\n')
  308. {
  309. more= false;
  310. }
  311. response_.append(buffer);
  312. }
  313. } while (more);
  314. return response_.size();
  315. }
  316. }
  317. fatal_assert(_error.size());
  318. return false;
  319. }
  320. } // namespace libtest