join 1.0
lightweight network framework library
Loading...
Searching...
No Matches
macaddress.hpp
Go to the documentation of this file.
1
25#ifndef __JOIN_MACADDRESS_HPP__
26#define __JOIN_MACADDRESS_HPP__
27
28// libjoin.
29#include <join/ipaddress.hpp>
30
31// C++
32#include <array>
33#include <string>
34#include <ostream>
35
36// C
37#include <unistd.h>
38#include <net/if.h>
39
40namespace join
41{
46 {
47 public:
49 using CaseConvert = std::ios_base& (std::ios_base&);
50
52 using iterator = std::array <uint8_t, IFHWADDRLEN>::iterator;
53
55 using const_iterator = std::array <uint8_t, IFHWADDRLEN>::const_iterator;
56
60 MacAddress ();
61
67
73
80 MacAddress (const uint8_t* address, size_t size);
81
87 MacAddress (const uint8_t (&address) [IFHWADDRLEN]);
88
94 MacAddress (std::initializer_list <uint8_t> address);
95
101 MacAddress (const struct sockaddr& address);
102
108 MacAddress (const char* address);
109
115 MacAddress (const std::string& address);
116
120 ~MacAddress () = default;
121
126 int family () const;
127
132 const uint8_t* addr () const;
133
138 socklen_t length () const;
139
144 bool isWildcard () const;
145
150 bool isBroadcast () const;
151
157 static bool isMacAddress (const std::string& address);
158
164 std::string toString (CaseConvert caseConvert = std::nouppercase) const;
165
172 IpAddress toIpv6 (const IpAddress& prefix, int len) const;
173
178 IpAddress toLinkLocalIpv6 () const;
179
185
189 void clear ();
190
195 iterator begin ();
196
201 const_iterator begin () const;
202
207 const_iterator cbegin () const;
208
213 iterator end ();
214
219 const_iterator end () const;
220
225 const_iterator cend () const;
226
232 static MacAddress address (const std::string& interface);
233
240
247
254 MacAddress& operator= (std::initializer_list <uint8_t> address);
255
262 MacAddress& operator= (const struct sockaddr& address);
263
269 MacAddress& operator+= (int value);
270
276
282
289 uint8_t& operator[] (size_t position);
290
297 const uint8_t& operator[] (size_t position) const;
298
303 MacAddress operator~ () const;
304
306 static const MacAddress wildcard;
307
309 static const MacAddress broadcast;
310
311 private:
313 std::array <uint8_t, IFHWADDRLEN> _mac = {};
314 };
315
322 MacAddress operator+ (int value, const MacAddress& a);
323
330 MacAddress operator+ (const MacAddress& a, int value);
331
338 bool operator== (const MacAddress& a, const MacAddress& b);
339
346 bool operator!= (const MacAddress& a, const MacAddress& b);
347
354 bool operator< (const MacAddress& a, const MacAddress& b);
355
362 bool operator<= (const MacAddress& a, const MacAddress& b);
363
370 bool operator> (const MacAddress& a, const MacAddress& b);
371
378 bool operator>= (const MacAddress& a, const MacAddress& b);
379
386 MacAddress operator& (const MacAddress& a, const MacAddress& b);
387
394 MacAddress operator| (const MacAddress& a, const MacAddress& b);
395
402 MacAddress operator^ (const MacAddress& a, const MacAddress& b);
403
410 std::ostream& operator<< (std::ostream& out, const MacAddress& a);
411}
412
413#endif
IPv6, IPv4 address class.
Definition ipaddress.hpp:51
MAC address class.
Definition macaddress.hpp:46
socklen_t length() const
get the size in byte of the internal MAC address array.
Definition macaddress.cpp:175
void clear()
clear MAC address (wilcard address).
Definition macaddress.cpp:297
bool isBroadcast() const
check if MAC address is a broadcast address.
Definition macaddress.cpp:195
std::array< uint8_t, IFHWADDRLEN >::const_iterator const_iterator
constant iterator from nested container.
Definition macaddress.hpp:55
~MacAddress()=default
destroy the MacAddress instance.
static const MacAddress broadcast
broadcast MAC address.
Definition macaddress.hpp:309
int family() const
get address family.
Definition macaddress.cpp:157
std::ios_base &(std::ios_base &) CaseConvert
case conversion function.
Definition macaddress.hpp:49
iterator begin()
returns an iterator to the first element of the nested container.
Definition macaddress.cpp:306
MacAddress()
create the MacAddress instance (wilcard address).
Definition macaddress.cpp:55
IpAddress toUniqueLocalIpv6() const
convert MAC address to a unique local IPv6 address using eui-64.
Definition macaddress.cpp:277
static bool isMacAddress(const std::string &address)
check if the specified string is a MAC address.
Definition macaddress.cpp:206
bool isWildcard() const
check if MAC address is a wildcard address.
Definition macaddress.cpp:184
MacAddress & operator+=(int value)
add the provided value to the current MAC address.
Definition macaddress.cpp:437
MacAddress operator~() const
perform NOT operation on MAC address.
Definition macaddress.cpp:501
MacAddress & operator=(const MacAddress &address)
assign the MacAddress instance by copy.
Definition macaddress.cpp:386
const_iterator cbegin() const
returns an iterator to the first element of the nested container.
Definition macaddress.cpp:324
MacAddress & operator++()
perform pre-increment operation on MAC address.
Definition macaddress.cpp:453
IpAddress toLinkLocalIpv6() const
convert MAC address to a link local IPv6 address.
Definition macaddress.cpp:263
const_iterator cend() const
return an iterator to the element following the last element of the nested container.
Definition macaddress.cpp:351
static const MacAddress wildcard
wildcard MAC address.
Definition macaddress.hpp:306
IpAddress toIpv6(const IpAddress &prefix, int len) const
convert MAC address to IPv6 address using prefix.
Definition macaddress.cpp:243
std::string toString(CaseConvert caseConvert=std::nouppercase) const
convert internal address array to string.
Definition macaddress.cpp:224
std::array< uint8_t, IFHWADDRLEN >::iterator iterator
iterator from nested container.
Definition macaddress.hpp:52
const uint8_t * addr() const
get the internal MAC address array address.
Definition macaddress.cpp:166
uint8_t & operator[](size_t position)
returns a reference to the element at the specified location.
Definition macaddress.cpp:473
iterator end()
return an iterator to the element following the last element of the nested container.
Definition macaddress.cpp:333
Definition acceptor.hpp:32
bool operator<(const BasicUnixEndpoint< Protocol > &a, const BasicUnixEndpoint< Protocol > &b) noexcept
compare if endpoint is lower.
Definition endpoint.hpp:207
IpAddress operator^(const IpAddress &a, const IpAddress &b)
perform XOR operation on IP address.
Definition ipaddress.cpp:1720
bool operator>(const BasicUnixEndpoint< Protocol > &a, const BasicUnixEndpoint< Protocol > &b) noexcept
compare if endpoint is greater.
Definition endpoint.hpp:219
IpAddress operator&(const IpAddress &a, const IpAddress &b)
perform AND operation on IP address.
Definition ipaddress.cpp:1666
bool operator!=(const BasicUnixEndpoint< Protocol > &a, const BasicUnixEndpoint< Protocol > &b) noexcept
compare if endpoints are not equal.
Definition endpoint.hpp:195
bool operator>=(const BasicUnixEndpoint< Protocol > &a, const BasicUnixEndpoint< Protocol > &b) noexcept
compare if endpoint is greater or equal.
Definition endpoint.hpp:243
std::ostream & operator<<(std::ostream &os, const BasicUnixEndpoint< Protocol > &endpoint)
push endpoint representation into a stream.
Definition endpoint.hpp:255
bool operator<=(const BasicUnixEndpoint< Protocol > &a, const BasicUnixEndpoint< Protocol > &b) noexcept
compare if endpoint is lower or equal.
Definition endpoint.hpp:231
IpAddress operator|(const IpAddress &a, const IpAddress &b)
perform OR operation on IP address.
Definition ipaddress.cpp:1693
bool operator==(const BasicUnixEndpoint< Protocol > &a, const BasicUnixEndpoint< Protocol > &b) noexcept
compare if endpoints are equal.
Definition endpoint.hpp:183
MacAddress operator+(int value, const MacAddress &a)
add the provided value to the provided MAC address.
Definition macaddress.cpp:517
IpAddress address
Definition tcpacceptor_test.cpp:35