Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

C++ bindings #48

Open
wants to merge 15 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 11 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
173 changes: 45 additions & 128 deletions bindings/cpp/tuntap++.cc
Original file line number Diff line number Diff line change
Expand Up @@ -6,205 +6,122 @@

namespace tuntap {

tun::tun()
tuntap::tuntap(int mode, int id)
: _dev{tuntap_init()}, _started{true}
{
if (tuntap_start(_dev, TUNTAP_MODE_TUNNEL, TUNTAP_ID_ANY) == -1) {
throw std::runtime_error("tuntap_start failed");
if (mode != TUNTAP_MODE_ETHERNET && mode != TUNTAP_MODE_TUNNEL) {
throw std::invalid_argument("Unknown tuntap mode");
}
}

tun::~tun()
{
if (_started) {
tuntap_destroy(_dev);
if (id < 0 || id > TUNTAP_ID_MAX) {
throw std::invalid_argument("Tunnel ID is invalid");
}
}

tun::tun(tun &&t)
: _dev(nullptr)
{
std::swap(t._dev, this->_dev);
}

void
tun::release()
{
tuntap_release(_dev);
_started = false;
}

std::string
tun::name() const
{
return tuntap_get_ifname(_dev);
}

void
tun::name(std::string const &s)
{
tuntap_set_ifname(_dev, s.c_str());
}

t_tun
tun::native_handle() const
{
return tuntap_get_fd(this->_dev);
}

void
tun::up()
{
tuntap_up(_dev);
}

void
tun::down()
{
tuntap_down(_dev);
}

int
tun::mtu() const
{
return tuntap_get_mtu(_dev);
}

void
tun::mtu(int m)
{
tuntap_set_mtu(_dev, m);
}

void
tun::ip(std::string const &s, int netmask)
{
tuntap_set_ip(_dev, s.c_str(), netmask);
}

int
tun::read(void *buf, size_t len)
{
return tuntap_read(_dev, buf, len);
}

int
tun::write(void *buf, size_t len)
{
return tuntap_write(_dev, buf, len);
}

void
tun::nonblocking(bool b)
{
tuntap_set_nonblocking(_dev, int(b));
}

tap::tap()
: _dev{tuntap_init()}, _started{true}
{
if (tuntap_start(_dev, TUNTAP_MODE_ETHERNET, TUNTAP_ID_ANY) == -1) {
if (tuntap_start(_dev, mode, id)) {
throw std::runtime_error("tuntap_start failed");
}
}

tap::~tap()
tuntap::~tuntap()
{
if (_started) {
tuntap_destroy(_dev);
}
}

tap::tap(tap &&t)
tuntap::tuntap(tuntap &&t) noexcept
: _dev(nullptr)
{
std::swap(t._dev, this->_dev);
}


void
tap::release()
tuntap::release() noexcept
{
tuntap_release(_dev);
_started = false;
}

std::string
tap::name() const
tuntap::name() const noexcept
{
return tuntap_get_ifname(_dev);
return std::string(tuntap_get_ifname(_dev));
}

void
tap::name(std::string const &s)
tuntap::name(std::string const &s)
{
tuntap_set_ifname(_dev, s.c_str());
}

std::string
tap::hwaddr() const
{
return tuntap_get_hwaddr(_dev);
}

void
tap::hwaddr(std::string const &s)
{
tuntap_set_hwaddr(_dev, s.c_str());
if (tuntap_set_ifname(_dev, s.c_str())) {
throw std::runtime_error("Failed to set ifname");
}
}

t_tun
tap::native_handle() const
tuntap::native_handle() const noexcept
{
return tuntap_get_fd(this->_dev);
}

void
tap::up()
tuntap::up()
{
tuntap_up(_dev);
if (tuntap_up(_dev)) {
throw std::runtime_error("Failed to bring up tuntap device");
}
}

void
tap::down()
tuntap::down()
{
tuntap_down(_dev);
if (tuntap_down(_dev)) {
throw std::runtime_error("Failed to bring down tuntap device");
}
}

int
tap::mtu() const
tuntap::mtu() const noexcept
{
return tuntap_get_mtu(_dev);
}

void
tap::mtu(int m)
tuntap::mtu(int m)
{
tuntap_set_mtu(_dev, m);
if (m < 1 || m > 65535) {
throw std::invalid_argument("Invalid mtu");
}
if (tuntap_set_mtu(_dev, m)) {
throw std::runtime_error("Failed to set mtu for tuntap device");
}
}

void
tap::ip(std::string const &s, int netmask)
tuntap::ip(std::string const &s, int netmask)
{
tuntap_set_ip(_dev, s.c_str(), netmask);
if (netmask > 128) {
throw std::invalid_argument("Invalid netmask");
}
if (tuntap_set_ip(_dev, s.c_str(), netmask)) {
throw std::runtime_error("Failed to set ip for tuntap device");
}
}

int
tap::read(void *buf, size_t len)
tuntap::read(void *buf, size_t len) noexcept
{
return tuntap_read(_dev, buf, len);
}

int
tap::write(void *buf, size_t len)
tuntap::write(void *buf, size_t len) noexcept
{
return tuntap_write(_dev, buf, len);
}

void
tap::nonblocking(bool b)
tuntap::nonblocking(bool b)
{
tuntap_set_nonblocking(_dev, int(b));
if (tuntap_set_nonblocking(_dev, int(b))) {
throw std::runtime_error("Failed to change non-blocking state for tuntap device");
}
}

} /* tuntap */
59 changes: 12 additions & 47 deletions bindings/cpp/tuntap++.hh
Original file line number Diff line number Diff line change
Expand Up @@ -8,68 +8,33 @@

namespace tuntap {

class tun
class tuntap
{
public:
tun();
~tun();
tun(tun const &) = delete;
tun & operator = (tun const &) = delete;
tun(tun &&);
tuntap(int, int = TUNTAP_ID_ANY);
~tuntap();
tuntap(tuntap const &) = delete;
tuntap & operator = (tuntap const &) = delete;
tuntap(tuntap &&) noexcept;

// Properties
std::string name() const;
std::string name() const noexcept;
void name(std::string const &);
int mtu() const ;
int mtu() const noexcept;
void mtu(int);
t_tun native_handle() const;
t_tun native_handle() const noexcept;

// Network
void up();
void down();
void ip(std::string const &presentation, int netmask);

//IO
int read(void *buf, size_t len);
int write(void *buf, size_t len);
int read(void *buf, size_t len) noexcept;
int write(void *buf, size_t len) noexcept;

// System
void release();
void nonblocking(bool);
private:
struct device* _dev;
bool _started;
};

class tap
{
public:
tap();
~tap();
tap(tap const &) = delete;
tap & operator = (tap const &) = delete;
tap(tap &&);

// Properties
std::string name() const;
void name(std::string const &);
std::string hwaddr() const;
void hwaddr(std::string const &);
int mtu() const;
void mtu(int);
t_tun native_handle() const;

// Network
void up();
void down();
void ip(std::string const &presentation, int netmask);

//IO
int read(void *buf, size_t len);
int write(void *buf, size_t len);

// System
void release();
void release() noexcept;
void nonblocking(bool);
private:
struct device* _dev;
Expand Down