return res;
}
}
- throw std::runtime_error("Error loading BPF program: (" + std::string(strerror(errno)) + "):\n" + std::string(log_buf));
+ throw std::runtime_error("Error loading BPF program: (" + stringerror() + "):\n" + std::string(log_buf));
}
return res;
}
{
d_v4map.fd = bpf_create_map(BPF_MAP_TYPE_HASH, sizeof(uint32_t), sizeof(uint64_t), (int) maxV4Addresses);
if (d_v4map.fd == -1) {
- throw std::runtime_error("Error creating a BPF v4 map of size " + std::to_string(maxV4Addresses) + ": " + std::string(strerror(errno)));
+ throw std::runtime_error("Error creating a BPF v4 map of size " + std::to_string(maxV4Addresses) + ": " + stringerror());
}
d_v6map.fd = bpf_create_map(BPF_MAP_TYPE_HASH, sizeof(struct KeyV6), sizeof(uint64_t), (int) maxV6Addresses);
if (d_v6map.fd == -1) {
- throw std::runtime_error("Error creating a BPF v6 map of size " + std::to_string(maxV6Addresses) + ": " + std::string(strerror(errno)));
+ throw std::runtime_error("Error creating a BPF v6 map of size " + std::to_string(maxV6Addresses) + ": " + stringerror());
}
d_qnamemap.fd = bpf_create_map(BPF_MAP_TYPE_HASH, sizeof(struct QNameKey), sizeof(struct QNameValue), (int) maxQNames);
if (d_qnamemap.fd == -1) {
- throw std::runtime_error("Error creating a BPF qname map of size " + std::to_string(maxQNames) + ": " + std::string(strerror(errno)));
+ throw std::runtime_error("Error creating a BPF qname map of size " + std::to_string(maxQNames) + ": " + stringerror());
}
d_filtermap.fd = bpf_create_map(BPF_MAP_TYPE_PROG_ARRAY, sizeof(uint32_t), sizeof(uint32_t), 1);
if (d_filtermap.fd == -1) {
- throw std::runtime_error("Error creating a BPF program map of size 1: " + std::string(strerror(errno)));
+ throw std::runtime_error("Error creating a BPF program map of size 1: " + stringerror());
}
struct bpf_insn main_filter[] = {
"GPL",
0);
if (d_mainfilter.fd == -1) {
- throw std::runtime_error("Error loading BPF main filter: " + std::string(strerror(errno)));
+ throw std::runtime_error("Error loading BPF main filter: " + stringerror());
}
struct bpf_insn qname_filter[] = {
"GPL",
0);
if (d_qnamefilter.fd == -1) {
- throw std::runtime_error("Error loading BPF qname filter: " + std::string(strerror(errno)));
+ throw std::runtime_error("Error loading BPF qname filter: " + stringerror());
}
uint32_t key = 0;
int res = bpf_update_elem(d_filtermap.fd, &key, &d_qnamefilter.fd, BPF_ANY);
if (res != 0) {
- throw std::runtime_error("Error updating BPF filters map: " + std::string(strerror(errno)));
+ throw std::runtime_error("Error updating BPF filters map: " + stringrerror());
}
}
int res = setsockopt(sock, SOL_SOCKET, SO_ATTACH_BPF, &d_mainfilter.fd, sizeof(d_mainfilter.fd));
if (res != 0) {
- throw std::runtime_error("Error attaching BPF filter to this socket: " + std::string(strerror(errno)));
+ throw std::runtime_error("Error attaching BPF filter to this socket: " + stringerror());
}
}
int res = setsockopt(sock, SOL_SOCKET, SO_DETACH_BPF, &d_mainfilter.fd, sizeof(d_mainfilter.fd));
if (res != 0) {
- throw std::runtime_error("Error detaching BPF filter from this socket: " + std::string(strerror(errno)));
+ throw std::runtime_error("Error detaching BPF filter from this socket: " + stringerror());
}
}
}
if (res != 0) {
- throw std::runtime_error("Error adding blocked address " + addr.toString() + ": " + std::string(strerror(errno)));
+ throw std::runtime_error("Error adding blocked address " + addr.toString() + ": " + stringerror());
}
}
}
if (res != 0) {
- throw std::runtime_error("Error removing blocked address " + addr.toString() + ": " + std::string(strerror(errno)));
+ throw std::runtime_error("Error removing blocked address " + addr.toString() + ": " + stringerror());
}
}
if (cap_set_proc(caps) != 0) {
cap_free(caps);
- throw std::runtime_error("Unable to drop capabilities: " + std::string(strerror(errno)));
+ throw std::runtime_error("Unable to drop capabilities: " + stringerror());
}
cap_free(caps);
if (chosen_rng == RNG_URANDOM) {
urandom_fd = open(rdev.c_str(), O_RDONLY);
if (urandom_fd == -1)
- throw std::runtime_error("Cannot open " + rdev + ": " + std::string(strerror(errno)));
+ throw std::runtime_error("Cannot open " + rdev + ": " + stringerror());
}
#if defined(HAVE_KISS_RNG)
if (chosen_rng == RNG_KISS) {
unsigned int seed;
urandom_fd = open(rdev.c_str(), O_RDONLY);
if (urandom_fd == -1)
- throw std::runtime_error("Cannot open " + rdev + ": " + std::string(strerror(errno)));
+ throw std::runtime_error("Cannot open " + rdev + ": " + stringerror());
if (read(urandom_fd, &seed, sizeof(seed)) < 0) {
(void)close(urandom_fd);
throw std::runtime_error("Cannot read random device");
uint32_t num = 0;
do {
if (getrandom(&num, sizeof(num), 0) != sizeof(num))
- throw std::runtime_error("getrandom() failed: " + std::string(strerror(errno)));
+ throw std::runtime_error("getrandom() failed: " + stringerror());
}
while(num < min);
else
d_fp = fopen(str.c_str(), "w");
if(!d_fp)
- throw std::runtime_error("Unable to open file '"+str+"' for logging: "+std::string(strerror(errno)));
+ throw std::runtime_error("Unable to open file '"+str+"' for logging: "+stringerror());
if(!buffered)
setbuf(d_fp, 0);
}
int fd = open(fname.c_str(), O_CREAT | O_EXCL | O_WRONLY, 0660);
if (fd < 0) {
- g_outputBuffer = "Error opening dump file for writing: " + string(strerror(errno)) + "\n";
+ g_outputBuffer = "Error opening dump file for writing: " + stringerror() + "\n";
return;
}
return res;
}
else if (res == -1) {
- if (errno == EINTR) {
+ int err = errno;
+ if (err == EINTR) {
continue;
}
- else if (errno == EAGAIN || errno == EWOULDBLOCK || errno == EINPROGRESS || errno == ENOTCONN) {
+ else if (err == EAGAIN || err == EWOULDBLOCK || err == EINPROGRESS || err == ENOTCONN) {
/* EINPROGRESS might happen with non blocking socket,
especially with TCP Fast Open */
return sent;
int err;
if((err = pthread_rwlock_wrlock(d_lock))) {
- errno = err;
- throw PDNSException("error acquiring rwlock wrlock: "+stringerror());
+ throw PDNSException("error acquiring rwlock wrlock: "+stringerror(err));
}
}
~WriteLock()
d_havelock=false;
int err;
if((err = pthread_rwlock_trywrlock(d_lock)) && err!=EBUSY) {
- errno = err;
- throw PDNSException("error acquiring rwlock tryrwlock: "+stringerror());
+ throw PDNSException("error acquiring rwlock tryrwlock: "+stringerror(err));
}
d_havelock=(err==0);
}
int err;
if((err = pthread_rwlock_tryrdlock(d_lock)) && err!=EBUSY) {
- errno = err;
- throw PDNSException("error acquiring rwlock tryrdlock: "+stringerror());
+ throw PDNSException("error acquiring rwlock tryrdlock: "+stringerror(err));
}
d_havelock=(err==0);
}
int err;
if((err = pthread_rwlock_rdlock(d_lock))) {
- errno = err;
- throw PDNSException("error acquiring rwlock readlock: "+stringerror());
+ throw PDNSException("error acquiring rwlock readlock: "+stringerror(err));
}
}
~ReadLock()
return ret;
}
-string stringerror()
+string stringerror(int err)
{
- return strerror(errno);
+ return strerror(err);
}
-string netstringerror()
+string stringerror()
{
- return stringerror();
+ return strerror(errno);
}
void cleanSlashes(string &str)
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
#pragma once
-#include <errno.h>
#include <inttypes.h>
#include <cstring>
#include <cstdio>
const string toLowerCanonic(const string &upper);
bool IpToU32(const string &str, uint32_t *ip);
string U32ToIP(uint32_t);
+string stringerror(int);
string stringerror();
-string netstringerror();
string itoa(int i);
string uitoa(unsigned int i);
string bitFlip(const string &str);
inline void unixDie(const string &why)
{
- throw runtime_error(why+": "+strerror(errno));
+ throw runtime_error(why+": "+stringerror());
}
string makeHexDump(const string& str);
return;
}
- if (setsockopt(fd, SOL_SOCKET, optname, (char*)&size, sizeof(size)) < 0 )
- g_log<<Logger::Error<<"Unable to raise socket buffer size to "<<size<<": "<<strerror(errno)<<endl;
+ if (setsockopt(fd, SOL_SOCKET, optname, (char*)&size, sizeof(size)) < 0) {
+ int err = errno;
+ g_log << Logger::Error << "Unable to raise socket buffer size to " << size << ": " << strerror(err) << endl;
+ }
}
ofstream of(s_pidfname.c_str(), std::ios_base::app);
if(of)
of<< Utility::getpid() <<endl;
- else
- g_log<<Logger::Error<<"Writing pid for "<<Utility::getpid()<<" to "<<s_pidfname<<" failed: "<<strerror(errno)<<endl;
+ else {
+ int err = errno;
+ g_log << Logger::Error << "Writing pid for " << Utility::getpid() << " to " << s_pidfname << " failed: "
+ << strerror(err) << endl;
+ }
}
TCPConnection::TCPConnection(int fd, const ComboAddress& addr) : data(2, 0), d_remote(addr), d_fd(fd)
if(g_fromtosockets.count(dc->d_socket)) {
addCMsgSrcAddr(&msgh, cbuf, &dc->d_local, 0);
}
- if(sendmsg(dc->d_socket, &msgh, 0) < 0 && g_logCommonErrors)
- g_log<<Logger::Warning<<"Sending UDP reply to client "<<dc->getRemote()<<" failed with: "<<strerror(errno)<<endl;
+ if(sendmsg(dc->d_socket, &msgh, 0) < 0 && g_logCommonErrors) {
+ int err = errno;
+ g_log << Logger::Warning << "Sending UDP reply to client " << dc->getRemote() << " failed with: "
+ << strerror(err) << endl;
+ }
if(variableAnswer || sr.wasVariable()) {
g_stats.variableResponses++;
if(wret == 0)
g_log<<Logger::Error<<"EOF writing TCP answer to "<<dc->getRemote()<<endl;
- else if(wret < 0 )
- g_log<<Logger::Error<<"Error writing TCP answer to "<<dc->getRemote()<<": "<< strerror(errno) <<endl;
- else if((unsigned int)wret != 2 + packet.size())
+ else if(wret < 0 ) {
+ int err = errno;
+ g_log << Logger::Error << "Error writing TCP answer to " << dc->getRemote() << ": " << strerror(err) << endl;
+ } else if((unsigned int)wret != 2 + packet.size())
g_log<<Logger::Error<<"Oops, partial answer sent to "<<dc->getRemote()<<" for "<<dc->d_mdp.d_qname<<" (size="<< (2 + packet.size()) <<", sent "<<wret<<")"<<endl;
else
hadError=false;
if(g_fromtosockets.count(fd)) {
addCMsgSrcAddr(&msgh, cbuf, &destaddr, 0);
}
- if(sendmsg(fd, &msgh, 0) < 0 && g_logCommonErrors)
- g_log<<Logger::Warning<<"Sending UDP reply to client "<<source.toStringWithPort()<<(source != fromaddr ? " (via "+fromaddr.toStringWithPort()+")" : "")<<" failed with: "<<strerror(errno)<<endl;
-
+ if(sendmsg(fd, &msgh, 0) < 0 && g_logCommonErrors) {
+ int err = errno;
+ g_log << Logger::Warning << "Sending UDP reply to client " << source.toStringWithPort()
+ << (source != fromaddr ? " (via " + fromaddr.toStringWithPort() + ")" : "") << " failed with: "
+ << strerror(err) << endl;
+ }
if(response.length() >= sizeof(struct dnsheader)) {
struct dnsheader tmpdh;
memcpy(&tmpdh, response.c_str(), sizeof(tmpdh));
exit(1);
}
if(sin.sin6.sin6_family == AF_INET6 && setsockopt(fd, IPPROTO_IPV6, IPV6_V6ONLY, &tmp, sizeof(tmp)) < 0) {
- g_log<<Logger::Error<<"Failed to set IPv6 socket to IPv6 only, continuing anyhow: "<<strerror(errno)<<endl;
+ int err = errno;
+ g_log<<Logger::Error<<"Failed to set IPv6 socket to IPv6 only, continuing anyhow: "<<strerror(err)<<endl;
}
#ifdef TCP_DEFER_ACCEPT
#ifdef TCP_FASTOPEN
int fastOpenQueueSize = ::arg().asNum("tcp-fast-open");
if (setsockopt(fd, IPPROTO_TCP, TCP_FASTOPEN, &fastOpenQueueSize, sizeof fastOpenQueueSize) < 0) {
- g_log<<Logger::Error<<"Failed to enable TCP Fast Open for listening socket: "<<strerror(errno)<<endl;
+ int err = errno;
+ g_log<<Logger::Error<<"Failed to enable TCP Fast Open for listening socket: "<<strerror(err)<<endl;
}
#else
g_log<<Logger::Warning<<"TCP Fast Open configured but not supported for listening socket"<<endl;
int fd=socket(sin.sin4.sin_family, SOCK_DGRAM, 0);
if(fd < 0) {
- throw PDNSException("Making a UDP server socket for resolver: "+netstringerror());
+ throw PDNSException("Making a UDP server socket for resolver: "+stringerror());
}
if (!setSocketTimestamps(fd))
g_log<<Logger::Warning<<"Unable to enable timestamp reporting for socket"<<endl;
g_fromtosockets.insert(fd);
#endif
if(sin.sin6.sin6_family == AF_INET6 && setsockopt(fd, IPPROTO_IPV6, IPV6_V6ONLY, &one, sizeof(one)) < 0) {
- g_log<<Logger::Error<<"Failed to set IPv6 socket to IPv6 only, continuing anyhow: "<<strerror(errno)<<endl;
+ int err = errno;
+ g_log<<Logger::Error<<"Failed to set IPv6 socket to IPv6 only, continuing anyhow: "<<strerror(err)<<endl;
}
}
if( ::arg().mustDo("non-local-bind") )
if (pipeBufferSize > 0) {
if (!setPipeBufferSize(threadInfos.pipes.writeQueriesToThread, pipeBufferSize)) {
- g_log<<Logger::Warning<<"Error resizing the buffer of the distribution pipe for thread "<<n<<" to "<<pipeBufferSize<<": "<<strerror(errno)<<endl;
+ int err = errno;
+ g_log<<Logger::Warning<<"Error resizing the buffer of the distribution pipe for thread "<<n<<" to "<<pipeBufferSize<<": "<<strerror(err)<<endl;
auto existingSize = getPipeBufferSize(threadInfos.pipes.writeQueriesToThread);
if (existingSize > 0) {
g_log<<Logger::Warning<<"The current size of the distribution pipe's buffer for thread "<<n<<" is "<<existingSize<<endl;
}
#endif
if (chroot(::arg()["chroot"].c_str())<0 || chdir("/") < 0) {
- g_log<<Logger::Error<<"Unable to chroot to '"+::arg()["chroot"]+"': "<<strerror (errno)<<", exiting"<<endl;
- exit(1);
+ int err = errno;
+ g_log<<Logger::Error<<"Unable to chroot to '"+::arg()["chroot"]+"': "<<strerror (err)<<", exiting"<<endl;
+ exit(1);
}
else
g_log<<Logger::Info<<"Chrooted to '"<<::arg()["chroot"]<<"'"<<endl;
setCloseOnExec(d_fd);
if(d_fd < 0)
- throw PDNSException("Creating UNIX domain socket: "+string(strerror(errno)));
+ throw PDNSException("Creating UNIX domain socket: "+stringerror());
try {
int tmp=1;
throw PDNSException("Timeout sending message over control channel");
}
else if(ret < 0) {
- throw PDNSException("Error sending message over control channel:" + string(strerror(errno)));
+ throw PDNSException("Error sending message over control channel:" + stringerror());
}
if(remote) {
remoteaddr.sun_path[sizeof(remoteaddr.sun_path)-1] = '\0';
if(::sendto(d_fd, msg.c_str(), msg.length(), 0, (struct sockaddr*) &remoteaddr, sizeof(remoteaddr) ) < 0)
- throw PDNSException("Unable to send message over control channel '"+string(remoteaddr.sun_path)+"': "+string(strerror(errno)));
+ throw PDNSException("Unable to send message over control channel '"+string(remoteaddr.sun_path)+"': "+stringerror());
}
else if(::send(d_fd, msg.c_str(), msg.length(), 0) < 0)
- throw PDNSException("Unable to send message over control channel: "+string(strerror(errno)));
+ throw PDNSException("Unable to send message over control channel: "+stringerror());
}
string RecursorControlChannel::recv(std::string* remote, unsigned int timeout)
throw PDNSException("Timeout waiting for answer from control channel");
if( ret < 0 || (len=::recvfrom(d_fd, buffer, sizeof(buffer), 0, (struct sockaddr*)&remoteaddr, &addrlen)) < 0)
- throw PDNSException("Unable to receive message over control channel: "+string(strerror(errno)));
+ throw PDNSException("Unable to receive message over control channel: "+stringerror());
if(remote)
*remote=remoteaddr.sun_path;
int fd=open(fname.c_str(), O_CREAT | O_EXCL | O_WRONLY, 0660);
if(fd < 0)
- return "Error opening dump file for writing: "+string(strerror(errno))+"\n";
+ return "Error opening dump file for writing: "+stringerror()+"\n";
uint64_t total = 0;
try {
total = broadcastAccFunction<uint64_t>(boost::bind(pleaseDumpNSSpeeds, fd));
int fd=open(fname.c_str(), O_CREAT | O_EXCL | O_WRONLY, 0660);
if(fd < 0)
- return "Error opening dump file for writing: "+string(strerror(errno))+"\n";
+ return "Error opening dump file for writing: "+stringerror()+"\n";
uint64_t total = 0;
try {
total = broadcastAccFunction<uint64_t>(boost::bind(pleaseDump, fd));
int fd=open(fname.c_str(), O_CREAT | O_EXCL | O_WRONLY, 0660);
if(fd < 0)
- return "Error opening dump file for writing: "+string(strerror(errno))+"\n";
+ return "Error opening dump file for writing: "+stringerror()+"\n";
uint64_t total = 0;
try {
total = broadcastAccFunction<uint64_t>(boost::bind(pleaseDumpEDNSMap, fd));
int fd = open(fname.c_str(), O_CREAT | O_EXCL | O_WRONLY, 0660);
if(fd < 0) {
- return "Error opening dump file for writing: "+string(strerror(errno))+"\n";
+ return "Error opening dump file for writing: "+stringerror()+"\n";
}
auto fp = std::unique_ptr<FILE, int(*)(FILE*)>(fdopen(fd, "w"), fclose);
if (!fp) {
close(fd);
- return "Error converting file descriptor: "+string(strerror(errno))+"\n";
+ return "Error converting file descriptor: "+stringerror()+"\n";
}
zone->dump(fp.get());
int fd=open(fname.c_str(), O_CREAT | O_EXCL | O_WRONLY, 0660);
if(fd < 0)
- return "Error opening dump file for writing: "+string(strerror(errno))+"\n";
+ return "Error opening dump file for writing: "+stringerror()+"\n";
uint64_t total = 0;
try {
total = broadcastAccFunction<uint64_t>(boost::bind(pleaseDumpThrottleMap, fd));