From e713e08a11cad9bb41ca51f4db2676df7313504e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marek=20K=C3=BCthe?= Date: Thu, 21 Dec 2023 14:44:06 +0000 Subject: [PATCH 01/15] C++ bindings: Avoidance of code redundancy MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Marek Küthe --- bindings/cpp/tuntap++.cc | 141 ++++++--------------------------------- bindings/cpp/tuntap++.hh | 47 ++----------- 2 files changed, 26 insertions(+), 162 deletions(-) diff --git a/bindings/cpp/tuntap++.cc b/bindings/cpp/tuntap++.cc index cbd7420..7c6f586 100644 --- a/bindings/cpp/tuntap++.cc +++ b/bindings/cpp/tuntap++.cc @@ -6,203 +6,102 @@ 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 > TUNTAP_ID_MAX) { + throw std::invalid_argument("Tunnel ID too big"); } -} - -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) == -1) { throw std::runtime_error("tuntap_start failed"); } } -tap::~tap() +tuntap::~tuntap() { if (_started) { tuntap_destroy(_dev); } } -tap::tap(tap &&t) +tuntap::tuntap(tuntap &&t) : _dev(nullptr) { std::swap(t._dev, this->_dev); } - void -tap::release() +tuntap::release() { tuntap_release(_dev); _started = false; } std::string -tap::name() const +tuntap::name() const { return 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()); -} - t_tun -tap::native_handle() const +tuntap::native_handle() const { return tuntap_get_fd(this->_dev); } void -tap::up() +tuntap::up() { tuntap_up(_dev); } void -tap::down() +tuntap::down() { tuntap_down(_dev); } int -tap::mtu() const +tuntap::mtu() const { return tuntap_get_mtu(_dev); } void -tap::mtu(int m) +tuntap::mtu(int m) { tuntap_set_mtu(_dev, m); } 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); } int -tap::read(void *buf, size_t len) +tuntap::read(void *buf, size_t len) { return tuntap_read(_dev, buf, len); } int -tap::write(void *buf, size_t len) +tuntap::write(void *buf, size_t len) { return tuntap_write(_dev, buf, len); } void -tap::nonblocking(bool b) +tuntap::nonblocking(bool b) { tuntap_set_nonblocking(_dev, int(b)); } diff --git a/bindings/cpp/tuntap++.hh b/bindings/cpp/tuntap++.hh index 18f23ac..8d73708 100644 --- a/bindings/cpp/tuntap++.hh +++ b/bindings/cpp/tuntap++.hh @@ -8,14 +8,14 @@ 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 &&); // Properties std::string name() const; @@ -41,41 +41,6 @@ class tun 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 nonblocking(bool); - private: - struct device* _dev; - bool _started; -}; - } /* tuntap */ From b91f1599fd4d00c7798b4eb40521016687bca702 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marek=20K=C3=BCthe?= Date: Thu, 21 Dec 2023 14:45:01 +0000 Subject: [PATCH 02/15] C++ bindings: Add option to specify tunnel id MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Marek Küthe --- bindings/cpp/tuntap++.cc | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/bindings/cpp/tuntap++.cc b/bindings/cpp/tuntap++.cc index 7c6f586..af3e456 100644 --- a/bindings/cpp/tuntap++.cc +++ b/bindings/cpp/tuntap++.cc @@ -12,10 +12,10 @@ tuntap::tuntap(int mode, int id) if (mode != TUNTAP_MODE_ETHERNET && mode != TUNTAP_MODE_TUNNEL) { throw std::invalid_argument("Unknown tuntap mode"); } - if (id > TUNTAP_ID_MAX) { - throw std::invalid_argument("Tunnel ID too big"); + if (id < 0 || id > TUNTAP_ID_MAX) { + throw std::invalid_argument("Tunnel ID is invalid"); } - if (tuntap_start(_dev, mode, id) == -1) { + if (tuntap_start(_dev, mode, id)) { throw std::runtime_error("tuntap_start failed"); } } From a820f53608485246861439f885ddcc040056af2b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marek=20K=C3=BCthe?= Date: Thu, 21 Dec 2023 14:45:34 +0000 Subject: [PATCH 03/15] C++ bindings: Add explicit cast to string MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Marek Küthe --- bindings/cpp/tuntap++.cc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/bindings/cpp/tuntap++.cc b/bindings/cpp/tuntap++.cc index af3e456..5df048d 100644 --- a/bindings/cpp/tuntap++.cc +++ b/bindings/cpp/tuntap++.cc @@ -43,7 +43,7 @@ tuntap::release() std::string tuntap::name() const { - return tuntap_get_ifname(_dev); + return std::string(tuntap_get_ifname(_dev)); } void From 86756c2699c9ff2486ae910bfe836f4ff9c05619 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marek=20K=C3=BCthe?= Date: Thu, 21 Dec 2023 14:46:25 +0000 Subject: [PATCH 04/15] C++ bindings: Improve error handling for set ifname MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Marek Küthe --- bindings/cpp/tuntap++.cc | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/bindings/cpp/tuntap++.cc b/bindings/cpp/tuntap++.cc index 5df048d..aac4230 100644 --- a/bindings/cpp/tuntap++.cc +++ b/bindings/cpp/tuntap++.cc @@ -49,7 +49,9 @@ tuntap::name() const void tuntap::name(std::string const &s) { - tuntap_set_ifname(_dev, s.c_str()); + if (tuntap_set_ifname(_dev, s.c_str())) { + throw std::runtime_error("Failed to set ifname"); + } } t_tun From caaff9029fce0803ded3b9e75ac6ca352ab94bbc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marek=20K=C3=BCthe?= Date: Thu, 21 Dec 2023 14:48:12 +0000 Subject: [PATCH 05/15] C++ bindings: Improve error handling for set interface up MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Marek Küthe --- bindings/cpp/tuntap++.cc | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/bindings/cpp/tuntap++.cc b/bindings/cpp/tuntap++.cc index aac4230..1d31fb6 100644 --- a/bindings/cpp/tuntap++.cc +++ b/bindings/cpp/tuntap++.cc @@ -63,7 +63,9 @@ tuntap::native_handle() const void tuntap::up() { - tuntap_up(_dev); + if (tuntap_up(_dev)) { + throw std::runtime_error("Failed to bring up tuntap device"); + } } void From 5ab4f0582716e2fddee97a05c52c16ee5f1db6e0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marek=20K=C3=BCthe?= Date: Thu, 21 Dec 2023 14:48:28 +0000 Subject: [PATCH 06/15] C++ bindings: Improve error handling for set interface down MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Marek Küthe --- bindings/cpp/tuntap++.cc | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/bindings/cpp/tuntap++.cc b/bindings/cpp/tuntap++.cc index 1d31fb6..786a0ac 100644 --- a/bindings/cpp/tuntap++.cc +++ b/bindings/cpp/tuntap++.cc @@ -71,7 +71,9 @@ tuntap::up() void tuntap::down() { - tuntap_down(_dev); + if (tuntap_down(_dev)) { + throw std::runtime_error("Failed to bring down tuntap device"); + } } int From 2bd243c44d3754343f29eaa3743c31066d4d2650 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marek=20K=C3=BCthe?= Date: Thu, 21 Dec 2023 14:49:18 +0000 Subject: [PATCH 07/15] C++ bindings: Improve error handling for setting mtu MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Marek Küthe --- bindings/cpp/tuntap++.cc | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/bindings/cpp/tuntap++.cc b/bindings/cpp/tuntap++.cc index 786a0ac..8ef2729 100644 --- a/bindings/cpp/tuntap++.cc +++ b/bindings/cpp/tuntap++.cc @@ -85,7 +85,9 @@ tuntap::mtu() const void tuntap::mtu(int m) { - tuntap_set_mtu(_dev, m); + if (tuntap_set_mtu(_dev, m)) { + throw std::runtime_error("Failed to set mtu for tuntap device"); + } } void From 4cb7b44ea1b91054d22d79ee7bbce55f60436232 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marek=20K=C3=BCthe?= Date: Thu, 21 Dec 2023 14:49:45 +0000 Subject: [PATCH 08/15] C++ bindings: Improve error handling for setting ip address MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Marek Küthe --- bindings/cpp/tuntap++.cc | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/bindings/cpp/tuntap++.cc b/bindings/cpp/tuntap++.cc index 8ef2729..a61b009 100644 --- a/bindings/cpp/tuntap++.cc +++ b/bindings/cpp/tuntap++.cc @@ -93,7 +93,9 @@ tuntap::mtu(int m) void tuntap::ip(std::string const &s, int netmask) { - tuntap_set_ip(_dev, s.c_str(), netmask); + if (tuntap_set_ip(_dev, s.c_str(), netmask)) { + throw std::runtime_error("Failed to set ip for tuntap device"); + } } int From 4ed831888965faed3fa50534b63e171309e78c0c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marek=20K=C3=BCthe?= Date: Thu, 21 Dec 2023 14:50:32 +0000 Subject: [PATCH 09/15] C++ bindings: Improve error handling for changing non-blocking state MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Marek Küthe --- bindings/cpp/tuntap++.cc | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/bindings/cpp/tuntap++.cc b/bindings/cpp/tuntap++.cc index a61b009..e6a2f2c 100644 --- a/bindings/cpp/tuntap++.cc +++ b/bindings/cpp/tuntap++.cc @@ -113,7 +113,9 @@ tuntap::write(void *buf, size_t len) void 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 */ From da1b387a9cf9c9bb311bb24631e2006b85073dd3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marek=20K=C3=BCthe?= Date: Thu, 21 Dec 2023 14:52:16 +0000 Subject: [PATCH 10/15] C++ bindings: Improve argument validation MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Marek Küthe --- bindings/cpp/tuntap++.cc | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/bindings/cpp/tuntap++.cc b/bindings/cpp/tuntap++.cc index e6a2f2c..1fba09f 100644 --- a/bindings/cpp/tuntap++.cc +++ b/bindings/cpp/tuntap++.cc @@ -85,6 +85,9 @@ tuntap::mtu() const void tuntap::mtu(int 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"); } @@ -93,6 +96,9 @@ tuntap::mtu(int m) void tuntap::ip(std::string const &s, int 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"); } From 210fa0970e5a3a520189ee7947e159131bd036c9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marek=20K=C3=BCthe?= Date: Thu, 21 Dec 2023 17:25:42 +0000 Subject: [PATCH 11/15] C++ bindings: Add noexcept hints --- bindings/cpp/tuntap++.cc | 14 +++++++------- bindings/cpp/tuntap++.hh | 14 +++++++------- 2 files changed, 14 insertions(+), 14 deletions(-) diff --git a/bindings/cpp/tuntap++.cc b/bindings/cpp/tuntap++.cc index 1fba09f..21e9b5c 100644 --- a/bindings/cpp/tuntap++.cc +++ b/bindings/cpp/tuntap++.cc @@ -27,21 +27,21 @@ tuntap::~tuntap() } } -tuntap::tuntap(tuntap &&t) +tuntap::tuntap(tuntap &&t) noexcept : _dev(nullptr) { std::swap(t._dev, this->_dev); } void -tuntap::release() +tuntap::release() noexcept { tuntap_release(_dev); _started = false; } std::string -tuntap::name() const +tuntap::name() const noexcept { return std::string(tuntap_get_ifname(_dev)); } @@ -55,7 +55,7 @@ tuntap::name(std::string const &s) } t_tun -tuntap::native_handle() const +tuntap::native_handle() const noexcept { return tuntap_get_fd(this->_dev); } @@ -77,7 +77,7 @@ tuntap::down() } int -tuntap::mtu() const +tuntap::mtu() const noexcept { return tuntap_get_mtu(_dev); } @@ -105,13 +105,13 @@ tuntap::ip(std::string const &s, int netmask) } int -tuntap::read(void *buf, size_t len) +tuntap::read(void *buf, size_t len) noexcept { return tuntap_read(_dev, buf, len); } int -tuntap::write(void *buf, size_t len) +tuntap::write(void *buf, size_t len) noexcept { return tuntap_write(_dev, buf, len); } diff --git a/bindings/cpp/tuntap++.hh b/bindings/cpp/tuntap++.hh index 8d73708..f546312 100644 --- a/bindings/cpp/tuntap++.hh +++ b/bindings/cpp/tuntap++.hh @@ -15,14 +15,14 @@ class tuntap ~tuntap(); tuntap(tuntap const &) = delete; tuntap & operator = (tuntap const &) = delete; - tuntap(tuntap &&); + 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(); @@ -30,11 +30,11 @@ class tuntap 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 release() noexcept; void nonblocking(bool); private: struct device* _dev; From 3a1be57260511a2c6aa87889f7dc0158fc84cb84 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marek=20K=C3=BCthe?= Date: Tue, 12 Mar 2024 09:59:10 +0000 Subject: [PATCH 12/15] Improve style --- bindings/cpp/tuntap++.cc | 30 +++++++++++++++--------------- 1 file changed, 15 insertions(+), 15 deletions(-) diff --git a/bindings/cpp/tuntap++.cc b/bindings/cpp/tuntap++.cc index 21e9b5c..d0c3a70 100644 --- a/bindings/cpp/tuntap++.cc +++ b/bindings/cpp/tuntap++.cc @@ -7,7 +7,7 @@ namespace tuntap { tuntap::tuntap(int mode, int id) - : _dev{tuntap_init()}, _started{true} + : _dev{::tuntap_init()}, _started{true} { if (mode != TUNTAP_MODE_ETHERNET && mode != TUNTAP_MODE_TUNNEL) { throw std::invalid_argument("Unknown tuntap mode"); @@ -15,7 +15,7 @@ tuntap::tuntap(int mode, int id) if (id < 0 || id > TUNTAP_ID_MAX) { throw std::invalid_argument("Tunnel ID is invalid"); } - if (tuntap_start(_dev, mode, id)) { + if (::tuntap_start(_dev, mode, id)) { throw std::runtime_error("tuntap_start failed"); } } @@ -23,7 +23,7 @@ tuntap::tuntap(int mode, int id) tuntap::~tuntap() { if (_started) { - tuntap_destroy(_dev); + ::tuntap_destroy(_dev); } } @@ -36,20 +36,20 @@ tuntap::tuntap(tuntap &&t) noexcept void tuntap::release() noexcept { - tuntap_release(_dev); + ::tuntap_release(_dev); _started = false; } std::string tuntap::name() const noexcept { - return std::string(tuntap_get_ifname(_dev)); + return std::string(::tuntap_get_ifname(_dev)); } void tuntap::name(std::string const &s) { - if (tuntap_set_ifname(_dev, s.c_str())) { + if (::tuntap_set_ifname(_dev, s.c_str())) { throw std::runtime_error("Failed to set ifname"); } } @@ -57,13 +57,13 @@ tuntap::name(std::string const &s) t_tun tuntap::native_handle() const noexcept { - return tuntap_get_fd(this->_dev); + return ::tuntap_get_fd(this->_dev); } void tuntap::up() { - if (tuntap_up(_dev)) { + if (::tuntap_up(_dev)) { throw std::runtime_error("Failed to bring up tuntap device"); } } @@ -71,7 +71,7 @@ tuntap::up() void tuntap::down() { - if (tuntap_down(_dev)) { + if (::tuntap_down(_dev)) { throw std::runtime_error("Failed to bring down tuntap device"); } } @@ -79,7 +79,7 @@ tuntap::down() int tuntap::mtu() const noexcept { - return tuntap_get_mtu(_dev); + return ::tuntap_get_mtu(_dev); } void @@ -88,7 +88,7 @@ tuntap::mtu(int m) if (m < 1 || m > 65535) { throw std::invalid_argument("Invalid mtu"); } - if (tuntap_set_mtu(_dev, m)) { + if (::tuntap_set_mtu(_dev, m)) { throw std::runtime_error("Failed to set mtu for tuntap device"); } } @@ -99,7 +99,7 @@ tuntap::ip(std::string const &s, int netmask) if (netmask > 128) { throw std::invalid_argument("Invalid netmask"); } - if (tuntap_set_ip(_dev, s.c_str(), netmask)) { + if (::tuntap_set_ip(_dev, s.c_str(), netmask)) { throw std::runtime_error("Failed to set ip for tuntap device"); } } @@ -107,19 +107,19 @@ tuntap::ip(std::string const &s, int netmask) int tuntap::read(void *buf, size_t len) noexcept { - return tuntap_read(_dev, buf, len); + return ::tuntap_read(_dev, buf, len); } int tuntap::write(void *buf, size_t len) noexcept { - return tuntap_write(_dev, buf, len); + return ::tuntap_write(_dev, buf, len); } void tuntap::nonblocking(bool b) { - if (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"); } } From 19e7e447c0dec65beef51508d54e8af279411613 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marek=20K=C3=BCthe?= Date: Wed, 13 Mar 2024 21:08:58 +0000 Subject: [PATCH 13/15] Fix memory leak See https://stackoverflow.com/questions/78143322/how-can-i-read-from-a-tap-device-via-posixstream-descriptor-simultaneously-w/78143690?noredirect=1#comment137772998_78143690 --- bindings/cpp/tuntap++.cc | 40 ++++++++++++++++------------------------ bindings/cpp/tuntap++.hh | 12 +++++++++--- 2 files changed, 25 insertions(+), 27 deletions(-) diff --git a/bindings/cpp/tuntap++.cc b/bindings/cpp/tuntap++.cc index d0c3a70..3dee788 100644 --- a/bindings/cpp/tuntap++.cc +++ b/bindings/cpp/tuntap++.cc @@ -7,7 +7,7 @@ namespace tuntap { tuntap::tuntap(int mode, int id) - : _dev{::tuntap_init()}, _started{true} + : _dev{::tuntap_init()} { if (mode != TUNTAP_MODE_ETHERNET && mode != TUNTAP_MODE_TUNNEL) { throw std::invalid_argument("Unknown tuntap mode"); @@ -15,41 +15,33 @@ tuntap::tuntap(int mode, int id) if (id < 0 || id > TUNTAP_ID_MAX) { throw std::invalid_argument("Tunnel ID is invalid"); } - if (::tuntap_start(_dev, mode, id)) { + if (::tuntap_start(_dev.get(), mode, id)) { throw std::runtime_error("tuntap_start failed"); } } -tuntap::~tuntap() -{ - if (_started) { - ::tuntap_destroy(_dev); - } -} - tuntap::tuntap(tuntap &&t) noexcept - : _dev(nullptr) + : _dev() { - std::swap(t._dev, this->_dev); + t._dev.swap(this->_dev); } void tuntap::release() noexcept { - ::tuntap_release(_dev); - _started = false; + _dev.release(); } std::string tuntap::name() const noexcept { - return std::string(::tuntap_get_ifname(_dev)); + return std::string(::tuntap_get_ifname(_dev.get())); } void tuntap::name(std::string const &s) { - if (::tuntap_set_ifname(_dev, s.c_str())) { + if (::tuntap_set_ifname(_dev.get(), s.c_str())) { throw std::runtime_error("Failed to set ifname"); } } @@ -57,13 +49,13 @@ tuntap::name(std::string const &s) t_tun tuntap::native_handle() const noexcept { - return ::tuntap_get_fd(this->_dev); + return ::tuntap_get_fd(_dev.get()); } void tuntap::up() { - if (::tuntap_up(_dev)) { + if (::tuntap_up(_dev.get())) { throw std::runtime_error("Failed to bring up tuntap device"); } } @@ -71,7 +63,7 @@ tuntap::up() void tuntap::down() { - if (::tuntap_down(_dev)) { + if (::tuntap_down(_dev.get())) { throw std::runtime_error("Failed to bring down tuntap device"); } } @@ -79,7 +71,7 @@ tuntap::down() int tuntap::mtu() const noexcept { - return ::tuntap_get_mtu(_dev); + return ::tuntap_get_mtu(_dev.get()); } void @@ -88,7 +80,7 @@ tuntap::mtu(int m) if (m < 1 || m > 65535) { throw std::invalid_argument("Invalid mtu"); } - if (::tuntap_set_mtu(_dev, m)) { + if (::tuntap_set_mtu(_dev.get(), m)) { throw std::runtime_error("Failed to set mtu for tuntap device"); } } @@ -99,7 +91,7 @@ tuntap::ip(std::string const &s, int netmask) if (netmask > 128) { throw std::invalid_argument("Invalid netmask"); } - if (::tuntap_set_ip(_dev, s.c_str(), netmask)) { + if (::tuntap_set_ip(_dev.get(), s.c_str(), netmask)) { throw std::runtime_error("Failed to set ip for tuntap device"); } } @@ -107,19 +99,19 @@ tuntap::ip(std::string const &s, int netmask) int tuntap::read(void *buf, size_t len) noexcept { - return ::tuntap_read(_dev, buf, len); + return ::tuntap_read(_dev.get(), buf, len); } int tuntap::write(void *buf, size_t len) noexcept { - return ::tuntap_write(_dev, buf, len); + return ::tuntap_write(_dev.get(), buf, len); } void tuntap::nonblocking(bool b) { - if (::tuntap_set_nonblocking(_dev, int(b))) { + if (::tuntap_set_nonblocking(_dev.get(), static_cast(b))) { throw std::runtime_error("Failed to change non-blocking state for tuntap device"); } } diff --git a/bindings/cpp/tuntap++.hh b/bindings/cpp/tuntap++.hh index f546312..a33322f 100644 --- a/bindings/cpp/tuntap++.hh +++ b/bindings/cpp/tuntap++.hh @@ -3,6 +3,7 @@ #define LIBTUNTAP_ALY0MA60 #include +#include #include @@ -12,7 +13,6 @@ class tuntap { public: tuntap(int, int = TUNTAP_ID_ANY); - ~tuntap(); tuntap(tuntap const &) = delete; tuntap & operator = (tuntap const &) = delete; tuntap(tuntap &&) noexcept; @@ -37,8 +37,14 @@ class tuntap void release() noexcept; void nonblocking(bool); private: - struct device* _dev; - bool _started; + class TunTapDestroyer final { + public: + void operator()(device * dev) const noexcept { + if (dev) + ::tuntap_destroy(dev); + } + }; + std::unique_ptr _dev; }; } /* tuntap */ From 68d65577242f54bd9b2785f521b75509ed50d0f1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marek=20K=C3=BCthe?= Date: Sun, 17 Mar 2024 11:10:53 +0000 Subject: [PATCH 14/15] Improve style by prefixing size_t with std --- bindings/cpp/tuntap++.cc | 8 ++------ bindings/cpp/tuntap++.hh | 6 ++++-- 2 files changed, 6 insertions(+), 8 deletions(-) diff --git a/bindings/cpp/tuntap++.cc b/bindings/cpp/tuntap++.cc index 3dee788..87972f6 100644 --- a/bindings/cpp/tuntap++.cc +++ b/bindings/cpp/tuntap++.cc @@ -1,9 +1,5 @@ #include "tuntap++.hh" -#include -#include -#include - namespace tuntap { tuntap::tuntap(int mode, int id) @@ -97,13 +93,13 @@ tuntap::ip(std::string const &s, int netmask) } int -tuntap::read(void *buf, size_t len) noexcept +tuntap::read(void *buf, std::size_t len) noexcept { return ::tuntap_read(_dev.get(), buf, len); } int -tuntap::write(void *buf, size_t len) noexcept +tuntap::write(void *buf, std::size_t len) noexcept { return ::tuntap_write(_dev.get(), buf, len); } diff --git a/bindings/cpp/tuntap++.hh b/bindings/cpp/tuntap++.hh index a33322f..3f1a269 100644 --- a/bindings/cpp/tuntap++.hh +++ b/bindings/cpp/tuntap++.hh @@ -4,6 +4,8 @@ #include #include +#include +#include #include @@ -30,8 +32,8 @@ class tuntap void ip(std::string const &presentation, int netmask); //IO - int read(void *buf, size_t len) noexcept; - int write(void *buf, size_t len) noexcept; + int read(void *buf, std::size_t len) noexcept; + int write(void *buf, std::size_t len) noexcept; // System void release() noexcept; From 376e644bff26203f4ae8a4876c9bd9d58c91bdeb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marek=20K=C3=BCthe?= Date: Mon, 18 Mar 2024 09:42:28 +0000 Subject: [PATCH 15/15] Improve style --- bindings/cpp/tuntap++.hh | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/bindings/cpp/tuntap++.hh b/bindings/cpp/tuntap++.hh index 3f1a269..ce2f5a2 100644 --- a/bindings/cpp/tuntap++.hh +++ b/bindings/cpp/tuntap++.hh @@ -2,6 +2,12 @@ #ifndef LIBTUNTAP_ALY0MA60 #define LIBTUNTAP_ALY0MA60 +#if __cplusplus >= 202002L + #define LIBTUNTAP_ALY0MA60_CONSTEXPR constexpr +#else + #define LIBTUNTAP_ALY0MA60_CONSTEXPR +#endif + #include #include #include @@ -41,7 +47,7 @@ class tuntap private: class TunTapDestroyer final { public: - void operator()(device * dev) const noexcept { + LIBTUNTAP_ALY0MA60_CONSTEXPR void operator()(device * dev) const noexcept { if (dev) ::tuntap_destroy(dev); }