2 * Copyright (c) 1999-2001, 2004, 2010, 2013 Sendmail, 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.30 2013/02/22 22:43:33 gshapiro Exp $")
15 #if NETINET || NETINET6
16 # include <arpa/inet.h>
17 #endif /* NETINET || NETINET6 */
18 #include "libmilter.h"
21 ** MI_GETHOSTBY{NAME,ADDR} -- compatibility routines for gethostbyXXX
23 ** Some operating systems have wierd 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 */
37 # endif /* ! AI_ADDRCONFIG */
39 # define AI_ALL 0 /* dummy */
40 # endif /* ! AI_ALL */
42 # define AI_DEFAULT 0 /* dummy */
43 # endif /* ! AI_DEFAULT */
45 static struct hostent *
46 sm_getipnodebyname(name, family, flags, err)
55 if (family == AF_INET6)
57 /* From RFC2133, section 6.1 */
58 resv6 = bitset(RES_USE_INET6, _res.options);
59 _res.options |= RES_USE_INET6;
62 h = gethostbyname(name);
63 if (family == AF_INET6 && !resv6)
64 _res.options &= ~RES_USE_INET6;
74 ** Stub routine -- if they don't have getipnodeby*(),
75 ** they probably don't have the free routine either.
80 #else /* NEEDSGETIPNODE && NETINET6 */
81 #define sm_getipnodebyname getipnodebyname
82 #endif /* NEEDSGETIPNODE && NETINET6 */
85 mi_gethostbyname(name, family)
89 struct hostent *h = NULL;
90 #if (SOLARIS > 10000 && SOLARIS < 20400) || (defined(SOLARIS) && SOLARIS < 204) || (defined(sony_news) && defined(__svr4))
91 # if SOLARIS == 20300 || SOLARIS == 203
92 static struct hostent hp;
93 static char buf[1000];
94 extern struct hostent *_switch_gethostbyname_r();
96 h = _switch_gethostbyname_r(name, &hp, buf, sizeof(buf), &h_errno);
97 # else /* SOLARIS == 20300 || SOLARIS == 203 */
98 extern struct hostent *__switch_gethostbyname();
100 h = __switch_gethostbyname(name);
101 # endif /* SOLARIS == 20300 || SOLARIS == 203 */
102 #else /* (SOLARIS > 10000 && SOLARIS < 20400) || (defined(SOLARIS) && SOLARIS < 204) || (defined(sony_news) && defined(__svr4)) */
104 # ifndef SM_IPNODEBYNAME_FLAGS
105 /* For IPv4-mapped addresses, use: AI_DEFAULT|AI_ALL */
106 # define SM_IPNODEBYNAME_FLAGS AI_ADDRCONFIG
107 # endif /* SM_IPNODEBYNAME_FLAGS */
109 int flags = SM_IPNODEBYNAME_FLAGS;
111 # endif /* NETINET6 */
114 # if ADDRCONFIG_IS_BROKEN
115 flags &= ~AI_ADDRCONFIG;
116 # endif /* ADDRCONFIG_IS_BROKEN */
117 h = sm_getipnodebyname(name, family, flags, &err);
119 # else /* NETINET6 */
120 h = gethostbyname(name);
121 # endif /* NETINET6 */
123 #endif /* (SOLARIS > 10000 && SOLARIS < 20400) || (defined(SOLARIS) && SOLARIS < 204) || (defined(sony_news) && defined(__svr4)) */
129 ** MI_INET_PTON -- convert printed form to network address.
131 ** Wrapper for inet_pton() which handles IPv6: labels.
134 ** family -- address family
136 ** dst -- destination address structure
139 ** 1 if the address was valid
140 ** 0 if the address wasn't parseable
145 mi_inet_pton(family, src, dst)
150 if (family == AF_INET6 &&
151 strncasecmp(src, "IPv6:", 5) == 0)
153 return inet_pton(family, src, dst);
155 #endif /* NETINET6 */