libntlmconnect.c
Go to the documentation of this file.
1 /***************************************************************************
2  * _ _ ____ _
3  * Project ___| | | | _ \| |
4  * / __| | | | |_) | |
5  * | (__| |_| | _ <| |___
6  * \___|\___/|_| \_\_____|
7  *
8  * Copyright (C) 2012 - 2017, 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.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 
24 #ifdef HAVE_LIMITS_H
25 #include <limits.h>
26 #endif
27 #include <assert.h>
28 
29 #include "testutil.h"
30 #include "warnless.h"
31 #include "memdebug.h"
32 
33 #define TEST_HANG_TIMEOUT 5 * 1000
34 #define MAX_EASY_HANDLES 3
35 
39 static int res = 0;
40 
41 static size_t callback(char *ptr, size_t size, size_t nmemb, void *data)
42 {
43  ssize_t idx = ((CURL **) data) - easy;
44  curl_socket_t sock;
45  long longdata;
46  CURLcode code;
47  const size_t failure = (size && nmemb) ? 0 : 1;
48  (void)ptr;
49 
50  counter[idx] += (int)(size * nmemb);
51 
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));
59  return failure;
60  }
61  if(longdata == -1L)
62  sock = CURL_SOCKET_BAD;
63  else
64  sock = (curl_socket_t)longdata;
65 
66  if(sock != CURL_SOCKET_BAD) {
67  /* Track relationship between this easy handle and the socket. */
68  if(sockets[idx] == CURL_SOCKET_BAD) {
69  /* An easy handle without previous socket, record the socket. */
70  sockets[idx] = sock;
71  }
72  else if(sock != sockets[idx]) {
73  /* An easy handle with a socket different to previously
74  tracked one, log and fail right away. Known bug #37. */
75  fprintf(stderr, "Handle %d started on socket %d and moved to %d\n",
76  curlx_sztosi(idx), (int)sockets[idx], (int)sock);
78  return failure;
79  }
80  }
81  return size * nmemb;
82 }
83 
88 };
89 
90 int test(char *url)
91 {
92  CURLM *multi = NULL;
93  int running;
94  int i;
95  int num_handles = 0;
97  size_t urllen = strlen(url) + 4 + 1;
98  char *full_url = malloc(urllen);
99 
101 
102  if(!full_url) {
103  fprintf(stderr, "Not enough memory for full url\n");
104  return TEST_ERR_MAJOR_BAD;
105  }
106 
107  for(i = 0; i < MAX_EASY_HANDLES; ++i) {
108  easy[i] = NULL;
110  }
111 
113  if(res) {
114  free(full_url);
115  return res;
116  }
117 
118  multi_init(multi);
119 
120 #ifdef USE_PIPELINING
121  multi_setopt(multi, CURLMOPT_PIPELINING, 1L);
122  multi_setopt(multi, CURLMOPT_MAX_HOST_CONNECTIONS, 5L);
123  multi_setopt(multi, CURLMOPT_MAX_TOTAL_CONNECTIONS, 10L);
124 #endif
125 
126  for(;;) {
127  struct timeval interval;
128  fd_set fdread;
129  fd_set fdwrite;
130  fd_set fdexcep;
131  long timeout = -99;
132  int maxfd = -99;
133  bool found_new_socket = FALSE;
134 
135  /* Start a new handle if we aren't at the max */
136  if(state == ReadyForNewHandle) {
137  easy_init(easy[num_handles]);
138 
139  if(num_handles % 3 == 2) {
140  snprintf(full_url, urllen, "%s0200", url);
141  easy_setopt(easy[num_handles], CURLOPT_HTTPAUTH, CURLAUTH_NTLM);
142  }
143  else {
144  snprintf(full_url, urllen, "%s0100", url);
145  easy_setopt(easy[num_handles], CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
146  }
147  easy_setopt(easy[num_handles], CURLOPT_FRESH_CONNECT, 1L);
148  easy_setopt(easy[num_handles], CURLOPT_URL, full_url);
149  easy_setopt(easy[num_handles], CURLOPT_VERBOSE, 1L);
150  easy_setopt(easy[num_handles], CURLOPT_HTTPGET, 1L);
151  easy_setopt(easy[num_handles], CURLOPT_USERPWD, "testuser:testpass");
152  easy_setopt(easy[num_handles], CURLOPT_WRITEFUNCTION, callback);
153  easy_setopt(easy[num_handles], CURLOPT_WRITEDATA, easy + num_handles);
154  easy_setopt(easy[num_handles], CURLOPT_HEADER, 1L);
155 
156  multi_add_handle(multi, easy[num_handles]);
157  num_handles += 1;
158  state = NeedSocketForNewHandle;
159  }
160 
161  multi_perform(multi, &running);
162 
164 
165  if(!running && state == NoMoreHandles)
166  break; /* done */
167 
168  FD_ZERO(&fdread);
169  FD_ZERO(&fdwrite);
170  FD_ZERO(&fdexcep);
171 
172  multi_fdset(multi, &fdread, &fdwrite, &fdexcep, &maxfd);
173 
174  /* At this point, maxfd is guaranteed to be greater or equal than -1. */
175 
176  if(state == NeedSocketForNewHandle) {
177  if(maxfd != -1 && !found_new_socket) {
178  fprintf(stderr, "Warning: socket did not open immediately for new "
179  "handle (trying again)\n");
180  continue;
181  }
182  state = num_handles < MAX_EASY_HANDLES ? ReadyForNewHandle
183  : NoMoreHandles;
184  }
185 
186  multi_timeout(multi, &timeout);
187 
188  /* At this point, timeout is guaranteed to be greater or equal than -1. */
189 
190  fprintf(stderr, "%s:%d num_handles %d timeout %ld\n",
191  __FILE__, __LINE__, num_handles, timeout);
192 
193  if(timeout != -1L) {
194  int itimeout = (timeout > (long)INT_MAX) ? INT_MAX : (int)timeout;
195  interval.tv_sec = itimeout/1000;
196  interval.tv_usec = (itimeout%1000)*1000;
197  }
198  else {
199  interval.tv_sec = TEST_HANG_TIMEOUT/1000 + 1;
200  interval.tv_usec = 0;
201 
202  /* if there's no timeout and we get here on the last handle, we may
203  already have read the last part of the stream so waiting makes no
204  sense */
205  if(!running && num_handles == MAX_EASY_HANDLES) {
206  break;
207  }
208  }
209 
210  select_test(maxfd + 1, &fdread, &fdwrite, &fdexcep, &interval);
211 
213  }
214 
215 test_cleanup:
216 
217  /* proper cleanup sequence - type PB */
218 
219  for(i = 0; i < MAX_EASY_HANDLES; i++) {
220  printf("Data connection %d: %d\n", i, counter[i]);
221  curl_multi_remove_handle(multi, easy[i]);
223  }
224 
225  curl_multi_cleanup(multi);
227 
228  free(full_url);
229 
230  return res;
231 }
#define free(ptr)
Definition: curl_memory.h:130
#define state(x, y)
Definition: ftp.c:100
static int num_handles
Definition: lib1900.c:37
#define select_test(A, B, C, D, E)
Definition: test.h:378
#define CURLAUTH_BASIC
Definition: curl.h:697
#define res_global_init(A)
Definition: test.h:419
#define CURL_SOCKET_BAD
Definition: curl.h:131
#define easy_setopt(A, B, C)
Definition: test.h:190
uv_timer_t timeout
Definition: multi-uv.c:42
#define multi_add_handle(A, B)
Definition: test.h:238
UNITTEST_START char * ptr
Definition: unit1330.c:38
CURLcode
Definition: curl.h:454
#define MAX_EASY_HANDLES
static int res
#define malloc(size)
Definition: curl_memory.h:124
unsigned int i
Definition: unit1303.c:79
HandleState
#define abort_on_test_timeout()
Definition: test.h:404
#define TEST_ERR_MAJOR_BAD
Definition: test.h:85
static int counter[MAX_EASY_HANDLES]
#define FALSE
#define curl_easy_getinfo(handle, info, arg)
#define printf
Definition: curl_printf.h:40
#define easy_init(A)
Definition: test.h:145
#define multi_setopt(A, B, C)
Definition: test.h:214
static curl_socket_t sockets[MAX_EASY_HANDLES]
#define multi_fdset(A, B, C, D, E)
Definition: test.h:323
CURL_EXTERN void curl_easy_cleanup(CURL *curl)
#define CURLAUTH_NTLM
Definition: curl.h:704
int test(char *url)
Definition: curl.h:455
#define multi_perform(A, B)
Definition: test.h:293
CURL_EXTERN CURLMcode curl_multi_remove_handle(CURLM *multi_handle, CURL *curl_handle)
#define multi_init(A)
Definition: test.h:166
#define ssize_t
Definition: config-win32.h:382
int curlx_sztosi(ssize_t sznum)
Definition: warnless.c:366
void CURLM
Definition: multi.h:58
void CURL
Definition: curl.h:102
#define TEST_HANG_TIMEOUT
size_t size
Definition: unit1302.c:52
#define fprintf
Definition: curl_printf.h:41
#define snprintf
Definition: curl_printf.h:42
static CURL * easy[MAX_EASY_HANDLES]
CURL_EXTERN void curl_global_cleanup(void)
curl_global_cleanup() globally cleanups curl, uses the value of "init_flags" to determine what needs ...
Definition: easy.c:312
#define CURL_GLOBAL_ALL
Definition: curl.h:2519
int curl_socket_t
Definition: curl.h:130
CURL_EXTERN const char * curl_easy_strerror(CURLcode)
Definition: strerror.c:57
#define start_test_timing()
Definition: test.h:383
Definition: debug.c:29
CURL_EXTERN CURLMcode curl_multi_cleanup(CURLM *multi_handle)
#define multi_timeout(A, B)
Definition: test.h:353
static size_t callback(char *ptr, size_t size, size_t nmemb, void *data)


rc_tagdetect_client
Author(s): Monika Florek-Jasinska , Raphael Schaller
autogenerated on Sat Feb 13 2021 03:42:15