From 66d73b0713fae1287b7e2d74b1a4bd9d4d4c50a6 Mon Sep 17 00:00:00 2001 From: switch_st Date: Tue, 21 Jul 2020 20:47:14 +0800 Subject: [PATCH 1/4] fix supernode coredump when accessing management socket --- include/n2n.h | 2 ++ src/sn_utils.c | 30 +++++++++++++++--------------- 2 files changed, 17 insertions(+), 15 deletions(-) diff --git a/include/n2n.h b/include/n2n.h index e4b3b4a..e0066a6 100644 --- a/include/n2n.h +++ b/include/n2n.h @@ -99,6 +99,8 @@ #include #endif +#define SAFE_SNPRINTF(BUF, SIZE, FORMAT, ...) (ssize_t)(SIZE) > 0 ? snprintf(BUF, SIZE, FORMAT, ##__VA_ARGS__) : 0 + #define ETH_ADDR_LEN 6 struct ether_hdr diff --git a/src/sn_utils.c b/src/sn_utils.c index 4ea34f6..c92950a 100644 --- a/src/sn_utils.c +++ b/src/sn_utils.c @@ -302,10 +302,10 @@ static int process_mgmt(n2n_sn_t *sss, traceEvent(TRACE_DEBUG, "process_mgmt"); - ressize += snprintf(resbuf + ressize, N2N_SN_PKTBUF_SIZE - ressize, + ressize += SAFE_SNPRINTF(resbuf + ressize, N2N_SN_PKTBUF_SIZE - ressize, "----------------\n"); - ressize += snprintf(resbuf + ressize, N2N_SN_PKTBUF_SIZE - ressize, + ressize += SAFE_SNPRINTF(resbuf + ressize, N2N_SN_PKTBUF_SIZE - ressize, "uptime %lu\n", (now - sss->start_time)); HASH_ITER(hh, sss->communities, community, tmp) @@ -313,54 +313,54 @@ static int process_mgmt(n2n_sn_t *sss, num_edges += HASH_COUNT(community->edges); } - ressize += snprintf(resbuf + ressize, N2N_SN_PKTBUF_SIZE - ressize, + ressize += SAFE_SNPRINTF(resbuf + ressize, N2N_SN_PKTBUF_SIZE - ressize, "edges %u\n", num_edges); - ressize += snprintf(resbuf + ressize, N2N_SN_PKTBUF_SIZE - ressize, + ressize += SAFE_SNPRINTF(resbuf + ressize, N2N_SN_PKTBUF_SIZE - ressize, "errors %u\n", (unsigned int)sss->stats.errors); - ressize += snprintf(resbuf + ressize, N2N_SN_PKTBUF_SIZE - ressize, + ressize += SAFE_SNPRINTF(resbuf + ressize, N2N_SN_PKTBUF_SIZE - ressize, "reg_sup %u\n", (unsigned int)sss->stats.reg_super); - ressize += snprintf(resbuf + ressize, N2N_SN_PKTBUF_SIZE - ressize, + ressize += SAFE_SNPRINTF(resbuf + ressize, N2N_SN_PKTBUF_SIZE - ressize, "reg_nak %u\n", (unsigned int)sss->stats.reg_super_nak); - ressize += snprintf(resbuf + ressize, N2N_SN_PKTBUF_SIZE - ressize, + ressize += SAFE_SNPRINTF(resbuf + ressize, N2N_SN_PKTBUF_SIZE - ressize, "fwd %u\n", (unsigned int)sss->stats.fwd); - ressize += snprintf(resbuf + ressize, N2N_SN_PKTBUF_SIZE - ressize, + ressize += SAFE_SNPRINTF(resbuf + ressize, N2N_SN_PKTBUF_SIZE - ressize, "broadcast %u\n", (unsigned int)sss->stats.broadcast); - ressize += snprintf(resbuf + ressize, N2N_SN_PKTBUF_SIZE - ressize, + ressize += SAFE_SNPRINTF(resbuf + ressize, N2N_SN_PKTBUF_SIZE - ressize, "last fwd %lu sec ago\n", (long unsigned int)(now - sss->stats.last_fwd)); - ressize += snprintf(resbuf + ressize, N2N_SN_PKTBUF_SIZE - ressize, + ressize += SAFE_SNPRINTF(resbuf + ressize, N2N_SN_PKTBUF_SIZE - ressize, "last reg %lu sec ago\n", (long unsigned int)(now - sss->stats.last_reg_super)); - ressize += snprintf(resbuf+ressize, N2N_SN_PKTBUF_SIZE-ressize, + ressize += SAFE_SNPRINTF(resbuf+ressize, N2N_SN_PKTBUF_SIZE-ressize, "cur_cmnts"); HASH_ITER(hh, sss->communities, community, tmp) { - ressize += snprintf(resbuf+ressize, N2N_SN_PKTBUF_SIZE-ressize, + ressize += SAFE_SNPRINTF(resbuf+ressize, N2N_SN_PKTBUF_SIZE-ressize, " [%s]", community->community); HASH_ITER(hh, community->edges, peer, tmpPeer) { - ressize += snprintf(resbuf+ressize, N2N_SN_PKTBUF_SIZE-ressize, + ressize += SAFE_SNPRINTF(resbuf+ressize, N2N_SN_PKTBUF_SIZE-ressize, " {%s}", macaddr_str(mac_buf, peer->mac_addr)); } } - ressize += snprintf(resbuf+ressize, N2N_SN_PKTBUF_SIZE-ressize, + ressize += SAFE_SNPRINTF(resbuf+ressize, N2N_SN_PKTBUF_SIZE-ressize, "\n"); - r = sendto(sss->mgmt_sock, resbuf, ressize, 0 /*flags*/, + r = sendto(sss->mgmt_sock, resbuf, MIN(ressize, N2N_SN_PKTBUF_SIZE), 0 /*flags*/, (struct sockaddr *)sender_sock, sizeof(struct sockaddr_in)); if (r <= 0) From 37bc520db2046eed812bd985c3a847f3aa262506 Mon Sep 17 00:00:00 2001 From: switch_st Date: Wed, 22 Jul 2020 11:58:07 +0800 Subject: [PATCH 2/4] fix supernode coredump when accessing management socket --- src/sn_utils.c | 54 +++++++++++++++++++++++++++++++++++--------------- 1 file changed, 38 insertions(+), 16 deletions(-) diff --git a/src/sn_utils.c b/src/sn_utils.c index 4ea34f6..6beedcd 100644 --- a/src/sn_utils.c +++ b/src/sn_utils.c @@ -15,6 +15,11 @@ static ssize_t sendto_sock(n2n_sn_t *sss, const uint8_t *pktbuf, size_t pktsize); +static int sendto_mgmt(n2n_sn_t *sss, + const struct sockaddr_in *sender_sock, + const uint8_t *mgmt_buf, + size_t mgmt_size); + static int try_broadcast(n2n_sn_t * sss, const struct sn_community *comm, const n2n_common_t * cmn, @@ -295,10 +300,11 @@ static int process_mgmt(n2n_sn_t *sss, char resbuf[N2N_SN_PKTBUF_SIZE]; size_t ressize = 0; uint32_t num_edges = 0; - ssize_t r; + uint32_t num = 0; struct sn_community *community, *tmp; struct peer_info * peer, *tmpPeer; macstr_t mac_buf; + n2n_sock_str_t sockbuf; traceEvent(TRACE_DEBUG, "process_mgmt"); @@ -346,30 +352,46 @@ static int process_mgmt(n2n_sn_t *sss, (long unsigned int)(now - sss->stats.last_reg_super)); ressize += snprintf(resbuf+ressize, N2N_SN_PKTBUF_SIZE-ressize, - "cur_cmnts"); + "cur_cmnts %u\n", HASH_COUNT(sss->communities)); HASH_ITER(hh, sss->communities, community, tmp) { - ressize += snprintf(resbuf+ressize, N2N_SN_PKTBUF_SIZE-ressize, - " [%s]", - community->community); + ressize += snprintf(resbuf + ressize, N2N_SN_PKTBUF_SIZE - ressize, + "community: %s\n", community->community); + sendto_mgmt(sss, sender_sock, resbuf, ressize); + ressize = 0; + + num = 0; HASH_ITER(hh, community->edges, peer, tmpPeer) { - ressize += snprintf(resbuf+ressize, N2N_SN_PKTBUF_SIZE-ressize, - " {%s}", - macaddr_str(mac_buf, peer->mac_addr)); + ressize += snprintf(resbuf + ressize, N2N_SN_PKTBUF_SIZE - ressize, + "\t[id: %u][MAC: %s][edge: %s][last seen: %lu sec ago]\n", + ++num, macaddr_str(mac_buf, peer->mac_addr), + sock_to_cstr(sockbuf, &(peer->sock)), now-peer->last_seen); + + sendto_mgmt(sss, sender_sock, resbuf, ressize); + ressize = 0; } } + ressize += snprintf(resbuf+ressize, N2N_SN_PKTBUF_SIZE-ressize, "\n"); + sendto_mgmt(sss, sender_sock, resbuf, ressize); - r = sendto(sss->mgmt_sock, resbuf, ressize, 0 /*flags*/, - (struct sockaddr *)sender_sock, sizeof(struct sockaddr_in)); + return 0; +} - if (r <= 0) - { - ++(sss->stats.errors); - traceEvent(TRACE_ERROR, "process_mgmt : sendto failed. %s", strerror(errno)); - } +static int sendto_mgmt(n2n_sn_t *sss, + const struct sockaddr_in *sender_sock, + const uint8_t *mgmt_buf, + size_t mgmt_size) +{ + ssize_t r = sendto(sss->mgmt_sock, mgmt_buf, mgmt_size, 0 /*flags*/, + (struct sockaddr *)sender_sock, sizeof (struct sockaddr_in)); - return 0; + if (r <= 0) { + ++(sss->stats.errors); + traceEvent (TRACE_ERROR, "process_mgmt : sendto failed. %s", strerror (errno)); + return -1; + } + return 0; } /** Examine a datagram and determine what to do with it. From ee46e3e86b7b6272078884c23af7843d676c6dd4 Mon Sep 17 00:00:00 2001 From: switch_st Date: Wed, 22 Jul 2020 16:40:15 +0800 Subject: [PATCH 3/4] Revert "fix supernode coredump when accessing management socket" This reverts commit 66d73b0713fae1287b7e2d74b1a4bd9d4d4c50a6. --- include/n2n.h | 2 -- src/sn_utils.c | 30 +++++++++++++++--------------- 2 files changed, 15 insertions(+), 17 deletions(-) diff --git a/include/n2n.h b/include/n2n.h index e0066a6..e4b3b4a 100644 --- a/include/n2n.h +++ b/include/n2n.h @@ -99,8 +99,6 @@ #include #endif -#define SAFE_SNPRINTF(BUF, SIZE, FORMAT, ...) (ssize_t)(SIZE) > 0 ? snprintf(BUF, SIZE, FORMAT, ##__VA_ARGS__) : 0 - #define ETH_ADDR_LEN 6 struct ether_hdr diff --git a/src/sn_utils.c b/src/sn_utils.c index c92950a..4ea34f6 100644 --- a/src/sn_utils.c +++ b/src/sn_utils.c @@ -302,10 +302,10 @@ static int process_mgmt(n2n_sn_t *sss, traceEvent(TRACE_DEBUG, "process_mgmt"); - ressize += SAFE_SNPRINTF(resbuf + ressize, N2N_SN_PKTBUF_SIZE - ressize, + ressize += snprintf(resbuf + ressize, N2N_SN_PKTBUF_SIZE - ressize, "----------------\n"); - ressize += SAFE_SNPRINTF(resbuf + ressize, N2N_SN_PKTBUF_SIZE - ressize, + ressize += snprintf(resbuf + ressize, N2N_SN_PKTBUF_SIZE - ressize, "uptime %lu\n", (now - sss->start_time)); HASH_ITER(hh, sss->communities, community, tmp) @@ -313,54 +313,54 @@ static int process_mgmt(n2n_sn_t *sss, num_edges += HASH_COUNT(community->edges); } - ressize += SAFE_SNPRINTF(resbuf + ressize, N2N_SN_PKTBUF_SIZE - ressize, + ressize += snprintf(resbuf + ressize, N2N_SN_PKTBUF_SIZE - ressize, "edges %u\n", num_edges); - ressize += SAFE_SNPRINTF(resbuf + ressize, N2N_SN_PKTBUF_SIZE - ressize, + ressize += snprintf(resbuf + ressize, N2N_SN_PKTBUF_SIZE - ressize, "errors %u\n", (unsigned int)sss->stats.errors); - ressize += SAFE_SNPRINTF(resbuf + ressize, N2N_SN_PKTBUF_SIZE - ressize, + ressize += snprintf(resbuf + ressize, N2N_SN_PKTBUF_SIZE - ressize, "reg_sup %u\n", (unsigned int)sss->stats.reg_super); - ressize += SAFE_SNPRINTF(resbuf + ressize, N2N_SN_PKTBUF_SIZE - ressize, + ressize += snprintf(resbuf + ressize, N2N_SN_PKTBUF_SIZE - ressize, "reg_nak %u\n", (unsigned int)sss->stats.reg_super_nak); - ressize += SAFE_SNPRINTF(resbuf + ressize, N2N_SN_PKTBUF_SIZE - ressize, + ressize += snprintf(resbuf + ressize, N2N_SN_PKTBUF_SIZE - ressize, "fwd %u\n", (unsigned int)sss->stats.fwd); - ressize += SAFE_SNPRINTF(resbuf + ressize, N2N_SN_PKTBUF_SIZE - ressize, + ressize += snprintf(resbuf + ressize, N2N_SN_PKTBUF_SIZE - ressize, "broadcast %u\n", (unsigned int)sss->stats.broadcast); - ressize += SAFE_SNPRINTF(resbuf + ressize, N2N_SN_PKTBUF_SIZE - ressize, + ressize += snprintf(resbuf + ressize, N2N_SN_PKTBUF_SIZE - ressize, "last fwd %lu sec ago\n", (long unsigned int)(now - sss->stats.last_fwd)); - ressize += SAFE_SNPRINTF(resbuf + ressize, N2N_SN_PKTBUF_SIZE - ressize, + ressize += snprintf(resbuf + ressize, N2N_SN_PKTBUF_SIZE - ressize, "last reg %lu sec ago\n", (long unsigned int)(now - sss->stats.last_reg_super)); - ressize += SAFE_SNPRINTF(resbuf+ressize, N2N_SN_PKTBUF_SIZE-ressize, + ressize += snprintf(resbuf+ressize, N2N_SN_PKTBUF_SIZE-ressize, "cur_cmnts"); HASH_ITER(hh, sss->communities, community, tmp) { - ressize += SAFE_SNPRINTF(resbuf+ressize, N2N_SN_PKTBUF_SIZE-ressize, + ressize += snprintf(resbuf+ressize, N2N_SN_PKTBUF_SIZE-ressize, " [%s]", community->community); HASH_ITER(hh, community->edges, peer, tmpPeer) { - ressize += SAFE_SNPRINTF(resbuf+ressize, N2N_SN_PKTBUF_SIZE-ressize, + ressize += snprintf(resbuf+ressize, N2N_SN_PKTBUF_SIZE-ressize, " {%s}", macaddr_str(mac_buf, peer->mac_addr)); } } - ressize += SAFE_SNPRINTF(resbuf+ressize, N2N_SN_PKTBUF_SIZE-ressize, + ressize += snprintf(resbuf+ressize, N2N_SN_PKTBUF_SIZE-ressize, "\n"); - r = sendto(sss->mgmt_sock, resbuf, MIN(ressize, N2N_SN_PKTBUF_SIZE), 0 /*flags*/, + r = sendto(sss->mgmt_sock, resbuf, ressize, 0 /*flags*/, (struct sockaddr *)sender_sock, sizeof(struct sockaddr_in)); if (r <= 0) From 4d88fef4e270f6b1016c65cca75783db1cb4480d Mon Sep 17 00:00:00 2001 From: switch_st Date: Wed, 22 Jul 2020 17:18:00 +0800 Subject: [PATCH 4/4] update log word in sendto_mgmt --- src/sn_utils.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/sn_utils.c b/src/sn_utils.c index 6beedcd..e2789d1 100644 --- a/src/sn_utils.c +++ b/src/sn_utils.c @@ -388,7 +388,7 @@ static int sendto_mgmt(n2n_sn_t *sss, if (r <= 0) { ++(sss->stats.errors); - traceEvent (TRACE_ERROR, "process_mgmt : sendto failed. %s", strerror (errno)); + traceEvent (TRACE_ERROR, "sendto_mgmt : sendto failed. %s", strerror (errno)); return -1; } return 0;