lib582.c 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364
  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. #if defined(__DJGPP__)
  169. #pragma GCC diagnostic push
  170. #pragma GCC diagnostic ignored "-Warith-conversion"
  171. #endif
  172. FD_SET(sockets->sockets[i], fdset);
  173. #if defined(__DJGPP__)
  174. #pragma GCC diagnostic pop
  175. #endif
  176. if(*maxFd < sockets->sockets[i] + 1) {
  177. *maxFd = sockets->sockets[i] + 1;
  178. }
  179. }
  180. }
  181. static void notifyCurl(CURLM *curl, curl_socket_t s, int evBitmask,
  182. const char *info)
  183. {
  184. int numhandles = 0;
  185. CURLMcode result = curl_multi_socket_action(curl, s, evBitmask, &numhandles);
  186. if(result != CURLM_OK) {
  187. fprintf(stderr, "Curl error on %s: %i (%s)\n",
  188. info, result, curl_multi_strerror(result));
  189. }
  190. }
  191. /**
  192. * Invoke curl when a file descriptor is set.
  193. */
  194. static void checkFdSet(CURLM *curl, struct Sockets *sockets, fd_set *fdset,
  195. int evBitmask, const char *name)
  196. {
  197. int i;
  198. for(i = 0; i < sockets->count; ++i) {
  199. if(FD_ISSET(sockets->sockets[i], fdset)) {
  200. notifyCurl(curl, sockets->sockets[i], evBitmask, name);
  201. }
  202. }
  203. }
  204. CURLcode test(char *URL)
  205. {
  206. CURLcode res = CURLE_OK;
  207. CURL *curl = NULL;
  208. FILE *hd_src = NULL;
  209. int hd;
  210. struct_stat file_info;
  211. CURLM *m = NULL;
  212. struct ReadWriteSockets sockets = {{NULL, 0, 0}, {NULL, 0, 0}};
  213. int success = 0;
  214. struct timeval timeout = {0};
  215. timeout.tv_sec = (time_t)-1;
  216. assert(test_argc >= 5);
  217. start_test_timing();
  218. if(!libtest_arg3) {
  219. fprintf(stderr, "Usage: lib582 [url] [filename] [username]\n");
  220. return TEST_ERR_USAGE;
  221. }
  222. hd_src = fopen(libtest_arg2, "rb");
  223. if(!hd_src) {
  224. fprintf(stderr, "fopen() failed with error: %d (%s)\n",
  225. errno, strerror(errno));
  226. fprintf(stderr, "Error opening file: (%s)\n", libtest_arg2);
  227. return TEST_ERR_FOPEN;
  228. }
  229. /* get the file size of the local file */
  230. hd = fstat(fileno(hd_src), &file_info);
  231. if(hd == -1) {
  232. /* can't open file, bail out */
  233. fprintf(stderr, "fstat() failed with error: %d (%s)\n",
  234. errno, strerror(errno));
  235. fprintf(stderr, "ERROR: cannot open file (%s)\n", libtest_arg2);
  236. fclose(hd_src);
  237. return TEST_ERR_FSTAT;
  238. }
  239. fprintf(stderr, "Set to upload %d bytes\n", (int)file_info.st_size);
  240. res_global_init(CURL_GLOBAL_ALL);
  241. if(res) {
  242. fclose(hd_src);
  243. return res;
  244. }
  245. easy_init(curl);
  246. /* enable uploading */
  247. easy_setopt(curl, CURLOPT_UPLOAD, 1L);
  248. /* specify target */
  249. easy_setopt(curl, CURLOPT_URL, URL);
  250. /* go verbose */
  251. easy_setopt(curl, CURLOPT_VERBOSE, 1L);
  252. /* now specify which file to upload */
  253. easy_setopt(curl, CURLOPT_READDATA, hd_src);
  254. easy_setopt(curl, CURLOPT_USERPWD, libtest_arg3);
  255. easy_setopt(curl, CURLOPT_SSH_PUBLIC_KEYFILE, test_argv[4]);
  256. easy_setopt(curl, CURLOPT_SSH_PRIVATE_KEYFILE, test_argv[5]);
  257. easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, 0L);
  258. easy_setopt(curl, CURLOPT_INFILESIZE_LARGE, (curl_off_t)file_info.st_size);
  259. multi_init(m);
  260. multi_setopt(m, CURLMOPT_SOCKETFUNCTION, curlSocketCallback);
  261. multi_setopt(m, CURLMOPT_SOCKETDATA, &sockets);
  262. multi_setopt(m, CURLMOPT_TIMERFUNCTION, curlTimerCallback);
  263. multi_setopt(m, CURLMOPT_TIMERDATA, &timeout);
  264. multi_add_handle(m, curl);
  265. while(!checkForCompletion(m, &success)) {
  266. fd_set readSet, writeSet;
  267. curl_socket_t maxFd = 0;
  268. struct timeval tv = {0};
  269. tv.tv_sec = 10;
  270. FD_ZERO(&readSet);
  271. FD_ZERO(&writeSet);
  272. updateFdSet(&sockets.read, &readSet, &maxFd);
  273. updateFdSet(&sockets.write, &writeSet, &maxFd);
  274. if(timeout.tv_sec != (time_t)-1) {
  275. int usTimeout = getMicroSecondTimeout(&timeout);
  276. tv.tv_sec = usTimeout / 1000000;
  277. tv.tv_usec = usTimeout % 1000000;
  278. }
  279. else if(maxFd <= 0) {
  280. tv.tv_sec = 0;
  281. tv.tv_usec = 100000;
  282. }
  283. select_test((int)maxFd, &readSet, &writeSet, NULL, &tv);
  284. /* Check the sockets for reading / writing */
  285. checkFdSet(m, &sockets.read, &readSet, CURL_CSELECT_IN, "read");
  286. checkFdSet(m, &sockets.write, &writeSet, CURL_CSELECT_OUT, "write");
  287. if(timeout.tv_sec != (time_t)-1 && getMicroSecondTimeout(&timeout) == 0) {
  288. /* Curl's timer has elapsed. */
  289. notifyCurl(m, CURL_SOCKET_TIMEOUT, 0, "timeout");
  290. }
  291. abort_on_test_timeout();
  292. }
  293. if(!success) {
  294. fprintf(stderr, "Error uploading file.\n");
  295. res = TEST_ERR_MAJOR_BAD;
  296. }
  297. test_cleanup:
  298. /* proper cleanup sequence - type PB */
  299. curl_multi_remove_handle(m, curl);
  300. curl_easy_cleanup(curl);
  301. curl_multi_cleanup(m);
  302. curl_global_cleanup();
  303. /* close the local file */
  304. fclose(hd_src);
  305. /* free local memory */
  306. free(sockets.read.sockets);
  307. free(sockets.write.sockets);
  308. return res;
  309. }