1/* $Id: testgetifaddr.c,v 1.7 2013/04/27 15:38:57 nanard Exp $ */ 2/* MiniUPnP project 3 * http://miniupnp.free.fr/ or http://miniupnp.tuxfamily.org/ 4 * (c) 2006-2014 Thomas Bernard 5 * This software is subject to the conditions detailed 6 * in the LICENCE file provided within the distribution */ 7#include <stdio.h> 8#include <syslog.h> 9#include <sys/types.h> 10#include <sys/socket.h> 11#include <netinet/in.h> 12#include <arpa/inet.h> 13#include "config.h" 14#include "getifaddr.h" 15 16#if defined(__sun) 17/* solaris 10 does not define LOG_PERROR */ 18#define LOG_PERROR 0 19#endif 20 21int main(int argc, char * * argv) { 22 char str_addr[64]; 23 struct in_addr addr; 24 struct in_addr mask; 25#ifdef ENABLE_IPV6 26 int r; 27 char str_addr6[64]; 28#endif 29 if(argc < 2) { 30 fprintf(stderr, "Usage:\t%s interface_name\n", argv[0]); 31 return 1; 32 } 33 34 openlog("testgetifaddr", LOG_CONS|LOG_PERROR, LOG_USER); 35 if(getifaddr(argv[1], str_addr, sizeof(str_addr), &addr, &mask) < 0) { 36 fprintf(stderr, "Cannot get address for interface %s.\n", argv[1]); 37 return 1; 38 } 39 printf("Interface %s has IP address %s.\n", argv[1], str_addr); 40 printf("addr=%s ", inet_ntoa(addr)); 41 printf("mask=%s\n", inet_ntoa(mask)); 42#ifdef ENABLE_IPV6 43 r = find_ipv6_addr(argv[1], str_addr6, sizeof(str_addr6)); 44 if(r < 0) { 45 fprintf(stderr, "find_ipv6_addr() failed\n"); 46 return 1; 47 } else if(r == 0) { 48 printf("Interface %s has no IPv6 address.\n", argv[1]); 49 } else { 50 printf("Interface %s has IPv6 address %s.\n", argv[1], str_addr6); 51 } 52#endif 53 return 0; 54} 55