Bitcoin Core
32.99.0
P2P Digital Currency
Loading...
Searching...
No Matches
src
ipc
libmultiprocess
test
mp
test
unixlistener.h
Go to the documentation of this file.
1
// Copyright (c) The Bitcoin Core developers
2
// Distributed under the MIT software license, see the accompanying
3
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
4
5
#ifndef MP_TEST_UNIXLISTENER_H
6
#define MP_TEST_UNIXLISTENER_H
7
8
#include <kj/test.h>
9
#include <sys/socket.h>
10
#include <sys/un.h>
11
#include <unistd.h>
12
13
#include <cassert>
14
#include <cstdlib>
15
#include <cstring>
16
#include <filesystem>
17
#include <string>
18
19
namespace
mp
{
20
namespace
test {
21
25
class
UnixListener
26
{
27
public
:
28
UnixListener
()
29
{
30
std::string
dir_template
= (std::filesystem::temp_directory_path() /
"mptest-listener-XXXXXX"
).
string
();
31
char
*
dir
=
mkdtemp
(
dir_template
.data());
32
KJ_REQUIRE
(
dir
!=
nullptr
);
33
m_dir
=
dir
;
34
m_path
=
m_dir
+
"/socket"
;
35
36
m_fd
=
socket
(
AF_UNIX
,
SOCK_STREAM
, 0);
37
KJ_REQUIRE
(
m_fd
>= 0);
38
39
sockaddr_un
addr{};
40
addr.sun_family =
AF_UNIX
;
41
KJ_REQUIRE
(
m_path
.size() <
sizeof
(addr.sun_path));
42
std::strncpy(addr.sun_path,
m_path
.c_str(),
sizeof
(addr.sun_path) - 1);
43
KJ_REQUIRE
(bind(
m_fd
,
reinterpret_cast<
sockaddr
*
>
(&addr),
sizeof
(addr)) == 0);
44
KJ_REQUIRE
(listen(
m_fd
,
SOMAXCONN
) == 0);
45
}
46
47
~UnixListener
()
48
{
49
if
(
m_fd
>= 0) close(
m_fd
);
50
if
(!
m_path
.empty())
unlink
(
m_path
.c_str());
51
if
(!
m_dir
.empty())
rmdir
(
m_dir
.c_str());
52
}
53
54
int
release
()
55
{
56
assert
(
m_fd
>= 0);
57
int
fd =
m_fd
;
58
m_fd
= -1;
59
return
fd;
60
}
61
62
int
MakeConnectedSocket
()
const
63
{
64
int
fd =
socket
(
AF_UNIX
,
SOCK_STREAM
, 0);
65
KJ_REQUIRE
(fd >= 0);
66
67
sockaddr_un
addr{};
68
addr.sun_family =
AF_UNIX
;
69
KJ_REQUIRE
(
m_path
.size() <
sizeof
(addr.sun_path));
70
std::strncpy(addr.sun_path,
m_path
.c_str(),
sizeof
(addr.sun_path) - 1);
71
KJ_REQUIRE
(connect(fd,
reinterpret_cast<
sockaddr
*
>
(&addr),
sizeof
(addr)) == 0);
72
return
fd;
73
}
74
75
private
:
76
int
m_fd
{-1};
77
std::string
m_dir
;
78
std::string
m_path
;
79
};
80
81
}
// namespace test
82
}
// namespace mp
83
84
#endif
// MP_TEST_UNIXLISTENER_H
mp::test::UnixListener
Owns a temporary Unix-domain listening socket.
Definition
unixlistener.h:26
mp::test::UnixListener::MakeConnectedSocket
int MakeConnectedSocket() const
Definition
unixlistener.h:62
mp::test::UnixListener::m_dir
std::string m_dir
Definition
unixlistener.h:77
mp::test::UnixListener::m_path
std::string m_path
Definition
unixlistener.h:78
mp::test::UnixListener::m_fd
int m_fd
Definition
unixlistener.h:76
mp::test::UnixListener::release
int release()
Definition
unixlistener.h:54
mp::test::UnixListener::~UnixListener
~UnixListener()
Definition
unixlistener.h:47
mp::test::UnixListener::UnixListener
UnixListener()
Definition
unixlistener.h:28
mp
Functions to serialize / deserialize common bitcoin types.
Definition
common-types.h:58
Ticks
constexpr auto Ticks(Dur2 d)
Helper to count the seconds of a duration/time_point.
Definition
time.h:82
assert
assert(!tx.IsCoinBase())
Generated on Mon Nov 9 2026 20:00:26 for Bitcoin Core by
1.9.8