0
0
Fork 0
mirror of https://github.com/matrix-construct/construct synced 2024-11-25 08:12:37 +01:00

ircd::net: Split net_dns_netdb from net_dns unit.

This commit is contained in:
Jason Volk 2020-04-13 12:05:15 -07:00
parent f511051e56
commit d4eaad08df
3 changed files with 179 additions and 167 deletions

View file

@ -158,6 +158,7 @@ libircd_la_SOURCES += db.cc
libircd_la_SOURCES += net.cc
libircd_la_SOURCES += net_addrs.cc
libircd_la_SOURCES += net_dns.cc
libircd_la_SOURCES += net_dns_netdb.cc
libircd_la_SOURCES += net_dns_cache.cc
libircd_la_SOURCES += net_dns_resolver.cc
libircd_la_SOURCES += net_listener.cc

View file

@ -8,8 +8,6 @@
// copyright notice and this permission notice is present in all copies. The
// full license for this software is available in the LICENSE file.
#include <RB_INC_NETDB_H
namespace ircd::net::dns
{
template<class T> static rfc1035::record *new_record(mutable_buffer &, const rfc1035::answer &);
@ -547,168 +545,3 @@ ircd::net::dns::new_record(mutable_buffer &buf,
consume(buf, sizeof(type));
return new (pos) type(answer);
}
uint16_t
ircd::net::dns::service_port(const string_view &name,
const string_view &prot)
{
const auto ret
{
service_port(std::nothrow, name, prot)
};
if(unlikely(!ret))
throw error
{
"Port for service %s:%s not found",
name,
prot?: "*"_sv,
};
return ret;
}
#ifdef HAVE_NETDB_H
uint16_t
ircd::net::dns::service_port(std::nothrow_t,
const string_view &name,
const string_view &prot)
try
{
thread_local struct ::servent res, *ent {nullptr};
thread_local char _name[32], _prot[32], buf[2048];
const prof::syscall_usage_warning timer
{
"net::dns::service_port(%s)", name
};
strlcpy(_name, name);
strlcpy(_prot, prot);
syscall
(
::getservbyname_r,
_name,
prot? _prot : nullptr,
&res,
buf,
sizeof(buf),
&ent
);
assert(!ent || ent->s_port != 0);
assert(!ent || name == ent->s_name);
assert(!ent || !prot || prot == ent->s_proto);
if(unlikely(!ent || !ent->s_port))
log::error
{
log, "Uknown service %s/%s; please add port number to /etc/services",
name,
prot?: "*"_sv
};
return ent?
htons(ent->s_port):
0U;
}
catch(const std::exception &e)
{
log::critical
{
log, "Failure when translating service %s:%s to port number :%s",
name,
prot?: "*"_sv,
e.what(),
};
throw;
}
#else
uint16_t
ircd::net::dns::service_port(std::nothrow_t,
const string_view &name,
const string_view &prot)
{
//TODO: XXX
always_assert(false);
return 0;
}
#endif
ircd::string_view
ircd::net::dns::service_name(const mutable_buffer &out,
const uint16_t &port,
const string_view &prot)
{
const auto ret
{
service_name(std::nothrow, out, port, prot)
};
if(unlikely(!ret))
throw error
{
"Name of service for port %u:%s not found",
port,
prot?: "*"_sv,
};
return ret;
}
#ifdef HAVE_NETDB_H
ircd::string_view
ircd::net::dns::service_name(std::nothrow_t,
const mutable_buffer &out,
const uint16_t &port,
const string_view &prot)
try
{
thread_local struct ::servent res, *ent {nullptr};
thread_local char _prot[32], buf[2048];
const prof::syscall_usage_warning timer
{
"net::dns::service_name(%u)", port
};
strlcpy(_prot, prot);
syscall
(
::getservbyport_r,
ntohs(port),
prot? _prot : nullptr,
&res,
buf,
sizeof(buf),
&ent
);
assert(!ent || ent->s_port == ntohs(port));
assert(!ent || !prot || prot == ent->s_proto);
return ent?
strlcpy(out, ent->s_name):
string_view{};
}
catch(const std::exception &e)
{
log::critical
{
log, "Failure when translating port %u:%s to service name :%s",
port,
prot?: "*"_sv,
e.what(),
};
throw;
}
#else
ircd::string_view
ircd::net::dns::service_name(std::nothrow_t,
const mutable_buffer &out,
const uint16_t &port,
const string_view &prot)
{
//TODO: XXX
always_assert(false);
return {};
}
#endif

178
ircd/net_dns_netdb.cc Normal file
View file

@ -0,0 +1,178 @@
// The Construct
//
// Copyright (C) Matrix Construct Developers, Authors & Contributors
// Copyright (C) 2016-2020 Jason Volk <jason@zemos.net>
//
// Permission to use, copy, modify, and/or distribute this software for any
// purpose with or without fee is hereby granted, provided that the above
// copyright notice and this permission notice is present in all copies. The
// full license for this software is available in the LICENSE file.
#include <RB_INC_NETDB_H
uint16_t
ircd::net::dns::service_port(const string_view &name,
const string_view &prot)
{
const auto ret
{
service_port(std::nothrow, name, prot)
};
if(unlikely(!ret))
throw error
{
"Port for service %s:%s not found",
name,
prot?: "*"_sv,
};
return ret;
}
#ifdef HAVE_NETDB_H
uint16_t
ircd::net::dns::service_port(std::nothrow_t,
const string_view &name,
const string_view &prot)
try
{
thread_local struct ::servent res, *ent {nullptr};
thread_local char _name[32], _prot[32], buf[2048];
const prof::syscall_usage_warning timer
{
"net::dns::service_port(%s)", name
};
strlcpy(_name, name);
strlcpy(_prot, prot);
syscall
(
::getservbyname_r,
_name,
prot? _prot : nullptr,
&res,
buf,
sizeof(buf),
&ent
);
assert(!ent || ent->s_port != 0);
assert(!ent || name == ent->s_name);
assert(!ent || !prot || prot == ent->s_proto);
if(unlikely(!ent || !ent->s_port))
log::error
{
log, "Uknown service %s/%s; please add port number to /etc/services",
name,
prot?: "*"_sv
};
return ent?
htons(ent->s_port):
0U;
}
catch(const std::exception &e)
{
log::critical
{
log, "Failure when translating service %s:%s to port number :%s",
name,
prot?: "*"_sv,
e.what(),
};
throw;
}
#else
uint16_t
ircd::net::dns::service_port(std::nothrow_t,
const string_view &name,
const string_view &prot)
{
//TODO: XXX
always_assert(false);
return 0;
}
#endif
ircd::string_view
ircd::net::dns::service_name(const mutable_buffer &out,
const uint16_t &port,
const string_view &prot)
{
const auto ret
{
service_name(std::nothrow, out, port, prot)
};
if(unlikely(!ret))
throw error
{
"Name of service for port %u:%s not found",
port,
prot?: "*"_sv,
};
return ret;
}
#ifdef HAVE_NETDB_H
ircd::string_view
ircd::net::dns::service_name(std::nothrow_t,
const mutable_buffer &out,
const uint16_t &port,
const string_view &prot)
try
{
thread_local struct ::servent res, *ent {nullptr};
thread_local char _prot[32], buf[2048];
const prof::syscall_usage_warning timer
{
"net::dns::service_name(%u)", port
};
strlcpy(_prot, prot);
syscall
(
::getservbyport_r,
ntohs(port),
prot? _prot : nullptr,
&res,
buf,
sizeof(buf),
&ent
);
assert(!ent || ent->s_port == ntohs(port));
assert(!ent || !prot || prot == ent->s_proto);
return ent?
strlcpy(out, ent->s_name):
string_view{};
}
catch(const std::exception &e)
{
log::critical
{
log, "Failure when translating port %u:%s to service name :%s",
port,
prot?: "*"_sv,
e.what(),
};
throw;
}
#else
ircd::string_view
ircd::net::dns::service_name(std::nothrow_t,
const mutable_buffer &out,
const uint16_t &port,
const string_view &prot)
{
//TODO: XXX
always_assert(false);
return {};
}
#endif