From 1237836a7fdcf2b703269bf13e4fc3e60125fbca Mon Sep 17 00:00:00 2001 From: rsc Date: Sat, 31 Dec 2005 20:02:45 +0000 Subject: darwin --- src/libip/BSD.c | 146 +++++++++++++++++++++++++++++++++++++++++++++++++++ src/libip/Darwin.c | 2 +- src/libip/FreeBSD.c | 147 +--------------------------------------------------- src/libip/eipfmt.c | 2 +- src/libip/mkfile | 2 + 5 files changed, 151 insertions(+), 148 deletions(-) create mode 100644 src/libip/BSD.c (limited to 'src/libip') diff --git a/src/libip/BSD.c b/src/libip/BSD.c new file mode 100644 index 00000000..9bf2db0c --- /dev/null +++ b/src/libip/BSD.c @@ -0,0 +1,146 @@ +#include +/* #include */ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +static void +sockaddr2ip(uchar *ip, struct sockaddr *sa) +{ + struct sockaddr_in *sin; + + sin = (struct sockaddr_in*)sa; + memmove(ip, v4prefix, IPaddrlen); + memmove(ip+IPv4off, &sin->sin_addr, 4); +} + +Ipifc* +readipifc(char *net, Ipifc *ifc, int index) +{ + char *p, *ep, *q, *bp; + int i, mib[6]; + size_t n; + Ipifc *list, **last; + Iplifc *lifc, **lastlifc; + struct if_msghdr *mh, *nmh; + struct ifa_msghdr *ah; + struct sockaddr *sa; + struct sockaddr_dl *sdl; + uchar ip[IPaddrlen]; + + USED(net); + + free(ifc); + ifc = nil; + list = nil; + last = &list; + + /* + * Does not handle IPv6 yet. + */ + + mib[0] = CTL_NET; + mib[1] = PF_ROUTE; + mib[2] = 0; + mib[3] = 0; + mib[4] = NET_RT_IFLIST; + mib[5] = 0; + + n = 0; + if(sysctl(mib, 6, nil, &n, nil, 0) < 0) + return nil; + bp = mallocz(n, 1); + if(bp == nil) + return nil; + if(sysctl(mib, 6, bp, &n, nil, 0) < 0){ + free(bp); + return nil; + } + + p = bp; + ep = p+n; + while(p < ep){ + mh = (struct if_msghdr*)p; + p += mh->ifm_msglen; + if(mh->ifm_type != RTM_IFINFO) + continue; + ifc = mallocz(sizeof *ifc, 1); + if(ifc == nil) + break; + *last = ifc; + last = &ifc->next; + sdl = (struct sockaddr_dl*)(mh+1); + n = sdl->sdl_nlen; + if(n >= sizeof ifc->dev) + n = sizeof ifc->dev - 1; + memmove(ifc->dev, sdl->sdl_data, n); + ifc->dev[n] = 0; + ifc->rp.linkmtu = mh->ifm_data.ifi_mtu; + lastlifc = &ifc->lifc; + + if(sdl->sdl_type == IFT_ETHER && sdl->sdl_alen == 6) + memmove(ifc->ether, LLADDR(sdl), 6); + + while(p < ep){ + ah = (struct ifa_msghdr*)p; + nmh = (struct if_msghdr*)p; + if(nmh->ifm_type != RTM_NEWADDR) + break; + p += nmh->ifm_msglen; + lifc = nil; + for(i=0, q=(char*)(ah+1); iifam_addrs & (1<sa_len+sizeof(long)-1) & ~(sizeof(long)-1); + if(sa->sa_family != AF_INET) + continue; + if(lifc == nil){ + lifc = mallocz(sizeof *lifc, 1); + if(lifc == nil) + continue; + *lastlifc = lifc; + lastlifc = &lifc->next; + } + sockaddr2ip(ip, sa); + switch(i){ + case RTAX_IFA: + ipmove(lifc->ip, ip); + break; + case RTAX_NETMASK: + memset(ip, 0xFF, IPv4off); + ipmove(lifc->mask, ip); + break; + case RTAX_BRD: + if(mh->ifm_flags & IFF_POINTOPOINT) + /* ipmove(lifc->remote, ip) */ ; + if(mh->ifm_flags & IFF_BROADCAST) + /* ipmove(lifc->bcast, ip) */ ; + break; + case RTAX_GATEWAY: + break; + case RTAX_DST: + break; + } + } + if(lifc) + maskip(lifc->ip, lifc->mask, lifc->net); + } + } + free(bp); + return list; +} diff --git a/src/libip/Darwin.c b/src/libip/Darwin.c index 48c87c62..8710a5fd 100644 --- a/src/libip/Darwin.c +++ b/src/libip/Darwin.c @@ -1 +1 @@ -#include "none.c" +#include "BSD.c" diff --git a/src/libip/FreeBSD.c b/src/libip/FreeBSD.c index 2e35330d..8710a5fd 100644 --- a/src/libip/FreeBSD.c +++ b/src/libip/FreeBSD.c @@ -1,146 +1 @@ -#include -/* #include */ -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include - -static void -sockaddr2ip(uchar *ip, struct sockaddr *sa) -{ - struct sockaddr_in *sin; - - sin = (struct sockaddr_in*)sa; - memmove(ip, v4prefix, IPaddrlen); - memmove(ip+IPv4off, &sin->sin_addr, 4); -} - -Ipifc* -readipifc(char *net, Ipifc *ifc, int index) -{ - char *p, *ep, *q, *bp; - int i, mib[6], n; - Ipifc *list, **last; - Iplifc *lifc, **lastlifc; - struct if_msghdr *mh, *nmh; - struct ifa_msghdr *ah; - struct sockaddr *sa; - struct sockaddr_dl *sdl; - uchar ip[IPaddrlen]; - - USED(net); - - free(ifc); - ifc = nil; - list = nil; - last = &list; - - /* - * Does not handle IPv6 yet. - */ - - mib[0] = CTL_NET; - mib[1] = PF_ROUTE; - mib[2] = 0; - mib[3] = 0; - mib[4] = NET_RT_IFLIST; - mib[5] = 0; - - n = 0; - if(sysctl(mib, 6, nil, &n, nil, 0) < 0) - return nil; - bp = mallocz(n, 1); - if(bp == nil) - return nil; - if(sysctl(mib, 6, bp, &n, nil, 0) < 0){ - free(bp); - return nil; - } - - p = bp; - ep = p+n; - while(p < ep){ - mh = (struct if_msghdr*)p; - p += mh->ifm_msglen; - if(mh->ifm_type != RTM_IFINFO) - continue; - ifc = mallocz(sizeof *ifc, 1); - if(ifc == nil) - break; - *last = ifc; - last = &ifc->next; - sdl = (struct sockaddr_dl*)(mh+1); - n = sdl->sdl_nlen; - if(n >= sizeof ifc->dev) - n = sizeof ifc->dev - 1; - memmove(ifc->dev, sdl->sdl_data, n); - ifc->dev[n] = 0; - ifc->rp.linkmtu = mh->ifm_data.ifi_mtu; - lastlifc = &ifc->lifc; - - if(sdl->sdl_type == IFT_ETHER && sdl->sdl_alen == 6) - memmove(ifc->ether, LLADDR(sdl), 6); - - while(p < ep){ - ah = (struct ifa_msghdr*)p; - nmh = (struct if_msghdr*)p; - if(nmh->ifm_type != RTM_NEWADDR) - break; - p += nmh->ifm_msglen; - lifc = nil; - for(i=0, q=(char*)(ah+1); iifam_addrs & (1<sa_len+sizeof(long)-1) & ~(sizeof(long)-1); - if(sa->sa_family != AF_INET) - continue; - if(lifc == nil){ - lifc = mallocz(sizeof *lifc, 1); - if(lifc == nil) - continue; - *lastlifc = lifc; - lastlifc = &lifc->next; - } - sockaddr2ip(ip, sa); - switch(i){ - case RTAX_IFA: - ipmove(lifc->ip, ip); - break; - case RTAX_NETMASK: - memset(ip, 0xFF, IPv4off); - ipmove(lifc->mask, ip); - break; - case RTAX_BRD: - if(mh->ifm_flags & IFF_POINTOPOINT) - /* ipmove(lifc->remote, ip) */ ; - if(mh->ifm_flags & IFF_BROADCAST) - /* ipmove(lifc->bcast, ip) */ ; - break; - case RTAX_GATEWAY: - break; - case RTAX_DST: - break; - } - } - if(lifc) - maskip(lifc->ip, lifc->mask, lifc->net); - } - } - free(bp); - return list; -} +#include "BSD.c" diff --git a/src/libip/eipfmt.c b/src/libip/eipfmt.c index fca028ee..bb715451 100644 --- a/src/libip/eipfmt.c +++ b/src/libip/eipfmt.c @@ -59,7 +59,7 @@ eipfmt(Fmt *f) fmt = efmt; if(f->flags&FmtSharp) fmt = altefmt; - snprint(buf, sizeof buf, efmt, p[0], p[1], p[2], p[3], p[4], p[5]); + snprint(buf, sizeof buf, fmt, p[0], p[1], p[2], p[3], p[4], p[5]); return fmtstrcpy(f, buf); case 'I': /* Ip address */ diff --git a/src/libip/mkfile b/src/libip/mkfile index 6f8ad2f8..9546034d 100644 --- a/src/libip/mkfile +++ b/src/libip/mkfile @@ -19,6 +19,8 @@ HFILES=\ <$PLAN9/src/mksyslib +Darwin.$O FreeBSD.$O: BSD.c + testreadipifc: testreadipifc.o $LIBDIR/$LIB $LD -o testreadipifc testreadipifc.o -- cgit v1.2.3