This repository has been archived by the owner on Oct 25, 2021. It is now read-only.
forked from mjansson/mdns
-
Notifications
You must be signed in to change notification settings - Fork 1
/
mdns-example.c
183 lines (163 loc) · 5.73 KB
/
mdns-example.c
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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
#include "libmdns.h"
#include <stdio.h>
#include <errno.h>
#ifndef __WIN32__
#include <netdb.h>
#endif
static char addrbuffer[64];
static char namebuffer[256];
static mdns_record_txt_t txtbuffer[128];
static mdns_string_t
ipv4_address_to_string(char* buffer, size_t capacity, const struct sockaddr_in* addr) {
char host[NI_MAXHOST] = {0};
char service[NI_MAXSERV] = {0};
int ret = getnameinfo((const struct sockaddr*)addr, sizeof(struct sockaddr_in),
host, NI_MAXHOST, service, NI_MAXSERV,
NI_NUMERICSERV | NI_NUMERICHOST);
int len = 0;
if (ret == 0) {
if (addr->sin_port != 0)
len = snprintf(buffer, capacity, "%s:%s", host, service);
else
len = snprintf(buffer, capacity, "%s", host);
}
if (len >= capacity)
len = (int)capacity - 1;
mdns_string_t str = {buffer, len};
return str;
}
static mdns_string_t
ipv6_address_to_string(char* buffer, size_t capacity, const struct sockaddr_in6* addr) {
char host[NI_MAXHOST] = {0};
char service[NI_MAXSERV] = {0};
int ret = getnameinfo((const struct sockaddr*)addr, sizeof(struct sockaddr_in6),
host, NI_MAXHOST, service, NI_MAXSERV,
NI_NUMERICSERV | NI_NUMERICHOST);
int len = 0;
if (ret == 0) {
if (addr->sin6_port != 0)
len = snprintf(buffer, capacity, "[%s]:%s", host, service);
else
len = snprintf(buffer, capacity, "%s", host);
}
if (len >= capacity)
len = (int)capacity - 1;
mdns_string_t str = {buffer, len};
return str;
}
static mdns_string_t
ip_address_to_string(char* buffer, size_t capacity, const struct sockaddr* addr) {
if (addr->sa_family == AF_INET6)
return ipv6_address_to_string(buffer, capacity, (const struct sockaddr_in6*)addr);
return ipv4_address_to_string(buffer, capacity, (const struct sockaddr_in*)addr);
}
static int
callback(const struct sockaddr* from,
mdns_entry_type_t entry, uint16_t type,
uint16_t rclass, uint32_t ttl,
const void* data, size_t size, size_t offset, size_t length) {
mdns_string_t fromaddrstr = ip_address_to_string(addrbuffer, sizeof(addrbuffer), from);
const char* entrytype = (entry == MDNS_ENTRYTYPE_ANSWER) ? "answer" :
((entry == MDNS_ENTRYTYPE_AUTHORITY) ? "authority" : "additional");
if (type == MDNS_RECORDTYPE_PTR) {
mdns_string_t namestr = mdns_record_parse_ptr(data, size, offset, length,
namebuffer, sizeof(namebuffer));
printf("%.*s : %s PTR %.*s type %u rclass 0x%x ttl %u length %d\n",
MDNS_STRING_FORMAT(fromaddrstr), entrytype,
MDNS_STRING_FORMAT(namestr), type, rclass, ttl, (int)length);
}
else if (type == MDNS_RECORDTYPE_SRV) {
mdns_record_srv_t srv = mdns_record_parse_srv(data, size, offset, length,
namebuffer, sizeof(namebuffer));
printf("%.*s : %s SRV %.*s priority %d weight %d port %d\n",
MDNS_STRING_FORMAT(fromaddrstr), entrytype,
MDNS_STRING_FORMAT(srv.name), srv.priority, srv.weight, srv.port);
}
else if (type == MDNS_RECORDTYPE_A) {
struct sockaddr_in addr;
mdns_record_parse_a(data, size, offset, length, &addr);
mdns_string_t addrstr = ipv4_address_to_string(namebuffer, sizeof(namebuffer), &addr);
printf("%.*s : %s A %.*s\n",
MDNS_STRING_FORMAT(fromaddrstr), entrytype,
MDNS_STRING_FORMAT(addrstr));
}
else if (type == MDNS_RECORDTYPE_AAAA) {
struct sockaddr_in6 addr;
mdns_record_parse_aaaa(data, size, offset, length, &addr);
mdns_string_t addrstr = ipv6_address_to_string(namebuffer, sizeof(namebuffer), &addr);
printf("%.*s : %s AAAA %.*s\n",
MDNS_STRING_FORMAT(fromaddrstr), entrytype,
MDNS_STRING_FORMAT(addrstr));
}
else if (type == MDNS_RECORDTYPE_TXT) {
size_t parsed = mdns_record_parse_txt(data, size, offset, length,
txtbuffer, sizeof(txtbuffer) / sizeof(mdns_record_txt_t));
for (size_t itxt = 0; itxt < parsed; ++itxt) {
if (txtbuffer[itxt].value.length) {
printf("%.*s : %s TXT %.*s = %.*s\n",
MDNS_STRING_FORMAT(fromaddrstr), entrytype,
MDNS_STRING_FORMAT(txtbuffer[itxt].key),
MDNS_STRING_FORMAT(txtbuffer[itxt].value));
}
else {
printf("%.*s : %s TXT %.*s\n",
MDNS_STRING_FORMAT(fromaddrstr), entrytype,
MDNS_STRING_FORMAT(txtbuffer[itxt].key));
}
}
}
else {
printf("%.*s : %s type %u rclass 0x%x ttl %u length %d\n",
MDNS_STRING_FORMAT(fromaddrstr), entrytype,
type, rclass, ttl, (int)length);
}
return 0;
}
int
main() {
#ifdef __WIN32__
WORD versionWanted = MAKEWORD(1, 1);
WSADATA wsaData;
WSAStartup(versionWanted, &wsaData);
#endif
size_t capacity = 2048;
void* buffer = 0;
size_t records;
int sock = mdns_socket_open_ipv4();
if (sock < 0) {
printf("Failed to open socket: %s\n", strerror(errno));
return -1;
}
printf("Opened IPv4 socket for mDNS/DNS-SD\n");
printf("Sending DNS-SD discovery\n");
if (mdns_discovery_send(sock)) {
printf("Failed to send DNS-DS discovery: %s\n", strerror(errno));
goto quit;
}
printf("Reading DNS-SD replies\n");
buffer = malloc(capacity);
for (int i = 0; i < 10; ++i) {
records = mdns_discovery_recv(sock, buffer, capacity, callback);
sleep(1);
}
printf("Sending mDNS query\n");
if (mdns_query_send(sock, MDNS_RECORDTYPE_PTR,
MDNS_STRING_CONST("_ssh._tcp.local."),
buffer, capacity)) {
printf("Failed to send mDNS query: %s\n", strerror(errno));
goto quit;
}
printf("Reading mDNS replies\n");
for (int i = 0; i < 10; ++i) {
records = mdns_query_recv(sock, buffer, capacity, callback);
sleep(1);
}
quit:
free(buffer);
mdns_socket_close(sock);
printf("Closed socket\n");
#ifdef __WIN32__
WSACleanup();
#endif
return 0;
}