Deepak Kodihalli | c496619 | 2018-08-23 02:19:58 -0500 | [diff] [blame] | 1 | #include "server-conf.hpp" |
| 2 | #include "utils.hpp" |
Deepak Kodihalli | 4db8146 | 2018-08-27 06:01:46 -0500 | [diff] [blame^] | 3 | #include "xyz/openbmc_project/Common/error.hpp" |
Deepak Kodihalli | c496619 | 2018-08-23 02:19:58 -0500 | [diff] [blame] | 4 | #include <fstream> |
Deepak Kodihalli | 4db8146 | 2018-08-27 06:01:46 -0500 | [diff] [blame^] | 5 | #include <phosphor-logging/log.hpp> |
| 6 | #include <phosphor-logging/elog.hpp> |
| 7 | #if __has_include("../../usr/include/phosphor-logging/elog-errors.hpp") |
| 8 | #include "../../usr/include/phosphor-logging/elog-errors.hpp" |
| 9 | #else |
| 10 | #include <phosphor-logging/elog-errors.hpp> |
| 11 | #endif |
| 12 | #include <netdb.h> |
| 13 | #include <arpa/inet.h> |
Deepak Kodihalli | c496619 | 2018-08-23 02:19:58 -0500 | [diff] [blame] | 14 | |
| 15 | namespace phosphor |
| 16 | { |
| 17 | namespace rsyslog_config |
| 18 | { |
| 19 | |
| 20 | namespace utils = phosphor::rsyslog_utils; |
Deepak Kodihalli | 4db8146 | 2018-08-27 06:01:46 -0500 | [diff] [blame^] | 21 | using namespace phosphor::logging; |
| 22 | using namespace sdbusplus::xyz::openbmc_project::Common::Error; |
Deepak Kodihalli | c496619 | 2018-08-23 02:19:58 -0500 | [diff] [blame] | 23 | |
| 24 | std::string Server::address(std::string value) |
| 25 | { |
Deepak Kodihalli | 4db8146 | 2018-08-27 06:01:46 -0500 | [diff] [blame^] | 26 | using Argument = xyz::openbmc_project::Common::InvalidArgument; |
| 27 | std::string result {}; |
| 28 | |
| 29 | try |
Deepak Kodihalli | 0febd26 | 2018-08-27 05:45:02 -0500 | [diff] [blame] | 30 | { |
Deepak Kodihalli | 4db8146 | 2018-08-27 06:01:46 -0500 | [diff] [blame^] | 31 | auto serverAddress = address(); |
| 32 | if (serverAddress == value) |
| 33 | { |
| 34 | return serverAddress; |
| 35 | } |
| 36 | |
| 37 | if (!value.empty() && !addressValid(value)) |
| 38 | { |
| 39 | elog<InvalidArgument>(Argument::ARGUMENT_NAME("Address"), |
| 40 | Argument::ARGUMENT_VALUE(value.c_str())); |
| 41 | } |
| 42 | |
| 43 | writeConfig(value, port(), configFilePath.c_str()); |
| 44 | result = std::move(NetworkClient::address(value)); |
| 45 | } |
| 46 | catch (const InvalidArgument& e) |
| 47 | { |
| 48 | throw; |
| 49 | } |
| 50 | catch (const InternalFailure& e) |
| 51 | { |
| 52 | throw; |
| 53 | } |
| 54 | catch (const std::exception& e) |
| 55 | { |
| 56 | log<level::ERR>(e.what()); |
| 57 | elog<InternalFailure>(); |
Deepak Kodihalli | 0febd26 | 2018-08-27 05:45:02 -0500 | [diff] [blame] | 58 | } |
| 59 | |
Deepak Kodihalli | c496619 | 2018-08-23 02:19:58 -0500 | [diff] [blame] | 60 | return result; |
| 61 | } |
| 62 | |
| 63 | uint16_t Server::port(uint16_t value) |
| 64 | { |
Deepak Kodihalli | 4db8146 | 2018-08-27 06:01:46 -0500 | [diff] [blame^] | 65 | uint16_t result {}; |
| 66 | |
| 67 | try |
Deepak Kodihalli | 0febd26 | 2018-08-27 05:45:02 -0500 | [diff] [blame] | 68 | { |
Deepak Kodihalli | 4db8146 | 2018-08-27 06:01:46 -0500 | [diff] [blame^] | 69 | auto serverPort = port(); |
| 70 | if (serverPort == value) |
| 71 | { |
| 72 | return serverPort; |
| 73 | } |
| 74 | |
| 75 | writeConfig(address(), value, configFilePath.c_str()); |
| 76 | result = NetworkClient::port(value); |
| 77 | } |
| 78 | catch (const InternalFailure& e) |
| 79 | { |
| 80 | throw; |
| 81 | } |
| 82 | catch (const std::exception& e) |
| 83 | { |
| 84 | log<level::ERR>(e.what()); |
| 85 | elog<InternalFailure>(); |
Deepak Kodihalli | 0febd26 | 2018-08-27 05:45:02 -0500 | [diff] [blame] | 86 | } |
| 87 | |
Deepak Kodihalli | c496619 | 2018-08-23 02:19:58 -0500 | [diff] [blame] | 88 | return result; |
| 89 | } |
| 90 | |
| 91 | void Server::writeConfig( |
| 92 | const std::string& serverAddress, |
| 93 | uint16_t serverPort, |
| 94 | const char* filePath) |
| 95 | { |
Deepak Kodihalli | 0febd26 | 2018-08-27 05:45:02 -0500 | [diff] [blame] | 96 | std::fstream stream(filePath, std::fstream::out); |
| 97 | |
Deepak Kodihalli | c496619 | 2018-08-23 02:19:58 -0500 | [diff] [blame] | 98 | if (serverPort && !serverAddress.empty()) |
| 99 | { |
Deepak Kodihalli | 0febd26 | 2018-08-27 05:45:02 -0500 | [diff] [blame] | 100 | // write '*.* @@<remote-host>:<port>' |
Deepak Kodihalli | c496619 | 2018-08-23 02:19:58 -0500 | [diff] [blame] | 101 | stream << "*.* @@" << serverAddress << ":" << serverPort; |
Deepak Kodihalli | c496619 | 2018-08-23 02:19:58 -0500 | [diff] [blame] | 102 | } |
Deepak Kodihalli | 0febd26 | 2018-08-27 05:45:02 -0500 | [diff] [blame] | 103 | else // this is a disable request |
| 104 | { |
| 105 | // write '#*.* @@remote-host:port' |
| 106 | stream << "#*.* @@remote-host:port"; |
| 107 | } |
| 108 | |
| 109 | utils::restart(); |
Deepak Kodihalli | c496619 | 2018-08-23 02:19:58 -0500 | [diff] [blame] | 110 | } |
| 111 | |
Deepak Kodihalli | 4db8146 | 2018-08-27 06:01:46 -0500 | [diff] [blame^] | 112 | bool Server::addressValid(const std::string& address) |
| 113 | { |
| 114 | addrinfo hints{}; |
| 115 | addrinfo* res = nullptr; |
| 116 | hints.ai_family = AF_UNSPEC; |
| 117 | hints.ai_socktype = SOCK_STREAM; |
| 118 | hints.ai_flags |= AI_CANONNAME; |
| 119 | |
| 120 | auto result = getaddrinfo(address.c_str(), nullptr, &hints, &res); |
| 121 | if (result) |
| 122 | { |
| 123 | log<level::ERR>("bad address", |
| 124 | entry("ADDRESS=%s", address.c_str()), |
| 125 | entry("ERRNO=%d", result)); |
| 126 | return false; |
| 127 | } |
| 128 | return true; |
| 129 | } |
| 130 | |
Deepak Kodihalli | c496619 | 2018-08-23 02:19:58 -0500 | [diff] [blame] | 131 | } // namespace rsyslog_config |
| 132 | } // namespace phosphor |