diff options
| author | Jelmer Vernooij <jelmer@samba.org> | 2008-03-05 19:42:15 +0100 | 
|---|---|---|
| committer | Jelmer Vernooij <jelmer@samba.org> | 2008-03-05 19:42:15 +0100 | 
| commit | 63036a6f3380652c0cb54627bdeabcd212fa2f8c (patch) | |
| tree | 90194f23cb1e6ca483e7773233c326a9b705f85f /source3/lib/replace/test/getifaddrs.c | |
| parent | d41d580c600e3228ff8fee5c16c47580f661a240 (diff) | |
| parent | 932c287a406048759fa1ac4bf86e29d96991ded1 (diff) | |
| download | samba-63036a6f3380652c0cb54627bdeabcd212fa2f8c.tar.gz samba-63036a6f3380652c0cb54627bdeabcd212fa2f8c.tar.bz2 samba-63036a6f3380652c0cb54627bdeabcd212fa2f8c.zip | |
Merge branch 'v3-2-test' of ssh://git.samba.org/data/git/samba into v3-2-test
(This used to be commit 3482cd9b0e81bbc801f1cec33fca82fc45a3ddef)
Diffstat (limited to 'source3/lib/replace/test/getifaddrs.c')
| -rw-r--r-- | source3/lib/replace/test/getifaddrs.c | 100 | 
1 files changed, 100 insertions, 0 deletions
| diff --git a/source3/lib/replace/test/getifaddrs.c b/source3/lib/replace/test/getifaddrs.c new file mode 100644 index 0000000000..8b00ac2f40 --- /dev/null +++ b/source3/lib/replace/test/getifaddrs.c @@ -0,0 +1,100 @@ +/* + * Unix SMB/CIFS implementation. + * + * libreplace getifaddrs test + * + * Copyright (C) Michael Adam <obnox@samba.org> 2008 + * + *  This program is free software; you can redistribute it and/or modify + *  it under the terms of the GNU General Public License as published by + *  the Free Software Foundation; either version 3 of the License, or + *  (at your option) any later version. + * + *  This program 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 General Public License for more details. + * + *  You should have received a copy of the GNU General Public License + *  along with this program.  If not, see <http://www.gnu.org/licenses/>. + */ + +#ifndef AUTOCONF_TEST +#include "replace.h" +#include "system/network.h" +#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); +#ifdef HAVE_STRUCT_SOCKADDR_IN6 +	} else if (addr->sa_family == AF_INET6) { +		result = rep_inet_ntop(AF_INET6, +				       &((struct sockaddr_in6 *)addr)->sin6_addr, +				       addrstring, +				       addrlen); +#endif +	} +	return result; +} + +int getifaddrs_test(void) +{ +	struct ifaddrs *ifs = NULL; +	struct ifaddrs *ifs_head = NULL; +	int ret; + +	ret = getifaddrs(&ifs); +	ifs_head = ifs; +	if (ret != 0) { +		fprintf(stderr, "getifaddrs() failed: %s\n", strerror(errno)); +		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; +	} + +	freeifaddrs(ifs_head); + +	return 0; +} | 
