2
0

lib582.c 9.0 KB

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