1/*
2 * ++Copyright++ 1985, 1988, 1993
3 * -
4 * Copyright (c) 1985, 1988, 1993
5 *    The Regents of the University of California.  All rights reserved.
6 *
7 * Portions copyright (c) 1999, 2000
8 * Intel Corporation.
9 * All rights reserved.
10 *
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
13 * are met:
14 *
15 * 1. Redistributions of source code must retain the above copyright
16 *    notice, this list of conditions and the following disclaimer.
17 *
18 * 2. Redistributions in binary form must reproduce the above copyright
19 *    notice, this list of conditions and the following disclaimer in the
20 *    documentation and/or other materials provided with the distribution.
21 *
22 * 3. All advertising materials mentioning features or use of this software
23 *    must display the following acknowledgement:
24 *
25 *    This product includes software developed by the University of
26 *    California, Berkeley, Intel Corporation, and its contributors.
27 *
28 * 4. Neither the name of University, Intel Corporation, or their respective
29 *    contributors may be used to endorse or promote products derived from
30 *    this software without specific prior written permission.
31 *
32 * THIS SOFTWARE IS PROVIDED BY THE REGENTS, INTEL CORPORATION AND
33 * CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING,
34 * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
35 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS,
36 * INTEL CORPORATION OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
37 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
38 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
39 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
40 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
41 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
42 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
43 * -
44 * Portions Copyright (c) 1993 by Digital Equipment Corporation.
45 *
46 * Permission to use, copy, modify, and distribute this software for any
47 * purpose with or without fee is hereby granted, provided that the above
48 * copyright notice and this permission notice appear in all copies, and that
49 * the name of Digital Equipment Corporation not be used in advertising or
50 * publicity pertaining to distribution of the document or software without
51 * specific, written prior permission.
52 *
53 * THE SOFTWARE IS PROVIDED "AS IS" AND DIGITAL EQUIPMENT CORP. DISCLAIMS ALL
54 * WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES
55 * OF MERCHANTABILITY AND FITNESS.   IN NO EVENT SHALL DIGITAL EQUIPMENT
56 * CORPORATION BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
57 * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
58 * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
59 * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
60 * SOFTWARE.
61 * -
62 * --Copyright--
63 */
64
65#if defined(LIBC_SCCS) && !defined(lint)
66static char sccsid[] = "@(#)gethostnamadr.c	8.1 (Berkeley) 6/4/93";
67static char rcsid[] = "$Id: map_v4v6.c,v 1.1.1.1 2003/11/19 01:51:31 kyu3 Exp $";
68#endif /* LIBC_SCCS and not lint */
69
70#include <sys/types.h>
71#include <sys/param.h>
72#include <sys/socket.h>
73#include <netinet/in.h>
74#include <arpa/inet.h>
75#include <arpa/nameser.h>
76
77#include <stdio.h>
78#include <string.h>
79#include <netdb.h>
80#include <resolv.h>
81#include <ctype.h>
82#include <errno.h>
83#ifdef _ORG_FREEBSD_
84#include <syslog.h>
85#endif
86#include "Socklib_internals.h"
87
88typedef union {
89	int32_t al;
90	char ac;
91} align;
92
93void
94_map_v4v6_address(const char *src, char *dst)
95{
96	u_char *p = (u_char *)dst;
97	char tmp[INADDRSZ];
98	int i;
99
100	/* Stash a temporary copy so our caller can update in place. */
101	bcopy(src, tmp, INADDRSZ);
102	/* Mark this ipv6 addr as a mapped ipv4. */
103	for (i = 0; i < 10; i++)
104		*p++ = 0x00;
105	*p++ = 0xff;
106	*p++ = 0xff;
107	/* Retrieve the saved copy and we're done. */
108	bcopy(tmp, (void*)p, INADDRSZ);
109}
110
111void
112_map_v4v6_hostent(struct hostent *hp, char **bpp, int *lenp)
113{
114	char **ap;
115
116	if (hp->h_addrtype != AF_INET || hp->h_length != INADDRSZ)
117		return;
118	hp->h_addrtype = AF_INET6;
119	hp->h_length = IN6ADDRSZ;
120	for (ap = hp->h_addr_list; *ap; ap++) {
121		int i = (int)(sizeof(align) - ((size_t)*bpp % sizeof(align)));
122
123		if (*lenp < (i + IN6ADDRSZ)) {
124			/* Out of memory.  Truncate address list here.  XXX */
125			*ap = NULL;
126			return;
127		}
128		*bpp += i;
129		*lenp -= i;
130		_map_v4v6_address(*ap, *bpp);
131		*ap = *bpp;
132		*bpp += IN6ADDRSZ;
133		*lenp -= IN6ADDRSZ;
134	}
135}
136