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>
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 explicit Mac_addr(uint64_t mac) : _mac{mac} {}
42
43 Mac_addr(Mac_addr const &other) : _mac{other._mac} {}
44
46 bool is_broadcast() const
47 {
48 /* There are broadcast and multicast addresses, both are supposed
49 to be delivered to all station and the local network (layer 2).
50
51 Broadcast address is FF:FF:FF:FF:FF:FF, multicast addresses have
52 the LSB of the first octet set. Since this holds for both
53 broadcast and multicast we test for the multicast bit here.
54
55 In our internal representation we store the bytes in reverse
56 order, so we test the least significant bit of the least
57 significant byte.
58 */
59 return _mac & 1;
60 }
61
63 bool is_unknown() const
64 { return _mac == Addr_unknown; }
65
66 bool operator == (Mac_addr const &other) const
67 { return _mac == other._mac; }
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 Mac_addr& operator = (Mac_addr const &other)
76 { _mac = other._mac; return *this; }
77
78 Mac_addr& operator = (uint64_t mac)
79 { _mac = mac; return *this; }
80
81 template<typename T>
82 void print(T &stream) const
83 {
84 stream.cprintf("%02x:%02x:%02x:%02x:%02x:%02x",
85 (int)(_mac & 0xff) , (int)((_mac >> 8) & 0xff),
86 (int)((_mac >> 16) & 0xff), (int)((_mac >> 24) & 0xff),
87 (int)((_mac >> 32) & 0xff), (int)((_mac >> 40) & 0xff));
88 }
89
90private:
92 uint64_t _mac;
93};
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:63
bool is_broadcast() const
Check if MAC address is a broadcast or multicast address.
Definition mac_addr.h:46