From 9a99da53c5654356af29ee7edb32eeef6285320f Mon Sep 17 00:00:00 2001
From: Benny Baumann <BenBE@geshi.org>
Date: Mon, 15 May 2023 16:37:55 +0200
Subject: [PATCH] add: unix socket client implementation

---
 src/net/client_factory.cpp | 15 +++++++++++----
 src/net/client_factory.hpp |  1 +
 2 files changed, 12 insertions(+), 4 deletions(-)

diff --git a/src/net/client_factory.cpp b/src/net/client_factory.cpp
index 739e2ef..472404f 100644
--- a/src/net/client_factory.cpp
+++ b/src/net/client_factory.cpp
@@ -71,13 +71,21 @@ namespace rmrf::net {
         return client_factory_construct_stream_client(socket_identifier, cb);
     }
 
+    [[nodiscard]] std::unique_ptr<connection_client> client_factory_construct_unix_client(const socketaddr& socket_identifier, connection_client::incomming_data_cb cb) {
+        if (socket_identifier.family() != AF_UNIX) {
+            throw netio_exception("Expected UNIX domain socket address as target");
+        }
+
+        return client_factory_construct_stream_client(socket_identifier, cb);
+    }
+
     [[nodiscard]] std::unique_ptr<connection_client> client_factory_construct_stream_client(const socketaddr& socket_identifier, connection_client::incomming_data_cb cb) {
         auto_fd socket_candidate{socket(socket_identifier.family(), SOCK_STREAM, 0)};
 
         if (socket_candidate.valid()) {
             if (connect(socket_candidate.get(), socket_identifier.ptr(), socket_identifier.size()) == 0) {
                 make_socket_nonblocking(socket_candidate);
-                const auto own_address = get_own_address_after_connect(socket_candidate);
+                const auto own_address = socket_identifier.family() != AF_UNIX ? get_own_address_after_connect(socket_candidate) : socketaddr{};
 
                 // TODO create client object using socket_candidate, own_address and socket_identifier as remote address
                 auto c = std::make_unique<tcp_client>(nullptr, std::move(socket_candidate), own_address, socket_identifier);
@@ -94,11 +102,10 @@ namespace rmrf::net {
         switch(type) {
             case socket_t::TCP:
                 return client_factory_construct_tcp_client(address);
+            case socket_t::UNIX:
+                return client_factory_construct_unix_client(address);
             case socket_t::UDP:
                 return client_factory_construct_udp_client(address);
-            case socket_t::UNIX:
-                // TODO implement
-                return nullptr;
             default:
                 return nullptr;
         }
diff --git a/src/net/client_factory.hpp b/src/net/client_factory.hpp
index 1322073..bd86c9a 100644
--- a/src/net/client_factory.hpp
+++ b/src/net/client_factory.hpp
@@ -11,6 +11,7 @@ namespace rmrf::net {
     
     [[nodiscard]] std::unique_ptr<udp_client> client_factory_construct_udp_client(const socketaddr& socket_identifier, connection_client::incomming_data_cb cb = nullptr);
     [[nodiscard]] std::unique_ptr<connection_client> client_factory_construct_tcp_client(const socketaddr& socket_identifier, connection_client::incomming_data_cb cb = nullptr);
+    [[nodiscard]] std::unique_ptr<connection_client> client_factory_construct_unix_client(const socketaddr& socket_identifier, connection_client::incomming_data_cb cb = nullptr);
     [[nodiscard]] std::unique_ptr<connection_client> client_factory_construct_stream_client(const socketaddr& socket_identifier, connection_client::incomming_data_cb cb = nullptr);
     
     /**
-- 
GitLab