2013-04-27 15:52:49 +00:00
|
|
|
/* $Id: testgetifaddr.c,v 1.7 2013/04/27 15:38:57 nanard Exp $ */
|
2011-09-28 19:13:20 +00:00
|
|
|
/* MiniUPnP project
|
|
|
|
* http://miniupnp.free.fr/ or http://miniupnp.tuxfamily.org/
|
2014-03-03 11:37:01 +00:00
|
|
|
* (c) 2006-2014 Thomas Bernard
|
2011-09-28 19:13:20 +00:00
|
|
|
* This software is subject to the conditions detailed
|
|
|
|
* in the LICENCE file provided within the distribution */
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <syslog.h>
|
2013-04-27 15:52:49 +00:00
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/socket.h>
|
2013-03-23 10:50:57 +00:00
|
|
|
#include <netinet/in.h>
|
2013-04-27 15:52:49 +00:00
|
|
|
#include <arpa/inet.h>
|
2014-03-03 11:37:01 +00:00
|
|
|
#include "config.h"
|
2011-09-28 19:13:20 +00:00
|
|
|
#include "getifaddr.h"
|
|
|
|
|
2012-05-24 17:37:42 +00:00
|
|
|
#if defined(__sun)
|
|
|
|
/* solaris 10 does not define LOG_PERROR */
|
|
|
|
#define LOG_PERROR 0
|
|
|
|
#endif
|
|
|
|
|
2011-09-28 19:13:20 +00:00
|
|
|
int main(int argc, char * * argv) {
|
2013-03-23 10:50:57 +00:00
|
|
|
char str_addr[64];
|
|
|
|
struct in_addr addr;
|
|
|
|
struct in_addr mask;
|
2014-03-03 11:37:01 +00:00
|
|
|
#ifdef ENABLE_IPV6
|
|
|
|
int r;
|
|
|
|
char str_addr6[64];
|
|
|
|
#endif
|
2011-09-28 19:13:20 +00:00
|
|
|
if(argc < 2) {
|
|
|
|
fprintf(stderr, "Usage:\t%s interface_name\n", argv[0]);
|
|
|
|
return 1;
|
|
|
|
}
|
2012-05-24 17:37:42 +00:00
|
|
|
|
2011-09-28 19:13:20 +00:00
|
|
|
openlog("testgetifaddr", LOG_CONS|LOG_PERROR, LOG_USER);
|
2013-03-23 10:50:57 +00:00
|
|
|
if(getifaddr(argv[1], str_addr, sizeof(str_addr), &addr, &mask) < 0) {
|
2011-09-28 19:13:20 +00:00
|
|
|
fprintf(stderr, "Cannot get address for interface %s.\n", argv[1]);
|
|
|
|
return 1;
|
|
|
|
}
|
2013-03-23 10:50:57 +00:00
|
|
|
printf("Interface %s has IP address %s.\n", argv[1], str_addr);
|
2013-04-27 15:52:49 +00:00
|
|
|
printf("addr=%s ", inet_ntoa(addr));
|
|
|
|
printf("mask=%s\n", inet_ntoa(mask));
|
2014-03-03 11:37:01 +00:00
|
|
|
#ifdef ENABLE_IPV6
|
|
|
|
r = find_ipv6_addr(argv[1], str_addr6, sizeof(str_addr6));
|
|
|
|
if(r < 0) {
|
|
|
|
fprintf(stderr, "find_ipv6_addr() failed\n");
|
|
|
|
return 1;
|
|
|
|
} else if(r == 0) {
|
|
|
|
printf("Interface %s has no IPv6 address.\n", argv[1]);
|
|
|
|
} else {
|
|
|
|
printf("Interface %s has IPv6 address %s.\n", argv[1], str_addr6);
|
|
|
|
}
|
|
|
|
#endif
|
2011-09-28 19:13:20 +00:00
|
|
|
return 0;
|
|
|
|
}
|