summaryrefslogtreecommitdiffstats
path: root/net/base/network_change_notifier_linux.cc
blob: e6f5453ddf246a8d5e5b7b314c447a248b0fa4bf (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
// Copyright (c) 2009 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/basictypes.h"
#include "base/logging.h"
#include "base/message_loop.h"
#include "net/base/net_errors.h"
#include "net/base/network_change_notifier_netlink_linux.h"

namespace net {

namespace {

const int kInvalidSocket = -1;

}  // namespace

NetworkChangeNotifierLinux::NetworkChangeNotifierLinux()
    : netlink_fd_(kInvalidSocket),
      loop_(MessageLoopForIO::current()) {
  netlink_fd_ = InitializeNetlinkSocket();
  if (netlink_fd_ < 0) {
    netlink_fd_ = kInvalidSocket;
    return;
  }

  ListenForNotifications();
}

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

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

  ListenForNotifications();
}

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

void NetworkChangeNotifierLinux::ListenForNotifications() {
  char buf[4096];
  int rv = ReadNotificationMessage(buf, arraysize(buf));
  while (rv > 0 ) {
    if (HandleNetlinkMessage(buf, rv))
      FOR_EACH_OBSERVER(Observer, observers_, OnIPAddressChanged());
    rv = ReadNotificationMessage(buf, arraysize(buf));
  }

  if (rv == ERR_IO_PENDING) {
    rv = loop_->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_NE(len, 0u);
  DCHECK(buf);

  memset(buf, 0, sizeof(buf));
  int rv = recv(netlink_fd_, buf, len, 0);
  if (rv > 0) {
    return rv;
  } else {
    DCHECK_NE(rv, 0);
    if (errno != EAGAIN && errno != EWOULDBLOCK) {
      PLOG(DFATAL) << "recv";
      return ERR_FAILED;
    }

    return ERR_IO_PENDING;
  }
}

}  // namespace net