31#ifndef _CRT_SECURE_NO_WARNINGS
32#define _CRT_SECURE_NO_WARNINGS 1
44#define sleep(x) Sleep(x * 1000)
53static char addrbuffer[64];
54static char namebuffer[256];
55static char sendbuffer[1024];
57static struct sockaddr_in service_address_ipv4;
58static struct sockaddr_in6 service_address_ipv6;
60volatile sig_atomic_t running_server = 1;
64 mdns_string_t service;
65 mdns_string_t hostname;
66 mdns_string_t service_instance;
67 mdns_string_t hostname_qualified;
68 struct sockaddr_in address_ipv4;
69 struct sockaddr_in6 address_ipv6;
71 mdns_record_t record_ptr;
72 mdns_record_t record_srv;
73 mdns_record_t record_a;
74 mdns_record_t record_aaaa;
75 mdns_record_t txt_record[2];
83ocpn_service_callback(
int sock,
const struct sockaddr* from,
size_t addrlen, mdns_entry_type_t entry,
84 uint16_t query_id, uint16_t rtype, uint16_t rclass, uint32_t ttl,
const void* data,
85 size_t size,
size_t name_offset,
size_t name_length,
size_t record_offset,
86 size_t record_length,
void* user_data) {
88 if (entry != MDNS_ENTRYTYPE_QUESTION)
91 const char dns_sd[] =
"_services._dns-sd._udp.local.";
94 mdns_string_t fromaddrstr = ip_address_to_string(addrbuffer,
sizeof(addrbuffer), from, addrlen);
96 size_t offset = name_offset;
97 mdns_string_t name = mdns_string_extract(data, size, &offset, namebuffer,
sizeof(namebuffer));
99 const char* record_name = 0;
100 if (rtype == MDNS_RECORDTYPE_PTR)
102 else if (rtype == MDNS_RECORDTYPE_SRV)
104 else if (rtype == MDNS_RECORDTYPE_A)
106 else if (rtype == MDNS_RECORDTYPE_AAAA)
107 record_name =
"AAAA";
108 else if (rtype == MDNS_RECORDTYPE_TXT)
110 else if (rtype == MDNS_RECORDTYPE_ANY)
114 printf(
"Query %s %.*s\n", record_name, MDNS_STRING_FORMAT(name));
116 if ((name.length == (
sizeof(dns_sd) - 1)) &&
117 (strncmp(name.str, dns_sd,
sizeof(dns_sd) - 1) == 0)) {
118 if ((rtype == MDNS_RECORDTYPE_PTR) || (rtype == MDNS_RECORDTYPE_ANY)) {
124 mdns_record_t answer;
126 answer.type = MDNS_RECORDTYPE_PTR;
127 answer.data.ptr.name = service->service;
130 uint16_t unicast = (rclass & MDNS_UNICAST_RESPONSE);
131 printf(
" --> answer %.*s (%s)\n", MDNS_STRING_FORMAT(answer.data.ptr.name),
132 (unicast ?
"unicast" :
"multicast"));
135 mdns_query_answer_unicast(sock, from, addrlen, sendbuffer,
sizeof(sendbuffer),
136 query_id, (mdns_record_type_t)rtype, name.str, name.length, answer,
139 mdns_query_answer_multicast(sock, sendbuffer,
sizeof(sendbuffer), answer, 0, 0, 0,
143 }
else if ((name.length == service->service.length) &&
144 (strncmp(name.str, service->service.str, name.length) == 0)) {
145 if ((rtype == MDNS_RECORDTYPE_PTR) || (rtype == MDNS_RECORDTYPE_ANY)) {
155 mdns_record_t answer = service->record_ptr;
157 mdns_record_t additional[5] = {0};
158 size_t additional_count = 0;
162 additional[additional_count++] = service->record_srv;
165 if (service->address_ipv4.sin_family == AF_INET)
166 additional[additional_count++] = service->record_a;
167 if (service->address_ipv6.sin6_family == AF_INET6)
168 additional[additional_count++] = service->record_aaaa;
176 uint16_t unicast = (rclass & MDNS_UNICAST_RESPONSE);
177 printf(
" --> answer %.*s (%s)\n",
178 MDNS_STRING_FORMAT(service->record_ptr.data.ptr.name),
179 (unicast ?
"unicast" :
"multicast"));
182 mdns_query_answer_unicast(sock, from, addrlen, sendbuffer,
sizeof(sendbuffer),
183 query_id, (mdns_record_type)rtype, name.str, name.length, answer, 0, 0,
184 additional, additional_count);
186 mdns_query_answer_multicast(sock, sendbuffer,
sizeof(sendbuffer), answer, 0, 0,
187 additional, additional_count);
190 }
else if ((name.length == service->service_instance.length) &&
191 (strncmp(name.str, service->service_instance.str, name.length) == 0)) {
192 if ((rtype == MDNS_RECORDTYPE_SRV) || (rtype == MDNS_RECORDTYPE_ANY)) {
201 mdns_record_t answer = service->record_srv;
203 mdns_record_t additional[5] = {0};
204 size_t additional_count = 0;
207 if (service->address_ipv4.sin_family == AF_INET)
208 additional[additional_count++] = service->record_a;
209 if (service->address_ipv6.sin6_family == AF_INET6)
210 additional[additional_count++] = service->record_aaaa;
214 additional[additional_count++] = service->txt_record[0];
215 additional[additional_count++] = service->txt_record[1];
218 uint16_t unicast = (rclass & MDNS_UNICAST_RESPONSE);
219 printf(
" --> answer %.*s port %d (%s)\n",
220 MDNS_STRING_FORMAT(service->record_srv.data.srv.name), service->port,
221 (unicast ?
"unicast" :
"multicast"));
224 mdns_query_answer_unicast(sock, from, addrlen, sendbuffer,
sizeof(sendbuffer),
225 query_id, (mdns_record_type)rtype, name.str, name.length, answer, 0, 0,
226 additional, additional_count);
228 mdns_query_answer_multicast(sock, sendbuffer,
sizeof(sendbuffer), answer, 0, 0,
229 additional, additional_count);
232 }
else if ((name.length == service->hostname_qualified.length) &&
233 (strncmp(name.str, service->hostname_qualified.str, name.length) == 0)) {
234 if (((rtype == MDNS_RECORDTYPE_A) || (rtype == MDNS_RECORDTYPE_ANY)) &&
235 (service->address_ipv4.sin_family == AF_INET)) {
241 mdns_record_t answer = service->record_a;
243 mdns_record_t additional[5] = {0};
244 size_t additional_count = 0;
247 if (service->address_ipv6.sin6_family == AF_INET6)
248 additional[additional_count++] = service->record_aaaa;
252 additional[additional_count++] = service->txt_record[0];
253 additional[additional_count++] = service->txt_record[1];
256 uint16_t unicast = (rclass & MDNS_UNICAST_RESPONSE);
257 mdns_string_t addrstr = ip_address_to_string(
258 addrbuffer,
sizeof(addrbuffer), (
struct sockaddr*)&service->record_a.data.a.addr,
259 sizeof(service->record_a.data.a.addr));
260 printf(
" --> answer %.*s IPv4 %.*s (%s)\n", MDNS_STRING_FORMAT(service->record_a.name),
261 MDNS_STRING_FORMAT(addrstr), (unicast ?
"unicast" :
"multicast"));
264 mdns_query_answer_unicast(sock, from, addrlen, sendbuffer,
sizeof(sendbuffer),
265 query_id, (mdns_record_type)rtype, name.str, name.length, answer, 0, 0,
266 additional, additional_count);
268 mdns_query_answer_multicast(sock, sendbuffer,
sizeof(sendbuffer), answer, 0, 0,
269 additional, additional_count);
271 }
else if (((rtype == MDNS_RECORDTYPE_AAAA) || (rtype == MDNS_RECORDTYPE_ANY)) &&
272 (service->address_ipv6.sin6_family == AF_INET6)) {
278 mdns_record_t answer = service->record_aaaa;
280 mdns_record_t additional[5] = {0};
281 size_t additional_count = 0;
284 if (service->address_ipv4.sin_family == AF_INET)
285 additional[additional_count++] = service->record_a;
289 additional[additional_count++] = service->txt_record[0];
290 additional[additional_count++] = service->txt_record[1];
293 uint16_t unicast = (rclass & MDNS_UNICAST_RESPONSE);
294 mdns_string_t addrstr =
295 ip_address_to_string(addrbuffer,
sizeof(addrbuffer),
296 (
struct sockaddr*)&service->record_aaaa.data.aaaa.addr,
297 sizeof(service->record_aaaa.data.aaaa.addr));
298 printf(
" --> answer %.*s IPv6 %.*s (%s)\n",
299 MDNS_STRING_FORMAT(service->record_aaaa.name), MDNS_STRING_FORMAT(addrstr),
300 (unicast ?
"unicast" :
"multicast"));
303 mdns_query_answer_unicast(sock, from, addrlen, sendbuffer,
sizeof(sendbuffer),
304 query_id, (mdns_record_type)rtype, name.str, name.length, answer, 0, 0,
305 additional, additional_count);
307 mdns_query_answer_multicast(sock, sendbuffer,
sizeof(sendbuffer), answer, 0, 0,
308 additional, additional_count);
316void service_mdns(
const char* hostname,
const char* service_name,
int service_port) {
318 int num_sockets = open_service_sockets(sockets,
sizeof(sockets) /
sizeof(sockets[0]));
319 if (num_sockets <= 0) {
320 printf(
"Failed to open any client sockets\n");
323 printf(
"Opened %d socket%s for mDNS service\n", num_sockets, num_sockets ?
"s" :
"");
325 size_t service_name_length = strlen(service_name);
326 if (!service_name_length) {
327 printf(
"Invalid service name\n");
331 char* service_name_buffer = (
char *)malloc(service_name_length + 2);
332 memcpy(service_name_buffer, service_name, service_name_length);
333 if (service_name_buffer[service_name_length - 1] !=
'.')
334 service_name_buffer[service_name_length++] =
'.';
335 service_name_buffer[service_name_length] = 0;
336 service_name = service_name_buffer;
338 printf(
"Service mDNS: %s:%d\n", service_name, service_port);
339 printf(
"Hostname: %s\n", hostname);
341 size_t capacity = 2048;
342 void* buffer = malloc(capacity);
344 mdns_string_t service_string;
345 service_string.str = service_name;
346 service_string.length = strlen(service_name);
347 mdns_string_t hostname_string;
348 hostname_string.str = hostname;
349 hostname_string.length = strlen(hostname);
352 char service_instance_buffer[256] = {0};
353 snprintf(service_instance_buffer,
sizeof(service_instance_buffer) - 1,
"%.*s.%.*s",
354 MDNS_STRING_FORMAT(hostname_string), MDNS_STRING_FORMAT(service_string));
356 mdns_string_t service_instance_string;
357 service_instance_string.str = service_instance_buffer;
358 service_instance_string.length = strlen(service_instance_buffer);
361 char qualified_hostname_buffer[256] = {0};
362 snprintf(qualified_hostname_buffer,
sizeof(qualified_hostname_buffer) - 1,
"%.*s.local.",
363 MDNS_STRING_FORMAT(hostname_string));
364 mdns_string_t hostname_qualified_string;
365 hostname_qualified_string.str = qualified_hostname_buffer;
366 hostname_qualified_string.length = strlen(qualified_hostname_buffer);
369 service.service = service_string;
370 service.hostname = hostname_string;
371 service.service_instance = service_instance_string;
372 service.hostname_qualified = hostname_qualified_string;
373 service.address_ipv4 = service_address_ipv4;
374 service.address_ipv6 = service_address_ipv6;
375 service.port = service_port;
381 service.record_ptr.name = service.service;
382 service.record_ptr.type = MDNS_RECORDTYPE_PTR;
383 service.record_ptr.rclass = 0;
384 service.record_ptr.ttl = 0;
385 service.record_ptr.data.ptr.name = service.service_instance;
389 service.record_srv.name = service.service_instance;
390 service.record_srv.type = MDNS_RECORDTYPE_SRV;
391 service.record_srv.data.srv.name = service.hostname_qualified;
392 service.record_srv.data.srv.port = service.port;
393 service.record_srv.data.srv.priority = 0;
394 service.record_srv.data.srv.weight = 0;
395 service.record_srv.rclass = 0;
396 service.record_srv.ttl = 0;
401 service.record_a.name = service.hostname_qualified;
402 service.record_a.type = MDNS_RECORDTYPE_A;
403 service.record_a.data.a.addr = service.address_ipv4;
404 service.record_a.rclass = 0;
405 service.record_a.ttl = 0;
408 service.record_aaaa.name = service.hostname_qualified;
409 service.record_aaaa.type = MDNS_RECORDTYPE_AAAA;
410 service.record_aaaa.data.aaaa.addr = service.address_ipv6;
411 service.record_aaaa.rclass = 0;
412 service.record_aaaa.ttl = 0;
418 service.txt_record[0].name = service.service_instance;
419 service.txt_record[0].type = MDNS_RECORDTYPE_TXT;
420 service.txt_record[0].data.txt.key = {MDNS_STRING_CONST(
"test")};
421 service.txt_record[0].data.txt.value = {MDNS_STRING_CONST(
"1")};
422 service.txt_record[0].rclass = 0;
423 service.txt_record[0].ttl = 0;
425 service.txt_record[1].name = service.service_instance;
426 service.txt_record[1].type = MDNS_RECORDTYPE_TXT;
427 service.txt_record[1].data.txt.key = {MDNS_STRING_CONST(
"other")};
428 service.txt_record[1].data.txt.value = {MDNS_STRING_CONST(
"value")};
429 service.txt_record[1].rclass = 0;
430 service.txt_record[1].ttl = 0;
436 printf(
"Sending announce\n");
437 mdns_record_t additional[5] = {0};
438 size_t additional_count = 0;
439 additional[additional_count++] = service.record_srv;
440 if (service.address_ipv4.sin_family == AF_INET)
441 additional[additional_count++] = service.record_a;
442 if (service.address_ipv6.sin6_family == AF_INET6)
443 additional[additional_count++] = service.record_aaaa;
447 for (
int isock = 0; isock < num_sockets; ++isock)
448 mdns_announce_multicast(sockets[isock], buffer, capacity, service.record_ptr, 0, 0,
449 additional, additional_count);
453 while (running_server) {
457 for (
int isock = 0; isock < num_sockets; ++isock) {
458 if (sockets[isock] >= nfds)
459 nfds = sockets[isock] + 1;
460 FD_SET(sockets[isock], &readfs);
463 struct timeval timeout;
465 timeout.tv_usec = 100000;
467 if (select(nfds, &readfs, 0, 0, &timeout) >= 0) {
468 for (
int isock = 0; isock < num_sockets; ++isock) {
469 if (FD_ISSET(sockets[isock], &readfs)) {
470 mdns_socket_listen(sockets[isock], buffer, capacity,
471 ocpn_service_callback,
474 FD_SET(sockets[isock], &readfs);
483 printf(
"Sending goodbye\n");
484 mdns_record_t additional[5] = {0};
485 size_t additional_count = 0;
486 additional[additional_count++] = service.record_srv;
487 if (service.address_ipv4.sin_family == AF_INET)
488 additional[additional_count++] = service.record_a;
489 if (service.address_ipv6.sin6_family == AF_INET6)
490 additional[additional_count++] = service.record_aaaa;
491 additional[additional_count++] = service.txt_record[0];
492 additional[additional_count++] = service.txt_record[1];
494 for (
int isock = 0; isock < num_sockets; ++isock)
495 mdns_goodbye_multicast(sockets[isock], buffer, capacity, service.record_ptr, 0, 0,
496 additional, additional_count);
500 free(service_name_buffer);
502 for (
int isock = 0; isock < num_sockets; ++isock)
503 mdns_socket_close(sockets[isock]);
504 printf(
"Closed socket%s\n", num_sockets ?
"s" :
"");
512int StartMDNSService(std::string hostname,
513 std::string service_name,
517 service = service_name;
519 std::thread mdns_service_thread(service_mdns, host.c_str(), service.c_str(), service_port);
520 mdns_service_thread.detach();
526bool StopMDNSService(){