posix/interface: add initial interface query logic

This commit is contained in:
Danny Robson 2019-02-02 19:09:35 +11:00
parent 46ff7299bf
commit a64e721cf0
3 changed files with 68 additions and 0 deletions

View File

@ -118,6 +118,8 @@ if (NOT WIN32)
library_posix.hpp library_posix.hpp
library_posix.cpp library_posix.cpp
posix/fwd.hpp posix/fwd.hpp
posix/interface.hpp
posix/interface.cpp
posix/map.cpp posix/map.cpp
posix/map.hpp posix/map.hpp
posix/socket.cpp posix/socket.cpp

12
posix/interface.cpp Normal file
View File

@ -0,0 +1,12 @@
/*
* 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>
*/
#include "interface.hpp"
///////////////////////////////////////////////////////////////////////////////

54
posix/interface.hpp Normal file
View File

@ -0,0 +1,54 @@
/*
* 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"
#include <sys/types.h>
#include <ifaddrs.h>
namespace cruft::posix {
auto
interface (void)
{
struct iterator {
iterator ()
{
error::try_call (getifaddrs, &cursor);
}
iterator (ifaddrs *_cursor):
cursor (_cursor)
{ ; }
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; }
};
struct container {
auto begin (void) { return iterator ( ); }
auto end (void) { return iterator (nullptr); }
};
return container ();
}
}