libntlmconnect.c 6.8 KB

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