Coverage Report

Created: 2025-07-12 06:05

/src/libzmq/src/ip_resolver.hpp
Line
Count
Source
1
/* SPDX-License-Identifier: MPL-2.0 */
2
3
#ifndef __ZMQ_IP_RESOLVER_HPP_INCLUDED__
4
#define __ZMQ_IP_RESOLVER_HPP_INCLUDED__
5
6
#if !defined ZMQ_HAVE_WINDOWS
7
#include <sys/socket.h>
8
#include <netinet/in.h>
9
#include <netdb.h>
10
#endif
11
12
#include "address.hpp"
13
14
namespace zmq
15
{
16
union ip_addr_t
17
{
18
    sockaddr generic;
19
    sockaddr_in ipv4;
20
    sockaddr_in6 ipv6;
21
22
    int family () const;
23
    bool is_multicast () const;
24
    uint16_t port () const;
25
26
    const struct sockaddr *as_sockaddr () const;
27
    zmq_socklen_t sockaddr_len () const;
28
29
    void set_port (uint16_t);
30
31
    static ip_addr_t any (int family_);
32
};
33
34
class ip_resolver_options_t
35
{
36
  public:
37
    ip_resolver_options_t ();
38
39
    ip_resolver_options_t &bindable (bool bindable_);
40
    ip_resolver_options_t &allow_nic_name (bool allow_);
41
    ip_resolver_options_t &ipv6 (bool ipv6_);
42
    ip_resolver_options_t &expect_port (bool expect_);
43
    ip_resolver_options_t &allow_dns (bool allow_);
44
    ip_resolver_options_t &allow_path (bool allow_);
45
46
    bool bindable ();
47
    bool allow_nic_name ();
48
    bool ipv6 ();
49
    bool expect_port ();
50
    bool allow_dns ();
51
    bool allow_path ();
52
53
  private:
54
    bool _bindable_wanted;
55
    bool _nic_name_allowed;
56
    bool _ipv6_wanted;
57
    bool _port_expected;
58
    bool _dns_allowed;
59
    bool _path_allowed;
60
};
61
62
class ip_resolver_t
63
{
64
  public:
65
    ip_resolver_t (ip_resolver_options_t opts_);
66
1.30k
    virtual ~ip_resolver_t (){};
67
68
    int resolve (ip_addr_t *ip_addr_, const char *name_);
69
70
  protected:
71
    //  Virtual functions that are overridden in tests
72
    virtual int do_getaddrinfo (const char *node_,
73
                                const char *service_,
74
                                const struct addrinfo *hints_,
75
                                struct addrinfo **res_);
76
77
    virtual void do_freeaddrinfo (struct addrinfo *res_);
78
79
    virtual unsigned int do_if_nametoindex (const char *ifname_);
80
81
  private:
82
    ip_resolver_options_t _options;
83
84
    int resolve_nic_name (ip_addr_t *ip_addr_, const char *nic_);
85
    int resolve_getaddrinfo (ip_addr_t *ip_addr_, const char *addr_);
86
87
#if defined ZMQ_HAVE_WINDOWS
88
    int get_interface_name (unsigned long index_, char **dest_) const;
89
    int wchar_to_utf8 (const WCHAR *src_, char **dest_) const;
90
#endif
91
};
92
}
93
94
#endif