SNode.C
Loading...
Searching...
No Matches
SocketAddress.cpp
Go to the documentation of this file.
1/*
2 * SNode.C - a slim toolkit for network communication
3 * Copyright (C) Volker Christian <me@vchrist.at>
4 * 2020, 2021, 2022, 2023, 2024, 2025
5 *
6 * This program is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU Lesser General Public License as published
8 * by the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 */
19
20#include "net/un/SocketAddress.h"
21
22#include "core/socket/State.h"
23#include "net/SocketAddress.hpp"
24
25#ifndef DOXYGEN_SHOULD_SKIP_THIS
26
27#include <cerrno>
28#include <cstddef>
29#include <cstring>
30
31#endif /* DOXYGEN_SHOULD_SKIP_THIS */
32
33namespace net::un {
34
35 SocketAddress::SocketAddress()
36 : Super(AF_UNIX, offsetof(sockaddr_un, sun_path)) {
37 }
38
39 SocketAddress::SocketAddress(const std::string& sunPath)
40 : SocketAddress() {
41 setSunPath(sunPath);
42 }
43
44 SocketAddress::SocketAddress(const SockAddr& sockAddr, SockLen sockAddrLen)
47 }
48
49 void SocketAddress::init() {
50 if (sunPath.length() < sizeof(sockAddr.sun_path)) {
51 const std::size_t len = sunPath.length();
52 std::memcpy(sockAddr.sun_path, sunPath.data(), len);
53 sockAddr.sun_path[len] = 0;
54 sockAddrLen = static_cast<SockLen>(offsetof(sockaddr_un, sun_path) + len + 1);
55 } else {
56 throw core::socket::SocketAddress::BadSocketAddress(
57 core::socket::STATE_FATAL,
58 "Unix-Domain error sun-path to long: Lenght is = " + std::to_string(sunPath.length()) +
59 ", should be: " + std::to_string(sizeof(sockAddr.sun_path) - 1),
60 EINVAL);
61 }
62 }
63
64 SocketAddress& SocketAddress::setSunPath(const std::string& sunPath) {
65 this->sunPath = sunPath;
66
67 return *this;
68 }
69
70 std::string SocketAddress::getSunPath() const {
71 return sunPath;
72 }
73
74 std::string SocketAddress::toString([[maybe_unused]] bool expanded) const {
75 return sockAddr.sun_path[0] != '\0' ? sockAddr.sun_path : std::string("@").append(sockAddr.sun_path + 1);
76 }
77
78} // namespace net::un
79
80template class net::SocketAddress<sockaddr_un>;
SocketAddress & setSunPath(const std::string &sunPath)
SocketAddress(const std::string &sunPath)
std::string toString(bool expanded=true) const override
std::string getSunPath() const
SocketAddress(const SockAddr &sockAddr, SockLen sockAddrLen)