blob: 1b46965f52bf1bb4698bebd1e58d206f3aaacfc7 [file] [log] [blame]
Deepak Kodihallic4966192018-08-23 02:19:58 -05001#include "server-conf.hpp"
2#include "utils.hpp"
Deepak Kodihalli4db81462018-08-27 06:01:46 -05003#include "xyz/openbmc_project/Common/error.hpp"
Deepak Kodihallic4966192018-08-23 02:19:58 -05004#include <fstream>
Deepak Kodihalli4db81462018-08-27 06:01:46 -05005#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 Kodihallic4966192018-08-23 02:19:58 -050014
15namespace phosphor
16{
17namespace rsyslog_config
18{
19
20namespace utils = phosphor::rsyslog_utils;
Deepak Kodihalli4db81462018-08-27 06:01:46 -050021using namespace phosphor::logging;
22using namespace sdbusplus::xyz::openbmc_project::Common::Error;
Deepak Kodihallic4966192018-08-23 02:19:58 -050023
24std::string Server::address(std::string value)
25{
Deepak Kodihalli4db81462018-08-27 06:01:46 -050026 using Argument = xyz::openbmc_project::Common::InvalidArgument;
27 std::string result {};
28
29 try
Deepak Kodihalli0febd262018-08-27 05:45:02 -050030 {
Deepak Kodihalli4db81462018-08-27 06:01:46 -050031 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 Kodihalli0febd262018-08-27 05:45:02 -050058 }
59
Deepak Kodihallic4966192018-08-23 02:19:58 -050060 return result;
61}
62
63uint16_t Server::port(uint16_t value)
64{
Deepak Kodihalli4db81462018-08-27 06:01:46 -050065 uint16_t result {};
66
67 try
Deepak Kodihalli0febd262018-08-27 05:45:02 -050068 {
Deepak Kodihalli4db81462018-08-27 06:01:46 -050069 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 Kodihalli0febd262018-08-27 05:45:02 -050086 }
87
Deepak Kodihallic4966192018-08-23 02:19:58 -050088 return result;
89}
90
91void Server::writeConfig(
92 const std::string& serverAddress,
93 uint16_t serverPort,
94 const char* filePath)
95{
Deepak Kodihalli0febd262018-08-27 05:45:02 -050096 std::fstream stream(filePath, std::fstream::out);
97
Deepak Kodihallic4966192018-08-23 02:19:58 -050098 if (serverPort && !serverAddress.empty())
99 {
Deepak Kodihalli0febd262018-08-27 05:45:02 -0500100 // write '*.* @@<remote-host>:<port>'
Deepak Kodihallic4966192018-08-23 02:19:58 -0500101 stream << "*.* @@" << serverAddress << ":" << serverPort;
Deepak Kodihallic4966192018-08-23 02:19:58 -0500102 }
Deepak Kodihalli0febd262018-08-27 05:45:02 -0500103 else // this is a disable request
104 {
105 // write '#*.* @@remote-host:port'
106 stream << "#*.* @@remote-host:port";
107 }
108
109 utils::restart();
Deepak Kodihallic4966192018-08-23 02:19:58 -0500110}
111
Deepak Kodihalli4db81462018-08-27 06:01:46 -0500112bool 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 Kodihallic4966192018-08-23 02:19:58 -0500131} // namespace rsyslog_config
132} // namespace phosphor