Line data Source code
1 : /* 2 : * Copyright (c) 2013 Juniper Networks, Inc. All rights reserved. 3 : * Copyright Contributors to the OpenSDN project 4 : */ 5 : 6 : #ifndef ctrlplane_mac_address_h 7 : #define ctrlplane_mac_address_h 8 : 9 : #include <string> 10 : #include <boost/system/error_code.hpp> 11 : 12 : #include <net/ethernet.h> 13 : #include <sys/socket.h> 14 : 15 : class MacAddress { 16 : public: 17 : MacAddress(); 18 : explicit MacAddress(const uint8_t *data); 19 : 20 3280434 : MacAddress(const MacAddress &rhs) { 21 3280434 : addr_ = rhs.addr_; 22 3280434 : } 23 : 24 18501 : explicit MacAddress(const struct ether_addr &a) { 25 18501 : addr_ = a; 26 18501 : } 27 2 : explicit MacAddress(const struct ether_addr *a) { 28 2 : addr_ = *a; 29 2 : } 30 : 31 : MacAddress(unsigned int a, unsigned int b, unsigned int c, 32 : unsigned int d, unsigned int e, unsigned int f); 33 : 34 : explicit MacAddress(const std::string &s, 35 : boost::system::error_code *error = NULL); 36 : 37 : bool IsBroadcast() const; 38 : bool IsMulticast() const; 39 : bool IsZero() const; 40 : int CompareTo(const MacAddress &rhs, int len = 0) const; 41 : 42 : bool operator==(const MacAddress *rhs) const { 43 : return CompareTo(*rhs) == 0; 44 : } 45 91079 : bool operator==(const MacAddress &rhs) const { 46 91079 : return CompareTo(rhs) == 0; 47 : } 48 1877462 : bool operator<(const MacAddress &rhs) const { 49 1877462 : return CompareTo(rhs) < 0; 50 : } 51 : bool operator>(const MacAddress &rhs) const { 52 : return CompareTo(rhs) > 0; 53 : } 54 83047 : bool operator!=(const MacAddress &rhs) const { 55 83047 : return !operator==(rhs); 56 : } 57 : 58 58668 : static size_t size() { 59 58668 : return sizeof(struct ether_addr); 60 : } 61 : 62 0 : static size_t bit_len() { 63 0 : return (sizeof(struct ether_addr) * 8); 64 : } 65 : 66 5654 : u_int8_t &operator[](size_t i) { 67 5654 : return ((u_int8_t *)&addr_)[i]; 68 : } 69 : 70 5358 : u_int8_t operator[](size_t i) const { 71 5358 : return ((u_int8_t *)&addr_)[i]; 72 : } 73 : 74 : MacAddress &operator=(const u_int8_t *c); 75 : 76 : MacAddress &operator=(const struct sockaddr *sa); 77 : 78 0 : MacAddress &operator=(const struct sockaddr &sa) { 79 0 : return operator=(&sa); 80 : } 81 : 82 1 : MacAddress &operator=(const struct ether_addr &ea) { 83 1 : addr_ = ea; 84 1 : return *this; 85 : } 86 : 87 137761 : MacAddress &operator=(const MacAddress &rhs) { 88 137761 : addr_ = rhs.addr_; 89 137761 : return *this; 90 : } 91 : 92 : bool ToArray(u_int8_t *p, size_t s) const; 93 : 94 : operator ether_addr() { 95 : return addr_; 96 : } 97 : 98 1 : operator ether_addr&() { 99 1 : return addr_; 100 : } 101 : 102 1 : operator sockaddr() const { 103 1 : struct sockaddr sa = { 0 }; 104 1 : ToArray((u_int8_t *)sa.sa_data, sizeof(sa)); 105 1 : return sa; 106 : } 107 : 108 182 : operator const ether_addr&() const { 109 182 : return addr_; 110 : } 111 : 112 0 : operator const u_int8_t *() const { 113 0 : return (const u_int8_t *)&addr_; 114 : } 115 : 116 : operator u_int8_t *() { 117 : return (u_int8_t *)&addr_; 118 : } 119 : 120 1178 : operator const int8_t *() const { 121 1178 : return (const int8_t *)&addr_; 122 : } 123 : 124 5308 : operator int8_t *() { 125 5308 : return (int8_t *)&addr_; 126 : } 127 : 128 175 : u_int8_t &last_octet() { 129 175 : return ((u_int8_t *)&addr_)[5]; 130 : } 131 : 132 181 : void Zero() { 133 181 : addr_ = kZeroMac; 134 181 : } 135 : 136 1 : void Broadcast() { 137 1 : addr_ = kBroadcastMac; 138 1 : } 139 : 140 : void Multicast() { 141 : addr_ = kMulticastMac; 142 : } 143 : 144 884003 : const uint8_t *GetData() const { return (uint8_t *)&addr_; } 145 : 146 : std::string ToString() const; 147 : static MacAddress FromString(const std::string &str, 148 : boost::system::error_code *error = NULL); 149 : 150 : static const MacAddress kZeroMac; 151 : static const MacAddress kBroadcastMac; 152 : static const MacAddress kMulticastMac; 153 1053053 : static const MacAddress &BroadcastMac() { 154 1053053 : return kBroadcastMac; 155 : } 156 0 : static const MacAddress &MulticastMac() { 157 0 : return kMulticastMac; 158 : } 159 40304 : static const MacAddress &ZeroMac() { 160 40304 : return kZeroMac; 161 : } 162 : 163 : private: 164 : struct ether_addr addr_; 165 : }; 166 : 167 : #endif