OpenVPN 3 Core Library
Loading...
Searching...
No Matches
tunutil.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// Tun interface utilities for Mac OS X.
13
14#ifndef OPENVPN_TUN_MAC_TUNUTIL_H
15#define OPENVPN_TUN_MAC_TUNUTIL_H
16
17#include <fcntl.h>
18#include <errno.h>
19
20#include <string>
21
27#include <openvpn/tun/layer.hpp>
28
30OPENVPN_EXCEPTION(tun_mac_util);
31
32inline int tuntap_open(const Layer &layer, std::string &name)
33{
34 for (int i = 0; i < 256; ++i)
35 {
36 const char *tuntap;
38 tuntap = "tun";
39 else if (layer() == Layer::OSI_LAYER_2)
40 tuntap = "tap";
41 else
42 throw tun_mac_util("unknown OSI layer");
43 const std::string node_str = tuntap + to_string(i);
44 const std::string node_fn = "/dev/" + node_str;
45
46 ScopedFD fd(open(node_fn.c_str(), O_RDWR));
47 if (fd.defined())
48 {
49 // got it
50 if (fcntl(fd(), F_SETFL, O_NONBLOCK) < 0)
51 throw tun_mac_util("fcntl error on " + node_fn + " : " + errinfo(errno));
52
53 name = node_str;
54 return fd.release();
55 }
56 }
57 throw tun_mac_util(std::string("error opening Mac ") + layer.dev_type() + " device");
58}
59
60} // namespace openvpn::TunMac::Util
61
62#endif
bool defined() const
Definition scoped_fd.hpp:58
#define OPENVPN_EXCEPTION(C)
int tuntap_open(const Layer &layer, std::string &name)
Definition tunutil.hpp:32
std::string errinfo(ErrorCode err)
Definition asioerr.hpp:23
const auto layer