aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAlex Vakulenko <avakulenko@google.com>2015-09-29 15:51:57 -0700
committerAlex Vakulenko <avakulenko@google.com>2015-09-29 15:58:15 -0700
commitfa1de83ce8f5587ed41b9c3a3fb3466b23d76f6d (patch)
tree06863c53d5ece31c826056c96b1848bf78c783c4
parente4ef2615bc583642bd95bdf3b282cc389a46a2a0 (diff)
downloadweaved-fa1de83ce8f5587ed41b9c3a3fb3466b23d76f6d.tar.gz
Fix IP address logging in weaved for XMPP connection attempts
Apparently there was a bug in how we converted address from sockaddr to string. The code was working completely incorrectly and produced bogus log messages with incorrect host addresses when establishing XMPP connection to talk.google.com Change-Id: Icf1ac41e19bb27fbcbc7819347cb4a74a3de8ff6
-rw-r--r--buffet/socket_stream.cc33
1 files changed, 29 insertions, 4 deletions
diff --git a/buffet/socket_stream.cc b/buffet/socket_stream.cc
index 8ee9c60..bec0cd1 100644
--- a/buffet/socket_stream.cc
+++ b/buffet/socket_stream.cc
@@ -13,6 +13,7 @@
#include <base/bind.h>
#include <base/files/file_util.h>
#include <base/message_loop/message_loop.h>
+#include <base/strings/stringprintf.h>
#include <chromeos/streams/file_stream.h>
#include <chromeos/streams/tls_stream.h>
@@ -23,6 +24,31 @@ namespace buffet {
namespace {
+std::string GetIPAddress(const sockaddr* sa) {
+ std::string addr;
+ char str[INET6_ADDRSTRLEN] = {};
+ switch (sa->sa_family) {
+ case AF_INET:
+ if (inet_ntop(AF_INET,
+ &reinterpret_cast<const sockaddr_in*>(sa)->sin_addr, str,
+ sizeof(str))) {
+ addr = str;
+ }
+ break;
+
+ case AF_INET6:
+ if (inet_ntop(AF_INET6,
+ &reinterpret_cast<const sockaddr_in6*>(sa)->sin6_addr, str,
+ sizeof(str))) {
+ addr = str;
+ }
+ break;
+ }
+ if (addr.empty())
+ addr = base::StringPrintf("<Unknown address family: %d>", sa->sa_family);
+ return addr;
+}
+
int ConnectSocket(const std::string& host, uint16_t port) {
std::string service = std::to_string(port);
addrinfo hints = {0, AF_UNSPEC, SOCK_STREAM};
@@ -38,13 +64,12 @@ int ConnectSocket(const std::string& host, uint16_t port) {
if (socket_fd < 0)
continue;
- char str[INET6_ADDRSTRLEN] = {};
- inet_ntop(info->ai_family, info->ai_addr, str, info->ai_addrlen);
- LOG(INFO) << "Connecting to address: " << str;
+ std::string addr = GetIPAddress(info->ai_addr);
+ LOG(INFO) << "Connecting to address: " << addr;
if (connect(socket_fd, info->ai_addr, info->ai_addrlen) == 0)
break; // Success.
- PLOG(WARNING) << "Failed to connect to address: " << str;
+ PLOG(WARNING) << "Failed to connect to address: " << addr;
close(socket_fd);
socket_fd = -1;
}