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_CONNECTION_INFO_READER_H_
18 #define SHILL_CONNECTION_INFO_READER_H_
19 
20 #include <string>
21 #include <vector>
22 
23 #include <base/macros.h>
24 #include <base/files/file_path.h>
25 #include <gtest/gtest_prod.h>
26 
27 #include "shill/connection_info.h"
28 
29 namespace shill {
30 
31 class ConnectionInfoReader {
32  public:
33   ConnectionInfoReader();
34   virtual ~ConnectionInfoReader();
35 
36   // Returns the file path (/proc/net/ip_conntrack by default) from where
37   // IP connection tracking information are read. Overloadded by unit tests
38   // to return a different file path.
39   virtual base::FilePath GetConnectionInfoFilePath() const;
40 
41   // Loads IP connection tracking information from the file path returned by
42   // GetConnectionInfoFilePath(). Existing entries in |info_list| are always
43   // discarded. Returns true on success.
44   virtual bool LoadConnectionInfo(std::vector<ConnectionInfo>* info_list);
45 
46  private:
47   FRIEND_TEST(ConnectionInfoReaderTest, ParseConnectionInfo);
48   FRIEND_TEST(ConnectionInfoReaderTest, ParseIPAddress);
49   FRIEND_TEST(ConnectionInfoReaderTest, ParseIsUnreplied);
50   FRIEND_TEST(ConnectionInfoReaderTest, ParsePort);
51   FRIEND_TEST(ConnectionInfoReaderTest, ParseProtocol);
52   FRIEND_TEST(ConnectionInfoReaderTest, ParseTimeToExpireSeconds);
53 
54   bool ParseConnectionInfo(const std::string& input, ConnectionInfo* info);
55   bool ParseProtocol(const std::string& input, int* protocol);
56   bool ParseTimeToExpireSeconds(const std::string& input,
57                                 int64_t* time_to_expire_seconds);
58   bool ParseIsUnreplied(const std::string& input, bool* is_unreplied);
59   bool ParseIPAddress(const std::string& input,
60                       IPAddress* ip_address, bool* is_source);
61   bool ParsePort(const std::string& input, uint16_t* port, bool* is_source);
62 
63   DISALLOW_COPY_AND_ASSIGN(ConnectionInfoReader);
64 };
65 
66 }  // namespace shill
67 
68 #endif  // SHILL_CONNECTION_INFO_READER_H_
69