shithub: riscv

Download patch

ref: 23e378a960f6027f8daba93f34ebeb1ae3d00517
parent: 4b3a0ca15911333d92389daa47220c8742855f1f
author: cinap_lenrek <cinap_lenrek@felloff.net>
date: Wed Sep 28 14:20:20 EDT 2022

arp: move arp expire logic into own function

Add arphit() function, handling validation and checking
for maximum life-time of the enty as well as copying
out the mac address.

--- a/sys/src/9/ip/arp.c
+++ b/sys/src/9/ip/arp.c
@@ -177,6 +177,21 @@
 	return nil;
 }
 
+static int
+arphit(Arpent *a, uchar *mac, int maclen, Routehint *rh)
+{
+	if(a->state == AOK){
+		memmove(mac, a->mac, maclen);
+		a->utime = NOW;
+		if(a->utime - a->ctime < MAXAGE_TIMER){
+			if(rh != nil)
+				rh->a = a;
+			return 1;
+		}
+	}
+	return 0;
+}
+
 /*
  *  fill in the media address if we have it.  Otherwise return an
  *  Arpent that represents the state of the address resolution FSM
@@ -196,33 +211,29 @@
 
 	if(rh != nil
 	&& (a = rh->a) != nil
-	&& a->state == AOK
 	&& a->ifc == ifc
 	&& a->ifcid == ifc->ifcid
-	&& ipcmp(ip, a->ip) == 0){
-		memmove(mac, a->mac, ifc->m->maclen);
-		a->utime = NOW;
-		if(a->utime - a->ctime < MAXAGE_TIMER)
-			return nil;
-	}
+	&& ipcmp(ip, a->ip) == 0
+	&& arphit(a, mac, ifc->m->maclen, nil))
+		return nil;
 
 	rlock(arp);
-	if((a = arplookup(arp, ifc, ip)) != nil && a->state == AOK){
-		memmove(mac, a->mac, ifc->m->maclen);
-		a->utime = NOW;
-		if(a->utime - a->ctime < MAXAGE_TIMER){
-			if(rh != nil)
-				rh->a = a;
-			runlock(arp);
-			return nil;
-		}
+	if((a = arplookup(arp, ifc, ip)) != nil
+	&& arphit(a, mac, ifc->m->maclen, rh)){
+		runlock(arp);
+		return nil;
 	}
 	if(rh != nil)
 		rh->a = nil;
 	runlock(arp);
+
 	wlock(arp);
 	if((a = arplookup(arp, ifc, ip)) == nil)
 		a = newarpent(arp, ip, ifc);
+	else if(arphit(a, mac, ifc->m->maclen, rh)){
+		wunlock(arp);
+		return nil;
+	}
 	a->state = AWAIT;
 	a->utime = NOW;
 	if(bp != nil){