map_v4v6.c revision 298226
1327952Sdim/*
2198092Srdivacky * ++Copyright++ 1985, 1988, 1993
3353358Sdim * -
4353358Sdim * Copyright (c) 1985, 1988, 1993
5353358Sdim *    The Regents of the University of California.  All rights reserved.
6198092Srdivacky *
7198092Srdivacky * Redistribution and use in source and binary forms, with or without
8198092Srdivacky * modification, are permitted provided that the following conditions
9198092Srdivacky * are met:
10198092Srdivacky * 1. Redistributions of source code must retain the above copyright
11198092Srdivacky *    notice, this list of conditions and the following disclaimer.
12198092Srdivacky * 2. Redistributions in binary form must reproduce the above copyright
13198092Srdivacky *    notice, this list of conditions and the following disclaimer in the
14198092Srdivacky *    documentation and/or other materials provided with the distribution.
15198092Srdivacky * 4. Neither the name of the University nor the names of its contributors
16198092Srdivacky *    may be used to endorse or promote products derived from this software
17198092Srdivacky *    without specific prior written permission.
18280031Sdim *
19280031Sdim * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20198092Srdivacky * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21249423Sdim * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22249423Sdim * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23327952Sdim * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24327952Sdim * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25204643Srdivacky * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26218893Sdim * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27210299Sed * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28327952Sdim * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29198092Srdivacky * SUCH DAMAGE.
30198092Srdivacky * -
31341825Sdim * Portions Copyright (c) 1993 by Digital Equipment Corporation.
32198092Srdivacky *
33198398Srdivacky * Permission to use, copy, modify, and distribute this software for any
34327952Sdim * purpose with or without fee is hereby granted, provided that the above
35198092Srdivacky * copyright notice and this permission notice appear in all copies, and that
36198092Srdivacky * the name of Digital Equipment Corporation not be used in advertising or
37198092Srdivacky * publicity pertaining to distribution of the document or software without
38198398Srdivacky * specific, written prior permission.
39296417Sdim *
40296417Sdim * THE SOFTWARE IS PROVIDED "AS IS" AND DIGITAL EQUIPMENT CORP. DISCLAIMS ALL
41296417Sdim * WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES
42296417Sdim * OF MERCHANTABILITY AND FITNESS.   IN NO EVENT SHALL DIGITAL EQUIPMENT
43296417Sdim * CORPORATION BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
44296417Sdim * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
45198092Srdivacky * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
46198092Srdivacky * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
47198092Srdivacky * SOFTWARE.
48198398Srdivacky * -
49341825Sdim * --Copyright--
50198092Srdivacky */
51198092Srdivacky
52198092Srdivacky#if defined(LIBC_SCCS) && !defined(lint)
53198092Srdivackystatic char sccsid[] = "@(#)gethostnamadr.c	8.1 (Berkeley) 6/4/93";
54341825Sdim#endif /* LIBC_SCCS and not lint */
55198092Srdivacky#include <sys/cdefs.h>
56198092Srdivacky__FBSDID("$FreeBSD: head/lib/libc/net/map_v4v6.c 298226 2016-04-18 21:05:15Z avos $");
57341825Sdim
58198092Srdivacky#include <sys/param.h>
59198092Srdivacky#include <sys/socket.h>
60327952Sdim#include <netinet/in.h>
61327952Sdim#include <arpa/inet.h>
62327952Sdim#include <arpa/nameser.h>
63327952Sdim
64198092Srdivacky#include <stdio.h>
65198092Srdivacky#include <string.h>
66327952Sdim#include <netdb.h>
67198092Srdivacky#include <resolv.h>
68198092Srdivacky#include <ctype.h>
69341825Sdim#include <syslog.h>
70198092Srdivacky#include "netdb_private.h"
71276479Sdim
72198092Srdivackytypedef union {
73198092Srdivacky	int32_t al;
74198092Srdivacky	char ac;
75198092Srdivacky} align;
76341825Sdim
77327952Sdimvoid
78327952Sdim_map_v4v6_address(const char *src, char *dst)
79327952Sdim{
80327952Sdim	/* Our caller may update in place. */
81327952Sdim	memmove(&dst[12], src, NS_INADDRSZ);
82341825Sdim	/* Mark this ipv6 addr as a mapped ipv4. */
83327952Sdim	memset(&dst[10], 0xff, 2);
84327952Sdim	memset(&dst[0], 0, 10);
85341825Sdim}
86327952Sdim
87327952Sdimvoid
88327952Sdim_map_v4v6_hostent(struct hostent *hp, char **bpp, char *ep) {
89327952Sdim	char **ap;
90341825Sdim
91198092Srdivacky	if (hp->h_addrtype != AF_INET || hp->h_length != INADDRSZ)
92198092Srdivacky		return;
93198092Srdivacky	hp->h_addrtype = AF_INET6;
94198092Srdivacky	hp->h_length = IN6ADDRSZ;
95198092Srdivacky	for (ap = hp->h_addr_list; *ap; ap++) {
96341825Sdim		int i = (u_long)*bpp % sizeof(align);
97198092Srdivacky
98327952Sdim		if (i != 0)
99198092Srdivacky			i = sizeof(align) - i;
100327952Sdim
101327952Sdim		if ((ep - *bpp) < (i + IN6ADDRSZ)) {
102327952Sdim			/* Out of memory.  Truncate address list here. */
103327952Sdim			*ap = NULL;
104341825Sdim			return;
105198092Srdivacky		}
106198092Srdivacky		*bpp += i;
107198092Srdivacky		_map_v4v6_address(*ap, *bpp);
108198092Srdivacky		*ap = *bpp;
109198092Srdivacky		*bpp += IN6ADDRSZ;
110198092Srdivacky	}
111198092Srdivacky}
112198092Srdivacky