blob: c0e9cde27466ba6a86d26c7e7ff7c04db22b7389 [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_NL80211_SOCKET_H_
26#define SHILL_NL80211_SOCKET_H_
27
28#include <iomanip>
29#include <string>
30
31#include <linux/nl80211.h>
32
33#include <base/basictypes.h>
34#include <base/bind.h>
35
36#include "shill/netlink_socket.h"
37
38struct nl_msg;
39struct sockaddr_nl;
40struct nlmsgerr;
41
42namespace shill {
43
44// Provides a mechanism to communicate with the cfg80211 and mac80211 modules
45// utilizing a netlink socket.
46class Nl80211Socket : public NetlinkSocket {
47 public:
48 Nl80211Socket() : nl80211_id_(-1) {}
49 virtual ~Nl80211Socket() {}
50
51 // Perform non-trivial initialization.
52 bool Init();
53
54 // Add ourself to the multicast group that gets sent messages of the
55 // specificed type. Legal |group_name| character strings are defined by
56 // cfg80211 module but they include "config", "scan", "regulatory", and
57 // "mlme".
58 virtual bool AddGroupMembership(const std::string &group_name);
59
60 // Returns the value returned by the 'genl_ctrl_resolve' call.
61 int GetFamilyId() const { return nl80211_id_; }
62
63 // Gets the name of the socket family.
64 std::string GetSocketFamilyName() const {
65 return Nl80211Socket::kSocketFamilyName;
66 }
67
68 private:
69 struct HandlerArgs {
70 HandlerArgs() : group(NULL), id(0) {}
71 HandlerArgs(const char *group_arg, int id_arg) :
72 group(group_arg), id(id_arg) {}
73 const char *group;
74 int id;
75 };
76
77 // Contains 'nl80211', the family name of the netlink socket.
78 static const char kSocketFamilyName[];
79
80 // Method called by cfg80211 to acknowledge messages sent to cfg80211.
81 static int OnAck(nl_msg *unused_msg, void *arg);
82
83 // Method called by cfg80211 for message errors.
84 static int OnError(sockaddr_nl *unused_nla, nlmsgerr *err, void *arg);
85
86 // Netlink Callback for handling response to 'CTRL_CMD_GETFAMILY' message.
87 static int OnFamilyResponse(nl_msg *msg, void *arg);
88
89 // Gets an ID for a specified type of multicast messages sent from the
90 // cfg80211 module.
91 virtual int GetMulticastGroupId(const std::string &group);
92
93 // The id returned by a call to 'genl_ctrl_resolve'.
94 int nl80211_id_;
95
96 DISALLOW_COPY_AND_ASSIGN(Nl80211Socket);
97};
98
99
100} // namespace shill
101
102#endif // SHILL_NL80211_SOCKET_H_