18
18
#include " util/Macros.hxx"
19
19
#include " util/SpanCast.hxx"
20
20
21
+ #ifdef HAVE_AVAHI
22
+ #include " net/HostParser.hxx"
23
+ #include " util/StringSplit.hxx"
24
+ #endif
25
+
21
26
#include < algorithm>
22
27
23
28
#include < fcntl.h>
@@ -113,6 +118,9 @@ ResultWriter::ResultWriter(bool _raw, bool _gzip,
113
118
GeoIP *_geoip_v4, GeoIP *_geoip_v6,
114
119
#endif
115
120
bool _track_visitors,
121
+ #ifdef HAVE_AVAHI
122
+ bool _resolve_forwarded_to,
123
+ #endif
116
124
const Net::Log::OneLineOptions _one_line_options,
117
125
bool _jsonl,
118
126
bool _single_site,
@@ -126,6 +134,9 @@ ResultWriter::ResultWriter(bool _raw, bool _gzip,
126
134
#endif
127
135
per_site (_per_site, _per_site_filename, _per_site_nested),
128
136
one_line_options(_one_line_options),
137
+ #ifdef HAVE_AVAHI
138
+ resolve_forwarded_to (_resolve_forwarded_to),
139
+ #endif
129
140
jsonl (_jsonl),
130
141
raw(_raw), gzip(_gzip),
131
142
track_visitors(_track_visitors)
@@ -174,8 +185,21 @@ ResultWriter::LookupGeoIP(const char *address) const noexcept
174
185
#endif // HAVE_LIBGEOIP
175
186
176
187
void
177
- ResultWriter::Append (const Net::Log::Datagram &d)
188
+ ResultWriter::Append (Net::Log::Datagram & &d)
178
189
{
190
+ #ifdef HAVE_AVAHI
191
+ if (resolve_forwarded_to && d.forwarded_to != nullptr ) {
192
+ /* extract the IP address, stripping the port and
193
+ square brackets (IPv6) */
194
+ if (const auto e = ExtractHost (d.forwarded_to ); !e.host .empty ())
195
+ /* remove the scope name (IPv6) because Avahi
196
+ doesn't understand it and query the Avahi
197
+ address resolver */
198
+ if (const char *name = address_resolver.ResolveAddress (std::string{Split (e.host , ' %' ).first }))
199
+ d.forwarded_to = name;
200
+ }
201
+ #endif // HAVE_AVAHI
202
+
179
203
if (buffer_fill > sizeof (buffer) - 16384 )
180
204
FlushBuffer ();
181
205
229
253
ResultWriter::Write (std::span<const std::byte> payload)
230
254
{
231
255
if (per_site.IsDefined ()) {
232
- const auto d = Net::Log::ParseDatagram (payload);
256
+ auto d = Net::Log::ParseDatagram (payload);
233
257
if (d.site == nullptr )
234
258
// TODO: where to log datagrams without a site?
235
259
return ;
@@ -285,7 +309,7 @@ ResultWriter::Write(std::span<const std::byte> payload)
285
309
/* skip this site */
286
310
return ;
287
311
288
- Append (d );
312
+ Append (std::move (d) );
289
313
} else if (socket.IsDefined ()) {
290
314
/* if fd2 is a packet socket, send raw
291
315
datagrams to it */
0 commit comments