1331722Seadler/*
217903Speter * ++Copyright++ 1985, 1988, 1993
317903Speter * -
417903Speter * Copyright (c) 1985, 1988, 1993
517903Speter *    The Regents of the University of California.  All rights reserved.
617903Speter *
717903Speter * Redistribution and use in source and binary forms, with or without
817903Speter * modification, are permitted provided that the following conditions
917903Speter * are met:
1017903Speter * 1. Redistributions of source code must retain the above copyright
1117903Speter *    notice, this list of conditions and the following disclaimer.
1217903Speter * 2. Redistributions in binary form must reproduce the above copyright
1317903Speter *    notice, this list of conditions and the following disclaimer in the
1417903Speter *    documentation and/or other materials provided with the distribution.
1517903Speter * 4. Neither the name of the University nor the names of its contributors
1617903Speter *    may be used to endorse or promote products derived from this software
1717903Speter *    without specific prior written permission.
1817903Speter *
1917903Speter * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
2017903Speter * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2117903Speter * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2217903Speter * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
2317903Speter * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2417903Speter * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2517903Speter * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2617903Speter * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2717903Speter * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2817903Speter * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2917903Speter * SUCH DAMAGE.
3017903Speter * -
3117903Speter * Portions Copyright (c) 1993 by Digital Equipment Corporation.
3217903Speter *
3317903Speter * Permission to use, copy, modify, and distribute this software for any
3417903Speter * purpose with or without fee is hereby granted, provided that the above
3517903Speter * copyright notice and this permission notice appear in all copies, and that
3617903Speter * the name of Digital Equipment Corporation not be used in advertising or
3717903Speter * publicity pertaining to distribution of the document or software without
3817903Speter * specific, written prior permission.
3917903Speter *
4017903Speter * THE SOFTWARE IS PROVIDED "AS IS" AND DIGITAL EQUIPMENT CORP. DISCLAIMS ALL
4117903Speter * WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES
4217903Speter * OF MERCHANTABILITY AND FITNESS.   IN NO EVENT SHALL DIGITAL EQUIPMENT
4317903Speter * CORPORATION BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
4417903Speter * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
4517903Speter * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
4617903Speter * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
4717903Speter * SOFTWARE.
4817903Speter * -
4917903Speter * --Copyright--
5017903Speter */
5117903Speter
5217903Speter#if defined(LIBC_SCCS) && !defined(lint)
5317903Speterstatic char sccsid[] = "@(#)gethostnamadr.c	8.1 (Berkeley) 6/4/93";
5417903Speter#endif /* LIBC_SCCS and not lint */
5592986Sobrien#include <sys/cdefs.h>
5692986Sobrien__FBSDID("$FreeBSD$");
5717903Speter
5817903Speter#include <sys/param.h>
5917903Speter#include <sys/socket.h>
6017903Speter#include <netinet/in.h>
6117903Speter#include <arpa/inet.h>
6217903Speter#include <arpa/nameser.h>
6317903Speter
6417903Speter#include <stdio.h>
6521057Speter#include <string.h>
6617903Speter#include <netdb.h>
6717903Speter#include <resolv.h>
6817903Speter#include <ctype.h>
6917903Speter#include <syslog.h>
70288015Srodrigc#include "netdb_private.h"
7117903Speter
7217903Spetertypedef union {
7317903Speter	int32_t al;
7417903Speter	char ac;
7517903Speter} align;
7617903Speter
7717903Spetervoid
78145635Sume_map_v4v6_address(const char *src, char *dst)
7917903Speter{
80292595Sume	/* Our caller may update in place. */
81292595Sume	memmove(&dst[12], src, NS_INADDRSZ);
8217903Speter	/* Mark this ipv6 addr as a mapped ipv4. */
83292595Sume	memset(&dst[10], 0xff, 2);
84292550Sume	memset(&dst[0], 0, 10);
8517903Speter}
8617903Speter
8717903Spetervoid
88145635Sume_map_v4v6_hostent(struct hostent *hp, char **bpp, char *ep) {
8917903Speter	char **ap;
9017903Speter
9117903Speter	if (hp->h_addrtype != AF_INET || hp->h_length != INADDRSZ)
9217903Speter		return;
9317903Speter	hp->h_addrtype = AF_INET6;
9417903Speter	hp->h_length = IN6ADDRSZ;
9517903Speter	for (ap = hp->h_addr_list; *ap; ap++) {
96145635Sume		int i = (u_long)*bpp % sizeof(align);
9717903Speter
98145635Sume		if (i != 0)
99145635Sume			i = sizeof(align) - i;
100145635Sume
101145635Sume		if ((ep - *bpp) < (i + IN6ADDRSZ)) {
102145635Sume			/* Out of memory.  Truncate address list here. */
10317903Speter			*ap = NULL;
10417903Speter			return;
10517903Speter		}
10617903Speter		*bpp += i;
10717903Speter		_map_v4v6_address(*ap, *bpp);
10817903Speter		*ap = *bpp;
10917903Speter		*bpp += IN6ADDRSZ;
11017903Speter	}
11117903Speter}
112