ref: 09ed7a6e568022e313e5e5d92808ef724b874adb
parent: 58fe52dc8cefee0513ac9afa4481d78ab3a54a83
author: cinap_lenrek <cinap_lenrek@felloff.net>
date: Sat Jun 10 14:45:40 EDT 2023
libip: Provide iplocalonifc() and ipremoteonifc() functions These functions are analog to devip's functions of the same name. This is mainly to maintain consistent naming...
--- a/sys/include/ip.h
+++ b/sys/include/ip.h
@@ -171,6 +171,8 @@
int equivip6(uchar*, uchar*);
Ipifc* readipifc(char*, Ipifc*, int);
+Iplifc* iplocalonifc(Ipifc *, uchar *);
+Iplifc* ipremoteonifc(Ipifc *, uchar *);
void hnputv(void*, uvlong);
void hnputl(void*, uint);
--- a/sys/man/2/ip
+++ b/sys/man/2/ip
@@ -1,6 +1,6 @@
.TH IP 2
.SH NAME
-eipfmt, parseip, parseipmask, parseipandmask, v4parseip, parseether, myipaddr, myetheraddr, maskip, equivip4, equivip6, defmask, isv4, v4tov6, v6tov4, nhgetv, nhgetl, nhgets, hnputv, hnputl, hnputs, ptclbsum, readipifc \- Internet Protocol addressing
+eipfmt, parseip, parseipmask, parseipandmask, v4parseip, parseether, myipaddr, myetheraddr, maskip, equivip4, equivip6, defmask, isv4, v4tov6, v6tov4, nhgetv, nhgetl, nhgets, hnputv, hnputl, hnputs, ptclbsum, readipifc, iplocalonifc, ipremoteonifc \- Internet Protocol addressing
.SH SYNOPSIS
.B #include <u.h>
.br
@@ -78,6 +78,12 @@
Ipifc* readipifc(char *net, Ipifc *ifc, int index)
.PP
.B
+Iplifc* iplocalonifc(Ipifc *ifc, uchar *ip)
+.PP
+.B
+Iplifc* ipremoteonifc(Ipifc *ifc, uchar *ip)
+.PP
+.B
uchar IPv4bcast[IPaddrlen];
.PP
.B
@@ -372,6 +378,33 @@
if the network is multipoint or
the remote address if the network is
point to point.
+.PP
+.I Iplocalonifc
+looks up the first local address structure
+that matches
+.I ip
+on the interface
+.IR ifc .
+E.g.
+.I ip
+is one of our local ip addresses on
+.IR ifc .
+.I Ipremoteonifc
+returns the first local address structure
+with a subnet that
+.I ip
+is a member of.
+E.g.
+.I ip
+is a directly reachable remote address on
+.IR ifc .
+Both
+.I iplocalonifc
+and
+.I ipremoteonifc
+return
+.B nil
+when no local address structrue is found.
.SH SOURCE
.B /sys/src/libip
.SH SEE ALSO
--- a/sys/src/cmd/auth/secureidcheck.c
+++ b/sys/src/cmd/auth/secureidcheck.c
@@ -325,22 +325,6 @@
syslog(0, AUTHLOG, "%s", pbuf);
}
-static uchar*
-getipv4addr(void)
-{
- Ipifc *nifc;
- Iplifc *lifc;
- static Ipifc *ifc;
-
- ifc = readipifc("/net", ifc, -1);
- for(nifc = ifc; nifc; nifc = nifc->next)
- for(lifc = nifc->lifc; lifc; lifc = lifc->next)
- if (ipcmp(lifc->ip, IPnoaddr) != 0 &&
- ipcmp(lifc->ip, v4prefix) != 0)
- return lifc->ip;
- return nil;
-}
-
extern Ndb *db;
/* returns 0 on success, error message on failure */
@@ -350,7 +334,7 @@
char *radiussecret = nil;
char *rv = "authentication failed";
char dest[3*IPaddrlen+20], ruser[64];
- uchar *ip;
+ uchar ip[IPaddrlen];
uchar x[16];
ulong u[4];
Ndbs s;
@@ -394,9 +378,12 @@
goto out;
shared.s = (uchar*)radiussecret;
shared.len = strlen(radiussecret);
- ip = getipv4addr();
- if(ip == nil){
+ if(myipaddr(ip, "/net") < 0){
syslog(0, AUTHLOG, "no interfaces: %r");
+ goto out;
+ }
+ if(!isv4(ip)){
+ syslog(0, AUTHLOG, "no ipv4 address");
goto out;
}
if(setAttribute(req, R_NASIPAddress, ip + IPv4off, 4) < 0)
--- a/sys/src/cmd/ip/dhcp6d.c
+++ b/sys/src/cmd/ip/dhcp6d.c
@@ -103,29 +103,12 @@
findifc(char *net, uchar ip[IPaddrlen])
{
Ipifc *ifc;
- Iplifc *lifc;
ipifcs = readipifc(net, ipifcs, -1);
- for(ifc = ipifcs; ifc != nil; ifc = ifc->next)
- for(lifc = ifc->lifc; lifc != nil; lifc = lifc->next)
- if(ipcmp(lifc->ip, ip) == 0)
- return ifc;
-
- return nil;
-}
-
-static Iplifc*
-localonifc(Ipifc *ifc, uchar ip[IPaddrlen])
-{
- Iplifc *lifc;
- uchar net[IPaddrlen];
-
- for(lifc = ifc->lifc; lifc != nil; lifc = lifc->next){
- maskip(ip, lifc->mask, net);
- if(ipcmp(net, lifc->net) == 0)
- return lifc;
+ for(ifc = ipifcs; ifc != nil; ifc = ifc->next){
+ if(iplocalonifc(ifc, ip) != nil)
+ return ifc;
}
-
return nil;
}
@@ -364,8 +347,7 @@
continue;
if((r->ifc = findifc(netmtpt, r->udp->ifcaddr)) == nil)
continue;
- lifc = localonifc(r->ifc, r->udp->raddr);
- if(lifc == nil)
+ if((lifc = ipremoteonifc(r->ifc, r->udp->raddr)) == nil)
continue;
memmove(obuf, ibuf, Udphdrsize);
--- a/sys/src/cmd/ip/dhcpd/dat.h
+++ b/sys/src/cmd/ip/dhcpd/dat.h
@@ -73,7 +73,6 @@
extern int lookupip(uchar*, char*, char*, Info*, int);
extern void lookupname(char*, int, Ndbtuple*);
extern Ipifc* findifc(uchar*);
-extern Iplifc* localonifc(uchar*, Ipifc*);
extern void localip(uchar*, uchar*, Ipifc*);
extern int lookupserver(char*, uchar**, int, Ndbtuple *t);
extern Ndbtuple* lookupinfo(uchar *ipaddr, char **attr, int n);
--- a/sys/src/cmd/ip/dhcpd/db.c
+++ b/sys/src/cmd/ip/dhcpd/db.c
@@ -177,23 +177,15 @@
extern int
samenet(uchar *ip, Info *iip)
{
- uchar x[IPaddrlen];
+ uchar net[IPaddrlen];
/* directly connected, check local networks */
- if(iip->ifc != nil){
- Iplifc *lifc;
+ if(iip->ifc != nil)
+ return ipremoteonifc(iip->ifc, ip) != nil;
- for(lifc = iip->ifc->lifc; lifc != nil; lifc = lifc->next){
- maskip(lifc->mask, ip, x);
- if(ipcmp(x, lifc->net) == 0)
- return 1;
- }
- return 0;
- }
-
/* relay agent, check upstream network */
- maskip(iip->ipmask, ip, x);
- return ipcmp(x, iip->ipnet) == 0;
+ maskip(ip, iip->ipmask, net);
+ return ipcmp(net, iip->ipnet) == 0;
}
/*
--- a/sys/src/cmd/ip/dhcpd/dhcpd.c
+++ b/sys/src/cmd/ip/dhcpd/dhcpd.c
@@ -350,7 +350,8 @@
rp->gii.ifc = nil;
} else {
/* no gateway, directly connected */
- if(ipcmp(rp->up->laddr, IPv4bcast) != 0 && localonifc(rp->up->laddr, rp->ifc) == nil){
+ if(ipcmp(rp->up->laddr, IPv4bcast) != 0
+ && ipremoteonifc(rp->ifc, rp->up->laddr) == nil){
warning("wrong network %I->%I on %s",
rp->up->raddr, rp->up->laddr, rp->ifc->dev);
return;
@@ -1135,7 +1136,7 @@
maskopt(rp, OBmask, rp->ii.ipmask);
else if(validip(rp->gii.ipmask))
maskopt(rp, OBmask, rp->gii.ipmask);
- else if((lifc = localonifc(ip, rp->ifc)) != nil)
+ else if((lifc = ipremoteonifc(rp->ifc, ip)) != nil)
maskopt(rp, OBmask, lifc->mask);
if(validip(rp->ii.gwip)){
--- a/sys/src/cmd/ip/dhcpd/ndb.c
+++ b/sys/src/cmd/ip/dhcpd/ndb.c
@@ -37,44 +37,24 @@
findifc(uchar *ip)
{
Ipifc *ifc;
- Iplifc *lifc;
for(ifc = ipifcs; ifc != nil; ifc = ifc->next){
- for(lifc = ifc->lifc; lifc != nil; lifc = lifc->next)
- if(ipcmp(ip, lifc->ip) == 0)
- return ifc;
+ if(iplocalonifc(ifc, ip) != nil)
+ return ifc;
}
return nil;
}
-Iplifc*
-localonifc(uchar *ip, Ipifc *ifc)
-{
- uchar x[IPaddrlen];
- Iplifc *lifc;
-
- if(ifc == nil)
- return nil;
-
- for(lifc = ifc->lifc; lifc != nil; lifc = lifc->next){
- maskip(ip, lifc->mask, x);
- if(ipcmp(x, lifc->net) == 0)
- return lifc;
- }
- return nil;
-}
-
void
localip(uchar *laddr, uchar *raddr, Ipifc *ifc)
{
Iplifc *lifc;
- if((lifc = localonifc(raddr, ifc)) != nil)
+ if((lifc = ipremoteonifc(ifc, raddr)) != nil)
ipmove(laddr, lifc->ip);
else if(ipcmp(laddr, IPv4bcast) == 0)
ipmove(laddr, IPnoaddr);
}
-
static void
setipaddr(uchar *addr, char *ip)
--- a/sys/src/cmd/ip/ipconfig/main.c
+++ b/sys/src/cmd/ip/ipconfig/main.c
@@ -297,15 +297,10 @@
int
myip(Ipifc *ifc, uchar *ip)
{
- Iplifc *lifc;
-
for(; ifc != nil; ifc = ifc->next) {
- for(lifc = ifc->lifc; lifc != nil; lifc = lifc->next){
- if(ipcmp(ip, lifc->ip) == 0)
- return 1;
- }
+ if(iplocalonifc(ifc, ip) != nil)
+ return 1;
}
-
return 0;
}
--- a/sys/src/cmd/ndb/dblookup.c
+++ b/sys/src/cmd/ndb/dblookup.c
@@ -900,15 +900,12 @@
myip(uchar *ip)
{
Ipifc *ifc;
- Iplifc *lifc;
qlock(&ipifclock);
for(ifc = ipifcs; ifc != nil; ifc = ifc->next){
- for(lifc = ifc->lifc; lifc != nil; lifc = lifc->next){
- if(ipcmp(ip, lifc->ip) == 0){
- qunlock(&ipifclock);
- return 1;
- }
+ if(iplocalonifc(ifc, ip) != nil){
+ qunlock(&ipifclock);
+ return 1;
}
}
qunlock(&ipifclock);
--- a/sys/src/libip/mkfile
+++ b/sys/src/libip/mkfile
@@ -12,6 +12,8 @@
bo.$O\
readipifc.$O\
ipaux.$O\
+ iplocalonifc.$O\
+ ipremoteonifc.$O\
ptclbsum.$O\
HFILES=\