1c0beca55d290fe0b1c96d78cbbcf94b05c23f5a5Peter Qiu//
2c0beca55d290fe0b1c96d78cbbcf94b05c23f5a5Peter Qiu// Copyright (C) 2012 The Android Open Source Project
3c0beca55d290fe0b1c96d78cbbcf94b05c23f5a5Peter Qiu//
4c0beca55d290fe0b1c96d78cbbcf94b05c23f5a5Peter Qiu// Licensed under the Apache License, Version 2.0 (the "License");
5c0beca55d290fe0b1c96d78cbbcf94b05c23f5a5Peter Qiu// you may not use this file except in compliance with the License.
6c0beca55d290fe0b1c96d78cbbcf94b05c23f5a5Peter Qiu// You may obtain a copy of the License at
7c0beca55d290fe0b1c96d78cbbcf94b05c23f5a5Peter Qiu//
8c0beca55d290fe0b1c96d78cbbcf94b05c23f5a5Peter Qiu//      http://www.apache.org/licenses/LICENSE-2.0
9c0beca55d290fe0b1c96d78cbbcf94b05c23f5a5Peter Qiu//
10c0beca55d290fe0b1c96d78cbbcf94b05c23f5a5Peter Qiu// Unless required by applicable law or agreed to in writing, software
11c0beca55d290fe0b1c96d78cbbcf94b05c23f5a5Peter Qiu// distributed under the License is distributed on an "AS IS" BASIS,
12c0beca55d290fe0b1c96d78cbbcf94b05c23f5a5Peter Qiu// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13c0beca55d290fe0b1c96d78cbbcf94b05c23f5a5Peter Qiu// See the License for the specific language governing permissions and
14c0beca55d290fe0b1c96d78cbbcf94b05c23f5a5Peter Qiu// limitations under the License.
15c0beca55d290fe0b1c96d78cbbcf94b05c23f5a5Peter Qiu//
160d43853b1b7aa82afbcbddb6c68bcfd72e98d950Wade Guthrie//
170d43853b1b7aa82afbcbddb6c68bcfd72e98d950Wade Guthrie// This code is derived from the 'iw' source code.  The copyright and license
180d43853b1b7aa82afbcbddb6c68bcfd72e98d950Wade Guthrie// of that code is as follows:
190d43853b1b7aa82afbcbddb6c68bcfd72e98d950Wade Guthrie//
200d43853b1b7aa82afbcbddb6c68bcfd72e98d950Wade Guthrie// Copyright (c) 2007, 2008  Johannes Berg
210d43853b1b7aa82afbcbddb6c68bcfd72e98d950Wade Guthrie// Copyright (c) 2007  Andy Lutomirski
220d43853b1b7aa82afbcbddb6c68bcfd72e98d950Wade Guthrie// Copyright (c) 2007  Mike Kershaw
230d43853b1b7aa82afbcbddb6c68bcfd72e98d950Wade Guthrie// Copyright (c) 2008-2009  Luis R. Rodriguez
240d43853b1b7aa82afbcbddb6c68bcfd72e98d950Wade Guthrie//
250d43853b1b7aa82afbcbddb6c68bcfd72e98d950Wade Guthrie// Permission to use, copy, modify, and/or distribute this software for any
260d43853b1b7aa82afbcbddb6c68bcfd72e98d950Wade Guthrie// purpose with or without fee is hereby granted, provided that the above
270d43853b1b7aa82afbcbddb6c68bcfd72e98d950Wade Guthrie// copyright notice and this permission notice appear in all copies.
280d43853b1b7aa82afbcbddb6c68bcfd72e98d950Wade Guthrie//
290d43853b1b7aa82afbcbddb6c68bcfd72e98d950Wade Guthrie// THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
300d43853b1b7aa82afbcbddb6c68bcfd72e98d950Wade Guthrie// WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
310d43853b1b7aa82afbcbddb6c68bcfd72e98d950Wade Guthrie// MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
320d43853b1b7aa82afbcbddb6c68bcfd72e98d950Wade Guthrie// ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
330d43853b1b7aa82afbcbddb6c68bcfd72e98d950Wade Guthrie// WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
340d43853b1b7aa82afbcbddb6c68bcfd72e98d950Wade Guthrie// ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
350d43853b1b7aa82afbcbddb6c68bcfd72e98d950Wade Guthrie// OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
360d43853b1b7aa82afbcbddb6c68bcfd72e98d950Wade Guthrie
3702e3dc3bc6e9fdb475bd13944f1c6764c921abbbPeter Qiu#ifndef SHILL_NET_NETLINK_SOCKET_H_
3802e3dc3bc6e9fdb475bd13944f1c6764c921abbbPeter Qiu#define SHILL_NET_NETLINK_SOCKET_H_
390d43853b1b7aa82afbcbddb6c68bcfd72e98d950Wade Guthrie
40879f25f654d864eab44a758d8321d589c167258eBen Chan#include <memory>
41879f25f654d864eab44a758d8321d589c167258eBen Chan
420d43853b1b7aa82afbcbddb6c68bcfd72e98d950Wade Guthrie#include <base/bind.h>
430d43853b1b7aa82afbcbddb6c68bcfd72e98d950Wade Guthrie#include <base/logging.h>
44cc67c52a2c00f90e877971d552208dd99825d84eBen Chan#include <base/macros.h>
45cc53f236c0696383151105337f50993c71e5b443Wade Guthrie#include <gtest/gtest_prod.h>  // for FRIEND_TEST
460d43853b1b7aa82afbcbddb6c68bcfd72e98d950Wade Guthrie
471da9419d10a1a617cc634e79772335fa08b3420fPeter Qiu#include "shill/net/shill_export.h"
48ba24e6fd0f0f2b974e0e73039d7d92bb475f6857Peter Qiu
490d43853b1b7aa82afbcbddb6c68bcfd72e98d950Wade Guthrienamespace shill {
500d43853b1b7aa82afbcbddb6c68bcfd72e98d950Wade Guthrie
51cc53f236c0696383151105337f50993c71e5b443Wade Guthrieclass Sockets;
52bee87c282fc49beeceaa0f5bbce34c639ac03816Wade Guthrieclass ByteString;
53dc085c8d2c50e71e230b594b2472da7498705fa4repo sync
540d43853b1b7aa82afbcbddb6c68bcfd72e98d950Wade Guthrie// Provides an abstraction to a netlink socket.  See
55cc53f236c0696383151105337f50993c71e5b443Wade Guthrie// http://www.infradead.org/~tgr/libnl/doc/core.html#core_netlink_fundamentals
56cc53f236c0696383151105337f50993c71e5b443Wade Guthrie// for documentation on how netlink sockets work (note that most of the rest of
5757282d5403b23162de1ded91ec8d73bdc19c922aPaul Stewart// this document discusses libnl -- something not used by this code).
58ba24e6fd0f0f2b974e0e73039d7d92bb475f6857Peter Qiuclass SHILL_EXPORT NetlinkSocket {
590d43853b1b7aa82afbcbddb6c68bcfd72e98d950Wade Guthrie public:
60cc53f236c0696383151105337f50993c71e5b443Wade Guthrie  static const int kReceiveBufferSize;
61cc53f236c0696383151105337f50993c71e5b443Wade Guthrie
62cc53f236c0696383151105337f50993c71e5b443Wade Guthrie  NetlinkSocket();
630d43853b1b7aa82afbcbddb6c68bcfd72e98d950Wade Guthrie  virtual ~NetlinkSocket();
640d43853b1b7aa82afbcbddb6c68bcfd72e98d950Wade Guthrie
650d43853b1b7aa82afbcbddb6c68bcfd72e98d950Wade Guthrie  // Non-trivial initialization.
660d43853b1b7aa82afbcbddb6c68bcfd72e98d950Wade Guthrie  bool Init();
670d43853b1b7aa82afbcbddb6c68bcfd72e98d950Wade Guthrie
680d43853b1b7aa82afbcbddb6c68bcfd72e98d950Wade Guthrie  // Returns the file descriptor used by the socket.
69fa2100e878bb51d2c51b68a2f5b34124e73b4aa0Wade Guthrie  virtual int file_descriptor() const { return file_descriptor_; }
700d43853b1b7aa82afbcbddb6c68bcfd72e98d950Wade Guthrie
71cc53f236c0696383151105337f50993c71e5b443Wade Guthrie  // Get the next message sequence number for this socket.
72cc53f236c0696383151105337f50993c71e5b443Wade Guthrie  // |GetSequenceNumber| won't return zero because that is the 'broadcast'
73cc53f236c0696383151105337f50993c71e5b443Wade Guthrie  // sequence number.
74dc085c8d2c50e71e230b594b2472da7498705fa4repo sync  virtual uint32_t GetSequenceNumber();
750d43853b1b7aa82afbcbddb6c68bcfd72e98d950Wade Guthrie
76cc53f236c0696383151105337f50993c71e5b443Wade Guthrie  // Reads data from the socket into |message| and returns true if successful.
77cc53f236c0696383151105337f50993c71e5b443Wade Guthrie  // The |message| parameter will be resized to hold the entirety of the read
78cc53f236c0696383151105337f50993c71e5b443Wade Guthrie  // message (and any data in |message| will be overwritten).
79e67a78539a05ea7fc68ed5ca18f6d1de333a3086Paul Stewart  virtual bool RecvMessage(ByteString* message);
800d43853b1b7aa82afbcbddb6c68bcfd72e98d950Wade Guthrie
81dc085c8d2c50e71e230b594b2472da7498705fa4repo sync  // Sends a message, returns true if successful.
82e67a78539a05ea7fc68ed5ca18f6d1de333a3086Paul Stewart  virtual bool SendMessage(const ByteString& message);
83393b93f3a7684828f32a481b34cc2e94c6825f50Christopher Wiley
84bee87c282fc49beeceaa0f5bbce34c639ac03816Wade Guthrie  // Subscribes to netlink broadcast events.
85bee87c282fc49beeceaa0f5bbce34c639ac03816Wade Guthrie  virtual bool SubscribeToEvents(uint32_t group_id);
86bee87c282fc49beeceaa0f5bbce34c639ac03816Wade Guthrie
87e67a78539a05ea7fc68ed5ca18f6d1de333a3086Paul Stewart  virtual const Sockets* sockets() const { return sockets_.get(); }
88fa2100e878bb51d2c51b68a2f5b34124e73b4aa0Wade Guthrie
89dc085c8d2c50e71e230b594b2472da7498705fa4repo sync protected:
90cc53f236c0696383151105337f50993c71e5b443Wade Guthrie  uint32_t sequence_number_;
910d43853b1b7aa82afbcbddb6c68bcfd72e98d950Wade Guthrie
920d43853b1b7aa82afbcbddb6c68bcfd72e98d950Wade Guthrie private:
93fa2100e878bb51d2c51b68a2f5b34124e73b4aa0Wade Guthrie  friend class NetlinkManagerTest;
94cc53f236c0696383151105337f50993c71e5b443Wade Guthrie  friend class NetlinkSocketTest;
95cc53f236c0696383151105337f50993c71e5b443Wade Guthrie  FRIEND_TEST(NetlinkSocketTest, SequenceNumberTest);
960d43853b1b7aa82afbcbddb6c68bcfd72e98d950Wade Guthrie
97879f25f654d864eab44a758d8321d589c167258eBen Chan  std::unique_ptr<Sockets> sockets_;
98cc53f236c0696383151105337f50993c71e5b443Wade Guthrie  int file_descriptor_;
990d43853b1b7aa82afbcbddb6c68bcfd72e98d950Wade Guthrie
1000d43853b1b7aa82afbcbddb6c68bcfd72e98d950Wade Guthrie  DISALLOW_COPY_AND_ASSIGN(NetlinkSocket);
1010d43853b1b7aa82afbcbddb6c68bcfd72e98d950Wade Guthrie};
1020d43853b1b7aa82afbcbddb6c68bcfd72e98d950Wade Guthrie
1030d43853b1b7aa82afbcbddb6c68bcfd72e98d950Wade Guthrie}  // namespace shill
1040d43853b1b7aa82afbcbddb6c68bcfd72e98d950Wade Guthrie
10502e3dc3bc6e9fdb475bd13944f1c6764c921abbbPeter Qiu#endif  // SHILL_NET_NETLINK_SOCKET_H_
106