Coverage Report

Created: 2025-08-26 06:06

/src/libzmq/src/address.cpp
Line
Count
Source (jump to first uncovered line)
1
/* SPDX-License-Identifier: MPL-2.0 */
2
3
#include "precompiled.hpp"
4
#include "macros.hpp"
5
#include "address.hpp"
6
#include "ctx.hpp"
7
#include "err.hpp"
8
#include "tcp_address.hpp"
9
#include "udp_address.hpp"
10
#include "ipc_address.hpp"
11
#include "tipc_address.hpp"
12
#include "ws_address.hpp"
13
14
#if defined ZMQ_HAVE_VMCI
15
#include "vmci_address.hpp"
16
#endif
17
18
#include <string>
19
#include <sstream>
20
21
zmq::address_t::address_t (const std::string &protocol_,
22
                           const std::string &address_,
23
                           ctx_t *parent_) :
24
0
    protocol (protocol_), address (address_), parent (parent_)
25
0
{
26
0
    resolved.dummy = NULL;
27
0
}
28
29
zmq::address_t::~address_t ()
30
0
{
31
0
    if (protocol == protocol_name::tcp) {
32
0
        LIBZMQ_DELETE (resolved.tcp_addr);
33
0
    } else if (protocol == protocol_name::udp) {
34
0
        LIBZMQ_DELETE (resolved.udp_addr);
35
0
    }
36
0
#ifdef ZMQ_HAVE_WS
37
0
    else if (protocol == protocol_name::ws) {
38
0
        LIBZMQ_DELETE (resolved.ws_addr);
39
0
    }
40
0
#endif
41
42
#ifdef ZMQ_HAVE_WSS
43
    else if (protocol == protocol_name::wss) {
44
        LIBZMQ_DELETE (resolved.ws_addr);
45
    }
46
#endif
47
48
0
#if defined ZMQ_HAVE_IPC
49
0
    else if (protocol == protocol_name::ipc) {
50
0
        LIBZMQ_DELETE (resolved.ipc_addr);
51
0
    }
52
0
#endif
53
0
#if defined ZMQ_HAVE_TIPC
54
0
    else if (protocol == protocol_name::tipc) {
55
0
        LIBZMQ_DELETE (resolved.tipc_addr);
56
0
    }
57
0
#endif
58
#if defined ZMQ_HAVE_VMCI
59
    else if (protocol == protocol_name::vmci) {
60
        LIBZMQ_DELETE (resolved.vmci_addr);
61
    }
62
#endif
63
0
}
64
65
int zmq::address_t::to_string (std::string &addr_) const
66
0
{
67
0
    if (protocol == protocol_name::tcp && resolved.tcp_addr)
68
0
        return resolved.tcp_addr->to_string (addr_);
69
0
    if (protocol == protocol_name::udp && resolved.udp_addr)
70
0
        return resolved.udp_addr->to_string (addr_);
71
0
#ifdef ZMQ_HAVE_WS
72
0
    if (protocol == protocol_name::ws && resolved.ws_addr)
73
0
        return resolved.ws_addr->to_string (addr_);
74
0
#endif
75
#ifdef ZMQ_HAVE_WSS
76
    if (protocol == protocol_name::wss && resolved.ws_addr)
77
        return resolved.ws_addr->to_string (addr_);
78
#endif
79
0
#if defined ZMQ_HAVE_IPC
80
0
    if (protocol == protocol_name::ipc && resolved.ipc_addr)
81
0
        return resolved.ipc_addr->to_string (addr_);
82
0
#endif
83
0
#if defined ZMQ_HAVE_TIPC
84
0
    if (protocol == protocol_name::tipc && resolved.tipc_addr)
85
0
        return resolved.tipc_addr->to_string (addr_);
86
0
#endif
87
#if defined ZMQ_HAVE_VMCI
88
    if (protocol == protocol_name::vmci && resolved.vmci_addr)
89
        return resolved.vmci_addr->to_string (addr_);
90
#endif
91
92
0
    if (!protocol.empty () && !address.empty ()) {
93
0
        std::stringstream s;
94
0
        s << protocol << "://" << address;
95
0
        addr_ = s.str ();
96
0
        return 0;
97
0
    }
98
0
    addr_.clear ();
99
0
    return -1;
100
0
}
101
102
zmq::zmq_socklen_t zmq::get_socket_address (fd_t fd_,
103
                                            socket_end_t socket_end_,
104
                                            sockaddr_storage *ss_)
105
223
{
106
223
    zmq_socklen_t sl = static_cast<zmq_socklen_t> (sizeof (*ss_));
107
108
223
    const int rc =
109
223
      socket_end_ == socket_end_local
110
223
        ? getsockname (fd_, reinterpret_cast<struct sockaddr *> (ss_), &sl)
111
223
        : getpeername (fd_, reinterpret_cast<struct sockaddr *> (ss_), &sl);
112
113
223
    return rc != 0 ? 0 : sl;
114
223
}