tdb upstream tarball: 1.1.3
[oss-qm-packages.git] / libreplace / test / getifaddrs.c
blob8b00ac2f40c5f20c5ad95cdf982f441670eaf021
1 /*
2 * Unix SMB/CIFS implementation.
4 * libreplace getifaddrs test
6 * Copyright (C) Michael Adam <obnox@samba.org> 2008
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 3 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program. If not, see <http://www.gnu.org/licenses/>.
22 #ifndef AUTOCONF_TEST
23 #include "replace.h"
24 #include "system/network.h"
25 #endif
27 #ifdef HAVE_INET_NTOP
28 #define rep_inet_ntop inet_ntop
29 #endif
31 static const char *format_sockaddr(struct sockaddr *addr,
32 char *addrstring,
33 socklen_t addrlen)
35 const char *result = NULL;
37 if (addr->sa_family == AF_INET) {
38 result = rep_inet_ntop(AF_INET,
39 &((struct sockaddr_in *)addr)->sin_addr,
40 addrstring,
41 addrlen);
42 #ifdef HAVE_STRUCT_SOCKADDR_IN6
43 } else if (addr->sa_family == AF_INET6) {
44 result = rep_inet_ntop(AF_INET6,
45 &((struct sockaddr_in6 *)addr)->sin6_addr,
46 addrstring,
47 addrlen);
48 #endif
50 return result;
53 int getifaddrs_test(void)
55 struct ifaddrs *ifs = NULL;
56 struct ifaddrs *ifs_head = NULL;
57 int ret;
59 ret = getifaddrs(&ifs);
60 ifs_head = ifs;
61 if (ret != 0) {
62 fprintf(stderr, "getifaddrs() failed: %s\n", strerror(errno));
63 return 1;
66 while (ifs) {
67 printf("%-10s ", ifs->ifa_name);
68 if (ifs->ifa_addr != NULL) {
69 char addrstring[INET6_ADDRSTRLEN];
70 const char *result;
72 result = format_sockaddr(ifs->ifa_addr,
73 addrstring,
74 sizeof(addrstring));
75 if (result != NULL) {
76 printf("IP=%s ", addrstring);
79 if (ifs->ifa_netmask != NULL) {
80 result = format_sockaddr(ifs->ifa_netmask,
81 addrstring,
82 sizeof(addrstring));
83 if (result != NULL) {
84 printf("NETMASK=%s", addrstring);
86 } else {
87 printf("AF=%d ", ifs->ifa_addr->sa_family);
89 } else {
90 printf("<no address>");
93 printf("\n");
94 ifs = ifs->ifa_next;
97 freeifaddrs(ifs_head);
99 return 0;