1/*
2 * Copyright (c) 2003 Apple Computer, Inc. All rights reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. Please obtain a copy of the License at
10 * http://www.opensource.apple.com/apsl/ and read it before using this
11 * file.
12 *
13 * The Original Code and all software distributed under the License are
14 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
15 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
16 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
18 * Please see the License for the specific language governing rights and
19 * limitations under the License.
20 *
21 * @APPLE_LICENSE_HEADER_END@
22 */
23/*
24 * eui64.h - EUI64 routines for IPv6CP.
25 *
26 * Copyright (c) 1999 Tommi Komulainen.  All rights reserved.
27 *
28 * Redistribution and use in source and binary forms, with or without
29 * modification, are permitted provided that the following conditions
30 * are met:
31 *
32 * 1. Redistributions of source code must retain the above copyright
33 *    notice, this list of conditions and the following disclaimer.
34 *
35 * 2. Redistributions in binary form must reproduce the above copyright
36 *    notice, this list of conditions and the following disclaimer in
37 *    the documentation and/or other materials provided with the
38 *    distribution.
39 *
40 * 3. The name(s) of the authors of this software must not be used to
41 *    endorse or promote products derived from this software without
42 *    prior written permission.
43 *
44 * 4. Redistributions of any form whatsoever must retain the following
45 *    acknowledgment:
46 *    "This product includes software developed by Tommi Komulainen
47 *     <Tommi.Komulainen@iki.fi>".
48 *
49 * THE AUTHORS OF THIS SOFTWARE DISCLAIM ALL WARRANTIES WITH REGARD TO
50 * THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
51 * AND FITNESS, IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY
52 * SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
53 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN
54 * AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING
55 * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
56 *
57 * $Id: eui64.h,v 1.4 2004/02/10 19:29:23 callie Exp $
58*/
59
60#ifndef __EUI64_H__
61#define __EUI64_H__
62
63#if !defined(INET6)
64#error	"this file should only be included when INET6 is defined"
65#endif /* not defined(INET6) */
66
67#if defined(SOL2)
68#include <netinet/in.h>
69
70typedef union {
71    uint8_t	e8[8];		/* lower 64-bit IPv6 address */
72    uint32_t	e32[2];		/* lower 64-bit IPv6 address */
73} eui64_t;
74
75/*
76 * Declare the two below, since in.h only defines them when _KERNEL
77 * is declared - which shouldn't be true when dealing with user-land programs
78 */
79#define	s6_addr8	_S6_un._S6_u8
80#define	s6_addr32	_S6_un._S6_u32
81
82#else /* else if not defined(SOL2) */
83
84/*
85 * TODO:
86 *
87 * Maybe this should be done by processing struct in6_addr directly...
88 */
89typedef union
90{
91    u_int8_t e8[8];
92    u_int16_t e16[4];
93    u_int32_t e32[2];
94} eui64_t;
95
96#endif /* defined(SOL2) */
97
98#define eui64_iszero(e)		(((e).e32[0] | (e).e32[1]) == 0)
99#define eui64_equals(e, o)	(((e).e32[0] == (o).e32[0]) && \
100				((e).e32[1] == (o).e32[1]))
101#define eui64_zero(e)		(e).e32[0] = (e).e32[1] = 0;
102
103#define eui64_copy(s, d)	memcpy(&(d), &(s), sizeof(eui64_t))
104
105#define eui64_magic(e)		do {			\
106				(e).e32[0] = magic();	\
107				(e).e32[1] = magic();	\
108				(e).e8[0] &= ~2;	\
109				} while (0)
110#define eui64_magic_nz(x)	do {				\
111				eui64_magic(x);			\
112				} while (eui64_iszero(x))
113#define eui64_magic_ne(x, y)	do {				\
114				eui64_magic(x);			\
115				} while (eui64_equals(x, y))
116
117#define eui64_get(ll, cp)	do {				\
118				eui64_copy((*cp), (ll));	\
119				(cp) += sizeof(eui64_t);	\
120				} while (0)
121
122#define eui64_put(ll, cp)	do {				\
123				eui64_copy((ll), (*cp));	\
124				(cp) += sizeof(eui64_t);	\
125				} while (0)
126
127#define eui64_set32(e, l)	do {			\
128				(e).e32[0] = 0;		\
129				(e).e32[1] = htonl(l);	\
130				} while (0)
131#define eui64_setlo32(e, l)	eui64_set32(e, l)
132
133char *eui64_ntoa __P((eui64_t));	/* Returns ascii representation of id */
134
135#endif /* __EUI64_H__ */
136
137