|
Packit |
03b34a |
/*
|
|
Packit |
03b34a |
* $Id: libnet_resolve.c,v 1.21 2004/11/09 07:05:07 mike Exp $
|
|
Packit |
03b34a |
*
|
|
Packit |
03b34a |
* libnet
|
|
Packit |
03b34a |
* libnet_resolve.c - various name resolution type routines
|
|
Packit |
03b34a |
*
|
|
Packit |
03b34a |
* Copyright (c) 1998 - 2004 Mike D. Schiffman <mike@infonexus.com>
|
|
Packit |
03b34a |
* All rights reserved.
|
|
Packit |
03b34a |
*
|
|
Packit |
03b34a |
* Redistribution and use in source and binary forms, with or without
|
|
Packit |
03b34a |
* modification, are permitted provided that the following conditions
|
|
Packit |
03b34a |
* are met:
|
|
Packit |
03b34a |
* 1. Redistributions of source code must retain the above copyright
|
|
Packit |
03b34a |
* notice, this list of conditions and the following disclaimer.
|
|
Packit |
03b34a |
* 2. Redistributions in binary form must reproduce the above copyright
|
|
Packit |
03b34a |
* notice, this list of conditions and the following disclaimer in the
|
|
Packit |
03b34a |
* documentation and/or other materials provided with the distribution.
|
|
Packit |
03b34a |
*
|
|
Packit |
03b34a |
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
|
|
Packit |
03b34a |
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
Packit |
03b34a |
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
Packit |
03b34a |
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
|
|
Packit |
03b34a |
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
Packit |
03b34a |
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
|
Packit |
03b34a |
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
|
Packit |
03b34a |
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
|
Packit |
03b34a |
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
|
Packit |
03b34a |
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
|
Packit |
03b34a |
* SUCH DAMAGE.
|
|
Packit |
03b34a |
*
|
|
Packit |
03b34a |
*/
|
|
Packit |
03b34a |
|
|
Packit |
03b34a |
#if (HAVE_CONFIG_H)
|
|
Packit |
03b34a |
#include "../include/config.h"
|
|
Packit |
03b34a |
#endif
|
|
Packit |
03b34a |
#if (!(_WIN32) || (__CYGWIN__))
|
|
Packit |
03b34a |
#include "../include/libnet.h"
|
|
Packit |
03b34a |
#else
|
|
Packit |
03b34a |
#include "../include/win32/libnet.h"
|
|
Packit |
03b34a |
#endif
|
|
Packit |
03b34a |
|
|
Packit |
03b34a |
#ifndef HAVE_GETHOSTBYNAME2
|
|
Packit |
03b34a |
struct hostent *
|
|
Packit |
03b34a |
gethostbyname2(const char *name, int af)
|
|
Packit |
03b34a |
{
|
|
Packit |
03b34a |
return gethostbyname(name);
|
|
Packit |
03b34a |
}
|
|
Packit |
03b34a |
#endif
|
|
Packit |
03b34a |
|
|
Packit |
03b34a |
char *
|
|
Packit |
03b34a |
libnet_addr2name4(uint32_t in, uint8_t use_name)
|
|
Packit |
03b34a |
{
|
|
Packit |
03b34a |
#define HOSTNAME_SIZE 512
|
|
Packit |
03b34a |
static char hostname[HOSTNAME_SIZE+1], hostname2[HOSTNAME_SIZE+1];
|
|
Packit |
03b34a |
static uint16_t which;
|
|
Packit |
03b34a |
uint8_t *p;
|
|
Packit |
03b34a |
|
|
Packit |
03b34a |
struct hostent *host_ent = NULL;
|
|
Packit |
03b34a |
struct in_addr addr;
|
|
Packit |
03b34a |
|
|
Packit |
03b34a |
/*
|
|
Packit |
03b34a |
* Swap to the other buffer. We swap static buffers to avoid having to
|
|
Packit |
03b34a |
* pass in a int8_t *. This makes the code that calls this function more
|
|
Packit |
03b34a |
* intuitive, but makes this function ugly. This function is seriously
|
|
Packit |
03b34a |
* non-reentrant. For threaded applications (or for signal handler code)
|
|
Packit |
03b34a |
* use host_lookup_r().
|
|
Packit |
03b34a |
*/
|
|
Packit |
03b34a |
which++;
|
|
Packit |
03b34a |
|
|
Packit |
03b34a |
if (use_name == LIBNET_RESOLVE)
|
|
Packit |
03b34a |
{
|
|
Packit |
03b34a |
addr.s_addr = in;
|
|
Packit |
03b34a |
host_ent = gethostbyaddr((int8_t *)&addr, sizeof(struct in_addr), AF_INET);
|
|
Packit |
03b34a |
/* if this fails, we silently ignore the error and move to plan b! */
|
|
Packit |
03b34a |
}
|
|
Packit |
03b34a |
if (!host_ent)
|
|
Packit |
03b34a |
{
|
|
Packit |
03b34a |
|
|
Packit |
03b34a |
p = (uint8_t *)∈
|
|
Packit |
03b34a |
snprintf(((which % 2) ? hostname : hostname2), HOSTNAME_SIZE,
|
|
Packit |
03b34a |
"%d.%d.%d.%d",
|
|
Packit |
03b34a |
(p[0] & 255), (p[1] & 255), (p[2] & 255), (p[3] & 255));
|
|
Packit |
03b34a |
}
|
|
Packit |
03b34a |
else if (use_name == LIBNET_RESOLVE)
|
|
Packit |
03b34a |
{
|
|
Packit |
03b34a |
char *ptr = ((which % 2) ? hostname : hostname2);
|
|
Packit |
03b34a |
strncpy(ptr, host_ent->h_name, HOSTNAME_SIZE);
|
|
Packit |
03b34a |
ptr[HOSTNAME_SIZE] = '\0';
|
|
Packit |
03b34a |
|
|
Packit |
03b34a |
}
|
|
Packit |
03b34a |
return (which % 2) ? (hostname) : (hostname2);
|
|
Packit |
03b34a |
}
|
|
Packit |
03b34a |
|
|
Packit |
03b34a |
void
|
|
Packit |
03b34a |
libnet_addr2name4_r(uint32_t in, uint8_t use_name, char *hostname,
|
|
Packit |
03b34a |
int hostname_len)
|
|
Packit |
03b34a |
{
|
|
Packit |
03b34a |
uint8_t *p;
|
|
Packit |
03b34a |
struct hostent *host_ent = NULL;
|
|
Packit |
03b34a |
struct in_addr addr;
|
|
Packit |
03b34a |
|
|
Packit |
03b34a |
if (use_name == LIBNET_RESOLVE)
|
|
Packit |
03b34a |
{
|
|
Packit |
03b34a |
addr.s_addr = in;
|
|
Packit |
03b34a |
host_ent = gethostbyaddr((int8_t *)&addr, sizeof(struct in_addr),
|
|
Packit |
03b34a |
AF_INET);
|
|
Packit |
03b34a |
}
|
|
Packit |
03b34a |
if (!host_ent)
|
|
Packit |
03b34a |
{
|
|
Packit |
03b34a |
p = (uint8_t *)∈
|
|
Packit |
03b34a |
snprintf(hostname, hostname_len, "%d.%d.%d.%d",
|
|
Packit |
03b34a |
(p[0] & 255), (p[1] & 255), (p[2] & 255), (p[3] & 255));
|
|
Packit |
03b34a |
}
|
|
Packit |
03b34a |
else
|
|
Packit |
03b34a |
{
|
|
Packit |
03b34a |
strncpy(hostname, host_ent->h_name, hostname_len - 1);
|
|
Packit |
03b34a |
hostname[sizeof(hostname) - 1] = '\0';
|
|
Packit |
03b34a |
}
|
|
Packit |
03b34a |
}
|
|
Packit |
03b34a |
|
|
Packit |
03b34a |
uint32_t
|
|
Packit |
03b34a |
libnet_name2addr4(libnet_t *l, char *host_name, uint8_t use_name)
|
|
Packit |
03b34a |
{
|
|
Packit |
03b34a |
struct in_addr addr;
|
|
Packit |
03b34a |
struct hostent *host_ent;
|
|
Packit |
03b34a |
uint32_t m;
|
|
Packit |
03b34a |
uint val;
|
|
Packit |
03b34a |
int i;
|
|
Packit |
03b34a |
|
|
Packit |
03b34a |
if (use_name == LIBNET_RESOLVE)
|
|
Packit |
03b34a |
{
|
|
Packit |
03b34a |
if ((addr.s_addr = inet_addr(host_name)) == -1)
|
|
Packit |
03b34a |
{
|
|
Packit |
03b34a |
if (!(host_ent = gethostbyname(host_name)))
|
|
Packit |
03b34a |
{
|
|
Packit |
03b34a |
snprintf(l->err_buf, LIBNET_ERRBUF_SIZE,
|
|
Packit |
03b34a |
"%s(): %s\n", __func__, hstrerror(h_errno));
|
|
Packit |
03b34a |
/* XXX - this is actually 255.255.255.255 */
|
|
Packit |
03b34a |
return (-1);
|
|
Packit |
03b34a |
}
|
|
Packit |
03b34a |
memcpy(&addr.s_addr, host_ent->h_addr, host_ent->h_length);
|
|
Packit |
03b34a |
}
|
|
Packit |
03b34a |
/* network byte order */
|
|
Packit |
03b34a |
return (addr.s_addr);
|
|
Packit |
03b34a |
}
|
|
Packit |
03b34a |
else
|
|
Packit |
03b34a |
{
|
|
Packit |
03b34a |
/*
|
|
Packit |
03b34a |
* We only want dots 'n decimals.
|
|
Packit |
03b34a |
*/
|
|
Packit |
03b34a |
if (!isdigit(host_name[0]))
|
|
Packit |
03b34a |
{
|
|
Packit |
03b34a |
if (l)
|
|
Packit |
03b34a |
{
|
|
Packit |
03b34a |
snprintf(l->err_buf, LIBNET_ERRBUF_SIZE,
|
|
Packit |
03b34a |
"%s(): expecting dots and decimals\n", __func__);
|
|
Packit |
03b34a |
}
|
|
Packit |
03b34a |
/* XXX - this is actually 255.255.255.255 */
|
|
Packit |
03b34a |
return (-1);
|
|
Packit |
03b34a |
}
|
|
Packit |
03b34a |
|
|
Packit |
03b34a |
m = 0;
|
|
Packit |
03b34a |
for (i = 0; i < 4; i++)
|
|
Packit |
03b34a |
{
|
|
Packit |
03b34a |
m <<= 8;
|
|
Packit |
03b34a |
if (*host_name)
|
|
Packit |
03b34a |
{
|
|
Packit |
03b34a |
val = 0;
|
|
Packit |
03b34a |
while (*host_name && *host_name != '.')
|
|
Packit |
03b34a |
{
|
|
Packit |
03b34a |
val *= 10;
|
|
Packit |
03b34a |
val += *host_name - '0';
|
|
Packit |
03b34a |
if (val > 255)
|
|
Packit |
03b34a |
{
|
|
Packit |
03b34a |
if (l)
|
|
Packit |
03b34a |
{
|
|
Packit |
03b34a |
snprintf(l->err_buf, LIBNET_ERRBUF_SIZE,
|
|
Packit |
03b34a |
"%s(): value greater than 255\n", __func__);
|
|
Packit |
03b34a |
}
|
|
Packit |
03b34a |
/* XXX - this is actually 255.255.255.255 */
|
|
Packit |
03b34a |
return (-1);
|
|
Packit |
03b34a |
}
|
|
Packit |
03b34a |
host_name++;
|
|
Packit |
03b34a |
}
|
|
Packit |
03b34a |
m |= val;
|
|
Packit |
03b34a |
if (*host_name)
|
|
Packit |
03b34a |
{
|
|
Packit |
03b34a |
host_name++;
|
|
Packit |
03b34a |
}
|
|
Packit |
03b34a |
}
|
|
Packit |
03b34a |
}
|
|
Packit |
03b34a |
/* host byte order */
|
|
Packit |
03b34a |
return (ntohl(m));
|
|
Packit |
03b34a |
}
|
|
Packit |
03b34a |
}
|
|
Packit |
03b34a |
|
|
Packit |
03b34a |
void
|
|
Packit |
03b34a |
libnet_addr2name6_r(struct libnet_in6_addr addr, uint8_t use_name,
|
|
Packit |
03b34a |
char *host_name, int host_name_len)
|
|
Packit |
03b34a |
{
|
|
Packit |
03b34a |
struct hostent *host_ent = NULL;
|
|
Packit |
03b34a |
|
|
Packit |
03b34a |
if (use_name == LIBNET_RESOLVE)
|
|
Packit |
03b34a |
{
|
|
Packit |
03b34a |
#ifdef HAVE_SOLARIS
|
|
Packit |
03b34a |
#ifdef HAVE_SOLARIS_IPV6
|
|
Packit |
03b34a |
host_ent = getipnodebyaddr((int8_t *)&addr, sizeof(struct in_addr),
|
|
Packit |
03b34a |
AF_INET6, NULL);
|
|
Packit |
03b34a |
#else
|
|
Packit |
03b34a |
/* XXX - Gah! Can't report error! */
|
|
Packit |
03b34a |
host_ent = NULL;
|
|
Packit |
03b34a |
#endif
|
|
Packit |
03b34a |
#else
|
|
Packit |
03b34a |
host_ent = gethostbyaddr((int8_t *)&addr, sizeof(struct in_addr),
|
|
Packit |
03b34a |
AF_INET6);
|
|
Packit |
03b34a |
#endif
|
|
Packit |
03b34a |
}
|
|
Packit |
03b34a |
if (!host_ent)
|
|
Packit |
03b34a |
{
|
|
Packit |
03b34a |
#if !defined(__WIN32__) /* Silence Win32 warning */
|
|
Packit |
03b34a |
inet_ntop(AF_INET6, &addr, host_name, host_name_len);
|
|
Packit |
03b34a |
#endif
|
|
Packit |
03b34a |
}
|
|
Packit |
03b34a |
else
|
|
Packit |
03b34a |
{
|
|
Packit |
03b34a |
strncpy(host_name, host_ent->h_name, host_name_len -1);
|
|
Packit |
03b34a |
host_name[sizeof(host_name) - 1] = '\0';
|
|
Packit |
03b34a |
}
|
|
Packit |
03b34a |
}
|
|
Packit |
03b34a |
|
|
Packit |
03b34a |
const struct libnet_in6_addr in6addr_error = IN6ADDR_ERROR_INIT;
|
|
Packit |
03b34a |
|
|
Packit |
03b34a |
int
|
|
Packit |
03b34a |
libnet_in6_is_error(struct libnet_in6_addr addr)
|
|
Packit |
03b34a |
{
|
|
Packit |
03b34a |
return 0 == memcmp(&addr, &in6addr_error, sizeof(addr));
|
|
Packit |
03b34a |
}
|
|
Packit |
03b34a |
|
|
Packit |
03b34a |
struct libnet_in6_addr
|
|
Packit |
03b34a |
libnet_name2addr6(libnet_t *l, const char *host_name, uint8_t use_name)
|
|
Packit |
03b34a |
{
|
|
Packit |
03b34a |
#if !defined (__WIN32__)
|
|
Packit |
03b34a |
struct libnet_in6_addr addr;
|
|
Packit |
03b34a |
struct hostent *host_ent;
|
|
Packit |
03b34a |
#endif
|
|
Packit |
03b34a |
|
|
Packit |
03b34a |
if (use_name == LIBNET_RESOLVE)
|
|
Packit |
03b34a |
{
|
|
Packit |
03b34a |
#ifdef __WIN32__
|
|
Packit |
03b34a |
/* XXX - we don't support this yet */
|
|
Packit |
03b34a |
if (l)
|
|
Packit |
03b34a |
{
|
|
Packit |
03b34a |
snprintf(l->err_buf, LIBNET_ERRBUF_SIZE,
|
|
Packit |
03b34a |
"%s(): can't resolve IPv6 addresses\n", __func__);
|
|
Packit |
03b34a |
}
|
|
Packit |
03b34a |
return (in6addr_error);
|
|
Packit |
03b34a |
#else
|
|
Packit |
03b34a |
#ifdef HAVE_SOLARIS
|
|
Packit |
03b34a |
#ifdef HAVE_SOLARIS_IPV6
|
|
Packit |
03b34a |
if (!(host_ent = getipnodebyname((int8_t *)&addr,
|
|
Packit |
03b34a |
sizeof(struct in_addr), AF_INET6, NULL)))
|
|
Packit |
03b34a |
#else
|
|
Packit |
03b34a |
snprintf(l->err_buf, LIBNET_ERRBUF_SIZE,
|
|
Packit |
03b34a |
"%s(): %s\n", __func__, strerror(errno));
|
|
Packit |
03b34a |
return (in6addr_error);
|
|
Packit |
03b34a |
#endif
|
|
Packit |
03b34a |
#else
|
|
Packit |
03b34a |
if (!(host_ent = gethostbyname2(host_name, AF_INET6)))
|
|
Packit |
03b34a |
#endif
|
|
Packit |
03b34a |
{
|
|
Packit |
03b34a |
snprintf(l->err_buf, LIBNET_ERRBUF_SIZE,
|
|
Packit |
03b34a |
"%s(): %s", __func__, strerror(errno));
|
|
Packit |
03b34a |
return (in6addr_error);
|
|
Packit |
03b34a |
}
|
|
Packit |
03b34a |
memcpy(&addr, host_ent->h_addr, host_ent->h_length);
|
|
Packit |
03b34a |
return (addr);
|
|
Packit |
03b34a |
#endif /* !__WIN32__ */
|
|
Packit |
03b34a |
}
|
|
Packit |
03b34a |
else
|
|
Packit |
03b34a |
{
|
|
Packit |
03b34a |
#if defined(__WIN32__) /* Silence Win32 warning */
|
|
Packit |
03b34a |
if (l)
|
|
Packit |
03b34a |
{
|
|
Packit |
03b34a |
snprintf(l->err_buf, LIBNET_ERRBUF_SIZE,
|
|
Packit |
03b34a |
"%s(): can't resolve IPv6 addresses.\n", __func__);
|
|
Packit |
03b34a |
}
|
|
Packit |
03b34a |
return (in6addr_error);
|
|
Packit |
03b34a |
#else
|
|
Packit |
03b34a |
if(!inet_pton(AF_INET6, host_name, &addr))
|
|
Packit |
03b34a |
{
|
|
Packit |
03b34a |
if (l)
|
|
Packit |
03b34a |
{
|
|
Packit |
03b34a |
snprintf(l->err_buf, LIBNET_ERRBUF_SIZE,
|
|
Packit |
03b34a |
"%s(): invalid IPv6 address\n", __func__);
|
|
Packit |
03b34a |
}
|
|
Packit |
03b34a |
return (in6addr_error);
|
|
Packit |
03b34a |
}
|
|
Packit |
03b34a |
return (addr);
|
|
Packit |
03b34a |
#endif
|
|
Packit |
03b34a |
}
|
|
Packit |
03b34a |
}
|
|
Packit |
03b34a |
|
|
Packit |
03b34a |
#ifdef HAVE_GETIFADDRS
|
|
Packit |
03b34a |
|
|
Packit |
03b34a |
#include <ifaddrs.h>
|
|
Packit |
03b34a |
|
|
Packit |
03b34a |
struct libnet_in6_addr
|
|
Packit |
03b34a |
libnet_get_ipaddr6(libnet_t *l)
|
|
Packit |
03b34a |
{
|
|
Packit |
03b34a |
struct ifaddrs *ifaddr, *p;
|
|
Packit |
03b34a |
struct libnet_in6_addr addr;
|
|
Packit |
03b34a |
|
|
Packit |
03b34a |
if (l == NULL)
|
|
Packit |
03b34a |
{
|
|
Packit |
03b34a |
return (in6addr_error);
|
|
Packit |
03b34a |
}
|
|
Packit |
03b34a |
|
|
Packit |
03b34a |
if (getifaddrs(&ifaddr) != 0)
|
|
Packit |
03b34a |
{
|
|
Packit |
03b34a |
snprintf(l->err_buf, LIBNET_ERRBUF_SIZE,
|
|
Packit |
03b34a |
"%s(): getifaddrs(): %s\n", __func__, strerror(errno));
|
|
Packit |
03b34a |
return (in6addr_error);
|
|
Packit |
03b34a |
}
|
|
Packit |
03b34a |
|
|
Packit |
03b34a |
if (l->device == NULL)
|
|
Packit |
03b34a |
{
|
|
Packit |
03b34a |
if (libnet_select_device(l) == -1)
|
|
Packit |
03b34a |
{
|
|
Packit |
03b34a |
/* error msg set in libnet_select_device() */
|
|
Packit |
03b34a |
return (in6addr_error);
|
|
Packit |
03b34a |
}
|
|
Packit |
03b34a |
}
|
|
Packit |
03b34a |
|
|
Packit |
03b34a |
for (p = ifaddr; p != NULL; p = p->ifa_next)
|
|
Packit |
03b34a |
{
|
|
Packit |
03b34a |
if ((strcmp(p->ifa_name, l->device) == 0) && (p->ifa_addr != NULL) &&
|
|
Packit |
03b34a |
(p->ifa_addr->sa_family == AF_INET6))
|
|
Packit |
03b34a |
{
|
|
Packit |
03b34a |
memcpy(&addr.__u6_addr,
|
|
Packit |
03b34a |
((struct sockaddr_in6*)p->ifa_addr)->sin6_addr.s6_addr, 16);
|
|
Packit |
03b34a |
freeifaddrs(ifaddr);
|
|
Packit |
03b34a |
return (addr);
|
|
Packit |
03b34a |
}
|
|
Packit |
03b34a |
}
|
|
Packit |
03b34a |
|
|
Packit |
03b34a |
freeifaddrs(ifaddr);
|
|
Packit |
03b34a |
return (in6addr_error);
|
|
Packit |
03b34a |
}
|
|
Packit |
03b34a |
#else
|
|
Packit |
03b34a |
struct libnet_in6_addr
|
|
Packit |
03b34a |
libnet_get_ipaddr6(libnet_t *l)
|
|
Packit |
03b34a |
{
|
|
Packit |
03b34a |
snprintf(l->err_buf, LIBNET_ERRBUF_SIZE,
|
|
Packit |
03b34a |
"%s(): not yet Implemented\n", __func__);
|
|
Packit |
03b34a |
return (in6addr_error);
|
|
Packit |
03b34a |
}
|
|
Packit |
03b34a |
#endif /* WIN32 */
|
|
Packit |
03b34a |
|
|
Packit |
03b34a |
#if !defined(__WIN32__)
|
|
Packit |
03b34a |
uint32_t
|
|
Packit |
03b34a |
libnet_get_ipaddr4(libnet_t *l)
|
|
Packit |
03b34a |
{
|
|
Packit |
03b34a |
struct ifreq ifr;
|
|
Packit |
03b34a |
register struct sockaddr_in *sin;
|
|
Packit |
03b34a |
int fd;
|
|
Packit |
03b34a |
|
|
Packit |
03b34a |
if (l == NULL)
|
|
Packit |
03b34a |
{
|
|
Packit |
03b34a |
return (-1);
|
|
Packit |
03b34a |
}
|
|
Packit |
03b34a |
|
|
Packit |
03b34a |
/* create dummy socket to perform an ioctl upon */
|
|
Packit |
03b34a |
fd = socket(PF_INET, SOCK_DGRAM, 0);
|
|
Packit |
03b34a |
if (fd == -1)
|
|
Packit |
03b34a |
{
|
|
Packit |
03b34a |
snprintf(l->err_buf, LIBNET_ERRBUF_SIZE,
|
|
Packit |
03b34a |
"%s(): socket(): %s\n", __func__, strerror(errno));
|
|
Packit |
03b34a |
return (-1);
|
|
Packit |
03b34a |
}
|
|
Packit |
03b34a |
|
|
Packit |
03b34a |
sin = (struct sockaddr_in *)&ifr.ifr_addr;
|
|
Packit |
03b34a |
|
|
Packit |
03b34a |
if (l->device == NULL)
|
|
Packit |
03b34a |
{
|
|
Packit |
03b34a |
if (libnet_select_device(l) == -1)
|
|
Packit |
03b34a |
{
|
|
Packit |
03b34a |
/* error msg set in libnet_select_device() */
|
|
Packit |
03b34a |
close(fd);
|
|
Packit |
03b34a |
return (-1);
|
|
Packit |
03b34a |
}
|
|
Packit |
03b34a |
}
|
|
Packit |
03b34a |
strncpy(ifr.ifr_name, l->device, sizeof(ifr.ifr_name) -1);
|
|
Packit |
03b34a |
ifr.ifr_name[sizeof(ifr.ifr_name) - 1] = '\0';
|
|
Packit |
03b34a |
|
|
Packit |
03b34a |
ifr.ifr_addr.sa_family = AF_INET;
|
|
Packit |
03b34a |
|
|
Packit |
03b34a |
if (ioctl(fd, SIOCGIFADDR, (int8_t*) &ifr) < 0)
|
|
Packit |
03b34a |
{
|
|
Packit |
03b34a |
snprintf(l->err_buf, LIBNET_ERRBUF_SIZE,
|
|
Packit |
03b34a |
"%s(): ioctl(): %s\n", __func__, strerror(errno));
|
|
Packit |
03b34a |
close(fd);
|
|
Packit |
03b34a |
return (-1);
|
|
Packit |
03b34a |
}
|
|
Packit |
03b34a |
close(fd);
|
|
Packit |
03b34a |
return (sin->sin_addr.s_addr);
|
|
Packit |
03b34a |
}
|
|
Packit |
03b34a |
#else
|
|
Packit |
03b34a |
#include <Packet32.h>
|
|
Packit |
03b34a |
uint32_t
|
|
Packit |
03b34a |
libnet_get_ipaddr4(libnet_t *l)
|
|
Packit |
03b34a |
{
|
|
Packit |
03b34a |
long npflen = 0;
|
|
Packit |
03b34a |
struct sockaddr_in sin;
|
|
Packit |
03b34a |
struct npf_if_addr ipbuff;
|
|
Packit |
03b34a |
|
|
Packit |
03b34a |
memset(&sin,0,sizeof(sin));
|
|
Packit |
03b34a |
memset(&ipbuff,0,sizeof(ipbuff));
|
|
Packit |
03b34a |
|
|
Packit |
03b34a |
npflen = sizeof(ipbuff);
|
|
Packit |
03b34a |
if (PacketGetNetInfoEx(l->device, &ipbuff, &npflen))
|
|
Packit |
03b34a |
{
|
|
Packit |
03b34a |
sin = *(struct sockaddr_in *)&ipbuff.IPAddress;
|
|
Packit |
03b34a |
}
|
|
Packit |
03b34a |
return (sin.sin_addr.s_addr);
|
|
Packit |
03b34a |
}
|
|
Packit |
03b34a |
#endif /* WIN32 */
|
|
Packit |
03b34a |
|
|
Packit |
03b34a |
uint8_t *
|
|
Packit |
03b34a |
libnet_hex_aton(const char *s, int *len)
|
|
Packit |
03b34a |
{
|
|
Packit |
03b34a |
uint8_t *buf;
|
|
Packit |
03b34a |
int i;
|
|
Packit |
03b34a |
int32_t l;
|
|
Packit |
03b34a |
char *pp;
|
|
Packit |
03b34a |
|
|
Packit |
03b34a |
while (isspace(*s))
|
|
Packit |
03b34a |
{
|
|
Packit |
03b34a |
s++;
|
|
Packit |
03b34a |
}
|
|
Packit |
03b34a |
for (i = 0, *len = 0; s[i]; i++)
|
|
Packit |
03b34a |
{
|
|
Packit |
03b34a |
if (s[i] == ':')
|
|
Packit |
03b34a |
{
|
|
Packit |
03b34a |
(*len)++;
|
|
Packit |
03b34a |
}
|
|
Packit |
03b34a |
}
|
|
Packit |
03b34a |
buf = malloc(*len + 1);
|
|
Packit |
03b34a |
if (buf == NULL)
|
|
Packit |
03b34a |
{
|
|
Packit |
03b34a |
return (NULL);
|
|
Packit |
03b34a |
}
|
|
Packit |
03b34a |
/* expect len hex octets separated by ':' */
|
|
Packit |
03b34a |
for (i = 0; i < *len + 1; i++)
|
|
Packit |
03b34a |
{
|
|
Packit |
03b34a |
l = strtol(s, &pp, 16);
|
|
Packit |
03b34a |
if (pp == s || l > 0xff || l < 0)
|
|
Packit |
03b34a |
{
|
|
Packit |
03b34a |
*len = 0;
|
|
Packit |
03b34a |
free(buf);
|
|
Packit |
03b34a |
return (NULL);
|
|
Packit |
03b34a |
}
|
|
Packit |
03b34a |
if (!(*pp == ':' || (i == *len && (isspace(*pp) || *pp == '\0'))))
|
|
Packit |
03b34a |
{
|
|
Packit |
03b34a |
*len = 0;
|
|
Packit |
03b34a |
free(buf);
|
|
Packit |
03b34a |
return (NULL);
|
|
Packit |
03b34a |
}
|
|
Packit |
03b34a |
buf[i] = (uint8_t)l;
|
|
Packit |
03b34a |
s = pp + 1;
|
|
Packit |
03b34a |
}
|
|
Packit |
03b34a |
/* return int8_tacter after the octets ala strtol(3) */
|
|
Packit |
03b34a |
(*len)++;
|
|
Packit |
03b34a |
return (buf);
|
|
Packit |
03b34a |
}
|
|
Packit |
03b34a |
|
|
Packit |
03b34a |
/* EOF */
|