2 * Copyright (c) 1999-2001, 2004, 2010, 2013 Proofpoint, Inc. and its suppliers.
5 * By using this file, you agree to the terms and conditions set
6 * forth in the LICENSE file which can be found at the top level of
7 * the sendmail distribution.
12 SM_RCSID("@(#)$Id: sm_gethost.c,v 8.32 2013-11-22 20:51:36 ca Exp $")
15 #if NETINET || NETINET6
16 # include <arpa/inet.h>
18 #include "libmilter.h"
21 ** MI_GETHOSTBY{NAME,ADDR} -- compatibility routines for gethostbyXXX
23 ** Some operating systems have weird problems with the gethostbyXXX
24 ** routines. For example, Solaris versions at least through 2.3
25 ** don't properly deliver a canonical h_name field. This tries to
26 ** work around these problems.
28 ** Support IPv6 as well as IPv4.
31 #if NETINET6 && NEEDSGETIPNODE
33 static struct hostent *sm_getipnodebyname __P((const char *, int, int, int *));
35 # ifndef AI_ADDRCONFIG
36 # define AI_ADDRCONFIG 0 /* dummy */
39 # define AI_ALL 0 /* dummy */
42 # define AI_DEFAULT 0 /* dummy */
45 static struct hostent *
46 sm_getipnodebyname(name, family, flags, err)
53 # if HAS_GETHOSTBYNAME2
55 h = gethostbyname2(name, family);
60 # else /* HAS_GETHOSTBYNAME2 */
64 if (family == AF_INET6)
66 /* From RFC2133, section 6.1 */
67 resv6 = bitset(RES_USE_INET6, _res.options);
68 _res.options |= RES_USE_INET6;
70 # endif /* RES_USE_INET6 */
72 h = gethostbyname(name);
75 _res.options &= ~RES_USE_INET6;
78 /* the function is supposed to return only the requested family */
79 if (h != NULL && h->h_addrtype != family)
90 # endif /* HAS_GETHOSTBYNAME2 */
98 ** Stub routine -- if they don't have getipnodeby*(),
99 ** they probably don't have the free routine either.
104 #else /* NEEDSGETIPNODE && NETINET6 */
105 #define sm_getipnodebyname getipnodebyname
106 #endif /* NEEDSGETIPNODE && NETINET6 */
109 mi_gethostbyname(name, family)
113 struct hostent *h = NULL;
114 #if (SOLARIS > 10000 && SOLARIS < 20400) || (defined(SOLARIS) && SOLARIS < 204) || (defined(sony_news) && defined(__svr4))
115 # if SOLARIS == 20300 || SOLARIS == 203
116 static struct hostent hp;
117 static char buf[1000];
118 extern struct hostent *_switch_gethostbyname_r();
120 h = _switch_gethostbyname_r(name, &hp, buf, sizeof(buf), &h_errno);
121 # else /* SOLARIS == 20300 || SOLARIS == 203 */
122 extern struct hostent *__switch_gethostbyname();
124 h = __switch_gethostbyname(name);
125 # endif /* SOLARIS == 20300 || SOLARIS == 203 */
126 #else /* (SOLARIS > 10000 && SOLARIS < 20400) || (defined(SOLARIS) && SOLARIS < 204) || (defined(sony_news) && defined(__svr4)) */
128 # ifndef SM_IPNODEBYNAME_FLAGS
129 /* For IPv4-mapped addresses, use: AI_DEFAULT|AI_ALL */
130 # define SM_IPNODEBYNAME_FLAGS AI_ADDRCONFIG
133 int flags = SM_IPNODEBYNAME_FLAGS;
135 # endif /* NETINET6 */
138 # if ADDRCONFIG_IS_BROKEN
139 flags &= ~AI_ADDRCONFIG;
141 h = sm_getipnodebyname(name, family, flags, &err);
143 # else /* NETINET6 */
144 h = gethostbyname(name);
145 # endif /* NETINET6 */
147 #endif /* (SOLARIS > 10000 && SOLARIS < 20400) || (defined(SOLARIS) && SOLARIS < 204) || (defined(sony_news) && defined(__svr4)) */
149 /* the function is supposed to return only the requested family */
150 if (h != NULL && h->h_addrtype != family)
156 SM_SET_H_ERRNO(NO_DATA);
163 ** MI_INET_PTON -- convert printed form to network address.
165 ** Wrapper for inet_pton() which handles IPv6: labels.
168 ** family -- address family
170 ** dst -- destination address structure
173 ** 1 if the address was valid
174 ** 0 if the address wasn't parsable
179 mi_inet_pton(family, src, dst)
184 if (family == AF_INET6 &&
185 strncasecmp(src, "IPv6:", 5) == 0)
187 return inet_pton(family, src, dst);
189 #endif /* NETINET6 */