blob: 4bf167cb51a50a465952ea59cfa24811ded68d9a [file] [log] [blame]
Deepak Kodihallic4966192018-08-23 02:19:58 -05001#include "server-conf.hpp"
Patrick Venturef18bf832018-10-26 18:14:00 -07002
Deepak Kodihallic4966192018-08-23 02:19:58 -05003#include "utils.hpp"
Deepak Kodihalli4db81462018-08-27 06:01:46 -05004#include "xyz/openbmc_project/Common/error.hpp"
Patrick Venturef18bf832018-10-26 18:14:00 -07005
Deepak Kodihallic4966192018-08-23 02:19:58 -05006#include <fstream>
Deepak Kodihalli4db81462018-08-27 06:01:46 -05007#include <phosphor-logging/elog.hpp>
8#if __has_include("../../usr/include/phosphor-logging/elog-errors.hpp")
9#include "../../usr/include/phosphor-logging/elog-errors.hpp"
10#else
11#include <phosphor-logging/elog-errors.hpp>
12#endif
Deepak Kodihalli4db81462018-08-27 06:01:46 -050013#include <arpa/inet.h>
Patrick Venturef18bf832018-10-26 18:14:00 -070014#include <netdb.h>
Deepak Kodihallic4966192018-08-23 02:19:58 -050015
16namespace phosphor
17{
18namespace rsyslog_config
19{
20
21namespace utils = phosphor::rsyslog_utils;
Deepak Kodihalli4db81462018-08-27 06:01:46 -050022using namespace phosphor::logging;
23using namespace sdbusplus::xyz::openbmc_project::Common::Error;
Deepak Kodihallic4966192018-08-23 02:19:58 -050024
25std::string Server::address(std::string value)
26{
Deepak Kodihalli4db81462018-08-27 06:01:46 -050027 using Argument = xyz::openbmc_project::Common::InvalidArgument;
Patrick Venturef18bf832018-10-26 18:14:00 -070028 std::string result{};
Deepak Kodihalli4db81462018-08-27 06:01:46 -050029
30 try
Deepak Kodihalli0febd262018-08-27 05:45:02 -050031 {
Deepak Kodihalli4db81462018-08-27 06:01:46 -050032 auto serverAddress = address();
33 if (serverAddress == value)
34 {
35 return serverAddress;
36 }
37
38 if (!value.empty() && !addressValid(value))
39 {
40 elog<InvalidArgument>(Argument::ARGUMENT_NAME("Address"),
41 Argument::ARGUMENT_VALUE(value.c_str()));
42 }
43
44 writeConfig(value, port(), configFilePath.c_str());
45 result = std::move(NetworkClient::address(value));
46 }
47 catch (const InvalidArgument& e)
48 {
49 throw;
50 }
51 catch (const InternalFailure& e)
52 {
53 throw;
54 }
55 catch (const std::exception& e)
56 {
57 log<level::ERR>(e.what());
58 elog<InternalFailure>();
Deepak Kodihalli0febd262018-08-27 05:45:02 -050059 }
60
Deepak Kodihallic4966192018-08-23 02:19:58 -050061 return result;
62}
63
64uint16_t Server::port(uint16_t value)
65{
Patrick Venturef18bf832018-10-26 18:14:00 -070066 uint16_t result{};
Deepak Kodihalli4db81462018-08-27 06:01:46 -050067
68 try
Deepak Kodihalli0febd262018-08-27 05:45:02 -050069 {
Deepak Kodihalli4db81462018-08-27 06:01:46 -050070 auto serverPort = port();
71 if (serverPort == value)
72 {
73 return serverPort;
74 }
75
76 writeConfig(address(), value, configFilePath.c_str());
77 result = NetworkClient::port(value);
78 }
79 catch (const InternalFailure& e)
80 {
81 throw;
82 }
83 catch (const std::exception& e)
84 {
85 log<level::ERR>(e.what());
86 elog<InternalFailure>();
Deepak Kodihalli0febd262018-08-27 05:45:02 -050087 }
88
Deepak Kodihallic4966192018-08-23 02:19:58 -050089 return result;
90}
91
Patrick Venturef18bf832018-10-26 18:14:00 -070092void Server::writeConfig(const std::string& serverAddress, uint16_t serverPort,
93 const char* filePath)
Deepak Kodihallic4966192018-08-23 02:19:58 -050094{
Deepak Kodihalli0febd262018-08-27 05:45:02 -050095 std::fstream stream(filePath, std::fstream::out);
96
Deepak Kodihallic4966192018-08-23 02:19:58 -050097 if (serverPort && !serverAddress.empty())
98 {
Deepak Kodihalli0febd262018-08-27 05:45:02 -050099 // write '*.* @@<remote-host>:<port>'
Deepak Kodihallic4966192018-08-23 02:19:58 -0500100 stream << "*.* @@" << serverAddress << ":" << serverPort;
Deepak Kodihallic4966192018-08-23 02:19:58 -0500101 }
Deepak Kodihalli0febd262018-08-27 05:45:02 -0500102 else // this is a disable request
103 {
104 // write '#*.* @@remote-host:port'
105 stream << "#*.* @@remote-host:port";
106 }
107
Deepak Kodihalli2ce7b2c2018-08-31 04:22:55 -0500108 restart();
Deepak Kodihallic4966192018-08-23 02:19:58 -0500109}
110
Deepak Kodihalli4db81462018-08-27 06:01:46 -0500111bool Server::addressValid(const std::string& address)
112{
113 addrinfo hints{};
114 addrinfo* res = nullptr;
115 hints.ai_family = AF_UNSPEC;
116 hints.ai_socktype = SOCK_STREAM;
117 hints.ai_flags |= AI_CANONNAME;
118
119 auto result = getaddrinfo(address.c_str(), nullptr, &hints, &res);
120 if (result)
121 {
Patrick Venturef18bf832018-10-26 18:14:00 -0700122 log<level::ERR>("bad address", entry("ADDRESS=%s", address.c_str()),
Deepak Kodihalli4db81462018-08-27 06:01:46 -0500123 entry("ERRNO=%d", result));
124 return false;
125 }
126 return true;
127}
128
Deepak Kodihalli9fab2792018-08-28 07:47:11 -0500129void Server::restore(const char* filePath)
130{
131 std::fstream stream(filePath, std::fstream::in);
132 std::string line;
133
134 getline(stream, line);
135
136 // Ignore if line is commented
137 if ('#' != line.at(0))
138 {
139 auto pos = line.find(':');
140 if (pos != std::string::npos)
141 {
142 //"*.* @@<address>:<port>"
143 constexpr auto start = 6; // Skip "*.* @@"
144 auto serverAddress = line.substr(start, pos - start);
145 auto serverPort = line.substr(pos + 1);
146 NetworkClient::address(std::move(serverAddress));
147 NetworkClient::port(std::stoul(serverPort));
148 }
149 }
150}
151
Deepak Kodihalli2ce7b2c2018-08-31 04:22:55 -0500152void Server::restart()
153{
154 utils::restart();
155}
156
Deepak Kodihallic4966192018-08-23 02:19:58 -0500157} // namespace rsyslog_config
158} // namespace phosphor