1// 2// Copyright (C) 2012 The Android Open Source Project 3// 4// Licensed under the Apache License, Version 2.0 (the "License"); 5// you may not use this file except in compliance with the License. 6// You may obtain a copy of the License at 7// 8// http://www.apache.org/licenses/LICENSE-2.0 9// 10// Unless required by applicable law or agreed to in writing, software 11// distributed under the License is distributed on an "AS IS" BASIS, 12// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13// See the License for the specific language governing permissions and 14// limitations under the License. 15// 16// 17// This code is derived from the 'iw' source code. The copyright and license 18// of that code is as follows: 19// 20// Copyright (c) 2007, 2008 Johannes Berg 21// Copyright (c) 2007 Andy Lutomirski 22// Copyright (c) 2007 Mike Kershaw 23// Copyright (c) 2008-2009 Luis R. Rodriguez 24// 25// Permission to use, copy, modify, and/or distribute this software for any 26// purpose with or without fee is hereby granted, provided that the above 27// copyright notice and this permission notice appear in all copies. 28// 29// THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES 30// WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 31// MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR 32// ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 33// WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 34// ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 35// OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 36 37#ifndef SHILL_NET_NETLINK_SOCKET_H_ 38#define SHILL_NET_NETLINK_SOCKET_H_ 39 40#include <memory> 41 42#include <base/bind.h> 43#include <base/logging.h> 44#include <base/macros.h> 45#include <gtest/gtest_prod.h> // for FRIEND_TEST 46 47#include "shill/net/shill_export.h" 48 49namespace shill { 50 51class Sockets; 52class ByteString; 53 54// Provides an abstraction to a netlink socket. See 55// http://www.infradead.org/~tgr/libnl/doc/core.html#core_netlink_fundamentals 56// for documentation on how netlink sockets work (note that most of the rest of 57// this document discusses libnl -- something not used by this code). 58class SHILL_EXPORT NetlinkSocket { 59 public: 60 static const int kReceiveBufferSize; 61 62 NetlinkSocket(); 63 virtual ~NetlinkSocket(); 64 65 // Non-trivial initialization. 66 bool Init(); 67 68 // Returns the file descriptor used by the socket. 69 virtual int file_descriptor() const { return file_descriptor_; } 70 71 // Get the next message sequence number for this socket. 72 // |GetSequenceNumber| won't return zero because that is the 'broadcast' 73 // sequence number. 74 virtual uint32_t GetSequenceNumber(); 75 76 // Reads data from the socket into |message| and returns true if successful. 77 // The |message| parameter will be resized to hold the entirety of the read 78 // message (and any data in |message| will be overwritten). 79 virtual bool RecvMessage(ByteString* message); 80 81 // Sends a message, returns true if successful. 82 virtual bool SendMessage(const ByteString& message); 83 84 // Subscribes to netlink broadcast events. 85 virtual bool SubscribeToEvents(uint32_t group_id); 86 87 virtual const Sockets* sockets() const { return sockets_.get(); } 88 89 protected: 90 uint32_t sequence_number_; 91 92 private: 93 friend class NetlinkManagerTest; 94 friend class NetlinkSocketTest; 95 FRIEND_TEST(NetlinkSocketTest, SequenceNumberTest); 96 97 std::unique_ptr<Sockets> sockets_; 98 int file_descriptor_; 99 100 DISALLOW_COPY_AND_ASSIGN(NetlinkSocket); 101}; 102 103} // namespace shill 104 105#endif // SHILL_NET_NETLINK_SOCKET_H_ 106