summaryrefslogtreecommitdiffstats
path: root/lib/base/socket.hpp
diff options
context:
space:
mode:
authorDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-28 12:34:54 +0000
committerDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-28 12:34:54 +0000
commit0915b3ef56dfac3113cce55a59a5765dc94976be (patch)
treea8fea11d50b4f083e1bf0f90025ece7f0824784a /lib/base/socket.hpp
parentInitial commit. (diff)
downloadicinga2-0915b3ef56dfac3113cce55a59a5765dc94976be.tar.xz
icinga2-0915b3ef56dfac3113cce55a59a5765dc94976be.zip
Adding upstream version 2.13.6.upstream/2.13.6upstream
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'lib/base/socket.hpp')
-rw-r--r--lib/base/socket.hpp66
1 files changed, 66 insertions, 0 deletions
diff --git a/lib/base/socket.hpp b/lib/base/socket.hpp
new file mode 100644
index 0000000..f7acf7f
--- /dev/null
+++ b/lib/base/socket.hpp
@@ -0,0 +1,66 @@
+/* Icinga 2 | (c) 2012 Icinga GmbH | GPLv2+ */
+
+#ifndef SOCKET_H
+#define SOCKET_H
+
+#include "base/i2-base.hpp"
+#include "base/object.hpp"
+#include <mutex>
+
+namespace icinga
+{
+
+/**
+ * Base class for connection-oriented sockets.
+ *
+ * @ingroup base
+ */
+class Socket : public Object
+{
+public:
+ DECLARE_PTR_TYPEDEFS(Socket);
+
+ Socket() = default;
+ Socket(SOCKET fd);
+ ~Socket() override;
+
+ SOCKET GetFD() const;
+
+ void Close();
+
+ std::pair<String, String> GetClientAddressDetails();
+ String GetClientAddress();
+ std::pair<String, String> GetPeerAddressDetails();
+ String GetPeerAddress();
+
+ size_t Read(void *buffer, size_t size);
+ size_t Write(const void *buffer, size_t size);
+
+ void Listen();
+ Socket::Ptr Accept();
+
+ bool Poll(bool read, bool write, struct timeval *timeout = nullptr);
+
+ void MakeNonBlocking();
+
+ static void SocketPair(SOCKET s[2]);
+
+protected:
+ void SetFD(SOCKET fd);
+
+ int GetError() const;
+
+ mutable std::mutex m_SocketMutex;
+
+private:
+ SOCKET m_FD{INVALID_SOCKET}; /**< The socket descriptor. */
+
+ static std::pair<String, String> GetDetailsFromSockaddr(sockaddr *address, socklen_t len);
+ static String GetHumanReadableAddress(const std::pair<String, String>& socketDetails);
+};
+
+class socket_error : virtual public std::exception, virtual public boost::exception { };
+
+}
+
+#endif /* SOCKET_H */