Commit 51416485 authored by Benoit Eudier's avatar Benoit Eudier

Throw runtime exception if cannot bind socket

If we exhaust the list of addrinfo candidates, it means something
went wrong when trying to bind the socket. In that case, we are
sending a runtime exception with the current errno value.
parent c2b1503c
...@@ -18,6 +18,7 @@ ...@@ -18,6 +18,7 @@
#include <signal.h> #include <signal.h>
#include <sys/timerfd.h> #include <sys/timerfd.h>
#include <sys/sendfile.h> #include <sys/sendfile.h>
#include <cerrno>
#include <pistache/listener.h> #include <pistache/listener.h>
#include <pistache/peer.h> #include <pistache/peer.h>
...@@ -160,7 +161,8 @@ Listener::bind(const Address& address) { ...@@ -160,7 +161,8 @@ Listener::bind(const Address& address) {
int fd = -1; int fd = -1;
for (struct addrinfo *addr = addrs; addr; addr = addr->ai_next) { addrinfo *addr;
for (addr = addrs; addr; addr = addr->ai_next) {
fd = ::socket(addr->ai_family, addr->ai_socktype, addr->ai_protocol); fd = ::socket(addr->ai_family, addr->ai_socktype, addr->ai_protocol);
if (fd < 0) continue; if (fd < 0) continue;
...@@ -174,6 +176,12 @@ Listener::bind(const Address& address) { ...@@ -174,6 +176,12 @@ Listener::bind(const Address& address) {
TRY(::listen(fd, backlog_)); TRY(::listen(fd, backlog_));
break; break;
} }
// At this point, it is still possible that we couldn't bind any socket. If it is the case, the previous
// loop would have exited naturally and addr will be null.
if (addr == nullptr) {
throw std::runtime_error(strerror(errno));
}
make_non_blocking(fd); make_non_blocking(fd);
poller.addFd(fd, Polling::NotifyOn::Read, Polling::Tag(fd)); poller.addFd(fd, Polling::NotifyOn::Read, Polling::Tag(fd));
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment