Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Purgable fixes #1147

Merged
merged 6 commits into from
Oct 24, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 33 additions & 0 deletions scripts/n2n-ctl
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import argparse
import socket
import json
import collections
import time


class JsonUDP():
Expand Down Expand Up @@ -171,6 +172,28 @@ def str_table(rows, columns, orderby):
return ''.join(result)


def num2timestr(seconds):
"""Convert a number of seconds into a human time"""

if seconds == 0:
return "now"

days, seconds = divmod(seconds, (60*60*24))
hours, seconds = divmod(seconds, (60*60))
minutes, seconds = divmod(seconds, 60)

r = []
if days:
r += [f"{days}d"]
if hours:
r += [f"{hours}h"]
if minutes:
r += [f"{minutes}m"]
if seconds:
r += [f"{seconds}s"]
return "".join(r)


def subcmd_show_supernodes(rpc, args):
rows = rpc.read('supernodes')
columns = [
Expand All @@ -179,8 +202,13 @@ def subcmd_show_supernodes(rpc, args):
'macaddr',
'sockaddr',
'uptime',
'last_seen',
]

now = int(time.time())
for row in rows:
row["last_seen"] = num2timestr(now - row["last_seen"])

return str_table(rows, columns, args.orderby)


Expand All @@ -192,8 +220,13 @@ def subcmd_show_edges(rpc, args):
'macaddr',
'sockaddr',
'desc',
'last_seen',
]

now = int(time.time())
for row in rows:
row["last_seen"] = num2timestr(now - row["last_seen"])

return str_table(rows, columns, args.orderby)


Expand Down
7 changes: 5 additions & 2 deletions src/edge_management.c
Original file line number Diff line number Diff line change
Expand Up @@ -381,7 +381,10 @@ static void handleMgmtJson (mgmt_req_t *req, char *udp_buf, const int recvlen) {
/* we reuse the buffer already on the stack for all our strings */
STRBUF_INIT(buf, udp_buf, N2N_SN_PKTBUF_SIZE);

mgmt_req_init2(req, buf, (char *)&cmdlinebuf);
if(!mgmt_req_init2(req, buf, (char *)&cmdlinebuf)) {
// if anything failed during init
return;
}

if(req->type == N2N_MGMT_SUB) {
int handler;
Expand Down Expand Up @@ -607,7 +610,7 @@ void readFromMgmtSocket (n2n_edge_t *eee) {
msg_len += snprintf((char *) (udp_buf + msg_len), (N2N_PKT_BUF_SIZE - msg_len),
"%-19s %1s%1s | %-17s | %-21s | %-15s | %9s | %10s\n",
peer->version,
(peer->purgeable == false) ? "l" : "",
(peer->purgeable) ? "" : "l",
(peer == eee->curr_sn) ? (eee->sn_wait ? "." : "*" ) : "",
is_null_mac(peer->mac_addr) ? "" : macaddr_str(mac_buf, peer->mac_addr),
sock_to_cstr(sockbuf, &(peer->sock)),
Expand Down
2 changes: 2 additions & 0 deletions src/edge_utils.c
Original file line number Diff line number Diff line change
Expand Up @@ -683,6 +683,7 @@ static void register_with_new_peer (n2n_edge_t *eee,
scan->sock = *peer;
scan->timeout = eee->conf.register_interval; /* TODO: should correspond to the peer supernode registration timeout */
scan->last_valid_time_stamp = initial_time_stamp();
scan->purgeable = true;
if(via_multicast)
scan->local = 1;

Expand Down Expand Up @@ -1903,6 +1904,7 @@ static int check_query_peer_info (n2n_edge_t *eee, time_t now, n2n_mac_t mac) {
scan->timeout = eee->conf.register_interval; /* TODO: should correspond to the peer supernode registration timeout */
scan->last_seen = now; /* Don't change this it marks the pending peer for removal. */
scan->last_valid_time_stamp = initial_time_stamp();
scan->purgeable = true;

HASH_ADD_PEER(eee->pending_peers, scan);
}
Expand Down
14 changes: 8 additions & 6 deletions src/management.c
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,7 @@ int mgmt_auth (mgmt_req_t *req, char *auth) {
/*
* Handle the common and shred parts of the mgmt_req_t initialisation
*/
void mgmt_req_init2 (mgmt_req_t *req, strbuf_t *buf, char *cmdline) {
bool mgmt_req_init2 (mgmt_req_t *req, strbuf_t *buf, char *cmdline) {
char *typechar;
char *options;
char *flagstr;
Expand All @@ -226,7 +226,7 @@ void mgmt_req_init2 (mgmt_req_t *req, strbuf_t *buf, char *cmdline) {
if(!typechar) {
/* should not happen */
mgmt_error(req, buf, "notype");
return;
return false;
}
if(*typechar == 'r') {
req->type=N2N_MGMT_READ;
Expand All @@ -236,20 +236,20 @@ void mgmt_req_init2 (mgmt_req_t *req, strbuf_t *buf, char *cmdline) {
req->type=N2N_MGMT_SUB;
} else {
mgmt_error(req, buf, "badtype");
return;
return false;
}

/* Extract the tag to use in all reply packets */
options = strtok(NULL, " \r\n");
if(!options) {
mgmt_error(req, buf, "nooptions");
return;
return false;
}

req->argv0 = strtok(NULL, " \r\n");
if(!req->argv0) {
mgmt_error(req, buf, "nocmd");
return;
return false;
}

/*
Expand Down Expand Up @@ -281,6 +281,8 @@ void mgmt_req_init2 (mgmt_req_t *req, strbuf_t *buf, char *cmdline) {

if(!mgmt_auth(req, auth)) {
mgmt_error(req, buf, "badauth");
return;
return false;
}

return true;
}
2 changes: 1 addition & 1 deletion src/management.h
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,6 @@ void mgmt_event_post2 (enum n2n_event_topic topic, int data0, void *data1, mgmt_
void mgmt_help_row (mgmt_req_t *req, strbuf_t *buf, char *cmd, char *help);
void mgmt_help_events_row (mgmt_req_t *req, strbuf_t *buf, mgmt_req_t *sub, char *cmd, char *help);
int mgmt_auth (mgmt_req_t *req, char *auth);
void mgmt_req_init2 (mgmt_req_t *req, strbuf_t *buf, char *cmdline);
bool mgmt_req_init2 (mgmt_req_t *req, strbuf_t *buf, char *cmdline);

#endif
3 changes: 2 additions & 1 deletion src/n2n.c
Original file line number Diff line number Diff line change
Expand Up @@ -528,6 +528,7 @@ struct peer_info* add_sn_to_list_by_mac_or_sock (struct peer_info **sn_list, n2n
if(peer) {
sn_selection_criterion_default(&(peer->selection_criterion));
peer->last_valid_time_stamp = initial_time_stamp();
peer->purgeable = true;
memcpy(&(peer->sock), sock, sizeof(n2n_sock_t));
memcpy(peer->mac_addr, mac, sizeof(n2n_mac_t));
HASH_ADD_PEER(*sn_list, peer);
Expand Down Expand Up @@ -687,7 +688,7 @@ size_t clear_peer_list (struct peer_info ** peer_list) {
size_t retval = 0;

HASH_ITER(hh, *peer_list, scan, tmp) {
if (scan->purgeable == false && scan->ip_addr) {
if (!scan->purgeable && scan->ip_addr) {
free(scan->ip_addr);
}
HASH_DEL(*peer_list, scan);
Expand Down
9 changes: 6 additions & 3 deletions src/sn_management.c
Original file line number Diff line number Diff line change
Expand Up @@ -241,7 +241,10 @@ static void handleMgmtJson (mgmt_req_t *req, char *udp_buf, const int recvlen) {
// xx
STRBUF_INIT(buf, udp_buf, N2N_SN_PKTBUF_SIZE);

mgmt_req_init2(req, buf, (char *)&cmdlinebuf);
if(!mgmt_req_init2(req, buf, (char *)&cmdlinebuf)) {
// if anything failed during init
return;
}

int handler;
lookup_handler(handler, mgmt_handlers, req->argv0);
Expand Down Expand Up @@ -371,7 +374,7 @@ int process_mgmt (n2n_sn_t *sss,

ressize += snprintf(resbuf + ressize, N2N_SN_PKTBUF_SIZE - ressize,
"%s '%s'\n",
(community->is_federation) ? "FEDERATION" : ((community->purgeable == false) ? "FIXED NAME COMMUNITY" : "COMMUNITY"),
(community->is_federation) ? "FEDERATION" : ((community->purgeable) ? "COMMUNITY" : "FIXED NAME COMMUNITY"),
(community->is_federation) ? "-/-" : community->community);
sendto_mgmt(sss, sender_sock, sock_size, (const uint8_t *) resbuf, ressize);
ressize = 0;
Expand All @@ -382,7 +385,7 @@ int process_mgmt (n2n_sn_t *sss,
ressize += snprintf(resbuf + ressize, N2N_SN_PKTBUF_SIZE - ressize,
"%4u | %-19s | %-17s | %-21s %-3s | %-15s | %9s\n",
++num,
(peer->dev_addr.net_addr == 0) ? ((peer->purgeable == false) ? "-l" : "") : ip_subnet_to_str(ip_bit_str, &peer->dev_addr),
(peer->dev_addr.net_addr == 0) ? ((peer->purgeable) ? "" : "-l") : ip_subnet_to_str(ip_bit_str, &peer->dev_addr),
(is_null_mac(peer->mac_addr)) ? "" : macaddr_str(mac_buf, peer->mac_addr),
sock_to_cstr(sockbuf, &(peer->sock)),
((peer->socket_fd >= 0) && (peer->socket_fd != sss->sock)) ? "TCP" : "",
Expand Down
2 changes: 1 addition & 1 deletion src/sn_utils.c
Original file line number Diff line number Diff line change
Expand Up @@ -1483,7 +1483,7 @@ static int purge_expired_communities (n2n_sn_t *sss,
}
}

if((comm->edges == NULL) && (comm->purgeable == true)) {
if((comm->edges == NULL) && (comm->purgeable)) {
traceEvent(TRACE_INFO, "purging idle community %s", comm->community);
if(NULL != comm->header_encryption_ctx_static) {
/* this should not happen as 'purgeable' and thus only communities w/o encrypted header here */
Expand Down
Loading