2
0

libntlmconnect.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) 2012 - 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 <limits.h>
  26. #include <assert.h>
  27. #include "testutil.h"
  28. #include "warnless.h"
  29. #include "memdebug.h"
  30. #define TEST_HANG_TIMEOUT 5 * 1000
  31. #define MAX_EASY_HANDLES 3
  32. static int counter[MAX_EASY_HANDLES];
  33. static CURL *easy[MAX_EASY_HANDLES];
  34. static curl_socket_t sockets[MAX_EASY_HANDLES];
  35. static int res = 0;
  36. static size_t callback(char *ptr, size_t size, size_t nmemb, void *data)
  37. {
  38. ssize_t idx = ((CURL **) data) - easy;
  39. curl_socket_t sock;
  40. long longdata;
  41. CURLcode code;
  42. const size_t failure = (size && nmemb) ? 0 : 1;
  43. (void)ptr;
  44. counter[idx] += (int)(size * nmemb);
  45. /* Get socket being used for this easy handle, otherwise CURL_SOCKET_BAD */
  46. code = curl_easy_getinfo(easy[idx], CURLINFO_LASTSOCKET, &longdata);
  47. if(CURLE_OK != code) {
  48. fprintf(stderr, "%s:%d curl_easy_getinfo() failed, "
  49. "with code %d (%s)\n",
  50. __FILE__, __LINE__, (int)code, curl_easy_strerror(code));
  51. res = TEST_ERR_MAJOR_BAD;
  52. return failure;
  53. }
  54. if(longdata == -1L)
  55. sock = CURL_SOCKET_BAD;
  56. else
  57. sock = (curl_socket_t)longdata;
  58. if(sock != CURL_SOCKET_BAD) {
  59. /* Track relationship between this easy handle and the socket. */
  60. if(sockets[idx] == CURL_SOCKET_BAD) {
  61. /* An easy handle without previous socket, record the socket. */
  62. sockets[idx] = sock;
  63. }
  64. else if(sock != sockets[idx]) {
  65. /* An easy handle with a socket different to previously
  66. tracked one, log and fail right away. Known bug #37. */
  67. fprintf(stderr, "Handle %d started on socket %d and moved to %d\n",
  68. curlx_sztosi(idx), (int)sockets[idx], (int)sock);
  69. res = TEST_ERR_MAJOR_BAD;
  70. return failure;
  71. }
  72. }
  73. return size * nmemb;
  74. }
  75. enum HandleState {
  76. ReadyForNewHandle,
  77. NeedSocketForNewHandle,
  78. NoMoreHandles
  79. };
  80. int test(char *url)
  81. {
  82. CURLM *multi = NULL;
  83. int running;
  84. int i;
  85. int num_handles = 0;
  86. enum HandleState state = ReadyForNewHandle;
  87. size_t urllen = strlen(url) + 4 + 1;
  88. char *full_url = malloc(urllen);
  89. start_test_timing();
  90. if(!full_url) {
  91. fprintf(stderr, "Not enough memory for full url\n");
  92. return TEST_ERR_MAJOR_BAD;
  93. }
  94. for(i = 0; i < MAX_EASY_HANDLES; ++i) {
  95. easy[i] = NULL;
  96. sockets[i] = CURL_SOCKET_BAD;
  97. }
  98. res_global_init(CURL_GLOBAL_ALL);
  99. if(res) {
  100. free(full_url);
  101. return res;
  102. }
  103. multi_init(multi);
  104. #ifdef USE_PIPELINING
  105. multi_setopt(multi, CURLMOPT_PIPELINING, 1L);
  106. multi_setopt(multi, CURLMOPT_MAX_HOST_CONNECTIONS, 5L);
  107. multi_setopt(multi, CURLMOPT_MAX_TOTAL_CONNECTIONS, 10L);
  108. #endif
  109. for(;;) {
  110. struct timeval interval;
  111. fd_set fdread;
  112. fd_set fdwrite;
  113. fd_set fdexcep;
  114. long timeout = -99;
  115. int maxfd = -99;
  116. bool found_new_socket = FALSE;
  117. /* Start a new handle if we aren't at the max */
  118. if(state == ReadyForNewHandle) {
  119. easy_init(easy[num_handles]);
  120. if(num_handles % 3 == 2) {
  121. msnprintf(full_url, urllen, "%s0200", url);
  122. easy_setopt(easy[num_handles], CURLOPT_HTTPAUTH, CURLAUTH_NTLM);
  123. }
  124. else {
  125. msnprintf(full_url, urllen, "%s0100", url);
  126. easy_setopt(easy[num_handles], CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
  127. }
  128. easy_setopt(easy[num_handles], CURLOPT_FRESH_CONNECT, 1L);
  129. easy_setopt(easy[num_handles], CURLOPT_URL, full_url);
  130. easy_setopt(easy[num_handles], CURLOPT_VERBOSE, 1L);
  131. easy_setopt(easy[num_handles], CURLOPT_HTTPGET, 1L);
  132. easy_setopt(easy[num_handles], CURLOPT_USERPWD, "testuser:testpass");
  133. easy_setopt(easy[num_handles], CURLOPT_WRITEFUNCTION, callback);
  134. easy_setopt(easy[num_handles], CURLOPT_WRITEDATA, easy + num_handles);
  135. easy_setopt(easy[num_handles], CURLOPT_HEADER, 1L);
  136. multi_add_handle(multi, easy[num_handles]);
  137. num_handles += 1;
  138. state = NeedSocketForNewHandle;
  139. }
  140. multi_perform(multi, &running);
  141. fprintf(stderr, "%s:%d running %d state %d\n",
  142. __FILE__, __LINE__, running, state);
  143. abort_on_test_timeout();
  144. if(!running && state == NoMoreHandles)
  145. break; /* done */
  146. FD_ZERO(&fdread);
  147. FD_ZERO(&fdwrite);
  148. FD_ZERO(&fdexcep);
  149. multi_fdset(multi, &fdread, &fdwrite, &fdexcep, &maxfd);
  150. /* At this point, maxfd is guaranteed to be greater or equal than -1. */
  151. if(state == NeedSocketForNewHandle) {
  152. if(maxfd != -1 && !found_new_socket) {
  153. fprintf(stderr, "Warning: socket did not open immediately for new "
  154. "handle (trying again)\n");
  155. continue;
  156. }
  157. state = num_handles < MAX_EASY_HANDLES ? ReadyForNewHandle
  158. : NoMoreHandles;
  159. fprintf(stderr, "%s:%d new state %d\n",
  160. __FILE__, __LINE__, state);
  161. }
  162. multi_timeout(multi, &timeout);
  163. /* At this point, timeout is guaranteed to be greater or equal than -1. */
  164. fprintf(stderr, "%s:%d num_handles %d timeout %ld running %d\n",
  165. __FILE__, __LINE__, num_handles, timeout, running);
  166. if(timeout != -1L) {
  167. int itimeout = (timeout > (long)INT_MAX) ? INT_MAX : (int)timeout;
  168. interval.tv_sec = itimeout/1000;
  169. interval.tv_usec = (itimeout%1000)*1000;
  170. }
  171. else {
  172. interval.tv_sec = 0;
  173. interval.tv_usec = 5000;
  174. /* if there's no timeout and we get here on the last handle, we may
  175. already have read the last part of the stream so waiting makes no
  176. sense */
  177. if(!running && num_handles == MAX_EASY_HANDLES) {
  178. break;
  179. }
  180. }
  181. select_test(maxfd + 1, &fdread, &fdwrite, &fdexcep, &interval);
  182. abort_on_test_timeout();
  183. }
  184. test_cleanup:
  185. /* proper cleanup sequence - type PB */
  186. for(i = 0; i < MAX_EASY_HANDLES; i++) {
  187. printf("Data connection %d: %d\n", i, counter[i]);
  188. curl_multi_remove_handle(multi, easy[i]);
  189. curl_easy_cleanup(easy[i]);
  190. }
  191. curl_multi_cleanup(multi);
  192. curl_global_cleanup();
  193. free(full_url);
  194. return res;
  195. }