sm_gethost.c revision 223067
1247834Skib/*
2247834Skib *  Copyright (c) 1999-2001, 2004, 2010 Sendmail, Inc. and its suppliers.
3247834Skib *	All rights reserved.
4247834Skib *
5247834Skib * By using this file, you agree to the terms and conditions set
6247834Skib * forth in the LICENSE file which can be found at the top level of
7247834Skib * the sendmail distribution.
8247834Skib *
9247834Skib */
10247834Skib
11247834Skib#include <sm/gen.h>
12247834SkibSM_RCSID("@(#)$Id: sm_gethost.c,v 8.29 2010/07/27 01:09:31 ca Exp $")
13247834Skib
14247834Skib#include <sendmail.h>
15247834Skib#if NETINET || NETINET6
16247834Skib# include <arpa/inet.h>
17247834Skib#endif /* NETINET || NETINET6 */
18247834Skib#include "libmilter.h"
19247834Skib
20247834Skib/*
21247834Skib**  MI_GETHOSTBY{NAME,ADDR} -- compatibility routines for gethostbyXXX
22247834Skib**
23247834Skib**	Some operating systems have wierd problems with the gethostbyXXX
24247834Skib**	routines.  For example, Solaris versions at least through 2.3
25247834Skib**	don't properly deliver a canonical h_name field.  This tries to
26247834Skib**	work around these problems.
27247834Skib**
28247834Skib**	Support IPv6 as well as IPv4.
29247834Skib*/
30247834Skib
31247834Skib#if NETINET6 && NEEDSGETIPNODE
32247834Skib
33247834Skibstatic struct hostent *sm_getipnodebyname __P((const char *, int, int, int *));
34247834Skib
35247834Skib# ifndef AI_ADDRCONFIG
36247834Skib#  define AI_ADDRCONFIG	0	/* dummy */
37247834Skib# endif /* ! AI_ADDRCONFIG */
38247834Skib# ifndef AI_ALL
39247834Skib#  define AI_ALL	0	/* dummy */
40247834Skib# endif /* ! AI_ALL */
41247834Skib# ifndef AI_DEFAULT
42247834Skib#  define AI_DEFAULT	0	/* dummy */
43247834Skib# endif /* ! AI_DEFAULT */
44247834Skib
45247834Skibstatic struct hostent *
46247834Skibsm_getipnodebyname(name, family, flags, err)
47247834Skib	const char *name;
48247834Skib	int family;
49247834Skib	int flags;
50247834Skib	int *err;
51247834Skib{
52247834Skib	bool resv6 = true;
53247834Skib	struct hostent *h;
54247834Skib
55247834Skib	if (family == AF_INET6)
56247834Skib	{
57		/* From RFC2133, section 6.1 */
58		resv6 = bitset(RES_USE_INET6, _res.options);
59		_res.options |= RES_USE_INET6;
60	}
61	SM_SET_H_ERRNO(0);
62	h = gethostbyname(name);
63	if (family == AF_INET6 && !resv6)
64		_res.options &= ~RES_USE_INET6;
65	*err = h_errno;
66	return h;
67}
68
69void
70freehostent(h)
71	struct hostent *h;
72{
73	/*
74	**  Stub routine -- if they don't have getipnodeby*(),
75	**  they probably don't have the free routine either.
76	*/
77
78	return;
79}
80#else /* NEEDSGETIPNODE && NETINET6 */
81#define sm_getipnodebyname getipnodebyname
82#endif /* NEEDSGETIPNODE && NETINET6 */
83
84struct hostent *
85mi_gethostbyname(name, family)
86	char *name;
87	int family;
88{
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();
95
96	h = _switch_gethostbyname_r(name, &hp, buf, sizeof(buf), &h_errno);
97# else /* SOLARIS == 20300 || SOLARIS == 203 */
98	extern struct hostent *__switch_gethostbyname();
99
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)) */
103# if NETINET6
104	int flags = AI_DEFAULT|AI_ALL;
105	int err;
106# endif /* NETINET6 */
107
108# if NETINET6
109#  if ADDRCONFIG_IS_BROKEN
110	flags &= ~AI_ADDRCONFIG;
111#  endif /* ADDRCONFIG_IS_BROKEN */
112	h = sm_getipnodebyname(name, family, flags, &err);
113	SM_SET_H_ERRNO(err);
114# else /* NETINET6 */
115	h = gethostbyname(name);
116# endif /* NETINET6 */
117
118#endif /* (SOLARIS > 10000 && SOLARIS < 20400) || (defined(SOLARIS) && SOLARIS < 204) || (defined(sony_news) && defined(__svr4)) */
119	return h;
120}
121
122#if NETINET6
123/*
124**  MI_INET_PTON -- convert printed form to network address.
125**
126**	Wrapper for inet_pton() which handles IPv6: labels.
127**
128**	Parameters:
129**		family -- address family
130**		src -- string
131**		dst -- destination address structure
132**
133**	Returns:
134**		1 if the address was valid
135**		0 if the address wasn't parseable
136**		-1 if error
137*/
138
139int
140mi_inet_pton(family, src, dst)
141	int family;
142	const char *src;
143	void *dst;
144{
145	if (family == AF_INET6 &&
146	    strncasecmp(src, "IPv6:", 5) == 0)
147		src += 5;
148	return inet_pton(family, src, dst);
149}
150#endif /* NETINET6 */
151