Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(420)

Side by Side Diff: chrome/browser/net/network_stats.h

Issue 7246021: Prevent DOS attack on UDP echo servers by distinguishing between an echo request (Closed) Base URL: svn://chrome-svn/chrome/trunk/src/
Patch Set: '' Created 9 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « no previous file | chrome/browser/net/network_stats.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #ifndef CHROME_BROWSER_NET_NETWORK_STATS_H_ 5 #ifndef CHROME_BROWSER_NET_NETWORK_STATS_H_
6 #define CHROME_BROWSER_NET_NETWORK_STATS_H_ 6 #define CHROME_BROWSER_NET_NETWORK_STATS_H_
7 #pragma once 7 #pragma once
8 8
9 #include <string> 9 #include <string>
10 10
11 #include "base/basictypes.h" 11 #include "base/basictypes.h"
12 #include "base/memory/ref_counted.h" 12 #include "base/memory/ref_counted.h"
13 #include "base/memory/scoped_ptr.h" 13 #include "base/memory/scoped_ptr.h"
14 #include "base/string_util.h"
14 #include "base/time.h" 15 #include "base/time.h"
15 #include "chrome/browser/io_thread.h" 16 #include "chrome/browser/io_thread.h"
16 #include "net/base/address_list.h" 17 #include "net/base/address_list.h"
17 #include "net/base/completion_callback.h" 18 #include "net/base/completion_callback.h"
18 #include "net/base/host_port_pair.h" 19 #include "net/base/host_port_pair.h"
19 #include "net/base/host_resolver.h" 20 #include "net/base/host_resolver.h"
20 #include "net/base/io_buffer.h" 21 #include "net/base/io_buffer.h"
21 #include "net/base/ip_endpoint.h" 22 #include "net/base/ip_endpoint.h"
22 #include "net/base/test_data_stream.h" 23 #include "net/base/test_data_stream.h"
23 #include "net/socket/socket.h" 24 #include "net/socket/socket.h"
24 25
25 namespace chrome_browser_net { 26 namespace chrome_browser_net {
26 27
27 // This class is used for live experiment of network connectivity (either TCP or 28 // This class is used for live experiment of network connectivity (either TCP or
28 // UDP) metrics. A small percentage of users participate in this experiment. All 29 // UDP) metrics. A small percentage of users participate in this experiment. All
29 // users (who are in the experiment) must have enabled "UMA upload". 30 // users (who are in the experiment) must have enabled "UMA upload".
30 // 31 //
31 // This class collects the following stats from users who have opted in. 32 // This class collects the following stats from users who have opted in.
32 // a) What percentage of users can get a message end-to-end to a UDP server? 33 // a) What percentage of users can get a message end-to-end to a UDP server?
33 // b) What percentage of users can get a message end-to-end to a TCP server? 34 // b) What percentage of users can get a message end-to-end to a TCP server?
34 // c) What is the latency for UDP and TCP. 35 // c) What is the latency for UDP and TCP.
35 // d) If connectivity failed, at what stage (Connect or Write or Read) did it 36 // d) If connectivity failed, at what stage (Connect or Write or Read) did it
36 // fail? 37 // fail?
38 //
39 // The following is the overview of the echo message protocol.
40 //
41 // We send the "echo request" to the TCP/UDP servers in the following format:
42 // <version><checksum><payload_size><payload>. <version> is the version number
43 // of the "echo request". <checksum> is the checksum of the <payload>.
44 // <payload_size> specifies the number of bytes in the <payload>.
45 //
46 // TCP/UDP servers respond to the "echo request" by returning "echo response".
47 // "echo response" is of the format:
48 // "<version><checksum><payload_size><key><encoded_payload>". <payload_size>
49 // specifies the number of bytes in the <encoded_payload>. <key> is used to
50 // decode the <encoded_payload>.
37 51
38 class NetworkStats { 52 class NetworkStats {
39 public: 53 public:
40 enum Status { // Used in HISTOGRAM_ENUMERATION. 54 enum Status { // Used in HISTOGRAM_ENUMERATION.
41 SUCCESS, // Successfully received bytes from the server. 55 SUCCESS, // Successfully received bytes from the server.
42 IP_STRING_PARSE_FAILED, // Parsing of IP string failed. 56 IP_STRING_PARSE_FAILED, // Parsing of IP string failed.
57 SOCKET_CREATE_FAILED, // Socket creation failed.
43 RESOLVE_FAILED, // Host resolution failed. 58 RESOLVE_FAILED, // Host resolution failed.
44 CONNECT_FAILED, // Connection to the server failed. 59 CONNECT_FAILED, // Connection to the server failed.
45 WRITE_FAILED, // Sending an echo message to the server failed. 60 WRITE_FAILED, // Sending an echo message to the server failed.
61 READ_TIMED_OUT, // Reading the reply from the server timed out.
46 READ_FAILED, // Reading the reply from the server failed. 62 READ_FAILED, // Reading the reply from the server failed.
47 READ_VERIFY_FAILED, // Verification of data failed. 63 READ_VERIFY_FAILED, // Verification of data failed.
48 STATUS_MAX, // Bounding value. 64 STATUS_MAX, // Bounding value.
49 }; 65 };
50 66
51 protected: 67 protected:
52 // Constructs an NetworkStats object that collects metrics for network 68 // Constructs an NetworkStats object that collects metrics for network
53 // connectivity (either TCP or UDP). 69 // connectivity (either TCP or UDP).
54 NetworkStats(); 70 NetworkStats();
55 virtual ~NetworkStats(); 71 virtual ~NetworkStats();
56 72
57 // Initializes |finished_callback_| and the number of bytes to send to the 73 // Initializes |finished_callback_| and the number of bytes to send to the
58 // server. |finished_callback| is called when we are done with the test. 74 // server. |finished_callback| is called when we are done with the test.
59 // |finished_callback| is mainly useful for unittests. 75 // |finished_callback| is mainly useful for unittests.
60 void Initialize(int bytes_to_send, 76 void Initialize(uint32 bytes_to_send,
61 net::CompletionCallback* finished_callback); 77 net::CompletionCallback* finished_callback);
62 78
63 // This method is called after socket connection is completed. It will send 79 // This method is called after socket connection is completed. It will send
64 // |bytes_to_send| bytes to |server| by calling SendData(). After successfully 80 // |bytes_to_send| bytes to |server| by calling SendData(). After successfully
65 // sending data to the |server|, it calls ReadData() to read/verify the data 81 // sending data to the |server|, it calls ReadData() to read/verify the data
66 // from the |server|. Returns true if successful. 82 // from the |server|. Returns true if successful.
67 bool DoStart(int result); 83 bool DoStart(int result);
68 84
69 // Collects network connectivity stats. This is called when all the data from 85 // Collects network connectivity stats. This is called when all the data from
70 // server is read or when there is a failure during connect/read/write. 86 // server is read or when there is a failure during connect/read/write.
71 virtual void Finish(Status status, int result) {} 87 virtual void Finish(Status status, int result) {}
72 88
73 // This method is called from Finish() and calls |finished_callback_| callback 89 // This method is called from Finish() and calls |finished_callback_| callback
74 // to indicate that the test has finished. 90 // to indicate that the test has finished.
75 void DoFinishCallback(int result); 91 void DoFinishCallback(int result);
76 92
93 // Verifies the data and calls Finish() if there is an error or if all bytes
94 // are read. Returns true if Finish() is called otherwise returns false.
95 virtual bool ReadComplete(int result);
96
77 // Returns the number of bytes to be sent to the |server|. 97 // Returns the number of bytes to be sent to the |server|.
78 int load_size() const { return load_size_; } 98 uint32 load_size() const { return load_size_; }
79 99
80 // Helper methods to get and set |socket_|. 100 // Helper methods to get and set |socket_|.
81 net::Socket* socket() { return socket_.get(); } 101 net::Socket* socket() { return socket_.get(); }
82 void set_socket(net::Socket* socket); 102 void set_socket(net::Socket* socket);
83 103
84 // Returns |start_time_| (used by histograms). 104 // Returns |start_time_| (used by histograms).
85 base::TimeTicks start_time() const { return start_time_; } 105 base::TimeTicks start_time() const { return start_time_; }
86 106
87 private: 107 private:
88 // Verifies the data and calls Finish() if there is an error or if all bytes
89 // are read. Returns true if Finish() is called otherwise returns false.
90 bool ReadComplete(int result);
91
92 // Callbacks when an internal IO is completed. 108 // Callbacks when an internal IO is completed.
93 void OnReadComplete(int result); 109 void OnReadComplete(int result);
94 void OnWriteComplete(int result); 110 void OnWriteComplete(int result);
95 111
96 // Reads data from server until an error occurs. 112 // Reads data from server until an error occurs.
97 void ReadData(); 113 void ReadData();
98 114
99 // Sends data to server until an error occurs. 115 // Sends data to server until an error occurs.
100 int SendData(); 116 int SendData();
101 117
118 // We set a timeout for responses from the echo servers.
119 void StartReadDataTimer(int milliseconds);
120 void OnReadDataTimeout(); // Called when the ReadData Timer fires.
121
122 // Fills the |io_buffer| with the "echo request" message. This gets the
123 // <payload> from |stream_| and calculates the <checksum> of the <payload> and
124 // returns the "echo request" that has <version>, <checksum>, <payload_size>
125 // and <payload>.
126 void GetEchoRequest(net::IOBuffer* io_buffer);
127
128 // This method parses the "echo response" message in the |encoded_message_| to
129 // verify that the <payload> is same as what we had sent in "echo request"
130 // message.
131 bool VerifyBytes();
132
102 // The socket handle for this session. 133 // The socket handle for this session.
103 scoped_ptr<net::Socket> socket_; 134 scoped_ptr<net::Socket> socket_;
104 135
105 // The read buffer used to read data from the socket. 136 // The read buffer used to read data from the socket.
106 scoped_refptr<net::IOBuffer> read_buffer_; 137 scoped_refptr<net::IOBuffer> read_buffer_;
107 138
108 // The write buffer used to write data to the socket. 139 // The write buffer used to write data to the socket.
109 scoped_refptr<net::DrainableIOBuffer> write_buffer_; 140 scoped_refptr<net::DrainableIOBuffer> write_buffer_;
110 141
111 // Some counters for the session. 142 // Some counters for the session.
112 int load_size_; 143 uint32 load_size_;
113 int bytes_to_read_; 144 int bytes_to_read_;
114 int bytes_to_send_; 145 int bytes_to_send_;
115 146
147 // The encoded message read from the server.
148 std::string encoded_message_;
149
116 // |stream_| is used to generate data to be sent to the server and it is also 150 // |stream_| is used to generate data to be sent to the server and it is also
117 // used to verify the data received from the server. 151 // used to verify the data received from the server.
118 net::TestDataStream stream_; 152 net::TestDataStream stream_;
119 153
120 // Callback to call when data is read from the server. 154 // Callback to call when data is read from the server.
121 net::CompletionCallbackImpl<NetworkStats> read_callback_; 155 net::CompletionCallbackImpl<NetworkStats> read_callback_;
122 156
123 // Callback to call when data is sent to the server. 157 // Callback to call when data is sent to the server.
124 net::CompletionCallbackImpl<NetworkStats> write_callback_; 158 net::CompletionCallbackImpl<NetworkStats> write_callback_;
125 159
126 // Callback to call when echo protocol is successefully finished or whenever 160 // Callback to call when echo protocol is successefully finished or whenever
127 // there is an error (this allows unittests to wait until echo protocol's 161 // there is an error (this allows unittests to wait until echo protocol's
128 // round trip is finished). 162 // round trip is finished).
129 net::CompletionCallback* finished_callback_; 163 net::CompletionCallback* finished_callback_;
130 164
131 // The time when the session was started. 165 // The time when the session was started.
132 base::TimeTicks start_time_; 166 base::TimeTicks start_time_;
167
168 // We use this factory to create timeout tasks for socket's ReadData.
169 ScopedRunnableMethodFactory<NetworkStats> timers_factory_;
133 }; 170 };
134 171
135 class UDPStatsClient : public NetworkStats { 172 class UDPStatsClient : public NetworkStats {
136 public: 173 public:
137 // Constructs an UDPStatsClient object that collects metrics for UDP 174 // Constructs an UDPStatsClient object that collects metrics for UDP
138 // connectivity. 175 // connectivity.
139 UDPStatsClient(); 176 UDPStatsClient();
140 virtual ~UDPStatsClient(); 177 virtual ~UDPStatsClient();
141 178
142 // Starts the client, connecting to |server|. 179 // Starts the client, connecting to |server|.
143 // Client will send |bytes_to_send| bytes to |server|. 180 // Client will send |bytes_to_send| bytes to |server|.
144 // When client has received all echoed bytes from the server, or 181 // When client has received all echoed bytes from the server, or
145 // when an error occurs causing the client to stop, |Finish| will be 182 // when an error occurs causing the client to stop, |Finish| will be
146 // called with a net status code. 183 // called with a net status code.
147 // |Finish| will collect histogram stats. 184 // |Finish| will collect histogram stats.
148 // Returns true if successful in starting the client. 185 // Returns true if successful in starting the client.
149 bool Start(const std::string& ip_str, 186 bool Start(const std::string& ip_str,
150 int port, 187 int port,
151 int bytes_to_send, 188 uint32 bytes_to_send,
152 net::CompletionCallback* callback); 189 net::CompletionCallback* callback);
153 190
154 protected: 191 protected:
155 // Allow tests to access our innards for testing purposes. 192 // Allow tests to access our innards for testing purposes.
156 friend class NetworkStatsTestUDP; 193 friend class NetworkStatsTestUDP;
157 194
158 // Collects stats for UDP connectivity. This is called when all the data from 195 // Collects stats for UDP connectivity. This is called when all the data from
159 // server is read or when there is a failure during connect/read/write. 196 // server is read or when there is a failure during connect/read/write.
160 virtual void Finish(Status status, int result); 197 virtual void Finish(Status status, int result);
198
199 // This method calls NetworkStats::ReadComplete() to verify the data and calls
200 // Finish() if there is an error or if read callback didn't return any data
201 // (|result| is less than or equal to 0).
202 virtual bool ReadComplete(int result);
161 }; 203 };
162 204
163 class TCPStatsClient : public NetworkStats { 205 class TCPStatsClient : public NetworkStats {
164 public: 206 public:
165 // Constructs a TCPStatsClient object that collects metrics for TCP 207 // Constructs a TCPStatsClient object that collects metrics for TCP
166 // connectivity. 208 // connectivity.
167 TCPStatsClient(); 209 TCPStatsClient();
168 virtual ~TCPStatsClient(); 210 virtual ~TCPStatsClient();
169 211
170 // Starts the client, connecting to |server|. 212 // Starts the client, connecting to |server|.
171 // Client will send |bytes_to_send| bytes. 213 // Client will send |bytes_to_send| bytes.
172 // When the client has received all echoed bytes from the server, or 214 // When the client has received all echoed bytes from the server, or
173 // when an error occurs causing the client to stop, |Finish| will be 215 // when an error occurs causing the client to stop, |Finish| will be
174 // called with a net status code. 216 // called with a net status code.
175 // |Finish| will collect histogram stats. 217 // |Finish| will collect histogram stats.
176 // Returns true if successful in starting the client. 218 // Returns true if successful in starting the client.
177 bool Start(net::HostResolver* host_resolver, 219 bool Start(net::HostResolver* host_resolver,
178 const net::HostPortPair& server, 220 const net::HostPortPair& server,
179 int bytes_to_send, 221 uint32 bytes_to_send,
180 net::CompletionCallback* callback); 222 net::CompletionCallback* callback);
181 223
182 protected: 224 protected:
183 // Allow tests to access our innards for testing purposes. 225 // Allow tests to access our innards for testing purposes.
184 friend class NetworkStatsTestTCP; 226 friend class NetworkStatsTestTCP;
185 227
186 // Collects stats for TCP connectivity. This is called when all the data from 228 // Collects stats for TCP connectivity. This is called when all the data from
187 // server is read or when there is a failure during connect/read/write. 229 // server is read or when there is a failure during connect/read/write.
188 virtual void Finish(Status status, int result); 230 virtual void Finish(Status status, int result);
189 231
232 // This method calls NetworkStats::ReadComplete() to verify the data and calls
233 // Finish() if there is an error (|result| is less than 0).
234 virtual bool ReadComplete(int result);
235
190 private: 236 private:
191 // Callback that is called when host resolution is completed. 237 // Callback that is called when host resolution is completed.
192 void OnResolveComplete(int result); 238 void OnResolveComplete(int result);
193 239
194 // Called after host is resolved. Creates TCPClientSocket and connects to the 240 // Called after host is resolved. Creates TCPClientSocket and connects to the
195 // server. 241 // server.
196 bool DoConnect(int result); 242 bool DoConnect(int result);
197 243
198 // Callback that is called when connect is completed and calls DoStart() to 244 // Callback that is called when connect is completed and calls DoStart() to
199 // start the echo protocl. 245 // start the echo protocl.
(...skipping 12 matching lines...) Expand all
212 // This collects the network connectivity stats for UDP and TCP for small 258 // This collects the network connectivity stats for UDP and TCP for small
213 // percentage of users who are participating in the experiment. All users must 259 // percentage of users who are participating in the experiment. All users must
214 // have enabled "UMA upload". This method gets called only if UMA upload to the 260 // have enabled "UMA upload". This method gets called only if UMA upload to the
215 // server has succeeded. 261 // server has succeeded.
216 void CollectNetworkStats(const std::string& network_stats_server_url, 262 void CollectNetworkStats(const std::string& network_stats_server_url,
217 IOThread* io_thread); 263 IOThread* io_thread);
218 264
219 } // namespace chrome_browser_net 265 } // namespace chrome_browser_net
220 266
221 #endif // CHROME_BROWSER_NET_NETWORK_STATS_H_ 267 #endif // CHROME_BROWSER_NET_NETWORK_STATS_H_
OLDNEW
« no previous file with comments | « no previous file | chrome/browser/net/network_stats.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698