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 | |
| 28 | #include <iomanip> |
| 29 | #include <string> |
| 30 | |
| 31 | #include <asm/types.h> |
| 32 | #include <linux/nl80211.h> |
| 33 | #include <netlink/handlers.h> |
| 34 | #include <netlink/netlink.h> |
| 35 | |
| 36 | #include <base/basictypes.h> |
| 37 | #include <base/bind.h> |
| 38 | #include <base/logging.h> |
| 39 | |
Wade Guthrie | 0d43853 | 2012-05-18 14:18:50 -0700 | [diff] [blame] | 40 | enum nl_cb_kind; |
| 41 | enum nl_cb_type; |
| 42 | struct nl_msg; |
| 43 | |
| 44 | namespace shill { |
| 45 | |
repo sync | dc085c8 | 2012-12-28 08:54:41 -0800 | [diff] [blame] | 46 | class Nl80211Message; |
| 47 | |
Wade Guthrie | 0d43853 | 2012-05-18 14:18:50 -0700 | [diff] [blame] | 48 | // Provides an abstraction to a netlink socket. See |
| 49 | // http://www.infradead.org/~tgr/libnl/ for documentation on how netlink |
| 50 | // sockets work. |
| 51 | class NetlinkSocket { |
| 52 | public: |
Wade Guthrie | 0d43853 | 2012-05-18 14:18:50 -0700 | [diff] [blame] | 53 | NetlinkSocket() : nl_sock_(NULL) {} |
| 54 | virtual ~NetlinkSocket(); |
| 55 | |
| 56 | // Non-trivial initialization. |
| 57 | bool Init(); |
| 58 | |
| 59 | // Disables sequence checking on the message stream. |
| 60 | virtual bool DisableSequenceChecking(); |
| 61 | |
| 62 | // Returns the file descriptor used by the socket. |
| 63 | virtual int GetFd() const; |
| 64 | |
Wade Guthrie | 5d3d6de | 2012-11-02 11:08:34 -0700 | [diff] [blame] | 65 | // Get the next message sequence number for this socket. Disallow zero so |
| 66 | // that we can use that as the 'broadcast' sequence number. |
repo sync | dc085c8 | 2012-12-28 08:54:41 -0800 | [diff] [blame] | 67 | virtual uint32_t GetSequenceNumber(); |
Wade Guthrie | 0d43853 | 2012-05-18 14:18:50 -0700 | [diff] [blame] | 68 | |
repo sync | dc085c8 | 2012-12-28 08:54:41 -0800 | [diff] [blame] | 69 | // Returns the value returned by the 'genl_ctrl_resolve' call. |
| 70 | virtual int family_id() const = 0; |
| 71 | |
Wade Guthrie | 0d43853 | 2012-05-18 14:18:50 -0700 | [diff] [blame] | 72 | // Returns the family name of the socket created by this type of object. |
| 73 | virtual std::string GetSocketFamilyName() const = 0; |
| 74 | |
repo sync | dc085c8 | 2012-12-28 08:54:41 -0800 | [diff] [blame] | 75 | // Sends a message, returns true if successful. |
| 76 | virtual bool SendMessage(Nl80211Message *message); |
Christopher Wiley | 393b93f | 2012-11-08 17:30:58 -0800 | [diff] [blame] | 77 | |
repo sync | dc085c8 | 2012-12-28 08:54:41 -0800 | [diff] [blame] | 78 | protected: |
Wade Guthrie | 0d43853 | 2012-05-18 14:18:50 -0700 | [diff] [blame] | 79 | struct nl_sock *GetNlSock() { return nl_sock_; } |
| 80 | |
| 81 | private: |
| 82 | // Netlink Callback function for nl80211. Used to disable the sequence |
| 83 | // checking on messages received from the netlink module. |
| 84 | static int IgnoreSequenceCheck(nl_msg *ignored_msg, void *ignored_arg); |
| 85 | |
| 86 | struct nl_sock *nl_sock_; |
| 87 | |
| 88 | DISALLOW_COPY_AND_ASSIGN(NetlinkSocket); |
| 89 | }; |
| 90 | |
| 91 | } // namespace shill |
| 92 | |
| 93 | #endif // SHILL_NETLINK_SOCKET_H_ |