blob: f8ccea5c9caf1c80ff26f8f2b6fe51006e4ed6ca [file] [log] [blame]
Ben Chan3e4bf162013-04-03 18:14:51 -07001// 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 Chan320d04a2013-04-09 13:10:53 -070012#include <base/file_path.h>
Ben Chan3e4bf162013-04-03 18:14:51 -070013#include <gtest/gtest_prod.h>
14
15#include "shill/socket_info.h"
16
Ben Chan3e4bf162013-04-03 18:14:51 -070017namespace shill {
18
19class SocketInfoReader {
20 public:
21 SocketInfoReader();
22 virtual ~SocketInfoReader();
23
Ben Chan320d04a2013-04-09 13:10:53 -070024 // 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 Chan3e4bf162013-04-03 18:14:51 -070037 virtual bool LoadTcpSocketInfo(std::vector<SocketInfo> *info_list);
38
39 private:
Ben Chan320d04a2013-04-09 13:10:53 -070040 FRIEND_TEST(SocketInfoReaderTest, AppendSocketInfo);
Ben Chan3e4bf162013-04-03 18:14:51 -070041 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 Chan320d04a2013-04-09 13:10:53 -070049 bool AppendSocketInfo(const base::FilePath &info_file_path,
50 std::vector<SocketInfo> *info_list);
Ben Chan3e4bf162013-04-03 18:14:51 -070051 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_