diff options
| -rw-r--r-- | src/Makefile.am | 2 | ||||
| -rw-r--r-- | src/sender.cc | 17 | ||||
| -rw-r--r-- | src/sender_client.cc | 12 | ||||
| -rw-r--r-- | src/sockutils.cc | 23 | ||||
| -rw-r--r-- | src/sockutils.hh | 10 |
5 files changed, 46 insertions, 18 deletions
diff --git a/src/Makefile.am b/src/Makefile.am index 65c5858..06647f5 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -28,5 +28,5 @@ libdb_la_CPPFLAGS = $(AM_CPPFLAGS) @SQLITE3_CFLAGS@ libdb_la_LIBADD = @SQLITE3_LIBS@ libutil_la_SOURCES = common.hh fsutils.cc fsutils.hh config.cc config.hh \ - strutils.hh strutils.cc + strutils.hh strutils.cc sockutils.hh sockutils.cc diff --git a/src/sender.cc b/src/sender.cc index d43042e..a248cbf 100644 --- a/src/sender.cc +++ b/src/sender.cc @@ -7,7 +7,6 @@ #include <cstdlib> #include <cstring> #include <csignal> -#include <fcntl.h> #include <iostream> #include <netdb.h> #include <time.h> @@ -18,6 +17,7 @@ #include "config.hh" #include "json.hh" +#include "sockutils.hh" /* { @@ -326,7 +326,7 @@ int main() { goto error; } - make_nonblock(sock_); + make_nonblocking(sock_); { int value = 1; @@ -413,12 +413,15 @@ int main() { std::cerr << "Accept failed: " << strerror(errno); goto error; } - make_nonblock(sock); - if (clients.size() == MAX_CLIENTS) { - // Remove oldest - clients.erase(clients.begin()); + if (!make_nonblocking(sock)) { + close(sock); + } else { + if (clients.size() == MAX_CLIENTS) { + // Remove oldest + clients.erase(clients.begin()); + } + clients.emplace_back(sock); } - clients.emplace_back(sock); } } diff --git a/src/sender_client.cc b/src/sender_client.cc index 9b89ed5..69899ec 100644 --- a/src/sender_client.cc +++ b/src/sender_client.cc @@ -12,6 +12,7 @@ #include "config.hh" #include "sender_client.hh" +#include "sockutils.hh" namespace stuff { @@ -183,20 +184,11 @@ private: } } - int flags = fcntl(sock_, F_GETFL, 0); - if (flags < 0) { + if (!make_nonblocking(sock_)) { close(sock_); sock_ = -1; return false; } - if (!(flags & O_NONBLOCK)) { - flags |= O_NONBLOCK; - if (fcntl(sock_, F_SETFL, flags) < 0) { - close(sock_); - sock_ = -1; - return false; - } - } return true; } diff --git a/src/sockutils.cc b/src/sockutils.cc new file mode 100644 index 0000000..53e3ef0 --- /dev/null +++ b/src/sockutils.cc @@ -0,0 +1,23 @@ +#include "common.hh" + +#include <fcntl.h> + +#include "sockutils.hh" + +namespace stuff { + +bool make_nonblocking(int sock) { + int flags = fcntl(sock, F_GETFL, 0); + if (flags < 0) { + return false; + } + if (!(flags & O_NONBLOCK)) { + flags |= O_NONBLOCK; + if (fcntl(sock, F_SETFL, flags) < 0) { + return false; + } + } + return true; +} + +} // namespace stuff diff --git a/src/sockutils.hh b/src/sockutils.hh new file mode 100644 index 0000000..2c47daa --- /dev/null +++ b/src/sockutils.hh @@ -0,0 +1,10 @@ +#ifndef SOCKUTILS_HH +#define SOCKUTILS_HH + +namespace stuff { + +bool make_nonblocking(int sock); + +} // namespace stuff + +#endif /* SOCKUTILS_HH */ |
