2 Unix SMB/CIFS implementation.
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/>.
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
38 struct dns_reg_state {
39 struct tevent_context *event_ctx;
41 DNSServiceRef srv_ref;
42 struct tevent_timer *te;
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);
63 static void dns_register_smbd_retry(struct tevent_context *ctx,
64 struct tevent_timer *te,
67 static void dns_register_smbd_fde_handler(struct tevent_context *ev,
68 struct tevent_fd *fde,
72 static bool dns_register_smbd_schedule(struct dns_reg_state *dns_state,
75 dns_reg_state_destructor(dns_state);
77 dns_state->te = tevent_add_timer(dns_state->event_ctx,
80 dns_register_smbd_retry,
89 static void dns_register_smbd_retry(struct tevent_context *ctx,
90 struct tevent_timer *te,
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",
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 */,
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));
126 dns_state->fd = DNSServiceRefSockFD(dns_state->srv_ref);
127 if (dns_state->fd == -1) {
131 dns_state->fde = tevent_add_fd(dns_state->event_ctx,
135 dns_register_smbd_fde_handler,
137 if (!dns_state->fde) {
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,
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",
164 talloc_free(dns_state);
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,
176 struct dns_reg_state *dns_state;
178 dns_state = talloc_zero(mem_ctx, struct dns_reg_state);
179 if (dns_state == NULL) {
182 dns_state->event_ctx = ev;
183 dns_state->port = port;
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,
200 #endif /* WITH_DNSSD_SUPPORT */