2
0

libntlmconnect.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  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 counter[MAX_EASY_HANDLES];
  33. static CURL *easy[MAX_EASY_HANDLES];
  34. static curl_socket_t sockets[MAX_EASY_HANDLES];
  35. static CURLcode 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) - 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. CURLcode 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. for(;;) {
  107. struct timeval interval;
  108. fd_set fdread;
  109. fd_set fdwrite;
  110. fd_set fdexcep;
  111. long timeout = -99;
  112. int maxfd = -99;
  113. bool found_new_socket = FALSE;
  114. /* Start a new handle if we aren't at the max */
  115. if(state == ReadyForNewHandle) {
  116. easy_init(easy[num_handles]);
  117. if(num_handles % 3 == 2) {
  118. msnprintf(full_url, urllen, "%s0200", url);
  119. easy_setopt(easy[num_handles], CURLOPT_HTTPAUTH, CURLAUTH_NTLM);
  120. }
  121. else {
  122. msnprintf(full_url, urllen, "%s0100", url);
  123. easy_setopt(easy[num_handles], CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
  124. }
  125. easy_setopt(easy[num_handles], CURLOPT_FRESH_CONNECT, 1L);
  126. easy_setopt(easy[num_handles], CURLOPT_URL, full_url);
  127. easy_setopt(easy[num_handles], CURLOPT_VERBOSE, 1L);
  128. easy_setopt(easy[num_handles], CURLOPT_HTTPGET, 1L);
  129. easy_setopt(easy[num_handles], CURLOPT_USERPWD, "testuser:testpass");
  130. easy_setopt(easy[num_handles], CURLOPT_WRITEFUNCTION, callback);
  131. easy_setopt(easy[num_handles], CURLOPT_WRITEDATA, easy + num_handles);
  132. easy_setopt(easy[num_handles], CURLOPT_HEADER, 1L);
  133. multi_add_handle(multi, easy[num_handles]);
  134. num_handles += 1;
  135. state = NeedSocketForNewHandle;
  136. }
  137. multi_perform(multi, &running);
  138. fprintf(stderr, "%s:%d running %d state %d\n",
  139. __FILE__, __LINE__, running, state);
  140. abort_on_test_timeout();
  141. if(!running && state == NoMoreHandles)
  142. break; /* done */
  143. FD_ZERO(&fdread);
  144. FD_ZERO(&fdwrite);
  145. FD_ZERO(&fdexcep);
  146. multi_fdset(multi, &fdread, &fdwrite, &fdexcep, &maxfd);
  147. /* At this point, maxfd is guaranteed to be greater or equal than -1. */
  148. if(state == NeedSocketForNewHandle) {
  149. if(maxfd != -1 && !found_new_socket) {
  150. fprintf(stderr, "Warning: socket did not open immediately for new "
  151. "handle (trying again)\n");
  152. continue;
  153. }
  154. state = num_handles < MAX_EASY_HANDLES ? ReadyForNewHandle
  155. : NoMoreHandles;
  156. fprintf(stderr, "%s:%d new state %d\n",
  157. __FILE__, __LINE__, state);
  158. }
  159. multi_timeout(multi, &timeout);
  160. /* At this point, timeout is guaranteed to be greater or equal than -1. */
  161. fprintf(stderr, "%s:%d num_handles %d timeout %ld running %d\n",
  162. __FILE__, __LINE__, num_handles, timeout, running);
  163. if(timeout != -1L) {
  164. int itimeout;
  165. #if LONG_MAX > INT_MAX
  166. itimeout = (timeout > (long)INT_MAX) ? INT_MAX : (int)timeout;
  167. #else
  168. itimeout = (int)timeout;
  169. #endif
  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. }