1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169
|
/* logger.cc: Very simple example C++ netconsd module
*
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree.
*/
#include <cerrno>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <unordered_map>
#include <inttypes.h>
#include <fcntl.h>
#include <netdb.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <msgbuf-struct.h>
#include <ncrx-struct.h>
#include <jhash.h>
/*
* The below allows us to index an unordered_map by an IP address.
*/
static bool operator==(const struct in6_addr& lhs, const struct in6_addr& rhs)
{
return std::memcmp(&lhs, &rhs, 16) == 0;
}
namespace std {
template<> struct hash<struct in6_addr>
{
std::size_t operator()(struct in6_addr const& s) const
{
return jhash2((uint32_t*)&s, sizeof(s) / sizeof(uint32_t),
0xbeefdead);
}
};
} /* namespace std */
/*
* Basic struct to hold the hostname and the FD for its logfile.
*/
struct logtarget {
char hostname[INET6_ADDRSTRLEN + 1];
int fd;
/*
* Resolve the hostname, and open() an appropriately named file to
* write the logs into.
*/
logtarget(struct in6_addr *src)
{
int ret;
struct sockaddr_in6 sa = {
.sin6_family = AF_INET6,
.sin6_port = 0,
};
memcpy(&sa.sin6_addr, src, sizeof(*src));
ret = getnameinfo((const struct sockaddr *)&sa, sizeof(sa),
hostname, sizeof(hostname) - 1, NULL, 0, NI_NAMEREQD);
if (ret) {
const char *ptr;
fprintf(stderr, "getnameinfo failed: %s\n", gai_strerror(ret));
ptr = inet_ntop(AF_INET6, src, hostname, INET6_ADDRSTRLEN);
if (ptr == NULL) {
fprintf(stderr, "inet_ntop failed: %s\n", strerror(errno));
snprintf(hostname, 8, "unknown");
}
}
ret = open(hostname, O_TRUNC|O_WRONLY|O_CREAT, 0644);
if (ret == -1) {
fprintf(stderr, "FATAL: open() failed: %m\n");
abort();
}
fd = ret;
}
/*
* Close the file
*/
~logtarget(void)
{
close(fd);
}
};
/*
* This relates the IP address of the remote host to its logtarget struct.
*/
static std::unordered_map<struct in6_addr, struct logtarget> *maps;
/*
* Return the existing logtarget struct if we've seen this host before; else,
* initialize a new logtarget, insert it, and return that.
*/
static struct logtarget& get_target(int thread_nr, struct in6_addr *src)
{
auto itr = maps[thread_nr].find(*src);
if (itr == maps[thread_nr].end())
return maps[thread_nr].emplace(*src, src).first->second;
return itr->second;
}
/*
* Actually write the line to the file
*/
static void write_log(struct logtarget& tgt, struct msg_buf *buf,
struct ncrx_msg *msg)
{
/* legacy non-extended netcons message */
if (!msg) {
dprintf(tgt.fd, "%s\n", buf->buf);
return;
}
/* extended netcons msg with metadata */
if (std::strlen(msg->version) > 1)
dprintf(tgt.fd, "%s ", msg->version);
dprintf(tgt.fd, "%06" PRIu64 " ", msg->seq);
dprintf(tgt.fd, "%014" PRIu64 " ", msg->ts_usec);
dprintf(tgt.fd, "%d ", msg->facility);
dprintf(tgt.fd, "%d ", msg->level);
if (msg->cont_start)
dprintf(tgt.fd, "[CONT START] ");
if (msg->cont)
dprintf(tgt.fd, "[CONT] ");
if (msg->oos)
dprintf(tgt.fd, "[OOS] ");
if (msg->seq_reset)
dprintf(tgt.fd, "[SEQ RESET] ");
dprintf(tgt.fd, "%s\n", msg->text);
}
extern "C" int netconsd_output_init(int nr)
{
maps = new std::unordered_map<struct in6_addr, struct logtarget>[nr];
return 0;
}
extern "C" void netconsd_output_exit(void)
{
delete[] maps;
}
/*
* This is the actual function called by netconsd.
*/
extern "C" void netconsd_output_handler(int t, struct in6_addr *src,
struct msg_buf *buf, struct ncrx_msg *msg)
{
struct logtarget& cur = get_target(t, src);
write_log(cur, buf, msg);
}
|