OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 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 #include "remoting/protocol/network_settings.h" | 5 #include "remoting/protocol/port_range.h" |
6 | 6 |
7 #include <limits.h> | 7 #include <limits.h> |
8 #include <stdlib.h> | 8 #include <stdlib.h> |
9 | 9 |
10 #include "base/strings/string_number_conversions.h" | 10 #include "base/strings/string_number_conversions.h" |
11 #include "base/strings/string_util.h" | 11 #include "base/strings/string_util.h" |
12 | 12 |
13 namespace remoting { | 13 namespace remoting { |
14 namespace protocol { | |
15 | 14 |
16 // static | 15 bool PortRange::Parse(const std::string& port_range, PortRange* result) { |
17 bool NetworkSettings::ParsePortRange(const std::string& port_range, | 16 DCHECK(result); |
18 uint16* out_min_port, | 17 |
19 uint16* out_max_port) { | 18 if (port_range.empty()) { |
| 19 result->min_port = 0; |
| 20 result->max_port = 0; |
| 21 return true; |
| 22 } |
| 23 |
20 size_t separator_index = port_range.find('-'); | 24 size_t separator_index = port_range.find('-'); |
21 if (separator_index == std::string::npos) | 25 if (separator_index == std::string::npos) |
22 return false; | 26 return false; |
23 | 27 |
24 std::string min_port_string, max_port_string; | 28 std::string min_port_string, max_port_string; |
25 base::TrimWhitespaceASCII(port_range.substr(0, separator_index), | 29 base::TrimWhitespaceASCII(port_range.substr(0, separator_index), |
26 base::TRIM_ALL, | 30 base::TRIM_ALL, &min_port_string); |
27 &min_port_string); | |
28 base::TrimWhitespaceASCII(port_range.substr(separator_index + 1), | 31 base::TrimWhitespaceASCII(port_range.substr(separator_index + 1), |
29 base::TRIM_ALL, | 32 base::TRIM_ALL, &max_port_string); |
30 &max_port_string); | |
31 | 33 |
32 unsigned min_port, max_port; | 34 unsigned min_port, max_port; |
33 if (!base::StringToUint(min_port_string, &min_port) || | 35 if (!base::StringToUint(min_port_string, &min_port) || |
34 !base::StringToUint(max_port_string, &max_port)) { | 36 !base::StringToUint(max_port_string, &max_port)) { |
35 return false; | 37 return false; |
36 } | 38 } |
37 | 39 |
38 if (min_port == 0 || min_port > max_port || max_port > USHRT_MAX) | 40 if (min_port == 0 || min_port > max_port || max_port > USHRT_MAX) |
39 return false; | 41 return false; |
40 | 42 |
41 *out_min_port = static_cast<uint16>(min_port); | 43 result->min_port = static_cast<uint16>(min_port); |
42 *out_max_port = static_cast<uint16>(max_port); | 44 result->max_port = static_cast<uint16>(max_port); |
43 return true; | 45 return true; |
44 } | 46 } |
45 | 47 |
46 } // namespace protocol | 48 std::ostream& operator<<(std::ostream& os, const PortRange& port_range) { |
| 49 if (port_range.is_null()) { |
| 50 os << "<no port range specified>"; |
| 51 } else { |
| 52 os << "[" << port_range.min_port << ", " << port_range.max_port << "]"; |
| 53 } |
| 54 return os; |
| 55 } |
| 56 |
47 } // namespace remoting | 57 } // namespace remoting |
OLD | NEW |