-
Notifications
You must be signed in to change notification settings - Fork 172
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
this will help getting more granular reporting of connectivity status from kernel. it is linux/android specific for now with possible future support for bsd/darwin. this is very much a prototype; cleaner interface and code will follow. it adds libnl (ships with android) dependency which is really not required, although expedited prototyping.
- Loading branch information
Showing
10 changed files
with
631 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,140 @@ | ||
# | ||
# Find the native netlink includes and library | ||
# | ||
# If they exist, differentiate between versions 1, 2 and 3. | ||
# Version 1 does not have netlink/version.h | ||
# Version 2 started separating libraries (libnl{,-genl,-route}). | ||
# Version 3 (>= 3.2) started appending the major version number as suffix to | ||
# library names (libnl-3) | ||
# | ||
# NL_INCLUDE_DIRS - where to find libnl.h, etc. | ||
# NL_LIBRARIES - List of libraries when using libnl. | ||
# NL_FOUND - True if libnl found. | ||
|
||
if(NL_LIBRARIES AND NL_INCLUDE_DIRS) | ||
# in cache already | ||
SET(NL_FOUND TRUE) | ||
else() | ||
SET( SEARCHPATHS | ||
/opt/local | ||
/sw | ||
/usr | ||
/usr/local | ||
) | ||
|
||
find_package(PkgConfig) | ||
pkg_check_modules(NL3 libnl-3.0 libnl-genl-3.0 libnl-route-3.0) | ||
if(NOT NL3_FOUND) | ||
pkg_search_module(NL2 libnl-2.0) | ||
endif() | ||
|
||
# Try to find NL 2.0, 3.0 or 3.1 (/usr/include/netlink/version.h) or | ||
# NL >= 3.2 (/usr/include/libnl3/netlink/version.h) | ||
find_path(NL3_INCLUDE_DIR | ||
PATH_SUFFIXES | ||
include/libnl3 | ||
include | ||
NAMES | ||
netlink/version.h | ||
HINTS | ||
"${NL3_libnl-3.0_INCLUDEDIR}" | ||
"${NL2_INCLUDEDIR}" | ||
PATHS | ||
$(SEARCHPATHS) | ||
) | ||
# NL version >= 2 | ||
if(NL3_INCLUDE_DIR) | ||
find_library(NL3_LIBRARY | ||
NAMES | ||
nl-3 nl | ||
PATH_SUFFIXES | ||
lib64 lib | ||
HINTS | ||
"${NL3_libnl-3.0_LIBDIR}" | ||
"${NL2_LIBDIR}" | ||
PATHS | ||
$(SEARCHPATHS) | ||
) | ||
find_library(NLGENL_LIBRARY | ||
NAMES | ||
nl-genl-3 nl-genl | ||
PATH_SUFFIXES | ||
lib64 lib | ||
HINTS | ||
"${NL3_libnl-genl-3.0_LIBDIR}" | ||
"${NL2_LIBDIR}" | ||
PATHS | ||
$(SEARCHPATHS) | ||
) | ||
find_library(NLROUTE_LIBRARY | ||
NAMES | ||
nl-route-3 nl-route | ||
PATH_SUFFIXES | ||
lib64 lib | ||
HINTS | ||
"${NL3_libnl-route-3.0_LIBDIR}" | ||
"${NL2_LIBDIR}" | ||
PATHS | ||
$(SEARCHPATHS) | ||
) | ||
# | ||
# If we don't have all of those libraries, we can't use libnl. | ||
# | ||
if(NL3_LIBRARY AND NLGENL_LIBRARY AND NLROUTE_LIBRARY) | ||
set(NL_LIBRARY ${NL3_LIBRARY}) | ||
# NL2 and NL3 are similar and just affect how the version is reported in | ||
# the --version output. In cast of doubt, assume NL3 since a library | ||
# without version number could be any of 2.0, 3.0 or 3.1. | ||
if(NOT NL3_FOUND AND NL2_FOUND) | ||
set(HAVE_LIBNL2 1) | ||
else() | ||
set(HAVE_LIBNL3 1) | ||
endif() | ||
endif() | ||
set(NL_INCLUDE_DIR ${NL3_INCLUDE_DIR}) | ||
endif() | ||
|
||
# libnl-2 and libnl-3 not found, try NL version 1 | ||
if(NOT (NL_LIBRARY AND NL_INCLUDE_DIR)) | ||
pkg_search_module(NL1 libnl-1) | ||
find_path(NL1_INCLUDE_DIR | ||
NAMES | ||
netlink/netlink.h | ||
HINTS | ||
"${NL1_INCLUDEDIR}" | ||
PATHS | ||
$(SEARCHPATHS) | ||
) | ||
find_library(NL1_LIBRARY | ||
NAMES | ||
nl | ||
PATH_SUFFIXES | ||
lib64 lib | ||
HINTS | ||
"${NL1_LIBDIR}" | ||
PATHS | ||
$(SEARCHPATHS) | ||
) | ||
set(NL_LIBRARY ${NL1_LIBRARY}) | ||
set(NL_INCLUDE_DIR ${NL1_INCLUDE_DIR}) | ||
if(NL1_LIBRARY AND NL1_INCLUDE_DIR) | ||
set(HAVE_LIBNL1 1) | ||
endif() | ||
endif() | ||
endif() | ||
|
||
# handle the QUIETLY and REQUIRED arguments and set NL_FOUND to TRUE if | ||
# all listed variables are TRUE | ||
INCLUDE(FindPackageHandleStandardArgs) | ||
FIND_PACKAGE_HANDLE_STANDARD_ARGS(NL DEFAULT_MSG NL_LIBRARY NL_INCLUDE_DIR) | ||
|
||
IF(NL_FOUND) | ||
set(NL_LIBRARIES ${NLGENL_LIBRARY} ${NLROUTE_LIBRARY} ${NL_LIBRARY}) | ||
set(NL_INCLUDE_DIRS ${NL_INCLUDE_DIR}) | ||
set(HAVE_LIBNL 1) | ||
else() | ||
set(NL_LIBRARIES ) | ||
set(NL_INCLUDE_DIRS) | ||
endif() | ||
|
||
MARK_AS_ADVANCED( NL_LIBRARIES NL_INCLUDE_DIRS ) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
/* | ||
* Copyright (C) 2014-2020 Savoir-faire Linux Inc. | ||
* Author(s) : Paymon <[email protected]> | ||
* | ||
* This program is free software; you can redistribute it and/or modify | ||
* it under the terms of the GNU General Public License as published by | ||
* the Free Software Foundation; either version 3 of the License, or | ||
* (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU General Public License | ||
* along with this program. If not, see <https://www.gnu.org/licenses/>. | ||
*/ | ||
|
||
#pragma once | ||
|
||
#include "log.h" | ||
|
||
#include <functional> | ||
#include <thread> | ||
#include <mutex> | ||
#include <atomic> | ||
|
||
struct nl_sock; | ||
struct nl_msg; | ||
struct sockaddr_nl; | ||
struct ucred; | ||
|
||
namespace dht { | ||
namespace net { | ||
|
||
class OPENDHT_PUBLIC ConnectivityStatus | ||
{ | ||
public: | ||
enum class Event : long unsigned int { | ||
NONE_EVENT = 0, | ||
ADDR, | ||
ROUTE, | ||
LINK, | ||
NEIGHT, | ||
EVENTS_NEW, | ||
NEWLINK, | ||
NEWROUTE4, | ||
NEWROUTE6, | ||
NEWROUTE, | ||
NEWADDR4, | ||
NEWADDR6, | ||
NEWADDR, | ||
NEWNEIGH, | ||
NEIGHTBL, | ||
IPV4_MROUTE, | ||
IPV6_MROUTE, | ||
IP_MROUTE, | ||
EVENTS_DEL, | ||
DELLINK, | ||
DELROUTE4, | ||
DELROUTE6, | ||
DELROUTE, | ||
DELADDR4, | ||
DELADDR6, | ||
DELADDR, | ||
DELNEIGH | ||
}; | ||
|
||
ConnectivityStatus(); | ||
~ConnectivityStatus(); | ||
|
||
using ConnectionEventCb = std::function<void (Event)>; | ||
|
||
void setEventListener (ConnectionEventCb ucb, Event); | ||
void removeEventListener (Event); | ||
|
||
private: | ||
|
||
std::mutex mtx_; | ||
std::unique_ptr<dht::Logger> logger_; | ||
|
||
std::map<Event, ConnectionEventCb> event_cbs = {}; | ||
|
||
using NlMsgPtr = std::unique_ptr<nl_msg, void(*)(nl_msg *)>; | ||
NlMsgPtr bye; | ||
using NlPtr = std::unique_ptr<nl_sock, void(*)(nl_sock *)>; | ||
NlPtr nlsk; | ||
static NlPtr nlsk_init (); | ||
|
||
void nlsk_setup (nl_sock*); | ||
void nl_event_loop_thrd (nl_sock*); | ||
void get_neigh_state (struct nl_msg*); | ||
int nl_event_cb (struct nl_msg*); | ||
void executer (Event); | ||
|
||
std::atomic_bool stop {false}; | ||
|
||
std::thread thrd_; | ||
}; | ||
|
||
} /* namespace net */ | ||
} /* namespace dht */ |
Oops, something went wrong.