d325d8bb1bbe3831272a029c79a8fb7584770652
[obnox/samba/samba-obnox.git] / lib / replace / test / getifaddrs.c
1 /*
2  * Unix SMB/CIFS implementation.
3  *
4  * libreplace getifaddrs test
5  *
6  * Copyright (C) Michael Adam <obnox@samba.org> 2008
7  *
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.
12  *
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.
17  *
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/>.
20  */
21
22 #ifndef AUTOCONF_TEST
23 #include "replace.h"
24 #include "system/network.h"
25 #include "replace-test.h"
26 #endif
27
28 #ifdef HAVE_INET_NTOP
29 #define rep_inet_ntop inet_ntop
30 #endif
31
32 static const char *format_sockaddr(struct sockaddr *addr,
33                                    char *addrstring,
34                                    socklen_t addrlen)
35 {
36         const char *result = NULL;
37
38         if (addr->sa_family == AF_INET) {
39                 result = rep_inet_ntop(AF_INET,
40                                        &((struct sockaddr_in *)addr)->sin_addr,
41                                        addrstring,
42                                        addrlen);
43 #ifdef HAVE_STRUCT_SOCKADDR_IN6
44         } else if (addr->sa_family == AF_INET6) {
45                 result = rep_inet_ntop(AF_INET6,
46                                        &((struct sockaddr_in6 *)addr)->sin6_addr,
47                                        addrstring,
48                                        addrlen);
49 #endif
50         }
51         return result;
52 }
53
54 int getifaddrs_test(void)
55 {
56         struct ifaddrs *ifs = NULL;
57         struct ifaddrs *ifs_head = NULL;
58         int ret;
59
60         ret = getifaddrs(&ifs);
61         ifs_head = ifs;
62         if (ret != 0) {
63                 fprintf(stderr, "getifaddrs() failed: %s\n", strerror(errno));
64                 return 1;
65         }
66
67         while (ifs) {
68                 printf("%-10s ", ifs->ifa_name);
69                 if (ifs->ifa_addr != NULL) {
70                         char addrstring[INET6_ADDRSTRLEN];
71                         const char *result;
72
73                         result = format_sockaddr(ifs->ifa_addr,
74                                                  addrstring,
75                                                  sizeof(addrstring));
76                         if (result != NULL) {
77                                 printf("IP=%s ", addrstring);
78                         }
79
80                         if (ifs->ifa_netmask != NULL) {
81                                 result = format_sockaddr(ifs->ifa_netmask,
82                                                          addrstring,
83                                                          sizeof(addrstring));
84                                 if (result != NULL) {
85                                         printf("NETMASK=%s", addrstring);
86                                 }
87                         } else {
88                                 printf("AF=%d ", ifs->ifa_addr->sa_family);
89                         }
90                 } else {
91                         printf("<no address>");
92                 }
93
94                 printf("\n");
95                 ifs = ifs->ifa_next;
96         }
97
98         freeifaddrs(ifs_head);
99
100         return 0;
101 }