blob: f9ddcae8e9eea97a92cd4a075c7f6a5c82939120 [file] [log] [blame]
Wade Guthrie0d438532012-05-18 14:18:50 -07001// Copyright (c) 2012 The Chromium OS Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4//
5// This code is derived from the 'iw' source code. The copyright and license
6// of that code is as follows:
7//
8// Copyright (c) 2007, 2008 Johannes Berg
9// Copyright (c) 2007 Andy Lutomirski
10// Copyright (c) 2007 Mike Kershaw
11// Copyright (c) 2008-2009 Luis R. Rodriguez
12//
13// Permission to use, copy, modify, and/or distribute this software for any
14// purpose with or without fee is hereby granted, provided that the above
15// copyright notice and this permission notice appear in all copies.
16//
17// THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
18// WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
19// MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
20// ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
21// WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
22// ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
23// OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
24
25#ifndef SHILL_NETLINK_SOCKET_H_
26#define SHILL_NETLINK_SOCKET_H_
27
Wade Guthrie0d438532012-05-18 14:18:50 -070028#include <base/basictypes.h>
29#include <base/bind.h>
30#include <base/logging.h>
Wade Guthrie89e6cb32013-03-07 08:03:45 -080031#include <base/memory/scoped_ptr.h>
Wade Guthriecc53f232013-03-05 13:22:23 -080032#include <gtest/gtest_prod.h> // for FRIEND_TEST
Wade Guthrie0d438532012-05-18 14:18:50 -070033
34namespace shill {
35
Wade Guthriecc53f232013-03-05 13:22:23 -080036class Sockets;
Wade Guthriebee87c22013-03-06 11:00:46 -080037class ByteString;
repo syncdc085c82012-12-28 08:54:41 -080038
Wade Guthrie0d438532012-05-18 14:18:50 -070039// Provides an abstraction to a netlink socket. See
Wade Guthriecc53f232013-03-05 13:22:23 -080040// http://www.infradead.org/~tgr/libnl/doc/core.html#core_netlink_fundamentals
41// for documentation on how netlink sockets work (note that most of the rest of
42// this document discusses libnl -- something not used by this code for
43// netlink communication).
Wade Guthrie0d438532012-05-18 14:18:50 -070044class NetlinkSocket {
45 public:
Wade Guthriecc53f232013-03-05 13:22:23 -080046 static const int kReceiveBufferSize;
47
48 NetlinkSocket();
Wade Guthrie0d438532012-05-18 14:18:50 -070049 virtual ~NetlinkSocket();
50
51 // Non-trivial initialization.
52 bool Init();
53
Wade Guthrie0d438532012-05-18 14:18:50 -070054 // Returns the file descriptor used by the socket.
Wade Guthriefa2100e2013-05-15 10:11:22 -070055 virtual int file_descriptor() const { return file_descriptor_; }
Wade Guthrie0d438532012-05-18 14:18:50 -070056
Wade Guthriecc53f232013-03-05 13:22:23 -080057 // Get the next message sequence number for this socket.
58 // |GetSequenceNumber| won't return zero because that is the 'broadcast'
59 // sequence number.
repo syncdc085c82012-12-28 08:54:41 -080060 virtual uint32_t GetSequenceNumber();
Wade Guthrie0d438532012-05-18 14:18:50 -070061
Wade Guthriecc53f232013-03-05 13:22:23 -080062 // Reads data from the socket into |message| and returns true if successful.
63 // The |message| parameter will be resized to hold the entirety of the read
64 // message (and any data in |message| will be overwritten).
Wade Guthriefa2100e2013-05-15 10:11:22 -070065 virtual bool RecvMessage(ByteString *message);
Wade Guthrie0d438532012-05-18 14:18:50 -070066
repo syncdc085c82012-12-28 08:54:41 -080067 // Sends a message, returns true if successful.
Wade Guthriebdcdaa72013-03-04 12:47:12 -080068 virtual bool SendMessage(const ByteString &message);
Christopher Wiley393b93f2012-11-08 17:30:58 -080069
Wade Guthriebee87c22013-03-06 11:00:46 -080070 // Subscribes to netlink broadcast events.
71 virtual bool SubscribeToEvents(uint32_t group_id);
72
Wade Guthriefa2100e2013-05-15 10:11:22 -070073 virtual const Sockets *sockets() const { return sockets_.get(); }
74
repo syncdc085c82012-12-28 08:54:41 -080075 protected:
Wade Guthriecc53f232013-03-05 13:22:23 -080076 uint32_t sequence_number_;
Wade Guthrie0d438532012-05-18 14:18:50 -070077
78 private:
Wade Guthriefa2100e2013-05-15 10:11:22 -070079 friend class NetlinkManagerTest;
Wade Guthriecc53f232013-03-05 13:22:23 -080080 friend class NetlinkSocketTest;
81 FRIEND_TEST(NetlinkSocketTest, SequenceNumberTest);
Wade Guthrie0d438532012-05-18 14:18:50 -070082
Wade Guthriecc53f232013-03-05 13:22:23 -080083 scoped_ptr<Sockets> sockets_;
84 int file_descriptor_;
Wade Guthrie0d438532012-05-18 14:18:50 -070085
86 DISALLOW_COPY_AND_ASSIGN(NetlinkSocket);
87};
88
89} // namespace shill
90
91#endif // SHILL_NETLINK_SOCKET_H_