From d24f3b8a9367cb3903e1d76fa66a14389554da33 Mon Sep 17 00:00:00 2001 From: Michael Adam Date: Thu, 28 Feb 2008 21:43:06 +0100 Subject: libreplace: add extended getifaddrs test that prints out the interfaces. Michael cherry-picked from libreplace-part of 9d2bab09aac22c00fe23f1e1265a2dbd0901e9ce and adapted replacetort creation (This used to be commit 52d79ad4872a20cf55f31aba97629c2561bfc16c) --- source3/lib/replace/test/getifaddrs.c | 96 +++++++++++++++++++++++++++++++++++ 1 file changed, 96 insertions(+) create mode 100644 source3/lib/replace/test/getifaddrs.c (limited to 'source3/lib/replace/test/getifaddrs.c') diff --git a/source3/lib/replace/test/getifaddrs.c b/source3/lib/replace/test/getifaddrs.c new file mode 100644 index 0000000000..66eed70268 --- /dev/null +++ b/source3/lib/replace/test/getifaddrs.c @@ -0,0 +1,96 @@ +/* + * Unix SMB/CIFS implementation. + * + * libreplace getifaddrs test + * + * Copyright (C) Michael Adam 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 . + */ + +#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); + } else if (addr->sa_family == AF_INET6) { + result = rep_inet_ntop(AF_INET6, + &((struct sockaddr_in6 *)addr)->sin6_addr, + addrstring, + addrlen); + } + return result; +} + +int getifaddrs_test(void) +{ + struct ifaddrs *ifs = NULL; + int ret; + + ret = getifaddrs(&ifs); + if (ret != 0) { + fprintf(stderr, "getifaddrs() failed: %s", 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(""); + } + + printf("\n"); + ifs = ifs->ifa_next; + } + + freeifaddrs(ifs); + + return 0; +} -- cgit From 3be85e76a82c4d67a66e2c6e86a7a9d5c394815f Mon Sep 17 00:00:00 2001 From: Michael Adam Date: Fri, 29 Feb 2008 01:49:30 +0100 Subject: libreplace: add missing newline in output of getifaddrs test. Michael (cherry picked from commit f8243cfc47c7414bab7f249d0e5d1c85e8ca7d64) (This used to be commit 3c1843f931b896dd3bb142346ecb44c0fb9202d6) --- source3/lib/replace/test/getifaddrs.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'source3/lib/replace/test/getifaddrs.c') diff --git a/source3/lib/replace/test/getifaddrs.c b/source3/lib/replace/test/getifaddrs.c index 66eed70268..4455462193 100644 --- a/source3/lib/replace/test/getifaddrs.c +++ b/source3/lib/replace/test/getifaddrs.c @@ -55,7 +55,7 @@ int getifaddrs_test(void) ret = getifaddrs(&ifs); if (ret != 0) { - fprintf(stderr, "getifaddrs() failed: %s", strerror(errno)); + fprintf(stderr, "getifaddrs() failed: %s\n", strerror(errno)); return 1; } -- cgit From b6578dc7fedcb98186940dc82d8faccac552a361 Mon Sep 17 00:00:00 2001 From: Michael Adam Date: Fri, 29 Feb 2008 02:22:02 +0100 Subject: libreplace: fix silly crashbug in getifaddrs_test(). Michael (cherry picked from commit 523626908d25f974fd1ae6d7306b1d4bc8414162) (This used to be commit 0906120a6e4fe0eb66f3c3fd99c625b30672cb2b) --- source3/lib/replace/test/getifaddrs.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) (limited to 'source3/lib/replace/test/getifaddrs.c') diff --git a/source3/lib/replace/test/getifaddrs.c b/source3/lib/replace/test/getifaddrs.c index 4455462193..c78c9b545e 100644 --- a/source3/lib/replace/test/getifaddrs.c +++ b/source3/lib/replace/test/getifaddrs.c @@ -51,9 +51,11 @@ static const char *format_sockaddr(struct sockaddr *addr, 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; @@ -90,7 +92,7 @@ int getifaddrs_test(void) ifs = ifs->ifa_next; } - freeifaddrs(ifs); + freeifaddrs(ifs_head); return 0; } -- cgit From a73f4bc27f79f7920a881fcb0050621119591e64 Mon Sep 17 00:00:00 2001 From: Michael Adam Date: Fri, 29 Feb 2008 02:43:24 +0100 Subject: libreplace: ifdef out ip6 code if unsupported. Michael (cherry picked from commit 54cc0df4dbf6d63a9b94e1ac6af4ec7f7803bc30) (This used to be commit 1ff0b616a9cb029040031f797b52112d4973e1eb) --- source3/lib/replace/test/getifaddrs.c | 2 ++ 1 file changed, 2 insertions(+) (limited to 'source3/lib/replace/test/getifaddrs.c') diff --git a/source3/lib/replace/test/getifaddrs.c b/source3/lib/replace/test/getifaddrs.c index c78c9b545e..8b00ac2f40 100644 --- a/source3/lib/replace/test/getifaddrs.c +++ b/source3/lib/replace/test/getifaddrs.c @@ -39,11 +39,13 @@ static const char *format_sockaddr(struct sockaddr *addr, &((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; } -- cgit