blob: edb1ee4d71b2590a2d0d49302bac9852ceb8ed97 [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
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 Guthrie0d438532012-05-18 14:18:50 -070040enum nl_cb_kind;
41enum nl_cb_type;
42struct nl_msg;
43
44namespace shill {
45
repo syncdc085c82012-12-28 08:54:41 -080046class Nl80211Message;
47
Wade Guthrie0d438532012-05-18 14:18:50 -070048// Provides an abstraction to a netlink socket. See
49// http://www.infradead.org/~tgr/libnl/ for documentation on how netlink
50// sockets work.
51class NetlinkSocket {
52 public:
Wade Guthrie0d438532012-05-18 14:18:50 -070053 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 Guthrie5d3d6de2012-11-02 11:08:34 -070065 // Get the next message sequence number for this socket. Disallow zero so
66 // that we can use that as the 'broadcast' sequence number.
repo syncdc085c82012-12-28 08:54:41 -080067 virtual uint32_t GetSequenceNumber();
Wade Guthrie0d438532012-05-18 14:18:50 -070068
repo syncdc085c82012-12-28 08:54:41 -080069 // Returns the value returned by the 'genl_ctrl_resolve' call.
70 virtual int family_id() const = 0;
71
Wade Guthrie0d438532012-05-18 14:18:50 -070072 // Returns the family name of the socket created by this type of object.
73 virtual std::string GetSocketFamilyName() const = 0;
74
repo syncdc085c82012-12-28 08:54:41 -080075 // Sends a message, returns true if successful.
76 virtual bool SendMessage(Nl80211Message *message);
Christopher Wiley393b93f2012-11-08 17:30:58 -080077
repo syncdc085c82012-12-28 08:54:41 -080078 protected:
Wade Guthrie0d438532012-05-18 14:18:50 -070079 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_