From abfa20401f4dcd49ddfbdf979581052dcad45f9d Mon Sep 17 00:00:00 2001 From: bizhiyuan Date: Thu, 27 Nov 2025 15:44:48 +0800 Subject: [PATCH] Refactor: Unindent various code blocks in raft.c. Refactor: Check the return value of setsockopt Refactor: Remove global booth_conf variable in raft.c. Refactor: Remove some (void) casts from transport.c. Refactor: Use whitespace more consistently in transport.c. Refactor: Remove global booth_conf variable in transport.c. Refactor: Add braces around conditional blocks in transport.c. Refactor: Unindent various code blocks in transport.c. --- ...ces-around-conditional-blocks-in-tra.patch | 324 +++++++++ ...Check-the-return-value-of-setsockopt.patch | 61 ++ ...global-booth_conf-variable-in-raft.c.patch | 157 +++++ ...global-booth_conf-variable-in-transp.patch | 206 ++++++ ...ove-some-void-casts-from-transport.c.patch | 43 ++ ...indent-various-code-blocks-in-raft.c.patch | 236 +++++++ ...t-various-code-blocks-in-transport.c.patch | 182 ++++++ ...tespace-more-consistently-in-transpo.patch | 617 ++++++++++++++++++ booth.spec | 28 +- 9 files changed, 1852 insertions(+), 2 deletions(-) create mode 100644 backport-Refactor-Add-braces-around-conditional-blocks-in-tra.patch create mode 100644 backport-Refactor-Check-the-return-value-of-setsockopt.patch create mode 100644 backport-Refactor-Remove-global-booth_conf-variable-in-raft.c.patch create mode 100644 backport-Refactor-Remove-global-booth_conf-variable-in-transp.patch create mode 100644 backport-Refactor-Remove-some-void-casts-from-transport.c.patch create mode 100644 backport-Refactor-Unindent-various-code-blocks-in-raft.c.patch create mode 100644 backport-Refactor-Unindent-various-code-blocks-in-transport.c.patch create mode 100644 backport-Refactor-Use-whitespace-more-consistently-in-transpo.patch diff --git a/backport-Refactor-Add-braces-around-conditional-blocks-in-tra.patch b/backport-Refactor-Add-braces-around-conditional-blocks-in-tra.patch new file mode 100644 index 0000000..9d55d44 --- /dev/null +++ b/backport-Refactor-Add-braces-around-conditional-blocks-in-tra.patch @@ -0,0 +1,324 @@ +From 6b6d0ba26107dac44644847eca444462e5921e78 Mon Sep 17 00:00:00 2001 +From: Chris Lumens +Date: Fri, 25 Oct 2024 16:22:16 -0400 +Subject: [PATCH] Refactor: Add braces around conditional blocks in + transport.c. + +--- + src/transport.c | 115 ++++++++++++++++++++++++++++++++---------------- + 1 file changed, 77 insertions(+), 38 deletions(-) + +diff --git a/src/transport.c b/src/transport.c +index 7072640..c725897 100644 +--- a/src/transport.c ++++ b/src/transport.c +@@ -67,8 +67,10 @@ static void parse_rtattr(struct rtattr *tb[], + int max, struct rtattr *rta, int len) + { + while (RTA_OK(rta, len)) { +- if (rta->rta_type <= max) ++ if (rta->rta_type <= max) { + tb[rta->rta_type] = rta; ++ } ++ + rta = RTA_NEXT(rta,len); + } + } +@@ -102,13 +104,17 @@ static int find_address(struct booth_config *conf, + mask = ~( (1 << (8 - bits_left)) -1); + + FOREACH_NODE(conf, i, node) { +- if (family != node->family) ++ if (family != node->family) { + continue; ++ } ++ + n_a = node_to_addr_pointer(node); + +- for(matched = 0; matched < node->addrlen; matched++) +- if (ipaddr[matched] != n_a[matched]) ++ for (matched = 0; matched < node->addrlen; matched++) { ++ if (ipaddr[matched] != n_a[matched]) { + break; ++ } ++ } + + if (matched == node->addrlen) { + *address_bits_matched = matched * 8; +@@ -117,15 +123,18 @@ static int find_address(struct booth_config *conf, + break; + } + +- if (!fuzzy_allowed) ++ if (!fuzzy_allowed) { + continue; +- ++ } + + /* Check prefix, whole bytes */ +- if (matched < bytes) ++ if (matched < bytes) { + continue; +- if (matched * 8 < *address_bits_matched) ++ } ++ ++ if (matched * 8 < *address_bits_matched) { + continue; ++ } + + node_bits = n_a[bytes]; + ip_bits = ipaddr[bytes]; +@@ -158,14 +167,15 @@ static int _find_myself(struct booth_config *conf, int family, + int address_bits_matched; + + +- if (local) ++ if (local) { + goto found; +- ++ } + + me = NULL; + address_bits_matched = 0; +- if (mep) ++ if (mep) { + *mep = NULL; ++ } + fd = socket(AF_NETLINK, SOCK_RAW, NETLINK_ROUTE); + if (fd < 0) { + log_error("failed to create netlink socket"); +@@ -215,8 +225,9 @@ static int _find_myself(struct booth_config *conf, int family, + struct rtattr *tb[IFA_MAX+1]; + int len; + +- if (h->nlmsg_type == NLMSG_DONE) ++ if (h->nlmsg_type == NLMSG_DONE) { + goto out; ++ } + + if (h->nlmsg_type == NLMSG_ERROR) { + close(fd); +@@ -288,14 +299,17 @@ static int _find_myself(struct booth_config *conf, int family, + out: + close(fd); + +- if (!me) ++ if (!me) { + return 0; ++ } + + me->local = 1; + local = me; + found: +- if (mep) ++ if (mep) { + *mep = local; ++ } ++ + return 1; + } + +@@ -332,8 +346,9 @@ int check_boothc_header(struct boothc_header *h, int len_incl_data) + } + + +- if (len_incl_data < 0) ++ if (len_incl_data < 0) { + return 0; ++ } + + if (l != len_incl_data) { + log_error("length error - got %d, wanted %d", +@@ -350,14 +365,19 @@ static int do_read(int fd, void *buf, size_t count) + + while (off < count) { + rv = read(fd, (char *)buf + off, count - off); +- if (rv == 0) ++ if (rv == 0) { + return -1; +- if (rv == -1 && errno == EINTR) ++ } ++ if (rv == -1 && errno == EINTR) { + continue; +- if (rv == -1 && errno == EWOULDBLOCK) ++ } ++ if (rv == -1 && errno == EWOULDBLOCK) { + break; +- if (rv == -1) ++ } ++ if (rv == -1) { + return -1; ++ } ++ + off += rv; + } + return off; +@@ -369,8 +389,10 @@ static int do_write(int fd, void *buf, size_t count) + + retry: + rv = send(fd, (char *)buf + off, count, MSG_NOSIGNAL); +- if (rv == -1 && errno == EINTR) ++ if (rv == -1 && errno == EINTR) { + goto retry; ++ } ++ + /* If we cannot write _any_ data, we'd be in an (potential) loop. */ + if (rv <= 0) { + log_error("send failed: %s (%d)", strerror(errno), errno); +@@ -416,8 +438,9 @@ int read_client(struct client *req_cl) + fd = req_cl->fd; + rv = do_read(fd, msg+req_cl->offset, len-req_cl->offset); + if (rv < 0) { +- if (errno == ECONNRESET) ++ if (errno == ECONNRESET) { + log_debug("client connection reset for fd %d", fd); ++ } + return -1; + } + req_cl->offset += rv; +@@ -592,12 +615,14 @@ static int booth_tcp_init(void * unused __attribute__((unused))) + { + int rv; + +- if (get_local_id() < 0) ++ if (get_local_id() < 0) { + return -1; ++ } + + rv = setup_tcp_listener(0); +- if (rv < 0) ++ if (rv < 0) { + return rv; ++ } + + client_add(rv, booth_transport + TCP, + process_tcp_listener, NULL); +@@ -620,12 +645,14 @@ static int connect_nonb(int sockfd, const struct sockaddr *saptr, + } + + error = 0; +- if ( (n = connect(sockfd, saptr, salen)) < 0) +- if (errno != EINPROGRESS) +- return -1; ++ n = connect(sockfd, saptr, salen); ++ if (n < 0 && errno != EINPROGRESS) { ++ return -1; ++ } + +- if (n == 0) ++ if (n == 0) { + goto done; /* connect completed immediately */ ++ } + + FD_ZERO(&rset); + FD_SET(sockfd, &rset); +@@ -643,8 +670,9 @@ static int connect_nonb(int sockfd, const struct sockaddr *saptr, + + if (FD_ISSET(sockfd, &rset) || FD_ISSET(sockfd, &wset)) { + len = sizeof(error); +- if (getsockopt(sockfd, SOL_SOCKET, SO_ERROR, &error, &len) < 0) ++ if (getsockopt(sockfd, SOL_SOCKET, SO_ERROR, &error, &len) < 0) { + return -1; /* Solaris pending error */ ++ } + } else { + log_error("select error: sockfd not set"); + return -1; +@@ -671,8 +699,9 @@ static int booth_tcp_open(struct booth_site *to) + { + int s, rv; + +- if (to->tcp_fd >= STDERR_FILENO) ++ if (to->tcp_fd >= STDERR_FILENO) { + goto found; ++ } + + s = socket(to->family, SOCK_STREAM, 0); + if (s == -1) { +@@ -683,11 +712,12 @@ static int booth_tcp_open(struct booth_site *to) + + rv = connect_nonb(s, (struct sockaddr *)&to->sa6, to->saddrlen, 10); + if (rv == -1) { +- if( errno == ETIMEDOUT) ++ if (errno == ETIMEDOUT) { + log_error("connect to %s got a timeout", site_string(to)); +- else ++ } else { + log_error("connect to %s got an error: %s", site_string(to), + strerror(errno)); ++ } + goto error; + } + +@@ -697,8 +727,10 @@ found: + return 1; + + error: +- if (s >= 0) ++ if (s >= 0) { + close(s); ++ } ++ + return -1; + } + +@@ -783,8 +815,10 @@ static int booth_tcp_recv_auth(struct booth_config *conf, struct booth_site *fro + static int booth_tcp_close(struct booth_site *to) + { + if (to) { +- if (to->tcp_fd > STDERR_FILENO) ++ if (to->tcp_fd > STDERR_FILENO) { + close(to->tcp_fd); ++ } ++ + to->tcp_fd = -1; + } + return 0; +@@ -841,8 +875,10 @@ static int setup_udp_server(void) + return 0; + + ex: +- if (fd >= 0) ++ if (fd >= 0) { + close(fd); ++ } ++ + return -1; + } + +@@ -866,17 +902,19 @@ static void process_recv(struct booth_config *conf, int ci) + buffer, sizeof(buffer), + MSG_NOSIGNAL | MSG_DONTWAIT, + (struct sockaddr *)&sa, &sa_len); +- if (rv == -1) ++ if (rv == -1) { + return; ++ } + + rv = deliver_fn(conf, (void*) msg, rv); + if (rv > 0) { + if (getnameinfo((struct sockaddr *)&sa, sa_len, + buffer, sizeof(buffer), NULL, 0, +- NI_NUMERICHOST) == 0) ++ NI_NUMERICHOST) == 0) { + log_error("unknown sender: %08x (real: %s)", rv, buffer); +- else ++ } else { + log_error("unknown sender: %08x", rv); ++ } + } + } + +@@ -885,8 +923,9 @@ static int booth_udp_init(void *f) + int rv; + + rv = setup_udp_server(); +- if (rv < 0) ++ if (rv < 0) { + return rv; ++ } + + deliver_fn = f; + client_add(local->udp_fd, +-- +2.25.1 + diff --git a/backport-Refactor-Check-the-return-value-of-setsockopt.patch b/backport-Refactor-Check-the-return-value-of-setsockopt.patch new file mode 100644 index 0000000..011bfac --- /dev/null +++ b/backport-Refactor-Check-the-return-value-of-setsockopt.patch @@ -0,0 +1,61 @@ +From 36765017e94a97ece54431e30fdf77d35e381e98 Mon Sep 17 00:00:00 2001 +From: Chris Lumens +Date: Thu, 5 Dec 2024 16:41:32 -0500 +Subject: [PATCH] Refactor: Check the return value of setsockopt. + +--- + src/transport.c | 17 +++++++++++++++-- + 1 file changed, 15 insertions(+), 2 deletions(-) + +diff --git a/src/transport.c b/src/transport.c +index 6344329..1b5844c 100644 +--- a/src/transport.c ++++ b/src/transport.c +@@ -149,6 +149,7 @@ static int find_address(struct booth_config *conf, + static int _find_myself(struct booth_config *conf, int family, + struct booth_site **mep, int fuzzy_allowed) + { ++ int rc; + int fd; + struct sockaddr_nl nladdr; + struct booth_site *me; +@@ -175,7 +176,12 @@ static int _find_myself(struct booth_config *conf, int family, + return 0; + } + +- setsockopt(fd, SOL_SOCKET, SO_RCVBUF, &rcvbuf, sizeof(rcvbuf)); ++ rc = setsockopt(fd, SOL_SOCKET, SO_RCVBUF, &rcvbuf, sizeof(rcvbuf)); ++ if (rc == -1) { ++ log_error("setsockopt error %d %d", fd, errno); ++ close(fd); ++ return 0; ++ } + + memset(&nladdr, 0, sizeof(nladdr)); + nladdr.nl_family = AF_NETLINK; +@@ -530,6 +536,7 @@ kill: + + static void process_tcp_listener(struct booth_config *conf, int ci) + { ++ int rc; + int fd, i, flags, one = 1; + socklen_t addrlen = sizeof(struct sockaddr); + struct sockaddr addr; +@@ -539,7 +546,13 @@ static void process_tcp_listener(struct booth_config *conf, int ci) + log_error("process_tcp_listener: accept error %d %d", fd, errno); + return; + } +- setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, (char *) &one, sizeof(one)); ++ ++ rc = setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, (char *) &one, sizeof(one)); ++ if (rc == -1) { ++ log_error("process_tcp_listener: setsockopt error %d %d", fd, errno); ++ close(fd); ++ return; ++ } + + flags = fcntl(fd, F_GETFL, 0); + if (fcntl(fd, F_SETFL, flags | O_NONBLOCK) == -1) { +-- +2.25.1 + diff --git a/backport-Refactor-Remove-global-booth_conf-variable-in-raft.c.patch b/backport-Refactor-Remove-global-booth_conf-variable-in-raft.c.patch new file mode 100644 index 0000000..c29616c --- /dev/null +++ b/backport-Refactor-Remove-global-booth_conf-variable-in-raft.c.patch @@ -0,0 +1,157 @@ +From be0eaea6bb5e0942010dcb6a6484d5ea653e4c2e Mon Sep 17 00:00:00 2001 +From: Chris Lumens +Date: Tue, 3 Dec 2024 13:53:08 -0500 +Subject: [PATCH] Refactor: Remove global booth_conf variable in raft.c. + +Co-authored-by: Jan Pokorny +--- + src/raft.c | 52 ++++++++++++++++++++++++++++++---------------------- + 1 file changed, 30 insertions(+), 22 deletions(-) + +diff --git a/src/raft.c b/src/raft.c +index a91e204..71137cd 100644 +--- a/src/raft.c ++++ b/src/raft.c +@@ -33,14 +33,15 @@ + #include "manual.h" + + +-inline static void clear_election(struct ticket_config *tk) ++inline static void clear_election(struct booth_config *conf, ++ struct ticket_config *tk) + { + int i; + struct booth_site *site; + + tk_log_debug("clear election"); + tk->votes_received = 0; +- _FOREACH_NODE(i, site) { ++ FOREACH_NODE(conf, i, site) { + tk->votes_for[site->index] = NULL; + } + } +@@ -155,54 +156,61 @@ static void won_elections(struct booth_config *conf, struct ticket_config *tk) + /* if more than one member got the same (and maximum within that + * election) number of votes, then that is a tie + */ +-static int is_tie(struct ticket_config *tk) ++static int is_tie(struct booth_config *conf, struct ticket_config *tk) + { + int i; + struct booth_site *v; ++ struct booth_site *ignored __attribute__((unused)); + int count[MAX_NODES] = { 0, }; + int max_votes = 0, max_cnt = 0; + +- for (i = 0; i < booth_conf->site_count; i++) { ++ assert(conf != NULL); ++ ++ FOREACH_NODE(conf, i, ignored) { + v = tk->votes_for[i]; +- if (!v) ++ if (!v) { + continue; ++ } + count[v->index]++; + max_votes = max(max_votes, count[v->index]); + } + +- for (i = 0; i < booth_conf->site_count; i++) { +- if (count[i] == max_votes) ++ FOREACH_NODE(conf, i, ignored) { ++ if (count[i] == max_votes) { + max_cnt++; ++ } + } + + return max_cnt > 1; + } + +-static struct booth_site *majority_votes(struct ticket_config *tk) ++static struct booth_site *majority_votes(struct booth_config *conf, ++ struct ticket_config *tk) + { + int i, n; + struct booth_site *v; ++ struct booth_site *node; + int count[MAX_NODES] = { 0, }; + +- for (i = 0; i < booth_conf->site_count; i++) { ++ assert(conf != NULL); ++ ++ FOREACH_NODE(conf, i, node) { + v = tk->votes_for[i]; +- if (!v || v == no_leader) ++ if (!v || v == no_leader) { + continue; ++ } + + n = v->index; + count[n]++; + tk_log_debug("Majority: %d %s wants %d %s => %d", +- i, site_string(&booth_conf->site[i]), +- n, site_string(v), +- count[n]); ++ i, site_string(node), n, site_string(v), count[n]); + +- if (count[n]*2 <= booth_conf->site_count) ++ if (count[n]*2 <= conf->site_count) { + continue; +- ++ } + + tk_log_debug("Majority reached: %d of %d for %s", +- count[n], booth_conf->site_count, +- site_string(v)); ++ count[n], conf->site_count, site_string(v)); + return v; + } + +@@ -220,7 +228,7 @@ void elections_end(struct booth_config *conf, struct ticket_config *tk) + } + + tk->in_election = 0; +- new_leader = majority_votes(tk); ++ new_leader = majority_votes(conf, tk); + if (new_leader == local) { + won_elections(conf, tk); + tk_log_info("granted successfully here"); +@@ -231,7 +239,7 @@ void elections_end(struct booth_config *conf, struct ticket_config *tk) + tk_log_info("nobody won elections, new elections"); + tk->outcome = RLT_MORE; + foreach_tkt_req(conf, tk, notify_client); +- if (!new_election(conf, tk, NULL, is_tie(tk) ? 2 : 0, OR_AGAIN)) { ++ if (!new_election(conf, tk, NULL, is_tie(conf, tk) ? 2 : 0, OR_AGAIN)) { + ticket_activate_timeout(tk); + } + } +@@ -510,7 +518,7 @@ static int process_VOTE_FOR(struct booth_config *conf, struct ticket_config *tk, + } + + if (newer_term(tk, sender, leader, msg, 0)) { +- clear_election(tk); ++ clear_election(conf, tk); + } + + record_vote(tk, sender, leader); +@@ -700,7 +708,7 @@ static int answer_REQ_VOTE(struct booth_config *conf, struct ticket_config *tk, + + /* if it's a newer term or ... */ + if (newer_term(tk, sender, leader, msg, 1)) { +- clear_election(tk); ++ clear_election(conf, tk); + goto vote_for_sender; + } + +@@ -779,7 +787,7 @@ int new_election(struct booth_config *conf, struct ticket_config *tk, + + tk_log_info("starting new election (term=%d)", + tk->current_term); +- clear_election(tk); ++ clear_election(conf, tk); + + new_leader = preference ? preference : local; + record_vote(tk, local, new_leader); +-- +2.25.1 + diff --git a/backport-Refactor-Remove-global-booth_conf-variable-in-transp.patch b/backport-Refactor-Remove-global-booth_conf-variable-in-transp.patch new file mode 100644 index 0000000..bcd4286 --- /dev/null +++ b/backport-Refactor-Remove-global-booth_conf-variable-in-transp.patch @@ -0,0 +1,206 @@ +From 05c261e6fcd5d6a6dc573b9760840923726caf04 Mon Sep 17 00:00:00 2001 +From: Chris Lumens +Date: Fri, 25 Oct 2024 16:05:30 -0400 +Subject: [PATCH] Refactor: Remove global booth_conf variable in transport.c. + +Co-authored-by: Jan Pokorny +--- + src/attr.c | 2 +- + src/main.c | 4 ++-- + src/transport.c | 41 ++++++++++++++++++++++++++--------------- + src/transport.h | 16 ++++++++++++++-- + 4 files changed, 43 insertions(+), 20 deletions(-) + +diff --git a/src/attr.c b/src/attr.c +index 3faa888..0c68777 100644 +--- a/src/attr.c ++++ b/src/attr.c +@@ -211,7 +211,7 @@ int do_attr_command(struct booth_config *conf, cmd_request_t cmd) + goto out_close; + } + +- if (check_auth(site, msg, len)) { ++ if (check_auth(conf, site, msg, len)) { + log_error("%s failed to authenticate", site_string(site)); + rv = -1; + goto out_close; +diff --git a/src/main.c b/src/main.c +index cd69322..0114cf5 100644 +--- a/src/main.c ++++ b/src/main.c +@@ -725,7 +725,7 @@ static int query_get_string_answer(cmd_request_t cmd) + if (rv < 0) + goto out_close; + +- rv = tpt->recv_auth(site, &reply, sizeof(reply)); ++ rv = tpt->recv_auth(booth_conf, site, &reply, sizeof(reply)); + if (rv < 0) + goto out_close; + +@@ -828,7 +828,7 @@ redirect: + } + + read_more: +- rv = tpt->recv_auth(site, &reply, sizeof(reply)); ++ rv = tpt->recv_auth(booth_conf, site, &reply, sizeof(reply)); + if (rv < 0) { + /* print any errors depending on the code sent by the + * server */ +diff --git a/src/transport.c b/src/transport.c +index 63f3acc..6752e8c 100644 +--- a/src/transport.c ++++ b/src/transport.c +@@ -455,7 +455,7 @@ static void process_connection(struct booth_config *conf, int ci) + } + + header = (struct boothc_header *)msg; +- if (check_auth(NULL, msg, ntohl(header->length))) { ++ if (check_auth(conf, NULL, msg, ntohl(header->length))) { + errc = RLT_AUTH; + goto send_err; + } +@@ -747,7 +747,8 @@ static int booth_tcp_recv(struct booth_site *from, void *buf, int len) + return got; + } + +-static int booth_tcp_recv_auth(struct booth_site *from, void *buf, int len) ++static int booth_tcp_recv_auth(struct booth_config *conf, struct booth_site *from, ++ void *buf, int len) + { + int got, total; + int payload_len; +@@ -760,10 +761,13 @@ static int booth_tcp_recv_auth(struct booth_site *from, void *buf, int len) + } + total = got; + if (is_auth_req()) { +- got = booth_tcp_recv(from, (unsigned char *)buf+payload_len, sizeof(struct hmac)); +- if (got != sizeof(struct hmac) || check_auth(from, buf, len)) { ++ got = booth_tcp_recv(from, (unsigned char *)buf+payload_len, ++ sizeof(struct hmac)); ++ ++ if (got != sizeof(struct hmac) || check_auth(conf, from, buf, len)) { + return -1; + } ++ + total += got; + } + return total; +@@ -1024,11 +1028,12 @@ const struct booth_transport booth_transport[TRANSPORT_ENTRIES] = { + /* verify the validity of timestamp from the header + * the timestamp needs to be either greater than the one already + * recorded for the site or, and this is checked for clients, +- * not to be older than booth_conf->maxtimeskew ++ * not to be older than conf->maxtimeskew + * update the timestamp for the site, if this packet is from a + * site + */ +-static int verify_ts(struct booth_site *from, void *buf, int len) ++static int verify_ts(struct booth_config *conf, struct booth_site *from, ++ void *buf, int len) + { + struct boothc_header *h; + struct timeval tv, curr_tv, now; +@@ -1044,18 +1049,22 @@ static int verify_ts(struct booth_site *from, void *buf, int len) + if (from) { + curr_tv.tv_sec = from->last_secs; + curr_tv.tv_usec = from->last_usecs; +- if (timercmp(&tv, &curr_tv, >)) ++ if (timercmp(&tv, &curr_tv, >)) { + goto accept; ++ } ++ + log_warn("%s: packet timestamp older than previous one", + site_string(from)); + } + + gettimeofday(&now, NULL); +- now.tv_sec -= booth_conf->maxtimeskew; +- if (timercmp(&tv, &now, >)) ++ now.tv_sec -= conf->maxtimeskew; ++ if (timercmp(&tv, &now, >)) { + goto accept; ++ } ++ + log_error("%s: packet timestamp older than %d seconds", +- peer_string(from), booth_conf->maxtimeskew); ++ peer_string(from), conf->maxtimeskew); + return -1; + + accept: +@@ -1067,15 +1076,17 @@ accept: + } + #endif + +-int check_auth(struct booth_site *from, void *buf, int len) ++int check_auth(struct booth_config *conf, struct booth_site *from, void *buf, ++ int len) + { + int rv = 0; + #if HAVE_LIBGNUTLS || HAVE_LIBGCRYPT || HAVE_LIBMHASH + int payload_len; + struct hmac *hp; + +- if (!is_auth_req()) ++ if (!is_auth_req()) { + return 0; ++ } + + payload_len = len - sizeof(struct hmac); + if (payload_len < 0) { +@@ -1085,9 +1096,9 @@ int check_auth(struct booth_site *from, void *buf, int len) + } + hp = (struct hmac *)((unsigned char *)buf + payload_len); + rv = verify_hmac(buf, payload_len, ntohl(hp->hid), hp->hash, +- booth_conf->authkey, booth_conf->authkey_len); ++ conf->authkey, conf->authkey_len); + if (!rv) { +- rv = verify_ts(from, buf, len); ++ rv = verify_ts(conf, from, buf, len); + } + if (rv != 0) { + log_error("%s: failed to authenticate", peer_string(from)); +@@ -1148,7 +1159,7 @@ int message_recv(struct booth_config *conf, void *msg, int msglen) + return -1; + } + +- if (check_auth(source, msg, msglen)) { ++ if (check_auth(conf, source, msg, msglen)) { + log_error("%s failed to authenticate", site_string(source)); + source->sec_cnt++; + return -1; +diff --git a/src/transport.h b/src/transport.h +index 2784ce4..ce108e9 100644 +--- a/src/transport.h ++++ b/src/transport.h +@@ -50,7 +50,7 @@ struct booth_transport { + int (*send) (struct booth_config *, struct booth_site *, void *, int); + int (*send_auth) (struct booth_config *, struct booth_site *, void *, int); + int (*recv) (struct booth_site *, void *, int); +- int (*recv_auth) (struct booth_site *, void *, int); ++ int (*recv_auth) (struct booth_config *, struct booth_site *, void *, int); + int (*broadcast) (void *, int); + int (*broadcast_auth) (struct booth_config *, void *, int); + int (*close) (struct booth_site *); +@@ -141,6 +141,18 @@ int send_header_plus(struct booth_config *conf, int fd, + + #define send_client_msg(conf, fd, msg) send_data(conf, fd, msg, sendmsglen(msg)) + +-int check_auth(struct booth_site *from, void *buf, int len); ++/** ++ * @internal ++ * First stage of incoming datagram handling (authentication) ++ * ++ * @param[in,out] conf config object to refer to ++ * @param[in] from site structure of the sender ++ * @param[in] buf message to check ++ * @param[in] len length of @buf ++ * ++ * @return see #send_data and #do_write ++ */ ++int check_auth(struct booth_config *conf, struct booth_site *from, void *buf, ++ int len); + + #endif /* _TRANSPORT_H */ +-- +2.25.1 + diff --git a/backport-Refactor-Remove-some-void-casts-from-transport.c.patch b/backport-Refactor-Remove-some-void-casts-from-transport.c.patch new file mode 100644 index 0000000..41d6296 --- /dev/null +++ b/backport-Refactor-Remove-some-void-casts-from-transport.c.patch @@ -0,0 +1,43 @@ +From 8bfffbd9f83627d85a8a2728245619a01bbed9da Mon Sep 17 00:00:00 2001 +From: Chris Lumens +Date: Fri, 25 Oct 2024 16:22:51 -0400 +Subject: [PATCH] Refactor: Remove some (void) casts from transport.c. + +These aren't necessary, and we don't use them consistently anyway so get +rid of the few that are still around. +--- + src/transport.c | 6 +++--- + 1 file changed, 3 insertions(+), 3 deletions(-) + +diff --git a/src/transport.c b/src/transport.c +index c725897..13a73cb 100644 +--- a/src/transport.c ++++ b/src/transport.c +@@ -182,7 +182,7 @@ static int _find_myself(struct booth_config *conf, int family, + return 0; + } + +- (void)setsockopt(fd, SOL_SOCKET, SO_RCVBUF, &rcvbuf, sizeof(rcvbuf)); ++ setsockopt(fd, SOL_SOCKET, SO_RCVBUF, &rcvbuf, sizeof(rcvbuf)); + + memset(&nladdr, 0, sizeof(nladdr)); + nladdr.nl_family = AF_NETLINK; +@@ -554,13 +554,13 @@ static void process_tcp_listener(struct booth_config *conf, int ci) + fd, errno); + return; + } +- (void)setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, (char *)&one, sizeof(one)); ++ setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, (char *)&one, sizeof(one)); + + flags = fcntl(fd, F_GETFL, 0); + if (fcntl(fd, F_SETFL, flags | O_NONBLOCK) == -1) { + log_error("process_tcp_listener: fcntl O_NONBLOCK error %d %d", + fd, errno); +- (void)close(fd); ++ close(fd); + return; + } + +-- +2.25.1 + diff --git a/backport-Refactor-Unindent-various-code-blocks-in-raft.c.patch b/backport-Refactor-Unindent-various-code-blocks-in-raft.c.patch new file mode 100644 index 0000000..36e4fda --- /dev/null +++ b/backport-Refactor-Unindent-various-code-blocks-in-raft.c.patch @@ -0,0 +1,236 @@ +From 527c5811c8d0866bd1a82fba4cd63d7832cca265 Mon Sep 17 00:00:00 2001 +From: Chris Lumens +Date: Tue, 3 Dec 2024 14:02:30 -0500 +Subject: [PATCH] Refactor: Unindent various code blocks in raft.c. + +When we have a condition with a bunch of code under it, these can often +be rewritten to invert the condition and exit which allows all the +previously indented code to be un-indented. This can make the resulting +code easier to follow. + +Additionally, we can unindent some else blocks if every path before them +included a return. +--- + src/raft.c | 147 ++++++++++++++++++++++++++--------------------------- + 1 file changed, 72 insertions(+), 75 deletions(-) + +diff --git a/src/raft.c b/src/raft.c +index 71137cd..af81279 100644 +--- a/src/raft.c ++++ b/src/raft.c +@@ -58,13 +58,12 @@ inline static void record_vote(struct ticket_config *tk, + if (!tk->votes_for[who->index]) { + tk->votes_for[who->index] = vote; + tk->votes_received |= who->bitmask; +- } else { +- if (tk->votes_for[who->index] != vote) +- tk_log_warn("%s voted previously " +- "for %s and now wants to vote for %s (ignored)", +- site_string(who), +- site_string(tk->votes_for[who->index]), +- site_string(vote)); ++ } else if (tk->votes_for[who->index] != vote) { ++ tk_log_warn("%s voted previously " ++ "for %s and now wants to vote for %s (ignored)", ++ site_string(who), ++ site_string(tk->votes_for[who->index]), ++ site_string(vote)); + } + } + +@@ -260,25 +259,25 @@ static int newer_term(struct ticket_config *tk, + + term = ntohl(msg->ticket.term); + /* §5.1 */ +- if (term > tk->current_term) { +- set_state(tk, ST_FOLLOWER); +- if (!in_election) { +- set_leader(tk, leader); +- tk_log_info("from %s: higher term %d vs. %d, following %s", +- site_string(sender), +- term, tk->current_term, +- ticket_leader_string(tk)); +- } else { +- tk_log_debug("from %s: higher term %d vs. %d (election)", +- site_string(sender), +- term, tk->current_term); +- } ++ if (term <= tk->current_term) { ++ return 0; ++ } + +- tk->current_term = term; +- return 1; ++ set_state(tk, ST_FOLLOWER); ++ if (!in_election) { ++ set_leader(tk, leader); ++ tk_log_info("from %s: higher term %d vs. %d, following %s", ++ site_string(sender), ++ term, tk->current_term, ++ ticket_leader_string(tk)); ++ } else { ++ tk_log_debug("from %s: higher term %d vs. %d (election)", ++ site_string(sender), ++ term, tk->current_term); + } + +- return 0; ++ tk->current_term = term; ++ return 1; + } + + static int msg_term_invalid(struct ticket_config *tk, +@@ -290,13 +289,13 @@ static int msg_term_invalid(struct ticket_config *tk, + + term = ntohl(msg->ticket.term); + /* §5.1 */ +- if (is_term_invalid(tk, term)) { +- tk_log_info("got invalid term from %s " +- "(%d), ignoring", site_string(sender), term); +- return 1; ++ if (!is_term_invalid(tk, term)) { ++ return 0; + } + +- return 0; ++ tk_log_info("got invalid term from %s " ++ "(%d), ignoring", site_string(sender), term); ++ return 1; + } + + static int term_too_low(struct booth_config *conf, struct ticket_config *tk, +@@ -307,16 +306,16 @@ static int term_too_low(struct booth_config *conf, struct ticket_config *tk, + + term = ntohl(msg->ticket.term); + /* §5.1 */ +- if (term < tk->current_term) { +- tk_log_info("sending reject to %s, its term too low " +- "(%d vs. %d)", site_string(sender), +- term, tk->current_term +- ); +- send_reject(conf, sender, tk, RLT_TERM_OUTDATED, msg); +- return 1; ++ if (term >= tk->current_term) { ++ return 0; + } + +- return 0; ++ tk_log_info("sending reject to %s, its term too low " ++ "(%d vs. %d)", site_string(sender), ++ term, tk->current_term ++ ); ++ send_reject(conf, sender, tk, RLT_TERM_OUTDATED, msg); ++ return 1; + } + + +@@ -405,13 +404,12 @@ static int process_REVOKE(struct booth_config *conf, struct ticket_config *tk, + "but it is not granted there (ignoring)", + site_string(sender)); + return -1; +- } else { +- rv = process_REVOKE_for_manual_ticket(conf, tk, sender, msg); +- +- // Ticket data stored in this site is not modified. This means +- // that this site will still follow another leader (the one which +- // has not been revoked) or be a leader itself. + } ++ ++ // Ticket data stored in this site is not modified. This means ++ // that this site will still follow another leader (the one which ++ // has not been revoked) or be a leader itself. ++ rv = process_REVOKE_for_manual_ticket(conf, tk, sender, msg); + } else if (tk->state != ST_FOLLOWER) { + tk_log_error("unexpected ticket revoke from %s " + "(in state %s) (ignoring)", +@@ -468,14 +466,12 @@ static int process_ACK(struct booth_config *conf, struct ticket_config *tk, + req = ntohl(msg->header.request); + if ((req == OP_UPDATE || req == OP_HEARTBEAT) && + term == tk->current_term && +- leader == tk->leader) { +- +- if (majority_of_bits(tk, tk->acks_received)) { +- /* OK, at least half of the nodes are reachable; +- * Update the ticket and send update messages out +- */ +- return leader_update_ticket(conf, tk); +- } ++ leader == tk->leader && ++ majority_of_bits(tk, tk->acks_received)) { ++ /* OK, at least half of the nodes are reachable; ++ * Update the ticket and send update messages out ++ */ ++ return leader_update_ticket(conf, tk); + } + + return 0; +@@ -645,23 +641,26 @@ static int test_reason( + int reason; + + reason = ntohl(msg->header.reason); +- if (reason == OR_TKT_LOST) { +- if (tk->state == ST_INIT && +- tk->leader == no_leader) { +- tk_log_warn("%s claims that the ticket is lost, " +- "but it's in %s state (reject sent)", +- site_string(sender), +- state_to_string(tk->state) +- ); +- return RLT_YOU_OUTDATED; +- } +- if (ticket_seems_ok(tk)) { +- tk_log_warn("%s claims that the ticket is lost, " +- "but it is ok here (reject sent)", +- site_string(sender)); +- return RLT_TERM_STILL_VALID; +- } ++ if (reason != OR_TKT_LOST) { ++ return 0; ++ } ++ ++ if (tk->state == ST_INIT && ++ tk->leader == no_leader) { ++ tk_log_warn("%s claims that the ticket is lost, " ++ "but it's in %s state (reject sent)", ++ site_string(sender), ++ state_to_string(tk->state) ++ ); ++ return RLT_YOU_OUTDATED; ++ } ++ if (ticket_seems_ok(tk)) { ++ tk_log_warn("%s claims that the ticket is lost, " ++ "but it is ok here (reject sent)", ++ site_string(sender)); ++ return RLT_TERM_STILL_VALID; + } ++ + return 0; + } + +@@ -883,16 +882,14 @@ static int process_MY_INDEX(struct booth_config *conf, struct ticket_config *tk, + site_string(leader) + ); + return leader_handle_newer_ticket(tk, sender, leader, msg); +- } else { +- /* we have the ticket and we don't care */ +- return 0; +- } +- } else if (tk->state == ST_CANDIDATE) { +- if (leader == local) { +- /* a belated MY_INDEX, we're already trying to get the +- * ticket */ +- return 0; + } ++ ++ /* we have the ticket and we don't care */ ++ return 0; ++ } else if (tk->state == ST_CANDIDATE && leader == local) { ++ /* a belated MY_INDEX, we're already trying to get the ++ * ticket */ ++ return 0; + } + + /* their ticket is either newer or not expired, don't +-- +2.25.1 + diff --git a/backport-Refactor-Unindent-various-code-blocks-in-transport.c.patch b/backport-Refactor-Unindent-various-code-blocks-in-transport.c.patch new file mode 100644 index 0000000..43bb663 --- /dev/null +++ b/backport-Refactor-Unindent-various-code-blocks-in-transport.c.patch @@ -0,0 +1,182 @@ +From fd679976bb8fc1f550c9b7bd6641144b74c39e84 Mon Sep 17 00:00:00 2001 +From: Chris Lumens +Date: Fri, 25 Oct 2024 16:16:02 -0400 +Subject: [PATCH] Refactor: Unindent various code blocks in transport.c. + +When we have a condition with a bunch of code under it, these can often +be rewritten to invert the condition and exit which allows all the +previously indented code to be un-indented. This can make the resulting +code easier to follow. +--- + src/transport.c | 125 ++++++++++++++++++++++++++---------------------- + 1 file changed, 67 insertions(+), 58 deletions(-) + +diff --git a/src/transport.c b/src/transport.c +index 6752e8c..7072640 100644 +--- a/src/transport.c ++++ b/src/transport.c +@@ -20,6 +20,7 @@ + #include "b_config.h" + + #include ++#include + #include + #include + #include +@@ -191,7 +192,7 @@ static int _find_myself(struct booth_config *conf, int family, + return 0; + } + +- while (1) { ++ while (true) { + int status; + struct nlmsghdr *h; + struct iovec iov = { rcvbuf, sizeof(rcvbuf) }; +@@ -210,6 +211,10 @@ static int _find_myself(struct booth_config *conf, int family, + } + + for (h = (struct nlmsghdr *)rcvbuf; NLMSG_OK(h, status); h = NLMSG_NEXT(h, status)) { ++ struct ifaddrmsg *ifa = NULL; ++ struct rtattr *tb[IFA_MAX+1]; ++ int len; ++ + if (h->nlmsg_type == NLMSG_DONE) + goto out; + +@@ -219,60 +224,62 @@ static int _find_myself(struct booth_config *conf, int family, + return 0; + } + +- if (h->nlmsg_type == RTM_NEWADDR) { +- struct ifaddrmsg *ifa = NLMSG_DATA(h); +- struct rtattr *tb[IFA_MAX+1]; +- int len = h->nlmsg_len +- - NLMSG_LENGTH(sizeof(*ifa)); +- +- memset(tb, 0, sizeof(tb)); +- parse_rtattr(tb, IFA_MAX, IFA_RTA(ifa), len); +- memset(ipaddr, 0, BOOTH_IPADDR_LEN); +- /* prefer IFA_LOCAL if it exists, for p-t-p +- * interfaces, otherwise use IFA_ADDRESS */ +- if (tb[IFA_LOCAL]) { +- memcpy(ipaddr, RTA_DATA(tb[IFA_LOCAL]), +- BOOTH_IPADDR_LEN); +- } else if (tb[IFA_ADDRESS]) { +- memcpy(ipaddr, RTA_DATA(tb[IFA_ADDRESS]), +- BOOTH_IPADDR_LEN); +- } else { +- log_error("failed to copy netlink addr"); +- close(fd); +- return 0; +- } ++ if (h->nlmsg_type != RTM_NEWADDR) { ++ continue; ++ } ++ ++ ifa = NLMSG_DATA(h); ++ len = h->nlmsg_len - NLMSG_LENGTH(sizeof(*ifa)); ++ ++ memset(tb, 0, sizeof(tb)); ++ parse_rtattr(tb, IFA_MAX, IFA_RTA(ifa), len); ++ memset(ipaddr, 0, BOOTH_IPADDR_LEN); ++ /* prefer IFA_LOCAL if it exists, for p-t-p ++ * interfaces, otherwise use IFA_ADDRESS */ ++ if (tb[IFA_LOCAL]) { ++ memcpy(ipaddr, RTA_DATA(tb[IFA_LOCAL]), ++ BOOTH_IPADDR_LEN); ++ } else if (tb[IFA_ADDRESS]) { ++ memcpy(ipaddr, RTA_DATA(tb[IFA_ADDRESS]), ++ BOOTH_IPADDR_LEN); ++ } else { ++ log_error("failed to copy netlink addr"); ++ close(fd); ++ return 0; ++ } + +- /* Try to find the exact address or the address with subnet matching. +- * The function find_address will be called for each address received +- * from NLMSG_DATA above. +- * The exact match will be preferred. If no exact match is found, +- * the function find_address will try to return another, most similar +- * address (with the longest possible number of same bytes). */ +- if (ifa->ifa_prefixlen > address_bits_matched) { +- find_address(conf, ipaddr, +- ifa->ifa_family, ifa->ifa_prefixlen, +- fuzzy_allowed, &me, &address_bits_matched); +- +- if (me) { +- log_debug("found myself at %s (%d bits matched)", +- site_string(me), address_bits_matched); +- } ++ /* Try to find the exact address or the address with subnet matching. ++ * The function find_address will be called for each address received ++ * from NLMSG_DATA above. ++ * The exact match will be preferred. If no exact match is found, ++ * the function find_address will try to return another, most similar ++ * address (with the longest possible number of same bytes). ++ */ ++ if (ifa->ifa_prefixlen > address_bits_matched) { ++ find_address(conf, ipaddr, ifa->ifa_family, ++ ifa->ifa_prefixlen, fuzzy_allowed, ++ &me, &address_bits_matched); ++ ++ if (me) { ++ log_debug("found myself at %s (%d bits matched)", ++ site_string(me), address_bits_matched); + } +- /* If the previous NLMSG_DATA calls have already allowed us +- * to find an address with address_bits_matched matching bits, +- * then no other better non-exact address can be found. +- * But we can still try to find an exact match, so let us +- * call the function find_address with disabled searching of +- * similar addresses (fuzzy_allowed == 0) */ +- else if (ifa->ifa_prefixlen == address_bits_matched) { +- find_address(conf, ipaddr, +- ifa->ifa_family, ifa->ifa_prefixlen, +- 0 /* fuzzy_allowed */, &me, &address_bits_matched); +- +- if (me) { +- log_debug("found myself at %s (exact match)", +- site_string(me)); +- } ++ } ++ /* If the previous NLMSG_DATA calls have already allowed us ++ * to find an address with address_bits_matched matching bits, ++ * then no other better non-exact address can be found. ++ * But we can still try to find an exact match, so let us ++ * call the function find_address with disabled searching of ++ * similar addresses (fuzzy_allowed == 0) ++ */ ++ else if (ifa->ifa_prefixlen == address_bits_matched) { ++ find_address(conf, ipaddr, ifa->ifa_family, ++ ifa->ifa_prefixlen, 0 /* fuzzy_allowed */, ++ &me, &address_bits_matched); ++ ++ if (me) { ++ log_debug("found myself at %s (exact match)", ++ site_string(me)); + } + } + } +@@ -944,11 +951,13 @@ static int booth_udp_broadcast_auth(struct booth_config *conf, void *buf, int le + + rvs = 0; + FOREACH_NODE(conf, i, site) { +- if (site != local) { +- rv = booth_udp_send(conf, site, buf, len); +- if (!rvs) { +- rvs = rv; +- } ++ if (site == local) { ++ continue; ++ } ++ ++ rv = booth_udp_send(conf, site, buf, len); ++ if (!rvs) { ++ rvs = rv; + } + } + +-- +2.25.1 + diff --git a/backport-Refactor-Use-whitespace-more-consistently-in-transpo.patch b/backport-Refactor-Use-whitespace-more-consistently-in-transpo.patch new file mode 100644 index 0000000..8f6f5af --- /dev/null +++ b/backport-Refactor-Use-whitespace-more-consistently-in-transpo.patch @@ -0,0 +1,617 @@ +From 8842fb9dc7f59d14f930f6e2566f7a0c8c076685 Mon Sep 17 00:00:00 2001 +From: Chris Lumens +Date: Fri, 25 Oct 2024 16:41:33 -0400 +Subject: [PATCH] Refactor: Use whitespace more consistently in transport.c. + +* Get rid of whitespace at the end of lines. + +* Add or remove whitespace as appropriate between functions, blocks, and + around casts. + +* Combine short lines into longer ones were possible. + +* Get rid of an unnecessary return at the end of a void function. +--- + src/transport.c | 197 +++++++++++++++++++++--------------------------- + 1 file changed, 85 insertions(+), 112 deletions(-) + +diff --git a/src/transport.c b/src/transport.c +index 13a73cb..6344329 100644 +--- a/src/transport.c ++++ b/src/transport.c +@@ -1,17 +1,17 @@ +-/* ++/* + * Copyright (C) 2011 Jiaju Zhang + * Copyright (C) 2013-2014 Philipp Marek +- * ++ * + * 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 2 of the License, or (at your option) any later version. +- * ++ * + * This software 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, write to the Free Software Foundation, Inc., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. +@@ -50,8 +50,6 @@ + #define SOCKET_BUFFER_SIZE 160000 + #define FRAME_SIZE_MAX 10000 + +- +- + struct booth_site *local = NULL; + + /* function to be called when handling booth-group-internal messages; +@@ -62,7 +60,6 @@ struct booth_site *local = NULL; + * address if this is available */ + static int (*deliver_fn) (struct booth_config *conf, void *msg, int msglen); + +- + static void parse_rtattr(struct rtattr *tb[], + int max, struct rtattr *rta, int len) + { +@@ -82,10 +79,8 @@ enum match_type { + }; + + static int find_address(struct booth_config *conf, +- unsigned char ipaddr[BOOTH_IPADDR_LEN], +- int family, int prefixlen, +- int fuzzy_allowed, +- struct booth_site **me, ++ unsigned char ipaddr[BOOTH_IPADDR_LEN], int family, ++ int prefixlen, int fuzzy_allowed, struct booth_site **me, + int *address_bits_matched) + { + int i; +@@ -151,7 +146,6 @@ static int find_address(struct booth_config *conf, + return did_match; + } + +- + static int _find_myself(struct booth_config *conf, int family, + struct booth_site **mep, int fuzzy_allowed) + { +@@ -166,7 +160,6 @@ static int _find_myself(struct booth_config *conf, int family, + } req; + int address_bits_matched; + +- + if (local) { + goto found; + } +@@ -195,8 +188,8 @@ static int _find_myself(struct booth_config *conf, int family, + req.nlh.nlmsg_seq = 1; + req.g.rtgen_family = family; + +- if (sendto(fd, (void *)&req, sizeof(req), 0, +- (struct sockaddr*)&nladdr, sizeof(nladdr)) < 0) { ++ if (sendto(fd, (void *) &req, sizeof(req), 0, ++ (struct sockaddr*) &nladdr, sizeof(nladdr)) < 0) { + close(fd); + log_error("failed to send data to netlink socket"); + return 0; +@@ -206,12 +199,8 @@ static int _find_myself(struct booth_config *conf, int family, + int status; + struct nlmsghdr *h; + struct iovec iov = { rcvbuf, sizeof(rcvbuf) }; +- struct msghdr msg = { +- (void *)&nladdr, sizeof(nladdr), +- &iov, 1, +- NULL, 0, +- 0 +- }; ++ struct msghdr msg = { (void *) &nladdr, sizeof(nladdr), &iov, ++ 1, NULL, 0, 0 }; + + status = recvmsg(fd, &msg, 0); + if (!status) { +@@ -220,7 +209,7 @@ static int _find_myself(struct booth_config *conf, int family, + return 0; + } + +- for (h = (struct nlmsghdr *)rcvbuf; NLMSG_OK(h, status); h = NLMSG_NEXT(h, status)) { ++ for (h = (struct nlmsghdr *) rcvbuf; NLMSG_OK(h, status); h = NLMSG_NEXT(h, status)) { + struct ifaddrmsg *ifa = NULL; + struct rtattr *tb[IFA_MAX+1]; + int len; +@@ -245,8 +234,10 @@ static int _find_myself(struct booth_config *conf, int family, + memset(tb, 0, sizeof(tb)); + parse_rtattr(tb, IFA_MAX, IFA_RTA(ifa), len); + memset(ipaddr, 0, BOOTH_IPADDR_LEN); +- /* prefer IFA_LOCAL if it exists, for p-t-p +- * interfaces, otherwise use IFA_ADDRESS */ ++ ++ /* prefer IFA_LOCAL if it exists, for p-t-p interfaces, ++ * otherwise use IFA_ADDRESS ++ */ + if (tb[IFA_LOCAL]) { + memcpy(ipaddr, RTA_DATA(tb[IFA_LOCAL]), + BOOTH_IPADDR_LEN); +@@ -305,6 +296,7 @@ out: + + me->local = 1; + local = me; ++ + found: + if (mep) { + *mep = local; +@@ -320,11 +312,10 @@ int find_myself(struct booth_config *conf, struct booth_site **mep, + _find_myself(conf, AF_INET, mep, fuzzy_allowed); + } + +- +-/** Checks the header fields for validity. +- * cf. init_header(). ++/* Check the header fields for validity. + * For @len_incl_data < 0 the length is not checked. +- * Return <0 if error, else bytes read. */ ++ * Return < 0 if error, else bytes read. ++ */ + int check_boothc_header(struct boothc_header *h, int len_incl_data) + { + int l; +@@ -338,21 +329,18 @@ int check_boothc_header(struct boothc_header *h, int len_incl_data) + return -EINVAL; + } + +- + l = ntohl(h->length); + if (l < sizeof(*h)) { + log_error("length %d out of range", l); + return -EINVAL; + } + +- + if (len_incl_data < 0) { + return 0; + } + + if (l != len_incl_data) { +- log_error("length error - got %d, wanted %d", +- len_incl_data, l); ++ log_error("length error - got %d, wanted %d", len_incl_data, l); + return -EINVAL; + } + +@@ -424,11 +412,12 @@ int read_client(struct client *req_cl) + } + + memset(msg, 0, MAX_MSG_LEN); +- req_cl->msg = (void *)msg; ++ req_cl->msg = (void *) msg; + } else { +- msg = (char *)req_cl->msg; ++ msg = (char *) req_cl->msg; + } +- header = (struct boothc_header *)msg; ++ ++ header = (struct boothc_header *) msg; + + /* update len if we read enough */ + if (req_cl->offset >= sizeof(*header)) { +@@ -443,6 +432,7 @@ int read_client(struct client *req_cl) + } + return -1; + } ++ + req_cl->offset += rv; + + /* update len if we read enough */ +@@ -462,7 +452,6 @@ int read_client(struct client *req_cl) + return 0; + } + +- + /* Only used for client requests (tcp) */ + static void process_connection(struct booth_config *conf, int ci) + { +@@ -484,15 +473,15 @@ static void process_connection(struct booth_config *conf, int ci) + msg = req_cl->msg; + } + +- header = (struct boothc_header *)msg; ++ header = (struct boothc_header *) msg; + if (check_auth(conf, NULL, msg, ntohl(header->length))) { + errc = RLT_AUTH; + goto send_err; + } + + /* For CMD_GRANT and CMD_REVOKE: +- * Don't close connection immediately, but send +- * result a second later? */ ++ * Don't close connection immediately, but send result a second later? ++ */ + switch (ntohl(header->cmd)) { + case CMD_LIST: + ticket_answer_list(conf, req_cl->fd); +@@ -520,8 +509,7 @@ static void process_connection(struct booth_config *conf, int ci) + } + + default: +- log_error("connection %d cmd %x unknown", +- ci, ntohl(header->cmd)); ++ log_error("connection %d cmd %x unknown", ci, ntohl(header->cmd)); + errc = RLT_INVALID_ARG; + goto send_err; + } +@@ -535,13 +523,11 @@ send_err: + + kill: + deadfn = req_cl->deadfn; +- if(deadfn) { ++ if (deadfn) { + deadfn(ci); + } +- return; + } + +- + static void process_tcp_listener(struct booth_config *conf, int ci) + { + int fd, i, flags, one = 1; +@@ -550,11 +536,10 @@ static void process_tcp_listener(struct booth_config *conf, int ci) + + fd = accept(clients[ci].fd, &addr, &addrlen); + if (fd < 0) { +- log_error("process_tcp_listener: accept error %d %d", +- fd, errno); ++ log_error("process_tcp_listener: accept error %d %d", fd, errno); + return; + } +- setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, (char *)&one, sizeof(one)); ++ setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, (char *) &one, sizeof(one)); + + flags = fcntl(fd, F_GETFL, 0); + if (fcntl(fd, F_SETFL, flags | O_NONBLOCK) == -1) { +@@ -564,8 +549,7 @@ static void process_tcp_listener(struct booth_config *conf, int ci) + return; + } + +- i = client_add(fd, clients[ci].transport, +- process_connection, NULL); ++ i = client_add(fd, clients[ci].transport, process_connection, NULL); + + log_debug("client connection %d fd %d", i, fd); + } +@@ -581,7 +565,7 @@ int setup_tcp_listener(int test_only) + return s; + } + +- rv = setsockopt(s, SOL_SOCKET, SO_REUSEADDR, (char *)&one, sizeof(one)); ++ rv = setsockopt(s, SOL_SOCKET, SO_REUSEADDR, (char *) &one, sizeof(one)); + if (rv == -1) { + close(s); + log_error("failed to set the SO_REUSEADDR option"); +@@ -624,19 +608,17 @@ static int booth_tcp_init(void * unused __attribute__((unused))) + return rv; + } + +- client_add(rv, booth_transport + TCP, +- process_tcp_listener, NULL); +- ++ client_add(rv, booth_transport + TCP, process_tcp_listener, NULL); + return 0; + } + + static int connect_nonb(int sockfd, const struct sockaddr *saptr, + socklen_t salen, int sec) + { +- int flags, n, error; +- socklen_t len; +- fd_set rset, wset; +- struct timeval tval; ++ int flags, n, error; ++ socklen_t len; ++ fd_set rset, wset; ++ struct timeval tval; + + flags = fcntl(sockfd, F_GETFL, 0); + if (fcntl(sockfd, F_SETFL, flags | O_NONBLOCK) == -1) { +@@ -661,9 +643,6 @@ static int connect_nonb(int sockfd, const struct sockaddr *saptr, + tval.tv_usec = 0; + + if (select(sockfd + 1, &rset, &wset, NULL, sec ? &tval : NULL) == 0) { +- /* leave outside function to close */ +- /* timeout */ +- /* close(sockfd); */ + errno = ETIMEDOUT; + return -1; + } +@@ -686,8 +665,6 @@ done: + } + + if (error) { +- /* leave outside function to close */ +- /* close(sockfd); */ + errno = error; + return -1; + } +@@ -709,14 +686,13 @@ static int booth_tcp_open(struct booth_site *to) + return -1; + } + +- +- rv = connect_nonb(s, (struct sockaddr *)&to->sa6, to->saddrlen, 10); ++ rv = connect_nonb(s, (struct sockaddr *) &to->sa6, to->saddrlen, 10); + if (rv == -1) { + if (errno == ETIMEDOUT) { + log_error("connect to %s got a timeout", site_string(to)); + } else { + log_error("connect to %s got an error: %s", site_string(to), +- strerror(errno)); ++ strerror(errno)); + } + goto error; + } +@@ -749,9 +725,10 @@ static int add_hmac(struct booth_config *conf, void *data, int len) + } + + payload_len = len - sizeof(struct hmac); +- hp = (struct hmac *)((unsigned char *)data + payload_len); ++ hp = (struct hmac *) ((unsigned char *) data + payload_len); + hp->hid = htonl(BOOTH_HASH); + memset(hp->hash, 0, BOOTH_MAC_SIZE); ++ + rv = calc_hmac(data, payload_len, BOOTH_HASH, hp->hash, + conf->authkey, conf->authkey_len); + if (rv < 0) { +@@ -776,13 +753,13 @@ static int booth_tcp_send(struct booth_config *conf, struct booth_site *to, + + static int booth_tcp_recv(struct booth_site *from, void *buf, int len) + { +- int got; + /* Needs timeouts! */ +- got = do_read(from->tcp_fd, buf, len); ++ int got = do_read(from->tcp_fd, buf, len); ++ + if (got < 0) { + log_error("read failed (%d): %s", errno, strerror(errno)); +- return got; + } ++ + return got; + } + +@@ -798,9 +775,11 @@ static int booth_tcp_recv_auth(struct booth_config *conf, struct booth_site *fro + if (got < 0) { + return got; + } ++ + total = got; ++ + if (is_auth_req()) { +- got = booth_tcp_recv(from, (unsigned char *)buf+payload_len, ++ got = booth_tcp_recv(from, (unsigned char *) buf+payload_len, + sizeof(struct hmac)); + + if (got != sizeof(struct hmac) || check_auth(conf, from, buf, len)) { +@@ -809,6 +788,7 @@ static int booth_tcp_recv_auth(struct booth_config *conf, struct booth_site *fro + + total += got; + } ++ + return total; + } + +@@ -821,6 +801,7 @@ static int booth_tcp_close(struct booth_site *to) + + to->tcp_fd = -1; + } ++ + return 0; + } + +@@ -843,29 +824,29 @@ static int setup_udp_server(void) + + rv = fcntl(fd, F_SETFL, O_NONBLOCK); + if (rv == -1) { +- log_error("failed to set non-blocking operation " +- "on UDP socket: %s", strerror(errno)); ++ log_error("failed to set non-blocking operation on UDP socket: %s", ++ strerror(errno)); + goto ex; + } + +- rv = setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, (char *)&one, sizeof(one)); ++ rv = setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, (char *) &one, sizeof(one)); + if (rv == -1) { + log_error("failed to set the SO_REUSEADDR option"); + goto ex; + } + +- rv = bind(fd, (struct sockaddr *)&local->sa6, local->saddrlen); ++ rv = bind(fd, (struct sockaddr *) &local->sa6, local->saddrlen); + + if (rv == -1) { + log_error("failed to bind UDP socket to [%s]:%d: %s", +- site_string(local), site_port(local), +- strerror(errno)); ++ site_string(local), site_port(local), strerror(errno)); + goto ex; + } + + recvbuf_size = SOCKET_BUFFER_SIZE; +- rv = setsockopt(fd, SOL_SOCKET, SO_RCVBUF, +- &recvbuf_size, sizeof(recvbuf_size)); ++ rv = setsockopt(fd, SOL_SOCKET, SO_RCVBUF, &recvbuf_size, ++ sizeof(recvbuf_size)); ++ + if (rv == -1) { + log_error("failed to set recvbuf size"); + goto ex; +@@ -882,35 +863,31 @@ ex: + return -1; + } + +- + /* Receive/process callback for UDP */ + static void process_recv(struct booth_config *conf, int ci) + { + struct sockaddr_storage sa; + int rv; + socklen_t sa_len; +- /* beware, the buffer needs to be large enough to accept +- * a packet */ ++ /* buffer needs to be large enough to accept a packet */ + char buffer[MAX_MSG_LEN]; + /* Used for unit tests */ + struct boothc_ticket_msg *msg; + +- + sa_len = sizeof(sa); +- msg = (void*)buffer; +- rv = recvfrom(clients[ci].fd, +- buffer, sizeof(buffer), +- MSG_NOSIGNAL | MSG_DONTWAIT, +- (struct sockaddr *)&sa, &sa_len); ++ msg = (void*) buffer; ++ rv = recvfrom(clients[ci].fd, buffer, sizeof(buffer), ++ MSG_NOSIGNAL | MSG_DONTWAIT, (struct sockaddr *) &sa, ++ &sa_len); ++ + if (rv == -1) { + return; + } + + rv = deliver_fn(conf, (void*) msg, rv); + if (rv > 0) { +- if (getnameinfo((struct sockaddr *)&sa, sa_len, +- buffer, sizeof(buffer), NULL, 0, +- NI_NUMERICHOST) == 0) { ++ if (getnameinfo((struct sockaddr *) &sa, sa_len, buffer, ++ sizeof(buffer), NULL, 0, NI_NUMERICHOST) == 0) { + log_error("unknown sender: %08x (real: %s)", rv, buffer); + } else { + log_error("unknown sender: %08x", rv); +@@ -928,10 +905,7 @@ static int booth_udp_init(void *f) + } + + deliver_fn = f; +- client_add(local->udp_fd, +- booth_transport + UDP, +- process_recv, NULL); +- ++ client_add(local->udp_fd, booth_transport + UDP, process_recv, NULL); + return 0; + } + +@@ -942,20 +916,18 @@ static int booth_udp_send(struct booth_config *conf, struct booth_site *to, + + to->sent_cnt++; + rv = sendto(local->udp_fd, buf, len, MSG_NOSIGNAL, +- (struct sockaddr *)&to->sa6, to->saddrlen); ++ (struct sockaddr *) &to->sa6, to->saddrlen); ++ + if (rv == len) { + rv = 0; + } else if (rv < 0) { + to->sent_err_cnt++; +- log_error("Cannot send to %s: %d %s", +- site_string(to), +- errno, +- strerror(errno)); ++ log_error("Cannot send to %s: %d %s", site_string(to), errno, ++ strerror(errno)); + } else { + rv = -1; + to->sent_err_cnt++; +- log_error("Packet sent to %s got truncated", +- site_string(to)); ++ log_error("Packet sent to %s got truncated", site_string(to)); + } + + return rv; +@@ -1037,6 +1009,7 @@ static int return_0(void) + { + return 0; + } ++ + const struct booth_transport booth_transport[TRANSPORT_ENTRIES] = { + [TCP] = { + .name = "TCP", +@@ -1091,9 +1064,10 @@ static int verify_ts(struct booth_config *conf, struct booth_site *from, + return -1; + } + +- h = (struct boothc_header *)buf; ++ h = (struct boothc_header *) buf; + tv.tv_sec = ntohl(h->secs); + tv.tv_usec = ntohl(h->usecs); ++ + if (from) { + curr_tv.tv_sec = from->last_secs; + curr_tv.tv_usec = from->last_usecs; +@@ -1102,7 +1076,7 @@ static int verify_ts(struct booth_config *conf, struct booth_site *from, + } + + log_warn("%s: packet timestamp older than previous one", +- site_string(from)); ++ site_string(from)); + } + + gettimeofday(&now, NULL); +@@ -1112,7 +1086,7 @@ static int verify_ts(struct booth_config *conf, struct booth_site *from, + } + + log_error("%s: packet timestamp older than %d seconds", +- peer_string(from), conf->maxtimeskew); ++ peer_string(from), conf->maxtimeskew); + return -1; + + accept: +@@ -1139,15 +1113,18 @@ int check_auth(struct booth_config *conf, struct booth_site *from, void *buf, + payload_len = len - sizeof(struct hmac); + if (payload_len < 0) { + log_error("%s: failed to authenticate, packet too short (size:%d)", +- peer_string(from), len); ++ peer_string(from), len); + return -1; + } +- hp = (struct hmac *)((unsigned char *)buf + payload_len); ++ ++ hp = (struct hmac *) ((unsigned char *) buf + payload_len); + rv = verify_hmac(buf, payload_len, ntohl(hp->hid), hp->hash, + conf->authkey, conf->authkey_len); ++ + if (!rv) { + rv = verify_ts(conf, from, buf, len); + } ++ + if (rv != 0) { + log_error("%s: failed to authenticate", peer_string(from)); + } +@@ -1185,11 +1162,9 @@ int send_header_plus(struct booth_config *conf, int fd, + int message_recv(struct booth_config *conf, void *msg, int msglen) + { + uint32_t from; +- struct boothc_header *header; ++ struct boothc_header *header = msg; + struct booth_site *source; + +- header = (struct boothc_header *)msg; +- + from = ntohl(header->from); + if (!find_site_by_id(conf, from, &source)) { + /* caller knows the actual source address, pass +@@ -1214,9 +1189,7 @@ int message_recv(struct booth_config *conf, void *msg, int msglen) + } + + if (ntohl(header->opts) & BOOTH_OPT_ATTR) { +- /* not used, clients send/retrieve attributes directly +- * from sites +- */ ++ /* not used, clients send/retrieve attributes directly from sites */ + return attr_recv(conf, msg, source); + } else { + return ticket_recv(conf, msg, source); +-- +2.25.1 + diff --git a/booth.spec b/booth.spec index ed11166..ddcbe7a 100644 --- a/booth.spec +++ b/booth.spec @@ -24,7 +24,7 @@ %bcond_with run_build_tests %bcond_with include_unit_test -%global release 11 +%global release 15 ## User and group to use for nonprivileged services (should be in sync with pacemaker) %global uname hacluster @@ -77,7 +77,15 @@ Patch25: backport-Coding-style-improvements-to-ticket.c.patch Patch26: backport-Refactor-Remove-global-booth_conf-variable-from-noti.patch Patch27: backport-Refactor-Remove-global-booth_conf-variable-from-list.patch Patch28: backport-Refactor-Remove-global-booth_conf-variable-from-netw.patch - +Patch29: backport-Refactor-Remove-global-booth_conf-variable-in-transp.patch +Patch30: backport-Refactor-Unindent-various-code-blocks-in-transport.c.patch +Patch31: backport-Refactor-Add-braces-around-conditional-blocks-in-tra.patch +Patch32: backport-Refactor-Remove-some-void-casts-from-transport.c.patch +Patch33: backport-Refactor-Use-whitespace-more-consistently-in-transpo.patch +Patch34: backport-Refactor-Check-the-return-value-of-setsockopt.patch +Patch35: backport-Refactor-Remove-global-booth_conf-variable-in-raft.c.patch +Patch36: backport-Refactor-Unindent-various-code-blocks-in-raft.c.patch + # direct build process dependencies BuildRequires: autoconf BuildRequires: automake @@ -325,6 +333,22 @@ VERBOSE=1 make check %{_usr}/lib/ocf/resource.d/booth/sharedrsc %changelog +* Tue Nov 11 2025 bizhiyuan - 1.2-15 +- Refactor: Unindent various code blocks in raft.c. + +* Tue Nov 04 2025 bizhiyuan - 1.2-14 +- Refactor: Check the return value of setsockopt +- Refactor: Remove global booth_conf variable in raft.c. + +* Fri Oct 31 2025 bizhiyuan - 1.2-13 +- Refactor: Remove some (void) casts from transport.c. +- Refactor: Use whitespace more consistently in transport.c. + +* Fri Oct 24 2025 bizhiyuan - 1.2-12 +- Refactor: Remove global booth_conf variable in transport.c. +- Refactor: Add braces around conditional blocks in transport.c. +- Refactor: Unindent various code blocks in transport.c. + * Wed Oct 22 2025 bizhiyuan - 1.2-11 - Remove global booth_conf variable from notify_client list_peers and network functions -- Gitee