2 /* #include <everything_but_the_kitchen_sink.h> */
3 #include <sys/socket.h>
4 #include <netinet/in.h>
7 #include <sys/sysctl.h>
8 #include <net/ethernet.h>
10 #include <net/if_var.h>
11 #include <net/if_dl.h>
12 #include <net/if_types.h>
13 #include <net/route.h>
14 #include <netinet/in.h>
15 #include <netinet/in_var.h>
16 #include <arpa/inet.h>
22 sockaddr2ip(uchar *ip, struct sockaddr *sa)
24 struct sockaddr_in *sin;
26 sin = (struct sockaddr_in*)sa;
27 memmove(ip, v4prefix, IPaddrlen);
28 memmove(ip+IPv4off, &sin->sin_addr, 4);
32 readipifc(char *net, Ipifc *ifc, int index)
34 char *p, *ep, *q, *bp;
38 Iplifc *lifc, **lastlifc;
39 struct if_msghdr *mh, *nmh;
40 struct ifa_msghdr *ah;
42 struct sockaddr_dl *sdl;
53 * Does not handle IPv6 yet.
60 mib[4] = NET_RT_IFLIST;
64 if(sysctl(mib, 6, nil, &n, nil, 0) < 0)
69 if(sysctl(mib, 6, bp, &n, nil, 0) < 0){
77 mh = (struct if_msghdr*)p;
79 if(mh->ifm_type != RTM_IFINFO)
81 ifc = mallocz(sizeof *ifc, 1);
86 sdl = (struct sockaddr_dl*)(mh+1);
88 if(n >= sizeof ifc->dev)
89 n = sizeof ifc->dev - 1;
90 memmove(ifc->dev, sdl->sdl_data, n);
92 ifc->rp.linkmtu = mh->ifm_data.ifi_mtu;
93 lastlifc = &ifc->lifc;
95 if(sdl->sdl_type == IFT_ETHER && sdl->sdl_alen == 6)
96 memmove(ifc->ether, LLADDR(sdl), 6);
99 ah = (struct ifa_msghdr*)p;
100 nmh = (struct if_msghdr*)p;
101 if(nmh->ifm_type != RTM_NEWADDR)
103 p += nmh->ifm_msglen;
105 for(i=0, q=(char*)(ah+1); i<RTAX_MAX && q<p; i++){
106 if(!(ah->ifam_addrs & (1<<i)))
108 sa = (struct sockaddr*)q;
109 q += (sa->sa_len+sizeof(long)-1) & ~(sizeof(long)-1);
110 if(sa->sa_family != AF_INET)
113 lifc = mallocz(sizeof *lifc, 1);
117 lastlifc = &lifc->next;
122 ipmove(lifc->ip, ip);
125 memset(ip, 0xFF, IPv4off);
126 ipmove(lifc->mask, ip);
129 if(mh->ifm_flags & IFF_POINTOPOINT)
130 /* ipmove(lifc->remote, ip) */ ;
131 if(mh->ifm_flags & IFF_BROADCAST)
132 /* ipmove(lifc->bcast, ip) */ ;
141 maskip(lifc->ip, lifc->mask, lifc->net);