2
0

libntlmconnect.c 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  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 60 * 1000
  31. #define MAX_EASY_HANDLES 3
  32. static int ntlm_counter[MAX_EASY_HANDLES];
  33. static CURL *ntlm_easy[MAX_EASY_HANDLES];
  34. static curl_socket_t ntlm_sockets[MAX_EASY_HANDLES];
  35. static CURLcode ntlmcb_res = CURLE_OK;
  36. static size_t callback(char *ptr, size_t size, size_t nmemb, void *data)
  37. {
  38. ssize_t idx = ((CURL **) data) - ntlm_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. ntlm_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(ntlm_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. ntlmcb_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(ntlm_sockets[idx] == CURL_SOCKET_BAD) {
  63. /* An easy handle without previous socket, record the socket. */
  64. ntlm_sockets[idx] = sock;
  65. }
  66. else if(sock != ntlm_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)ntlm_sockets[idx], (int)sock);
  71. ntlmcb_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. CURLcode test(char *url)
  83. {
  84. CURLcode res = CURLE_OK;
  85. CURLM *multi = NULL;
  86. int running;
  87. int i;
  88. int num_handles = 0;
  89. enum HandleState state = ReadyForNewHandle;
  90. size_t urllen = strlen(url) + 4 + 1;
  91. char *full_url = malloc(urllen);
  92. start_test_timing();
  93. if(!full_url) {
  94. fprintf(stderr, "Not enough memory for full url\n");
  95. return TEST_ERR_MAJOR_BAD;
  96. }
  97. for(i = 0; i < MAX_EASY_HANDLES; ++i) {
  98. ntlm_easy[i] = NULL;
  99. ntlm_sockets[i] = CURL_SOCKET_BAD;
  100. }
  101. res_global_init(CURL_GLOBAL_ALL);
  102. if(res) {
  103. free(full_url);
  104. return res;
  105. }
  106. multi_init(multi);
  107. for(;;) {
  108. struct timeval interval;
  109. fd_set fdread;
  110. fd_set fdwrite;
  111. fd_set fdexcep;
  112. long timeout = -99;
  113. int maxfd = -99;
  114. bool found_new_socket = FALSE;
  115. /* Start a new handle if we aren't at the max */
  116. if(state == ReadyForNewHandle) {
  117. easy_init(ntlm_easy[num_handles]);
  118. if(num_handles % 3 == 2) {
  119. msnprintf(full_url, urllen, "%s0200", url);
  120. easy_setopt(ntlm_easy[num_handles], CURLOPT_HTTPAUTH, CURLAUTH_NTLM);
  121. }
  122. else {
  123. msnprintf(full_url, urllen, "%s0100", url);
  124. easy_setopt(ntlm_easy[num_handles], CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
  125. }
  126. easy_setopt(ntlm_easy[num_handles], CURLOPT_FRESH_CONNECT, 1L);
  127. easy_setopt(ntlm_easy[num_handles], CURLOPT_URL, full_url);
  128. easy_setopt(ntlm_easy[num_handles], CURLOPT_VERBOSE, 1L);
  129. easy_setopt(ntlm_easy[num_handles], CURLOPT_HTTPGET, 1L);
  130. easy_setopt(ntlm_easy[num_handles], CURLOPT_USERPWD,
  131. "testuser:testpass");
  132. easy_setopt(ntlm_easy[num_handles], CURLOPT_WRITEFUNCTION, callback);
  133. easy_setopt(ntlm_easy[num_handles], CURLOPT_WRITEDATA,
  134. ntlm_easy + num_handles);
  135. easy_setopt(ntlm_easy[num_handles], CURLOPT_HEADER, 1L);
  136. multi_add_handle(multi, ntlm_easy[num_handles]);
  137. num_handles += 1;
  138. state = NeedSocketForNewHandle;
  139. res = ntlmcb_res;
  140. }
  141. multi_perform(multi, &running);
  142. fprintf(stderr, "%s:%d running %d state %d\n",
  143. __FILE__, __LINE__, running, state);
  144. abort_on_test_timeout();
  145. if(!running && state == NoMoreHandles)
  146. break; /* done */
  147. FD_ZERO(&fdread);
  148. FD_ZERO(&fdwrite);
  149. FD_ZERO(&fdexcep);
  150. multi_fdset(multi, &fdread, &fdwrite, &fdexcep, &maxfd);
  151. /* At this point, maxfd is guaranteed to be greater or equal than -1. */
  152. if(state == NeedSocketForNewHandle) {
  153. if(maxfd != -1 && !found_new_socket) {
  154. fprintf(stderr, "Warning: socket did not open immediately for new "
  155. "handle (trying again)\n");
  156. continue;
  157. }
  158. state = num_handles < MAX_EASY_HANDLES ? ReadyForNewHandle
  159. : NoMoreHandles;
  160. fprintf(stderr, "%s:%d new state %d\n",
  161. __FILE__, __LINE__, state);
  162. }
  163. multi_timeout(multi, &timeout);
  164. /* At this point, timeout is guaranteed to be greater or equal than -1. */
  165. fprintf(stderr, "%s:%d num_handles %d timeout %ld running %d\n",
  166. __FILE__, __LINE__, num_handles, timeout, running);
  167. if(timeout != -1L) {
  168. int itimeout;
  169. #if LONG_MAX > INT_MAX
  170. itimeout = (timeout > (long)INT_MAX) ? INT_MAX : (int)timeout;
  171. #else
  172. itimeout = (int)timeout;
  173. #endif
  174. interval.tv_sec = itimeout/1000;
  175. interval.tv_usec = (itimeout%1000)*1000;
  176. }
  177. else {
  178. interval.tv_sec = 0;
  179. interval.tv_usec = 5000;
  180. /* if there's no timeout and we get here on the last handle, we may
  181. already have read the last part of the stream so waiting makes no
  182. sense */
  183. if(!running && num_handles == MAX_EASY_HANDLES) {
  184. break;
  185. }
  186. }
  187. select_test(maxfd + 1, &fdread, &fdwrite, &fdexcep, &interval);
  188. abort_on_test_timeout();
  189. }
  190. test_cleanup:
  191. /* proper cleanup sequence - type PB */
  192. for(i = 0; i < MAX_EASY_HANDLES; i++) {
  193. printf("Data connection %d: %d\n", i, ntlm_counter[i]);
  194. curl_multi_remove_handle(multi, ntlm_easy[i]);
  195. curl_easy_cleanup(ntlm_easy[i]);
  196. }
  197. curl_multi_cleanup(multi);
  198. curl_global_cleanup();
  199. free(full_url);
  200. return res;
  201. }