Permalink
Browse files

Added getifaddrs() and native BSD versions of findif.

Native BSD version based off of:
http://www.freebsd.org/cgi/query-pr.cgi?pr=122333
  • Loading branch information...
ThomasHabets committed Nov 1, 2011
1 parent 2183af6 commit a03413aa161cace9733a0a3c3c98420c761484ee
Showing with 378 additions and 15 deletions.
  1. +20 −12 configure.ac
  2. +3 −2 src/arping.c
  3. +1 −0 src/arping.h
  4. +4 −1 src/{findif_bsd.c → findif_bsdroute.c}
  5. +107 −0 src/findif_getifaddrs.c
  6. +243 −0 src/findif_sysctl.c
@@ -60,19 +60,27 @@ AC_PROG_GCC_TRADITIONAL
AC_FUNC_SELECT_ARGTYPES
AC_FUNC_SETVBUF_REVERSED
AC_TYPE_SIGNAL
AC_CHECK_FUNCS([gettimeofday memset select strchr strdup strerror strstr])
AC_CHECK_FUNCS([gettimeofday memset select strchr strdup strerror strstr \
getifaddrs])
case "$target_os" in
*linux*)
AC_LIBOBJ([findif_linux])
;;
*freebsd*|*openbsd*|*solaris*|*darwin*)
AC_LIBOBJ([findif_bsd])
;;
*)
AC_LIBOBJ([findif_other])
;;
esac
if test x$ac_cv_func_getifaddrs = xyes; then
AC_LIBOBJ([findif_getifaddrs])
else
case "$target_os" in
*linux*)
AC_LIBOBJ([findif_linux])
;;
*freebsd*|*openbsd*|*darwin*)
AC_LIBOBJ([findif_sysctl])
;;
*solaris*)
AC_LIBOBJ([findif_bsdroute])
;;
*)
AC_LIBOBJ([findif_other])
;;
esac
fi
@@ -124,7 +124,6 @@ uint32_t srcip, dstip;
static int beep = 0;
static int reverse_beep = 0;
static int verbose = 0;
static int alsototal = 0;
/*static int pingmac = 0; */
static int finddup = 0;
@@ -142,8 +141,10 @@ static char srcmac[ETH_ALEN];
static char dstmac[ETH_ALEN];
static char lastreplymac[ETH_ALEN];
int verbose = 0;
/* doesn't need to be volatile */
volatile sig_atomic_t time_to_die = 0;
static volatile sig_atomic_t time_to_die = 0;
/**
*
@@ -9,6 +9,7 @@
#endif
extern uint32_t srcip,dstip;
extern int verbose;
void do_libnet_init(const char *ifname);
const char *arping_lookupdev_default(const char *ifname,
uint32_t srcip, uint32_t dstip,
@@ -1,4 +1,4 @@
/* arping/src/findif_bsd.c
/* arping/src/findif_bsdroute.c
*
* Copyright (C) 2000-2009 Thomas Habets <thomas@habets.pp.se>
*
@@ -16,6 +16,9 @@
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
/**
* Fallback to ugly solution.
*/
#if HAVE_CONFIG_H
#include "config.h"
#endif
@@ -0,0 +1,107 @@
/* arping/src/findif_getifaddrs.c
*
* Copyright (C) 2000-2011 Thomas Habets <thomas@habets.se>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public
* License as published by the Free Software Foundation; either
* version 2 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
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
/**
* Most modern systems should have getifaddrs().
*/
#if HAVE_CONFIG_H
#include "config.h"
#endif
#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/ioctl.h>
#include <ifaddrs.h>
#include <net/if.h>
#include <netinet/in.h>
#include "arping.h"
const char *
arping_lookupdev(const char *ifname_unused,
uint32_t srcip,
uint32_t dstip,
char *ebuf)
{
struct ifaddrs *ifa = NULL;
struct ifaddrs *cur;
const char *ret;
int match_count = 0; /* Matching interfaces */
/* best match */
in_addr_t best_mask = 0;
in_addr_t best_addr;
/* Results */
static char ifname[IFNAMSIZ];
ifname[0] = 0;
if (getifaddrs(&ifa)) {
if (verbose) {
printf("getifaddrs(): %s\n", strerror(errno));
}
goto failed;
}
for (cur = ifa; cur; cur = cur->ifa_next) {
in_addr_t addr, mask;
if (cur->ifa_addr->sa_family != AF_INET) {
continue;
}
if (cur->ifa_flags & (IFF_LOOPBACK|IFF_POINTOPOINT)) {
continue;
}
addr =((struct sockaddr_in*)cur->ifa_addr)->sin_addr.s_addr;
mask =((struct sockaddr_in*)cur->ifa_netmask)->sin_addr.s_addr;
if ((addr & mask) != (dstip & mask)) {
continue;
}
if (ntohl(mask) > ntohl(best_mask)) {
memset(ifname, 0, sizeof(ifname));
strncpy(ifname, cur->ifa_name, sizeof(ifname)-1);
best_addr = addr;
best_mask = mask;
}
}
if (!ifname[0]) {
if (verbose) {
printf("Failed to find iface using getifaddrs().\n");
}
goto failed;
}
ret = ifname;
out:
if (ifa) {
freeifaddrs(ifa);
}
return ret;
failed:
ret = arping_lookupdev_default(ifname_unused, srcip, dstip, ebuf);
goto out;
}
/* ---- Emacs Variables ----
* Local Variables:
* c-basic-offset: 8
* indent-tabs-mode: nil
* End:
*/
Oops, something went wrong.

0 comments on commit a03413a

Please sign in to comment.