annotate fake-rfc2553.c @ 447:278805938dcf

Patch from Nicolai Ehemann to try binding before going to the background, so that if it exits early (because something's already listening etc) then it will return an exitcode of 1.
author Matt Johnston <matt@ucc.asn.au>
date Thu, 19 Jul 2007 15:54:18 +0000
parents 86725004a0ea
children 9fd27bc37807
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
64
efb5e0b335cf TCP forwarding works.
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1 /*
67
86725004a0ea fake-rfc stuff
Matt Johnston <matt@ucc.asn.au>
parents: 64
diff changeset
2 *
86725004a0ea fake-rfc stuff
Matt Johnston <matt@ucc.asn.au>
parents: 64
diff changeset
3 * Taken from OpenSSH 3.8.1p1
86725004a0ea fake-rfc stuff
Matt Johnston <matt@ucc.asn.au>
parents: 64
diff changeset
4 *
64
efb5e0b335cf TCP forwarding works.
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
5 * Copyright (C) 2000-2003 Damien Miller. All rights reserved.
efb5e0b335cf TCP forwarding works.
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
6 * Copyright (C) 1999 WIDE Project. All rights reserved.
efb5e0b335cf TCP forwarding works.
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
7 *
efb5e0b335cf TCP forwarding works.
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
8 * Redistribution and use in source and binary forms, with or without
efb5e0b335cf TCP forwarding works.
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
9 * modification, are permitted provided that the following conditions
efb5e0b335cf TCP forwarding works.
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
10 * are met:
efb5e0b335cf TCP forwarding works.
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
11 * 1. Redistributions of source code must retain the above copyright
efb5e0b335cf TCP forwarding works.
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
12 * notice, this list of conditions and the following disclaimer.
efb5e0b335cf TCP forwarding works.
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
13 * 2. Redistributions in binary form must reproduce the above copyright
efb5e0b335cf TCP forwarding works.
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
14 * notice, this list of conditions and the following disclaimer in the
efb5e0b335cf TCP forwarding works.
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
15 * documentation and/or other materials provided with the distribution.
efb5e0b335cf TCP forwarding works.
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
16 * 3. Neither the name of the project nor the names of its contributors
efb5e0b335cf TCP forwarding works.
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
17 * may be used to endorse or promote products derived from this software
efb5e0b335cf TCP forwarding works.
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
18 * without specific prior written permission.
efb5e0b335cf TCP forwarding works.
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
19 *
efb5e0b335cf TCP forwarding works.
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
20 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
efb5e0b335cf TCP forwarding works.
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
efb5e0b335cf TCP forwarding works.
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
efb5e0b335cf TCP forwarding works.
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
efb5e0b335cf TCP forwarding works.
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
efb5e0b335cf TCP forwarding works.
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
efb5e0b335cf TCP forwarding works.
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
efb5e0b335cf TCP forwarding works.
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
efb5e0b335cf TCP forwarding works.
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
efb5e0b335cf TCP forwarding works.
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
efb5e0b335cf TCP forwarding works.
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
30 * SUCH DAMAGE.
efb5e0b335cf TCP forwarding works.
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
31 */
efb5e0b335cf TCP forwarding works.
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
32
efb5e0b335cf TCP forwarding works.
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
33 /*
efb5e0b335cf TCP forwarding works.
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
34 * Pseudo-implementation of RFC2553 name / address resolution functions
efb5e0b335cf TCP forwarding works.
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
35 *
efb5e0b335cf TCP forwarding works.
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
36 * But these functions are not implemented correctly. The minimum subset
efb5e0b335cf TCP forwarding works.
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
37 * is implemented for ssh use only. For example, this routine assumes
efb5e0b335cf TCP forwarding works.
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
38 * that ai_family is AF_INET. Don't use it for another purpose.
efb5e0b335cf TCP forwarding works.
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
39 */
efb5e0b335cf TCP forwarding works.
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
40
efb5e0b335cf TCP forwarding works.
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
41 #include "includes.h"
efb5e0b335cf TCP forwarding works.
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
42
67
86725004a0ea fake-rfc stuff
Matt Johnston <matt@ucc.asn.au>
parents: 64
diff changeset
43 /* RCSID("$.Id: fake-rfc2553.c,v 1.5 2003/09/22 02:08:23 dtucker Exp $");*/
64
efb5e0b335cf TCP forwarding works.
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
44
efb5e0b335cf TCP forwarding works.
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
45 #ifndef HAVE_GETNAMEINFO
efb5e0b335cf TCP forwarding works.
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
46 int getnameinfo(const struct sockaddr *sa, size_t salen, char *host,
efb5e0b335cf TCP forwarding works.
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
47 size_t hostlen, char *serv, size_t servlen, int flags)
efb5e0b335cf TCP forwarding works.
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
48 {
efb5e0b335cf TCP forwarding works.
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
49 struct sockaddr_in *sin = (struct sockaddr_in *)sa;
efb5e0b335cf TCP forwarding works.
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
50 struct hostent *hp;
efb5e0b335cf TCP forwarding works.
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
51 char tmpserv[16];
efb5e0b335cf TCP forwarding works.
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
52
efb5e0b335cf TCP forwarding works.
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
53 if (serv != NULL) {
efb5e0b335cf TCP forwarding works.
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
54 snprintf(tmpserv, sizeof(tmpserv), "%d", ntohs(sin->sin_port));
efb5e0b335cf TCP forwarding works.
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
55 if (strlcpy(serv, tmpserv, servlen) >= servlen)
efb5e0b335cf TCP forwarding works.
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
56 return (EAI_MEMORY);
efb5e0b335cf TCP forwarding works.
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
57 }
efb5e0b335cf TCP forwarding works.
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
58
efb5e0b335cf TCP forwarding works.
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
59 if (host != NULL) {
efb5e0b335cf TCP forwarding works.
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
60 if (flags & NI_NUMERICHOST) {
efb5e0b335cf TCP forwarding works.
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
61 if (strlcpy(host, inet_ntoa(sin->sin_addr),
efb5e0b335cf TCP forwarding works.
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
62 hostlen) >= hostlen)
efb5e0b335cf TCP forwarding works.
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
63 return (EAI_MEMORY);
efb5e0b335cf TCP forwarding works.
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
64 else
efb5e0b335cf TCP forwarding works.
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
65 return (0);
efb5e0b335cf TCP forwarding works.
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
66 } else {
efb5e0b335cf TCP forwarding works.
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
67 hp = gethostbyaddr((char *)&sin->sin_addr,
efb5e0b335cf TCP forwarding works.
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
68 sizeof(struct in_addr), AF_INET);
efb5e0b335cf TCP forwarding works.
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
69 if (hp == NULL)
efb5e0b335cf TCP forwarding works.
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
70 return (EAI_NODATA);
efb5e0b335cf TCP forwarding works.
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
71
efb5e0b335cf TCP forwarding works.
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
72 if (strlcpy(host, hp->h_name, hostlen) >= hostlen)
efb5e0b335cf TCP forwarding works.
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
73 return (EAI_MEMORY);
efb5e0b335cf TCP forwarding works.
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
74 else
efb5e0b335cf TCP forwarding works.
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
75 return (0);
efb5e0b335cf TCP forwarding works.
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
76 }
efb5e0b335cf TCP forwarding works.
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
77 }
efb5e0b335cf TCP forwarding works.
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
78 return (0);
efb5e0b335cf TCP forwarding works.
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
79 }
efb5e0b335cf TCP forwarding works.
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
80 #endif /* !HAVE_GETNAMEINFO */
efb5e0b335cf TCP forwarding works.
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
81
efb5e0b335cf TCP forwarding works.
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
82 #ifndef HAVE_GAI_STRERROR
efb5e0b335cf TCP forwarding works.
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
83 #ifdef HAVE_CONST_GAI_STRERROR_PROTO
efb5e0b335cf TCP forwarding works.
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
84 const char *
efb5e0b335cf TCP forwarding works.
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
85 #else
efb5e0b335cf TCP forwarding works.
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
86 char *
efb5e0b335cf TCP forwarding works.
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
87 #endif
efb5e0b335cf TCP forwarding works.
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
88 gai_strerror(int err)
efb5e0b335cf TCP forwarding works.
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
89 {
efb5e0b335cf TCP forwarding works.
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
90 switch (err) {
efb5e0b335cf TCP forwarding works.
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
91 case EAI_NODATA:
efb5e0b335cf TCP forwarding works.
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
92 return ("no address associated with name");
efb5e0b335cf TCP forwarding works.
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
93 case EAI_MEMORY:
efb5e0b335cf TCP forwarding works.
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
94 return ("memory allocation failure.");
efb5e0b335cf TCP forwarding works.
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
95 case EAI_NONAME:
efb5e0b335cf TCP forwarding works.
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
96 return ("nodename nor servname provided, or not known");
efb5e0b335cf TCP forwarding works.
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
97 default:
efb5e0b335cf TCP forwarding works.
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
98 return ("unknown/invalid error.");
efb5e0b335cf TCP forwarding works.
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
99 }
efb5e0b335cf TCP forwarding works.
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
100 }
efb5e0b335cf TCP forwarding works.
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
101 #endif /* !HAVE_GAI_STRERROR */
efb5e0b335cf TCP forwarding works.
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
102
efb5e0b335cf TCP forwarding works.
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
103 #ifndef HAVE_FREEADDRINFO
efb5e0b335cf TCP forwarding works.
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
104 void
efb5e0b335cf TCP forwarding works.
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
105 freeaddrinfo(struct addrinfo *ai)
efb5e0b335cf TCP forwarding works.
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
106 {
efb5e0b335cf TCP forwarding works.
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
107 struct addrinfo *next;
efb5e0b335cf TCP forwarding works.
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
108
efb5e0b335cf TCP forwarding works.
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
109 for(; ai != NULL;) {
efb5e0b335cf TCP forwarding works.
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
110 next = ai->ai_next;
efb5e0b335cf TCP forwarding works.
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
111 free(ai);
efb5e0b335cf TCP forwarding works.
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
112 ai = next;
efb5e0b335cf TCP forwarding works.
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
113 }
efb5e0b335cf TCP forwarding works.
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
114 }
efb5e0b335cf TCP forwarding works.
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
115 #endif /* !HAVE_FREEADDRINFO */
efb5e0b335cf TCP forwarding works.
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
116
efb5e0b335cf TCP forwarding works.
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
117 #ifndef HAVE_GETADDRINFO
efb5e0b335cf TCP forwarding works.
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
118 static struct
efb5e0b335cf TCP forwarding works.
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
119 addrinfo *malloc_ai(int port, u_long addr, const struct addrinfo *hints)
efb5e0b335cf TCP forwarding works.
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
120 {
efb5e0b335cf TCP forwarding works.
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
121 struct addrinfo *ai;
efb5e0b335cf TCP forwarding works.
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
122
efb5e0b335cf TCP forwarding works.
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
123 ai = malloc(sizeof(*ai) + sizeof(struct sockaddr_in));
efb5e0b335cf TCP forwarding works.
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
124 if (ai == NULL)
efb5e0b335cf TCP forwarding works.
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
125 return (NULL);
efb5e0b335cf TCP forwarding works.
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
126
efb5e0b335cf TCP forwarding works.
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
127 memset(ai, '\0', sizeof(*ai) + sizeof(struct sockaddr_in));
efb5e0b335cf TCP forwarding works.
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
128
efb5e0b335cf TCP forwarding works.
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
129 ai->ai_addr = (struct sockaddr *)(ai + 1);
efb5e0b335cf TCP forwarding works.
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
130 /* XXX -- ssh doesn't use sa_len */
efb5e0b335cf TCP forwarding works.
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
131 ai->ai_addrlen = sizeof(struct sockaddr_in);
efb5e0b335cf TCP forwarding works.
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
132 ai->ai_addr->sa_family = ai->ai_family = AF_INET;
efb5e0b335cf TCP forwarding works.
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
133
efb5e0b335cf TCP forwarding works.
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
134 ((struct sockaddr_in *)(ai)->ai_addr)->sin_port = port;
efb5e0b335cf TCP forwarding works.
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
135 ((struct sockaddr_in *)(ai)->ai_addr)->sin_addr.s_addr = addr;
efb5e0b335cf TCP forwarding works.
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
136
efb5e0b335cf TCP forwarding works.
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
137 /* XXX: the following is not generally correct, but does what we want */
efb5e0b335cf TCP forwarding works.
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
138 if (hints->ai_socktype)
efb5e0b335cf TCP forwarding works.
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
139 ai->ai_socktype = hints->ai_socktype;
efb5e0b335cf TCP forwarding works.
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
140 else
efb5e0b335cf TCP forwarding works.
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
141 ai->ai_socktype = SOCK_STREAM;
efb5e0b335cf TCP forwarding works.
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
142
efb5e0b335cf TCP forwarding works.
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
143 if (hints->ai_protocol)
efb5e0b335cf TCP forwarding works.
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
144 ai->ai_protocol = hints->ai_protocol;
efb5e0b335cf TCP forwarding works.
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
145
efb5e0b335cf TCP forwarding works.
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
146 return (ai);
efb5e0b335cf TCP forwarding works.
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
147 }
efb5e0b335cf TCP forwarding works.
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
148
efb5e0b335cf TCP forwarding works.
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
149 int
efb5e0b335cf TCP forwarding works.
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
150 getaddrinfo(const char *hostname, const char *servname,
efb5e0b335cf TCP forwarding works.
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
151 const struct addrinfo *hints, struct addrinfo **res)
efb5e0b335cf TCP forwarding works.
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
152 {
efb5e0b335cf TCP forwarding works.
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
153 struct hostent *hp;
efb5e0b335cf TCP forwarding works.
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
154 struct servent *sp;
efb5e0b335cf TCP forwarding works.
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
155 struct in_addr in;
efb5e0b335cf TCP forwarding works.
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
156 int i;
efb5e0b335cf TCP forwarding works.
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
157 long int port;
efb5e0b335cf TCP forwarding works.
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
158 u_long addr;
efb5e0b335cf TCP forwarding works.
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
159
efb5e0b335cf TCP forwarding works.
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
160 port = 0;
efb5e0b335cf TCP forwarding works.
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
161 if (servname != NULL) {
efb5e0b335cf TCP forwarding works.
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
162 char *cp;
efb5e0b335cf TCP forwarding works.
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
163
efb5e0b335cf TCP forwarding works.
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
164 port = strtol(servname, &cp, 10);
efb5e0b335cf TCP forwarding works.
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
165 if (port > 0 && port <= 65535 && *cp == '\0')
efb5e0b335cf TCP forwarding works.
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
166 port = htons(port);
efb5e0b335cf TCP forwarding works.
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
167 else if ((sp = getservbyname(servname, NULL)) != NULL)
efb5e0b335cf TCP forwarding works.
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
168 port = sp->s_port;
efb5e0b335cf TCP forwarding works.
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
169 else
efb5e0b335cf TCP forwarding works.
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
170 port = 0;
efb5e0b335cf TCP forwarding works.
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
171 }
efb5e0b335cf TCP forwarding works.
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
172
efb5e0b335cf TCP forwarding works.
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
173 if (hints && hints->ai_flags & AI_PASSIVE) {
efb5e0b335cf TCP forwarding works.
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
174 addr = htonl(0x00000000);
efb5e0b335cf TCP forwarding works.
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
175 if (hostname && inet_aton(hostname, &in) != 0)
efb5e0b335cf TCP forwarding works.
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
176 addr = in.s_addr;
efb5e0b335cf TCP forwarding works.
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
177 *res = malloc_ai(port, addr, hints);
efb5e0b335cf TCP forwarding works.
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
178 if (*res == NULL)
efb5e0b335cf TCP forwarding works.
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
179 return (EAI_MEMORY);
efb5e0b335cf TCP forwarding works.
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
180 return (0);
efb5e0b335cf TCP forwarding works.
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
181 }
efb5e0b335cf TCP forwarding works.
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
182
efb5e0b335cf TCP forwarding works.
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
183 if (!hostname) {
efb5e0b335cf TCP forwarding works.
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
184 *res = malloc_ai(port, htonl(0x7f000001), hints);
efb5e0b335cf TCP forwarding works.
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
185 if (*res == NULL)
efb5e0b335cf TCP forwarding works.
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
186 return (EAI_MEMORY);
efb5e0b335cf TCP forwarding works.
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
187 return (0);
efb5e0b335cf TCP forwarding works.
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
188 }
efb5e0b335cf TCP forwarding works.
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
189
efb5e0b335cf TCP forwarding works.
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
190 if (inet_aton(hostname, &in)) {
efb5e0b335cf TCP forwarding works.
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
191 *res = malloc_ai(port, in.s_addr, hints);
efb5e0b335cf TCP forwarding works.
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
192 if (*res == NULL)
efb5e0b335cf TCP forwarding works.
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
193 return (EAI_MEMORY);
efb5e0b335cf TCP forwarding works.
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
194 return (0);
efb5e0b335cf TCP forwarding works.
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
195 }
efb5e0b335cf TCP forwarding works.
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
196
efb5e0b335cf TCP forwarding works.
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
197 /* Don't try DNS if AI_NUMERICHOST is set */
efb5e0b335cf TCP forwarding works.
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
198 if (hints && hints->ai_flags & AI_NUMERICHOST)
efb5e0b335cf TCP forwarding works.
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
199 return (EAI_NONAME);
efb5e0b335cf TCP forwarding works.
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
200
efb5e0b335cf TCP forwarding works.
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
201 hp = gethostbyname(hostname);
efb5e0b335cf TCP forwarding works.
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
202 if (hp && hp->h_name && hp->h_name[0] && hp->h_addr_list[0]) {
efb5e0b335cf TCP forwarding works.
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
203 struct addrinfo *cur, *prev;
efb5e0b335cf TCP forwarding works.
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
204
efb5e0b335cf TCP forwarding works.
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
205 cur = prev = *res = NULL;
efb5e0b335cf TCP forwarding works.
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
206 for (i = 0; hp->h_addr_list[i]; i++) {
efb5e0b335cf TCP forwarding works.
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
207 struct in_addr *in = (struct in_addr *)hp->h_addr_list[i];
efb5e0b335cf TCP forwarding works.
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
208
efb5e0b335cf TCP forwarding works.
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
209 cur = malloc_ai(port, in->s_addr, hints);
efb5e0b335cf TCP forwarding works.
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
210 if (cur == NULL) {
efb5e0b335cf TCP forwarding works.
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
211 if (*res != NULL)
efb5e0b335cf TCP forwarding works.
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
212 freeaddrinfo(*res);
efb5e0b335cf TCP forwarding works.
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
213 return (EAI_MEMORY);
efb5e0b335cf TCP forwarding works.
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
214 }
efb5e0b335cf TCP forwarding works.
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
215 if (prev)
efb5e0b335cf TCP forwarding works.
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
216 prev->ai_next = cur;
efb5e0b335cf TCP forwarding works.
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
217 else
efb5e0b335cf TCP forwarding works.
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
218 *res = cur;
efb5e0b335cf TCP forwarding works.
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
219
efb5e0b335cf TCP forwarding works.
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
220 prev = cur;
efb5e0b335cf TCP forwarding works.
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
221 }
efb5e0b335cf TCP forwarding works.
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
222 return (0);
efb5e0b335cf TCP forwarding works.
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
223 }
efb5e0b335cf TCP forwarding works.
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
224
efb5e0b335cf TCP forwarding works.
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
225 return (EAI_NODATA);
efb5e0b335cf TCP forwarding works.
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
226 }
efb5e0b335cf TCP forwarding works.
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
227 #endif /* !HAVE_GETADDRINFO */