client.cc 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389
  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. #ifdef HAVE_POLL_H
  43. # include <poll.h>
  44. #endif
  45. #ifndef HAVE_MSG_NOSIGNAL
  46. # define MSG_NOSIGNAL 0
  47. #endif
  48. namespace libtest {
  49. SimpleClient::SimpleClient(const std::string& hostname_, in_port_t port_) :
  50. _is_connected(false),
  51. _hostname(hostname_),
  52. _port(port_),
  53. sock_fd(INVALID_SOCKET),
  54. requested_message(1)
  55. {
  56. }
  57. bool SimpleClient::ready(int event_)
  58. {
  59. struct pollfd fds[1];
  60. fds[0].fd= sock_fd;
  61. fds[0].events= event_;
  62. fds[0].revents= 0;
  63. int timeout= 5000;
  64. if (_is_connected == false)
  65. {
  66. timeout= timeout * 30;
  67. }
  68. int ready_fds= poll(fds, 1, timeout);
  69. if (ready_fds == -1)
  70. {
  71. _error= strerror(errno);
  72. return false;
  73. }
  74. else if (ready_fds == 1)
  75. {
  76. if (fds[0].revents & (POLLERR | POLLHUP | POLLNVAL))
  77. {
  78. int err;
  79. socklen_t len= sizeof (err);
  80. // We replace errno with err if getsockopt() passes, but err has been
  81. // set.
  82. if (getsockopt(fds[0].fd, SOL_SOCKET, SO_ERROR, &err, &len) == 0)
  83. {
  84. // We check the value to see what happened wth the socket.
  85. if (err == 0)
  86. {
  87. _error= "getsockopt() returned no error but poll() indicated one existed";
  88. return false;
  89. }
  90. errno= err;
  91. }
  92. _error= strerror(errno);
  93. return false;
  94. }
  95. _is_connected= true;
  96. if (fds[0].revents & event_)
  97. {
  98. return true;
  99. }
  100. }
  101. fatal_assert(ready_fds == 0);
  102. _error= "TIMEOUT";
  103. return false;
  104. }
  105. struct addrinfo* SimpleClient::lookup()
  106. {
  107. struct addrinfo *ai= NULL;
  108. struct addrinfo hints;
  109. memset(&hints, 0, sizeof(struct addrinfo));
  110. hints.ai_socktype= SOCK_STREAM;
  111. hints.ai_protocol= IPPROTO_TCP;
  112. libtest::vchar_t service;
  113. service.resize(NI_MAXSERV);
  114. (void)snprintf(&service[0], service.size(), "%d", _port);
  115. int getaddrinfo_error;
  116. if ((getaddrinfo_error= getaddrinfo(_hostname.c_str(), &service[0], &hints, &ai)) != 0)
  117. {
  118. if (getaddrinfo_error != EAI_SYSTEM)
  119. {
  120. _error= gai_strerror(getaddrinfo_error);
  121. return NULL;
  122. }
  123. else
  124. {
  125. _error= strerror(getaddrinfo_error);
  126. return NULL;
  127. }
  128. }
  129. return ai;
  130. }
  131. SimpleClient::~SimpleClient()
  132. {
  133. close_socket();
  134. }
  135. void SimpleClient::close_socket()
  136. {
  137. if (sock_fd != INVALID_SOCKET)
  138. {
  139. close(sock_fd);
  140. sock_fd= INVALID_SOCKET;
  141. }
  142. }
  143. bool SimpleClient::instance_connect()
  144. {
  145. _is_connected= false;
  146. struct addrinfo *ai;
  147. if ((ai= lookup()))
  148. {
  149. {
  150. struct addrinfo* address_info_next= ai;
  151. while (address_info_next and sock_fd == INVALID_SOCKET)
  152. {
  153. if ((sock_fd= socket(address_info_next->ai_family, address_info_next->ai_socktype, address_info_next->ai_protocol)) != SOCKET_ERROR)
  154. {
  155. if (connect(sock_fd, address_info_next->ai_addr, address_info_next->ai_addrlen) == SOCKET_ERROR)
  156. {
  157. switch (errno)
  158. {
  159. case EINTR:
  160. close_socket();
  161. continue;
  162. case EINPROGRESS: // nonblocking mode - first return
  163. case EALREADY: // nonblocking mode - subsequent returns
  164. continue; // Jump to while() and continue on
  165. case ECONNREFUSED:
  166. default:
  167. break;
  168. }
  169. close_socket();
  170. _error= strerror(errno);
  171. }
  172. }
  173. else
  174. {
  175. fatal_message(strerror(errno));
  176. }
  177. address_info_next= address_info_next->ai_next;
  178. }
  179. freeaddrinfo(ai);
  180. }
  181. if (sock_fd == INVALID_SOCKET)
  182. {
  183. fatal_assert(_error.size());
  184. }
  185. return bool(sock_fd != INVALID_SOCKET);
  186. }
  187. return false;
  188. }
  189. bool SimpleClient::is_valid()
  190. {
  191. _error.clear();
  192. if (sock_fd == INVALID_SOCKET)
  193. {
  194. return instance_connect();
  195. }
  196. return true;
  197. }
  198. bool SimpleClient::message(const char* ptr, const size_t len)
  199. {
  200. if (is_valid())
  201. {
  202. if (ready(POLLOUT))
  203. {
  204. off_t offset= 0;
  205. do
  206. {
  207. ssize_t nw= send(sock_fd, ptr + offset, len - offset, MSG_NOSIGNAL);
  208. if (nw == -1)
  209. {
  210. if (errno != EINTR)
  211. {
  212. _error= strerror(errno);
  213. return false;
  214. }
  215. }
  216. else
  217. {
  218. offset += nw;
  219. }
  220. } while (offset < ssize_t(len));
  221. return true;
  222. }
  223. }
  224. fatal_assert(_error.size());
  225. return false;
  226. }
  227. bool SimpleClient::send_message(const std::string& arg)
  228. {
  229. if (message(arg.c_str(), arg.size()) == true)
  230. {
  231. return message("\r\n", 2);
  232. }
  233. return false;
  234. }
  235. bool SimpleClient::send_data(const libtest::vchar_t& message_, libtest::vchar_t& response_)
  236. {
  237. requested_message++;
  238. if (message(&message_[0], message_.size()))
  239. {
  240. return response(response_);
  241. }
  242. return false;
  243. }
  244. bool SimpleClient::send_message(const std::string& message_, std::string& response_)
  245. {
  246. requested_message++;
  247. if (send_message(message_))
  248. {
  249. return response(response_);
  250. }
  251. return false;
  252. }
  253. bool SimpleClient::response(libtest::vchar_t& response_)
  254. {
  255. response_.clear();
  256. if (is_valid())
  257. {
  258. if (ready(POLLIN))
  259. {
  260. bool more= true;
  261. char buffer[2];
  262. buffer[1]= 0;
  263. do
  264. {
  265. ssize_t nr= recv(sock_fd, buffer, 1, MSG_NOSIGNAL);
  266. if (nr == -1)
  267. {
  268. if (errno != EINTR)
  269. {
  270. _error= strerror(errno);
  271. return false;
  272. }
  273. }
  274. else if (nr == 0)
  275. {
  276. close_socket();
  277. more= false;
  278. }
  279. else
  280. {
  281. response_.reserve(response_.size() + nr +1);
  282. fatal_assert(nr == 1);
  283. if (buffer[0] == '\n')
  284. {
  285. more= false;
  286. }
  287. response_.insert(response_.end(), buffer, buffer +nr);
  288. }
  289. } while (more);
  290. return response_.size();
  291. }
  292. }
  293. fatal_assert(_error.size());
  294. return false;
  295. }
  296. bool SimpleClient::response(std::string& response_)
  297. {
  298. response_.clear();
  299. if (is_valid())
  300. {
  301. if (ready(POLLIN))
  302. {
  303. bool more= true;
  304. char buffer[2];
  305. buffer[1]= 0;
  306. do
  307. {
  308. ssize_t nr= recv(sock_fd, buffer, 1, MSG_NOSIGNAL);
  309. if (nr == -1)
  310. {
  311. if (errno != EINTR)
  312. {
  313. _error= strerror(errno);
  314. return false;
  315. }
  316. }
  317. else if (nr == 0)
  318. {
  319. close_socket();
  320. more= false;
  321. }
  322. else
  323. {
  324. fatal_assert(nr == 1);
  325. if (buffer[0] == '\n')
  326. {
  327. more= false;
  328. }
  329. response_.append(buffer);
  330. }
  331. } while (more);
  332. return response_.size();
  333. }
  334. }
  335. fatal_assert(_error.size());
  336. return false;
  337. }
  338. } // namespace libtest