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...
This commit is contained in:
cinap_lenrek 2023-06-10 18:45:40 +00:00
parent 58fe52dc8c
commit 09ed7a6e56
11 changed files with 64 additions and 94 deletions

View file

@ -171,6 +171,8 @@ int equivip4(uchar*, uchar*);
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);

View file

@ -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 @@ ushort ptclbsum(uchar *a, int n)
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 @@ is
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

View file

@ -325,22 +325,6 @@ logPacket(Packet *p)
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 @@ secureidcheck(char *user, char *response)
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,11 +378,14 @@ secureidcheck(char *user, char *response)
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)
goto out;

View file

@ -103,29 +103,12 @@ static Ipifc*
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 @@ main(int argc, char *argv[])
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);

View file

@ -73,7 +73,6 @@ extern int lookup(Bootp*, Info*, Info*);
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);

View file

@ -177,23 +177,15 @@ syncbinding(Binding *b, int returnfd)
extern int
samenet(uchar *ip, Info *iip)
{
uchar x[IPaddrlen];
uchar net[IPaddrlen];
/* directly connected, check local networks */
if(iip->ifc != nil){
Iplifc *lifc;
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;
}
if(iip->ifc != nil)
return ipremoteonifc(iip->ifc, ip) != nil;
/* 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;
}
/*

View file

@ -350,7 +350,8 @@ proto(Req *rp, int n)
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 @@ miscoptions(Req *rp, uchar *ip)
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)){

View file

@ -37,29 +37,10 @@ Ipifc*
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;
}
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;
if(iplocalonifc(ifc, ip) != nil)
return ifc;
}
return nil;
}
@ -69,13 +50,12 @@ 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)
{

View file

@ -297,15 +297,10 @@ findmyifc(void)
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;
}

View file

@ -900,15 +900,12 @@ int
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);

View file

@ -12,6 +12,8 @@ OFILES=\
bo.$O\
readipifc.$O\
ipaux.$O\
iplocalonifc.$O\
ipremoteonifc.$O\
ptclbsum.$O\
HFILES=\