summaryrefslogtreecommitdiff
path: root/sql/wsrep_utils.cc
diff options
context:
space:
mode:
authorNirbhay Choubey <nirbhay@mariadb.com>2015-01-26 11:58:16 -0500
committerNirbhay Choubey <nirbhay@mariadb.com>2015-01-26 11:58:16 -0500
commite6f35f15115283f6d446af25a5c33e615c6afe7c (patch)
treec9ef43617a55a6255378196d7153ad0db30406d1 /sql/wsrep_utils.cc
parent53b9f755aff1d6ef5753e0fbca50859126927752 (diff)
downloadmariadb-git-e6f35f15115283f6d446af25a5c33e615c6afe7c.tar.gz
Backported changes done in wsrep_guess_ip() from 10.1.
Diffstat (limited to 'sql/wsrep_utils.cc')
-rw-r--r--sql/wsrep_utils.cc73
1 files changed, 25 insertions, 48 deletions
diff --git a/sql/wsrep_utils.cc b/sql/wsrep_utils.cc
index 64b9fb35b48..545870f1c07 100644
--- a/sql/wsrep_utils.cc
+++ b/sql/wsrep_utils.cc
@@ -34,6 +34,10 @@
#include <sys/socket.h>
#include <netdb.h> // getaddrinfo()
+#ifdef HAVE_GETIFADDRS
+#include <ifaddrs.h>
+#endif
+
extern char** environ; // environment variables
static wsp::string wsrep_PATH;
@@ -321,10 +325,6 @@ thd::~thd ()
/* Returns INADDR_NONE, INADDR_ANY, INADDR_LOOPBACK or something else */
unsigned int wsrep_check_ip (const char* const addr)
{
-#if 0
- if (addr && 0 == strcasecmp(addr, MY_BIND_ALL_ADDRESSES)) return INADDR_ANY;
-#endif
-
unsigned int ret = INADDR_NONE;
struct addrinfo *res, hints;
@@ -371,16 +371,17 @@ size_t wsrep_guess_ip (char* buf, size_t buf_len)
{
size_t ip_len = 0;
- if (my_bind_addr_str && strlen(my_bind_addr_str))
+ if (my_bind_addr_str && my_bind_addr_str[0] != '\0')
{
unsigned int const ip_type= wsrep_check_ip(my_bind_addr_str);
if (INADDR_NONE == ip_type) {
- WSREP_ERROR("Networking not configured, cannot receive state transfer.");
+ WSREP_ERROR("Networking not configured, cannot receive state "
+ "transfer.");
return 0;
}
- if (INADDR_ANY != ip_type) {;
+ if (INADDR_ANY != ip_type) {
strncpy (buf, my_bind_addr_str, buf_len);
return strlen(buf);
}
@@ -405,53 +406,29 @@ size_t wsrep_guess_ip (char* buf, size_t buf_len)
return ip_len;
}
- // try to find the address of the first one
-#if (TARGET_OS_LINUX == 1)
- const char cmd[] = "ip addr show | grep -E '^[[:space:]]*inet' | grep -m1 global |"
- " awk '{ print $2 }' | sed -e 's/\\/.*//'";
-#elif defined(__sun__)
- const char cmd[] = "/sbin/ifconfig -a | "
- "/usr/gnu/bin/grep -m1 -1 -E 'net[0-9]:' | tail -n 1 | awk '{ print $2 }'";
-#elif defined(__APPLE__) || defined(__FreeBSD__) || defined(__OpenBSD__)
- const char cmd[] = "/sbin/route -nv get 8.8.8.8 | tail -n1 | awk '{print $(NF)}'";
-#else
- char *cmd;
-#error "OS not supported"
-#endif
- wsp::process proc (cmd, "r");
-
- if (NULL != proc.pipe()) {
- char* ret;
+#if HAVE_GETIFADDRS
+ struct ifaddrs *ifaddr, *ifa;
+ if (getifaddrs(&ifaddr) == 0)
+ {
+ for (ifa= ifaddr; ifa != NULL; ifa = ifa->ifa_next)
+ {
+ if (!ifa->ifa_addr || ifa->ifa_addr->sa_family != AF_INET) // TODO AF_INET6
+ continue;
- ret = fgets (buf, buf_len, proc.pipe());
+ if (vio_getnameinfo(ifa->ifa_addr, buf, buf_len, NULL, 0, NI_NUMERICHOST))
+ continue;
- if (proc.wait()) return 0;
+ if (strcmp(buf, "127.0.0.1") == 0) // lame
+ continue;
- if (NULL == ret) {
- WSREP_ERROR("Failed to read output of: '%s'", cmd);
- return 0;
- }
- }
- else {
- WSREP_ERROR("Failed to execute: '%s'", cmd);
- return 0;
- }
-
- // clear possible \n at the end of ip string left by fgets()
- ip_len = strlen (buf);
- if (ip_len > 0 && '\n' == buf[ip_len - 1]) {
- ip_len--;
- buf[ip_len] = '\0';
- }
-
- if (INADDR_NONE == inet_addr(buf)) {
- if (strlen(buf) != 0) {
- WSREP_WARN("Shell command returned invalid address: '%s'", buf);
+ freeifaddrs(ifaddr);
+ return strlen(buf);
}
- return 0;
+ freeifaddrs(ifaddr);
}
+#endif
- return ip_len;
+ return 0;
}
size_t wsrep_guess_address(char* buf, size_t buf_len)