summaryrefslogtreecommitdiffstats
path: root/net/base/network_change_notifier_linux.cc
blob: edc77b9abe17e168fb433b172f74390e0b66400f (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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
// 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.
//
// This implementation of NetworkChangeNotifier's offline state detection
// depends on D-Bus and NetworkManager, and is known to work on at least
// GNOME version 2.30.  If D-Bus or NetworkManager are unavailable, this
// implementation will always behave as if it is online.

#include "net/base/network_change_notifier_linux.h"

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

#include "base/bind.h"
#include "base/bind_helpers.h"
#include "base/callback.h"
#include "base/compiler_specific.h"
#include "base/eintr_wrapper.h"
#include "base/file_util.h"
#include "base/files/file_path_watcher.h"
#include "base/memory/weak_ptr.h"
#include "base/synchronization/lock.h"
#include "base/synchronization/waitable_event.h"
#include "base/threading/platform_thread.h"
#include "base/threading/thread.h"
#include "dbus/bus.h"
#include "dbus/message.h"
#include "dbus/object_proxy.h"
#include "net/base/net_errors.h"
#include "net/base/network_change_notifier_netlink_linux.h"

using ::base::files::FilePathWatcher;

namespace net {

namespace {

const int kInvalidSocket = -1;

const char kNetworkManagerServiceName[] = "org.freedesktop.NetworkManager";
const char kNetworkManagerPath[] = "/org/freedesktop/NetworkManager";
const char kNetworkManagerInterface[] = "org.freedesktop.NetworkManager";

// http://projects.gnome.org/NetworkManager/developers/spec-08.html#type-NM_STATE
enum {
  NM_LEGACY_STATE_UNKNOWN = 0,
  NM_LEGACY_STATE_ASLEEP = 1,
  NM_LEGACY_STATE_CONNECTING = 2,
  NM_LEGACY_STATE_CONNECTED = 3,
  NM_LEGACY_STATE_DISCONNECTED = 4
};

// http://projects.gnome.org/NetworkManager/developers/migrating-to-09/spec.html#type-NM_STATE
enum {
  NM_STATE_UNKNOWN = 0,
  NM_STATE_ASLEEP = 10,
  NM_STATE_DISCONNECTED = 20,
  NM_STATE_DISCONNECTING = 30,
  NM_STATE_CONNECTING = 40,
  NM_STATE_CONNECTED_LOCAL = 50,
  NM_STATE_CONNECTED_SITE = 60,
  NM_STATE_CONNECTED_GLOBAL = 70
};

// A wrapper around NetworkManager's D-Bus API.
class NetworkManagerApi {
 public:
  NetworkManagerApi(const base::Closure& notification_callback, dbus::Bus* bus)
      : is_offline_(false),
        offline_state_initialized_(true /*manual_reset*/, false),
        notification_callback_(notification_callback),
        helper_thread_id_(base::kInvalidThreadId),
        ALLOW_THIS_IN_INITIALIZER_LIST(ptr_factory_(this)),
        system_bus_(bus) { }

  ~NetworkManagerApi() { }

  // Should be called on a helper thread which must be of type IO.
  void Init();

  // Must be called by the helper thread's CleanUp() method.
  void CleanUp();

  // Implementation of NetworkChangeNotifierLinux::IsCurrentlyOffline().
  // Safe to call from any thread, but will block until Init() has completed.
  bool IsCurrentlyOffline();

 private:
  // Callbacks for D-Bus API.
  void OnInitialResponse(dbus::Response* response) {
    HandleResponse(response);
    offline_state_initialized_.Signal();
  }

  void OnSignaled(dbus::Signal* signal);

  void OnConnected(const std::string&, const std::string&, bool success) {
    if (!success) {
      DLOG(WARNING) << "Failed to set up offline state detection";
      offline_state_initialized_.Signal();
    }
  }

  // Helper for OnInitialResponse.
  void HandleResponse(dbus::Response* response);

  // Converts a NetworkManager state uint to a bool.
  static bool StateIsOffline(uint32 state);

  bool is_offline_;
  base::Lock is_offline_lock_;
  base::WaitableEvent offline_state_initialized_;

  base::Closure notification_callback_;

  base::PlatformThreadId helper_thread_id_;

  base::WeakPtrFactory<NetworkManagerApi> ptr_factory_;

  scoped_refptr<dbus::Bus> system_bus_;

  DISALLOW_COPY_AND_ASSIGN(NetworkManagerApi);
};

void NetworkManagerApi::Init() {
  // D-Bus requires an IO MessageLoop.
  DCHECK_EQ(MessageLoop::current()->type(), MessageLoop::TYPE_IO);
  helper_thread_id_ = base::PlatformThread::CurrentId();

  if (!system_bus_) {
    dbus::Bus::Options options;
    options.bus_type = dbus::Bus::SYSTEM;
    options.connection_type = dbus::Bus::PRIVATE;
    system_bus_ = new dbus::Bus(options);
  }

  // Ignore ServiceUnknown errors to avoid log spam: http://crbug.com/109696.
  dbus::ObjectProxy* proxy = system_bus_->GetObjectProxyWithOptions(
          kNetworkManagerServiceName, kNetworkManagerPath,
          dbus::ObjectProxy::IGNORE_SERVICE_UNKNOWN_ERRORS);

  // Get the initial state asynchronously.
  dbus::MethodCall method_call(DBUS_INTERFACE_PROPERTIES, "Get");
  dbus::MessageWriter builder(&method_call);
  builder.AppendString(kNetworkManagerInterface);
  builder.AppendString("State");
  proxy->CallMethod(
      &method_call, dbus::ObjectProxy::TIMEOUT_USE_DEFAULT,
      base::Bind(&NetworkManagerApi::OnInitialResponse,
                 ptr_factory_.GetWeakPtr()));

  // And sign up for notifications.
  proxy->ConnectToSignal(
      kNetworkManagerInterface,
      "StateChanged",
      base::Bind(&NetworkManagerApi::OnSignaled, ptr_factory_.GetWeakPtr()),
      base::Bind(&NetworkManagerApi::OnConnected, ptr_factory_.GetWeakPtr()));
}

void NetworkManagerApi::CleanUp() {
  DCHECK_EQ(helper_thread_id_, base::PlatformThread::CurrentId());
  ptr_factory_.InvalidateWeakPtrs();
}

void NetworkManagerApi::HandleResponse(dbus::Response* response) {
  DCHECK_EQ(helper_thread_id_, base::PlatformThread::CurrentId());
  if (!response) {
    DLOG(WARNING) << "No response received for initial state request";
    return;
  }
  dbus::MessageReader reader(response);
  uint32 state = 0;
  if (!reader.PopVariantOfUint32(&state)) {
    DLOG(WARNING) << "Unexpected response for NetworkManager State request: "
                  << response->ToString();
    return;
  }
  {
    base::AutoLock lock(is_offline_lock_);
    is_offline_ = StateIsOffline(state);
  }
}

void NetworkManagerApi::OnSignaled(dbus::Signal* signal) {
  DCHECK_EQ(helper_thread_id_, base::PlatformThread::CurrentId());
  dbus::MessageReader reader(signal);
  uint32 state = 0;
  if (!reader.PopUint32(&state)) {
    DLOG(WARNING) << "Unexpected signal for NetworkManager StateChanged: "
                  << signal->ToString();
    return;
  }
  bool new_is_offline = StateIsOffline(state);
  {
    base::AutoLock lock(is_offline_lock_);
    if (is_offline_ != new_is_offline)
      is_offline_ = new_is_offline;
    else
      return;
  }
  notification_callback_.Run();
}

bool NetworkManagerApi::StateIsOffline(uint32 state) {
  switch (state) {
    case NM_LEGACY_STATE_CONNECTED:
    case NM_STATE_CONNECTED_SITE:
    case NM_STATE_CONNECTED_GLOBAL:
      // Definitely connected
      return false;
    case NM_LEGACY_STATE_DISCONNECTED:
    case NM_STATE_DISCONNECTED:
      // Definitely disconnected
      return true;
    case NM_STATE_CONNECTED_LOCAL:
      // Local networking only; I'm treating this as offline (keybuk)
      return true;
    case NM_LEGACY_STATE_CONNECTING:
    case NM_STATE_DISCONNECTING:
    case NM_STATE_CONNECTING:
      // In-flight change to connection status currently underway
      return true;
    case NM_LEGACY_STATE_ASLEEP:
    case NM_STATE_ASLEEP:
      // Networking disabled or no devices on system
      return true;
    default:
      // Unknown status
      return false;
  }
}

bool NetworkManagerApi::IsCurrentlyOffline() {
  offline_state_initialized_.Wait();
  base::AutoLock lock(is_offline_lock_);
  return is_offline_;
}

class DNSWatchDelegate : public FilePathWatcher::Delegate {
 public:
  explicit DNSWatchDelegate(const base::Closure& callback)
      : callback_(callback) {}
  virtual ~DNSWatchDelegate() {}
  // FilePathWatcher::Delegate interface
  virtual void OnFilePathChanged(const FilePath& path) OVERRIDE;
  virtual void OnFilePathError(const FilePath& path) OVERRIDE;
 private:
  base::Closure callback_;
  DISALLOW_COPY_AND_ASSIGN(DNSWatchDelegate);
};

void DNSWatchDelegate::OnFilePathChanged(const FilePath& path) {
  // Calls NetworkChangeNotifier::NotifyObserversOfDNSChange().
  callback_.Run();
}

void DNSWatchDelegate::OnFilePathError(const FilePath& path) {
  LOG(ERROR) << "DNSWatchDelegate::OnFilePathError for " << path.value();
}

}  // namespace

class NetworkChangeNotifierLinux::Thread
    : public base::Thread, public MessageLoopForIO::Watcher {
 public:
  explicit Thread(dbus::Bus* bus);
  virtual ~Thread();

  // MessageLoopForIO::Watcher:
  virtual void OnFileCanReadWithoutBlocking(int fd);
  virtual void OnFileCanWriteWithoutBlocking(int /* fd */);

  // Plumbing for NetworkChangeNotifier::IsCurrentlyOffline.
  // Safe to call from any thread.
  bool IsCurrentlyOffline() {
    return network_manager_api_.IsCurrentlyOffline();
  }

 protected:
  // base::Thread
  virtual void Init();
  virtual void CleanUp();

 private:
  void NotifyObserversOfIPAddressChange() {
    NetworkChangeNotifier::NotifyObserversOfIPAddressChange();
  }

  static void NotifyObserversOfDNSChange() {
    NetworkChangeNotifier::NotifyObserversOfDNSChange();
  }

  static void NotifyObserversOfOnlineStateChange() {
    NetworkChangeNotifier::NotifyObserversOfOnlineStateChange();
  }

  // Starts listening for netlink messages.  Also handles the messages if there
  // are any available on the netlink socket.
  void ListenForNotifications();

  // Attempts to read from the netlink socket into |buf| of length |len|.
  // Returns the bytes read on synchronous success and ERR_IO_PENDING if the
  // recv() would block.  Otherwise, it returns a net error code.
  int ReadNotificationMessage(char* buf, size_t len);

  // The netlink socket descriptor.
  int netlink_fd_;
  MessageLoopForIO::FileDescriptorWatcher netlink_watcher_;

  // Technically only needed for ChromeOS, but it's ugly to #ifdef out.
  base::WeakPtrFactory<Thread> ptr_factory_;

  // Used to watch for changes to /etc/resolv.conf and /etc/hosts.
  scoped_ptr<base::files::FilePathWatcher> resolv_file_watcher_;
  scoped_ptr<base::files::FilePathWatcher> hosts_file_watcher_;
  scoped_refptr<DNSWatchDelegate> file_watcher_delegate_;

  // Used to detect online/offline state changes.
  NetworkManagerApi network_manager_api_;

  DISALLOW_COPY_AND_ASSIGN(Thread);
};

NetworkChangeNotifierLinux::Thread::Thread(dbus::Bus* bus)
    : base::Thread("NetworkChangeNotifier"),
      netlink_fd_(kInvalidSocket),
      ALLOW_THIS_IN_INITIALIZER_LIST(ptr_factory_(this)),
      network_manager_api_(
          base::Bind(&NetworkChangeNotifierLinux::Thread
                     ::NotifyObserversOfOnlineStateChange),
          bus) {
}

NetworkChangeNotifierLinux::Thread::~Thread() {
  DCHECK(!Thread::IsRunning());
}

void NetworkChangeNotifierLinux::Thread::Init() {
  resolv_file_watcher_.reset(new FilePathWatcher);
  hosts_file_watcher_.reset(new FilePathWatcher);
  file_watcher_delegate_ = new DNSWatchDelegate(base::Bind(
      &NetworkChangeNotifierLinux::Thread::NotifyObserversOfDNSChange));
  if (!resolv_file_watcher_->Watch(
          FilePath(FILE_PATH_LITERAL("/etc/resolv.conf")),
          file_watcher_delegate_.get())) {
    LOG(ERROR) << "Failed to setup watch for /etc/resolv.conf";
  }
  if (!hosts_file_watcher_->Watch(FilePath(FILE_PATH_LITERAL("/etc/hosts")),
          file_watcher_delegate_.get())) {
    LOG(ERROR) << "Failed to setup watch for /etc/hosts";
  }
  netlink_fd_ = InitializeNetlinkSocket();
  if (netlink_fd_ < 0) {
    netlink_fd_ = kInvalidSocket;
    return;
  }
  ListenForNotifications();

  network_manager_api_.Init();
}

void NetworkChangeNotifierLinux::Thread::CleanUp() {
  if (netlink_fd_ != kInvalidSocket) {
    if (HANDLE_EINTR(close(netlink_fd_)) != 0)
      PLOG(ERROR) << "Failed to close socket";
    netlink_fd_ = kInvalidSocket;
    netlink_watcher_.StopWatchingFileDescriptor();
  }
  // Kill watchers early to make sure they won't try to call
  // into us via the delegate during destruction.
  resolv_file_watcher_.reset();
  hosts_file_watcher_.reset();

  network_manager_api_.CleanUp();
}

void NetworkChangeNotifierLinux::Thread::OnFileCanReadWithoutBlocking(int fd) {
  DCHECK_EQ(fd, netlink_fd_);
  ListenForNotifications();
}

void NetworkChangeNotifierLinux::Thread::OnFileCanWriteWithoutBlocking(
    int /* fd */) {
  NOTREACHED();
}

void NetworkChangeNotifierLinux::Thread::ListenForNotifications() {
  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(oshima): chromium-os:8285 - introduced artificial delay to
      // work around the issue of network load issue after connection
      // restored. See the bug for more details.
      //  This should be removed once this bug is properly fixed.
      const int kObserverNotificationDelayMS = 200;
      message_loop()->PostDelayedTask(
          FROM_HERE,
          base::Bind(
              &Thread::NotifyObserversOfIPAddressChange,
              ptr_factory_.GetWeakPtr()),
          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::Thread::ReadNotificationMessage(
    char* buf,
    size_t len) {
  DCHECK_NE(len, 0u);
  DCHECK(buf);
  memset(buf, 0, len);
  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;
}

NetworkChangeNotifierLinux* NetworkChangeNotifierLinux::Create() {
  return new NetworkChangeNotifierLinux(NULL);
}

NetworkChangeNotifierLinux* NetworkChangeNotifierLinux::CreateForTest(
    dbus::Bus* bus) {
  return new NetworkChangeNotifierLinux(bus);
}

NetworkChangeNotifierLinux::NetworkChangeNotifierLinux(dbus::Bus* bus)
    : notifier_thread_(new Thread(bus)) {
  // 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);
}

NetworkChangeNotifierLinux::~NetworkChangeNotifierLinux() {
  // Stopping from here allows us to sanity- check that the notifier
  // thread shut down properly.
  notifier_thread_->Stop();
}

bool NetworkChangeNotifierLinux::IsCurrentlyOffline() const {
  return notifier_thread_->IsCurrentlyOffline();
}

}  // namespace net