Click
Loading...
Searching...
No Matches
etheraddress.hh
Go to the documentation of this file.
1// -*- related-file-name: "../../lib/etheraddress.cc" -*-
2#ifndef CLICK_ETHERADDRESS_HH
3#define CLICK_ETHERADDRESS_HH
4#include <click/string.hh>
5#include <click/glue.hh>
7CLICK_DECLS
8
9class EtherAddress { public:
10
11 typedef uninitialized_type uninitialized_t;
12
14 inline EtherAddress() {
15 _data[0] = _data[1] = _data[2] = 0;
16 }
17
22 explicit inline EtherAddress(const unsigned char *data) {
23 memcpy(_data, data, 6);
24 }
25
27 inline EtherAddress(const uninitialized_type &unused) {
28 (void) unused;
29 }
30
33 return EtherAddress(0xFFFF);
34 }
35
36 static inline EtherAddress broadcast() CLICK_DEPRECATED;
37
38
39 typedef bool (EtherAddress::*unspecified_bool_type)() const;
41 inline operator unspecified_bool_type() const {
42 return _data[0] || _data[1] || _data[2] ? &EtherAddress::is_group : 0;
43 }
44
49 inline bool is_group() const {
50 return data()[0] & 1;
51 }
52
57 inline bool is_local() const {
58 return data()[0] & 2;
59 }
60
64 inline bool is_broadcast() const {
65 return _data[0] + _data[1] + _data[2] == 0x2FFFD;
66 }
67
69 static inline bool is_broadcast(const unsigned char *data) {
70#if HAVE_INDIFFERENT_ALIGNMENT
71 return reinterpret_cast<const EtherAddress *>(data)->is_broadcast();
72#else
73 return data[0] + data[1] + data[2] + data[3] + data[4] + data[5] == 0x5FA;
74#endif
75 }
76
78 inline unsigned char *data() {
79 return reinterpret_cast<unsigned char *>(_data);
80 }
81
83 inline const unsigned char *data() const {
84 return reinterpret_cast<const unsigned char *>(_data);
85 }
86
87#if __GNUC__ >= 9
88# pragma GCC diagnostic push
89# pragma GCC diagnostic ignored "-Waddress-of-packed-member"
90#endif
93 inline const uint16_t *sdata() const {
94 return _data;
95 }
96#if __GNUC__ >= 9
97# pragma GCC diagnostic pop
98#endif
99
101 inline size_t hashcode() const {
102 return (_data[2] | ((size_t) _data[1] << 16))
103 ^ ((size_t) _data[0] << 9);
104 }
105
106 // bool operator==(EtherAddress, EtherAddress);
107 // bool operator!=(EtherAddress, EtherAddress);
108
116 inline String unparse() const {
117 return unparse_dash();
118 }
119
125 String unparse_colon() const;
126
133 String unparse_dash() const;
134
138 inline String s() const CLICK_DEPRECATED;
139
143 inline operator String() const CLICK_DEPRECATED;
144
145 typedef const EtherAddress &parameter_type;
146
147 private:
148
149 uint16_t _data[3];
150
151 EtherAddress(uint16_t m) {
152 _data[0] = _data[1] = _data[2] = m;
153 }
154
155} CLICK_SIZE_PACKED_ATTRIBUTE;
156
157inline EtherAddress EtherAddress::broadcast() {
158 return make_broadcast();
159}
160
161inline
162EtherAddress::operator String() const
163{
164 return unparse();
165}
166
167inline String
169{
170 return unparse();
171}
172
175inline bool
177{
178 return (a.sdata()[0] == b.sdata()[0]
179 && a.sdata()[1] == b.sdata()[1]
180 && a.sdata()[2] == b.sdata()[2]);
181}
182
185inline bool
187{
188 return !(a == b);
189}
190
191class StringAccum;
193
194
195class ArgContext;
196class Args;
197extern const ArgContext blank_args;
198
212class EtherAddressArg { public:
213 typedef void enable_direct_parse;
214 EtherAddressArg(int flags = 0) : flags_(flags) {}
215 inline bool parse(const String& str, EtherAddress& value, const ArgContext& args = blank_args) {
216 return parse(str, value, args, flags_);
217 }
218 inline bool parse(const String& str, unsigned char* value, const ArgContext& args = blank_args) {
219 return parse(str, *reinterpret_cast<EtherAddress *>(value), args);
220 }
221 inline bool direct_parse(const String& str, EtherAddress& value, Args& args) {
222 return direct_parse(str, value, args, flags_);
223 }
224 inline bool direct_parse(const String& str, unsigned char* value, Args& args) {
225 return direct_parse(str, *reinterpret_cast<EtherAddress *>(value), args);
226 }
227 static bool parse(const String &str, Vector<EtherAddress> &result,
228 const ArgContext &args = blank_args);
229 private:
230 int flags_;
231 static bool parse(const String& str, EtherAddress& value, const ArgContext& args, int flags);
232 static bool direct_parse(const String& str, EtherAddress& value, Args& args, int flags);
233};
234
235template<> struct DefaultArg<EtherAddress> : public EtherAddressArg {};
236template<> struct has_trivial_copy<EtherAddress> : public true_type {};
237template<> struct DefaultArg<Vector<EtherAddress> > : public EtherAddressArg {};
238
239CLICK_ENDDECLS
240#endif
Argument context class.
Definition args.hh:31
Argument parser class.
Definition args.hh:249
Parser class for Ethernet addresses.
Definition etheraddress.hh:212
An Ethernet address.
Definition etheraddress.hh:9
bool operator!=(const EtherAddress &a, const EtherAddress &b)
Compares two EtherAddress objects for inequality.
Definition etheraddress.hh:186
bool operator==(const EtherAddress &a, const EtherAddress &b)
Compares two EtherAddress objects for equality.
Definition etheraddress.hh:176
static bool is_broadcast(const unsigned char *data)
Return true if data points to a broadcast address.
Definition etheraddress.hh:69
size_t hashcode() const
Hash function.
Definition etheraddress.hh:101
EtherAddress(const unsigned char *data)
Construct an EtherAddress from data.
Definition etheraddress.hh:22
String unparse() const
Unparse this address into a dash-separated hex String.
Definition etheraddress.hh:116
bool is_local() const
Return true iff this address is a "local" address.
Definition etheraddress.hh:57
static EtherAddress make_broadcast()
Return the broadcast EtherAddress, FF-FF-FF-FF-FF-FF.
Definition etheraddress.hh:32
const uint16_t * sdata() const
Return a pointer to the address data, as an array of uint16_ts.
Definition etheraddress.hh:93
bool is_broadcast() const
Return true iff this address is the broadcast address.
Definition etheraddress.hh:64
EtherAddress()
Construct an EtherAddress equal to 00-00-00-00-00-00.
Definition etheraddress.hh:14
unsigned char * data()
Return a pointer to the address data.
Definition etheraddress.hh:78
bool is_group() const
Return true iff this address is a group address.
Definition etheraddress.hh:49
String unparse_dash() const
Unparse this address into a dash-separated hex String.
Definition etheraddress.cc:43
String unparse_colon() const
Unparse this address into a colon-separated hex String.
Definition etheraddress.cc:61
String s() const
Unparse this address into a dash-separated hex String.
Definition etheraddress.hh:168
EtherAddress(const uninitialized_type &unused)
Construct an uninitialized EtherAddress.
Definition etheraddress.hh:27
const unsigned char * data() const
Definition etheraddress.hh:83
Efficiently build up Strings from pieces.
Definition straccum.hh:28
friend StringAccum & operator<<(StringAccum &sa, const String &str)
Append the contents of str to sa.
Definition straccum.hh:601
A string of characters.
Definition string.hh:22
Vector template.
Definition vector.hh:111
Type wrapper for the constant true.
Click's String class.
Template determining whether T may be copied by memcpy.
Definition type_traits.hh:66
Type traits structures.