OpenVPN 3 Core Library
Loading...
Searching...
No Matches
hwaddr.hpp
Go to the documentation of this file.
1// OpenVPN -- An application to securely tunnel IP networks
2// over a single port, with support for SSL/TLS-based
3// session authentication and key exchange,
4// packet encryption, packet authentication, and
5// packet compression.
6//
7// Copyright (C) 2012- OpenVPN Inc.
8//
9// SPDX-License-Identifier: MPL-2.0 OR AGPL-3.0-only WITH openvpn3-openssl-exception
10//
11
12// Get the local MAC addr of the interface that owns the default route
13
14#ifndef OPENVPN_NETCONF_HWADDR_H
15#define OPENVPN_NETCONF_HWADDR_H
16
17#include <string>
18
21
22#if defined(OPENVPN_PLATFORM_WIN) && !defined(OPENVPN_PLATFORM_UWP)
24#elif defined(OPENVPN_PLATFORM_MAC)
26#elif defined(TARGET_OS_IPHONE)
27#include <UIKit/UIKit.h>
28#endif
29
30namespace openvpn {
31inline std::string get_hwaddr([[maybe_unused]] IP::Addr server_addr)
32{
33#if defined(OPENVPN_PLATFORM_WIN) && !defined(OPENVPN_PLATFORM_UWP)
34 const TunWin::Util::BestGateway dg{AF_INET};
35 if (dg.defined())
36 {
37 const TunWin::Util::IPAdaptersInfo ai_list;
38 const IP_ADAPTER_INFO *ai = ai_list.adapter(dg.interface_index());
39 if (ai)
40 {
41 const MACAddr mac(ai->Address);
42 return mac.to_string();
43 }
44 }
45#elif defined(OPENVPN_PLATFORM_MAC)
46 const MacGatewayInfo gw{server_addr};
47 if (gw.hwaddr_defined())
48 {
49 const MACAddr &mac = gw.hwaddr();
50 return mac.to_string();
51 }
52#elif defined(TARGET_OS_IPHONE)
53 // as reported at https://developer.apple.com/library/content/releasenotes/General/WhatsNewIniOS/Articles/iOS7.html#//apple_ref/doc/uid/TP40013162-SW34
54 // we can't get the MAC address from iOS for privacy reasons, but we can
55 // use the UUID as unique identifier. It is unique among the App domain,
56 // meaning that a different app will get a different UUID from this call
57 const NSString *uuid = [[[UIDevice currentDevice] identifierForVendor] UUIDString];
58 return std::string([uuid UTF8String]);
59#endif
60 return std::string();
61}
62} // namespace openvpn
63
64#endif
std::string to_string() const
Definition macaddr.hpp:40
Support deferred server-side state creation when client connects.
Definition ovpncli.cpp:95
std::string get_hwaddr(IP::Addr server_addr)
Definition hwaddr.hpp:31
const IP_ADAPTER_INFO * adapter(const DWORD index) const
Definition tunutil.hpp:809