Skip to content

Commit

Permalink
turn on debug
Browse files Browse the repository at this point in the history
  • Loading branch information
257 committed Feb 11, 2020
1 parent cd6139b commit 0229223
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 10 deletions.
7 changes: 7 additions & 0 deletions include/opendht/connstat.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,12 @@
#include <functional>
#include <thread>
#include <mutex>
#include <atomic>

struct nl_sock;
struct nl_msg;
struct sockaddr_nl;
struct ucred;

namespace dht {
namespace net {
Expand Down Expand Up @@ -78,6 +81,8 @@ class OPENDHT_PUBLIC ConnectivityStatus

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 ();
Expand All @@ -88,6 +93,8 @@ class OPENDHT_PUBLIC ConnectivityStatus
int nl_event_cb (struct nl_msg*);
void executer (Event);

std::atomic_bool stop {false};

std::thread thrd_;
};

Expand Down
2 changes: 2 additions & 0 deletions include/opendht/def.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,3 +42,5 @@

// bytes
#define HASH_LEN 20u

#define OPENDHT_UNUSED_ARG(arg) (void)arg
45 changes: 35 additions & 10 deletions src/connstat.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ namespace net {

ConnectivityStatus::ConnectivityStatus()
: logger_(new dht::Logger)
, bye(nlmsg_alloc_simple(NLMSG_NOOP, NLM_F_ECHO), &nlmsg_free)
, nlsk(nlsk_init())
{
nlsk_setup(nlsk.get());
Expand All @@ -20,7 +21,31 @@ ConnectivityStatus::ConnectivityStatus()

ConnectivityStatus::~ConnectivityStatus()
{
nlsk.reset();
stop = true;

nl_socket_modify_cb(nlsk.get(), NL_CB_VALID, NL_CB_CUSTOM, [](nl_msg* msg, void* data) -> int {
OPENDHT_UNUSED_ARG(msg);
if (((ConnectivityStatus*)data)->logger_)
((ConnectivityStatus*)data)->logger_->w("stopping msg processing\n");
return (((ConnectivityStatus*)data)->stop) ? NL_OK : NL_STOP;
}, (void*)this);

nl_socket_modify_cb(nlsk.get(), NL_CB_MSG_IN, NL_CB_CUSTOM, [](nl_msg* msg, void* data) -> int {
OPENDHT_UNUSED_ARG(msg);
if (((ConnectivityStatus*)data)->logger_)
((ConnectivityStatus*)data)->logger_->w("got incoming msg\n");
return (((ConnectivityStatus*)data)->stop) ? NL_OK : NL_STOP;
}, (void*)this);

nl_socket_modify_cb(nlsk.get(), NL_CB_ACK, NL_CB_CUSTOM, [](nl_msg* msg, void* data) -> int {
OPENDHT_UNUSED_ARG(msg);
if (((ConnectivityStatus*)data)->logger_)
((ConnectivityStatus*)data)->logger_->w("taking care of ACK\n");
return (((ConnectivityStatus*)data)->stop) ? NL_OK : NL_STOP;
}, (void*)this);


nl_send_auto(nlsk.get(), bye.get());

if (thrd_.joinable())
thrd_.join();
Expand Down Expand Up @@ -51,9 +76,9 @@ ConnectivityStatus::removeEventListener(Event event)
event_cbs.erase(event);
switch (event) {
case Event::NEWADDR:
nl_socket_drop_memberships(nlsk.get(), RTNLGRP_IPV6_IFADDR, RTNLGRP_NONE);
break;
case Event::DELADDR:
case Event::ADDR:
nl_socket_drop_memberships(nlsk.get(), RTNLGRP_IPV6_IFADDR, RTNLGRP_NONE);
nl_socket_drop_memberships(nlsk.get(), RTNLGRP_IPV4_IFADDR, RTNLGRP_NONE);
break;
default:
Expand All @@ -74,9 +99,10 @@ void
ConnectivityStatus::nlsk_setup(nl_sock* nlsk)
{
nl_socket_disable_seq_check(nlsk);
nl_socket_set_nonblocking(nlsk);

nl_socket_modify_cb(nlsk, NL_CB_VALID, NL_CB_CUSTOM, [](nl_msg* msg, void* data) -> int {
return ((ConnectivityStatus*)data)->nl_event_cb(msg);
return ((ConnectivityStatus*)data)->nl_event_cb(msg);
}, (void*)this);

nl_connect(nlsk, NETLINK_ROUTE);
Expand All @@ -88,9 +114,8 @@ ConnectivityStatus::nlsk_init(void)
NlPtr ret(nl_socket_alloc(), &nl_socket_free);
if (not ret.get())
throw std::runtime_error("couldn't allocate netlink socket!\n");


return ret;
else
return ret;
}

void
Expand Down Expand Up @@ -140,9 +165,9 @@ ConnectivityStatus::nl_event_cb(struct nl_msg* msg)
void
ConnectivityStatus::nl_event_loop_thrd(nl_sock *nlsk)
{
int status = NL_OK;
while ((status = nl_recvmsgs_default(nlsk)) >= 0)
logger_->w("still looping!\n");
int status;
while (!stop && (status = nl_recvmsgs_report(nlsk, nl_socket_get_cb(nlsk))) > 0)
;
}

} /* namespace net */
Expand Down

0 comments on commit 0229223

Please sign in to comment.