libdoip  0.1.0
DoIP (Diagnostics over Internet Protocol) ISO 13400 C++17 Library
MacAddress.h
Go to the documentation of this file.
1 #ifndef MACADDRESS_H
2 #define MACADDRESS_H
3 
4 #include <array>
5 #include <cstdint>
6 
7 namespace doip {
8 
9 /**
10  * @brief Type alias for MAC address (6 bytes)
11  */
12 using MacAddress = std::array<uint8_t, 6>;
13 
14 /**
15  * @brief Retrieves the MAC address of a network interface.
16  *
17  * This function is cross-platform and works on Linux, macOS, and Windows.
18  * It retrieves the hardware (MAC) address of the specified network interface.
19  *
20  * @param ifname The name of the network interface (e.g., "eth0" on Linux, "en0" on macOS, "Ethernet" on Windows)
21  * If nullptr, the function will attempt to find the first available Ethernet interface.
22  * @param mac Reference to a MacAddress where the result will be stored
23  * @return true if the MAC address was successfully retrieved, false otherwise
24  *
25  * @note On Linux: Requires interface names like "eth0", "ens33", "wlan0", etc.
26  * @note On macOS: Requires interface names like "en0", "en1", etc.
27  * @note On Windows: Requires adapter names like "Ethernet", "Wi-Fi", etc.
28  *
29  * @example
30  * MacAddress mac;
31  * if (getMacAddress("eth0", mac)) {
32  * // MAC address successfully retrieved
33  * printf("%02X:%02X:%02X:%02X:%02X:%02X\n",
34  * mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);
35  * }
36  */
37 bool getMacAddress(const char* ifname, MacAddress& mac);
38 
39 /**
40  * @brief Retrieves the MAC address of the first available network interface.
41  *
42  * This is a convenience function that attempts to find and return the MAC address
43  * of the first available network interface on the system.
44  *
45  * @param mac Reference to a MacAddress where the result will be stored
46  * @return true if a MAC address was successfully retrieved, false otherwise
47  */
49 
50 } // namespace doip
51 
52 #endif /* MACADDRESS_H */
Definition: AnsiColors.h:3
bool getFirstMacAddress(MacAddress &mac)
Retrieves the MAC address of the first available network interface.
bool getMacAddress(const char *ifname, MacAddress &mac)
std::array< uint8_t, 6 > MacAddress
Type alias for MAC address (6 bytes)
Definition: MacAddress.h:12