xref: /freebsd/contrib/sendmail/libmilter/sm_gethost.c (revision 2357939bc239bd5334a169b62313806178dd8f30)
1 /*
2  *  Copyright (c) 1999-2001 Sendmail, Inc. and its suppliers.
3  *	All rights reserved.
4  *
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.
8  *
9  */
10 
11 #include <sm/gen.h>
12 SM_RCSID("@(#)$Id: sm_gethost.c,v 8.26 2001/09/11 04:04:45 gshapiro Exp $")
13 
14 #include <sendmail.h>
15 #if NETINET || NETINET6
16 # include <arpa/inet.h>
17 #endif /* NETINET || NETINET6 */
18 
19 /*
20 **  MI_GETHOSTBY{NAME,ADDR} -- compatibility routines for gethostbyXXX
21 **
22 **	Some operating systems have wierd problems with the gethostbyXXX
23 **	routines.  For example, Solaris versions at least through 2.3
24 **	don't properly deliver a canonical h_name field.  This tries to
25 **	work around these problems.
26 **
27 **	Support IPv6 as well as IPv4.
28 */
29 
30 #if NETINET6 && NEEDSGETIPNODE
31 
32 # ifndef AI_ADDRCONFIG
33 #  define AI_ADDRCONFIG	0	/* dummy */
34 # endif /* ! AI_ADDRCONFIG */
35 # ifndef AI_ALL
36 #  define AI_ALL	0	/* dummy */
37 # endif /* ! AI_ALL */
38 # ifndef AI_DEFAULT
39 #  define AI_DEFAULT	0	/* dummy */
40 # endif /* ! AI_DEFAULT */
41 
42 static struct hostent *
43 getipnodebyname(name, family, flags, err)
44 	char *name;
45 	int family;
46 	int flags;
47 	int *err;
48 {
49 	bool resv6 = true;
50 	struct hostent *h;
51 
52 	if (family == AF_INET6)
53 	{
54 		/* From RFC2133, section 6.1 */
55 		resv6 = bitset(RES_USE_INET6, _res.options);
56 		_res.options |= RES_USE_INET6;
57 	}
58 	SM_SET_H_ERRNO(0);
59 	h = gethostbyname(name);
60 	if (family == AF_INET6 && !resv6)
61 		_res.options &= ~RES_USE_INET6;
62 	*err = h_errno;
63 	return h;
64 }
65 
66 void
67 freehostent(h)
68 	struct hostent *h;
69 {
70 	/*
71 	**  Stub routine -- if they don't have getipnodeby*(),
72 	**  they probably don't have the free routine either.
73 	*/
74 
75 	return;
76 }
77 #endif /* NEEDSGETIPNODE && NETINET6 */
78 
79 struct hostent *
80 mi_gethostbyname(name, family)
81 	char *name;
82 	int family;
83 {
84 	struct hostent *h = NULL;
85 #if (SOLARIS > 10000 && SOLARIS < 20400) || (defined(SOLARIS) && SOLARIS < 204) || (defined(sony_news) && defined(__svr4))
86 # if SOLARIS == 20300 || SOLARIS == 203
87 	static struct hostent hp;
88 	static char buf[1000];
89 	extern struct hostent *_switch_gethostbyname_r();
90 
91 	h = _switch_gethostbyname_r(name, &hp, buf, sizeof(buf), &h_errno);
92 # else /* SOLARIS == 20300 || SOLARIS == 203 */
93 	extern struct hostent *__switch_gethostbyname();
94 
95 	h = __switch_gethostbyname(name);
96 # endif /* SOLARIS == 20300 || SOLARIS == 203 */
97 #else /* (SOLARIS > 10000 && SOLARIS < 20400) || (defined(SOLARIS) && SOLARIS < 204) || (defined(sony_news) && defined(__svr4)) */
98 # if NETINET6
99 	int flags = AI_DEFAULT|AI_ALL;
100 	int err;
101 # endif /* NETINET6 */
102 
103 # if NETINET6
104 #  if ADDRCONFIG_IS_BROKEN
105 	flags &= ~AI_ADDRCONFIG;
106 #  endif /* ADDRCONFIG_IS_BROKEN */
107 	h = getipnodebyname(name, family, flags, &err);
108 	SM_SET_H_ERRNO(err);
109 # else /* NETINET6 */
110 	h = gethostbyname(name);
111 # endif /* NETINET6 */
112 
113 #endif /* (SOLARIS > 10000 && SOLARIS < 20400) || (defined(SOLARIS) && SOLARIS < 204) || (defined(sony_news) && defined(__svr4)) */
114 	return h;
115 }
116 
117 #if NETINET6
118 /*
119 **  MI_INET_PTON -- convert printed form to network address.
120 **
121 **	Wrapper for inet_pton() which handles IPv6: labels.
122 **
123 **	Parameters:
124 **		family -- address family
125 **		src -- string
126 **		dst -- destination address structure
127 **
128 **	Returns:
129 **		1 if the address was valid
130 **		0 if the address wasn't parseable
131 **		-1 if error
132 */
133 
134 int
135 mi_inet_pton(family, src, dst)
136 	int family;
137 	const char *src;
138 	void *dst;
139 {
140 	if (family == AF_INET6 &&
141 	    strncasecmp(src, "IPv6:", 5) == 0)
142 		src += 5;
143 	return inet_pton(family, src, dst);
144 }
145 #endif /* NETINET6 */
146