1 #include "cetlib/PluginTypeDeducer.h"
2 #include "fhiclcpp/ParameterSet.h"
4 #include "messagefacility/MessageService/ELdestination.h"
5 #include "messagefacility/Utilities/ELseverityLevel.h"
6 #if MESSAGEFACILITY_HEX_VERSION < 0x20201 // v2_02_01 is s67
7 #include "messagefacility/MessageService/MessageDrop.h"
9 #include "messagefacility/MessageLogger/MessageLogger.h"
11 #include "cetlib/compiler_macros.h"
12 #include "messagefacility/Utilities/exception.h"
15 #include <arpa/inet.h>
18 #include <netinet/in.h>
25 #define TRACE_NAME "UDP_mfPlugin"
29 #include <boost/algorithm/string.hpp>
31 #if MESSAGEFACILITY_HEX_VERSION < 0x20201 // format changed to format_ for s67
32 #define format_ format
37 using mf::service::ELdestination;
43 class ELUDP :
public ELdestination
56 fhicl::Name{
"error_turnoff_threshold"},
57 fhicl::Comment{
"Number of errors before turning off destination (default: 0, don't turn off)"}, 0};
59 fhicl::Atom<int>
error_report = fhicl::Atom<int>{fhicl::Name{
"error_report_backoff_factor"},
60 fhicl::Comment{
"Print an error message every N errors"}, 100};
62 fhicl::Atom<std::string>
host =
63 fhicl::Atom<std::string>{fhicl::Name{
"host"}, fhicl::Comment{
"Address to send messages to"},
"227.128.12.27"};
65 fhicl::Atom<int>
port = fhicl::Atom<int>{fhicl::Name{
"port"}, fhicl::Comment{
"Port to send messages to"}, 5140};
68 fhicl::Name{
"multicast_enabled"}, fhicl::Comment{
"Whether messages should be sent via multicast"},
false};
72 fhicl::Name{
"multicast_interface_ip"},
73 fhicl::Comment{
"Use this hostname for multicast output(to assign to the proper NIC)"},
"0.0.0.0"};
90 void fillPrefix(std::ostringstream& o,
const ErrorObj& msg)
override;
97 void fillUsrMsg(std::ostringstream& o,
const ErrorObj& msg)
override;
102 void fillSuffix(std::ostringstream& ,
const ErrorObj& )
override {}
109 void routePayload(
const std::ostringstream& o,
const ErrorObj& e)
override;
115 int error_report_backoff_factor_;
119 bool multicast_enabled_;
120 std::string multicast_out_addr_;
123 struct sockaddr_in message_addr_;
126 int consecutive_success_count_;
128 int next_error_report_;
132 std::string hostname_;
133 std::string hostaddr_;
146 : ELdestination(pset().elDestConfig()), error_report_backoff_factor_(pset().error_report()), error_max_(pset().error_max()), host_(pset().host()), port_(pset().port()), multicast_enabled_(pset().multicast_enabled()), multicast_out_addr_(pset().output_address()), message_socket_(-1), consecutive_success_count_(0), error_count_(0), next_error_report_(1), seqNum_(0), pid_(static_cast<int64_t>(getpid()))
149 char hostname_c[1024];
150 hostname_ = (gethostname(hostname_c, 1023) == 0) ? hostname_c :
"Unkonwn Host";
153 hostent* host =
nullptr;
154 host = gethostbyname(hostname_c);
159 char* ip = inet_ntoa(*reinterpret_cast<struct in_addr*>(host->h_addr));
165 struct ifaddrs* ifAddrStruct =
nullptr;
166 struct ifaddrs* ifa =
nullptr;
167 void* tmpAddrPtr =
nullptr;
169 if (getifaddrs(&ifAddrStruct) != 0)
172 hostaddr_ =
"127.0.0.1";
177 for (ifa = ifAddrStruct; ifa !=
nullptr; ifa = ifa->ifa_next)
179 if (ifa->ifa_addr->sa_family == AF_INET)
182 tmpAddrPtr = &(
reinterpret_cast<struct sockaddr_in*
>(ifa->ifa_addr)->sin_addr);
183 char addressBuffer[INET_ADDRSTRLEN];
184 inet_ntop(AF_INET, tmpAddrPtr, addressBuffer, INET_ADDRSTRLEN);
185 hostaddr_ = addressBuffer;
188 else if (ifa->ifa_addr->sa_family == AF_INET6)
191 tmpAddrPtr = &(
reinterpret_cast<struct sockaddr_in6*
>(ifa->ifa_addr)->sin6_addr);
192 char addressBuffer[INET6_ADDRSTRLEN];
193 inet_ntop(AF_INET6, tmpAddrPtr, addressBuffer, INET6_ADDRSTRLEN);
194 hostaddr_ = addressBuffer;
198 if (!hostaddr_.empty() && (hostaddr_ !=
"127.0.0.1") && (hostaddr_ !=
"::1"))
204 if (hostaddr_.empty())
206 hostaddr_ =
"127.0.0.1";
214 std::ostringstream pid_ostr;
215 pid_ostr <<
"/proc/" << pid_ <<
"/exe";
216 exe = realpath(pid_ostr.str().c_str(), NULL);
218 size_t end = exe.find(
'\0');
219 size_t start = exe.find_last_of(
'/', end);
221 app_ = exe.substr(start + 1, end - start - 1);
224 std::stringstream ss;
225 ss <<
"//proc//" << pid_ <<
"//cmdline";
226 std::ifstream procfile{ss.str().c_str()};
228 std::string procinfo;
230 if (procfile.is_open())
232 procfile >> procinfo;
236 size_t end = procinfo.find(
'\0');
237 size_t start = procinfo.find_last_of(
'/', end);
239 app_ = procinfo.substr(start + 1, end - start - 1);
243 void ELUDP::reconnect_()
245 message_socket_ = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
246 if (message_socket_ < 0)
248 TLOG(TLVL_ERROR) <<
"I failed to create the socket for sending Data messages! err=" << strerror(errno);
251 int sts =
ResolveHost(host_.c_str(), port_, message_addr_);
254 TLOG(TLVL_ERROR) <<
"Unable to resolve Data message address, err=" << strerror(errno);
258 if (multicast_out_addr_ ==
"0.0.0.0")
260 multicast_out_addr_.reserve(HOST_NAME_MAX);
261 sts = gethostname(&multicast_out_addr_[0], HOST_NAME_MAX);
264 TLOG(TLVL_ERROR) <<
"Could not get current hostname, err=" << strerror(errno);
269 if (multicast_out_addr_ !=
"localhost")
276 TLOG(TLVL_ERROR) <<
"Unable to resolve multicast interface address, err=" << strerror(errno);
280 if (setsockopt(message_socket_, IPPROTO_IP, IP_MULTICAST_IF, &addr,
sizeof(addr)) == -1)
282 TLOG(TLVL_ERROR) <<
"Cannot set outgoing interface, err=" << strerror(errno);
287 if (setsockopt(message_socket_, SOL_SOCKET, SO_REUSEADDR, &yes,
sizeof(yes)) < 0)
289 TLOG(TLVL_ERROR) <<
"Unable to enable port reuse on message socket, err=" << strerror(errno);
292 if (setsockopt(message_socket_, IPPROTO_IP, IP_MULTICAST_LOOP, &yes,
sizeof(yes)) < 0)
294 TLOG(TLVL_ERROR) <<
"Unable to enable multicast loopback on message socket, err=" << strerror(errno);
297 if (setsockopt(message_socket_, SOL_SOCKET, SO_BROADCAST, &yes,
sizeof(yes)) == -1)
299 TLOG(TLVL_ERROR) <<
"Cannot set message socket to broadcast, err=" << strerror(errno);
309 const auto& xid = msg.xid();
312 auto module = xid.module();
314 std::replace(
id.begin(),
id.end(),
'|',
'!');
315 std::replace(app.begin(), app.end(),
'|',
'!');
316 std::replace(module.begin(), module.end(),
'|',
'!');
318 oss << format_.timestamp(msg.timestamp()) <<
"|";
319 oss << std::to_string(++seqNum_) <<
"|";
320 oss << hostname_ <<
"|";
321 oss << hostaddr_ <<
"|";
322 oss << xid.severity().getName() <<
"|";
325 #if MESSAGEFACILITY_HEX_VERSION >= 0x20201 // an indication of s67
327 oss << mf::GetIteration() <<
"|";
330 oss << mf::MessageDrop::instance()->iteration <<
"|";
332 oss << module <<
"|";
333 #if MESSAGEFACILITY_HEX_VERSION >= 0x20201
334 oss << msg.filename() <<
"|" << std::to_string(msg.lineNumber()) <<
"|";
343 std::ostringstream tmposs;
345 for (
auto const& val : msg.items())
351 const std::string& usrMsg = tmposs.str().compare(0, 1,
"\n") == 0 ? tmposs.str().erase(0, 1) : tmposs.str();
361 if (message_socket_ == -1)
365 if (error_count_ < error_max_ || error_max_ == 0)
367 char str[INET_ADDRSTRLEN];
368 inet_ntop(AF_INET, &(message_addr_.sin_addr), str, INET_ADDRSTRLEN);
370 auto string =
"UDPMFMESSAGE" + std::to_string(pid_) +
"|" + oss.str();
371 auto sts = sendto(message_socket_,
string.c_str(),
string.size(), 0, reinterpret_cast<struct sockaddr*>(&message_addr_),
372 sizeof(message_addr_));
376 consecutive_success_count_ = 0;
378 if (error_count_ == next_error_report_)
380 TLOG(TLVL_ERROR) <<
"Error sending message " << seqNum_ <<
" to " << host_ <<
", errno=" << errno <<
" ("
381 << strerror(errno) <<
")";
382 next_error_report_ *= error_report_backoff_factor_;
387 ++consecutive_success_count_;
388 if (consecutive_success_count_ >= 5)
391 next_error_report_ = 1;
404 #ifndef EXTERN_C_FUNC_DECLARE_START
405 #define EXTERN_C_FUNC_DECLARE_START extern "C" {
408 EXTERN_C_FUNC_DECLARE_START
409 auto makePlugin(
const std::string& ,
const fhicl::ParameterSet& pset)
411 return std::make_unique<mfplugins::ELUDP>(pset);
415 DEFINE_BASIC_PLUGINTYPE_FUNC(mf::service::ELdestination)
ELUDP(Parameters const &pset)
ELUDP Constructor
int ResolveHost(char const *host_in, in_addr &addr)
Convert a string hostname to a in_addr suitable for socket communication.
Message Facility UDP Streamer Destination Formats messages into a delimited string and sends via UDP ...
void fillPrefix(std::ostringstream &o, const ErrorObj &msg) override
Fill the "Prefix" portion of the message.
int GetInterfaceForNetwork(char const *host_in, in_addr &addr)
Convert an IP address to the network address of the interface sharing the subnet mask.
fhicl::Atom< int > error_report
"error_report_backoff_factor" (Default: 100): Print an error message every N errors ...
fhicl::Atom< int > port
"port" (Default: 5140): Port to send messages to
fhicl::Atom< std::string > host
"host" (Default: "227.128.12.27"): Address to send messages to
fhicl::Atom< std::string > output_address
Configuration Parameters for ELUDP.
void routePayload(const std::ostringstream &o, const ErrorObj &e) override
Serialize a MessageFacility message to the output.
fhicl::WrappedTable< Config > Parameters
Used for ParameterSet validation.
fhicl::Atom< int > error_max
fhicl::TableFragment< ELdestination::Config > elDestConfig
ELDestination common config parameters.
void fillUsrMsg(std::ostringstream &o, const ErrorObj &msg) override
Fill the "User Message" portion of the message.
void fillSuffix(std::ostringstream &, const ErrorObj &) override
Fill the "Suffix" portion of the message (Unused)
fhicl::Atom< bool > multicast_enabled
"multicast_enabled" (Default: false): Whether messages should be sent via multicast ...