Chromium Code Reviews| Index: net/base/address_tracker_linux.cc |
| diff --git a/net/base/address_tracker_linux.cc b/net/base/address_tracker_linux.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..b70a427922c1f52beb20d3928667112be473bf78 |
| --- /dev/null |
| +++ b/net/base/address_tracker_linux.cc |
| @@ -0,0 +1,214 @@ |
| +// Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#include "net/base/address_tracker_linux.h" |
| + |
| +#include <errno.h> |
| + |
| +#include "base/eintr_wrapper.h" |
| +#include "base/logging.h" |
| +#include "net/base/network_change_notifier_linux.h" |
| + |
| +namespace net { |
| +namespace internal { |
| + |
| +namespace { |
| + |
| +// Retrieves address from NETLINK address message. |
| +bool GetAddress(const struct nlmsghdr* header, IPAddressNumber* out) { |
| + const struct ifaddrmsg* msg = |
| + reinterpret_cast<struct ifaddrmsg*>(NLMSG_DATA(header)); |
| + size_t address_length = 0; |
| + switch (msg->ifa_family) { |
| + case AF_INET: |
| + address_length = kIPv4AddressSize; |
| + break; |
| + case AF_INET6: |
| + address_length = kIPv6AddressSize; |
| + break; |
| + default: |
| + // Unknown family. |
| + return false; |
| + } |
| + unsigned char* address = NULL; |
| + unsigned char* local = NULL; |
| + const struct rtattr* attr = reinterpret_cast<struct rtattr*>(IFA_RTA(msg)); |
| + size_t length = IFA_PAYLOAD(header); |
| + for (; RTA_OK(attr, length); attr = RTA_NEXT(attr, length)) { |
| + switch (attr->rta_type) { |
| + case IFA_ADDRESS: |
| + local = reinterpret_cast<unsigned char*>(RTA_DATA(attr)); |
|
vandebo (ex-Chrome)
2012/06/29 22:24:29
you assign local in IFA_ADDRESS and address in IFA
szym
2012/06/30 00:00:57
Definitely a typo.
|
| + break; |
| + case IFA_LOCAL: |
| + address = reinterpret_cast<unsigned char*>(RTA_DATA(attr)); |
| + break; |
| + default: |
| + break; |
| + } |
| + } |
| + if (local) |
|
vandebo (ex-Chrome)
2012/06/29 22:24:29
A comment about what's going on here would be help
szym
2012/06/30 00:00:57
The behavior here is based on getaddrinfo in glibc
|
| + address = local; |
| + if (!address) |
| + return false; |
| + out->assign(address, address + address_length); |
| + return true; |
| +} |
| + |
| +void CloseSocket(int fd) { |
| + if (HANDLE_EINTR(close(fd)) < 0) |
| + PLOG(ERROR) << "Could not close NETLINK socket."; |
| +} |
| + |
| +} // namespace |
| + |
| +AddressTrackerLinux::AddressTrackerLinux(const base::Closure& callback) |
| + : callback_(callback), netlink_fd_(-1) { |
| + DCHECK(!callback.is_null()); |
| +} |
| + |
| +AddressTrackerLinux::~AddressTrackerLinux() { |
| + if (netlink_fd_ >= 0) |
| + CloseSocket(netlink_fd_); |
| +} |
| + |
| +void AddressTrackerLinux::Init() { |
| + int sock = socket(AF_NETLINK, SOCK_RAW, NETLINK_ROUTE); |
| + if (sock < 0) { |
| + PLOG(ERROR) << "Could not create NETLINK socket"; |
| + return; |
| + } |
| + |
| + // Request notifications. |
| + struct sockaddr_nl addr = {}; |
| + addr.nl_family = AF_NETLINK; |
| + addr.nl_pid = getpid(); |
| + // TODO(szym): Track RTMGRP_LINK as well for ifi_type, http://crbug.com/113993 |
| + addr.nl_groups = RTMGRP_IPV4_IFADDR | RTMGRP_IPV6_IFADDR | RTMGRP_NOTIFY; |
| + int rv = bind(sock, reinterpret_cast<struct sockaddr*>(&addr), sizeof(addr)); |
| + if (rv < 0) { |
| + PLOG(ERROR) << "Could not bind NETLINK socket"; |
| + CloseSocket(sock); |
| + return; |
| + } |
| + |
| + // Watch for asynchronous messages. |
| + if (SetNonBlocking(sock)) { |
| + PLOG(ERROR) << "Could not make NETLINK socket non-blocking"; |
| + CloseSocket(sock); |
| + return; |
| + } |
| + |
| + rv = MessageLoopForIO::current()->WatchFileDescriptor( |
| + sock, true, MessageLoopForIO::WATCH_READ, &watcher_, this); |
| + if (rv < 0) { |
| + PLOG(ERROR) << "Could not watch NETLINK socket"; |
| + CloseSocket(sock); |
| + return; |
| + } |
| + |
| + // Request dump of addresses. |
| + struct sockaddr_nl peer = {}; |
| + peer.nl_family = AF_NETLINK; |
| + |
| + struct { |
| + struct nlmsghdr header; |
| + struct rtgenmsg msg; |
| + } request = {}; |
| + |
| + request.header.nlmsg_len = NLMSG_LENGTH(sizeof(request.msg)); |
| + request.header.nlmsg_type = RTM_GETADDR; |
| + request.header.nlmsg_flags = NLM_F_REQUEST | NLM_F_DUMP; |
| + request.header.nlmsg_pid = getpid(); |
| + request.msg.rtgen_family = AF_UNSPEC; |
| + |
| + rv = HANDLE_EINTR(sendto(sock, &request, request.header.nlmsg_len, 0, |
| + reinterpret_cast<struct sockaddr*>(&peer), |
| + sizeof(peer))); |
| + if (rv < 0) { |
| + PLOG(ERROR) << "Could not send NETLINK request"; |
| + CloseSocket(sock); |
| + return; |
| + } |
| + |
| + netlink_fd_ = sock; |
| +} |
| + |
| +AddressTrackerLinux::AddressMap AddressTrackerLinux::GetMap() const { |
| + base::AutoLock lock(lock_); |
| + return map_; |
| +} |
| + |
| +bool AddressTrackerLinux::ReadMessages() { |
| + char buf[4096]; |
| + bool changed = false; |
| + for (;;) { |
| + int rv = HANDLE_EINTR(recv(netlink_fd_, buf, sizeof(buf), 0)); |
| + if (rv == 0) { |
| + LOG(ERROR) << "Unexpected shutdown of NETLINK socket."; |
| + return false; |
| + } |
| + if (rv < 0) { |
| + if ((errno == EAGAIN) || (errno == EWOULDBLOCK)) |
| + break; |
| + PLOG(ERROR) << "Failed to recv from netlink socket"; |
| + return false; |
| + } |
| + changed |= HandleMessage(buf, rv); |
| + }; |
| + return changed; |
| +} |
| + |
| +bool AddressTrackerLinux::HandleMessage(char* buf, size_t len) { |
| + DCHECK(buf); |
| + bool changed = false; |
| + for (const struct nlmsghdr* header = reinterpret_cast<struct nlmsghdr*>(buf); |
| + NLMSG_OK(header, len); header = NLMSG_NEXT(header, len)) { |
| + switch (header->nlmsg_type) { |
| + case NLMSG_DONE: |
| + return changed; |
| + case NLMSG_ERROR: |
| + LOG(ERROR) << "Unexpected netlink error."; |
| + return changed; |
| + case RTM_NEWADDR: { |
| + IPAddressNumber address; |
| + if (GetAddress(header, &address)) { |
| + base::AutoLock lock(lock_); |
| + const struct ifaddrmsg* msg = |
| + reinterpret_cast<struct ifaddrmsg*>(NLMSG_DATA(header)); |
| + // Only indicate change if the address is new or ifaddrmsg info has |
| + // changed. |
| + AddressMap::iterator it = map_.find(address); |
| + if (it == map_.end()) { |
| + map_.insert(it, std::make_pair(address, *msg)); |
| + changed = true; |
| + } else if (memcmp(&it->second, msg, sizeof(*msg))) { |
|
vandebo (ex-Chrome)
2012/06/29 22:24:29
Why do we require that the struct ifaddrmsg be ide
szym
2012/06/30 00:00:57
We require that something in ifaddrmsg changes bef
|
| + it->second = *msg; |
| + changed = true; |
| + } |
| + } |
| + } break; |
| + case RTM_DELADDR: { |
| + IPAddressNumber address; |
| + if (GetAddress(header, &address)) { |
| + base::AutoLock lock(lock_); |
| + if (map_.erase(address)) |
| + changed = true; |
| + } |
| + } break; |
| + default: |
| + break; |
| + } |
| + } |
| + return changed; |
| +} |
| + |
| +void AddressTrackerLinux::OnFileCanReadWithoutBlocking(int fd) { |
| + if (ReadMessages()) |
| + callback_.Run(); |
| +} |
| + |
| +void AddressTrackerLinux::OnFileCanWriteWithoutBlocking(int /* fd */) {} |
| + |
| +} // namespace internal |
| +} // namespace net |