eui48.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2018 Freie Universität Berlin
3  *
4  * This file is subject to the terms and conditions of the GNU Lesser
5  * General Public License v2.1. See the file LICENSE in the top level
6  * directory for more details.
7  */
8 
21 #ifndef NET_EUI48_H
22 #define NET_EUI48_H
23 
24 #include <stdint.h>
25 
26 #include "net/eui64.h"
27 
28 #ifdef __cplusplus
29 extern "C" {
30 #endif
31 
35 typedef struct {
36  uint8_t uint8[6];
37 } eui48_t;
38 
49 #define EUI48_LOCAL_FLAG 0x02
50 
54 #define EUI48_GROUP_FLAG 0x01
55 
64 static inline void eui48_set_local(eui48_t *addr)
65 {
66  addr->uint8[0] |= EUI48_LOCAL_FLAG;
67 }
68 
77 static inline void eui48_clear_group(eui48_t *addr)
78 {
79  addr->uint8[0] &= ~EUI48_GROUP_FLAG;
80 }
81 
90 static inline void eui48_to_eui64(eui64_t *eui64, const eui48_t *addr)
91 {
92  eui64->uint8[0] = addr->uint8[0];
93  eui64->uint8[1] = addr->uint8[1];
94  eui64->uint8[2] = addr->uint8[2];
95  eui64->uint8[3] = 0xff;
96  eui64->uint8[4] = 0xfe;
97  eui64->uint8[5] = addr->uint8[3];
98  eui64->uint8[6] = addr->uint8[4];
99  eui64->uint8[7] = addr->uint8[5];
100 }
101 
111 static inline void eui64_to_eui48(eui48_t *eui48, const eui64_t *addr)
112 {
113  /* Preserve vendor id */
114  eui48->uint8[0] = addr->uint8[0];
115  eui48->uint8[1] = addr->uint8[1];
116  eui48->uint8[2] = addr->uint8[2];
117 
118  /* Use most volatile bits */
119  eui48->uint8[3] = addr->uint8[5];
120  eui48->uint8[4] = addr->uint8[6];
121  eui48->uint8[5] = addr->uint8[7];
122 
123  /* EUI is only locally unique */
124  eui48_set_local(eui48);
125 }
126 
137 static inline void eui48_to_ipv6_iid(eui64_t *iid, const eui48_t *addr)
138 {
139  eui48_to_eui64(iid, addr);
140  iid->uint8[0] ^= 0x02;
141 }
142 
149 static inline void eui48_from_ipv6_iid(eui48_t *addr, const eui64_t *iid)
150 {
151  addr->uint8[0] = iid->uint8[0] ^ 0x02;
152  addr->uint8[1] = iid->uint8[1];
153  addr->uint8[2] = iid->uint8[2];
154  addr->uint8[3] = iid->uint8[5];
155  addr->uint8[4] = iid->uint8[6];
156  addr->uint8[5] = iid->uint8[7];
157 }
158 
159 #ifdef __cplusplus
160 }
161 #endif
162 
163 #endif /* NET_EUI48_H */
164 
eui64_t::uint8
uint8_t uint8[8]
split into 8 8-bit words.
Definition: eui64.h:57
eui64_to_eui48
static void eui64_to_eui48(eui48_t *eui48, const eui64_t *addr)
Generates an EUI-48 from a 64-bit device address.
Definition: eui48.h:111
eui48_set_local
static void eui48_set_local(eui48_t *addr)
Set the locally administrated bit in the EUI-48 address.
Definition: eui48.h:64
eui48_t::uint8
uint8_t uint8[6]
split into 6 8-bit words.
Definition: eui48.h:36
eui48_t
Data type to represent an EUI-48.
Definition: eui48.h:35
EUI48_LOCAL_FLAG
#define EUI48_LOCAL_FLAG
Locally administered address.
Definition: eui48.h:49
eui48_to_ipv6_iid
static void eui48_to_ipv6_iid(eui64_t *iid, const eui48_t *addr)
Generates an IPv6 interface identifier from a 48-bit device address.
Definition: eui48.h:137
eui48_from_ipv6_iid
static void eui48_from_ipv6_iid(eui48_t *addr, const eui64_t *iid)
Convert a 64-bit IPv6 IID into a EUI-48 device address.
Definition: eui48.h:149
eui48_to_eui64
static void eui48_to_eui64(eui64_t *eui64, const eui48_t *addr)
Generates an EUI-64 from a 48-bit device address.
Definition: eui48.h:90
eui64_t
Data type to represent an EUI-64.
Definition: eui64.h:55
eui48_clear_group
static void eui48_clear_group(eui48_t *addr)
Clear the group address bit to signal the address as individual address.
Definition: eui48.h:77
eui64.h
EUI-64 data type definition.
EUI48_GROUP_FLAG
#define EUI48_GROUP_FLAG
Group type address.
Definition: eui48.h:54