Ben Chan | bac5bc8 | 2013-04-12 17:15:43 -0700 | [diff] [blame] | 1 | // Copyright (c) 2013 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 | #ifndef SHILL_CONNECTION_INFO_READER_H_ |
| 6 | #define SHILL_CONNECTION_INFO_READER_H_ |
| 7 | |
| 8 | #include <string> |
| 9 | #include <vector> |
| 10 | |
| 11 | #include <base/basictypes.h> |
| 12 | #include <base/file_path.h> |
| 13 | #include <gtest/gtest_prod.h> |
| 14 | |
| 15 | #include "shill/connection_info.h" |
| 16 | |
| 17 | namespace shill { |
| 18 | |
| 19 | class ConnectionInfoReader { |
| 20 | public: |
| 21 | ConnectionInfoReader(); |
| 22 | virtual ~ConnectionInfoReader(); |
| 23 | |
| 24 | // Returns the file path (/proc/net/ip_conntrack by default) from where |
| 25 | // IP connection tracking information are read. Overloadded by unit tests |
| 26 | // to return a different file path. |
| 27 | virtual base::FilePath GetConnectionInfoFilePath() const; |
| 28 | |
| 29 | // Loads IP connection tracking information from the file path returned by |
| 30 | // GetConnectionInfoFilePath(). Existing entries in |info_list| are always |
| 31 | // discarded. Returns true on success. |
| 32 | virtual bool LoadConnectionInfo(std::vector<ConnectionInfo> *info_list); |
| 33 | |
| 34 | private: |
| 35 | FRIEND_TEST(ConnectionInfoReaderTest, ParseConnectionInfo); |
| 36 | FRIEND_TEST(ConnectionInfoReaderTest, ParseIPAddress); |
| 37 | FRIEND_TEST(ConnectionInfoReaderTest, ParseIsUnreplied); |
| 38 | FRIEND_TEST(ConnectionInfoReaderTest, ParsePort); |
| 39 | FRIEND_TEST(ConnectionInfoReaderTest, ParseProtocol); |
| 40 | FRIEND_TEST(ConnectionInfoReaderTest, ParseTimeToExpireSeconds); |
| 41 | |
| 42 | bool ParseConnectionInfo(const std::string &input, ConnectionInfo *info); |
| 43 | bool ParseProtocol(const std::string &input, int *protocol); |
| 44 | bool ParseTimeToExpireSeconds(const std::string &input, |
| 45 | int64 *time_to_expire_seconds); |
| 46 | bool ParseIsUnreplied(const std::string &input, bool *is_unreplied); |
| 47 | bool ParseIPAddress(const std::string &input, |
| 48 | IPAddress *ip_address, bool *is_source); |
| 49 | bool ParsePort(const std::string &input, uint16 *port, bool *is_source); |
| 50 | |
| 51 | DISALLOW_COPY_AND_ASSIGN(ConnectionInfoReader); |
| 52 | }; |
| 53 | |
| 54 | } // namespace shill |
| 55 | |
| 56 | #endif // SHILL_CONNECTION_INFO_READER_H_ |