Ben Chan | 3e4bf16 | 2013-04-03 18:14:51 -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_SOCKET_INFO_READER_H_ |
| 6 | #define SHILL_SOCKET_INFO_READER_H_ |
| 7 | |
| 8 | #include <string> |
| 9 | #include <vector> |
| 10 | |
| 11 | #include <base/basictypes.h> |
Ben Chan | 320d04a | 2013-04-09 13:10:53 -0700 | [diff] [blame] | 12 | #include <base/file_path.h> |
Ben Chan | 3e4bf16 | 2013-04-03 18:14:51 -0700 | [diff] [blame] | 13 | #include <gtest/gtest_prod.h> |
| 14 | |
| 15 | #include "shill/socket_info.h" |
| 16 | |
Ben Chan | 3e4bf16 | 2013-04-03 18:14:51 -0700 | [diff] [blame] | 17 | namespace shill { |
| 18 | |
| 19 | class SocketInfoReader { |
| 20 | public: |
| 21 | SocketInfoReader(); |
| 22 | virtual ~SocketInfoReader(); |
| 23 | |
Ben Chan | 320d04a | 2013-04-09 13:10:53 -0700 | [diff] [blame] | 24 | // Returns the file path (/proc/net/tcp by default) from where TCP/IPv4 |
| 25 | // socket information are read. Overloadded by unit tests to return a |
| 26 | // different file path. |
| 27 | virtual base::FilePath GetTcpv4SocketInfoFilePath() const; |
| 28 | |
| 29 | // Returns the file path (/proc/net/tcp6 by default) from where TCP/IPv6 |
| 30 | // socket information are read. Overloadded by unit tests to return a |
| 31 | // different file path. |
| 32 | virtual base::FilePath GetTcpv6SocketInfoFilePath() const; |
| 33 | |
| 34 | // Loads TCP socket information from /proc/net/tcp and /proc/net/tcp6. |
| 35 | // Existing entries in |info_list| are always discarded. Returns false |
| 36 | // if when neither /proc/net/tcp nor /proc/net/tcp6 can be read. |
Ben Chan | 3e4bf16 | 2013-04-03 18:14:51 -0700 | [diff] [blame] | 37 | virtual bool LoadTcpSocketInfo(std::vector<SocketInfo> *info_list); |
| 38 | |
| 39 | private: |
Ben Chan | 320d04a | 2013-04-09 13:10:53 -0700 | [diff] [blame] | 40 | FRIEND_TEST(SocketInfoReaderTest, AppendSocketInfo); |
Ben Chan | 3e4bf16 | 2013-04-03 18:14:51 -0700 | [diff] [blame] | 41 | FRIEND_TEST(SocketInfoReaderTest, ParseConnectionState); |
| 42 | FRIEND_TEST(SocketInfoReaderTest, ParseIPAddress); |
| 43 | FRIEND_TEST(SocketInfoReaderTest, ParseIPAddressAndPort); |
| 44 | FRIEND_TEST(SocketInfoReaderTest, ParsePort); |
| 45 | FRIEND_TEST(SocketInfoReaderTest, ParseSocketInfo); |
| 46 | FRIEND_TEST(SocketInfoReaderTest, ParseTimerState); |
| 47 | FRIEND_TEST(SocketInfoReaderTest, ParseTransimitAndReceiveQueueValues); |
| 48 | |
Ben Chan | 320d04a | 2013-04-09 13:10:53 -0700 | [diff] [blame] | 49 | bool AppendSocketInfo(const base::FilePath &info_file_path, |
| 50 | std::vector<SocketInfo> *info_list); |
Ben Chan | 3e4bf16 | 2013-04-03 18:14:51 -0700 | [diff] [blame] | 51 | bool ParseSocketInfo(const std::string &input, SocketInfo *socket_info); |
| 52 | bool ParseIPAddressAndPort( |
| 53 | const std::string &input, IPAddress *ip_address, uint16 *port); |
| 54 | bool ParseIPAddress(const std::string &input, IPAddress *ip_address); |
| 55 | bool ParsePort(const std::string &input, uint16 *port); |
| 56 | bool ParseTransimitAndReceiveQueueValues( |
| 57 | const std::string &input, |
| 58 | uint64 *transmit_queue_value, uint64 *receive_queue_value); |
| 59 | bool ParseConnectionState(const std::string &input, |
| 60 | SocketInfo::ConnectionState *connection_state); |
| 61 | bool ParseTimerState(const std::string &input, |
| 62 | SocketInfo::TimerState *timer_state); |
| 63 | |
| 64 | DISALLOW_COPY_AND_ASSIGN(SocketInfoReader); |
| 65 | }; |
| 66 | |
| 67 | } // namespace shill |
| 68 | |
| 69 | #endif // SHILL_SOCKET_INFO_READER_H_ |