2008-02-28 21:43:06 +01:00
/*
* Unix SMB / CIFS implementation .
*
* libreplace getifaddrs test
*
* Copyright ( C ) Michael Adam < obnox @ samba . org > 2008
*
2013-06-03 13:00:31 +02:00
* * * NOTE ! The following LGPL license applies to the replace
* * * library . This does NOT imply that all of Samba is released
* * * under the LGPL
2008-02-28 21:43:06 +01:00
*
2013-06-03 13:00:31 +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 3 of the License , or ( at your option ) any later version .
2008-02-28 21:43:06 +01:00
*
2013-06-03 13:00:31 +02:00
* 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
* Library 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 , see < http : //www.gnu.org/licenses/>.
2008-02-28 21:43:06 +01:00
*/
# ifndef AUTOCONF_TEST
# include "replace.h"
# include "system/network.h"
2011-03-19 00:44:47 +01:00
# include "replace-test.h"
2008-02-28 21:43:06 +01:00
# endif
# ifdef HAVE_INET_NTOP
# define rep_inet_ntop inet_ntop
# endif
static const char * format_sockaddr ( struct sockaddr * addr ,
char * addrstring ,
socklen_t addrlen )
{
const char * result = NULL ;
if ( addr - > sa_family = = AF_INET ) {
result = rep_inet_ntop ( AF_INET ,
& ( ( struct sockaddr_in * ) addr ) - > sin_addr ,
addrstring ,
addrlen ) ;
2008-02-29 02:43:24 +01:00
# ifdef HAVE_STRUCT_SOCKADDR_IN6
2008-02-28 21:43:06 +01:00
} else if ( addr - > sa_family = = AF_INET6 ) {
result = rep_inet_ntop ( AF_INET6 ,
& ( ( struct sockaddr_in6 * ) addr ) - > sin6_addr ,
addrstring ,
addrlen ) ;
2008-02-29 02:43:24 +01:00
# endif
2008-02-28 21:43:06 +01:00
}
return result ;
}
int getifaddrs_test ( void )
{
struct ifaddrs * ifs = NULL ;
2008-02-29 02:22:02 +01:00
struct ifaddrs * ifs_head = NULL ;
2008-02-28 21:43:06 +01:00
int ret ;
ret = getifaddrs ( & ifs ) ;
2008-02-29 02:22:02 +01:00
ifs_head = ifs ;
2008-02-28 21:43:06 +01:00
if ( ret ! = 0 ) {
2008-02-29 01:49:30 +01:00
fprintf ( stderr , " getifaddrs() failed: %s \n " , strerror ( errno ) ) ;
2008-02-28 21:43:06 +01:00
return 1 ;
}
while ( ifs ) {
printf ( " %-10s " , ifs - > ifa_name ) ;
if ( ifs - > ifa_addr ! = NULL ) {
char addrstring [ INET6_ADDRSTRLEN ] ;
const char * result ;
result = format_sockaddr ( ifs - > ifa_addr ,
addrstring ,
sizeof ( addrstring ) ) ;
if ( result ! = NULL ) {
printf ( " IP=%s " , addrstring ) ;
}
if ( ifs - > ifa_netmask ! = NULL ) {
result = format_sockaddr ( ifs - > ifa_netmask ,
addrstring ,
sizeof ( addrstring ) ) ;
if ( result ! = NULL ) {
printf ( " NETMASK=%s " , addrstring ) ;
}
} else {
printf ( " AF=%d " , ifs - > ifa_addr - > sa_family ) ;
}
} else {
printf ( " <no address> " ) ;
}
printf ( " \n " ) ;
ifs = ifs - > ifa_next ;
}
2008-02-29 02:22:02 +01:00
freeifaddrs ( ifs_head ) ;
2008-02-28 21:43:06 +01:00
return 0 ;
}