map_v4v6.c revision 145635
117903Speter/*
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 * 3. All advertising materials mentioning features or use of this software
1617903Speter *    must display the following acknowledgement:
1717903Speter * 	This product includes software developed by the University of
1817903Speter * 	California, Berkeley and its contributors.
1917903Speter * 4. Neither the name of the University nor the names of its contributors
2017903Speter *    may be used to endorse or promote products derived from this software
2117903Speter *    without specific prior written permission.
2217903Speter *
2317903Speter * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
2417903Speter * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2517903Speter * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2617903Speter * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
2717903Speter * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2817903Speter * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2917903Speter * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
3017903Speter * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
3117903Speter * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
3217903Speter * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
3317903Speter * SUCH DAMAGE.
3417903Speter * -
3517903Speter * Portions Copyright (c) 1993 by Digital Equipment Corporation.
3617903Speter *
3717903Speter * Permission to use, copy, modify, and distribute this software for any
3817903Speter * purpose with or without fee is hereby granted, provided that the above
3917903Speter * copyright notice and this permission notice appear in all copies, and that
4017903Speter * the name of Digital Equipment Corporation not be used in advertising or
4117903Speter * publicity pertaining to distribution of the document or software without
4217903Speter * specific, written prior permission.
4317903Speter *
4417903Speter * THE SOFTWARE IS PROVIDED "AS IS" AND DIGITAL EQUIPMENT CORP. DISCLAIMS ALL
4517903Speter * WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES
4617903Speter * OF MERCHANTABILITY AND FITNESS.   IN NO EVENT SHALL DIGITAL EQUIPMENT
4717903Speter * CORPORATION BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
4817903Speter * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
4917903Speter * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
5017903Speter * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
5117903Speter * SOFTWARE.
5217903Speter * -
5317903Speter * --Copyright--
5417903Speter */
5517903Speter
5617903Speter#if defined(LIBC_SCCS) && !defined(lint)
5717903Speterstatic char sccsid[] = "@(#)gethostnamadr.c	8.1 (Berkeley) 6/4/93";
5817903Speter#endif /* LIBC_SCCS and not lint */
5992986Sobrien#include <sys/cdefs.h>
6092986Sobrien__FBSDID("$FreeBSD: head/lib/libc/net/map_v4v6.c 145635 2005-04-28 18:52:40Z ume $");
6117903Speter
6217903Speter#include <sys/types.h>
6317903Speter#include <sys/param.h>
6417903Speter#include <sys/socket.h>
6517903Speter#include <netinet/in.h>
6617903Speter#include <arpa/inet.h>
6717903Speter#include <arpa/nameser.h>
6817903Speter
6917903Speter#include <stdio.h>
7021057Speter#include <string.h>
7117903Speter#include <netdb.h>
7217903Speter#include <resolv.h>
7317903Speter#include <ctype.h>
7417903Speter#include <syslog.h>
7517903Speter
7617903Spetertypedef union {
7717903Speter	int32_t al;
7817903Speter	char ac;
7917903Speter} align;
8017903Speter
8117903Spetervoid
82145635Sume_map_v4v6_address(const char *src, char *dst)
8317903Speter{
8417903Speter	u_char *p = (u_char *)dst;
85145635Sume	char tmp[NS_INADDRSZ];
8617903Speter	int i;
8717903Speter
8817903Speter	/* Stash a temporary copy so our caller can update in place. */
89145635Sume	memcpy(tmp, src, NS_INADDRSZ);
9017903Speter	/* Mark this ipv6 addr as a mapped ipv4. */
9117903Speter	for (i = 0; i < 10; i++)
9217903Speter		*p++ = 0x00;
9317903Speter	*p++ = 0xff;
9417903Speter	*p++ = 0xff;
9517903Speter	/* Retrieve the saved copy and we're done. */
96145635Sume	memcpy((void*)p, tmp, NS_INADDRSZ);
9717903Speter}
9817903Speter
9917903Spetervoid
100145635Sume_map_v4v6_hostent(struct hostent *hp, char **bpp, char *ep) {
10117903Speter	char **ap;
10217903Speter
10317903Speter	if (hp->h_addrtype != AF_INET || hp->h_length != INADDRSZ)
10417903Speter		return;
10517903Speter	hp->h_addrtype = AF_INET6;
10617903Speter	hp->h_length = IN6ADDRSZ;
10717903Speter	for (ap = hp->h_addr_list; *ap; ap++) {
108145635Sume		int i = (u_long)*bpp % sizeof(align);
10917903Speter
110145635Sume		if (i != 0)
111145635Sume			i = sizeof(align) - i;
112145635Sume
113145635Sume		if ((ep - *bpp) < (i + IN6ADDRSZ)) {
114145635Sume			/* Out of memory.  Truncate address list here. */
11517903Speter			*ap = NULL;
11617903Speter			return;
11717903Speter		}
11817903Speter		*bpp += i;
11917903Speter		_map_v4v6_address(*ap, *bpp);
12017903Speter		*ap = *bpp;
12117903Speter		*bpp += IN6ADDRSZ;
12217903Speter	}
12317903Speter}
124