|
16 | 16 |
|
17 | 17 | #include <stdio.h> |
18 | 18 |
|
19 | | -#include <winsock2.h> |
20 | 19 | #include <windows.h> |
21 | | - |
22 | | -#include <dns_sd.h> |
| 20 | +#include <windns.h> |
| 21 | +#include <strsafe.h> |
23 | 22 |
|
24 | 23 | #include <serialosc/serialosc.h> |
25 | | -#include <serialosc/zeroconf.h> |
| 24 | + |
| 25 | +static void WINAPI |
| 26 | +register_complete(DWORD status, PVOID context, PDNS_SERVICE_INSTANCE instance) |
| 27 | +{ |
| 28 | + if (status != ERROR_SUCCESS) { |
| 29 | + fprintf(stderr, "sosc_zeroconf_register(): DnsServiceRegister failed (%lu)\n", status); |
| 30 | + } |
| 31 | +} |
| 32 | + |
| 33 | +static void WINAPI |
| 34 | +deregister_complete(DWORD status, PVOID context, PDNS_SERVICE_INSTANCE instance) |
| 35 | +{ |
| 36 | + if (status != ERROR_SUCCESS) { |
| 37 | + fprintf(stderr, "sosc_zeroconf_unregister(): DnsServiceDeRegister failed (%lu)\n", status); |
| 38 | + } |
| 39 | +} |
26 | 40 |
|
27 | 41 | void |
28 | 42 | sosc_zeroconf_init(void) |
29 | 43 | { |
30 | | - FARPROC rfunc, dfunc; |
31 | | - HMODULE ldnssd; |
| 44 | + /* dnsapi.dll is linked at build time, no library to load */ |
| 45 | + return; |
| 46 | +} |
| 47 | + |
| 48 | +void |
| 49 | +sosc_zeroconf_register(sosc_state_t *state, const char *svc_name) |
| 50 | +{ |
| 51 | + PDNS_SERVICE_INSTANCE instance; |
| 52 | + WCHAR w_service_name[256]; |
| 53 | + WCHAR w_hostname[256]; |
| 54 | + DWORD w_hostname_len = ARRAYSIZE(w_hostname); |
| 55 | + int port; |
| 56 | + |
| 57 | + port = lo_server_get_port(state->server); |
| 58 | + |
| 59 | + MultiByteToWideChar(CP_UTF8, 0, svc_name, -1, w_service_name, ARRAYSIZE(w_service_name)); |
| 60 | + |
| 61 | + StringCchCatW(w_service_name, ARRAYSIZE(w_service_name), L"._monome-osc._udp.local"); |
32 | 62 |
|
33 | | - if (!(ldnssd = LoadLibrary("dnssd.dll"))) { |
34 | | - fprintf(stderr, "sosc_zeroconf_init(): couldn't load dnssd.dll\n"); |
| 63 | + if (!GetComputerNameExW(ComputerNameDnsHostname, w_hostname, &w_hostname_len)) { |
| 64 | + fprintf(stderr, "sosc_zeroconf_register(): GetComputerNameExW failed\n"); |
35 | 65 | return; |
36 | 66 | } |
37 | 67 |
|
38 | | - rfunc = GetProcAddress(ldnssd, "DNSServiceRegister"); |
39 | | - dfunc = GetProcAddress(ldnssd, "DNSServiceRefDeallocate"); |
| 68 | + StringCchCatW(w_hostname, ARRAYSIZE(w_hostname), L".local"); |
40 | 69 |
|
41 | | - if (!rfunc || !dfunc) { |
42 | | - fprintf(stderr, "sosc_zeroconf_init(): couldn't resolve symbols in dnssd.dll\n"); |
43 | | - FreeLibrary(ldnssd); |
| 70 | + instance = DnsServiceConstructInstance( |
| 71 | + w_service_name, |
| 72 | + w_hostname, |
| 73 | + NULL, /* ipv4 */ |
| 74 | + NULL, /* ipv6 */ |
| 75 | + (WORD) port, |
| 76 | + 0, /* priority */ |
| 77 | + 0, /* weight */ |
| 78 | + 0, /* properties count */ |
| 79 | + NULL, /* keys */ |
| 80 | + NULL /* values */ |
| 81 | + ); |
| 82 | + |
| 83 | + if (!instance) { |
| 84 | + fprintf(stderr, "sosc_zeroconf_register(): DnsServiceConstructInstance failed\n"); |
| 85 | + return; |
| 86 | + } |
| 87 | + |
| 88 | + DNS_SERVICE_REGISTER_REQUEST request = { |
| 89 | + .Version = DNS_QUERY_REQUEST_VERSION1, |
| 90 | + .pServiceInstance = instance, |
| 91 | + .pRegisterCompletionCallback = register_complete, |
| 92 | + }; |
| 93 | + |
| 94 | + if (DnsServiceRegister(&request, NULL) != DNS_REQUEST_PENDING) { |
| 95 | + fprintf(stderr, "sosc_zeroconf_register(): DnsServiceRegister failed\n"); |
| 96 | + DnsServiceFreeInstance(instance); |
44 | 97 | return; |
45 | 98 | } |
46 | 99 |
|
47 | | - sosc_dnssd_registration_func = (dnssd_registration_func_t) rfunc; |
48 | | - sosc_dnssd_deallocation_func = (dnssd_deallocation_func_t) dfunc; |
| 100 | + state->dnssd_service_ref = instance; |
| 101 | +} |
| 102 | + |
| 103 | +void |
| 104 | +sosc_zeroconf_unregister(sosc_state_t *state) |
| 105 | +{ |
| 106 | + PDNS_SERVICE_INSTANCE instance = state->dnssd_service_ref; |
| 107 | + |
| 108 | + if (!instance) { |
| 109 | + return; |
| 110 | + } |
| 111 | + |
| 112 | + DNS_SERVICE_REGISTER_REQUEST request = { |
| 113 | + .Version = DNS_QUERY_REQUEST_VERSION1, |
| 114 | + .pServiceInstance = instance, |
| 115 | + .pRegisterCompletionCallback = deregister_complete, |
| 116 | + }; |
| 117 | + |
| 118 | + DnsServiceDeRegister(&request, NULL); |
| 119 | + |
| 120 | + DnsServiceFreeInstance(instance); |
| 121 | + state->dnssd_service_ref = NULL; |
49 | 122 | } |
0 commit comments