Wade Guthrie | 0d43853 | 2012-05-18 14:18:50 -0700 | [diff] [blame] | 1 | // 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 Guthrie | 0d43853 | 2012-05-18 14:18:50 -0700 | [diff] [blame] | 28 | #include <base/basictypes.h> |
| 29 | #include <base/bind.h> |
| 30 | #include <base/logging.h> |
Wade Guthrie | 89e6cb3 | 2013-03-07 08:03:45 -0800 | [diff] [blame] | 31 | #include <base/memory/scoped_ptr.h> |
Wade Guthrie | cc53f23 | 2013-03-05 13:22:23 -0800 | [diff] [blame] | 32 | #include <gtest/gtest_prod.h> // for FRIEND_TEST |
Wade Guthrie | 0d43853 | 2012-05-18 14:18:50 -0700 | [diff] [blame] | 33 | |
| 34 | namespace shill { |
| 35 | |
Wade Guthrie | cc53f23 | 2013-03-05 13:22:23 -0800 | [diff] [blame] | 36 | class Sockets; |
Wade Guthrie | bee87c2 | 2013-03-06 11:00:46 -0800 | [diff] [blame] | 37 | class ByteString; |
repo sync | dc085c8 | 2012-12-28 08:54:41 -0800 | [diff] [blame] | 38 | |
Wade Guthrie | 0d43853 | 2012-05-18 14:18:50 -0700 | [diff] [blame] | 39 | // Provides an abstraction to a netlink socket. See |
Wade Guthrie | cc53f23 | 2013-03-05 13:22:23 -0800 | [diff] [blame] | 40 | // 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 Guthrie | 0d43853 | 2012-05-18 14:18:50 -0700 | [diff] [blame] | 44 | class NetlinkSocket { |
| 45 | public: |
Wade Guthrie | cc53f23 | 2013-03-05 13:22:23 -0800 | [diff] [blame] | 46 | static const int kReceiveBufferSize; |
| 47 | |
| 48 | NetlinkSocket(); |
Wade Guthrie | 0d43853 | 2012-05-18 14:18:50 -0700 | [diff] [blame] | 49 | virtual ~NetlinkSocket(); |
| 50 | |
| 51 | // Non-trivial initialization. |
| 52 | bool Init(); |
| 53 | |
Wade Guthrie | 0d43853 | 2012-05-18 14:18:50 -0700 | [diff] [blame] | 54 | // Returns the file descriptor used by the socket. |
Wade Guthrie | fa2100e | 2013-05-15 10:11:22 -0700 | [diff] [blame^] | 55 | virtual int file_descriptor() const { return file_descriptor_; } |
Wade Guthrie | 0d43853 | 2012-05-18 14:18:50 -0700 | [diff] [blame] | 56 | |
Wade Guthrie | cc53f23 | 2013-03-05 13:22:23 -0800 | [diff] [blame] | 57 | // Get the next message sequence number for this socket. |
| 58 | // |GetSequenceNumber| won't return zero because that is the 'broadcast' |
| 59 | // sequence number. |
repo sync | dc085c8 | 2012-12-28 08:54:41 -0800 | [diff] [blame] | 60 | virtual uint32_t GetSequenceNumber(); |
Wade Guthrie | 0d43853 | 2012-05-18 14:18:50 -0700 | [diff] [blame] | 61 | |
Wade Guthrie | cc53f23 | 2013-03-05 13:22:23 -0800 | [diff] [blame] | 62 | // 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 Guthrie | fa2100e | 2013-05-15 10:11:22 -0700 | [diff] [blame^] | 65 | virtual bool RecvMessage(ByteString *message); |
Wade Guthrie | 0d43853 | 2012-05-18 14:18:50 -0700 | [diff] [blame] | 66 | |
repo sync | dc085c8 | 2012-12-28 08:54:41 -0800 | [diff] [blame] | 67 | // Sends a message, returns true if successful. |
Wade Guthrie | bdcdaa7 | 2013-03-04 12:47:12 -0800 | [diff] [blame] | 68 | virtual bool SendMessage(const ByteString &message); |
Christopher Wiley | 393b93f | 2012-11-08 17:30:58 -0800 | [diff] [blame] | 69 | |
Wade Guthrie | bee87c2 | 2013-03-06 11:00:46 -0800 | [diff] [blame] | 70 | // Subscribes to netlink broadcast events. |
| 71 | virtual bool SubscribeToEvents(uint32_t group_id); |
| 72 | |
Wade Guthrie | fa2100e | 2013-05-15 10:11:22 -0700 | [diff] [blame^] | 73 | virtual const Sockets *sockets() const { return sockets_.get(); } |
| 74 | |
repo sync | dc085c8 | 2012-12-28 08:54:41 -0800 | [diff] [blame] | 75 | protected: |
Wade Guthrie | cc53f23 | 2013-03-05 13:22:23 -0800 | [diff] [blame] | 76 | uint32_t sequence_number_; |
Wade Guthrie | 0d43853 | 2012-05-18 14:18:50 -0700 | [diff] [blame] | 77 | |
| 78 | private: |
Wade Guthrie | fa2100e | 2013-05-15 10:11:22 -0700 | [diff] [blame^] | 79 | friend class NetlinkManagerTest; |
Wade Guthrie | cc53f23 | 2013-03-05 13:22:23 -0800 | [diff] [blame] | 80 | friend class NetlinkSocketTest; |
| 81 | FRIEND_TEST(NetlinkSocketTest, SequenceNumberTest); |
Wade Guthrie | 0d43853 | 2012-05-18 14:18:50 -0700 | [diff] [blame] | 82 | |
Wade Guthrie | cc53f23 | 2013-03-05 13:22:23 -0800 | [diff] [blame] | 83 | scoped_ptr<Sockets> sockets_; |
| 84 | int file_descriptor_; |
Wade Guthrie | 0d43853 | 2012-05-18 14:18:50 -0700 | [diff] [blame] | 85 | |
| 86 | DISALLOW_COPY_AND_ASSIGN(NetlinkSocket); |
| 87 | }; |
| 88 | |
| 89 | } // namespace shill |
| 90 | |
| 91 | #endif // SHILL_NETLINK_SOCKET_H_ |