L4Re Operating System Framework
Interface and Usage Documentation
Loading...
Searching...
No Matches
mac_addr.h
1/*
2 * Copyright (C) 2016-2017, 2020, 2022-2024 Kernkonzept GmbH.
3 * Author(s): Jean Wolter <jean.wolter@kernkonzept.com>
4 *
5 * License: see LICENSE.spdx (in this directory or the directories above)
6 */
7#pragma once
8
9#include <cstring>
10#include <inttypes.h>
15
19class Mac_addr
20{
21public:
22 enum
23 {
24 Addr_length = 6,
25 Addr_unknown = 0ULL
26 };
27
28 explicit Mac_addr(char const *_src)
29 {
30 /* A mac address is 6 bytes long, it is transmitted in big endian
31 order over the network. For our internal representation we
32 focus on easy testability of broadcast/multicast and reorder
33 the bytes that the most significant byte becomes the least
34 significant one. */
35 unsigned char const *src = reinterpret_cast<unsigned char const *>(_src);
36 _mac = ((uint64_t)src[0]) | (((uint64_t)src[1]) << 8)
37 | (((uint64_t)src[2]) << 16) | (((uint64_t)src[3]) << 24)
38 | (((uint64_t)src[4]) << 32) | (((uint64_t)src[5]) << 40);
39 }
40
41 static Mac_addr from_uncached(char volatile const *src)
42 { return Mac_addr(src); }
43
44 explicit Mac_addr(uint64_t mac) : _mac{mac} {}
45
46 Mac_addr(Mac_addr const &other) : _mac{other._mac} {}
47
49 bool is_broadcast() const
50 {
51 /* There are broadcast and multicast addresses, both are supposed
52 to be delivered to all station and the local network (layer 2).
53
54 Broadcast address is FF:FF:FF:FF:FF:FF, multicast addresses have
55 the LSB of the first octet set. Since this holds for both
56 broadcast and multicast we test for the multicast bit here.
57
58 In our internal representation we store the bytes in reverse
59 order, so we test the least significant bit of the least
60 significant byte.
61 */
62 return _mac & 1;
63 }
64
66 bool is_unknown() const
67 { return _mac == Addr_unknown; }
68
69 bool operator == (Mac_addr const &other) const
70 { return _mac == other._mac; }
71
72 bool operator != (Mac_addr const &other) const
73 { return _mac != other._mac; }
74
75 bool operator < (Mac_addr const &other) const
76 { return _mac < other._mac; }
77
78 Mac_addr& operator = (Mac_addr const &other)
79 { _mac = other._mac; return *this; }
80
81 Mac_addr& operator = (uint64_t mac)
82 { _mac = mac; return *this; }
83
84 template<typename T>
85 void print(T &stream) const
86 {
87 stream.cprintf("%02x:%02x:%02x:%02x:%02x:%02x",
88 (int)(_mac & 0xff) , (int)((_mac >> 8) & 0xff),
89 (int)((_mac >> 16) & 0xff), (int)((_mac >> 24) & 0xff),
90 (int)((_mac >> 32) & 0xff), (int)((_mac >> 40) & 0xff));
91 }
92
93 void to_array(unsigned char mac[6]) const
94 {
95 mac[0] = _mac & 0xffU;
96 mac[1] = (_mac >> 8) & 0xffU;
97 mac[2] = (_mac >> 16) & 0xffU;
98 mac[3] = (_mac >> 24) & 0xffU;
99 mac[4] = (_mac >> 32) & 0xffU;
100 mac[5] = (_mac >> 40) & 0xffU;
101 }
102
103private:
104 explicit Mac_addr(char volatile const *_src)
105 {
106 /* A mac address is 6 bytes long, it is transmitted in big endian
107 order over the network. For our internal representation we
108 focus on easy testability of broadcast/multicast and reorder
109 the bytes that the most significant byte becomes the least
110 significant one. */
111 volatile unsigned char const *src = reinterpret_cast<volatile unsigned char const *>(_src);
112 _mac = ((uint64_t)src[0]) | (((uint64_t)src[1]) << 8)
113 | (((uint64_t)src[2]) << 16) | (((uint64_t)src[3]) << 24)
114 | (((uint64_t)src[4]) << 32) | (((uint64_t)src[5]) << 40);
115 }
116
118 uint64_t _mac;
119};
120
A wrapper class around the value of a MAC address.
Definition mac_addr.h:20
bool is_unknown() const
Check if the MAC address is not yet known.
Definition mac_addr.h:66
bool is_broadcast() const
Check if MAC address is a broadcast or multicast address.
Definition mac_addr.h:49