libntlmconnect.c 6.8 KB

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