1*7c478bd9Sstevel@tonic-gate /*
2*7c478bd9Sstevel@tonic-gate     eui64.h - EUI64 routines for IPv6CP.
3*7c478bd9Sstevel@tonic-gate     Copyright (C) 1999  Tommi Komulainen <Tommi.Komulainen@iki.fi>
4*7c478bd9Sstevel@tonic-gate 
5*7c478bd9Sstevel@tonic-gate     Redistribution and use in source and binary forms are permitted
6*7c478bd9Sstevel@tonic-gate     provided that the above copyright notice and this paragraph are
7*7c478bd9Sstevel@tonic-gate     duplicated in all such forms and that any documentation,
8*7c478bd9Sstevel@tonic-gate     advertising materials, and other materials related to such
9*7c478bd9Sstevel@tonic-gate     distribution and use acknowledge that the software was developed
10*7c478bd9Sstevel@tonic-gate     by Tommi Komulainen.  The name of the author may not be used
11*7c478bd9Sstevel@tonic-gate     to endorse or promote products derived from this software without
12*7c478bd9Sstevel@tonic-gate     specific prior written permission.
13*7c478bd9Sstevel@tonic-gate     THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
14*7c478bd9Sstevel@tonic-gate     IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
15*7c478bd9Sstevel@tonic-gate     WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
16*7c478bd9Sstevel@tonic-gate 
17*7c478bd9Sstevel@tonic-gate 
18*7c478bd9Sstevel@tonic-gate     $Id: eui64.h,v 1.3 1999/09/30 19:56:37 masputra Exp $
19*7c478bd9Sstevel@tonic-gate */
20*7c478bd9Sstevel@tonic-gate 
21*7c478bd9Sstevel@tonic-gate #ifndef __EUI64_H__
22*7c478bd9Sstevel@tonic-gate #define __EUI64_H__
23*7c478bd9Sstevel@tonic-gate 
24*7c478bd9Sstevel@tonic-gate #if !defined(INET6)
25*7c478bd9Sstevel@tonic-gate #error	"this file should only be included when INET6 is defined"
26*7c478bd9Sstevel@tonic-gate #endif /* not defined(INET6) */
27*7c478bd9Sstevel@tonic-gate 
28*7c478bd9Sstevel@tonic-gate #if defined(SOL2)
29*7c478bd9Sstevel@tonic-gate #include <netinet/in.h>
30*7c478bd9Sstevel@tonic-gate 
31*7c478bd9Sstevel@tonic-gate typedef union {
32*7c478bd9Sstevel@tonic-gate     uint8_t	e8[8];		/* lower 64-bit IPv6 address */
33*7c478bd9Sstevel@tonic-gate     uint32_t	e32[2];		/* lower 64-bit IPv6 address */
34*7c478bd9Sstevel@tonic-gate } eui64_t;
35*7c478bd9Sstevel@tonic-gate 
36*7c478bd9Sstevel@tonic-gate /*
37*7c478bd9Sstevel@tonic-gate  * Declare the two below, since in.h only defines them when _KERNEL
38*7c478bd9Sstevel@tonic-gate  * is declared - which shouldn't be true when dealing with user-land programs
39*7c478bd9Sstevel@tonic-gate  */
40*7c478bd9Sstevel@tonic-gate #define	s6_addr8	_S6_un._S6_u8
41*7c478bd9Sstevel@tonic-gate #define	s6_addr32	_S6_un._S6_u32
42*7c478bd9Sstevel@tonic-gate 
43*7c478bd9Sstevel@tonic-gate #else /* else if not defined(SOL2) */
44*7c478bd9Sstevel@tonic-gate 
45*7c478bd9Sstevel@tonic-gate /*
46*7c478bd9Sstevel@tonic-gate  * TODO:
47*7c478bd9Sstevel@tonic-gate  *
48*7c478bd9Sstevel@tonic-gate  * Maybe this should be done by processing struct in6_addr directly...
49*7c478bd9Sstevel@tonic-gate  */
50*7c478bd9Sstevel@tonic-gate typedef union
51*7c478bd9Sstevel@tonic-gate {
52*7c478bd9Sstevel@tonic-gate     u_int8_t e8[8];
53*7c478bd9Sstevel@tonic-gate     u_int16_t e16[4];
54*7c478bd9Sstevel@tonic-gate     u_int32_t e32[2];
55*7c478bd9Sstevel@tonic-gate } eui64_t;
56*7c478bd9Sstevel@tonic-gate 
57*7c478bd9Sstevel@tonic-gate #endif /* defined(SOL2) */
58*7c478bd9Sstevel@tonic-gate 
59*7c478bd9Sstevel@tonic-gate #define eui64_iszero(e)		(((e).e32[0] | (e).e32[1]) == 0)
60*7c478bd9Sstevel@tonic-gate #define eui64_equals(e, o)	(((e).e32[0] == (o).e32[0]) && \
61*7c478bd9Sstevel@tonic-gate 				((e).e32[1] == (o).e32[1]))
62*7c478bd9Sstevel@tonic-gate #define eui64_zero(e)		((e).e32[0] = (e).e32[1] = 0)
63*7c478bd9Sstevel@tonic-gate 
64*7c478bd9Sstevel@tonic-gate #define eui64_copy(s, d)	(void) memcpy(&(d), &(s), sizeof(eui64_t))
65*7c478bd9Sstevel@tonic-gate 
66*7c478bd9Sstevel@tonic-gate #define eui64_magic(e)		(			\
67*7c478bd9Sstevel@tonic-gate 				(e).e32[0] = magic(),	\
68*7c478bd9Sstevel@tonic-gate 				(e).e32[1] = magic(),	\
69*7c478bd9Sstevel@tonic-gate 				(e).e8[0] &= ~2		\
70*7c478bd9Sstevel@tonic-gate 				)
71*7c478bd9Sstevel@tonic-gate #define eui64_magic_nz(x)	do {				\
72*7c478bd9Sstevel@tonic-gate 				eui64_magic(x);			\
73*7c478bd9Sstevel@tonic-gate 				} while (eui64_iszero(x))
74*7c478bd9Sstevel@tonic-gate #define eui64_magic_ne(x, y)	do {				\
75*7c478bd9Sstevel@tonic-gate 				eui64_magic(x);			\
76*7c478bd9Sstevel@tonic-gate 				} while (eui64_equals(x, y))
77*7c478bd9Sstevel@tonic-gate 
78*7c478bd9Sstevel@tonic-gate #define eui64_get(ll, cp)	(				\
79*7c478bd9Sstevel@tonic-gate 				eui64_copy((*cp), (ll)),	\
80*7c478bd9Sstevel@tonic-gate 				(cp) += sizeof(eui64_t)		\
81*7c478bd9Sstevel@tonic-gate 				)
82*7c478bd9Sstevel@tonic-gate 
83*7c478bd9Sstevel@tonic-gate #define eui64_put(ll, cp)	(				\
84*7c478bd9Sstevel@tonic-gate 				eui64_copy((ll), (*cp)),	\
85*7c478bd9Sstevel@tonic-gate 				(cp) += sizeof(eui64_t)		\
86*7c478bd9Sstevel@tonic-gate 				)
87*7c478bd9Sstevel@tonic-gate 
88*7c478bd9Sstevel@tonic-gate #define eui64_set32(e, l)	(			\
89*7c478bd9Sstevel@tonic-gate 				(e).e32[0] = 0,		\
90*7c478bd9Sstevel@tonic-gate 				(e).e32[1] = htonl(l)	\
91*7c478bd9Sstevel@tonic-gate 				)
92*7c478bd9Sstevel@tonic-gate #define eui64_setlo32(e, l)	eui64_set32(e, l)
93*7c478bd9Sstevel@tonic-gate 
94*7c478bd9Sstevel@tonic-gate /*
95*7c478bd9Sstevel@tonic-gate  * Returns ascii representation of ID.  This is at most 20 bytes long;
96*7c478bd9Sstevel@tonic-gate  * 19 characters plus one NUL.
97*7c478bd9Sstevel@tonic-gate  */
98*7c478bd9Sstevel@tonic-gate char *eui64_ntoa __P((eui64_t));
99*7c478bd9Sstevel@tonic-gate 
100*7c478bd9Sstevel@tonic-gate #endif /* __EUI64_H__ */
101*7c478bd9Sstevel@tonic-gate 
102