SNode.C
Loading...
Searching...
No Matches
SocketAddress.hpp
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/SocketAddress.h" // IWYU pragma: export
21
22#ifndef DOXYGEN_SHOULD_SKIP_THIS
23
24#include <cstring>
25
26#endif /* DOXYGEN_SHOULD_SKIP_THIS */
27
28namespace net {
29
30 template <typename SockAddr>
31 SocketAddress<SockAddr>::SocketAddress(sa_family_t af, SockLen sockAddrLen)
32 : sockAddrLen(sockAddrLen) {
33 std::memset(&sockAddr, 0, sizeof(sockAddr));
34 reinterpret_cast<sockaddr*>(&sockAddr)->sa_family = af;
35 }
36
37 template <typename SockAddr>
38 SocketAddress<SockAddr>::SocketAddress(const SocketAddress& socketAddress)
39 : sockAddr(socketAddress.sockAddr)
40 , sockAddrLen(socketAddress.sockAddrLen) {
41 }
43 template <typename SockAddr>
44 SocketAddress<SockAddr>::SocketAddress(const SockAddr& sockAddr, SockLen sockAddrLen)
45 : sockAddr(sockAddr)
46 , sockAddrLen(sockAddrLen) {
47 }
49 template <typename SockAddr>
50 SocketAddress<SockAddr>& SocketAddress<SockAddr>::operator=(const SocketAddress& socketAddress) {
51 if (this != &socketAddress) {
52 this->sockAddr = socketAddress.sockAddr;
53 this->sockAddrLen = socketAddress.sockAddrLen;
54 }
55
56 return *this;
57 }
58
59 template <typename SockAddr>
60 const sockaddr& SocketAddress<SockAddr>::getSockAddr() {
61 return reinterpret_cast<const sockaddr&>(sockAddr);
62 }
63
64 template <typename SockAddr>
65 const typename SocketAddress<SockAddr>::SockLen& SocketAddress<SockAddr>::getSockAddrLen() const {
66 return sockAddrLen;
67 }
68
69 template <typename SockAddr>
70 sa_family_t SocketAddress<SockAddr>::getAddressFamily() const {
71 return reinterpret_cast<const sockaddr*>(&sockAddr)->sa_family;
72 }
73
74} // namespace net
SocketAddress & setPort(uint16_t port)
SocketAddress(const std::string &ipOrHostname, uint16_t port)
SocketAddress & setHost(const std::string &ipOrHostname)
SocketAddress(uint16_t port)
SocketAddress(const std::string &ipOrHostname)
std::string getHost() const
std::string getCanonName() const
SocketAddress(const SockAddr &sockAddr, SockLen sockAddrLen, bool numeric=true)
std::string toString(bool expanded=true) const override
void init(const Hints &hints={.aiFlags=0,.aiSockType=0,.aiProtocol=0})
uint16_t getPort() const