replace: Add nss_wrapper_enabled().
[Samba.git] / source3 / smbd / dnsregister.c
blobdf189001a092ac07b46439fec4c4508c7843c427
1 /*
2 Unix SMB/CIFS implementation.
3 DNS-SD registration
4 Copyright (C) Rishi Srivatsavai 2007
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 3 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program. If not, see <http://www.gnu.org/licenses/>.
20 #include <includes.h>
21 #include "smbd/smbd.h"
23 /* Uses DNS service discovery (libdns_sd) to
24 * register the SMB service. SMB service is registered
25 * on ".local" domain via Multicast DNS & any
26 * other unicast DNS domains available.
28 * Users use the smbclient -B (Browse) option to
29 * browse for advertised SMB services.
32 #define DNS_REG_RETRY_INTERVAL (5*60) /* in seconds */
34 #ifdef WITH_DNSSD_SUPPORT
36 #include <dns_sd.h>
38 struct dns_reg_state {
39 struct tevent_context *event_ctx;
40 uint16_t port;
41 DNSServiceRef srv_ref;
42 struct tevent_timer *te;
43 int fd;
44 struct tevent_fd *fde;
47 static int dns_reg_state_destructor(struct dns_reg_state *dns_state)
49 if (dns_state->srv_ref != NULL) {
50 /* Close connection to the mDNS daemon */
51 DNSServiceRefDeallocate(dns_state->srv_ref);
52 dns_state->srv_ref = NULL;
55 /* Clear event handler */
56 TALLOC_FREE(dns_state->te);
57 TALLOC_FREE(dns_state->fde);
58 dns_state->fd = -1;
60 return 0;
63 static void dns_register_smbd_retry(struct tevent_context *ctx,
64 struct tevent_timer *te,
65 struct timeval now,
66 void *private_data);
67 static void dns_register_smbd_fde_handler(struct tevent_context *ev,
68 struct tevent_fd *fde,
69 uint16_t flags,
70 void *private_data);
72 static bool dns_register_smbd_schedule(struct dns_reg_state *dns_state,
73 struct timeval tval)
75 dns_reg_state_destructor(dns_state);
77 dns_state->te = tevent_add_timer(dns_state->event_ctx,
78 dns_state,
79 tval,
80 dns_register_smbd_retry,
81 dns_state);
82 if (!dns_state->te) {
83 return false;
86 return true;
89 static void dns_register_smbd_retry(struct tevent_context *ctx,
90 struct tevent_timer *te,
91 struct timeval now,
92 void *private_data)
94 struct dns_reg_state *dns_state = talloc_get_type_abort(private_data,
95 struct dns_reg_state);
96 DNSServiceErrorType err;
98 dns_reg_state_destructor(dns_state);
100 DEBUG(6, ("registering _smb._tcp service on port %d\n",
101 dns_state->port));
103 /* Register service with DNS. Connects with the mDNS
104 * daemon running on the local system to perform DNS
105 * service registration.
107 err = DNSServiceRegister(&dns_state->srv_ref, 0 /* flags */,
108 kDNSServiceInterfaceIndexAny,
109 NULL /* service name */,
110 "_smb._tcp" /* service type */,
111 NULL /* domain */,
112 "" /* SRV target host name */,
113 htons(dns_state->port),
114 0 /* TXT record len */,
115 NULL /* TXT record data */,
116 NULL /* callback func */,
117 NULL /* callback context */);
119 if (err != kDNSServiceErr_NoError) {
120 /* Failed to register service. Schedule a re-try attempt.
122 DEBUG(3, ("unable to register with mDNS (err %d)\n", err));
123 goto retry;
126 dns_state->fd = DNSServiceRefSockFD(dns_state->srv_ref);
127 if (dns_state->fd == -1) {
128 goto retry;
131 dns_state->fde = tevent_add_fd(dns_state->event_ctx,
132 dns_state,
133 dns_state->fd,
134 TEVENT_FD_READ,
135 dns_register_smbd_fde_handler,
136 dns_state);
137 if (!dns_state->fde) {
138 goto retry;
141 return;
142 retry:
143 dns_register_smbd_schedule(dns_state,
144 timeval_current_ofs(DNS_REG_RETRY_INTERVAL, 0));
147 /* Processes reply from mDNS daemon. Returns true if a reply was received */
148 static void dns_register_smbd_fde_handler(struct tevent_context *ev,
149 struct tevent_fd *fde,
150 uint16_t flags,
151 void *private_data)
153 struct dns_reg_state *dns_state = talloc_get_type_abort(private_data,
154 struct dns_reg_state);
155 DNSServiceErrorType err;
157 err = DNSServiceProcessResult(dns_state->srv_ref);
158 if (err != kDNSServiceErr_NoError) {
159 DEBUG(3, ("failed to process mDNS result (err %d), re-trying\n",
160 err));
161 goto retry;
164 talloc_free(dns_state);
165 return;
167 retry:
168 dns_register_smbd_schedule(dns_state,
169 timeval_current_ofs(DNS_REG_RETRY_INTERVAL, 0));
172 bool smbd_setup_mdns_registration(struct tevent_context *ev,
173 TALLOC_CTX *mem_ctx,
174 uint16_t port)
176 struct dns_reg_state *dns_state;
178 dns_state = talloc_zero(mem_ctx, struct dns_reg_state);
179 if (dns_state == NULL) {
180 return false;
182 dns_state->event_ctx = ev;
183 dns_state->port = port;
184 dns_state->fd = -1;
186 talloc_set_destructor(dns_state, dns_reg_state_destructor);
188 return dns_register_smbd_schedule(dns_state, timeval_zero());
191 #else /* WITH_DNSSD_SUPPORT */
193 bool smbd_setup_mdns_registration(struct tevent_context *ev,
194 TALLOC_CTX *mem_ctx,
195 uint16_t port)
197 return true;
200 #endif /* WITH_DNSSD_SUPPORT */