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/>.
22 /* Uses DNS service discovery (libdns_sd) to
23 * register the SMB service. SMB service is registered
24 * on ".local" domain via Multicast DNS & any
25 * other unicast DNS domains available.
27 * Users use the smbclient -B (Browse) option to
28 * browse for advertised SMB services.
31 #define DNS_REG_RETRY_INTERVAL (5*60) /* in seconds */
33 #ifdef WITH_DNSSD_SUPPORT
37 struct dns_reg_state {
38 struct tevent_context *event_ctx;
40 DNSServiceRef srv_ref;
41 struct tevent_timer *te;
43 struct tevent_fd *fde;
46 static int dns_reg_state_destructor(struct dns_reg_state *dns_state)
48 if (dns_state->srv_ref != NULL) {
49 /* Close connection to the mDNS daemon */
50 DNSServiceRefDeallocate(dns_state->srv_ref);
51 dns_state->srv_ref = NULL;
54 /* Clear event handler */
55 TALLOC_FREE(dns_state->te);
56 TALLOC_FREE(dns_state->fde);
62 static void dns_register_smbd_retry(struct tevent_context *ctx,
63 struct tevent_timer *te,
66 static void dns_register_smbd_fde_handler(struct tevent_context *ev,
67 struct tevent_fd *fde,
71 static bool dns_register_smbd_schedule(struct dns_reg_state *dns_state,
74 dns_reg_state_destructor(dns_state);
76 dns_state->te = tevent_add_timer(dns_state->event_ctx,
79 dns_register_smbd_retry,
88 static void dns_register_smbd_retry(struct tevent_context *ctx,
89 struct tevent_timer *te,
93 struct dns_reg_state *dns_state = talloc_get_type_abort(private_data,
94 struct dns_reg_state);
95 DNSServiceErrorType err;
97 dns_reg_state_destructor(dns_state);
99 DEBUG(6, ("registering _smb._tcp service on port %d\n",
102 /* Register service with DNS. Connects with the mDNS
103 * daemon running on the local system to perform DNS
104 * service registration.
106 err = DNSServiceRegister(&dns_state->srv_ref, 0 /* flags */,
107 kDNSServiceInterfaceIndexAny,
108 NULL /* service name */,
109 "_smb._tcp" /* service type */,
111 "" /* SRV target host name */,
112 htons(dns_state->port),
113 0 /* TXT record len */,
114 NULL /* TXT record data */,
115 NULL /* callback func */,
116 NULL /* callback context */);
118 if (err != kDNSServiceErr_NoError) {
119 /* Failed to register service. Schedule a re-try attempt.
121 DEBUG(3, ("unable to register with mDNS (err %d)\n", err));
125 dns_state->fd = DNSServiceRefSockFD(dns_state->srv_ref);
126 if (dns_state->fd == -1) {
130 dns_state->fde = tevent_add_fd(dns_state->event_ctx,
134 dns_register_smbd_fde_handler,
136 if (!dns_state->fde) {
142 dns_register_smbd_schedule(dns_state,
143 timeval_current_ofs(DNS_REG_RETRY_INTERVAL, 0));
146 /* Processes reply from mDNS daemon. Returns true if a reply was received */
147 static void dns_register_smbd_fde_handler(struct tevent_context *ev,
148 struct tevent_fd *fde,
152 struct dns_reg_state *dns_state = talloc_get_type_abort(private_data,
153 struct dns_reg_state);
154 DNSServiceErrorType err;
156 err = DNSServiceProcessResult(dns_state->srv_ref);
157 if (err != kDNSServiceErr_NoError) {
158 DEBUG(3, ("failed to process mDNS result (err %d), re-trying\n",
163 talloc_free(dns_state);
167 dns_register_smbd_schedule(dns_state,
168 timeval_current_ofs(DNS_REG_RETRY_INTERVAL, 0));
171 bool smbd_setup_mdns_registration(struct tevent_context *ev,
175 struct dns_reg_state *dns_state;
177 dns_state = talloc_zero(mem_ctx, struct dns_reg_state);
178 if (dns_state == NULL) {
181 dns_state->event_ctx = ev;
182 dns_state->port = port;
185 talloc_set_destructor(dns_state, dns_reg_state_destructor);
187 return dns_register_smbd_schedule(dns_state, timeval_zero());
190 #else /* WITH_DNSSD_SUPPORT */
192 bool smbd_setup_mdns_registration(struct tevent_context *ev,
199 #endif /* WITH_DNSSD_SUPPORT */