lib582.c 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) 1998 - 2016, Daniel Stenberg, <daniel@haxx.se>, et al.
  9. *
  10. * This software is licensed as described in the file COPYING, which
  11. * you should have received as part of this distribution. The terms
  12. * are also available at https://curl.haxx.se/docs/copyright.html.
  13. *
  14. * You may opt to use, copy, modify, merge, publish, distribute and/or sell
  15. * copies of the Software, and permit persons to whom the Software is
  16. * furnished to do so, under the terms of the COPYING file.
  17. *
  18. * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
  19. * KIND, either express or implied.
  20. *
  21. ***************************************************************************/
  22. #include "test.h"
  23. #include <fcntl.h>
  24. #include "testutil.h"
  25. #include "warnless.h"
  26. #include "memdebug.h"
  27. #define TEST_HANG_TIMEOUT 60 * 1000
  28. struct Sockets
  29. {
  30. curl_socket_t *sockets;
  31. int count; /* number of sockets actually stored in array */
  32. int max_count; /* max number of sockets that fit in allocated array */
  33. };
  34. struct ReadWriteSockets
  35. {
  36. struct Sockets read, write;
  37. };
  38. /**
  39. * Remove a file descriptor from a sockets array.
  40. */
  41. static void removeFd(struct Sockets* sockets, curl_socket_t fd, int mention)
  42. {
  43. int i;
  44. if(mention)
  45. fprintf(stderr, "Remove socket fd %d\n", (int) fd);
  46. for(i = 0; i < sockets->count; ++i) {
  47. if(sockets->sockets[i] == fd) {
  48. if(i < sockets->count - 1)
  49. memmove(&sockets->sockets[i], &sockets->sockets[i + 1],
  50. sizeof(curl_socket_t) * (sockets->count - (i + 1)));
  51. --sockets->count;
  52. }
  53. }
  54. }
  55. /**
  56. * Add a file descriptor to a sockets array.
  57. */
  58. static void addFd(struct Sockets* sockets, curl_socket_t fd, const char *what)
  59. {
  60. /**
  61. * To ensure we only have each file descriptor once, we remove it then add
  62. * it again.
  63. */
  64. fprintf(stderr, "Add socket fd %d for %s\n", (int) fd, what);
  65. removeFd(sockets, fd, 0);
  66. /*
  67. * Allocate array storage when required.
  68. */
  69. if(!sockets->sockets) {
  70. sockets->sockets = malloc(sizeof(curl_socket_t) * 20U);
  71. if(!sockets->sockets)
  72. return;
  73. sockets->max_count = 20;
  74. }
  75. else if(sockets->count + 1 > sockets->max_count) {
  76. curl_socket_t *oldptr = sockets->sockets;
  77. sockets->sockets = realloc(oldptr, sizeof(curl_socket_t) *
  78. (sockets->max_count + 20));
  79. if(!sockets->sockets) {
  80. /* cleanup in test_cleanup */
  81. sockets->sockets = oldptr;
  82. return;
  83. }
  84. sockets->max_count += 20;
  85. }
  86. /*
  87. * Add file descriptor to array.
  88. */
  89. sockets->sockets[sockets->count] = fd;
  90. ++sockets->count;
  91. }
  92. /**
  93. * Callback invoked by curl to poll reading / writing of a socket.
  94. */
  95. static int curlSocketCallback(CURL *easy, curl_socket_t s, int action,
  96. void *userp, void *socketp)
  97. {
  98. struct ReadWriteSockets* sockets = userp;
  99. (void)easy; /* unused */
  100. (void)socketp; /* unused */
  101. if(action == CURL_POLL_IN || action == CURL_POLL_INOUT)
  102. addFd(&sockets->read, s, "read");
  103. if(action == CURL_POLL_OUT || action == CURL_POLL_INOUT)
  104. addFd(&sockets->write, s, "write");
  105. if(action == CURL_POLL_REMOVE) {
  106. removeFd(&sockets->read, s, 1);
  107. removeFd(&sockets->write, s, 0);
  108. }
  109. return 0;
  110. }
  111. /**
  112. * Callback invoked by curl to set a timeout.
  113. */
  114. static int curlTimerCallback(CURLM *multi, long timeout_ms, void *userp)
  115. {
  116. struct timeval* timeout = userp;
  117. (void)multi; /* unused */
  118. if(timeout_ms != -1) {
  119. *timeout = tutil_tvnow();
  120. timeout->tv_usec += timeout_ms * 1000;
  121. }
  122. else {
  123. timeout->tv_sec = -1;
  124. }
  125. return 0;
  126. }
  127. /**
  128. * Check for curl completion.
  129. */
  130. static int checkForCompletion(CURLM *curl, int *success)
  131. {
  132. int numMessages;
  133. CURLMsg *message;
  134. int result = 0;
  135. *success = 0;
  136. while((message = curl_multi_info_read(curl, &numMessages)) != NULL) {
  137. if(message->msg == CURLMSG_DONE) {
  138. result = 1;
  139. if(message->data.result == CURLE_OK)
  140. *success = 1;
  141. else
  142. *success = 0;
  143. }
  144. else {
  145. fprintf(stderr, "Got an unexpected message from curl: %i\n",
  146. (int)message->msg);
  147. result = 1;
  148. *success = 0;
  149. }
  150. }
  151. return result;
  152. }
  153. static int getMicroSecondTimeout(struct timeval* timeout)
  154. {
  155. struct timeval now;
  156. ssize_t result;
  157. now = tutil_tvnow();
  158. result = (ssize_t)((timeout->tv_sec - now.tv_sec) * 1000000 +
  159. timeout->tv_usec - now.tv_usec);
  160. if(result < 0)
  161. result = 0;
  162. return curlx_sztosi(result);
  163. }
  164. /**
  165. * Update a fd_set with all of the sockets in use.
  166. */
  167. static void updateFdSet(struct Sockets* sockets, fd_set* fdset,
  168. curl_socket_t *maxFd)
  169. {
  170. int i;
  171. for(i = 0; i < sockets->count; ++i) {
  172. FD_SET(sockets->sockets[i], fdset);
  173. if(*maxFd < sockets->sockets[i] + 1) {
  174. *maxFd = sockets->sockets[i] + 1;
  175. }
  176. }
  177. }
  178. static void notifyCurl(CURLM *curl, curl_socket_t s, int evBitmask,
  179. const char *info)
  180. {
  181. int numhandles = 0;
  182. CURLMcode result = curl_multi_socket_action(curl, s, evBitmask, &numhandles);
  183. if(result != CURLM_OK) {
  184. fprintf(stderr, "Curl error on %s: %i (%s)\n",
  185. info, result, curl_multi_strerror(result));
  186. }
  187. }
  188. /**
  189. * Invoke curl when a file descriptor is set.
  190. */
  191. static void checkFdSet(CURLM *curl, struct Sockets *sockets, fd_set *fdset,
  192. int evBitmask, const char *name)
  193. {
  194. int i;
  195. for(i = 0; i < sockets->count; ++i) {
  196. if(FD_ISSET(sockets->sockets[i], fdset)) {
  197. notifyCurl(curl, sockets->sockets[i], evBitmask, name);
  198. }
  199. }
  200. }
  201. int test(char *URL)
  202. {
  203. int res = 0;
  204. CURL *curl = NULL;
  205. FILE *hd_src = NULL;
  206. int hd;
  207. int error;
  208. struct_stat file_info;
  209. CURLM *m = NULL;
  210. struct ReadWriteSockets sockets = {{NULL, 0, 0}, {NULL, 0, 0}};
  211. struct timeval timeout = {-1, 0};
  212. int success = 0;
  213. start_test_timing();
  214. if(!libtest_arg3) {
  215. fprintf(stderr, "Usage: lib582 [url] [filename] [username]\n");
  216. return TEST_ERR_USAGE;
  217. }
  218. hd_src = fopen(libtest_arg2, "rb");
  219. if(NULL == hd_src) {
  220. error = ERRNO;
  221. fprintf(stderr, "fopen() failed with error: %d (%s)\n",
  222. error, strerror(error));
  223. fprintf(stderr, "Error opening file: (%s)\n", libtest_arg2);
  224. return TEST_ERR_FOPEN;
  225. }
  226. /* get the file size of the local file */
  227. hd = fstat(fileno(hd_src), &file_info);
  228. if(hd == -1) {
  229. /* can't open file, bail out */
  230. error = ERRNO;
  231. fprintf(stderr, "fstat() failed with error: %d (%s)\n",
  232. error, strerror(error));
  233. fprintf(stderr, "ERROR: cannot open file (%s)\n", libtest_arg2);
  234. fclose(hd_src);
  235. return TEST_ERR_FSTAT;
  236. }
  237. fprintf(stderr, "Set to upload %d bytes\n", (int)file_info.st_size);
  238. res_global_init(CURL_GLOBAL_ALL);
  239. if(res) {
  240. fclose(hd_src);
  241. return res;
  242. }
  243. easy_init(curl);
  244. /* enable uploading */
  245. easy_setopt(curl, CURLOPT_UPLOAD, 1L);
  246. /* specify target */
  247. easy_setopt(curl, CURLOPT_URL, URL);
  248. /* go verbose */
  249. easy_setopt(curl, CURLOPT_VERBOSE, 1L);
  250. /* now specify which file to upload */
  251. easy_setopt(curl, CURLOPT_READDATA, hd_src);
  252. easy_setopt(curl, CURLOPT_USERPWD, libtest_arg3);
  253. easy_setopt(curl, CURLOPT_SSH_PUBLIC_KEYFILE, "curl_client_key.pub");
  254. easy_setopt(curl, CURLOPT_SSH_PRIVATE_KEYFILE, "curl_client_key");
  255. easy_setopt(curl, CURLOPT_INFILESIZE_LARGE, (curl_off_t)file_info.st_size);
  256. multi_init(m);
  257. multi_setopt(m, CURLMOPT_SOCKETFUNCTION, curlSocketCallback);
  258. multi_setopt(m, CURLMOPT_SOCKETDATA, &sockets);
  259. multi_setopt(m, CURLMOPT_TIMERFUNCTION, curlTimerCallback);
  260. multi_setopt(m, CURLMOPT_TIMERDATA, &timeout);
  261. multi_add_handle(m, curl);
  262. while(!checkForCompletion(m, &success)) {
  263. fd_set readSet, writeSet;
  264. curl_socket_t maxFd = 0;
  265. struct timeval tv = {10, 0};
  266. FD_ZERO(&readSet);
  267. FD_ZERO(&writeSet);
  268. updateFdSet(&sockets.read, &readSet, &maxFd);
  269. updateFdSet(&sockets.write, &writeSet, &maxFd);
  270. if(timeout.tv_sec != -1) {
  271. int usTimeout = getMicroSecondTimeout(&timeout);
  272. tv.tv_sec = usTimeout / 1000000;
  273. tv.tv_usec = usTimeout % 1000000;
  274. }
  275. else if(maxFd <= 0) {
  276. tv.tv_sec = 0;
  277. tv.tv_usec = 100000;
  278. }
  279. select_test((int)maxFd, &readSet, &writeSet, NULL, &tv);
  280. /* Check the sockets for reading / writing */
  281. checkFdSet(m, &sockets.read, &readSet, CURL_CSELECT_IN, "read");
  282. checkFdSet(m, &sockets.write, &writeSet, CURL_CSELECT_OUT, "write");
  283. if(timeout.tv_sec != -1 && getMicroSecondTimeout(&timeout) == 0) {
  284. /* Curl's timer has elapsed. */
  285. notifyCurl(m, CURL_SOCKET_TIMEOUT, 0, "timeout");
  286. }
  287. abort_on_test_timeout();
  288. }
  289. if(!success) {
  290. fprintf(stderr, "Error uploading file.\n");
  291. res = TEST_ERR_MAJOR_BAD;
  292. }
  293. test_cleanup:
  294. /* proper cleanup sequence - type PB */
  295. curl_multi_remove_handle(m, curl);
  296. curl_easy_cleanup(curl);
  297. curl_multi_cleanup(m);
  298. curl_global_cleanup();
  299. /* close the local file */
  300. fclose(hd_src);
  301. /* free local memory */
  302. free(sockets.read.sockets);
  303. free(sockets.write.sockets);
  304. return res;
  305. }