Toolbox
snapshot
The Reactive C++ Toolbox
Loading...
Searching...
No Matches
toolbox
net
Socket.cpp
Go to the documentation of this file.
1
// The Reactive C++ Toolbox.
2
// Copyright (C) 2013-2019 Swirly Cloud Limited
3
// Copyright (C) 2021 Reactive Markets Limited
4
//
5
// Licensed under the Apache License, Version 2.0 (the "License");
6
// you may not use this file except in compliance with the License.
7
// You may obtain a copy of the License at
8
//
9
// http://www.apache.org/licenses/LICENSE-2.0
10
//
11
// Unless required by applicable law or agreed to in writing, software
12
// distributed under the License is distributed on an "AS IS" BASIS,
13
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
// See the License for the specific language governing permissions and
15
// limitations under the License.
16
17
#include "
Socket.hpp
"
18
19
#include <
toolbox/util/String.hpp
>
20
21
#include <sys/un.h>
22
23
namespace
toolbox
{
24
inline
namespace
net {
25
using namespace
std
;
26
namespace
{
27
void
free_unix_addrinfo
(
addrinfo
*
ai
)
28
{
29
if
(
ai
) {
30
delete
reinterpret_cast<
sockaddr_un
*
>
(
ai
->ai_addr);
31
delete
ai
;
32
}
33
}
34
}
// namespace
35
AddrInfoPtr
get_unix_addrinfo
(string_view path,
int
type)
36
{
37
auto
sun
=
make_unique<sockaddr_un>
();
38
sun
->sun_family =
AF_UNIX
;
39
const
socklen_t
path_len
=
pstrcpy<'\0'>
(
sun
->sun_path, path);
40
// Check that path has not exceeded max length.
41
if
(
path_len
==
sizeof
(
sun
->sun_path)) {
42
throw
invalid_argument{
"invalid unix domain address"
};
43
}
44
// '|' signifies an abstract unix socket
45
bool
abstract
= (*
sun
->sun_path ==
'|'
);
46
if
(
abstract
) {
47
sun
->sun_path[0] =
'\0'
;
48
}
49
50
AddrInfoPtr
ai
{
new
addrinfo
{},
free_unix_addrinfo
};
51
ai
->ai_family =
AF_UNIX
;
52
ai
->ai_socktype = type;
53
// Size includes null terminator, unless it's abstract. See unix(7).
54
ai
->ai_addrlen =
offsetof
(
sockaddr_un
,
sun_path
) +
path_len
+ (!
abstract
);
55
ai
->ai_addr =
reinterpret_cast<
sockaddr
*
>
(
sun
.release());
56
return
ai
;
57
}
58
}
// namespace net
59
}
// namespace toolbox
Socket.hpp
String.hpp
std
STL namespace.
toolbox::net::AddrInfoPtr
std::unique_ptr< addrinfo, void(*)(addrinfo *)> AddrInfoPtr
Definition
Socket.hpp:30
toolbox::net::get_unix_addrinfo
AddrInfoPtr get_unix_addrinfo(string_view path, int type)
Create an addrinfo structure for a Unix domain address.
Definition
Socket.cpp:35
toolbox::util::bind
constexpr auto bind() noexcept
Definition
Slot.hpp:92
toolbox
Definition
Benchmark.cpp:26
Generated by
1.9.8