2
0

lib582.c 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) 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.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. * SPDX-License-Identifier: curl
  22. *
  23. ***************************************************************************/
  24. #include "test.h"
  25. #include <fcntl.h>
  26. #include "testutil.h"
  27. #include "warnless.h"
  28. #include "memdebug.h"
  29. #define TEST_HANG_TIMEOUT 60 * 1000
  30. struct Sockets {
  31. curl_socket_t *sockets;
  32. int count; /* number of sockets actually stored in array */
  33. int max_count; /* max number of sockets that fit in allocated array */
  34. };
  35. struct ReadWriteSockets {
  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 >= sockets->max_count) {
  76. /* this can't happen in normal cases */
  77. fprintf(stderr, "too many file handles error\n");
  78. exit(2);
  79. }
  80. /*
  81. * Add file descriptor to array.
  82. */
  83. sockets->sockets[sockets->count] = fd;
  84. ++sockets->count;
  85. }
  86. /**
  87. * Callback invoked by curl to poll reading / writing of a socket.
  88. */
  89. static int curlSocketCallback(CURL *easy, curl_socket_t s, int action,
  90. void *userp, void *socketp)
  91. {
  92. struct ReadWriteSockets *sockets = userp;
  93. (void)easy; /* unused */
  94. (void)socketp; /* unused */
  95. if(action == CURL_POLL_IN || action == CURL_POLL_INOUT)
  96. addFd(&sockets->read, s, "read");
  97. if(action == CURL_POLL_OUT || action == CURL_POLL_INOUT)
  98. addFd(&sockets->write, s, "write");
  99. if(action == CURL_POLL_REMOVE) {
  100. removeFd(&sockets->read, s, 1);
  101. removeFd(&sockets->write, s, 0);
  102. }
  103. return 0;
  104. }
  105. /**
  106. * Callback invoked by curl to set a timeout.
  107. */
  108. static int curlTimerCallback(CURLM *multi, long timeout_ms, void *userp)
  109. {
  110. struct timeval *timeout = userp;
  111. (void)multi; /* unused */
  112. if(timeout_ms != -1) {
  113. *timeout = tutil_tvnow();
  114. timeout->tv_usec += (int)timeout_ms * 1000;
  115. }
  116. else {
  117. timeout->tv_sec = -1;
  118. }
  119. return 0;
  120. }
  121. /**
  122. * Check for curl completion.
  123. */
  124. static int checkForCompletion(CURLM *curl, int *success)
  125. {
  126. int result = 0;
  127. *success = 0;
  128. while(1) {
  129. int numMessages;
  130. CURLMsg *message = curl_multi_info_read(curl, &numMessages);
  131. if(!message)
  132. break;
  133. if(message->msg == CURLMSG_DONE) {
  134. result = 1;
  135. if(message->data.result == CURLE_OK)
  136. *success = 1;
  137. else
  138. *success = 0;
  139. }
  140. else {
  141. fprintf(stderr, "Got an unexpected message from curl: %i\n",
  142. (int)message->msg);
  143. result = 1;
  144. *success = 0;
  145. }
  146. }
  147. return result;
  148. }
  149. static int getMicroSecondTimeout(struct timeval *timeout)
  150. {
  151. struct timeval now;
  152. ssize_t result;
  153. now = tutil_tvnow();
  154. result = (ssize_t)((timeout->tv_sec - now.tv_sec) * 1000000 +
  155. timeout->tv_usec - now.tv_usec);
  156. if(result < 0)
  157. result = 0;
  158. return curlx_sztosi(result);
  159. }
  160. /**
  161. * Update a fd_set with all of the sockets in use.
  162. */
  163. static void updateFdSet(struct Sockets *sockets, fd_set* fdset,
  164. curl_socket_t *maxFd)
  165. {
  166. int i;
  167. for(i = 0; i < sockets->count; ++i) {
  168. FD_SET(sockets->sockets[i], fdset);
  169. if(*maxFd < sockets->sockets[i] + 1) {
  170. *maxFd = sockets->sockets[i] + 1;
  171. }
  172. }
  173. }
  174. static void notifyCurl(CURLM *curl, curl_socket_t s, int evBitmask,
  175. const char *info)
  176. {
  177. int numhandles = 0;
  178. CURLMcode result = curl_multi_socket_action(curl, s, evBitmask, &numhandles);
  179. if(result != CURLM_OK) {
  180. fprintf(stderr, "Curl error on %s: %i (%s)\n",
  181. info, result, curl_multi_strerror(result));
  182. }
  183. }
  184. /**
  185. * Invoke curl when a file descriptor is set.
  186. */
  187. static void checkFdSet(CURLM *curl, struct Sockets *sockets, fd_set *fdset,
  188. int evBitmask, const char *name)
  189. {
  190. int i;
  191. for(i = 0; i < sockets->count; ++i) {
  192. if(FD_ISSET(sockets->sockets[i], fdset)) {
  193. notifyCurl(curl, sockets->sockets[i], evBitmask, name);
  194. }
  195. }
  196. }
  197. CURLcode test(char *URL)
  198. {
  199. CURLcode res = CURLE_OK;
  200. CURL *curl = NULL;
  201. FILE *hd_src = NULL;
  202. int hd;
  203. struct_stat file_info;
  204. CURLM *m = NULL;
  205. struct ReadWriteSockets sockets = {{NULL, 0, 0}, {NULL, 0, 0}};
  206. struct timeval timeout = {-1, 0};
  207. int success = 0;
  208. assert(test_argc >= 5);
  209. start_test_timing();
  210. if(!libtest_arg3) {
  211. fprintf(stderr, "Usage: lib582 [url] [filename] [username]\n");
  212. return TEST_ERR_USAGE;
  213. }
  214. hd_src = fopen(libtest_arg2, "rb");
  215. if(!hd_src) {
  216. fprintf(stderr, "fopen() failed with error: %d (%s)\n",
  217. errno, strerror(errno));
  218. fprintf(stderr, "Error opening file: (%s)\n", libtest_arg2);
  219. return TEST_ERR_FOPEN;
  220. }
  221. /* get the file size of the local file */
  222. hd = fstat(fileno(hd_src), &file_info);
  223. if(hd == -1) {
  224. /* can't open file, bail out */
  225. fprintf(stderr, "fstat() failed with error: %d (%s)\n",
  226. errno, strerror(errno));
  227. fprintf(stderr, "ERROR: cannot open file (%s)\n", libtest_arg2);
  228. fclose(hd_src);
  229. return TEST_ERR_FSTAT;
  230. }
  231. fprintf(stderr, "Set to upload %d bytes\n", (int)file_info.st_size);
  232. res_global_init(CURL_GLOBAL_ALL);
  233. if(res) {
  234. fclose(hd_src);
  235. return res;
  236. }
  237. easy_init(curl);
  238. /* enable uploading */
  239. easy_setopt(curl, CURLOPT_UPLOAD, 1L);
  240. /* specify target */
  241. easy_setopt(curl, CURLOPT_URL, URL);
  242. /* go verbose */
  243. easy_setopt(curl, CURLOPT_VERBOSE, 1L);
  244. /* now specify which file to upload */
  245. easy_setopt(curl, CURLOPT_READDATA, hd_src);
  246. easy_setopt(curl, CURLOPT_USERPWD, libtest_arg3);
  247. easy_setopt(curl, CURLOPT_SSH_PUBLIC_KEYFILE, test_argv[4]);
  248. easy_setopt(curl, CURLOPT_SSH_PRIVATE_KEYFILE, test_argv[5]);
  249. easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, 0L);
  250. easy_setopt(curl, CURLOPT_INFILESIZE_LARGE, (curl_off_t)file_info.st_size);
  251. multi_init(m);
  252. multi_setopt(m, CURLMOPT_SOCKETFUNCTION, curlSocketCallback);
  253. multi_setopt(m, CURLMOPT_SOCKETDATA, &sockets);
  254. multi_setopt(m, CURLMOPT_TIMERFUNCTION, curlTimerCallback);
  255. multi_setopt(m, CURLMOPT_TIMERDATA, &timeout);
  256. multi_add_handle(m, curl);
  257. while(!checkForCompletion(m, &success)) {
  258. fd_set readSet, writeSet;
  259. curl_socket_t maxFd = 0;
  260. struct timeval tv = {10, 0};
  261. FD_ZERO(&readSet);
  262. FD_ZERO(&writeSet);
  263. updateFdSet(&sockets.read, &readSet, &maxFd);
  264. updateFdSet(&sockets.write, &writeSet, &maxFd);
  265. if(timeout.tv_sec != -1) {
  266. int usTimeout = getMicroSecondTimeout(&timeout);
  267. tv.tv_sec = usTimeout / 1000000;
  268. tv.tv_usec = usTimeout % 1000000;
  269. }
  270. else if(maxFd <= 0) {
  271. tv.tv_sec = 0;
  272. tv.tv_usec = 100000;
  273. }
  274. select_test((int)maxFd, &readSet, &writeSet, NULL, &tv);
  275. /* Check the sockets for reading / writing */
  276. checkFdSet(m, &sockets.read, &readSet, CURL_CSELECT_IN, "read");
  277. checkFdSet(m, &sockets.write, &writeSet, CURL_CSELECT_OUT, "write");
  278. if(timeout.tv_sec != -1 && getMicroSecondTimeout(&timeout) == 0) {
  279. /* Curl's timer has elapsed. */
  280. notifyCurl(m, CURL_SOCKET_TIMEOUT, 0, "timeout");
  281. }
  282. abort_on_test_timeout();
  283. }
  284. if(!success) {
  285. fprintf(stderr, "Error uploading file.\n");
  286. res = TEST_ERR_MAJOR_BAD;
  287. }
  288. test_cleanup:
  289. /* proper cleanup sequence - type PB */
  290. curl_multi_remove_handle(m, curl);
  291. curl_easy_cleanup(curl);
  292. curl_multi_cleanup(m);
  293. curl_global_cleanup();
  294. /* close the local file */
  295. fclose(hd_src);
  296. /* free local memory */
  297. free(sockets.read.sockets);
  298. free(sockets.write.sockets);
  299. return res;
  300. }