summaryrefslogtreecommitdiff
path: root/source3/smbd/dnsregister.c
blob: 2319097ca564bd6371b4541c050b871c0070c7ec (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
/*
   Unix SMB/CIFS implementation.
   DNS-SD registration
   Copyright (C) Rishi Srivatsavai 2007

   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 3 of the License, or
   (at your option) any later version.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

#include <includes.h>

/* Uses DNS service discovery (libdns_sd) to
 * register the SMB service. SMB service is registered
 * on ".local" domain via Multicast DNS & any
 * other unicast DNS domains available.
 *
 * Users use the smbclient -B (Browse) option to
 * browse for advertised SMB services.
 */

#define DNS_REG_RETRY_INTERVAL (5*60)  /* in seconds */

#ifdef WITH_DNSSD_SUPPORT

#include <dns_sd.h>

struct dns_reg_state {
	DNSServiceRef srv_ref;
	struct timed_event *retry_handler;
};

void dns_register_close(struct dns_reg_state **dns_state_ptr)
{
	struct dns_reg_state *dns_state = *dns_state_ptr;

	if (dns_state == NULL) {
		return;
	}

	if (dns_state->srv_ref != NULL) {
		/* Close connection to the mDNS daemon */
		DNSServiceRefDeallocate(dns_state->srv_ref);
		dns_state->srv_ref = NULL;
	}

	/* Clear event handler */
	if (dns_state->retry_handler != NULL) {
		TALLOC_FREE(dns_state->retry_handler);
		dns_state->retry_handler = NULL;
	}

	talloc_free(dns_state);
	*dns_state_ptr = NULL;
}

static void dns_register_smbd_retry(struct event_context *ctx,
                                   struct timed_event *te,
                                   const struct timeval *now,
                                   void *private_data)
{
	struct dns_reg_state *dns_state = (struct dns_reg_state *)private_data;

	/* Clear previous registration state to force new
	 * registration attempt. Clears event handler.
	 */
	dns_register_close(&dns_state);
}

static void schedule_dns_register_smbd_retry(struct dns_reg_state *dns_state,
		struct timeval *timeout)
{
	struct timed_event * event;

	dns_state->srv_ref = NULL;
	event= event_add_timed(smbd_event_context(),
			NULL,
			timeval_current_ofs(DNS_REG_RETRY_INTERVAL, 0),
			"DNS registration handler",
			dns_register_smbd_retry,
			dns_state);

	dns_state->retry_handler = event;
	get_timed_events_timeout(smbd_event_context(), timeout);
}

/* Kick off a mDNS request to register the "_smb._tcp" on the specified port.
 * We really ought to register on all the ports we are listening on. This will
 * have to be an exercise for some-one who knows the DNS registration API a bit
 * better.
 */
void dns_register_smbd(struct dns_reg_state ** dns_state_ptr,
		unsigned port,
		int *maxfd,
		fd_set *listen_set,
		struct timeval *timeout)
{
	int mdnsd_conn_fd;
	DNSServiceErrorType err;
	struct dns_reg_state *dns_state = *dns_state_ptr;

	if (dns_state == NULL) {
		*dns_state_ptr = dns_state = talloc(NULL, struct dns_reg_state);
		if (dns_state == NULL) {
			return;
		}
	}

	/* Quit if a re-try attempt has been scheduled.  */
	if (dns_state->retry_handler != NULL) {
		return;
	}

	/* If a registration is active add conn
	 * fd to select listen_set and return
	 */
	if (dns_state->srv_ref != NULL) {
		mdnsd_conn_fd = DNSServiceRefSockFD(dns_state->srv_ref);
		FD_SET(mdnsd_conn_fd, listen_set);
		return;
	}

	DEBUG(6, ("registering _smb._tcp service on port %d\n", port));

	/* Register service with DNS. Connects with the mDNS
	 * daemon running on the local system to perform DNS
	 * service registration.
	 */
	err = DNSServiceRegister(&dns_state->srv_ref, 0 /* flags */,
			kDNSServiceInterfaceIndexAny,
			NULL /* service name */,
			"_smb._tcp" /* service type */,
			NULL /* domain */,
			"" /* SRV target host name */,
			htons(port),
			0 /* TXT record len */,
			NULL /* TXT record data */,
			NULL /* callback func */,
			NULL /* callback context */);

	if (err != kDNSServiceErr_NoError) {
		/* Failed to register service. Schedule a re-try attempt.
		 */
		DEBUG(3, ("unable to register with mDNS (err %d)\n", err));
		schedule_dns_register_smbd_retry(dns_state, timeout);
		return;
	}

	mdnsd_conn_fd = DNSServiceRefSockFD(dns_state->srv_ref);
	FD_SET(mdnsd_conn_fd, listen_set);
	*maxfd = MAX(*maxfd, mdnsd_conn_fd);
	*timeout = timeval_zero();

}

/* Processes reply from mDNS daemon. Returns true if a reply was received */
bool dns_register_smbd_reply(struct dns_reg_state *dns_state,
		fd_set *lfds, struct timeval *timeout)
{
	int mdnsd_conn_fd = -1;

	if (dns_state->srv_ref == NULL) {
		return false;
	}

	mdnsd_conn_fd = DNSServiceRefSockFD(dns_state->srv_ref);

	/* Process reply from daemon. Handles any errors. */
	if ((mdnsd_conn_fd != -1) && (FD_ISSET(mdnsd_conn_fd,lfds)) ) {
		DNSServiceErrorType err;
		
		err = DNSServiceProcessResult(dns_state->srv_ref);
		if (err != kDNSServiceErr_NoError) {
			DEBUG(3, ("failed to process mDNS result (err %d), re-trying\n",
				    err));
			schedule_dns_register_smbd_retry(dns_state, timeout);
		}

		return true;
	}

	return false;
}

#else /* WITH_DNSSD_SUPPORT */

 void dns_register_smbd(struct dns_reg_state ** dns_state_ptr,
		unsigned port,
		int *maxfd,
		fd_set *listen_set,
		struct timeval *timeout)
{}

 void dns_register_close(struct dns_reg_state ** dns_state_ptr)
{}

 bool dns_register_smbd_reply(struct dns_reg_state *dns_state,
		fd_set *lfds, struct timeval *timeout)
{
	return false;
}

#endif /* WITH_DNSSD_SUPPORT */