1
0
mirror of https://github.com/samba-team/samba.git synced 2025-08-02 00:22:11 +03:00

Use common net utility code (address and sockaddr manipulation).

This commit is contained in:
Jelmer Vernooij
2008-10-23 20:41:15 +02:00
parent d1bc7e56d0
commit fe36fe8c3e
14 changed files with 361 additions and 468 deletions

View File

@ -69,7 +69,7 @@ static TALLOC_CTX *talloc_stackframe_internal(size_t poolsize)
TALLOC_CTX **tmp, *top, *parent; TALLOC_CTX **tmp, *top, *parent;
if (talloc_stack_arraysize < talloc_stacksize + 1) { if (talloc_stack_arraysize < talloc_stacksize + 1) {
tmp = TALLOC_REALLOC_ARRAY(NULL, talloc_stack, TALLOC_CTX *, tmp = talloc_realloc(NULL, talloc_stack, TALLOC_CTX *,
talloc_stacksize + 1); talloc_stacksize + 1);
if (tmp == NULL) { if (tmp == NULL) {
goto fail; goto fail;

View File

@ -541,12 +541,14 @@ _PUBLIC_ struct in_addr interpret_addr2(const char *str);
/** /**
Check if an IP is the 0.0.0.0. Check if an IP is the 0.0.0.0.
**/ **/
_PUBLIC_ bool is_zero_ip(struct in_addr ip); _PUBLIC_ bool is_zero_ip_v4(struct in_addr ip);
/** /**
Are two IPs on the same subnet? Are two IPs on the same subnet?
**/ **/
_PUBLIC_ bool same_net(struct in_addr ip1,struct in_addr ip2,struct in_addr mask); _PUBLIC_ bool same_net_v4(struct in_addr ip1,struct in_addr ip2,struct in_addr mask);
_PUBLIC_ bool is_ipaddress_v4(const char *str);
/** /**
Check if a process exists. Does this work on all unixes? Check if a process exists. Does this work on all unixes?

View File

@ -3,7 +3,7 @@
Samba utility functions Samba utility functions
Copyright (C) Jelmer Vernooij <jelmer@samba.org> 2008 Copyright (C) Jelmer Vernooij <jelmer@samba.org> 2008
Copyright (C) Andrew Tridgell 1992-1998 Copyright (C) Andrew Tridgell 1992-1998
Copyright (C) Jeremy Allison 2001-2002 Copyright (C) Jeremy Allison 2001-2007
Copyright (C) Simo Sorce 2001 Copyright (C) Simo Sorce 2001
Copyright (C) Jim McDonough (jmcd@us.ibm.com) 2003. Copyright (C) Jim McDonough (jmcd@us.ibm.com) 2003.
Copyright (C) James J Myers 2003 Copyright (C) James J Myers 2003
@ -26,50 +26,105 @@
#include "system/network.h" #include "system/network.h"
#include "system/locale.h" #include "system/locale.h"
#include "system/filesys.h" #include "system/filesys.h"
#undef strcasecmp
/** /**
Interpret an internet address or name into an IP address in 4 byte form. * Wrap getaddrinfo...
**/ */
_PUBLIC_ uint32_t interpret_addr(const char *str) bool interpret_string_addr_internal(struct addrinfo **ppres,
const char *str, int flags)
{ {
struct hostent *hp; int ret;
uint32_t res; struct addrinfo hints;
if (str == NULL || *str == 0 || memset(&hints, '\0', sizeof(hints));
strcmp(str,"0.0.0.0") == 0) { /* By default make sure it supports TCP. */
hints.ai_socktype = SOCK_STREAM;
hints.ai_flags = flags;
/* Linux man page on getaddinfo() says port will be
uninitialized when service string in NULL */
ret = getaddrinfo(str, NULL,
&hints,
ppres);
if (ret) {
DEBUG(3,("interpret_string_addr_internal: getaddrinfo failed "
"for name %s [%s]\n",
str,
gai_strerror(ret) ));
return false;
}
return true;
}
/**
* Interpret an internet address or name into an IP address in 4 byte form.
* RETURNS IN NETWORK BYTE ORDER (big endian).
*/
uint32_t interpret_addr(const char *str)
{
uint32_t ret;
/* If it's in the form of an IP address then
* get the lib to interpret it */
if (is_ipaddress_v4(str)) {
struct in_addr dest;
if (inet_pton(AF_INET, str, &dest) <= 0) {
/* Error - this shouldn't happen ! */
DEBUG(0,("interpret_addr: inet_pton failed "
"host %s\n",
str));
return 0;
}
ret = dest.s_addr; /* NETWORK BYTE ORDER ! */
} else {
/* Otherwise assume it's a network name of some sort and use
getadddrinfo. */
struct addrinfo *res = NULL;
struct addrinfo *res_list = NULL;
if (!interpret_string_addr_internal(&res_list,
str,
AI_ADDRCONFIG)) {
DEBUG(3,("interpret_addr: Unknown host. %s\n",str));
return 0;
}
/* Find the first IPv4 address. */
for (res = res_list; res; res = res->ai_next) {
if (res->ai_family != AF_INET) {
continue;
}
if (res->ai_addr == NULL) {
continue;
}
break;
}
if(res == NULL) {
DEBUG(3,("interpret_addr: host address is "
"invalid for host %s\n",str));
if (res_list) {
freeaddrinfo(res_list);
}
return 0;
}
memcpy((char *)&ret,
&((struct sockaddr_in *)res->ai_addr)->sin_addr.s_addr,
sizeof(ret));
if (res_list) {
freeaddrinfo(res_list);
}
}
/* This is so bogus - all callers need fixing... JRA. */
if (ret == (uint32_t)-1) {
return 0; return 0;
} }
if (strcmp(str,"255.255.255.255") == 0) {
return 0xFFFFFFFF;
}
/* recognise 'localhost' as a special name. This fixes problems with
some hosts that don't have localhost in /etc/hosts */
if (strcasecmp(str,"localhost") == 0) {
str = "127.0.0.1";
}
/* if it's in the form of an IP address then get the lib to interpret it */ return ret;
if (is_ipaddress(str)) {
res = inet_addr(str);
} else {
/* otherwise assume it's a network name of some sort and use
sys_gethostbyname */
if ((hp = sys_gethostbyname(str)) == 0) {
DEBUG(3,("sys_gethostbyname: Unknown host. %s\n",str));
return 0;
}
if(hp->h_addr == NULL) {
DEBUG(3,("sys_gethostbyname: host address is invalid for host %s\n",str));
return 0;
}
memcpy((char *)&res,(char *)hp->h_addr, 4);
}
if (res == (uint32_t)-1)
return(0);
return(res);
} }
/** /**
@ -87,7 +142,7 @@ _PUBLIC_ struct in_addr interpret_addr2(const char *str)
Check if an IP is the 0.0.0.0. Check if an IP is the 0.0.0.0.
**/ **/
_PUBLIC_ bool is_zero_ip(struct in_addr ip) _PUBLIC_ bool is_zero_ip_v4(struct in_addr ip)
{ {
return ip.s_addr == 0; return ip.s_addr == 0;
} }
@ -108,24 +163,248 @@ _PUBLIC_ bool same_net_v4(struct in_addr ip1, struct in_addr ip2, struct in_addr
} }
/** /**
Return true if a string could be a pure IP address. * Return true if a string could be an IPv4 address.
**/ */
_PUBLIC_ bool is_ipaddress(const char *str) bool is_ipaddress_v4(const char *str)
{ {
bool pure_address = true; int ret = -1;
int i; struct in_addr dest;
if (str == NULL) return false; ret = inet_pton(AF_INET, str, &dest);
if (ret > 0) {
for (i=0; pure_address && str[i]; i++) return true;
if (!(isdigit((int)str[i]) || str[i] == '.')) }
pure_address = false; return false;
/* Check that a pure number is not misinterpreted as an IP */
pure_address = pure_address && (strchr(str, '.') != NULL);
return pure_address;
} }
/**
* Return true if a string could be an IPv4 or IPv6 address.
*/
bool is_ipaddress(const char *str)
{
#if defined(HAVE_IPV6)
int ret = -1;
if (strchr_m(str, ':')) {
char addr[INET6_ADDRSTRLEN];
struct in6_addr dest6;
const char *sp = str;
char *p = strchr_m(str, '%');
/*
* Cope with link-local.
* This is IP:v6:addr%ifname.
*/
if (p && (p > str) && (if_nametoindex(p+1) != 0)) {
strlcpy(addr, str,
MIN(PTR_DIFF(p,str)+1,
sizeof(addr)));
sp = addr;
}
ret = inet_pton(AF_INET6, sp, &dest6);
if (ret > 0) {
return true;
}
}
#endif
return is_ipaddress_v4(str);
}
/**
* Is a sockaddr a broadcast address ?
*/
bool is_broadcast_addr(const struct sockaddr *pss)
{
#if defined(HAVE_IPV6)
if (pss->sa_family == AF_INET6) {
const struct in6_addr *sin6 =
&((const struct sockaddr_in6 *)pss)->sin6_addr;
return IN6_IS_ADDR_MULTICAST(sin6);
}
#endif
if (pss->sa_family == AF_INET) {
uint32_t addr =
ntohl(((const struct sockaddr_in *)pss)->sin_addr.s_addr);
return addr == INADDR_BROADCAST;
}
return false;
}
/**
* Check if an IPv7 is 127.0.0.1
*/
bool is_loopback_ip_v4(struct in_addr ip)
{
struct in_addr a;
a.s_addr = htonl(INADDR_LOOPBACK);
return(ip.s_addr == a.s_addr);
}
/**
* Check if a struct sockaddr is the loopback address.
*/
bool is_loopback_addr(const struct sockaddr *pss)
{
#if defined(HAVE_IPV6)
if (pss->sa_family == AF_INET6) {
const struct in6_addr *pin6 =
&((const struct sockaddr_in6 *)pss)->sin6_addr;
return IN6_IS_ADDR_LOOPBACK(pin6);
}
#endif
if (pss->sa_family == AF_INET) {
const struct in_addr *pin = &((const struct sockaddr_in *)pss)->sin_addr;
return is_loopback_ip_v4(*pin);
}
return false;
}
/**
* Check if a struct sockaddr has an unspecified address.
*/
bool is_zero_addr(const struct sockaddr *pss)
{
#if defined(HAVE_IPV6)
if (pss->sa_family == AF_INET6) {
const struct in6_addr *pin6 =
&((const struct sockaddr_in6 *)pss)->sin6_addr;
return IN6_IS_ADDR_UNSPECIFIED(pin6);
}
#endif
if (pss->sa_family == AF_INET) {
const struct in_addr *pin = &((const struct sockaddr_in *)pss)->sin_addr;
return is_zero_ip_v4(*pin);
}
return false;
}
/**
* Set an IP to 0.0.0.0.
*/
void zero_ip_v4(struct in_addr *ip)
{
memset(ip, '\0', sizeof(struct in_addr));
}
/**
* Convert an IPv4 struct in_addr to a struct sockaddr_storage.
*/
void in_addr_to_sockaddr_storage(struct sockaddr_storage *ss,
struct in_addr ip)
{
struct sockaddr_in *sa = (struct sockaddr_in *)ss;
memset(ss, '\0', sizeof(*ss));
sa->sin_family = AF_INET;
sa->sin_addr = ip;
}
#if defined(HAVE_IPV6)
/**
* Convert an IPv6 struct in_addr to a struct sockaddr_storage.
*/
void in6_addr_to_sockaddr_storage(struct sockaddr_storage *ss,
struct in6_addr ip)
{
struct sockaddr_in6 *sa = (struct sockaddr_in6 *)ss;
memset(ss, '\0', sizeof(*ss));
sa->sin6_family = AF_INET6;
sa->sin6_addr = ip;
}
#endif
/**
* Are two IPs on the same subnet?
*/
bool same_net(const struct sockaddr *ip1,
const struct sockaddr *ip2,
const struct sockaddr *mask)
{
if (ip1->sa_family != ip2->sa_family) {
/* Never on the same net. */
return false;
}
#if defined(HAVE_IPV6)
if (ip1->sa_family == AF_INET6) {
struct sockaddr_in6 ip1_6 = *(const struct sockaddr_in6 *)ip1;
struct sockaddr_in6 ip2_6 = *(const struct sockaddr_in6 *)ip2;
struct sockaddr_in6 mask_6 = *(const struct sockaddr_in6 *)mask;
char *p1 = (char *)&ip1_6.sin6_addr;
char *p2 = (char *)&ip2_6.sin6_addr;
char *m = (char *)&mask_6.sin6_addr;
int i;
for (i = 0; i < sizeof(struct in6_addr); i++) {
*p1++ &= *m;
*p2++ &= *m;
m++;
}
return (memcmp(&ip1_6.sin6_addr,
&ip2_6.sin6_addr,
sizeof(struct in6_addr)) == 0);
}
#endif
if (ip1->sa_family == AF_INET) {
return same_net_v4(((const struct sockaddr_in *)ip1)->sin_addr,
((const struct sockaddr_in *)ip2)->sin_addr,
((const struct sockaddr_in *)mask)->sin_addr);
}
return false;
}
/**
* Are two sockaddr 's the same family and address ? Ignore port etc.
*/
bool addr_equal(const struct sockaddr *ip1,
const struct sockaddr *ip2)
{
if (ip1->sa_family != ip2->sa_family) {
/* Never the same. */
return false;
}
#if defined(HAVE_IPV6)
if (ip1->sa_family == AF_INET6) {
return (memcmp(&((const struct sockaddr_in6 *)ip1)->sin6_addr,
&((const struct sockaddr_in6 *)ip2)->sin6_addr,
sizeof(struct in6_addr)) == 0);
}
#endif
if (ip1->sa_family == AF_INET) {
return (memcmp(&((const struct sockaddr_in *)ip1)->sin_addr,
&((const struct sockaddr_in *)ip2)->sin_addr,
sizeof(struct in_addr)) == 0);
}
return false;
}
/**
* Is an IP address the INADDR_ANY or in6addr_any value ?
*/
bool is_address_any(const struct sockaddr *psa)
{
#if defined(HAVE_IPV6)
if (psa->sa_family == AF_INET6) {
const struct sockaddr_in6 *si6 = (const struct sockaddr_in6 *)psa;
if (memcmp(&in6addr_any,
&si6->sin6_addr,
sizeof(in6addr_any)) == 0) {
return true;
}
return false;
}
#endif
if (psa->sa_family == AF_INET) {
const struct sockaddr_in *si = (const struct sockaddr_in *)psa;
if (si->sin_addr.s_addr == INADDR_ANY) {
return true;
}
return false;
}
return false;
}

View File

@ -321,7 +321,7 @@ UTIL_OBJ = ../lib/util/rbtree.o ../lib/util/signal.o ../lib/util/time.o \
../lib/util/util_file.o ../lib/util/data_blob.o \ ../lib/util/util_file.o ../lib/util/data_blob.o \
../lib/util/util.o ../lib/util/fsusage.o \ ../lib/util/util.o ../lib/util/fsusage.o \
../lib/util/params.o ../lib/util/talloc_stack.o \ ../lib/util/params.o ../lib/util/talloc_stack.o \
../lib/util/genrand.o ../lib/util/genrand.o ../lib/util/util_net.o
CRYPTO_OBJ = ../lib/crypto/crc32.o ../lib/crypto/md5.o \ CRYPTO_OBJ = ../lib/crypto/crc32.o ../lib/crypto/md5.o \
../lib/crypto/hmacmd5.o ../lib/crypto/arcfour.o \ ../lib/crypto/hmacmd5.o ../lib/crypto/arcfour.o \

View File

@ -1476,11 +1476,13 @@ NTSTATUS sid_array_from_info3(TALLOC_CTX *mem_ctx,
/* The following definitions come from lib/util_sock.c */ /* The following definitions come from lib/util_sock.c */
bool interpret_string_addr_internal(struct addrinfo **ppres,
const char *str, int flags);
bool is_ipaddress_v4(const char *str); bool is_ipaddress_v4(const char *str);
bool is_ipaddress(const char *str); bool is_ipaddress(const char *str);
bool is_broadcast_addr(const struct sockaddr *pss); bool is_broadcast_addr(const struct sockaddr *pss);
uint32 interpret_addr(const char *str); uint32 interpret_addr(const char *str);
struct in_addr *interpret_addr2(struct in_addr *ip, const char *str); struct in_addr interpret_addr2(const char *str);
bool interpret_string_addr(struct sockaddr_storage *pss, bool interpret_string_addr(struct sockaddr_storage *pss,
const char *str, const char *str,
int flags); int flags);

View File

@ -21,188 +21,6 @@
#include "includes.h" #include "includes.h"
/****************************************************************************
Return true if a string could be an IPv4 address.
****************************************************************************/
bool is_ipaddress_v4(const char *str)
{
int ret = -1;
struct in_addr dest;
ret = inet_pton(AF_INET, str, &dest);
if (ret > 0) {
return true;
}
return false;
}
/****************************************************************************
Return true if a string could be an IPv4 or IPv6 address.
****************************************************************************/
bool is_ipaddress(const char *str)
{
#if defined(HAVE_IPV6)
int ret = -1;
if (strchr_m(str, ':')) {
char addr[INET6_ADDRSTRLEN];
struct in6_addr dest6;
const char *sp = str;
char *p = strchr_m(str, '%');
/*
* Cope with link-local.
* This is IP:v6:addr%ifname.
*/
if (p && (p > str) && (if_nametoindex(p+1) != 0)) {
strlcpy(addr, str,
MIN(PTR_DIFF(p,str)+1,
sizeof(addr)));
sp = addr;
}
ret = inet_pton(AF_INET6, sp, &dest6);
if (ret > 0) {
return true;
}
}
#endif
return is_ipaddress_v4(str);
}
/****************************************************************************
Is a sockaddr a broadcast address ?
****************************************************************************/
bool is_broadcast_addr(const struct sockaddr *pss)
{
#if defined(HAVE_IPV6)
if (pss->sa_family == AF_INET6) {
const struct in6_addr *sin6 =
&((const struct sockaddr_in6 *)pss)->sin6_addr;
return IN6_IS_ADDR_MULTICAST(sin6);
}
#endif
if (pss->sa_family == AF_INET) {
uint32_t addr =
ntohl(((const struct sockaddr_in *)pss)->sin_addr.s_addr);
return addr == INADDR_BROADCAST;
}
return false;
}
/*******************************************************************
Wrap getaddrinfo...
******************************************************************/
static bool interpret_string_addr_internal(struct addrinfo **ppres,
const char *str, int flags)
{
int ret;
struct addrinfo hints;
memset(&hints, '\0', sizeof(hints));
/* By default make sure it supports TCP. */
hints.ai_socktype = SOCK_STREAM;
hints.ai_flags = flags;
/* Linux man page on getaddinfo() says port will be
uninitialized when service string in NULL */
ret = getaddrinfo(str, NULL,
&hints,
ppres);
if (ret) {
DEBUG(3,("interpret_string_addr_internal: getaddrinfo failed "
"for name %s [%s]\n",
str,
gai_strerror(ret) ));
return false;
}
return true;
}
/****************************************************************************
Interpret an internet address or name into an IP address in 4 byte form.
RETURNS IN NETWORK BYTE ORDER (big endian).
****************************************************************************/
uint32 interpret_addr(const char *str)
{
uint32 ret;
/* If it's in the form of an IP address then
* get the lib to interpret it */
if (is_ipaddress_v4(str)) {
struct in_addr dest;
if (inet_pton(AF_INET, str, &dest) <= 0) {
/* Error - this shouldn't happen ! */
DEBUG(0,("interpret_addr: inet_pton failed "
"host %s\n",
str));
return 0;
}
ret = dest.s_addr; /* NETWORK BYTE ORDER ! */
} else {
/* Otherwise assume it's a network name of some sort and use
getadddrinfo. */
struct addrinfo *res = NULL;
struct addrinfo *res_list = NULL;
if (!interpret_string_addr_internal(&res_list,
str,
AI_ADDRCONFIG)) {
DEBUG(3,("interpret_addr: Unknown host. %s\n",str));
return 0;
}
/* Find the first IPv4 address. */
for (res = res_list; res; res = res->ai_next) {
if (res->ai_family != AF_INET) {
continue;
}
if (res->ai_addr == NULL) {
continue;
}
break;
}
if(res == NULL) {
DEBUG(3,("interpret_addr: host address is "
"invalid for host %s\n",str));
if (res_list) {
freeaddrinfo(res_list);
}
return 0;
}
putip((char *)&ret,
&((struct sockaddr_in *)res->ai_addr)->sin_addr.s_addr);
if (res_list) {
freeaddrinfo(res_list);
}
}
/* This is so bogus - all callers need fixing... JRA. */
if (ret == (uint32)-1) {
return 0;
}
return ret;
}
/*******************************************************************
A convenient addition to interpret_addr().
******************************************************************/
struct in_addr *interpret_addr2(struct in_addr *ip, const char *str)
{
uint32 a = interpret_addr(str);
ip->s_addr = a;
return ip;
}
/******************************************************************* /*******************************************************************
Map a text hostname or IP address (IPv4 or IPv6) into a Map a text hostname or IP address (IPv4 or IPv6) into a
struct sockaddr_storage. struct sockaddr_storage.
@ -259,77 +77,6 @@ bool interpret_string_addr(struct sockaddr_storage *pss,
return true; return true;
} }
/*******************************************************************
Check if an IPv7 is 127.0.0.1
******************************************************************/
bool is_loopback_ip_v4(struct in_addr ip)
{
struct in_addr a;
a.s_addr = htonl(INADDR_LOOPBACK);
return(ip.s_addr == a.s_addr);
}
/*******************************************************************
Check if a struct sockaddr is the loopback address.
******************************************************************/
bool is_loopback_addr(const struct sockaddr *pss)
{
#if defined(HAVE_IPV6)
if (pss->sa_family == AF_INET6) {
struct in6_addr *pin6 =
&((struct sockaddr_in6 *)pss)->sin6_addr;
return IN6_IS_ADDR_LOOPBACK(pin6);
}
#endif
if (pss->sa_family == AF_INET) {
struct in_addr *pin = &((struct sockaddr_in *)pss)->sin_addr;
return is_loopback_ip_v4(*pin);
}
return false;
}
/*******************************************************************
Check if an IPv4 is 0.0.0.0.
******************************************************************/
bool is_zero_ip_v4(struct in_addr ip)
{
uint32 a;
putip((char *)&a,(char *)&ip);
return(a == 0);
}
/*******************************************************************
Check if a struct sockaddr has an unspecified address.
******************************************************************/
bool is_zero_addr(const struct sockaddr *pss)
{
#if defined(HAVE_IPV6)
if (pss->sa_family == AF_INET6) {
struct in6_addr *pin6 =
&((struct sockaddr_in6 *)pss)->sin6_addr;
return IN6_IS_ADDR_UNSPECIFIED(pin6);
}
#endif
if (pss->sa_family == AF_INET) {
struct in_addr *pin = &((struct sockaddr_in *)pss)->sin_addr;
return is_zero_ip_v4(*pin);
}
return false;
}
/*******************************************************************
Set an IP to 0.0.0.0.
******************************************************************/
void zero_ip_v4(struct in_addr *ip)
{
memset(ip, '\0', sizeof(struct in_addr));
}
/******************************************************************* /*******************************************************************
Set an address to INADDR_ANY. Set an address to INADDR_ANY.
******************************************************************/ ******************************************************************/
@ -341,144 +88,6 @@ void zero_addr(struct sockaddr_storage *pss)
pss->ss_family = AF_INET; pss->ss_family = AF_INET;
} }
/*******************************************************************
Are two IPs on the same subnet - IPv4 version ?
********************************************************************/
bool same_net_v4(struct in_addr ip1,struct in_addr ip2,struct in_addr mask)
{
uint32 net1,net2,nmask;
nmask = ntohl(mask.s_addr);
net1 = ntohl(ip1.s_addr);
net2 = ntohl(ip2.s_addr);
return((net1 & nmask) == (net2 & nmask));
}
/*******************************************************************
Convert an IPv4 struct in_addr to a struct sockaddr_storage.
********************************************************************/
void in_addr_to_sockaddr_storage(struct sockaddr_storage *ss,
struct in_addr ip)
{
struct sockaddr_in *sa = (struct sockaddr_in *)ss;
memset(ss, '\0', sizeof(*ss));
sa->sin_family = AF_INET;
sa->sin_addr = ip;
}
#if defined(HAVE_IPV6)
/*******************************************************************
Convert an IPv6 struct in_addr to a struct sockaddr_storage.
********************************************************************/
void in6_addr_to_sockaddr_storage(struct sockaddr_storage *ss,
struct in6_addr ip)
{
struct sockaddr_in6 *sa = (struct sockaddr_in6 *)ss;
memset(ss, '\0', sizeof(*ss));
sa->sin6_family = AF_INET6;
sa->sin6_addr = ip;
}
#endif
/*******************************************************************
Are two IPs on the same subnet?
********************************************************************/
bool same_net(const struct sockaddr *ip1,
const struct sockaddr *ip2,
const struct sockaddr *mask)
{
if (ip1->sa_family != ip2->sa_family) {
/* Never on the same net. */
return false;
}
#if defined(HAVE_IPV6)
if (ip1->sa_family == AF_INET6) {
struct sockaddr_in6 ip1_6 = *(struct sockaddr_in6 *)ip1;
struct sockaddr_in6 ip2_6 = *(struct sockaddr_in6 *)ip2;
struct sockaddr_in6 mask_6 = *(struct sockaddr_in6 *)mask;
char *p1 = (char *)&ip1_6.sin6_addr;
char *p2 = (char *)&ip2_6.sin6_addr;
char *m = (char *)&mask_6.sin6_addr;
int i;
for (i = 0; i < sizeof(struct in6_addr); i++) {
*p1++ &= *m;
*p2++ &= *m;
m++;
}
return (memcmp(&ip1_6.sin6_addr,
&ip2_6.sin6_addr,
sizeof(struct in6_addr)) == 0);
}
#endif
if (ip1->sa_family == AF_INET) {
return same_net_v4(((const struct sockaddr_in *)ip1)->sin_addr,
((const struct sockaddr_in *)ip2)->sin_addr,
((const struct sockaddr_in *)mask)->sin_addr);
}
return false;
}
/*******************************************************************
Are two sockaddr 's the same family and address ? Ignore port etc.
********************************************************************/
bool addr_equal(const struct sockaddr *ip1,
const struct sockaddr *ip2)
{
if (ip1->sa_family != ip2->sa_family) {
/* Never the same. */
return false;
}
#if defined(HAVE_IPV6)
if (ip1->sa_family == AF_INET6) {
return (memcmp(&((const struct sockaddr_in6 *)ip1)->sin6_addr,
&((const struct sockaddr_in6 *)ip2)->sin6_addr,
sizeof(struct in6_addr)) == 0);
}
#endif
if (ip1->sa_family == AF_INET) {
return (memcmp(&((const struct sockaddr_in *)ip1)->sin_addr,
&((const struct sockaddr_in *)ip2)->sin_addr,
sizeof(struct in_addr)) == 0);
}
return false;
}
/****************************************************************************
Is an IP address the INADDR_ANY or in6addr_any value ?
****************************************************************************/
bool is_address_any(const struct sockaddr *psa)
{
#if defined(HAVE_IPV6)
if (psa->sa_family == AF_INET6) {
struct sockaddr_in6 *si6 = (struct sockaddr_in6 *)psa;
if (memcmp(&in6addr_any,
&si6->sin6_addr,
sizeof(in6addr_any)) == 0) {
return true;
}
return false;
}
#endif
if (psa->sa_family == AF_INET) {
struct sockaddr_in *si = (struct sockaddr_in *)psa;
if (si->sin_addr.s_addr == INADDR_ANY) {
return true;
}
return false;
}
return false;
}
/**************************************************************************** /****************************************************************************
Get a port number in host byte order from a sockaddr_storage. Get a port number in host byte order from a sockaddr_storage.
****************************************************************************/ ****************************************************************************/
@ -1555,7 +1164,7 @@ int open_udp_socket(const char *host, int port)
int res; int res;
struct in_addr addr; struct in_addr addr;
(void)interpret_addr2(&addr, host); addr = interpret_addr2(host);
res = socket(PF_INET, type, 0); res = socket(PF_INET, type, 0);
if (res == -1) { if (res == -1) {

View File

@ -183,11 +183,11 @@ static void parse_ip(struct tagged_ip *ip, const char *str)
char *s = strchr(str, ':'); char *s = strchr(str, ':');
if (!s) { if (!s) {
fstrcpy(ip->tag, "*"); fstrcpy(ip->tag, "*");
(void)interpret_addr2(&ip->ip,str); ip->ip = interpret_addr2(str);
return; return;
} }
(void)interpret_addr2(&ip->ip,s+1); ip->ip = interpret_addr2(s+1);
fstrcpy(ip->tag, str); fstrcpy(ip->tag, str);
s = strchr(ip->tag, ':'); s = strchr(ip->tag, ':');
if (s) { if (s) {

View File

@ -168,7 +168,8 @@ bool register_my_workgroup_and_names(void)
namerec = find_name_on_subnet(unicast_subnet, &nmbname, FIND_SELF_NAME); namerec = find_name_on_subnet(unicast_subnet, &nmbname, FIND_SELF_NAME);
if (namerec == NULL) continue; if (namerec == NULL) continue;
for (a=0;cluster_addresses[a];a++) { for (a=0;cluster_addresses[a];a++) {
add_ip_to_name_record(namerec, *interpret_addr2(&ip, cluster_addresses[a])); ip = interpret_addr2(cluster_addresses[a]);
add_ip_to_name_record(namerec, ip);
} }
} }
} }

View File

@ -490,7 +490,7 @@ void announce_remote(time_t t)
else else
wgroup = pwgroup; wgroup = pwgroup;
(void)interpret_addr2(&addr,s2); addr = interpret_addr2(s2);
/* Announce all our names including aliases */ /* Announce all our names including aliases */
/* Give the ip address as the address of our first /* Give the ip address as the address of our first
@ -574,7 +574,7 @@ for workgroup %s on subnet %s.\n", lp_workgroup(), FIRST_SUBNET->subnet_name ));
frame = talloc_stackframe(); frame = talloc_stackframe();
for (ptr=s; next_token_talloc(frame,&ptr,&s2,NULL); ) { for (ptr=s; next_token_talloc(frame,&ptr,&s2,NULL); ) {
/* The entries are of the form a.b.c.d */ /* The entries are of the form a.b.c.d */
(void)interpret_addr2(&addr,s2); addr = interpret_addr2(s2);
DEBUG(5,("announce_remote: Doing remote browse sync announce for server %s to IP %s.\n", DEBUG(5,("announce_remote: Doing remote browse sync announce for server %s to IP %s.\n",
global_myname(), inet_ntoa(addr) )); global_myname(), inet_ntoa(addr) ));

View File

@ -701,7 +701,7 @@ bool initialise_wins(void)
next_token_talloc(frame,&ptr,&ttl_str,NULL); next_token_talloc(frame,&ptr,&ttl_str,NULL);
for(i = 0; i < num_ips; i++) { for(i = 0; i < num_ips; i++) {
next_token_talloc(frame,&ptr, &ip_str, NULL); next_token_talloc(frame,&ptr, &ip_str, NULL);
(void)interpret_addr2(&ip_list[i], ip_str); ip_list[i] = interpret_addr2(ip_str);
} }
next_token_talloc(frame,&ptr,&nb_flags_str,NULL); next_token_talloc(frame,&ptr,&nb_flags_str,NULL);
@ -835,7 +835,7 @@ void wins_process_name_refresh_request( struct subnet_record *subrec,
struct in_addr from_ip; struct in_addr from_ip;
struct in_addr our_fake_ip; struct in_addr our_fake_ip;
(void)interpret_addr2(&our_fake_ip, "0.0.0.0"); our_fake_ip = interpret_addr2("0.0.0.0");
putip( (char *)&from_ip, &nmb->additional->rdata[2] ); putip( (char *)&from_ip, &nmb->additional->rdata[2] );
if(bcast) { if(bcast) {
@ -1142,7 +1142,7 @@ void wins_process_name_registration_request(struct subnet_record *subrec,
bool registering_group_name = (nb_flags & NB_GROUP) ? True : False; bool registering_group_name = (nb_flags & NB_GROUP) ? True : False;
struct in_addr our_fake_ip; struct in_addr our_fake_ip;
(void)interpret_addr2(&our_fake_ip, "0.0.0.0"); our_fake_ip = interpret_addr2("0.0.0.0");
putip((char *)&from_ip,&nmb->additional->rdata[2]); putip((char *)&from_ip,&nmb->additional->rdata[2]);
if(bcast) { if(bcast) {
@ -1217,7 +1217,7 @@ to register name %s. Name already exists in WINS with source type %d.\n",
*/ */
if(registering_group_name && (question->name_type != 0x1c)) { if(registering_group_name && (question->name_type != 0x1c)) {
(void)interpret_addr2(&from_ip, "255.255.255.255"); from_ip = interpret_addr2("255.255.255.255");
} }
/* /*
@ -1424,7 +1424,7 @@ static void wins_multihomed_register_query_success(struct subnet_record *subrec,
int ttl; int ttl;
struct in_addr our_fake_ip; struct in_addr our_fake_ip;
(void)interpret_addr2(&our_fake_ip, "0.0.0.0"); our_fake_ip = interpret_addr2("0.0.0.0");
memcpy((char *)&orig_reg_packet, userdata->data, sizeof(struct packet_struct *)); memcpy((char *)&orig_reg_packet, userdata->data, sizeof(struct packet_struct *));
nmb = &orig_reg_packet->packet.nmb; nmb = &orig_reg_packet->packet.nmb;
@ -1515,7 +1515,7 @@ void wins_process_multihomed_name_registration_request( struct subnet_record *su
struct in_addr our_fake_ip; struct in_addr our_fake_ip;
unstring qname; unstring qname;
(void)interpret_addr2(&our_fake_ip, "0.0.0.0"); our_fake_ip = interpret_addr2("0.0.0.0");
putip((char *)&from_ip,&nmb->additional->rdata[2]); putip((char *)&from_ip,&nmb->additional->rdata[2]);
if(bcast) { if(bcast) {
@ -2141,7 +2141,7 @@ static int wins_processing_traverse_fn(TDB_CONTEXT *tdb, TDB_DATA kbuf, TDB_DATA
struct name_record *namerec = NULL; struct name_record *namerec = NULL;
struct in_addr our_fake_ip; struct in_addr our_fake_ip;
(void)interpret_addr2(&our_fake_ip, "0.0.0.0"); our_fake_ip = interpret_addr2("0.0.0.0");
if (kbuf.dsize != sizeof(unstring) + 1) { if (kbuf.dsize != sizeof(unstring) + 1) {
return 0; return 0;
} }
@ -2422,7 +2422,7 @@ void nmbd_wins_new_entry(struct messaging_context *msg,
struct in_addr our_fake_ip; struct in_addr our_fake_ip;
int i; int i;
(void)interpret_addr2(&our_fake_ip, "0.0.0.0"); our_fake_ip = interpret_addr2("0.0.0.0");
if (buf==NULL) { if (buf==NULL) {
return; return;
} }

View File

@ -332,7 +332,7 @@ int main(int argc,char *argv[])
if(lookup_by_ip) { if(lookup_by_ip) {
struct sockaddr_storage ss; struct sockaddr_storage ss;
(void)interpret_addr2(&ip, lookup); ip = interpret_addr2(lookup);
in_addr_to_sockaddr_storage(&ss, ip); in_addr_to_sockaddr_storage(&ss, ip);
fstrcpy(lookup,"*"); fstrcpy(lookup,"*");
do_node_status(ServerFD, lookup, lookup_type, &ss); do_node_status(ServerFD, lookup, lookup_type, &ss);

View File

@ -1129,7 +1129,7 @@ static bool do_nodestatus(struct messaging_context *msg_ctx,
ZERO_STRUCT(p); ZERO_STRUCT(p);
(void)interpret_addr2(&p.ip, argv[1]); p.ip = interpret_addr2(argv[1]);
p.port = 137; p.port = 137;
p.packet_type = NMB_PACKET; p.packet_type = NMB_PACKET;

View File

@ -800,7 +800,7 @@ static void parse_mount_smb(int argc, char **argv)
DEBUGLEVEL = val; DEBUGLEVEL = val;
} else if(!strcmp(opts, "ip")) { } else if(!strcmp(opts, "ip")) {
dest_ip = interpret_addr2(opteq+1); dest_ip = interpret_addr2(opteq+1);
if (is_zero_ip(dest_ip)) { if (is_zero_ip_v4(dest_ip)) {
fprintf(stderr,"Can't resolve address %s\n", opteq+1); fprintf(stderr,"Can't resolve address %s\n", opteq+1);
exit(1); exit(1);
} }

View File

@ -49,7 +49,7 @@ static struct interface *iface_find(struct interface *interfaces,
struct in_addr ip, bool CheckMask) struct in_addr ip, bool CheckMask)
{ {
struct interface *i; struct interface *i;
if (is_zero_ip(ip)) return interfaces; if (is_zero_ip_v4(ip)) return interfaces;
for (i=interfaces;i;i=i->next) for (i=interfaces;i;i=i->next)
if (CheckMask) { if (CheckMask) {