2011-11-13 17:30:38 +01:00
|
|
|
/* Copyright (C) 2003,2006,2011 Juan Lang
|
2003-05-13 05:32:20 +02:00
|
|
|
*
|
|
|
|
* This library is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
* version 2.1 of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This library is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* Lesser General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
|
|
|
* License along with this library; if not, write to the Free Software
|
2006-05-18 14:49:52 +02:00
|
|
|
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
|
2003-05-13 05:32:20 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include "config.h"
|
|
|
|
|
2003-09-06 01:08:26 +02:00
|
|
|
#include <stdarg.h>
|
2003-05-13 05:32:20 +02:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
2003-08-08 23:07:23 +02:00
|
|
|
#ifdef HAVE_UNISTD_H
|
2003-05-13 05:32:20 +02:00
|
|
|
#include <unistd.h>
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#include <sys/types.h>
|
2006-10-22 21:15:46 +02:00
|
|
|
#ifdef HAVE_SYS_PARAM_H
|
|
|
|
#include <sys/param.h>
|
|
|
|
#endif
|
2003-05-13 05:32:20 +02:00
|
|
|
|
2003-08-08 23:07:23 +02:00
|
|
|
#ifdef HAVE_SYS_SOCKET_H
|
2003-05-13 05:32:20 +02:00
|
|
|
#include <sys/socket.h>
|
|
|
|
#endif
|
|
|
|
|
2003-08-08 23:07:23 +02:00
|
|
|
#ifdef HAVE_NET_IF_H
|
2003-05-13 05:32:20 +02:00
|
|
|
#include <net/if.h>
|
|
|
|
#endif
|
|
|
|
|
2003-08-08 23:07:23 +02:00
|
|
|
#ifdef HAVE_NET_IF_ARP_H
|
2003-05-13 05:32:20 +02:00
|
|
|
#include <net/if_arp.h>
|
|
|
|
#endif
|
|
|
|
|
2003-08-08 23:07:23 +02:00
|
|
|
#ifdef HAVE_NET_ROUTE_H
|
2003-05-13 05:32:20 +02:00
|
|
|
#include <net/route.h>
|
|
|
|
#endif
|
|
|
|
|
2003-08-08 23:07:23 +02:00
|
|
|
#ifdef HAVE_SYS_IOCTL_H
|
2003-05-13 05:32:20 +02:00
|
|
|
#include <sys/ioctl.h>
|
|
|
|
#endif
|
|
|
|
|
2003-08-08 23:07:23 +02:00
|
|
|
#ifdef HAVE_SYS_SYSCTL_H
|
2003-05-13 05:32:20 +02:00
|
|
|
#include <sys/sysctl.h>
|
|
|
|
#endif
|
|
|
|
|
2003-08-08 23:07:23 +02:00
|
|
|
#ifdef HAVE_SYS_SOCKIO_H
|
2003-05-13 05:32:20 +02:00
|
|
|
#include <sys/sockio.h>
|
|
|
|
#endif
|
|
|
|
|
2003-08-08 23:07:23 +02:00
|
|
|
#ifdef HAVE_NET_IF_DL_H
|
2003-05-13 05:32:20 +02:00
|
|
|
#include <net/if_dl.h>
|
|
|
|
#endif
|
|
|
|
|
2003-08-08 23:07:23 +02:00
|
|
|
#ifdef HAVE_NET_IF_TYPES_H
|
2003-05-13 05:32:20 +02:00
|
|
|
#include <net/if_types.h>
|
|
|
|
#endif
|
|
|
|
|
2021-02-21 14:05:46 +01:00
|
|
|
#ifdef HAVE_NETINET_IN_H
|
|
|
|
#include <netinet/in.h>
|
|
|
|
#endif
|
|
|
|
|
2010-03-12 03:36:46 +01:00
|
|
|
#ifdef HAVE_IFADDRS_H
|
|
|
|
#include <ifaddrs.h>
|
|
|
|
#endif
|
|
|
|
|
2013-09-19 16:39:21 +02:00
|
|
|
#ifdef HAVE_LINUX_RTNETLINK_H
|
|
|
|
#include <linux/rtnetlink.h>
|
|
|
|
#endif
|
|
|
|
|
2003-05-13 05:32:20 +02:00
|
|
|
#include "ifenum.h"
|
2010-03-12 03:36:46 +01:00
|
|
|
#include "ws2ipdef.h"
|
2003-05-13 05:32:20 +02:00
|
|
|
|
2003-08-08 23:07:23 +02:00
|
|
|
#ifdef HAVE_STRUCT_SOCKADDR_SA_LEN
|
2003-05-13 05:32:20 +02:00
|
|
|
#define ifreq_len(ifr) \
|
|
|
|
max(sizeof(struct ifreq), sizeof((ifr)->ifr_name)+(ifr)->ifr_addr.sa_len)
|
|
|
|
#else
|
|
|
|
#define ifreq_len(ifr) sizeof(struct ifreq)
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifndef ETH_ALEN
|
|
|
|
#define ETH_ALEN 6
|
|
|
|
#endif
|
|
|
|
|
2006-02-24 11:27:45 +01:00
|
|
|
#ifndef IF_NAMESIZE
|
|
|
|
#define IF_NAMESIZE 16
|
|
|
|
#endif
|
|
|
|
|
2003-05-13 05:32:20 +02:00
|
|
|
#ifndef INADDR_NONE
|
|
|
|
#define INADDR_NONE (~0U)
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#define INITIAL_INTERFACES_ASSUMED 4
|
|
|
|
|
|
|
|
/* Functions */
|
|
|
|
|
2013-10-04 23:32:15 +02:00
|
|
|
static BOOL isLoopbackInterface(int fd, const char *name)
|
2003-05-13 05:32:20 +02:00
|
|
|
{
|
2013-10-04 23:32:15 +02:00
|
|
|
BOOL ret = FALSE;
|
2003-05-13 05:32:20 +02:00
|
|
|
|
|
|
|
if (name) {
|
|
|
|
struct ifreq ifr;
|
|
|
|
|
2005-03-28 16:17:51 +02:00
|
|
|
lstrcpynA(ifr.ifr_name, name, IFNAMSIZ);
|
2003-05-13 05:32:20 +02:00
|
|
|
if (ioctl(fd, SIOCGIFFLAGS, &ifr) == 0)
|
|
|
|
ret = ifr.ifr_flags & IFF_LOOPBACK;
|
|
|
|
}
|
2013-10-04 23:32:15 +02:00
|
|
|
return !!ret;
|
2003-05-13 05:32:20 +02:00
|
|
|
}
|
|
|
|
|
2006-01-27 19:19:32 +01:00
|
|
|
/* The comments say MAX_ADAPTER_NAME is required, but really only IF_NAMESIZE
|
|
|
|
* bytes are necessary.
|
2003-05-13 05:32:20 +02:00
|
|
|
*/
|
2011-12-15 10:06:24 +01:00
|
|
|
char *getInterfaceNameByIndex(IF_INDEX index, char *name)
|
2003-05-13 05:32:20 +02:00
|
|
|
{
|
2006-01-27 19:19:32 +01:00
|
|
|
return if_indextoname(index, name);
|
2003-05-13 05:32:20 +02:00
|
|
|
}
|
|
|
|
|
2011-12-15 10:06:24 +01:00
|
|
|
DWORD getInterfaceIndexByName(const char *name, IF_INDEX *index)
|
2003-05-13 05:32:20 +02:00
|
|
|
{
|
2006-01-27 19:19:32 +01:00
|
|
|
DWORD ret;
|
|
|
|
unsigned int idx;
|
2003-05-13 05:32:20 +02:00
|
|
|
|
2006-01-27 19:19:32 +01:00
|
|
|
if (!name)
|
|
|
|
return ERROR_INVALID_PARAMETER;
|
|
|
|
if (!index)
|
|
|
|
return ERROR_INVALID_PARAMETER;
|
|
|
|
idx = if_nametoindex(name);
|
|
|
|
if (idx) {
|
|
|
|
*index = idx;
|
|
|
|
ret = NO_ERROR;
|
2003-05-13 05:32:20 +02:00
|
|
|
}
|
2006-01-27 19:19:32 +01:00
|
|
|
else
|
|
|
|
ret = ERROR_INVALID_DATA;
|
|
|
|
return ret;
|
2003-05-13 05:32:20 +02:00
|
|
|
}
|
|
|
|
|
2010-03-23 22:56:34 +01:00
|
|
|
BOOL isIfIndexLoopback(ULONG idx)
|
|
|
|
{
|
|
|
|
BOOL ret = FALSE;
|
|
|
|
char name[IFNAMSIZ];
|
|
|
|
int fd;
|
|
|
|
|
|
|
|
getInterfaceNameByIndex(idx, name);
|
|
|
|
fd = socket(PF_INET, SOCK_DGRAM, 0);
|
|
|
|
if (fd != -1) {
|
|
|
|
ret = isLoopbackInterface(fd, name);
|
|
|
|
close(fd);
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2013-09-19 16:39:21 +02:00
|
|
|
#ifdef HAVE_IF_NAMEINDEX
|
2013-09-19 16:39:20 +02:00
|
|
|
DWORD get_interface_indices( BOOL skip_loopback, InterfaceIndexTable **table )
|
2003-05-13 05:32:20 +02:00
|
|
|
{
|
2013-09-19 16:39:20 +02:00
|
|
|
DWORD count = 0, i;
|
|
|
|
struct if_nameindex *p, *indices = if_nameindex();
|
|
|
|
InterfaceIndexTable *ret;
|
2003-05-13 05:32:20 +02:00
|
|
|
|
2013-09-19 16:39:20 +02:00
|
|
|
if (table) *table = NULL;
|
|
|
|
if (!indices) return 0;
|
2003-05-13 05:32:20 +02:00
|
|
|
|
2013-09-19 16:39:20 +02:00
|
|
|
for (p = indices; p->if_name; p++)
|
|
|
|
{
|
|
|
|
if (skip_loopback && isIfIndexLoopback( p->if_index )) continue;
|
|
|
|
count++;
|
2006-01-27 19:19:32 +01:00
|
|
|
}
|
2003-05-13 05:32:20 +02:00
|
|
|
|
2013-09-19 16:39:20 +02:00
|
|
|
if (table)
|
|
|
|
{
|
|
|
|
ret = HeapAlloc( GetProcessHeap(), 0, FIELD_OFFSET(InterfaceIndexTable, indexes[count]) );
|
|
|
|
if (!ret)
|
|
|
|
{
|
|
|
|
count = 0;
|
|
|
|
goto end;
|
|
|
|
}
|
|
|
|
for (p = indices, i = 0; p->if_name && i < count; p++)
|
|
|
|
{
|
|
|
|
if (skip_loopback && isIfIndexLoopback( p->if_index )) continue;
|
|
|
|
ret->indexes[i++] = p->if_index;
|
|
|
|
}
|
|
|
|
ret->numIndexes = count = i;
|
|
|
|
*table = ret;
|
2006-01-27 19:19:32 +01:00
|
|
|
}
|
2013-09-19 16:39:20 +02:00
|
|
|
|
|
|
|
end:
|
|
|
|
if_freenameindex( indices );
|
|
|
|
return count;
|
2003-05-13 05:32:20 +02:00
|
|
|
}
|
|
|
|
|
2013-09-19 16:39:21 +02:00
|
|
|
#elif defined(HAVE_LINUX_RTNETLINK_H)
|
|
|
|
static int open_netlink( int *pid )
|
|
|
|
{
|
|
|
|
int fd = socket( AF_NETLINK, SOCK_RAW, NETLINK_ROUTE );
|
|
|
|
struct sockaddr_nl addr;
|
|
|
|
socklen_t len;
|
|
|
|
|
|
|
|
if (fd < 0) return fd;
|
|
|
|
|
|
|
|
memset( &addr, 0, sizeof(addr) );
|
|
|
|
addr.nl_family = AF_NETLINK;
|
|
|
|
|
|
|
|
if (bind( fd, (struct sockaddr *)&addr, sizeof(addr) ) < 0)
|
|
|
|
goto fail;
|
|
|
|
|
|
|
|
len = sizeof(addr);
|
|
|
|
if (getsockname( fd, (struct sockaddr *)&addr, &len ) < 0)
|
|
|
|
goto fail;
|
|
|
|
|
|
|
|
*pid = addr.nl_pid;
|
|
|
|
return fd;
|
|
|
|
fail:
|
|
|
|
close( fd );
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int send_netlink_req( int fd, int pid, int type, int *seq_no )
|
|
|
|
{
|
|
|
|
static LONG seq;
|
|
|
|
struct request
|
|
|
|
{
|
|
|
|
struct nlmsghdr hdr;
|
|
|
|
struct rtgenmsg gen;
|
|
|
|
} req;
|
|
|
|
struct sockaddr_nl addr;
|
|
|
|
|
|
|
|
req.hdr.nlmsg_len = sizeof(req);
|
|
|
|
req.hdr.nlmsg_type = type;
|
|
|
|
req.hdr.nlmsg_flags = NLM_F_ROOT | NLM_F_MATCH | NLM_F_REQUEST;
|
|
|
|
req.hdr.nlmsg_pid = pid;
|
|
|
|
req.hdr.nlmsg_seq = InterlockedIncrement( &seq );
|
|
|
|
req.gen.rtgen_family = AF_UNSPEC;
|
|
|
|
|
|
|
|
memset( &addr, 0, sizeof(addr) );
|
|
|
|
addr.nl_family = AF_NETLINK;
|
|
|
|
if (sendto( fd, &req, sizeof(req), 0, (struct sockaddr *)&addr, sizeof(addr) ) != sizeof(req))
|
|
|
|
return -1;
|
|
|
|
*seq_no = req.hdr.nlmsg_seq;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
struct netlink_reply
|
|
|
|
{
|
|
|
|
struct netlink_reply *next;
|
|
|
|
int size;
|
|
|
|
struct nlmsghdr *hdr;
|
|
|
|
};
|
|
|
|
|
|
|
|
static void free_netlink_reply( struct netlink_reply *data )
|
|
|
|
{
|
|
|
|
struct netlink_reply *ptr;
|
|
|
|
while( data )
|
|
|
|
{
|
|
|
|
ptr = data->next;
|
|
|
|
HeapFree( GetProcessHeap(), 0, data );
|
|
|
|
data = ptr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static int recv_netlink_reply( int fd, int pid, int seq, struct netlink_reply **data )
|
|
|
|
{
|
|
|
|
int bufsize = getpagesize();
|
2013-10-04 23:32:15 +02:00
|
|
|
int left, read;
|
|
|
|
BOOL done = FALSE;
|
2013-09-19 16:39:21 +02:00
|
|
|
socklen_t sa_len;
|
|
|
|
struct sockaddr_nl addr;
|
|
|
|
struct netlink_reply *cur, *last = NULL;
|
|
|
|
struct nlmsghdr *hdr;
|
|
|
|
char *buf;
|
|
|
|
|
|
|
|
*data = NULL;
|
|
|
|
buf = HeapAlloc( GetProcessHeap(), 0, bufsize );
|
|
|
|
if (!buf) return -1;
|
|
|
|
|
|
|
|
do
|
|
|
|
{
|
|
|
|
left = read = recvfrom( fd, buf, bufsize, 0, (struct sockaddr *)&addr, &sa_len );
|
|
|
|
if (read < 0) goto fail;
|
|
|
|
if (addr.nl_pid != 0) continue; /* not from kernel */
|
|
|
|
|
|
|
|
for (hdr = (struct nlmsghdr *)buf; NLMSG_OK(hdr, left); hdr = NLMSG_NEXT(hdr, left))
|
|
|
|
{
|
|
|
|
if (hdr->nlmsg_pid != pid || hdr->nlmsg_seq != seq) continue;
|
|
|
|
if (hdr->nlmsg_type == NLMSG_DONE)
|
|
|
|
{
|
2013-10-04 23:32:15 +02:00
|
|
|
done = TRUE;
|
2013-09-19 16:39:21 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
cur = HeapAlloc( GetProcessHeap(), 0, sizeof(*cur) + read );
|
|
|
|
if (!cur) goto fail;
|
|
|
|
cur->next = NULL;
|
|
|
|
cur->size = read;
|
|
|
|
cur->hdr = (struct nlmsghdr *)(cur + 1);
|
|
|
|
memcpy( cur->hdr, buf, read );
|
|
|
|
if (last) last->next = cur;
|
|
|
|
else *data = cur;
|
|
|
|
last = cur;
|
|
|
|
} while (!done);
|
|
|
|
|
|
|
|
HeapFree( GetProcessHeap(), 0, buf );
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
fail:
|
|
|
|
free_netlink_reply( *data );
|
|
|
|
HeapFree( GetProcessHeap(), 0, buf );
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static DWORD get_indices_from_reply( struct netlink_reply *reply, int pid, int seq,
|
|
|
|
BOOL skip_loopback, InterfaceIndexTable *table )
|
|
|
|
{
|
|
|
|
struct nlmsghdr *hdr;
|
|
|
|
struct netlink_reply *r;
|
|
|
|
int count = 0;
|
|
|
|
|
|
|
|
for (r = reply; r; r = r->next)
|
|
|
|
{
|
|
|
|
int size = r->size;
|
|
|
|
for (hdr = r->hdr; NLMSG_OK(hdr, size); hdr = NLMSG_NEXT(hdr, size))
|
|
|
|
{
|
|
|
|
if (hdr->nlmsg_pid != pid || hdr->nlmsg_seq != seq) continue;
|
|
|
|
if (hdr->nlmsg_type == NLMSG_DONE) break;
|
|
|
|
|
|
|
|
if (hdr->nlmsg_type == RTM_NEWLINK)
|
|
|
|
{
|
|
|
|
struct ifinfomsg *info = NLMSG_DATA(hdr);
|
|
|
|
|
|
|
|
if (skip_loopback && (info->ifi_flags & IFF_LOOPBACK)) continue;
|
|
|
|
if (table) table->indexes[count] = info->ifi_index;
|
|
|
|
count++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return count;
|
|
|
|
}
|
|
|
|
|
|
|
|
DWORD get_interface_indices( BOOL skip_loopback, InterfaceIndexTable **table )
|
|
|
|
{
|
|
|
|
int fd, pid, seq;
|
|
|
|
struct netlink_reply *reply = NULL;
|
|
|
|
DWORD count = 0;
|
|
|
|
|
|
|
|
if (table) *table = NULL;
|
|
|
|
fd = open_netlink( &pid );
|
|
|
|
if (fd < 0) return 0;
|
|
|
|
|
|
|
|
if (send_netlink_req( fd, pid, RTM_GETLINK, &seq ) < 0)
|
|
|
|
goto end;
|
|
|
|
|
|
|
|
if (recv_netlink_reply( fd, pid, seq, &reply ) < 0)
|
|
|
|
goto end;
|
|
|
|
|
|
|
|
count = get_indices_from_reply( reply, pid, seq, skip_loopback, NULL );
|
|
|
|
|
|
|
|
if (table)
|
|
|
|
{
|
|
|
|
InterfaceIndexTable *ret = HeapAlloc( GetProcessHeap(), 0, FIELD_OFFSET(InterfaceIndexTable, indexes[count]) );
|
|
|
|
if (!ret)
|
|
|
|
{
|
|
|
|
count = 0;
|
|
|
|
goto end;
|
|
|
|
}
|
|
|
|
|
|
|
|
ret->numIndexes = count;
|
|
|
|
get_indices_from_reply( reply, pid, seq, skip_loopback, ret );
|
|
|
|
*table = ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
end:
|
|
|
|
free_netlink_reply( reply );
|
|
|
|
close( fd );
|
|
|
|
return count;
|
|
|
|
}
|
|
|
|
|
|
|
|
#else
|
|
|
|
DWORD get_interface_indices( BOOL skip_loopback, InterfaceIndexTable **table )
|
|
|
|
{
|
|
|
|
if (table) *table = NULL;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2006-01-25 13:14:12 +01:00
|
|
|
static DWORD getInterfaceBCastAddrByName(const char *name)
|
2003-05-13 05:32:20 +02:00
|
|
|
{
|
|
|
|
DWORD ret = INADDR_ANY;
|
|
|
|
|
|
|
|
if (name) {
|
|
|
|
int fd = socket(PF_INET, SOCK_DGRAM, 0);
|
|
|
|
|
|
|
|
if (fd != -1) {
|
|
|
|
struct ifreq ifr;
|
|
|
|
|
2005-03-28 16:17:51 +02:00
|
|
|
lstrcpynA(ifr.ifr_name, name, IFNAMSIZ);
|
2003-05-13 05:32:20 +02:00
|
|
|
if (ioctl(fd, SIOCGIFBRDADDR, &ifr) == 0)
|
|
|
|
memcpy(&ret, ifr.ifr_addr.sa_data + 2, sizeof(DWORD));
|
|
|
|
close(fd);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2006-01-25 13:14:12 +01:00
|
|
|
static DWORD getInterfaceMaskByName(const char *name)
|
2003-05-13 05:32:20 +02:00
|
|
|
{
|
|
|
|
DWORD ret = INADDR_NONE;
|
|
|
|
|
|
|
|
if (name) {
|
|
|
|
int fd = socket(PF_INET, SOCK_DGRAM, 0);
|
|
|
|
|
|
|
|
if (fd != -1) {
|
|
|
|
struct ifreq ifr;
|
|
|
|
|
2005-03-28 16:17:51 +02:00
|
|
|
lstrcpynA(ifr.ifr_name, name, IFNAMSIZ);
|
2003-05-13 05:32:20 +02:00
|
|
|
if (ioctl(fd, SIOCGIFNETMASK, &ifr) == 0)
|
|
|
|
memcpy(&ret, ifr.ifr_addr.sa_data + 2, sizeof(DWORD));
|
|
|
|
close(fd);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2010-03-12 19:38:46 +01:00
|
|
|
#if defined (SIOCGIFHWADDR) && defined (HAVE_STRUCT_IFREQ_IFR_HWADDR)
|
2009-05-13 10:35:50 +02:00
|
|
|
static DWORD getInterfacePhysicalByName(const char *name, PDWORD len, PBYTE addr,
|
2003-05-13 05:32:20 +02:00
|
|
|
PDWORD type)
|
|
|
|
{
|
|
|
|
DWORD ret;
|
|
|
|
int fd;
|
|
|
|
|
|
|
|
if (!name || !len || !addr || !type)
|
|
|
|
return ERROR_INVALID_PARAMETER;
|
|
|
|
|
|
|
|
fd = socket(PF_INET, SOCK_DGRAM, 0);
|
|
|
|
if (fd != -1) {
|
|
|
|
struct ifreq ifr;
|
|
|
|
|
|
|
|
memset(&ifr, 0, sizeof(struct ifreq));
|
2005-03-28 16:17:51 +02:00
|
|
|
lstrcpynA(ifr.ifr_name, name, IFNAMSIZ);
|
2003-05-13 05:32:20 +02:00
|
|
|
if ((ioctl(fd, SIOCGIFHWADDR, &ifr)))
|
|
|
|
ret = ERROR_INVALID_DATA;
|
|
|
|
else {
|
2004-09-08 03:23:57 +02:00
|
|
|
unsigned int addrLen;
|
2003-05-13 05:32:20 +02:00
|
|
|
|
|
|
|
switch (ifr.ifr_hwaddr.sa_family)
|
|
|
|
{
|
2003-10-14 07:27:43 +02:00
|
|
|
#ifdef ARPHRD_LOOPBACK
|
2003-05-13 05:32:20 +02:00
|
|
|
case ARPHRD_LOOPBACK:
|
|
|
|
addrLen = 0;
|
|
|
|
*type = MIB_IF_TYPE_LOOPBACK;
|
|
|
|
break;
|
2003-10-14 07:27:43 +02:00
|
|
|
#endif
|
|
|
|
#ifdef ARPHRD_ETHER
|
2003-05-13 05:32:20 +02:00
|
|
|
case ARPHRD_ETHER:
|
|
|
|
addrLen = ETH_ALEN;
|
|
|
|
*type = MIB_IF_TYPE_ETHERNET;
|
|
|
|
break;
|
2003-10-14 07:27:43 +02:00
|
|
|
#endif
|
|
|
|
#ifdef ARPHRD_FDDI
|
2003-05-13 05:32:20 +02:00
|
|
|
case ARPHRD_FDDI:
|
|
|
|
addrLen = ETH_ALEN;
|
|
|
|
*type = MIB_IF_TYPE_FDDI;
|
|
|
|
break;
|
2003-10-14 07:27:43 +02:00
|
|
|
#endif
|
|
|
|
#ifdef ARPHRD_IEEE802
|
2003-05-13 05:32:20 +02:00
|
|
|
case ARPHRD_IEEE802: /* 802.2 Ethernet && Token Ring, guess TR? */
|
|
|
|
addrLen = ETH_ALEN;
|
|
|
|
*type = MIB_IF_TYPE_TOKENRING;
|
|
|
|
break;
|
2003-10-14 07:27:43 +02:00
|
|
|
#endif
|
|
|
|
#ifdef ARPHRD_IEEE802_TR
|
2003-05-13 05:32:20 +02:00
|
|
|
case ARPHRD_IEEE802_TR: /* also Token Ring? */
|
|
|
|
addrLen = ETH_ALEN;
|
|
|
|
*type = MIB_IF_TYPE_TOKENRING;
|
|
|
|
break;
|
2003-10-14 07:27:43 +02:00
|
|
|
#endif
|
|
|
|
#ifdef ARPHRD_SLIP
|
2003-05-13 05:32:20 +02:00
|
|
|
case ARPHRD_SLIP:
|
|
|
|
addrLen = 0;
|
|
|
|
*type = MIB_IF_TYPE_SLIP;
|
|
|
|
break;
|
2003-10-14 07:27:43 +02:00
|
|
|
#endif
|
|
|
|
#ifdef ARPHRD_PPP
|
2003-05-13 05:32:20 +02:00
|
|
|
case ARPHRD_PPP:
|
|
|
|
addrLen = 0;
|
|
|
|
*type = MIB_IF_TYPE_PPP;
|
|
|
|
break;
|
2003-10-14 07:27:43 +02:00
|
|
|
#endif
|
2003-05-13 05:32:20 +02:00
|
|
|
default:
|
|
|
|
addrLen = min(MAX_INTERFACE_PHYSADDR, sizeof(ifr.ifr_hwaddr.sa_data));
|
|
|
|
*type = MIB_IF_TYPE_OTHER;
|
|
|
|
}
|
|
|
|
if (addrLen > *len) {
|
|
|
|
ret = ERROR_INSUFFICIENT_BUFFER;
|
|
|
|
*len = addrLen;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
if (addrLen > 0)
|
|
|
|
memcpy(addr, ifr.ifr_hwaddr.sa_data, addrLen);
|
|
|
|
/* zero out remaining bytes for broken implementations */
|
|
|
|
memset(addr + addrLen, 0, *len - addrLen);
|
|
|
|
*len = addrLen;
|
|
|
|
ret = NO_ERROR;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
close(fd);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
ret = ERROR_NO_MORE_FILES;
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
#elif defined (SIOCGARP)
|
2009-05-13 10:35:50 +02:00
|
|
|
static DWORD getInterfacePhysicalByName(const char *name, PDWORD len, PBYTE addr,
|
2003-05-13 05:32:20 +02:00
|
|
|
PDWORD type)
|
|
|
|
{
|
|
|
|
DWORD ret;
|
|
|
|
int fd;
|
|
|
|
|
|
|
|
if (!name || !len || !addr || !type)
|
|
|
|
return ERROR_INVALID_PARAMETER;
|
|
|
|
|
|
|
|
fd = socket(PF_INET, SOCK_DGRAM, 0);
|
|
|
|
if (fd != -1) {
|
|
|
|
if (isLoopbackInterface(fd, name)) {
|
|
|
|
*type = MIB_IF_TYPE_LOOPBACK;
|
|
|
|
memset(addr, 0, *len);
|
|
|
|
*len = 0;
|
2003-07-09 23:55:09 +02:00
|
|
|
ret=NOERROR;
|
2003-05-13 05:32:20 +02:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
struct arpreq arp;
|
|
|
|
struct sockaddr_in *saddr;
|
2006-01-25 13:14:12 +01:00
|
|
|
struct ifreq ifr;
|
2003-05-13 05:32:20 +02:00
|
|
|
|
2006-01-25 13:14:12 +01:00
|
|
|
/* get IP addr */
|
|
|
|
lstrcpynA(ifr.ifr_name, name, IFNAMSIZ);
|
|
|
|
ioctl(fd, SIOCGIFADDR, &ifr);
|
2003-05-13 05:32:20 +02:00
|
|
|
memset(&arp, 0, sizeof(struct arpreq));
|
|
|
|
arp.arp_pa.sa_family = AF_INET;
|
|
|
|
saddr = (struct sockaddr_in *)&arp; /* proto addr is first member */
|
|
|
|
saddr->sin_family = AF_INET;
|
2006-01-25 13:14:12 +01:00
|
|
|
memcpy(&saddr->sin_addr.s_addr, ifr.ifr_addr.sa_data + 2, sizeof(DWORD));
|
2003-05-13 05:32:20 +02:00
|
|
|
if ((ioctl(fd, SIOCGARP, &arp)))
|
|
|
|
ret = ERROR_INVALID_DATA;
|
|
|
|
else {
|
|
|
|
/* FIXME: heh: who said it was ethernet? */
|
|
|
|
int addrLen = ETH_ALEN;
|
|
|
|
|
|
|
|
if (addrLen > *len) {
|
|
|
|
ret = ERROR_INSUFFICIENT_BUFFER;
|
|
|
|
*len = addrLen;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
if (addrLen > 0)
|
|
|
|
memcpy(addr, &arp.arp_ha.sa_data[0], addrLen);
|
|
|
|
/* zero out remaining bytes for broken implementations */
|
|
|
|
memset(addr + addrLen, 0, *len - addrLen);
|
|
|
|
*len = addrLen;
|
|
|
|
*type = MIB_IF_TYPE_ETHERNET;
|
|
|
|
ret = NO_ERROR;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2003-07-09 23:55:09 +02:00
|
|
|
close(fd);
|
|
|
|
}
|
2003-05-13 05:32:20 +02:00
|
|
|
else
|
|
|
|
ret = ERROR_NO_MORE_FILES;
|
2003-07-09 23:55:09 +02:00
|
|
|
|
2003-05-13 05:32:20 +02:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
#elif defined (HAVE_SYS_SYSCTL_H) && defined (HAVE_NET_IF_DL_H)
|
2009-05-13 10:35:50 +02:00
|
|
|
static DWORD getInterfacePhysicalByName(const char *name, PDWORD len, PBYTE addr,
|
2003-05-13 05:32:20 +02:00
|
|
|
PDWORD type)
|
|
|
|
{
|
|
|
|
DWORD ret;
|
|
|
|
struct if_msghdr *ifm;
|
|
|
|
struct sockaddr_dl *sdl;
|
|
|
|
u_char *p, *buf;
|
|
|
|
size_t mibLen;
|
|
|
|
int mib[] = { CTL_NET, AF_ROUTE, 0, AF_LINK, NET_RT_IFLIST, 0 };
|
2011-05-22 20:54:45 +02:00
|
|
|
unsigned addrLen;
|
2003-05-13 05:32:20 +02:00
|
|
|
BOOL found = FALSE;
|
|
|
|
|
|
|
|
if (!name || !len || !addr || !type)
|
|
|
|
return ERROR_INVALID_PARAMETER;
|
|
|
|
|
2003-05-21 20:26:00 +02:00
|
|
|
if (sysctl(mib, 6, NULL, &mibLen, NULL, 0) < 0)
|
2003-05-13 05:32:20 +02:00
|
|
|
return ERROR_NO_MORE_FILES;
|
|
|
|
|
2005-03-24 22:01:35 +01:00
|
|
|
buf = HeapAlloc(GetProcessHeap(), 0, mibLen);
|
2003-05-13 05:32:20 +02:00
|
|
|
if (!buf)
|
|
|
|
return ERROR_NOT_ENOUGH_MEMORY;
|
|
|
|
|
|
|
|
if (sysctl(mib, 6, buf, &mibLen, NULL, 0) < 0) {
|
2004-12-13 14:21:39 +01:00
|
|
|
HeapFree(GetProcessHeap(), 0, buf);
|
2003-05-13 05:32:20 +02:00
|
|
|
return ERROR_NO_MORE_FILES;
|
|
|
|
}
|
|
|
|
|
|
|
|
ret = ERROR_INVALID_DATA;
|
|
|
|
for (p = buf; !found && p < buf + mibLen; p += ifm->ifm_msglen) {
|
|
|
|
ifm = (struct if_msghdr *)p;
|
|
|
|
sdl = (struct sockaddr_dl *)(ifm + 1);
|
|
|
|
|
|
|
|
if (ifm->ifm_type != RTM_IFINFO || (ifm->ifm_addrs & RTA_IFP) == 0)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if (sdl->sdl_family != AF_LINK || sdl->sdl_nlen == 0 ||
|
|
|
|
memcmp(sdl->sdl_data, name, max(sdl->sdl_nlen, strlen(name))) != 0)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
found = TRUE;
|
|
|
|
addrLen = min(MAX_INTERFACE_PHYSADDR, sdl->sdl_alen);
|
|
|
|
if (addrLen > *len) {
|
|
|
|
ret = ERROR_INSUFFICIENT_BUFFER;
|
|
|
|
*len = addrLen;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
if (addrLen > 0)
|
|
|
|
memcpy(addr, LLADDR(sdl), addrLen);
|
|
|
|
/* zero out remaining bytes for broken implementations */
|
|
|
|
memset(addr + addrLen, 0, *len - addrLen);
|
|
|
|
*len = addrLen;
|
|
|
|
#if defined(HAVE_NET_IF_TYPES_H)
|
|
|
|
switch (sdl->sdl_type)
|
|
|
|
{
|
|
|
|
case IFT_ETHER:
|
|
|
|
*type = MIB_IF_TYPE_ETHERNET;
|
|
|
|
break;
|
|
|
|
case IFT_FDDI:
|
|
|
|
*type = MIB_IF_TYPE_FDDI;
|
|
|
|
break;
|
|
|
|
case IFT_ISO88024: /* Token Bus */
|
|
|
|
*type = MIB_IF_TYPE_TOKENRING;
|
|
|
|
break;
|
|
|
|
case IFT_ISO88025: /* Token Ring */
|
|
|
|
*type = MIB_IF_TYPE_TOKENRING;
|
|
|
|
break;
|
|
|
|
case IFT_PPP:
|
|
|
|
*type = MIB_IF_TYPE_PPP;
|
|
|
|
break;
|
|
|
|
case IFT_SLIP:
|
|
|
|
*type = MIB_IF_TYPE_SLIP;
|
|
|
|
break;
|
|
|
|
case IFT_LOOP:
|
|
|
|
*type = MIB_IF_TYPE_LOOPBACK;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
*type = MIB_IF_TYPE_OTHER;
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
/* default if we don't know */
|
|
|
|
*type = MIB_IF_TYPE_ETHERNET;
|
|
|
|
#endif
|
|
|
|
ret = NO_ERROR;
|
|
|
|
}
|
|
|
|
}
|
2004-12-13 14:21:39 +01:00
|
|
|
HeapFree(GetProcessHeap(), 0, buf);
|
2003-05-13 05:32:20 +02:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2011-12-15 10:06:24 +01:00
|
|
|
DWORD getInterfacePhysicalByIndex(IF_INDEX index, PDWORD len, PBYTE addr,
|
2003-05-13 05:32:20 +02:00
|
|
|
PDWORD type)
|
|
|
|
{
|
2006-01-27 19:19:32 +01:00
|
|
|
char nameBuf[IF_NAMESIZE];
|
|
|
|
char *name = getInterfaceNameByIndex(index, nameBuf);
|
2003-05-13 05:32:20 +02:00
|
|
|
|
|
|
|
if (name)
|
|
|
|
return getInterfacePhysicalByName(name, len, addr, type);
|
|
|
|
else
|
|
|
|
return ERROR_INVALID_DATA;
|
|
|
|
}
|
|
|
|
|
2009-04-29 11:41:02 +02:00
|
|
|
DWORD getInterfaceMtuByName(const char *name, PDWORD mtu)
|
2003-05-13 05:32:20 +02:00
|
|
|
{
|
|
|
|
DWORD ret;
|
|
|
|
int fd;
|
|
|
|
|
|
|
|
if (!name)
|
|
|
|
return ERROR_INVALID_PARAMETER;
|
|
|
|
if (!mtu)
|
|
|
|
return ERROR_INVALID_PARAMETER;
|
|
|
|
|
|
|
|
fd = socket(PF_INET, SOCK_DGRAM, 0);
|
|
|
|
if (fd != -1) {
|
|
|
|
struct ifreq ifr;
|
|
|
|
|
2005-03-28 16:17:51 +02:00
|
|
|
lstrcpynA(ifr.ifr_name, name, IFNAMSIZ);
|
2003-05-13 05:32:20 +02:00
|
|
|
if ((ioctl(fd, SIOCGIFMTU, &ifr)))
|
|
|
|
ret = ERROR_INVALID_DATA;
|
|
|
|
else {
|
2004-01-14 06:31:23 +01:00
|
|
|
#ifndef __sun
|
2003-05-13 05:32:20 +02:00
|
|
|
*mtu = ifr.ifr_mtu;
|
2004-01-14 06:31:23 +01:00
|
|
|
#else
|
|
|
|
*mtu = ifr.ifr_metric;
|
|
|
|
#endif
|
2003-05-13 05:32:20 +02:00
|
|
|
ret = NO_ERROR;
|
|
|
|
}
|
2005-12-13 11:11:53 +01:00
|
|
|
close(fd);
|
2003-05-13 05:32:20 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
ret = ERROR_NO_MORE_FILES;
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2011-12-13 21:10:40 +01:00
|
|
|
DWORD getInterfaceStatusByName(const char *name, INTERNAL_IF_OPER_STATUS *status)
|
2003-05-13 05:32:20 +02:00
|
|
|
{
|
|
|
|
DWORD ret;
|
|
|
|
int fd;
|
|
|
|
|
|
|
|
if (!name)
|
|
|
|
return ERROR_INVALID_PARAMETER;
|
|
|
|
if (!status)
|
|
|
|
return ERROR_INVALID_PARAMETER;
|
|
|
|
|
|
|
|
fd = socket(PF_INET, SOCK_DGRAM, 0);
|
|
|
|
if (fd != -1) {
|
|
|
|
struct ifreq ifr;
|
|
|
|
|
2005-03-28 16:17:51 +02:00
|
|
|
lstrcpynA(ifr.ifr_name, name, IFNAMSIZ);
|
2003-05-13 05:32:20 +02:00
|
|
|
if ((ioctl(fd, SIOCGIFFLAGS, &ifr)))
|
|
|
|
ret = ERROR_INVALID_DATA;
|
|
|
|
else {
|
|
|
|
if (ifr.ifr_flags & IFF_UP)
|
|
|
|
*status = MIB_IF_OPER_STATUS_OPERATIONAL;
|
|
|
|
else
|
|
|
|
*status = MIB_IF_OPER_STATUS_NON_OPERATIONAL;
|
|
|
|
ret = NO_ERROR;
|
|
|
|
}
|
2005-12-13 11:11:53 +01:00
|
|
|
close(fd);
|
2003-05-13 05:32:20 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
ret = ERROR_NO_MORE_FILES;
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
DWORD getInterfaceEntryByName(const char *name, PMIB_IFROW entry)
|
|
|
|
{
|
|
|
|
BYTE addr[MAX_INTERFACE_PHYSADDR];
|
|
|
|
DWORD ret, len = sizeof(addr), type;
|
|
|
|
|
|
|
|
if (!name)
|
|
|
|
return ERROR_INVALID_PARAMETER;
|
|
|
|
if (!entry)
|
|
|
|
return ERROR_INVALID_PARAMETER;
|
|
|
|
|
|
|
|
if (getInterfacePhysicalByName(name, &len, addr, &type) == NO_ERROR) {
|
|
|
|
WCHAR *assigner;
|
|
|
|
const char *walker;
|
|
|
|
|
|
|
|
memset(entry, 0, sizeof(MIB_IFROW));
|
|
|
|
for (assigner = entry->wszName, walker = name; *walker;
|
|
|
|
walker++, assigner++)
|
|
|
|
*assigner = *walker;
|
|
|
|
*assigner = 0;
|
|
|
|
getInterfaceIndexByName(name, &entry->dwIndex);
|
|
|
|
entry->dwPhysAddrLen = len;
|
|
|
|
memcpy(entry->bPhysAddr, addr, len);
|
|
|
|
memset(entry->bPhysAddr + len, 0, sizeof(entry->bPhysAddr) - len);
|
|
|
|
entry->dwType = type;
|
|
|
|
/* FIXME: how to calculate real speed? */
|
|
|
|
getInterfaceMtuByName(name, &entry->dwMtu);
|
|
|
|
/* lie, there's no "administratively down" here */
|
|
|
|
entry->dwAdminStatus = MIB_IF_ADMIN_STATUS_UP;
|
|
|
|
getInterfaceStatusByName(name, &entry->dwOperStatus);
|
|
|
|
/* punt on dwLastChange? */
|
|
|
|
entry->dwDescrLen = min(strlen(name), MAX_INTERFACE_DESCRIPTION - 1);
|
|
|
|
memcpy(entry->bDescr, name, entry->dwDescrLen);
|
|
|
|
entry->bDescr[entry->dwDescrLen] = '\0';
|
|
|
|
entry->dwDescrLen++;
|
|
|
|
ret = NO_ERROR;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
ret = ERROR_INVALID_DATA;
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2011-11-13 21:37:43 +01:00
|
|
|
static DWORD getIPAddrRowByName(PMIB_IPADDRROW ipAddrRow, const char *ifName,
|
|
|
|
const struct sockaddr *sa)
|
|
|
|
{
|
|
|
|
DWORD ret, bcast;
|
|
|
|
|
|
|
|
ret = getInterfaceIndexByName(ifName, &ipAddrRow->dwIndex);
|
|
|
|
memcpy(&ipAddrRow->dwAddr, sa->sa_data + 2, sizeof(DWORD));
|
|
|
|
ipAddrRow->dwMask = getInterfaceMaskByName(ifName);
|
|
|
|
/* the dwBCastAddr member isn't the broadcast address, it indicates whether
|
|
|
|
* the interface uses the 1's broadcast address (1) or the 0's broadcast
|
|
|
|
* address (0).
|
|
|
|
*/
|
|
|
|
bcast = getInterfaceBCastAddrByName(ifName);
|
|
|
|
ipAddrRow->dwBCastAddr = (bcast & ipAddrRow->dwMask) ? 1 : 0;
|
|
|
|
/* FIXME: hardcoded reasm size, not sure where to get it */
|
|
|
|
ipAddrRow->dwReasmSize = 65535;
|
|
|
|
ipAddrRow->unused1 = 0;
|
2015-05-14 03:02:24 +02:00
|
|
|
/* wType is a bit field composed of MIB_IPADDR_* flags. Windows <= XP seems
|
|
|
|
* to like returning undocumented values 0x20 + 0x02 but for our current
|
|
|
|
* needs returning MIB_IPADDR_PRIMARY is enough.
|
|
|
|
*/
|
|
|
|
ipAddrRow->wType = MIB_IPADDR_PRIMARY;
|
2011-11-13 21:37:43 +01:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2017-06-19 13:47:38 +02:00
|
|
|
#if defined(HAVE_IFADDRS_H) && defined(HAVE_GETIFADDRS)
|
2011-11-13 21:37:43 +01:00
|
|
|
|
|
|
|
/* Counts the IPv4 addresses in the system using the return value from
|
|
|
|
* getifaddrs, returning the count.
|
|
|
|
*/
|
|
|
|
static DWORD countIPv4Addresses(struct ifaddrs *ifa)
|
|
|
|
{
|
|
|
|
DWORD numAddresses = 0;
|
|
|
|
|
|
|
|
for (; ifa; ifa = ifa->ifa_next)
|
|
|
|
if (ifa->ifa_addr && ifa->ifa_addr->sa_family == AF_INET)
|
|
|
|
numAddresses++;
|
|
|
|
return numAddresses;
|
|
|
|
}
|
|
|
|
|
|
|
|
DWORD getNumIPAddresses(void)
|
|
|
|
{
|
|
|
|
DWORD numAddresses = 0;
|
|
|
|
struct ifaddrs *ifa;
|
|
|
|
|
|
|
|
if (!getifaddrs(&ifa))
|
|
|
|
{
|
|
|
|
numAddresses = countIPv4Addresses(ifa);
|
|
|
|
freeifaddrs(ifa);
|
|
|
|
}
|
|
|
|
return numAddresses;
|
|
|
|
}
|
|
|
|
|
|
|
|
DWORD getIPAddrTable(PMIB_IPADDRTABLE *ppIpAddrTable, HANDLE heap, DWORD flags)
|
|
|
|
{
|
|
|
|
DWORD ret;
|
|
|
|
|
|
|
|
if (!ppIpAddrTable)
|
|
|
|
ret = ERROR_INVALID_PARAMETER;
|
|
|
|
else
|
|
|
|
{
|
|
|
|
struct ifaddrs *ifa;
|
|
|
|
|
|
|
|
if (!getifaddrs(&ifa))
|
|
|
|
{
|
|
|
|
DWORD size = sizeof(MIB_IPADDRTABLE);
|
|
|
|
DWORD numAddresses = countIPv4Addresses(ifa);
|
|
|
|
|
|
|
|
if (numAddresses > 1)
|
|
|
|
size += (numAddresses - 1) * sizeof(MIB_IPADDRROW);
|
|
|
|
*ppIpAddrTable = HeapAlloc(heap, flags, size);
|
|
|
|
if (*ppIpAddrTable)
|
|
|
|
{
|
|
|
|
DWORD i = 0;
|
|
|
|
struct ifaddrs *ifp;
|
|
|
|
|
|
|
|
ret = NO_ERROR;
|
|
|
|
(*ppIpAddrTable)->dwNumEntries = numAddresses;
|
|
|
|
for (ifp = ifa; !ret && ifp; ifp = ifp->ifa_next)
|
|
|
|
{
|
|
|
|
if (!ifp->ifa_addr || ifp->ifa_addr->sa_family != AF_INET)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
ret = getIPAddrRowByName(&(*ppIpAddrTable)->table[i], ifp->ifa_name,
|
|
|
|
ifp->ifa_addr);
|
|
|
|
i++;
|
|
|
|
}
|
2012-11-07 10:36:46 +01:00
|
|
|
if (ret)
|
|
|
|
HeapFree(GetProcessHeap(), 0, *ppIpAddrTable);
|
2011-11-13 21:37:43 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
ret = ERROR_OUTOFMEMORY;
|
|
|
|
freeifaddrs(ifa);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
ret = ERROR_INVALID_PARAMETER;
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2013-10-08 13:30:00 +02:00
|
|
|
ULONG v6addressesFromIndex(IF_INDEX index, SOCKET_ADDRESS **addrs, ULONG *num_addrs, SOCKET_ADDRESS **masks)
|
2011-11-13 17:25:52 +01:00
|
|
|
{
|
|
|
|
struct ifaddrs *ifa;
|
|
|
|
ULONG ret;
|
|
|
|
|
|
|
|
if (!getifaddrs(&ifa))
|
|
|
|
{
|
|
|
|
struct ifaddrs *p;
|
|
|
|
ULONG n;
|
|
|
|
char name[IFNAMSIZ];
|
|
|
|
|
|
|
|
getInterfaceNameByIndex(index, name);
|
|
|
|
for (p = ifa, n = 0; p; p = p->ifa_next)
|
|
|
|
if (p->ifa_addr && p->ifa_addr->sa_family == AF_INET6 &&
|
|
|
|
!strcmp(name, p->ifa_name))
|
|
|
|
n++;
|
|
|
|
if (n)
|
|
|
|
{
|
|
|
|
*addrs = HeapAlloc(GetProcessHeap(), 0, n * (sizeof(SOCKET_ADDRESS) +
|
|
|
|
sizeof(struct WS_sockaddr_in6)));
|
2013-10-08 13:30:00 +02:00
|
|
|
*masks = HeapAlloc(GetProcessHeap(), 0, n * (sizeof(SOCKET_ADDRESS) +
|
|
|
|
sizeof(struct WS_sockaddr_in6)));
|
|
|
|
if (*addrs && *masks)
|
2011-11-13 17:25:52 +01:00
|
|
|
{
|
|
|
|
struct WS_sockaddr_in6 *next_addr = (struct WS_sockaddr_in6 *)(
|
|
|
|
(BYTE *)*addrs + n * sizeof(SOCKET_ADDRESS));
|
2013-10-08 13:30:00 +02:00
|
|
|
struct WS_sockaddr_in6 *mask_addr = (struct WS_sockaddr_in6 *)(
|
|
|
|
(BYTE *)*masks + n * sizeof(SOCKET_ADDRESS));
|
2011-11-13 17:25:52 +01:00
|
|
|
|
|
|
|
for (p = ifa, n = 0; p; p = p->ifa_next)
|
|
|
|
{
|
|
|
|
if (p->ifa_addr && p->ifa_addr->sa_family == AF_INET6 &&
|
|
|
|
!strcmp(name, p->ifa_name))
|
|
|
|
{
|
|
|
|
struct sockaddr_in6 *addr = (struct sockaddr_in6 *)p->ifa_addr;
|
2013-10-08 13:30:00 +02:00
|
|
|
struct sockaddr_in6 *mask = (struct sockaddr_in6 *)p->ifa_netmask;
|
2011-11-13 17:25:52 +01:00
|
|
|
|
|
|
|
next_addr->sin6_family = WS_AF_INET6;
|
|
|
|
next_addr->sin6_port = addr->sin6_port;
|
|
|
|
next_addr->sin6_flowinfo = addr->sin6_flowinfo;
|
|
|
|
memcpy(&next_addr->sin6_addr, &addr->sin6_addr,
|
|
|
|
sizeof(next_addr->sin6_addr));
|
|
|
|
next_addr->sin6_scope_id = addr->sin6_scope_id;
|
|
|
|
(*addrs)[n].lpSockaddr = (LPSOCKADDR)next_addr;
|
|
|
|
(*addrs)[n].iSockaddrLength = sizeof(struct WS_sockaddr_in6);
|
|
|
|
next_addr++;
|
2013-10-08 13:30:00 +02:00
|
|
|
|
|
|
|
mask_addr->sin6_family = WS_AF_INET6;
|
|
|
|
mask_addr->sin6_port = mask->sin6_port;
|
|
|
|
mask_addr->sin6_flowinfo = mask->sin6_flowinfo;
|
|
|
|
memcpy(&mask_addr->sin6_addr, &mask->sin6_addr,
|
|
|
|
sizeof(mask_addr->sin6_addr));
|
|
|
|
mask_addr->sin6_scope_id = mask->sin6_scope_id;
|
|
|
|
(*masks)[n].lpSockaddr = (LPSOCKADDR)mask_addr;
|
|
|
|
(*masks)[n].iSockaddrLength = sizeof(struct WS_sockaddr_in6);
|
|
|
|
mask_addr++;
|
2011-11-13 17:25:52 +01:00
|
|
|
n++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
*num_addrs = n;
|
|
|
|
ret = ERROR_SUCCESS;
|
|
|
|
}
|
|
|
|
else
|
2013-10-08 13:30:00 +02:00
|
|
|
{
|
|
|
|
HeapFree(GetProcessHeap(), 0, *addrs);
|
|
|
|
HeapFree(GetProcessHeap(), 0, *masks);
|
2011-11-13 17:25:52 +01:00
|
|
|
ret = ERROR_OUTOFMEMORY;
|
2013-10-08 13:30:00 +02:00
|
|
|
}
|
2011-11-13 17:25:52 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
*addrs = NULL;
|
|
|
|
*num_addrs = 0;
|
2013-10-08 13:30:00 +02:00
|
|
|
*masks = NULL;
|
2011-11-13 17:25:52 +01:00
|
|
|
ret = ERROR_SUCCESS;
|
|
|
|
}
|
|
|
|
freeifaddrs(ifa);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
ret = ERROR_NO_DATA;
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2011-11-13 21:37:43 +01:00
|
|
|
#else
|
|
|
|
|
2006-01-25 13:14:12 +01:00
|
|
|
/* Enumerates the IP addresses in the system using SIOCGIFCONF, returning
|
|
|
|
* the count to you in *pcAddresses. It also returns to you the struct ifconf
|
|
|
|
* used by the call to ioctl, so that you may process the addresses further.
|
|
|
|
* Free ifc->ifc_buf using HeapFree.
|
|
|
|
* Returns NO_ERROR on success, something else on failure.
|
|
|
|
*/
|
|
|
|
static DWORD enumIPAddresses(PDWORD pcAddresses, struct ifconf *ifc)
|
|
|
|
{
|
|
|
|
DWORD ret;
|
|
|
|
int fd;
|
|
|
|
|
|
|
|
fd = socket(PF_INET, SOCK_DGRAM, 0);
|
|
|
|
if (fd != -1) {
|
|
|
|
int ioctlRet = 0;
|
|
|
|
DWORD guessedNumAddresses = 0, numAddresses = 0;
|
|
|
|
caddr_t ifPtr;
|
2007-01-26 02:14:53 +01:00
|
|
|
int lastlen;
|
2006-01-25 13:14:12 +01:00
|
|
|
|
|
|
|
ret = NO_ERROR;
|
|
|
|
ifc->ifc_len = 0;
|
|
|
|
ifc->ifc_buf = NULL;
|
|
|
|
/* there is no way to know the interface count beforehand,
|
|
|
|
so we need to loop again and again upping our max each time
|
2007-01-26 02:14:53 +01:00
|
|
|
until returned is constant across 2 calls */
|
2006-01-25 13:14:12 +01:00
|
|
|
do {
|
2007-01-26 02:14:53 +01:00
|
|
|
lastlen = ifc->ifc_len;
|
2006-01-25 13:14:12 +01:00
|
|
|
HeapFree(GetProcessHeap(), 0, ifc->ifc_buf);
|
|
|
|
if (guessedNumAddresses == 0)
|
|
|
|
guessedNumAddresses = INITIAL_INTERFACES_ASSUMED;
|
|
|
|
else
|
|
|
|
guessedNumAddresses *= 2;
|
|
|
|
ifc->ifc_len = sizeof(struct ifreq) * guessedNumAddresses;
|
|
|
|
ifc->ifc_buf = HeapAlloc(GetProcessHeap(), 0, ifc->ifc_len);
|
|
|
|
ioctlRet = ioctl(fd, SIOCGIFCONF, ifc);
|
2007-01-26 02:14:53 +01:00
|
|
|
} while ((ioctlRet == 0) && (ifc->ifc_len != lastlen));
|
2006-01-25 13:14:12 +01:00
|
|
|
|
|
|
|
if (ioctlRet == 0) {
|
|
|
|
ifPtr = ifc->ifc_buf;
|
2018-06-28 13:54:45 +02:00
|
|
|
while (ifPtr && (char *)ifPtr < ifc->ifc_buf + ifc->ifc_len) {
|
2007-01-26 02:15:58 +01:00
|
|
|
struct ifreq *ifr = (struct ifreq *)ifPtr;
|
|
|
|
|
|
|
|
if (ifr->ifr_addr.sa_family == AF_INET)
|
|
|
|
numAddresses++;
|
|
|
|
|
2018-06-28 13:54:45 +02:00
|
|
|
ifPtr = (char *)ifPtr + ifreq_len((struct ifreq *)ifPtr);
|
2006-01-25 13:14:12 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
ret = ERROR_INVALID_PARAMETER; /* FIXME: map from errno to Win32 */
|
|
|
|
if (!ret)
|
|
|
|
*pcAddresses = numAddresses;
|
|
|
|
else
|
|
|
|
{
|
|
|
|
HeapFree(GetProcessHeap(), 0, ifc->ifc_buf);
|
|
|
|
ifc->ifc_buf = NULL;
|
|
|
|
}
|
|
|
|
close(fd);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
ret = ERROR_NO_SYSTEM_RESOURCES;
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
DWORD getNumIPAddresses(void)
|
|
|
|
{
|
|
|
|
DWORD numAddresses = 0;
|
|
|
|
struct ifconf ifc;
|
|
|
|
|
|
|
|
if (!enumIPAddresses(&numAddresses, &ifc))
|
|
|
|
HeapFree(GetProcessHeap(), 0, ifc.ifc_buf);
|
|
|
|
return numAddresses;
|
|
|
|
}
|
|
|
|
|
2006-01-24 12:18:50 +01:00
|
|
|
DWORD getIPAddrTable(PMIB_IPADDRTABLE *ppIpAddrTable, HANDLE heap, DWORD flags)
|
|
|
|
{
|
|
|
|
DWORD ret;
|
|
|
|
|
|
|
|
if (!ppIpAddrTable)
|
|
|
|
ret = ERROR_INVALID_PARAMETER;
|
|
|
|
else
|
|
|
|
{
|
2006-01-25 13:14:12 +01:00
|
|
|
DWORD numAddresses = 0;
|
|
|
|
struct ifconf ifc;
|
2006-01-24 12:18:50 +01:00
|
|
|
|
2006-01-25 13:14:12 +01:00
|
|
|
ret = enumIPAddresses(&numAddresses, &ifc);
|
|
|
|
if (!ret)
|
|
|
|
{
|
2007-11-15 20:06:07 +01:00
|
|
|
DWORD size = sizeof(MIB_IPADDRTABLE);
|
|
|
|
|
|
|
|
if (numAddresses > 1)
|
|
|
|
size += (numAddresses - 1) * sizeof(MIB_IPADDRROW);
|
|
|
|
*ppIpAddrTable = HeapAlloc(heap, flags, size);
|
2006-01-25 13:14:12 +01:00
|
|
|
if (*ppIpAddrTable) {
|
2011-11-13 21:37:43 +01:00
|
|
|
DWORD i = 0;
|
2006-01-25 13:14:12 +01:00
|
|
|
caddr_t ifPtr;
|
2006-01-24 12:18:50 +01:00
|
|
|
|
2006-01-25 13:14:12 +01:00
|
|
|
ret = NO_ERROR;
|
|
|
|
(*ppIpAddrTable)->dwNumEntries = numAddresses;
|
|
|
|
ifPtr = ifc.ifc_buf;
|
2018-06-28 13:54:45 +02:00
|
|
|
while (!ret && ifPtr && (char *)ifPtr < ifc.ifc_buf + ifc.ifc_len) {
|
2006-01-25 13:14:12 +01:00
|
|
|
struct ifreq *ifr = (struct ifreq *)ifPtr;
|
|
|
|
|
2018-06-28 13:54:45 +02:00
|
|
|
ifPtr = (char *)ifPtr + ifreq_len(ifr);
|
2007-01-26 02:15:58 +01:00
|
|
|
|
|
|
|
if (ifr->ifr_addr.sa_family != AF_INET)
|
|
|
|
continue;
|
|
|
|
|
2011-11-13 21:37:43 +01:00
|
|
|
ret = getIPAddrRowByName(&(*ppIpAddrTable)->table[i], ifr->ifr_name,
|
|
|
|
&ifr->ifr_addr);
|
2006-01-25 13:14:12 +01:00
|
|
|
i++;
|
2006-01-24 12:18:50 +01:00
|
|
|
}
|
2012-11-07 10:36:46 +01:00
|
|
|
if (ret)
|
|
|
|
HeapFree(GetProcessHeap(), 0, *ppIpAddrTable);
|
2006-01-24 12:18:50 +01:00
|
|
|
}
|
|
|
|
else
|
2006-01-25 13:14:12 +01:00
|
|
|
ret = ERROR_OUTOFMEMORY;
|
2006-01-24 12:18:50 +01:00
|
|
|
HeapFree(GetProcessHeap(), 0, ifc.ifc_buf);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2013-10-08 13:30:00 +02:00
|
|
|
ULONG v6addressesFromIndex(IF_INDEX index, SOCKET_ADDRESS **addrs, ULONG *num_addrs, SOCKET_ADDRESS **masks)
|
2010-03-12 03:36:46 +01:00
|
|
|
{
|
|
|
|
*addrs = NULL;
|
|
|
|
*num_addrs = 0;
|
2013-10-08 13:30:00 +02:00
|
|
|
*masks = NULL;
|
2010-03-12 03:36:46 +01:00
|
|
|
return ERROR_SUCCESS;
|
|
|
|
}
|
2011-11-13 17:25:52 +01:00
|
|
|
|
2010-03-12 03:36:46 +01:00
|
|
|
#endif
|