summaryrefslogtreecommitdiff
path: root/source3/lib/replace/test/getifaddrs.c
blob: 8b00ac2f40c5f20c5ad95cdf982f441670eaf021 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
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;
}