Alex Vakulenko | b04936f | 2014-09-19 14:53:58 -0700 | [diff] [blame] | 1 | // Copyright 2014 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 | #include "buffet/utils.h" |
| 6 | |
| 7 | #include <map> |
Alex Vakulenko | eedf3be | 2015-05-13 17:52:02 -0700 | [diff] [blame] | 8 | #include <netdb.h> |
Alex Vakulenko | b04936f | 2014-09-19 14:53:58 -0700 | [diff] [blame] | 9 | #include <string> |
Alex Vakulenko | eedf3be | 2015-05-13 17:52:02 -0700 | [diff] [blame] | 10 | #include <sys/socket.h> |
| 11 | #include <sys/types.h> |
| 12 | #include <unistd.h> |
Alex Vakulenko | b04936f | 2014-09-19 14:53:58 -0700 | [diff] [blame] | 13 | |
| 14 | #include <base/files/file_util.h> |
| 15 | #include <base/json/json_reader.h> |
| 16 | #include <chromeos/errors/error_codes.h> |
| 17 | |
| 18 | namespace buffet { |
| 19 | |
Alex Vakulenko | 36c85aa | 2015-04-09 09:06:39 -0700 | [diff] [blame] | 20 | namespace { |
| 21 | |
| 22 | // Truncates a string if it is too long. Used for error reporting with really |
| 23 | // long JSON strings. |
| 24 | std::string LimitString(const std::string& text, size_t max_len) { |
| 25 | if (text.size() <= max_len) |
| 26 | return text; |
| 27 | return text.substr(0, max_len - 3) + "..."; |
| 28 | } |
| 29 | |
| 30 | const size_t kMaxStrLen = 1700; // Log messages are limited to 2000 chars. |
| 31 | |
| 32 | } // anonymous namespace |
| 33 | |
Alex Vakulenko | b04936f | 2014-09-19 14:53:58 -0700 | [diff] [blame] | 34 | const char kErrorDomainBuffet[] = "buffet"; |
| 35 | const char kFileReadError[] = "file_read_error"; |
Alex Vakulenko | 07216fe | 2014-09-19 15:31:09 -0700 | [diff] [blame] | 36 | const char kInvalidCategoryError[] = "invalid_category"; |
| 37 | const char kInvalidPackageError[] = "invalid_package"; |
Alex Vakulenko | b04936f | 2014-09-19 14:53:58 -0700 | [diff] [blame] | 38 | |
Vitaly Buka | 207c1cb | 2015-05-14 17:06:18 -0700 | [diff] [blame] | 39 | std::unique_ptr<base::DictionaryValue> LoadJsonDict( |
| 40 | const base::FilePath& json_file_path, |
| 41 | chromeos::ErrorPtr* error) { |
Alex Vakulenko | b04936f | 2014-09-19 14:53:58 -0700 | [diff] [blame] | 42 | std::string json_string; |
| 43 | if (!base::ReadFileToString(json_file_path, &json_string)) { |
Alex Vakulenko | ac8037d | 2014-11-11 11:42:05 -0800 | [diff] [blame] | 44 | chromeos::errors::system::AddSystemError(error, FROM_HERE, errno); |
| 45 | chromeos::Error::AddToPrintf(error, FROM_HERE, kErrorDomainBuffet, |
Alex Vakulenko | b04936f | 2014-09-19 14:53:58 -0700 | [diff] [blame] | 46 | kFileReadError, |
| 47 | "Failed to read file '%s'", |
| 48 | json_file_path.value().c_str()); |
Alex Vakulenko | 9e2f8cd | 2015-04-07 16:28:09 -0700 | [diff] [blame] | 49 | return {}; |
Alex Vakulenko | b04936f | 2014-09-19 14:53:58 -0700 | [diff] [blame] | 50 | } |
Alex Vakulenko | 9e2f8cd | 2015-04-07 16:28:09 -0700 | [diff] [blame] | 51 | return LoadJsonDict(json_string, error); |
| 52 | } |
| 53 | |
Vitaly Buka | 207c1cb | 2015-05-14 17:06:18 -0700 | [diff] [blame] | 54 | std::unique_ptr<base::DictionaryValue> LoadJsonDict( |
| 55 | const std::string& json_string, |
| 56 | chromeos::ErrorPtr* error) { |
| 57 | std::unique_ptr<base::DictionaryValue> result; |
Alex Vakulenko | b04936f | 2014-09-19 14:53:58 -0700 | [diff] [blame] | 58 | std::string error_message; |
| 59 | base::Value* value = base::JSONReader::ReadAndReturnError( |
| 60 | json_string, base::JSON_PARSE_RFC, nullptr, &error_message); |
| 61 | if (!value) { |
Alex Vakulenko | ac8037d | 2014-11-11 11:42:05 -0800 | [diff] [blame] | 62 | chromeos::Error::AddToPrintf(error, FROM_HERE, |
| 63 | chromeos::errors::json::kDomain, |
Alex Vakulenko | b04936f | 2014-09-19 14:53:58 -0700 | [diff] [blame] | 64 | chromeos::errors::json::kParseError, |
Alex Vakulenko | 9e2f8cd | 2015-04-07 16:28:09 -0700 | [diff] [blame] | 65 | "Error parsing JSON string '%s': %s", |
Alex Vakulenko | 36c85aa | 2015-04-09 09:06:39 -0700 | [diff] [blame] | 66 | LimitString(json_string, kMaxStrLen).c_str(), |
Alex Vakulenko | b04936f | 2014-09-19 14:53:58 -0700 | [diff] [blame] | 67 | error_message.c_str()); |
| 68 | return result; |
| 69 | } |
Vitaly Buka | 207c1cb | 2015-05-14 17:06:18 -0700 | [diff] [blame] | 70 | base::DictionaryValue* dict_value = nullptr; |
Alex Vakulenko | b04936f | 2014-09-19 14:53:58 -0700 | [diff] [blame] | 71 | if (!value->GetAsDictionary(&dict_value)) { |
| 72 | delete value; |
Alex Vakulenko | ac8037d | 2014-11-11 11:42:05 -0800 | [diff] [blame] | 73 | chromeos::Error::AddToPrintf(error, FROM_HERE, |
| 74 | chromeos::errors::json::kDomain, |
Alex Vakulenko | b04936f | 2014-09-19 14:53:58 -0700 | [diff] [blame] | 75 | chromeos::errors::json::kObjectExpected, |
Alex Vakulenko | 9e2f8cd | 2015-04-07 16:28:09 -0700 | [diff] [blame] | 76 | "JSON string '%s' is not a JSON object", |
Alex Vakulenko | 36c85aa | 2015-04-09 09:06:39 -0700 | [diff] [blame] | 77 | LimitString(json_string, kMaxStrLen).c_str()); |
Alex Vakulenko | b04936f | 2014-09-19 14:53:58 -0700 | [diff] [blame] | 78 | return result; |
| 79 | } |
| 80 | result.reset(dict_value); |
| 81 | return result; |
| 82 | } |
| 83 | |
Alex Vakulenko | eedf3be | 2015-05-13 17:52:02 -0700 | [diff] [blame] | 84 | int ConnectSocket(const std::string& host, uint16_t port) { |
| 85 | std::string service = std::to_string(port); |
| 86 | addrinfo hints = {0, AF_UNSPEC, SOCK_STREAM}; |
| 87 | addrinfo* result = nullptr; |
| 88 | if (getaddrinfo(host.c_str(), service.c_str(), &hints, &result)) |
| 89 | return -1; |
| 90 | |
| 91 | int socket_fd = -1; |
| 92 | for (const addrinfo* info = result; info != nullptr; info = info->ai_next) { |
| 93 | socket_fd = socket(info->ai_family, info->ai_socktype, info->ai_protocol); |
| 94 | if (socket_fd < 0) |
| 95 | continue; |
| 96 | |
| 97 | if (connect(socket_fd, info->ai_addr, info->ai_addrlen) == 0) |
| 98 | break; // Success. |
| 99 | |
| 100 | close(socket_fd); |
| 101 | socket_fd = -1; |
| 102 | } |
| 103 | |
| 104 | freeaddrinfo(result); |
| 105 | return socket_fd; |
| 106 | } |
| 107 | |
Alex Vakulenko | b04936f | 2014-09-19 14:53:58 -0700 | [diff] [blame] | 108 | } // namespace buffet |