lib582.c 9.2 KB

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