From 0e38cb0fcb54b0475f0bf408c504b7a1a6de7b48 Mon Sep 17 00:00:00 2001
From: "Rob Swindell (on ChromeOS)" <rob@synchro.net>
Date: Sat, 18 Feb 2023 12:30:14 -0800
Subject: [PATCH] Rework NetMail routing logic to handle point destinations
 better

With this commmit, the logic is now:

If the NetMail destination is a point and there's no linked-node for it
   and the boss node for this point is not one of our AKAs
   then route to the boss node (no linked-node match needed)
   if the boss node matches a linked-node with a route configuration
   then route to that configured route destination (2-levels of routing
   and thus 2 "Routing NetMail" msgs will be logged in this scenario)

If the NetMail destination is a point and there is a linked-node for it
   then the netmail will not be routed
		unless that linked node has a route configuration

NetMail destined for non-points are treated the same as before this
commit.

No change in the routing of echomail for points.

I think this change fixes issue #520 which can be traced back to commit
7bd42e56d for poindexter FORTRAN (REALITY), 2 years ago almost to the
day, where that change added point->boss routing, but only if the boss
had a matching linked-node configuration (for both echomail and
netmail).

This commit also re-thinks commit ffe69b43623d (for Alterego) which
would not route a point netmail if any our local AKAs matched the
destination address, igorining the point value. I think what was
intended here (I coudln't locate the original enhancmenet request or
issue), was to not route if a local AKA was the *boss* of the point.
And with this commit, that's now how it now works.

As before, "test results appreciated".
---
 src/sbbs3/sbbsecho.c | 38 +++++++++++++++++---------------------
 1 file changed, 17 insertions(+), 21 deletions(-)

diff --git a/src/sbbs3/sbbsecho.c b/src/sbbs3/sbbsecho.c
index 389ef5882f..d76c71e4b7 100644
--- a/src/sbbs3/sbbsecho.c
+++ b/src/sbbs3/sbbsecho.c
@@ -1274,18 +1274,12 @@ bool area_is_valid(uint areanum)
 	return areanum < cfg.areas;
 }
 
-// if full is true, compare full address, otherwise, exclude point in comparison
-uint find_sysfaddr(faddr_t addr, bool full)
+uint find_sysfaddr(faddr_t addr)
 {
 	unsigned u;
 
 	for(u=0; u < scfg.total_faddrs; u++) {
-		if(full && memcmp(&scfg.faddr[u], &addr, sizeof(addr)) == 0)
-			break;
-		if(!full
-			&& scfg.faddr[u].zone == addr.zone
-			&& scfg.faddr[u].net == addr.net
-			&& scfg.faddr[u].node == addr.node)
+		if(memcmp(&scfg.faddr[u], &addr, sizeof(addr)) == 0)
 			break;
 	}
 
@@ -5421,7 +5415,7 @@ void pack_netmail(void)
 		addr.point		= hdr.destpoint;
 		printf("NetMail msg %s from %s (%s) to %s (%s): "
 			,getfname(path), hdr.from, fmsghdr_srcaddr_str(&hdr), hdr.to, smb_faddrtoa(&addr,NULL));
-		if(sysfaddr_is_valid(find_sysfaddr(addr, true)))	{				  /* In-bound, so ignore */
+		if(sysfaddr_is_valid(find_sysfaddr(addr)))	{				  /* In-bound, so ignore */
 			printf("in-bound\n");
 			fclose(fidomsg);
 			continue;
@@ -5474,24 +5468,26 @@ void pack_netmail(void)
 			return;
 		}
 
-		nodecfg=findnodecfg(&cfg, addr, /* exact: */false);
-		if(!sysfaddr_is_valid(find_sysfaddr(addr, false))) {
-			if(nodecfg!=NULL && nodecfg->route.zone	&& nodecfg->status==MAIL_STATUS_NORMAL
-				&& !(hdr.attr&(FIDO_CRASH|FIDO_HOLD))) {
-				addr=nodecfg->route;		/* Routed */
-				lprintf(LOG_INFO, "Routing NetMail (%s) to %s",getfname(path),smb_faddrtoa(&addr,NULL));
-				nodecfg=findnodecfg(&cfg, addr, /* exact: */false);
-			}
-			if(nodecfg == NULL && addr.point != 0) {
+		if(addr.point != 0) {
+			nodecfg = findnodecfg(&cfg, addr, /* exact: */true);
+			if(nodecfg == NULL) {
 				fidoaddr_t	boss = addr;
 				boss.point = 0;
-				if((nodecfg = findnodecfg(&cfg, boss, /* exact: */true)) != NULL) {
+				if(!sysfaddr_is_valid(find_sysfaddr(boss))) {
 					addr = boss;
 					lprintf(LOG_INFO, "Routing NetMail (%s) to boss-node %s"
 						,getfname(path), smb_faddrtoa(&addr, NULL));
 				}
 			}
 		}
+		if(nodecfg == NULL)
+			nodecfg = findnodecfg(&cfg, addr, /* exact: */false);
+		if(nodecfg!=NULL && nodecfg->route.zone	&& nodecfg->status==MAIL_STATUS_NORMAL
+			&& !(hdr.attr&(FIDO_CRASH|FIDO_HOLD))) {
+			addr=nodecfg->route;		/* Routed */
+			lprintf(LOG_INFO, "Routing NetMail (%s) to %s",getfname(path),smb_faddrtoa(&addr,NULL));
+			nodecfg=findnodecfg(&cfg, addr, /* exact: */false);
+		}
 		if(!bso_lock_node(addr))
 			continue;
 
@@ -5634,7 +5630,7 @@ void find_stray_packets(void)
 			delfile(packet, __LINE__);
 			continue;
 		}
-		uint sysfaddr = find_sysfaddr(pkt_orig, true);
+		uint sysfaddr = find_sysfaddr(pkt_orig);
 		lprintf(LOG_WARNING,"Stray Outbound Packet (type %s) found, from %s address %s to %s: %s"
 			,pktTypeStringList[pkt_type], sysfaddr_is_valid(sysfaddr) ? "local" : "FOREIGN"
 			,smb_faddrtoa(&pkt_orig, NULL), smb_faddrtoa(&pkt_dest, str), packet);
@@ -6286,7 +6282,7 @@ int main(int argc, char **argv)
 	}
 
 	for(uint u = 0; u < cfg.nodecfgs; u++) {
-		if(sysfaddr_is_valid(find_sysfaddr(cfg.nodecfg[u].addr, true))) {
+		if(sysfaddr_is_valid(find_sysfaddr(cfg.nodecfg[u].addr))) {
 			lprintf(LOG_ERR, "Configuration ERROR: Linked node #%u is your own address: %s"
 				,u + 1, faddrtoa(&cfg.nodecfg[u].addr));
 			bail(1);
-- 
GitLab