Skip to content

Commit ae10f1c

Browse files
authored
fix uds sockets (#8)
1 parent 44b653e commit ae10f1c

8 files changed

Lines changed: 430 additions & 102 deletions

File tree

.idea/editor.xml

Lines changed: 335 additions & 96 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

examples/cpp/uds_server/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
*build*
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
cmake_minimum_required(VERSION 3.11)
2+
3+
project(netkit-example LANGUAGES CXX)
4+
set(CMAKE_CXX_STANDARD 23)
5+
set(CMAKE_CXX_STANDARD_REQUIRED ON)
6+
set(CMAKE_CXX_EXTENSIONS OFF)
7+
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
8+
9+
find_package(netkit)
10+
11+
add_executable(
12+
netkit-example
13+
main.cpp
14+
)
15+
target_link_libraries(netkit-example PRIVATE netkit::netkit)

examples/cpp/uds_server/main.cpp

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
#include <iostream>
2+
#include <fstream>
3+
#include <string_view>
4+
#include <netkit/netkit.hpp>
5+
6+
int main() {
7+
netkit::sock::addr addr("/tmp/test.sock");
8+
netkit::sock::sync_sock sock(addr, netkit::sock::type::unix);
9+
10+
sock.bind();
11+
sock.listen();
12+
13+
while (true) {
14+
auto rec = sock.accept();
15+
auto buffer = rec->recv().data;
16+
std::cout << buffer << "\n";
17+
}
18+
19+
return 0;
20+
}

include/netkit/http/request_handler.hpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -423,6 +423,9 @@ namespace netkit::http::server {
423423
}
424424

425425
for (const auto& it : response.headers) {
426+
if (it.name == "Content-Length") {
427+
continue;
428+
}
426429
net_response << it.name << ": " << it.data << "\r\n";
427430
}
428431

src/sock/sock_peer.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,9 @@ netkit::sock::addr netkit::sock::get_peer(fd_t sockfd) {
3939
inet_ntop(AF_INET, &(addr_in->sin_addr), ip_str, sizeof(ip_str));
4040
port = ntohs(addr_in->sin_port);
4141
} else if (addr_storage.ss_family == AF_INET6) {
42-
auto* addr_in6 = reinterpret_cast<sockaddr_in6*>(&addr_storage);
43-
inet_ntop(AF_INET6, &(addr_in6->sin6_addr), ip_str, sizeof(ip_str));
44-
port = ntohs(addr_in6->sin6_port);
42+
auto* addr_in6 = reinterpret_cast<sockaddr_in6*>(&addr_storage);
43+
inet_ntop(AF_INET6, &(addr_in6->sin6_addr), ip_str, sizeof(ip_str));
44+
port = ntohs(addr_in6->sin6_port);
4545
} else {
4646
throw ip_error("unsupported address family");
4747
}

src/sock/sync_sock.cpp

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -180,9 +180,11 @@ void netkit::sock::sync_sock::set_sock_opts(opt opts) const {
180180
netkit::sock::sync_sock::sync_sock(const sock::addr& addr, sock::type t, opt opts) : addr_(addr), type_(t) {
181181
this->sockfd = -1;
182182

183-
if (addr.get_ip().empty() && !addr.is_file_path()) {
184-
throw socket_error("IP address/file path is empty");
185-
}
183+
if (!addr.is_file_path()) {
184+
if (addr.get_ip().empty()) {
185+
throw socket_error("IP address/file path is empty");
186+
}
187+
}
186188

187189
if (t != type::unix) {
188190
this->sockfd = ::socket(addr.is_ipv6() ? AF_INET6 : AF_INET,
@@ -363,6 +365,10 @@ std::unique_ptr<netkit::sock::basic_sync_sock> netkit::sock::sync_sock::accept()
363365
throw socket_error("failed to accept connection: " + std::string(strerror(errno)));
364366
}
365367

368+
if (this->type_ == type::unix) {
369+
return std::make_unique<sync_sock>(client_sockfd, sock::addr(reinterpret_cast<const sockaddr_un*>(&client_addr)->sun_path), this->type_);
370+
}
371+
366372
auto peer = sock::get_peer(client_sockfd);
367373

368374
return std::make_unique<sync_sock>(client_sockfd, peer, this->type_);

tests/test.cpp

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
#include <catch2/catch_test_macros.hpp>
66
#include <set>
77
#include <unordered_set>
8+
#include <thread>
89

910
TEST_CASE("Ensure addr works", "[sock_addr]") {
1011
netkit::sock::addr addr("google.com", 443, netkit::sock::addr_type::hostname);
@@ -128,4 +129,47 @@ TEST_CASE("Ensure general utility functions work", "[utility]") {
128129
REQUIRE(tokens[4] == "five");
129130
std::string joined = netkit::utility::join(tokens, ",");
130131
REQUIRE(joined == to_split);
132+
}
133+
134+
TEST_CASE("Ensure basic sockets work", "[socket]") {
135+
std::thread t([]() {
136+
netkit::sock::addr addr("127.0.0.1", 1337, netkit::sock::addr_type::ipv4);
137+
netkit::sock::sync_sock sock(addr, netkit::sock::type::tcp);
138+
139+
sock.bind();
140+
sock.listen();
141+
142+
while (true) {
143+
auto result = sock.recv(-1);
144+
if (result.status != netkit::sock::recv_status::success) {
145+
return;
146+
}
147+
148+
if (result.status == netkit::sock::recv_status::success) {
149+
std::string data = result.data;
150+
REQUIRE(data == "Hello, World!");
151+
sock.send("Hello, World!");
152+
}
153+
}
154+
155+
sock.unbind();
156+
});
157+
t.detach();
158+
159+
std::this_thread::sleep_for(std::chrono::milliseconds(1000));
160+
161+
netkit::sock::addr addr("127.0.0.1", 1337, netkit::sock::addr_type::ipv4);
162+
netkit::sock::sync_sock sock(addr, netkit::sock::type::tcp);
163+
164+
sock.connect();
165+
sock.send("Hello, World!");
166+
auto result = sock.recv(-1);
167+
if (result.status == netkit::sock::recv_status::success) {
168+
std::string data = result.data;
169+
REQUIRE(data == "Hello, World!");
170+
} else {
171+
REQUIRE(false); // fail the test if we didn't receive a response
172+
}
173+
174+
t.join();
131175
}

0 commit comments

Comments
 (0)