17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate     eui64.h - EUI64 routines for IPv6CP.
37c478bd9Sstevel@tonic-gate     Copyright (C) 1999  Tommi Komulainen <Tommi.Komulainen@iki.fi>
47c478bd9Sstevel@tonic-gate 
57c478bd9Sstevel@tonic-gate     Redistribution and use in source and binary forms are permitted
67c478bd9Sstevel@tonic-gate     provided that the above copyright notice and this paragraph are
77c478bd9Sstevel@tonic-gate     duplicated in all such forms and that any documentation,
87c478bd9Sstevel@tonic-gate     advertising materials, and other materials related to such
97c478bd9Sstevel@tonic-gate     distribution and use acknowledge that the software was developed
107c478bd9Sstevel@tonic-gate     by Tommi Komulainen.  The name of the author may not be used
117c478bd9Sstevel@tonic-gate     to endorse or promote products derived from this software without
127c478bd9Sstevel@tonic-gate     specific prior written permission.
137c478bd9Sstevel@tonic-gate     THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
147c478bd9Sstevel@tonic-gate     IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
157c478bd9Sstevel@tonic-gate     WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
167c478bd9Sstevel@tonic-gate 
17*2a8bcb4eSToomas Soome 
187c478bd9Sstevel@tonic-gate     $Id: eui64.h,v 1.3 1999/09/30 19:56:37 masputra Exp $
197c478bd9Sstevel@tonic-gate */
207c478bd9Sstevel@tonic-gate 
217c478bd9Sstevel@tonic-gate #ifndef __EUI64_H__
227c478bd9Sstevel@tonic-gate #define __EUI64_H__
237c478bd9Sstevel@tonic-gate 
247c478bd9Sstevel@tonic-gate #if !defined(INET6)
257c478bd9Sstevel@tonic-gate #error	"this file should only be included when INET6 is defined"
267c478bd9Sstevel@tonic-gate #endif /* not defined(INET6) */
277c478bd9Sstevel@tonic-gate 
287c478bd9Sstevel@tonic-gate #if defined(SOL2)
297c478bd9Sstevel@tonic-gate #include <netinet/in.h>
307c478bd9Sstevel@tonic-gate 
317c478bd9Sstevel@tonic-gate typedef union {
327c478bd9Sstevel@tonic-gate     uint8_t	e8[8];		/* lower 64-bit IPv6 address */
337c478bd9Sstevel@tonic-gate     uint32_t	e32[2];		/* lower 64-bit IPv6 address */
347c478bd9Sstevel@tonic-gate } eui64_t;
357c478bd9Sstevel@tonic-gate 
367c478bd9Sstevel@tonic-gate /*
377c478bd9Sstevel@tonic-gate  * Declare the two below, since in.h only defines them when _KERNEL
387c478bd9Sstevel@tonic-gate  * is declared - which shouldn't be true when dealing with user-land programs
397c478bd9Sstevel@tonic-gate  */
407c478bd9Sstevel@tonic-gate #define	s6_addr8	_S6_un._S6_u8
417c478bd9Sstevel@tonic-gate #define	s6_addr32	_S6_un._S6_u32
427c478bd9Sstevel@tonic-gate 
437c478bd9Sstevel@tonic-gate #else /* else if not defined(SOL2) */
447c478bd9Sstevel@tonic-gate 
457c478bd9Sstevel@tonic-gate /*
467c478bd9Sstevel@tonic-gate  * TODO:
477c478bd9Sstevel@tonic-gate  *
487c478bd9Sstevel@tonic-gate  * Maybe this should be done by processing struct in6_addr directly...
497c478bd9Sstevel@tonic-gate  */
507c478bd9Sstevel@tonic-gate typedef union
517c478bd9Sstevel@tonic-gate {
527c478bd9Sstevel@tonic-gate     u_int8_t e8[8];
537c478bd9Sstevel@tonic-gate     u_int16_t e16[4];
547c478bd9Sstevel@tonic-gate     u_int32_t e32[2];
557c478bd9Sstevel@tonic-gate } eui64_t;
567c478bd9Sstevel@tonic-gate 
577c478bd9Sstevel@tonic-gate #endif /* defined(SOL2) */
587c478bd9Sstevel@tonic-gate 
597c478bd9Sstevel@tonic-gate #define eui64_iszero(e)		(((e).e32[0] | (e).e32[1]) == 0)
607c478bd9Sstevel@tonic-gate #define eui64_equals(e, o)	(((e).e32[0] == (o).e32[0]) && \
617c478bd9Sstevel@tonic-gate 				((e).e32[1] == (o).e32[1]))
627c478bd9Sstevel@tonic-gate #define eui64_zero(e)		((e).e32[0] = (e).e32[1] = 0)
637c478bd9Sstevel@tonic-gate 
647c478bd9Sstevel@tonic-gate #define eui64_copy(s, d)	(void) memcpy(&(d), &(s), sizeof(eui64_t))
657c478bd9Sstevel@tonic-gate 
667c478bd9Sstevel@tonic-gate #define eui64_magic(e)		(			\
677c478bd9Sstevel@tonic-gate 				(e).e32[0] = magic(),	\
687c478bd9Sstevel@tonic-gate 				(e).e32[1] = magic(),	\
697c478bd9Sstevel@tonic-gate 				(e).e8[0] &= ~2		\
707c478bd9Sstevel@tonic-gate 				)
717c478bd9Sstevel@tonic-gate #define eui64_magic_nz(x)	do {				\
727c478bd9Sstevel@tonic-gate 				eui64_magic(x);			\
737c478bd9Sstevel@tonic-gate 				} while (eui64_iszero(x))
747c478bd9Sstevel@tonic-gate #define eui64_magic_ne(x, y)	do {				\
757c478bd9Sstevel@tonic-gate 				eui64_magic(x);			\
767c478bd9Sstevel@tonic-gate 				} while (eui64_equals(x, y))
777c478bd9Sstevel@tonic-gate 
787c478bd9Sstevel@tonic-gate #define eui64_get(ll, cp)	(				\
797c478bd9Sstevel@tonic-gate 				eui64_copy((*cp), (ll)),	\
807c478bd9Sstevel@tonic-gate 				(cp) += sizeof(eui64_t)		\
817c478bd9Sstevel@tonic-gate 				)
827c478bd9Sstevel@tonic-gate 
837c478bd9Sstevel@tonic-gate #define eui64_put(ll, cp)	(				\
847c478bd9Sstevel@tonic-gate 				eui64_copy((ll), (*cp)),	\
857c478bd9Sstevel@tonic-gate 				(cp) += sizeof(eui64_t)		\
867c478bd9Sstevel@tonic-gate 				)
877c478bd9Sstevel@tonic-gate 
887c478bd9Sstevel@tonic-gate #define eui64_set32(e, l)	(			\
897c478bd9Sstevel@tonic-gate 				(e).e32[0] = 0,		\
907c478bd9Sstevel@tonic-gate 				(e).e32[1] = htonl(l)	\
917c478bd9Sstevel@tonic-gate 				)
927c478bd9Sstevel@tonic-gate #define eui64_setlo32(e, l)	eui64_set32(e, l)
937c478bd9Sstevel@tonic-gate 
947c478bd9Sstevel@tonic-gate /*
957c478bd9Sstevel@tonic-gate  * Returns ascii representation of ID.  This is at most 20 bytes long;
967c478bd9Sstevel@tonic-gate  * 19 characters plus one NUL.
977c478bd9Sstevel@tonic-gate  */
987c478bd9Sstevel@tonic-gate char *eui64_ntoa __P((eui64_t));
997c478bd9Sstevel@tonic-gate 
1007c478bd9Sstevel@tonic-gate #endif /* __EUI64_H__ */
1017c478bd9Sstevel@tonic-gate 
102