blob: 0e7d4eb7ba0b39818e6d8894ce482e3fcad3c283 [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
Ben Chana0ddf462014-02-06 11:32:42 -080011#include <base/files/file_path.h>
Ben Chancc67c522014-09-03 07:19:18 -070012#include <base/macros.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(
Ben Chan7fab8972014-08-10 17:14:46 -070053 const std::string &input, IPAddress *ip_address, uint16_t *port);
Ben Chan3e4bf162013-04-03 18:14:51 -070054 bool ParseIPAddress(const std::string &input, IPAddress *ip_address);
Ben Chan7fab8972014-08-10 17:14:46 -070055 bool ParsePort(const std::string &input, uint16_t *port);
Ben Chan3e4bf162013-04-03 18:14:51 -070056 bool ParseTransimitAndReceiveQueueValues(
57 const std::string &input,
Ben Chan7fab8972014-08-10 17:14:46 -070058 uint64_t *transmit_queue_value, uint64_t *receive_queue_value);
Ben Chan3e4bf162013-04-03 18:14:51 -070059 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_