| 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 | #endif
|
|---|
| 26 |
|
|---|
| 27 | #ifdef HAVE_INET_NTOP
|
|---|
| 28 | #define rep_inet_ntop inet_ntop
|
|---|
| 29 | #endif
|
|---|
| 30 |
|
|---|
| 31 | static const char *format_sockaddr(struct sockaddr *addr,
|
|---|
| 32 | char *addrstring,
|
|---|
| 33 | socklen_t addrlen)
|
|---|
| 34 | {
|
|---|
| 35 | const char *result = NULL;
|
|---|
| 36 |
|
|---|
| 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
|
|---|
| 49 | }
|
|---|
| 50 | return result;
|
|---|
| 51 | }
|
|---|
| 52 |
|
|---|
| 53 | int getifaddrs_test(void)
|
|---|
| 54 | {
|
|---|
| 55 | struct ifaddrs *ifs = NULL;
|
|---|
| 56 | struct ifaddrs *ifs_head = NULL;
|
|---|
| 57 | int ret;
|
|---|
| 58 |
|
|---|
| 59 | ret = getifaddrs(&ifs);
|
|---|
| 60 | ifs_head = ifs;
|
|---|
| 61 | if (ret != 0) {
|
|---|
| 62 | fprintf(stderr, "getifaddrs() failed: %s\n", strerror(errno));
|
|---|
| 63 | return 1;
|
|---|
| 64 | }
|
|---|
| 65 |
|
|---|
| 66 | while (ifs) {
|
|---|
| 67 | printf("%-10s ", ifs->ifa_name);
|
|---|
| 68 | if (ifs->ifa_addr != NULL) {
|
|---|
| 69 | char addrstring[INET6_ADDRSTRLEN];
|
|---|
| 70 | const char *result;
|
|---|
| 71 |
|
|---|
| 72 | result = format_sockaddr(ifs->ifa_addr,
|
|---|
| 73 | addrstring,
|
|---|
| 74 | sizeof(addrstring));
|
|---|
| 75 | if (result != NULL) {
|
|---|
| 76 | printf("IP=%s ", addrstring);
|
|---|
| 77 | }
|
|---|
| 78 |
|
|---|
| 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);
|
|---|
| 85 | }
|
|---|
| 86 | } else {
|
|---|
| 87 | printf("AF=%d ", ifs->ifa_addr->sa_family);
|
|---|
| 88 | }
|
|---|
| 89 | } else {
|
|---|
| 90 | printf("<no address>");
|
|---|
| 91 | }
|
|---|
| 92 |
|
|---|
| 93 | printf("\n");
|
|---|
| 94 | ifs = ifs->ifa_next;
|
|---|
| 95 | }
|
|---|
| 96 |
|
|---|
| 97 | freeifaddrs(ifs_head);
|
|---|
| 98 |
|
|---|
| 99 | return 0;
|
|---|
| 100 | }
|
|---|