mirror of
https://github.com/dreamlayers/netbsd-mopd.git
synced 2026-04-29 21:16:54 +00:00
SIOCGIFCONF -> getifaddrs conversion
This commit is contained in:
@@ -1,4 +1,4 @@
|
|||||||
/* $NetBSD: device.c,v 1.7 2003/07/14 08:37:53 itojun Exp $ */
|
/* $NetBSD: device.c,v 1.8 2003/08/18 05:39:52 itojun Exp $ */
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Copyright (c) 1993-95 Mats O Jansson. All rights reserved.
|
* Copyright (c) 1993-95 Mats O Jansson. All rights reserved.
|
||||||
@@ -31,7 +31,7 @@
|
|||||||
|
|
||||||
#include <sys/cdefs.h>
|
#include <sys/cdefs.h>
|
||||||
#ifndef lint
|
#ifndef lint
|
||||||
__RCSID("$NetBSD: device.c,v 1.7 2003/07/14 08:37:53 itojun Exp $");
|
__RCSID("$NetBSD: device.c,v 1.8 2003/08/18 05:39:52 itojun Exp $");
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#include "os.h"
|
#include "os.h"
|
||||||
@@ -55,40 +55,25 @@ deviceEthAddr(ifname, eaddr)
|
|||||||
char *ifname;
|
char *ifname;
|
||||||
u_char *eaddr;
|
u_char *eaddr;
|
||||||
{
|
{
|
||||||
char inbuf[8192];
|
|
||||||
struct ifconf ifc;
|
|
||||||
struct ifreq *ifr;
|
|
||||||
struct sockaddr_dl *sdl;
|
struct sockaddr_dl *sdl;
|
||||||
int fd;
|
struct ifaddrs *ifap, *ifa;
|
||||||
int i, len;
|
|
||||||
|
|
||||||
/* We cannot use SIOCGIFADDR on the BPF descriptor.
|
if (getifaddrs(&ifap) != 0)
|
||||||
We must instead get all the interfaces with SIOCGIFCONF
|
mopLogErr("deviceEthAddr: getifaddrs");
|
||||||
and find the right one. */
|
|
||||||
|
|
||||||
/* Use datagram socket to get Ethernet address. */
|
for (ifa = ifap; ifa; ifa = ifa->ifa_next) {
|
||||||
if ((fd = socket(AF_INET, SOCK_DGRAM, 0)) < 0)
|
sdl = (struct sockaddr_dl *)ifa->ifa_addr;
|
||||||
mopLogErr("deviceEthAddr: socket");
|
|
||||||
|
|
||||||
ifc.ifc_len = sizeof(inbuf);
|
|
||||||
ifc.ifc_buf = inbuf;
|
|
||||||
if (ioctl(fd, SIOCGIFCONF, (caddr_t)&ifc) < 0 ||
|
|
||||||
ifc.ifc_len < sizeof(struct ifreq))
|
|
||||||
mopLogErr("deviceEthAddr: SIOGIFCONF");
|
|
||||||
ifr = ifc.ifc_req;
|
|
||||||
for (i = 0; i < ifc.ifc_len;
|
|
||||||
i += len, ifr = (struct ifreq *)((caddr_t)ifr + len)) {
|
|
||||||
len = sizeof(ifr->ifr_name) + ifr->ifr_addr.sa_len;
|
|
||||||
sdl = (struct sockaddr_dl *)&ifr->ifr_addr;
|
|
||||||
if (sdl->sdl_family != AF_LINK || sdl->sdl_type != IFT_ETHER ||
|
if (sdl->sdl_family != AF_LINK || sdl->sdl_type != IFT_ETHER ||
|
||||||
sdl->sdl_alen != 6)
|
sdl->sdl_alen != 6)
|
||||||
continue;
|
continue;
|
||||||
if (!strncmp(ifr->ifr_name, ifname, sizeof(ifr->ifr_name))) {
|
if (!strcmp(ifa->ifa_name, ifname)) {
|
||||||
memmove((caddr_t)eaddr, (caddr_t)LLADDR(sdl), 6);
|
memmove((caddr_t)eaddr, (caddr_t)LLADDR(sdl), 6);
|
||||||
|
freeifaddrs(ifap);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
freeifaddrs(ifap);
|
||||||
mopLogErrX("deviceEthAddr: Never saw interface `%s'!", ifname);
|
mopLogErrX("deviceEthAddr: Never saw interface `%s'!", ifname);
|
||||||
}
|
}
|
||||||
#endif /* DEV_NEW_CONF */
|
#endif /* DEV_NEW_CONF */
|
||||||
@@ -247,65 +232,38 @@ void
|
|||||||
deviceInitAll()
|
deviceInitAll()
|
||||||
{
|
{
|
||||||
#ifdef DEV_NEW_CONF
|
#ifdef DEV_NEW_CONF
|
||||||
char inbuf[8192];
|
|
||||||
struct ifconf ifc;
|
|
||||||
struct ifreq *ifr;
|
|
||||||
struct sockaddr_dl *sdl;
|
struct sockaddr_dl *sdl;
|
||||||
int fd;
|
struct ifaddrs *ifap, *ifa;
|
||||||
int i, len;
|
|
||||||
|
|
||||||
if ((fd = socket(AF_INET, SOCK_DGRAM, 0)) < 0)
|
if (getifaddrs(&ifap) != 0)
|
||||||
mopLogErr("deviceInitAll: socket");
|
mopLogErr("deviceInitAll: socket");
|
||||||
|
|
||||||
ifc.ifc_len = sizeof(inbuf);
|
for (ifa = ifap; ifa; ifa = ifa->ifa_next) {
|
||||||
ifc.ifc_buf = inbuf;
|
sdl = (struct sockaddr_dl *)ifa->ifa_addr;
|
||||||
if (ioctl(fd, SIOCGIFCONF, (caddr_t)&ifc) < 0 ||
|
|
||||||
ifc.ifc_len < sizeof(struct ifreq))
|
|
||||||
mopLogErr("deviceInitAll: SIOCGIFCONF");
|
|
||||||
ifr = ifc.ifc_req;
|
|
||||||
for (i = 0; i < ifc.ifc_len;
|
|
||||||
i += len, ifr = (struct ifreq *)((caddr_t)ifr + len)) {
|
|
||||||
len = sizeof(ifr->ifr_name) + ifr->ifr_addr.sa_len;
|
|
||||||
sdl = (struct sockaddr_dl *)&ifr->ifr_addr;
|
|
||||||
if (sdl->sdl_family != AF_LINK || sdl->sdl_type != IFT_ETHER ||
|
if (sdl->sdl_family != AF_LINK || sdl->sdl_type != IFT_ETHER ||
|
||||||
sdl->sdl_alen != 6)
|
sdl->sdl_alen != 6)
|
||||||
continue;
|
continue;
|
||||||
if (ioctl(fd, SIOCGIFFLAGS, (caddr_t)ifr) < 0) {
|
if ((ifa->ifa_flags &
|
||||||
mopLogWarn("deviceInitAll: SIOCGIFFLAGS");
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
if ((ifr->ifr_flags &
|
|
||||||
(IFF_UP | IFF_LOOPBACK | IFF_POINTOPOINT)) != IFF_UP)
|
(IFF_UP | IFF_LOOPBACK | IFF_POINTOPOINT)) != IFF_UP)
|
||||||
continue;
|
continue;
|
||||||
deviceInitOne(ifr->ifr_name);
|
deviceInitOne(ifa->ifa_name);
|
||||||
}
|
}
|
||||||
(void) close(fd);
|
|
||||||
#else
|
|
||||||
int fd;
|
|
||||||
int n;
|
|
||||||
struct ifreq ibuf[8], *ifrp;
|
|
||||||
struct ifconf ifc;
|
|
||||||
|
|
||||||
if ((fd = socket(AF_INET, SOCK_DGRAM, 0)) < 0)
|
freeifaddrs(ifap);
|
||||||
|
#else
|
||||||
|
struct ifaddrs *ifap, *ifa;
|
||||||
|
|
||||||
|
if (getifaddrs(&ifap) != 0)
|
||||||
mopLogErr("deviceInitAll: old socket");
|
mopLogErr("deviceInitAll: old socket");
|
||||||
ifc.ifc_len = sizeof ibuf;
|
|
||||||
ifc.ifc_buf = (caddr_t)ibuf;
|
for (ifa = ifap; ifa; ifa = ifa->ifa_next) {
|
||||||
if (ioctl(fd, SIOCGIFCONF, (char *)&ifc) < 0 ||
|
if (/*(ifa->ifa_flags & IFF_UP) == 0 ||*/
|
||||||
ifc.ifc_len < sizeof(struct ifreq))
|
ifa->ifa_flags & IFF_LOOPBACK ||
|
||||||
mopLogErr("deviceInitAll: old SIOCGIFCONF");
|
ifa->ifa_flags & IFF_POINTOPOINT)
|
||||||
ifrp = ibuf;
|
|
||||||
n = ifc.ifc_len / sizeof(*ifrp);
|
|
||||||
for (; --n >= 0; ++ifrp) {
|
|
||||||
if (ioctl(fd, SIOCGIFFLAGS, (char *)ifrp) < 0) {
|
|
||||||
continue;
|
continue;
|
||||||
}
|
deviceInitOne(ifa->ifa_name);
|
||||||
if (/*(ifrp->ifr_flags & IFF_UP) == 0 ||*/
|
|
||||||
ifrp->ifr_flags & IFF_LOOPBACK ||
|
|
||||||
ifrp->ifr_flags & IFF_POINTOPOINT)
|
|
||||||
continue;
|
|
||||||
deviceInitOne(ifrp->ifr_name);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
(void) close(fd);
|
freeifaddrs(ifap);
|
||||||
#endif /* DEV_NEW_CONF */
|
#endif /* DEV_NEW_CONF */
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
/* $NetBSD: os.h,v 1.4 2001/01/16 02:50:31 cgd Exp $ */
|
/* $NetBSD: os.h,v 1.5 2003/08/18 05:39:52 itojun Exp $ */
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Copyright (c) 1994-95 Mats O Jansson. All rights reserved.
|
* Copyright (c) 1994-95 Mats O Jansson. All rights reserved.
|
||||||
@@ -28,7 +28,7 @@
|
|||||||
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
||||||
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||||
*
|
*
|
||||||
* $NetBSD: os.h,v 1.4 2001/01/16 02:50:31 cgd Exp $
|
* $NetBSD: os.h,v 1.5 2003/08/18 05:39:52 itojun Exp $
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
|
|
||||||
@@ -55,6 +55,7 @@
|
|||||||
#include <syslog.h>
|
#include <syslog.h>
|
||||||
#include <time.h>
|
#include <time.h>
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
|
#include <ifaddrs.h>
|
||||||
|
|
||||||
#define DEV_NEW_CONF
|
#define DEV_NEW_CONF
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user