s3:smbd: split out smbd_do_qfsinfo() from call_trans2qfsinfo()
[Samba/fernandojvsilva.git] / source3 / smbd / dnsregister.c
blob2fd95f9fb7abbb6f12cd1cbdbe796d4b42c72f56
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>
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
35 #include <dns_sd.h>
37 struct dns_reg_state {
38 struct tevent_context *event_ctx;
39 uint16_t port;
40 DNSServiceRef srv_ref;
41 struct tevent_timer *te;
42 int fd;
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);
57 dns_state->fd = -1;
59 return 0;
62 static void dns_register_smbd_retry(struct tevent_context *ctx,
63 struct tevent_timer *te,
64 struct timeval now,
65 void *private_data);
66 static void dns_register_smbd_fde_handler(struct tevent_context *ev,
67 struct tevent_fd *fde,
68 uint16_t flags,
69 void *private_data);
71 static bool dns_register_smbd_schedule(struct dns_reg_state *dns_state,
72 struct timeval tval)
74 dns_reg_state_destructor(dns_state);
76 dns_state->te = tevent_add_timer(dns_state->event_ctx,
77 dns_state,
78 tval,
79 dns_register_smbd_retry,
80 dns_state);
81 if (!dns_state->te) {
82 return false;
85 return true;
88 static void dns_register_smbd_retry(struct tevent_context *ctx,
89 struct tevent_timer *te,
90 struct timeval now,
91 void *private_data)
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",
100 dns_state->port));
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 */,
110 NULL /* domain */,
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));
122 goto retry;
125 dns_state->fd = DNSServiceRefSockFD(dns_state->srv_ref);
126 if (dns_state->fd == -1) {
127 goto retry;
130 dns_state->fde = tevent_add_fd(dns_state->event_ctx,
131 dns_state,
132 dns_state->fd,
133 TEVENT_FD_READ,
134 dns_register_smbd_fde_handler,
135 dns_state);
136 if (!dns_state->fde) {
137 goto retry;
140 return;
141 retry:
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,
149 uint16_t flags,
150 void *private_data)
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",
159 err));
160 goto retry;
163 talloc_free(dns_state);
164 return;
166 retry:
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,
172 TALLOC_CTX *mem_ctx,
173 uint16_t port)
175 struct dns_reg_state *dns_state;
177 dns_state = talloc_zero(mem_ctx, struct dns_reg_state);
178 if (dns_state == NULL) {
179 return false;
181 dns_state->event_ctx = ev;
182 dns_state->port = port;
183 dns_state->fd = -1;
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,
193 TALLOC_CTX *mem_ctx,
194 uint16_t port)
196 return true;
199 #endif /* WITH_DNSSD_SUPPORT */