2 Unix SMB/CIFS implementation.
4 wins client name registration and refresh
6 Copyright (C) Andrew Tridgell 2005
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program. If not, see <http://www.gnu.org/licenses/>.
23 #include "nbt_server/nbt_server.h"
24 #include "nbt_server/wins/winsserver.h"
25 #include "libcli/composite/composite.h"
26 #include "lib/events/events.h"
27 #include "librpc/gen_ndr/ndr_nbt.h"
28 #include "smbd/service_task.h"
29 #include "param/param.h"
31 static void nbtd_wins_refresh_handler(struct composite_context *c);
33 /* we send WINS client requests using our primary network interface
35 static struct nbt_name_socket *wins_socket(struct nbtd_interface *iface)
37 struct nbtd_server *nbtsrv = iface->nbtsrv;
38 return nbtsrv->interfaces->nbtsock;
42 static void nbtd_wins_refresh(struct event_context *ev, struct timed_event *te,
43 struct timeval t, void *private_data);
46 retry a WINS name registration
48 static void nbtd_wins_register_retry(struct event_context *ev, struct timed_event *te,
49 struct timeval t, void *private_data)
51 struct nbtd_iface_name *iname = talloc_get_type(private_data, struct nbtd_iface_name);
52 nbtd_winsclient_register(iname);
56 start a timer to refresh this name
58 static void nbtd_wins_start_refresh_timer(struct nbtd_iface_name *iname)
60 uint32_t refresh_time;
61 uint32_t max_refresh_time = lp_parm_int(NULL, "nbtd", "max_refresh_time", 7200);
63 refresh_time = MIN(max_refresh_time, iname->ttl/2);
65 event_add_timed(iname->iface->nbtsrv->task->event_ctx,
67 timeval_add(&iname->registration_time, refresh_time, 0),
68 nbtd_wins_refresh, iname);
72 called when a wins name refresh has completed
74 static void nbtd_wins_refresh_handler(struct composite_context *c)
77 struct nbt_name_refresh_wins io;
78 struct nbtd_iface_name *iname = talloc_get_type(c->async.private_data,
79 struct nbtd_iface_name);
80 TALLOC_CTX *tmp_ctx = talloc_new(iname);
82 status = nbt_name_refresh_wins_recv(c, tmp_ctx, &io);
83 if (NT_STATUS_EQUAL(status, NT_STATUS_IO_TIMEOUT)) {
84 /* our WINS server is dead - start registration over
86 DEBUG(2,("Failed to refresh %s with WINS server %s\n",
87 nbt_name_string(tmp_ctx, &iname->name), iname->wins_server));
89 nbtd_winsclient_register(iname);
93 if (!NT_STATUS_IS_OK(status)) {
94 DEBUG(1,("Name refresh failure with WINS for %s - %s\n",
95 nbt_name_string(tmp_ctx, &iname->name), nt_errstr(status)));
100 if (io.out.rcode != 0) {
101 DEBUG(1,("WINS server %s rejected name refresh of %s - %s\n",
102 io.out.wins_server, nbt_name_string(tmp_ctx, &iname->name),
103 nt_errstr(nbt_rcode_to_ntstatus(io.out.rcode))));
104 iname->nb_flags |= NBT_NM_CONFLICT;
105 talloc_free(tmp_ctx);
109 DEBUG(4,("Refreshed name %s with WINS server %s\n",
110 nbt_name_string(tmp_ctx, &iname->name), iname->wins_server));
111 /* success - start a periodic name refresh */
112 iname->nb_flags |= NBT_NM_ACTIVE;
113 if (iname->wins_server) {
115 * talloc_free() would generate a warning,
116 * so steal it into the tmp context
118 talloc_steal(tmp_ctx, iname->wins_server);
120 iname->wins_server = talloc_steal(iname, io.out.wins_server);
122 iname->registration_time = timeval_current();
123 nbtd_wins_start_refresh_timer(iname);
125 talloc_free(tmp_ctx);
130 refresh a WINS name registration
132 static void nbtd_wins_refresh(struct event_context *ev, struct timed_event *te,
133 struct timeval t, void *private_data)
135 struct nbtd_iface_name *iname = talloc_get_type(private_data, struct nbtd_iface_name);
136 struct nbtd_interface *iface = iname->iface;
137 struct nbt_name_refresh_wins io;
138 struct composite_context *c;
139 TALLOC_CTX *tmp_ctx = talloc_new(iname);
141 /* setup a wins name refresh request */
142 io.in.name = iname->name;
143 io.in.wins_servers = str_list_make(tmp_ctx, iname->wins_server, NULL);
144 io.in.addresses = nbtd_address_list(iface, tmp_ctx);
145 io.in.nb_flags = iname->nb_flags;
146 io.in.ttl = iname->ttl;
148 if (!io.in.addresses) {
149 talloc_free(tmp_ctx);
153 c = nbt_name_refresh_wins_send(wins_socket(iface), &io);
155 talloc_free(tmp_ctx);
158 talloc_steal(c, io.in.addresses);
160 c->async.fn = nbtd_wins_refresh_handler;
161 c->async.private_data = iname;
163 talloc_free(tmp_ctx);
168 called when a wins name register has completed
170 static void nbtd_wins_register_handler(struct composite_context *c)
173 struct nbt_name_register_wins io;
174 struct nbtd_iface_name *iname = talloc_get_type(c->async.private_data,
175 struct nbtd_iface_name);
176 TALLOC_CTX *tmp_ctx = talloc_new(iname);
178 status = nbt_name_register_wins_recv(c, tmp_ctx, &io);
179 if (NT_STATUS_EQUAL(status, NT_STATUS_IO_TIMEOUT)) {
180 /* none of the WINS servers responded - try again
182 int wins_retry_time = lp_parm_int(NULL, "nbtd", "wins_retry", 300);
183 event_add_timed(iname->iface->nbtsrv->task->event_ctx,
185 timeval_current_ofs(wins_retry_time, 0),
186 nbtd_wins_register_retry,
188 talloc_free(tmp_ctx);
192 if (!NT_STATUS_IS_OK(status)) {
193 DEBUG(1,("Name register failure with WINS for %s - %s\n",
194 nbt_name_string(tmp_ctx, &iname->name), nt_errstr(status)));
195 talloc_free(tmp_ctx);
199 if (io.out.rcode != 0) {
200 DEBUG(1,("WINS server %s rejected name register of %s - %s\n",
201 io.out.wins_server, nbt_name_string(tmp_ctx, &iname->name),
202 nt_errstr(nbt_rcode_to_ntstatus(io.out.rcode))));
203 iname->nb_flags |= NBT_NM_CONFLICT;
204 talloc_free(tmp_ctx);
208 /* success - start a periodic name refresh */
209 iname->nb_flags |= NBT_NM_ACTIVE;
210 if (iname->wins_server) {
212 * talloc_free() would generate a warning,
213 * so steal it into the tmp context
215 talloc_steal(tmp_ctx, iname->wins_server);
217 iname->wins_server = talloc_steal(iname, io.out.wins_server);
219 iname->registration_time = timeval_current();
220 nbtd_wins_start_refresh_timer(iname);
222 DEBUG(3,("Registered %s with WINS server %s\n",
223 nbt_name_string(tmp_ctx, &iname->name), iname->wins_server));
225 talloc_free(tmp_ctx);
229 register a name with our WINS servers
231 void nbtd_winsclient_register(struct nbtd_iface_name *iname)
233 struct nbtd_interface *iface = iname->iface;
234 struct nbt_name_register_wins io;
235 struct composite_context *c;
237 /* setup a wins name register request */
238 io.in.name = iname->name;
239 io.in.wins_servers = lp_wins_server_list(global_loadparm);
240 io.in.addresses = nbtd_address_list(iface, iname);
241 io.in.nb_flags = iname->nb_flags;
242 io.in.ttl = iname->ttl;
244 if (!io.in.addresses) {
248 c = nbt_name_register_wins_send(wins_socket(iface), &io);
250 talloc_free(io.in.addresses);
253 talloc_steal(c, io.in.addresses);
255 c->async.fn = nbtd_wins_register_handler;
256 c->async.private_data = iname;