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
36 using mf::ELseverityLevel;
38 using mf::service::ELdestination;
44 class ELUDP :
public ELdestination {
46 fhicl::TableFragment<ELdestination::Config> elDestConfig;
47 fhicl::Atom<int> error_max{
48 fhicl::Name{
"error_turnoff_threshold"},
49 fhicl::Comment{
"Number of errors before turning off destination (default: 0, don't turn off)"}, 0};
50 fhicl::Atom<int> error_report{fhicl::Name{
"error_report_backoff_factor"},
51 fhicl::Comment{
"Print an error message every N errors"}, 100};
52 fhicl::Atom<std::string> host{fhicl::Name{
"host"}, fhicl::Comment{
"Address to send messages to"},
"227.128.12.27"};
53 fhicl::Atom<int> port{fhicl::Name{
"port"}, fhicl::Comment{
"Port to send messages to"}, 5140};
54 fhicl::Atom<bool> multicast_enabled{fhicl::Name{
"multicast_enabled"},
55 fhicl::Comment{
"Whether messages should be sent via multicast"},
false};
58 fhicl::Atom<std::string> output_address{
59 fhicl::Name{
"multicast_interface_ip"},
60 fhicl::Comment{
"Use this hostname for multicast output(to assign to the proper NIC)"},
"0.0.0.0"};
62 using Parameters = fhicl::WrappedTable<Config>;
69 ELUDP(Parameters
const& pset);
76 virtual void fillPrefix(std::ostringstream& o,
const ErrorObj& e)
override;
83 virtual void fillUsrMsg(std::ostringstream& o,
const ErrorObj& e)
override;
88 virtual void fillSuffix(std::ostringstream&,
const ErrorObj&)
override {}
95 virtual void routePayload(
const std::ostringstream& o,
const ErrorObj& e)
override;
101 int error_report_backoff_factor_;
105 bool multicast_enabled_;
106 std::string multicast_out_addr_;
109 struct sockaddr_in message_addr_;
112 int consecutive_success_count_;
114 int next_error_report_;
118 std::string hostname_;
119 std::string hostaddr_;
132 : ELdestination(pset().elDestConfig()),
133 error_report_backoff_factor_(pset().error_report()),
134 error_max_(pset().error_max()),
135 host_(pset().host()),
136 port_(pset().port()),
137 multicast_enabled_(pset().multicast_enabled()),
138 multicast_out_addr_(pset().output_address()),
140 consecutive_success_count_(0),
142 next_error_report_(1),
144 pid_(static_cast<long>(getpid())) {
146 char hostname_c[1024];
147 hostname_ = (gethostname(hostname_c, 1023) == 0) ? hostname_c :
"Unkonwn Host";
150 hostent* host =
nullptr;
151 host = gethostbyname(hostname_c);
153 if (host !=
nullptr) {
155 char* ip = inet_ntoa(*(
struct in_addr*)host->h_addr);
159 struct ifaddrs* ifAddrStruct =
nullptr;
160 struct ifaddrs* ifa =
nullptr;
161 void* tmpAddrPtr =
nullptr;
163 if (getifaddrs(&ifAddrStruct)) {
165 hostaddr_ =
"127.0.0.1";
168 for (ifa = ifAddrStruct; ifa !=
nullptr; ifa = ifa->ifa_next) {
169 if (ifa->ifa_addr->sa_family == AF_INET) {
171 tmpAddrPtr = &((
struct sockaddr_in*)ifa->ifa_addr)->sin_addr;
172 char addressBuffer[INET_ADDRSTRLEN];
173 inet_ntop(AF_INET, tmpAddrPtr, addressBuffer, INET_ADDRSTRLEN);
174 hostaddr_ = addressBuffer;
177 else if (ifa->ifa_addr->sa_family == AF_INET6) {
179 tmpAddrPtr = &((
struct sockaddr_in6*)ifa->ifa_addr)->sin6_addr;
180 char addressBuffer[INET6_ADDRSTRLEN];
181 inet_ntop(AF_INET6, tmpAddrPtr, addressBuffer, INET6_ADDRSTRLEN);
182 hostaddr_ = addressBuffer;
186 if (!hostaddr_.empty() && hostaddr_.compare(
"127.0.0.1") && hostaddr_.compare(
"::1"))
break;
189 if (hostaddr_.empty())
190 hostaddr_ =
"127.0.0.1";
197 std::ostringstream pid_ostr;
198 pid_ostr <<
"/proc/" << pid_ <<
"/exe";
199 exe = realpath(pid_ostr.str().c_str(), NULL);
201 size_t end = exe.find(
'\0');
202 size_t start = exe.find_last_of(
'/', end);
204 app_ = exe.substr(start + 1, end - start - 1);
207 std::stringstream ss;
208 ss <<
"//proc//" << pid_ <<
"//cmdline";
209 std::ifstream procfile{ss.str().c_str()};
211 std::string procinfo;
213 if (procfile.is_open()) {
214 procfile >> procinfo;
218 size_t end = procinfo.find(
'\0');
219 size_t start = procinfo.find_last_of(
'/', end);
221 app_ = procinfo.substr(start + 1, end - start - 1);
225 void ELUDP::reconnect_() {
226 message_socket_ = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
227 if (message_socket_ < 0) {
228 TLOG(TLVL_ERROR) <<
"I failed to create the socket for sending Data messages! err=" << strerror(errno);
231 int sts =
ResolveHost(host_.c_str(), port_, message_addr_);
233 TLOG(TLVL_ERROR) <<
"Unable to resolve Data message address, err=" << strerror(errno);
237 if (multicast_out_addr_ ==
"0.0.0.0") {
238 multicast_out_addr_.reserve(HOST_NAME_MAX);
239 sts = gethostname(&multicast_out_addr_[0], HOST_NAME_MAX);
241 TLOG(TLVL_ERROR) <<
"Could not get current hostname, err=" << strerror(errno);
246 if (multicast_out_addr_ !=
"localhost") {
251 TLOG(TLVL_ERROR) <<
"Unable to resolve multicast interface address, err=" << strerror(errno);
255 if (setsockopt(message_socket_, IPPROTO_IP, IP_MULTICAST_IF, &addr,
sizeof(addr)) == -1) {
256 TLOG(TLVL_ERROR) <<
"Cannot set outgoing interface, err=" << strerror(errno);
261 if (setsockopt(message_socket_, SOL_SOCKET, SO_REUSEADDR, &yes,
sizeof(yes)) < 0) {
262 TLOG(TLVL_ERROR) <<
"Unable to enable port reuse on message socket, err=" << strerror(errno);
265 if (setsockopt(message_socket_, IPPROTO_IP, IP_MULTICAST_LOOP, &yes,
sizeof(yes)) < 0) {
266 TLOG(TLVL_ERROR) <<
"Unable to enable multicast loopback on message socket, err=" << strerror(errno);
269 if (setsockopt(message_socket_, SOL_SOCKET, SO_BROADCAST, (
void*)&yes,
sizeof(
int)) == -1) {
270 TLOG(TLVL_ERROR) <<
"Cannot set message socket to broadcast, err=" << strerror(errno);
279 const auto& xid = msg.xid();
282 auto module = xid.module();
284 std::replace(
id.begin(),
id.end(),
'|',
'!');
285 std::replace(app.begin(), app.end(),
'|',
'!');
286 std::replace(module.begin(), module.end(),
'|',
'!');
288 oss << format_.timestamp(msg.timestamp()) <<
"|";
289 oss << std::to_string(++seqNum_) <<
"|";
290 oss << hostname_ <<
"|";
291 oss << hostaddr_ <<
"|";
292 oss << xid.severity().getName() <<
"|";
295 #if MESSAGEFACILITY_HEX_VERSION >= 0x20201 // an indication of s67
297 oss << mf::GetIteration() <<
"|";
300 oss << mf::MessageDrop::instance()->iteration <<
"|";
302 oss << module <<
"|";
303 #if MESSAGEFACILITY_HEX_VERSION >= 0x20201
304 oss << msg.filename() <<
"|" << std::to_string(msg.lineNumber()) <<
"|";
312 std::ostringstream tmposs;
314 for (
auto const& val : msg.items()) {
319 const std::string& usrMsg = !tmposs.str().compare(0, 1,
"\n") ? tmposs.str().erase(0, 1) : tmposs.str();
328 if (message_socket_ == -1) reconnect_();
329 if (error_count_ < error_max_ || error_max_ == 0) {
330 char str[INET_ADDRSTRLEN];
331 inet_ntop(AF_INET, &(message_addr_.sin_addr), str, INET_ADDRSTRLEN);
333 auto string =
"UDPMFMESSAGE" + std::to_string(pid_) +
"|" + oss.str();
334 auto sts = sendto(message_socket_,
string.c_str(),
string.size(), 0, (
struct sockaddr*)&message_addr_,
335 sizeof(message_addr_));
338 consecutive_success_count_ = 0;
340 if (error_count_ == next_error_report_) {
341 TLOG(TLVL_ERROR) <<
"Error sending message " << seqNum_ <<
" to " << host_ <<
", errno=" << errno <<
" ("
342 << strerror(errno) <<
")";
343 next_error_report_ *= error_report_backoff_factor_;
346 ++consecutive_success_count_;
347 if (consecutive_success_count_ >= 5) {
349 next_error_report_ = 1;
362 #ifndef EXTERN_C_FUNC_DECLARE_START
363 #define EXTERN_C_FUNC_DECLARE_START extern "C" {
366 EXTERN_C_FUNC_DECLARE_START
367 auto makePlugin(
const std::string&,
const fhicl::ParameterSet& pset) {
368 return std::make_unique<mfplugins::ELUDP>(pset);
372 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 ...
int GetInterfaceForNetwork(char const *host_in, in_addr &addr)
Convert an IP address to the network address of the interface sharing the subnet mask.
virtual void routePayload(const std::ostringstream &o, const ErrorObj &e) override
Serialize a MessageFacility message to the output.
virtual void fillUsrMsg(std::ostringstream &o, const ErrorObj &e) override
Fill the "User Message" portion of the message.
virtual void fillPrefix(std::ostringstream &o, const ErrorObj &e) override
Fill the "Prefix" portion of the message.
virtual void fillSuffix(std::ostringstream &, const ErrorObj &) override
Fill the "Suffix" portion of the message (Unused)