quic/qbox
Loading...
Searching...
No Matches
mac.h
1/*
2 * Copyright (c) 2022-2023 Qualcomm Innovation Center, Inc. All Rights Reserved.
3 * Author: GreenSocs 2022
4 *
5 * SPDX-License-Identifier: BSD-3-Clause
6 */
7
8#ifndef _MAC_H_
9#define _MAC_H_
10
11#include <cstring>
12#include <inttypes.h>
13
14#include <iostream>
15#include <string>
16
18{
19private:
20 uint8_t m_bytes[6];
21
22public:
23 MACAddress() { memset(m_bytes, 0, sizeof(m_bytes)); }
24
25public:
26 uint32_t lo() const;
27 uint16_t hi() const;
28
29 void set_lo(uint32_t value);
30 void set_hi(uint16_t value);
31
32public:
33 void zero();
34
35 inline void randomize()
36 {
37 for (int i = 0; i < 6; ++i) {
38 m_bytes[i] = i;
39 }
40 m_bytes[0] &= ~((uint8_t)1); // clear multicast bit
41 }
42
43 bool set_from_str(const std::string& s);
44
45public:
46 bool operator==(const MACAddress& other) const { return memcmp(m_bytes, other.m_bytes, 6) == 0; }
47
48 bool operator!=(const MACAddress& other) const { return !this->operator==(other); }
49
50 bool operator<(const MACAddress& other) const { return memcmp(m_bytes, other.m_bytes, 6) < 0; }
51
52 bool operator>(const MACAddress& other) const { return memcmp(m_bytes, other.m_bytes, 6) > 0; }
53
54 bool operator<=(const MACAddress& other) const { return memcmp(m_bytes, other.m_bytes, 6) <= 0; }
55
56 bool operator>=(const MACAddress& other) const { return memcmp(m_bytes, other.m_bytes, 6) >= 0; }
57
58 uint8_t operator[](int i) const { return (i < 6) ? m_bytes[i] : 0; }
59};
60
61#endif
Definition mac.h:18
Definition target.h:160