2019-02-02 19:09:35 +11:00
|
|
|
/*
|
|
|
|
* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
|
|
|
|
*
|
|
|
|
* Copyright 2019 Danny Robson <danny@nerdcruft.net>
|
|
|
|
*/
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include "except.hpp"
|
|
|
|
|
2022-07-25 11:58:59 +10:00
|
|
|
#include <utility>
|
|
|
|
|
2019-02-02 19:09:35 +11:00
|
|
|
#include <sys/types.h>
|
|
|
|
#include <ifaddrs.h>
|
|
|
|
|
|
|
|
|
|
|
|
namespace cruft::posix {
|
|
|
|
auto
|
|
|
|
interface (void)
|
|
|
|
{
|
|
|
|
struct iterator {
|
2019-02-02 20:39:32 +11:00
|
|
|
explicit iterator (ifaddrs *_cursor) noexcept
|
|
|
|
: cursor (_cursor)
|
2019-02-02 19:09:35 +11:00
|
|
|
{ ; }
|
|
|
|
|
|
|
|
ifaddrs *cursor;
|
|
|
|
|
|
|
|
decltype (auto) operator* (void) { return *cursor; }
|
|
|
|
decltype (auto) operator-> (void) { return cursor; }
|
|
|
|
|
|
|
|
iterator& operator++ (void)
|
|
|
|
{
|
|
|
|
cursor = cursor->ifa_next;
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool operator== (iterator const &rhs) const noexcept { return cursor == rhs.cursor; }
|
|
|
|
bool operator!= (iterator const &rhs) const noexcept { return cursor != rhs.cursor; }
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2019-02-02 20:39:32 +11:00
|
|
|
class container {
|
|
|
|
public:
|
|
|
|
container ()
|
|
|
|
{
|
|
|
|
error::try_call (getifaddrs, &m_data);
|
|
|
|
}
|
|
|
|
|
|
|
|
~container ()
|
|
|
|
{
|
|
|
|
if (m_data)
|
|
|
|
freeifaddrs (m_data);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
container (container &&rhs) noexcept:
|
|
|
|
m_data (std::exchange (rhs.m_data, nullptr))
|
|
|
|
{ ; }
|
|
|
|
|
|
|
|
container& operator= (container &&rhs) noexcept
|
|
|
|
{
|
|
|
|
std::swap (m_data, rhs.m_data);
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
container (container const&) = delete;
|
|
|
|
container& operator= (container const&) = delete;
|
|
|
|
|
|
|
|
auto begin (void) { return iterator (m_data); }
|
2019-02-02 19:09:35 +11:00
|
|
|
auto end (void) { return iterator (nullptr); }
|
2019-02-02 20:39:32 +11:00
|
|
|
|
|
|
|
ifaddrs *m_data;
|
2019-02-02 19:09:35 +11:00
|
|
|
};
|
|
|
|
|
|
|
|
return container ();
|
|
|
|
}
|
|
|
|
}
|