1/***************************************************************************
2 *                                  _   _ ____  _
3 *  Project                     ___| | | |  _ \| |
4 *                             / __| | | | |_) | |
5 *                            | (__| |_| |  _ <| |___
6 *                             \___|\___/|_| \_\_____|
7 *
8 * Copyright (C) 1998 - 2011, 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
24#include "memdebug.h"
25
26#ifdef LIB585
27
28static int counter;
29
30static curl_socket_t tst_opensocket(void *clientp,
31                                    curlsocktype purpose,
32                                    struct curl_sockaddr *addr)
33{
34  (void)clientp;
35  (void)purpose;
36  printf("[OPEN] counter: %d\n", ++counter);
37  return socket(addr->family, addr->socktype, addr->protocol);
38}
39
40static int tst_closesocket(void *clientp, curl_socket_t sock)
41{
42  (void)clientp;
43  printf("[CLOSE] counter: %d\n", counter--);
44  return sclose(sock);
45}
46
47static void setupcallbacks(CURL *curl)
48{
49  curl_easy_setopt(curl, CURLOPT_OPENSOCKETFUNCTION, tst_opensocket);
50  curl_easy_setopt(curl, CURLOPT_CLOSESOCKETFUNCTION, tst_closesocket);
51  counter = 0;
52}
53
54#else
55#define setupcallbacks(x)
56#endif
57
58
59int test(char *URL)
60{
61  CURLcode res;
62  CURL *curl;
63  char *ipstr=NULL;
64
65  if (curl_global_init(CURL_GLOBAL_ALL) != CURLE_OK) {
66    fprintf(stderr, "curl_global_init() failed\n");
67    return TEST_ERR_MAJOR_BAD;
68  }
69
70  if ((curl = curl_easy_init()) == NULL) {
71    fprintf(stderr, "curl_easy_init() failed\n");
72    curl_global_cleanup();
73    return TEST_ERR_MAJOR_BAD;
74  }
75
76  test_setopt(curl, CURLOPT_URL, URL);
77  test_setopt(curl, CURLOPT_HEADER, 1L);
78
79  setupcallbacks(curl);
80
81  res = curl_easy_perform(curl);
82
83  if(!res) {
84    FILE *moo;
85    res = curl_easy_getinfo(curl, CURLINFO_PRIMARY_IP, &ipstr);
86    moo = fopen(libtest_arg2, "wb");
87    if(moo) {
88      fprintf(moo, "IP: %s\n", ipstr);
89      fclose(moo);
90    }
91  }
92
93test_cleanup:
94
95  curl_easy_cleanup(curl);
96  curl_global_cleanup();
97
98  return (int)res;
99}
100
101