forked from techtim/mdnsServicepp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.h
114 lines (97 loc) · 2.73 KB
/
utils.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
#pragma once
#include <string>
#include <functional>
#include "include/mdns/mdns.h"
namespace mdns {
using std::string;
static inline string
ipv4_address_to_string(const sockaddr_in *addr, bool addPort = true)
{
string ip{inet_ntoa(addr->sin_addr)};
if (addr->sin_port != 0 && addPort)
ip.append(":").append(std::to_string(addr->sin_port));
return ip;
}
static inline string
ipv6_address_to_string(const sockaddr_in6 *addr, bool addPort = true)
{
char buf[INET6_ADDRSTRLEN];
inet_ntop(AF_INET6, &(addr->sin6_addr), buf, INET6_ADDRSTRLEN);
string ip{buf};
if (addr->sin6_port != 0 && addPort)
ip.append(":").append(std::to_string(addr->sin6_port));
return ip;
}
static inline string
ip_address_to_string(const sockaddr *addr)
{
if (addr->sa_family == AF_INET6)
return ipv6_address_to_string((const sockaddr_in6 *)addr);
return ipv4_address_to_string((const sockaddr_in *)addr);
}
static inline string
recordtype_to_string(mdns_record_type rtype)
{
switch (rtype) {
case MDNS_RECORDTYPE_PTR:
return "PTR";
case MDNS_RECORDTYPE_SRV:
return "SRV";
case MDNS_RECORDTYPE_A:
return "A";
case MDNS_RECORDTYPE_AAAA:
return "AAAA";
case MDNS_RECORDTYPE_TXT:
return "TXT";
case MDNS_RECORDTYPE_ANY:
return "ANY";
default:
return "";
}
}
static inline string
entrytype_to_string(mdns_entry_type_t entry)
{
switch (entry) {
case MDNS_ENTRYTYPE_ANSWER:
return "answer";
case MDNS_ENTRYTYPE_AUTHORITY:
return "authority";
case MDNS_ENTRYTYPE_ADDITIONAL:
return "additional";
default:
return "question";
}
}
static inline bool
isEqual(mdns_string_t const &lhs, mdns_string_t const &rhs)
{
return lhs.length == rhs.length && strncmp(lhs.str, rhs.str, lhs.length) == 0;
}
template <typename T>
struct CallbackQuery;
template <typename Ret, typename... Params>
struct CallbackQuery<Ret(Params...)> {
template <typename... Args>
static Ret callback(Args... args)
{
return func(args...);
}
static std::function<Ret(Params...)> func;
};
template <typename Ret, typename... Params>
std::function<Ret(Params...)> CallbackQuery<Ret(Params...)>::func;
template <typename T>
struct CallbackService;
template <typename Ret, typename... Params>
struct CallbackService<Ret(Params...)> {
template <typename... Args>
static Ret callback(Args... args)
{
return func(args...);
}
static std::function<Ret(Params...)> func;
};
template <typename Ret, typename... Params>
std::function<Ret(Params...)> CallbackService<Ret(Params...)>::func;
} // namespace mdns