libntlmconnect.c 8.2 KB

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