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