Skip to content

Commit 2e326f7

Browse files
committed
turn on debug
1 parent cd6139b commit 2e326f7

File tree

3 files changed

+44
-10
lines changed

3 files changed

+44
-10
lines changed

include/opendht/connstat.h

+7
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,12 @@
2323
#include <functional>
2424
#include <thread>
2525
#include <mutex>
26+
#include <atomic>
2627

2728
struct nl_sock;
2829
struct nl_msg;
30+
struct sockaddr_nl;
31+
struct ucred;
2932

3033
namespace dht {
3134
namespace net {
@@ -78,6 +81,8 @@ class OPENDHT_PUBLIC ConnectivityStatus
7881

7982
std::map<Event, ConnectionEventCb> event_cbs = {};
8083

84+
using NlMsgPtr = std::unique_ptr<nl_msg, void(*)(nl_msg *)>;
85+
NlMsgPtr bye;
8186
using NlPtr = std::unique_ptr<nl_sock, void(*)(nl_sock *)>;
8287
NlPtr nlsk;
8388
static NlPtr nlsk_init ();
@@ -88,6 +93,8 @@ class OPENDHT_PUBLIC ConnectivityStatus
8893
int nl_event_cb (struct nl_msg*);
8994
void executer (Event);
9095

96+
std::atomic_bool stop {false};
97+
9198
std::thread thrd_;
9299
};
93100

include/opendht/def.h

+2
Original file line numberDiff line numberDiff line change
@@ -42,3 +42,5 @@
4242

4343
// bytes
4444
#define HASH_LEN 20u
45+
46+
#define OPENDHT_UNUSED_ARG(arg) (void)arg

src/connstat.cpp

+35-10
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ namespace net {
1111

1212
ConnectivityStatus::ConnectivityStatus()
1313
: logger_(new dht::Logger)
14+
, bye(nlmsg_alloc_simple(NLMSG_NOOP, NLM_F_ECHO), &nlmsg_free)
1415
, nlsk(nlsk_init())
1516
{
1617
nlsk_setup(nlsk.get());
@@ -20,7 +21,31 @@ ConnectivityStatus::ConnectivityStatus()
2021

2122
ConnectivityStatus::~ConnectivityStatus()
2223
{
23-
nlsk.reset();
24+
stop = true;
25+
26+
nl_socket_modify_cb(nlsk.get(), NL_CB_VALID, NL_CB_CUSTOM, [](nl_msg* msg, void* data) -> int {
27+
OPENDHT_UNUSED_ARG(msg);
28+
if (((ConnectivityStatus*)data)->logger_)
29+
((ConnectivityStatus*)data)->logger_->w("stopping msg processing\n");
30+
return (((ConnectivityStatus*)data)->stop) ? NL_OK : NL_STOP;
31+
}, (void*)this);
32+
33+
nl_socket_modify_cb(nlsk.get(), NL_CB_MSG_IN, NL_CB_CUSTOM, [](nl_msg* msg, void* data) -> int {
34+
OPENDHT_UNUSED_ARG(msg);
35+
if (((ConnectivityStatus*)data)->logger_)
36+
((ConnectivityStatus*)data)->logger_->w("got incoming msg\n");
37+
return (((ConnectivityStatus*)data)->stop) ? NL_OK : NL_STOP;
38+
}, (void*)this);
39+
40+
nl_socket_modify_cb(nlsk.get(), NL_CB_ACK, NL_CB_CUSTOM, [](nl_msg* msg, void* data) -> int {
41+
OPENDHT_UNUSED_ARG(msg);
42+
if (((ConnectivityStatus*)data)->logger_)
43+
((ConnectivityStatus*)data)->logger_->w("taking care of ACK\n");
44+
return (((ConnectivityStatus*)data)->stop) ? NL_OK : NL_STOP;
45+
}, (void*)this);
46+
47+
48+
nl_send_auto(nlsk.get(), bye.get());
2449

2550
if (thrd_.joinable())
2651
thrd_.join();
@@ -51,9 +76,9 @@ ConnectivityStatus::removeEventListener(Event event)
5176
event_cbs.erase(event);
5277
switch (event) {
5378
case Event::NEWADDR:
54-
nl_socket_drop_memberships(nlsk.get(), RTNLGRP_IPV6_IFADDR, RTNLGRP_NONE);
55-
break;
5679
case Event::DELADDR:
80+
case Event::ADDR:
81+
nl_socket_drop_memberships(nlsk.get(), RTNLGRP_IPV6_IFADDR, RTNLGRP_NONE);
5782
nl_socket_drop_memberships(nlsk.get(), RTNLGRP_IPV4_IFADDR, RTNLGRP_NONE);
5883
break;
5984
default:
@@ -74,9 +99,10 @@ void
7499
ConnectivityStatus::nlsk_setup(nl_sock* nlsk)
75100
{
76101
nl_socket_disable_seq_check(nlsk);
102+
nl_socket_set_nonblocking(nlsk);
77103

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

82108
nl_connect(nlsk, NETLINK_ROUTE);
@@ -88,9 +114,8 @@ ConnectivityStatus::nlsk_init(void)
88114
NlPtr ret(nl_socket_alloc(), &nl_socket_free);
89115
if (not ret.get())
90116
throw std::runtime_error("couldn't allocate netlink socket!\n");
91-
92-
93-
return ret;
117+
else
118+
return ret;
94119
}
95120

96121
void
@@ -140,9 +165,9 @@ ConnectivityStatus::nl_event_cb(struct nl_msg* msg)
140165
void
141166
ConnectivityStatus::nl_event_loop_thrd(nl_sock *nlsk)
142167
{
143-
int status = NL_OK;
144-
while ((status = nl_recvmsgs_default(nlsk)) >= 0)
145-
logger_->w("still looping!\n");
168+
int status;
169+
while (!stop && (status = nl_recvmsgs_report(nlsk, nl_socket_get_cb(nlsk))) > 0)
170+
;
146171
}
147172

148173
} /* namespace net */

0 commit comments

Comments
 (0)