Home | History | Annotate | Download | only in shill
      1 //
      2 // Copyright (C) 2013 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 #ifndef SHILL_SOCKET_INFO_READER_H_
     18 #define SHILL_SOCKET_INFO_READER_H_
     19 
     20 #include <string>
     21 #include <vector>
     22 
     23 #include <base/files/file_path.h>
     24 #include <base/macros.h>
     25 #include <gtest/gtest_prod.h>
     26 
     27 #include "shill/socket_info.h"
     28 
     29 namespace shill {
     30 
     31 class SocketInfoReader {
     32  public:
     33   SocketInfoReader();
     34   virtual ~SocketInfoReader();
     35 
     36   // Returns the file path (/proc/net/tcp by default) from where TCP/IPv4
     37   // socket information are read. Overloadded by unit tests to return a
     38   // different file path.
     39   virtual base::FilePath GetTcpv4SocketInfoFilePath() const;
     40 
     41   // Returns the file path (/proc/net/tcp6 by default) from where TCP/IPv6
     42   // socket information are read. Overloadded by unit tests to return a
     43   // different file path.
     44   virtual base::FilePath GetTcpv6SocketInfoFilePath() const;
     45 
     46   // Loads TCP socket information from /proc/net/tcp and /proc/net/tcp6.
     47   // Existing entries in |info_list| are always discarded. Returns false
     48   // if when neither /proc/net/tcp nor /proc/net/tcp6 can be read.
     49   virtual bool LoadTcpSocketInfo(std::vector<SocketInfo>* info_list);
     50 
     51  private:
     52   FRIEND_TEST(SocketInfoReaderTest, AppendSocketInfo);
     53   FRIEND_TEST(SocketInfoReaderTest, ParseConnectionState);
     54   FRIEND_TEST(SocketInfoReaderTest, ParseIPAddress);
     55   FRIEND_TEST(SocketInfoReaderTest, ParseIPAddressAndPort);
     56   FRIEND_TEST(SocketInfoReaderTest, ParsePort);
     57   FRIEND_TEST(SocketInfoReaderTest, ParseSocketInfo);
     58   FRIEND_TEST(SocketInfoReaderTest, ParseTimerState);
     59   FRIEND_TEST(SocketInfoReaderTest, ParseTransimitAndReceiveQueueValues);
     60 
     61   bool AppendSocketInfo(const base::FilePath& info_file_path,
     62                         std::vector<SocketInfo>* info_list);
     63   bool ParseSocketInfo(const std::string& input, SocketInfo* socket_info);
     64   bool ParseIPAddressAndPort(
     65       const std::string& input, IPAddress* ip_address, uint16_t* port);
     66   bool ParseIPAddress(const std::string& input, IPAddress* ip_address);
     67   bool ParsePort(const std::string& input, uint16_t* port);
     68   bool ParseTransimitAndReceiveQueueValues(
     69       const std::string& input,
     70       uint64_t* transmit_queue_value, uint64_t* receive_queue_value);
     71   bool ParseConnectionState(const std::string& input,
     72                             SocketInfo::ConnectionState* connection_state);
     73   bool ParseTimerState(const std::string& input,
     74                        SocketInfo::TimerState* timer_state);
     75 
     76   DISALLOW_COPY_AND_ASSIGN(SocketInfoReader);
     77 };
     78 
     79 }  // namespace shill
     80 
     81 #endif  // SHILL_SOCKET_INFO_READER_H_
     82