sm_gethost.c revision 141858
1173899Sjkoshy/* 2173899Sjkoshy * Copyright (c) 1999-2001, 2004 Sendmail, Inc. and its suppliers. 3173899Sjkoshy * All rights reserved. 4173899Sjkoshy * 5173899Sjkoshy * By using this file, you agree to the terms and conditions set 6173899Sjkoshy * forth in the LICENSE file which can be found at the top level of 7173899Sjkoshy * the sendmail distribution. 8173899Sjkoshy * 9173899Sjkoshy */ 10173899Sjkoshy 11173899Sjkoshy#include <sm/gen.h> 12231871SbruefferSM_RCSID("@(#)$Id: sm_gethost.c,v 8.27 2004/08/20 21:12:37 ca Exp $") 13231871Sbrueffer 14231871Sbrueffer#include <sendmail.h> 15231871Sbrueffer#if NETINET || NETINET6 16231871Sbrueffer# include <arpa/inet.h> 17231871Sbrueffer#endif /* NETINET || NETINET6 */ 18231871Sbrueffer#include "libmilter.h" 19231871Sbrueffer 20231871Sbrueffer/* 21231871Sbrueffer** MI_GETHOSTBY{NAME,ADDR} -- compatibility routines for gethostbyXXX 22231871Sbrueffer** 23173899Sjkoshy** Some operating systems have wierd problems with the gethostbyXXX 24173899Sjkoshy** routines. For example, Solaris versions at least through 2.3 25173899Sjkoshy** don't properly deliver a canonical h_name field. This tries to 26173899Sjkoshy** work around these problems. 27206622Suqs** 28173899Sjkoshy** Support IPv6 as well as IPv4. 29173899Sjkoshy*/ 30173899Sjkoshy 31173899Sjkoshy#if NETINET6 && NEEDSGETIPNODE 32173899Sjkoshy 33173899Sjkoshystatic struct hostent *getipnodebyname __P((char *, int, int, int *)); 34173899Sjkoshy 35173899Sjkoshy# ifndef AI_ADDRCONFIG 36173899Sjkoshy# define AI_ADDRCONFIG 0 /* dummy */ 37173899Sjkoshy# endif /* ! AI_ADDRCONFIG */ 38173899Sjkoshy# ifndef AI_ALL 39173899Sjkoshy# define AI_ALL 0 /* dummy */ 40173899Sjkoshy# endif /* ! AI_ALL */ 41173899Sjkoshy# ifndef AI_DEFAULT 42173899Sjkoshy# define AI_DEFAULT 0 /* dummy */ 43173899Sjkoshy# endif /* ! AI_DEFAULT */ 44173899Sjkoshy 45173899Sjkoshystatic struct hostent * 46173899Sjkoshygetipnodebyname(name, family, flags, err) 47173899Sjkoshy char *name; 48173899Sjkoshy int family; 49173899Sjkoshy int flags; 50173899Sjkoshy int *err; 51173899Sjkoshy{ 52173899Sjkoshy bool resv6 = true; 53173899Sjkoshy struct hostent *h; 54173899Sjkoshy 55173899Sjkoshy if (family == AF_INET6) 56173899Sjkoshy { 57173899Sjkoshy /* From RFC2133, section 6.1 */ 58173899Sjkoshy resv6 = bitset(RES_USE_INET6, _res.options); 59173899Sjkoshy _res.options |= RES_USE_INET6; 60173899Sjkoshy } 61173899Sjkoshy SM_SET_H_ERRNO(0); 62173899Sjkoshy h = gethostbyname(name); 63173899Sjkoshy if (family == AF_INET6 && !resv6) 64173899Sjkoshy _res.options &= ~RES_USE_INET6; 65173899Sjkoshy *err = h_errno; 66173899Sjkoshy return h; 67173899Sjkoshy} 68173899Sjkoshy 69173899Sjkoshyvoid 70173899Sjkoshyfreehostent(h) 71173899Sjkoshy struct hostent *h; 72173899Sjkoshy{ 73173899Sjkoshy /* 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#endif /* NEEDSGETIPNODE && NETINET6 */ 81 82struct hostent * 83mi_gethostbyname(name, family) 84 char *name; 85 int family; 86{ 87 struct hostent *h = NULL; 88#if (SOLARIS > 10000 && SOLARIS < 20400) || (defined(SOLARIS) && SOLARIS < 204) || (defined(sony_news) && defined(__svr4)) 89# if SOLARIS == 20300 || SOLARIS == 203 90 static struct hostent hp; 91 static char buf[1000]; 92 extern struct hostent *_switch_gethostbyname_r(); 93 94 h = _switch_gethostbyname_r(name, &hp, buf, sizeof(buf), &h_errno); 95# else /* SOLARIS == 20300 || SOLARIS == 203 */ 96 extern struct hostent *__switch_gethostbyname(); 97 98 h = __switch_gethostbyname(name); 99# endif /* SOLARIS == 20300 || SOLARIS == 203 */ 100#else /* (SOLARIS > 10000 && SOLARIS < 20400) || (defined(SOLARIS) && SOLARIS < 204) || (defined(sony_news) && defined(__svr4)) */ 101# if NETINET6 102 int flags = AI_DEFAULT|AI_ALL; 103 int err; 104# endif /* NETINET6 */ 105 106# if NETINET6 107# if ADDRCONFIG_IS_BROKEN 108 flags &= ~AI_ADDRCONFIG; 109# endif /* ADDRCONFIG_IS_BROKEN */ 110 h = getipnodebyname(name, family, flags, &err); 111 SM_SET_H_ERRNO(err); 112# else /* NETINET6 */ 113 h = gethostbyname(name); 114# endif /* NETINET6 */ 115 116#endif /* (SOLARIS > 10000 && SOLARIS < 20400) || (defined(SOLARIS) && SOLARIS < 204) || (defined(sony_news) && defined(__svr4)) */ 117 return h; 118} 119 120#if NETINET6 121/* 122** MI_INET_PTON -- convert printed form to network address. 123** 124** Wrapper for inet_pton() which handles IPv6: labels. 125** 126** Parameters: 127** family -- address family 128** src -- string 129** dst -- destination address structure 130** 131** Returns: 132** 1 if the address was valid 133** 0 if the address wasn't parseable 134** -1 if error 135*/ 136 137int 138mi_inet_pton(family, src, dst) 139 int family; 140 const char *src; 141 void *dst; 142{ 143 if (family == AF_INET6 && 144 strncasecmp(src, "IPv6:", 5) == 0) 145 src += 5; 146 return inet_pton(family, src, dst); 147} 148#endif /* NETINET6 */ 149