Commit c7114f5c authored by Dominik Charousset's avatar Dominik Charousset Committed by Dominik Charousset

Use socket guard instead of calling close manually

parent 9bf9af2f
#include "caf/config.hpp" /******************************************************************************
* ____ _ _____ *
* / ___| / \ | ___| C++ *
* | | / _ \ | |_ Actor *
* | |___ / ___ \| _| Framework *
* \____/_/ \_|_| *
* *
* Copyright 2011-2018 Dominik Charousset *
* *
* Distributed under the terms and conditions of the BSD 3-Clause License or *
* (at your option) under the terms and conditions of the Boost Software *
* License 1.0. See accompanying files LICENSE and LICENSE_ALTERNATIVE. *
* *
* If you did not receive a copy of the license files, see *
* http://opensource.org/licenses/BSD-3-Clause and *
* http://www.boost.org/LICENSE_1_0.txt. *
******************************************************************************/
#include "caf/detail/get_mac_addresses.hpp" #include "caf/detail/get_mac_addresses.hpp"
#include "caf/config.hpp"
#include "caf/detail/socket_guard.hpp"
#if defined(CAF_MACOS) || defined(CAF_BSD) || defined(CAF_IOS) #if defined(CAF_MACOS) || defined(CAF_BSD) || defined(CAF_IOS)
#include <sys/types.h> #include <sys/types.h>
...@@ -110,6 +130,7 @@ std::vector<iface_info> get_mac_addresses() { ...@@ -110,6 +130,7 @@ std::vector<iface_info> get_mac_addresses() {
perror("socket"); perror("socket");
return {}; return {};
} }
socket_guard guard{sck};
// query available interfaces // query available interfaces
char buf[1024] = {0}; char buf[1024] = {0};
ifconf ifc; ifconf ifc;
...@@ -117,7 +138,6 @@ std::vector<iface_info> get_mac_addresses() { ...@@ -117,7 +138,6 @@ std::vector<iface_info> get_mac_addresses() {
ifc.ifc_buf = buf; ifc.ifc_buf = buf;
if (ioctl(sck, SIOCGIFCONF, &ifc) < 0) { if (ioctl(sck, SIOCGIFCONF, &ifc) < 0) {
perror("ioctl(SIOCGIFCONF)"); perror("ioctl(SIOCGIFCONF)");
close(sck);
return {}; return {};
} }
std::vector<iface_info> result; std::vector<iface_info> result;
...@@ -132,7 +152,6 @@ std::vector<iface_info> get_mac_addresses() { ...@@ -132,7 +152,6 @@ std::vector<iface_info> get_mac_addresses() {
// get mac address // get mac address
if (ioctl(sck, SIOCGIFHWADDR, item) < 0) { if (ioctl(sck, SIOCGIFHWADDR, item) < 0) {
perror("ioctl(SIOCGIFHWADDR)"); perror("ioctl(SIOCGIFHWADDR)");
close(sck);
return {}; return {};
} }
std::ostringstream oss; std::ostringstream oss;
...@@ -149,7 +168,6 @@ std::vector<iface_info> get_mac_addresses() { ...@@ -149,7 +168,6 @@ std::vector<iface_info> get_mac_addresses() {
result.push_back({item->ifr_name, std::move(addr)}); result.push_back({item->ifr_name, std::move(addr)});
} }
} }
close(sck);
return result; return result;
} }
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment