~ubuntu-branches/ubuntu/natty/curl/natty-proposed

« back to all changes in this revision

Viewing changes to tests/server/resolve.c

  • Committer: Bazaar Package Importer
  • Author(s): Andreas Schuldei
  • Date: 2009-05-24 21:12:19 UTC
  • mfrom: (1.1.12 upstream)
  • mto: (3.3.1 squeeze)
  • mto: This revision was merged to the branch mainline in revision 39.
  • Revision ID: james.westby@ubuntu.com-20090524211219-7jgcwuhl04ixuqsm
Tags: upstream-7.19.5
ImportĀ upstreamĀ versionĀ 7.19.5

Show diffs side-by-side

added added

removed removed

Lines of Context:
18
18
 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
19
19
 * KIND, either express or implied.
20
20
 *
21
 
 * $Id: resolve.c,v 1.8 2008-02-28 00:55:06 yangtse Exp $
 
21
 * $Id: resolve.c,v 1.13 2008-10-28 20:03:22 danf Exp $
22
22
 ***************************************************************************/
23
23
 
24
24
/* Purpose
38
38
#ifdef HAVE_UNISTD_H
39
39
#include <unistd.h>
40
40
#endif
 
41
#ifdef HAVE_SYS_TYPES_H
 
42
#include <sys/types.h>
 
43
#endif
41
44
#ifdef HAVE_SYS_SOCKET_H
42
45
#include <sys/socket.h>
43
46
#endif
61
64
/* include memdebug.h last */
62
65
#include "memdebug.h"
63
66
 
64
 
char use_ipv6=FALSE;
 
67
static bool use_ipv6 = FALSE;
 
68
static const char *ipv_inuse = "IPv4";
65
69
 
66
70
const char *serverlogfile=""; /* for a util.c function we don't use */
67
71
 
68
72
int main(int argc, char *argv[])
69
73
{
70
74
  int arg=1;
71
 
  char *host;
72
 
  int rc;
 
75
  const char *host = NULL;
 
76
  int rc = 0;
73
77
 
74
78
  while(argc>arg) {
75
79
    if(!strcmp("--version", argv[arg])) {
83
87
      return 0;
84
88
    }
85
89
    else if(!strcmp("--ipv6", argv[arg])) {
86
 
#ifdef ENABLE_IPV6
87
 
      use_ipv6=TRUE;
88
 
#endif
 
90
      ipv_inuse = "IPv6";
 
91
      use_ipv6 = TRUE;
89
92
      arg++;
90
93
    }
91
94
    else if(!strcmp("--ipv4", argv[arg])) {
92
95
      /* for completeness, we support this option as well */
93
 
      use_ipv6=FALSE;
 
96
      ipv_inuse = "IPv4";
 
97
      use_ipv6 = FALSE;
94
98
      arg++;
95
99
    }
96
100
    else {
100
104
  if(!host) {
101
105
    puts("Usage: resolve [option] <host>\n"
102
106
         " --version\n"
103
 
         " --ipv4\n"
104
 
         " --ipv6");
105
 
    return 0;
 
107
         " --ipv4"
 
108
#ifdef ENABLE_IPV6
 
109
         "\n --ipv6"
 
110
#endif
 
111
         );
 
112
    return 1;
106
113
  }
107
114
 
108
115
#ifdef WIN32
110
117
  atexit(win32_cleanup);
111
118
#endif
112
119
 
113
 
#ifdef ENABLE_IPV6
114
 
  if(!use_ipv6)
115
 
#endif
116
 
  {
 
120
  if(!use_ipv6) {
117
121
    /* gethostbyname() resolve */
118
122
    struct hostent *he;
119
123
 
121
125
 
122
126
    rc = !he;
123
127
  }
124
 
#ifdef ENABLE_IPV6
125
128
  else {
126
 
    /* getaddrinfo() resolve */
127
 
    struct addrinfo *ai;
128
 
    struct addrinfo hints;
129
 
 
130
 
    memset(&hints, 0, sizeof(hints));
131
 
    hints.ai_family = PF_INET6;
132
 
    hints.ai_socktype = SOCK_STREAM;
133
 
    hints.ai_flags = AI_CANONNAME;
134
 
    rc = (getaddrinfo)(host, "80", &hints, &ai);
135
 
 
 
129
#ifdef ENABLE_IPV6
 
130
    /* Check that the system has IPv6 enabled before checking the resolver */
 
131
    int s = socket(PF_INET6, SOCK_DGRAM, 0);
 
132
    if(s == -1)
 
133
      /* an ipv6 address was requested and we can't get/use one */
 
134
      rc = -1;
 
135
    else {
 
136
      sclose(s);
 
137
    }
 
138
 
 
139
    if (rc == 0) {
 
140
      /* getaddrinfo() resolve */
 
141
      struct addrinfo *ai;
 
142
      struct addrinfo hints;
 
143
 
 
144
      memset(&hints, 0, sizeof(hints));
 
145
      hints.ai_family = PF_INET6;
 
146
      hints.ai_socktype = SOCK_STREAM;
 
147
      hints.ai_flags = AI_CANONNAME;
 
148
      /* Use parenthesis around function to stop it from being replaced by
 
149
      the macro in memdebug.h */
 
150
      rc = (getaddrinfo)(host, "80", &hints, &ai);
 
151
    }
 
152
 
 
153
#else
 
154
    puts("IPv6 support has been disabled in this program");
 
155
    return 1;
 
156
#endif
136
157
  }
137
 
#endif
138
158
  if(rc)
139
 
    printf("Resolving '%s' didn't work\n", host);
 
159
    printf("Resolving %s '%s' didn't work\n", ipv_inuse, host);
140
160
 
141
 
  return !rc?0:1;
 
161
  return !!rc;
142
162
}