freebsd-dev/contrib/sendmail/libmilter/sm_gethost.c

190 lines
4.3 KiB
C
Raw Normal View History

/*
2014-01-26 20:46:55 +00:00
* Copyright (c) 1999-2001, 2004, 2010, 2013 Proofpoint, Inc. and its suppliers.
* All rights reserved.
*
* By using this file, you agree to the terms and conditions set
* forth in the LICENSE file which can be found at the top level of
* the sendmail distribution.
*
*/
2002-02-17 21:56:45 +00:00
#include <sm/gen.h>
2014-05-22 03:45:17 +00:00
SM_RCSID("@(#)$Id: sm_gethost.c,v 8.32 2013-11-22 20:51:36 ca Exp $")
#include <sendmail.h>
#if NETINET || NETINET6
# include <arpa/inet.h>
2020-07-14 21:40:53 +00:00
#endif
2005-02-14 02:30:00 +00:00
#include "libmilter.h"
2002-02-17 21:56:45 +00:00
/*
** MI_GETHOSTBY{NAME,ADDR} -- compatibility routines for gethostbyXXX
**
2020-07-14 21:40:53 +00:00
** Some operating systems have weird problems with the gethostbyXXX
** routines. For example, Solaris versions at least through 2.3
** don't properly deliver a canonical h_name field. This tries to
** work around these problems.
**
** Support IPv6 as well as IPv4.
*/
2001-05-28 17:08:52 +00:00
#if NETINET6 && NEEDSGETIPNODE
2011-06-14 03:55:17 +00:00
static struct hostent *sm_getipnodebyname __P((const char *, int, int, int *));
2005-02-14 02:30:00 +00:00
2001-08-01 01:33:27 +00:00
# ifndef AI_ADDRCONFIG
# define AI_ADDRCONFIG 0 /* dummy */
2020-07-14 21:40:53 +00:00
# endif
# ifndef AI_ALL
# define AI_ALL 0 /* dummy */
2020-07-14 21:40:53 +00:00
# endif
2001-08-01 01:33:27 +00:00
# ifndef AI_DEFAULT
# define AI_DEFAULT 0 /* dummy */
2020-07-14 21:40:53 +00:00
# endif
static struct hostent *
2011-06-14 03:55:17 +00:00
sm_getipnodebyname(name, family, flags, err)
const char *name;
int family;
int flags;
int *err;
{
struct hostent *h;
2020-07-14 21:40:53 +00:00
# if HAS_GETHOSTBYNAME2
h = gethostbyname2(name, family);
if (h == NULL)
*err = h_errno;
return h;
# else /* HAS_GETHOSTBYNAME2 */
# ifdef RES_USE_INET6
bool resv6 = true;
if (family == AF_INET6)
{
/* From RFC2133, section 6.1 */
resv6 = bitset(RES_USE_INET6, _res.options);
_res.options |= RES_USE_INET6;
}
2020-07-14 21:40:53 +00:00
# endif /* RES_USE_INET6 */
2001-02-28 00:19:57 +00:00
SM_SET_H_ERRNO(0);
h = gethostbyname(name);
2020-07-14 21:40:53 +00:00
# ifdef RES_USE_INET6
if (!resv6)
_res.options &= ~RES_USE_INET6;
2020-07-14 21:40:53 +00:00
# endif
2014-01-26 20:46:55 +00:00
/* the function is supposed to return only the requested family */
if (h != NULL && h->h_addrtype != family)
{
2020-07-14 21:40:53 +00:00
# if NETINET6
2014-01-26 20:46:55 +00:00
freehostent(h);
2020-07-14 21:40:53 +00:00
# endif
2014-01-26 20:46:55 +00:00
h = NULL;
*err = NO_DATA;
}
else
*err = h_errno;
return h;
2020-07-14 21:40:53 +00:00
# endif /* HAS_GETHOSTBYNAME2 */
}
2001-01-21 22:17:06 +00:00
void
freehostent(h)
struct hostent *h;
{
/*
** Stub routine -- if they don't have getipnodeby*(),
** they probably don't have the free routine either.
*/
return;
}
2011-06-14 03:55:17 +00:00
#else /* NEEDSGETIPNODE && NETINET6 */
2020-07-14 21:40:53 +00:00
#define sm_getipnodebyname getipnodebyname
2001-05-28 17:08:52 +00:00
#endif /* NEEDSGETIPNODE && NETINET6 */
struct hostent *
mi_gethostbyname(name, family)
char *name;
int family;
{
struct hostent *h = NULL;
#if (SOLARIS > 10000 && SOLARIS < 20400) || (defined(SOLARIS) && SOLARIS < 204) || (defined(sony_news) && defined(__svr4))
# if SOLARIS == 20300 || SOLARIS == 203
static struct hostent hp;
static char buf[1000];
extern struct hostent *_switch_gethostbyname_r();
h = _switch_gethostbyname_r(name, &hp, buf, sizeof(buf), &h_errno);
# else /* SOLARIS == 20300 || SOLARIS == 203 */
extern struct hostent *__switch_gethostbyname();
h = __switch_gethostbyname(name);
# endif /* SOLARIS == 20300 || SOLARIS == 203 */
#else /* (SOLARIS > 10000 && SOLARIS < 20400) || (defined(SOLARIS) && SOLARIS < 204) || (defined(sony_news) && defined(__svr4)) */
# if NETINET6
2013-04-21 16:35:04 +00:00
# ifndef SM_IPNODEBYNAME_FLAGS
/* For IPv4-mapped addresses, use: AI_DEFAULT|AI_ALL */
# define SM_IPNODEBYNAME_FLAGS AI_ADDRCONFIG
2020-07-14 21:40:53 +00:00
# endif
2013-04-21 16:35:04 +00:00
int flags = SM_IPNODEBYNAME_FLAGS;
int err;
# endif /* NETINET6 */
# if NETINET6
2001-08-01 01:33:27 +00:00
# if ADDRCONFIG_IS_BROKEN
flags &= ~AI_ADDRCONFIG;
2020-07-14 21:40:53 +00:00
# endif
2011-06-14 03:55:17 +00:00
h = sm_getipnodebyname(name, family, flags, &err);
2001-02-28 00:19:57 +00:00
SM_SET_H_ERRNO(err);
# else /* NETINET6 */
h = gethostbyname(name);
# endif /* NETINET6 */
#endif /* (SOLARIS > 10000 && SOLARIS < 20400) || (defined(SOLARIS) && SOLARIS < 204) || (defined(sony_news) && defined(__svr4)) */
2014-01-26 20:46:55 +00:00
/* the function is supposed to return only the requested family */
if (h != NULL && h->h_addrtype != family)
{
# if NETINET6
freehostent(h);
2020-07-14 21:40:53 +00:00
# endif
2014-01-26 20:46:55 +00:00
h = NULL;
SM_SET_H_ERRNO(NO_DATA);
}
return h;
}
2002-02-17 21:56:45 +00:00
#if NETINET6
/*
** MI_INET_PTON -- convert printed form to network address.
**
** Wrapper for inet_pton() which handles IPv6: labels.
**
** Parameters:
** family -- address family
** src -- string
** dst -- destination address structure
**
** Returns:
** 1 if the address was valid
2020-07-14 21:40:53 +00:00
** 0 if the address wasn't parsable
2002-02-17 21:56:45 +00:00
** -1 if error
*/
int
mi_inet_pton(family, src, dst)
int family;
const char *src;
void *dst;
{
if (family == AF_INET6 &&
strncasecmp(src, "IPv6:", 5) == 0)
src += 5;
return inet_pton(family, src, dst);
}
#endif /* NETINET6 */