summaryrefslogtreecommitdiffstats
path: root/net/base/network_change_notifier_linux.cc
blob: 713184538276a2707849c4799d6ef2aaa11a29b8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
// Copyright (c) 2010 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/network_change_notifier_linux.h"

#include <errno.h>
#include <sys/socket.h>

#include "base/eintr_wrapper.h"
#include "base/task.h"
#include "base/thread.h"
#include "net/base/net_errors.h"
#include "net/base/network_change_notifier_netlink_linux.h"

// We only post tasks to a child thread we own, so we don't need refcounting.
DISABLE_RUNNABLE_METHOD_REFCOUNT(net::NetworkChangeNotifierLinux);

namespace net {

namespace {

const int kInvalidSocket = -1;

}  // namespace

NetworkChangeNotifierLinux::NetworkChangeNotifierLinux()
    : notifier_thread_(new base::Thread("NetworkChangeNotifier")),
      netlink_fd_(kInvalidSocket) {
  // We create this notifier thread because the notification implementation
  // needs a MessageLoopForIO, and there's no guarantee that
  // MessageLoop::current() meets that criterion.
  base::Thread::Options thread_options(MessageLoop::TYPE_IO, 0);
  notifier_thread_->StartWithOptions(thread_options);
  notifier_thread_->message_loop()->PostTask(FROM_HERE,
      NewRunnableMethod(this, &NetworkChangeNotifierLinux::Init));
}

NetworkChangeNotifierLinux::~NetworkChangeNotifierLinux() {
  // We don't need to explicitly Stop(), but doing so allows us to sanity-
  // check that the notifier thread shut down properly.
  notifier_thread_->Stop();
  DCHECK_EQ(kInvalidSocket, netlink_fd_);
}

bool NetworkChangeNotifierLinux::IsCurrentlyOffline() const {
  // TODO(eroman): http://crbug.com/53473
  return false;
}

void NetworkChangeNotifierLinux::WillDestroyCurrentMessageLoop() {
  DCHECK(notifier_thread_ != NULL);
  // We can't check the notifier_thread_'s message_loop(), as it's now 0.
  // DCHECK_EQ(notifier_thread_->message_loop(), MessageLoop::current());

  if (netlink_fd_ != kInvalidSocket) {
    if (HANDLE_EINTR(close(netlink_fd_)) != 0)
      PLOG(ERROR) << "Failed to close socket";
    netlink_fd_ = kInvalidSocket;
    netlink_watcher_.StopWatchingFileDescriptor();
  }
}

void NetworkChangeNotifierLinux::OnFileCanReadWithoutBlocking(int fd) {
  DCHECK(notifier_thread_ != NULL);
  DCHECK_EQ(notifier_thread_->message_loop(), MessageLoop::current());

  DCHECK_EQ(fd, netlink_fd_);
  ListenForNotifications();
}

void NetworkChangeNotifierLinux::OnFileCanWriteWithoutBlocking(int /* fd */) {
  DCHECK(notifier_thread_ != NULL);
  DCHECK_EQ(notifier_thread_->message_loop(), MessageLoop::current());

  NOTREACHED();
}

void NetworkChangeNotifierLinux::Init() {
  DCHECK(notifier_thread_ != NULL);
  DCHECK_EQ(notifier_thread_->message_loop(), MessageLoop::current());

  netlink_fd_ = InitializeNetlinkSocket();
  if (netlink_fd_ < 0) {
    netlink_fd_ = kInvalidSocket;
    return;
  }
  MessageLoop::current()->AddDestructionObserver(this);
  ListenForNotifications();
}

void NetworkChangeNotifierLinux::ListenForNotifications() {
  DCHECK(notifier_thread_ != NULL);
  DCHECK_EQ(notifier_thread_->message_loop(), MessageLoop::current());

  char buf[4096];
  int rv = ReadNotificationMessage(buf, arraysize(buf));
  while (rv > 0) {
    if (HandleNetlinkMessage(buf, rv)) {
      VLOG(1) << "Detected IP address changes.";
#if defined(OS_CHROMEOS)
      // TODO(zelidrag): chromium-os:3996 - introduced artificial delay to
      // work around the issue of proxy initialization before name resolving
      // is functional in ChromeOS. This should be removed once this bug
      // is properly fixed.
      const int kObserverNotificationDelayMS = 500;
      MessageLoop::current()->PostDelayedTask(FROM_HERE, NewRunnableFunction(
          &NetworkChangeNotifier::NotifyObserversOfIPAddressChange),
          kObserverNotificationDelayMS);
#else
      NotifyObserversOfIPAddressChange();
#endif
    }
    rv = ReadNotificationMessage(buf, arraysize(buf));
  }

  if (rv == ERR_IO_PENDING) {
    rv = MessageLoopForIO::current()->WatchFileDescriptor(netlink_fd_, false,
        MessageLoopForIO::WATCH_READ, &netlink_watcher_, this);
    LOG_IF(ERROR, !rv) << "Failed to watch netlink socket: " << netlink_fd_;
  }
}

int NetworkChangeNotifierLinux::ReadNotificationMessage(char* buf, size_t len) {
  DCHECK(notifier_thread_ != NULL);
  DCHECK_EQ(notifier_thread_->message_loop(), MessageLoop::current());

  DCHECK_NE(len, 0u);
  DCHECK(buf);
  memset(buf, 0, sizeof(buf));
  int rv = recv(netlink_fd_, buf, len, 0);
  if (rv > 0)
    return rv;

  DCHECK_NE(rv, 0);
  if (errno != EAGAIN && errno != EWOULDBLOCK) {
    PLOG(DFATAL) << "recv";
    return ERR_FAILED;
  }

  return ERR_IO_PENDING;
}

}  // namespace net