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"
30 static void nbtd_wins_refresh_handler(struct composite_context *c);
32 /* we send WINS client requests using our primary network interface
34 static struct nbt_name_socket *wins_socket(struct nbtd_interface *iface)
36 struct nbtd_server *nbtsrv = iface->nbtsrv;
37 return nbtsrv->interfaces->nbtsock;
41 static void nbtd_wins_refresh(struct event_context *ev, struct timed_event *te,
42 struct timeval t, void *private_data);
45 retry a WINS name registration
47 static void nbtd_wins_register_retry(struct event_context *ev, struct timed_event *te,
48 struct timeval t, void *private_data)
50 struct nbtd_iface_name *iname = talloc_get_type(private_data, struct nbtd_iface_name);
51 nbtd_winsclient_register(iname);
55 start a timer to refresh this name
57 static void nbtd_wins_start_refresh_timer(struct nbtd_iface_name *iname)
59 uint32_t refresh_time;
60 uint32_t max_refresh_time = lp_parm_int(-1, "nbtd", "max_refresh_time", 7200);
62 refresh_time = MIN(max_refresh_time, iname->ttl/2);
64 event_add_timed(iname->iface->nbtsrv->task->event_ctx,
66 timeval_add(&iname->registration_time, refresh_time, 0),
67 nbtd_wins_refresh, iname);
71 called when a wins name refresh has completed
73 static void nbtd_wins_refresh_handler(struct composite_context *c)
76 struct nbt_name_refresh_wins io;
77 struct nbtd_iface_name *iname = talloc_get_type(c->async.private_data,
78 struct nbtd_iface_name);
79 TALLOC_CTX *tmp_ctx = talloc_new(iname);
81 status = nbt_name_refresh_wins_recv(c, tmp_ctx, &io);
82 if (NT_STATUS_EQUAL(status, NT_STATUS_IO_TIMEOUT)) {
83 /* our WINS server is dead - start registration over
85 DEBUG(2,("Failed to refresh %s with WINS server %s\n",
86 nbt_name_string(tmp_ctx, &iname->name), iname->wins_server));
88 nbtd_winsclient_register(iname);
92 if (!NT_STATUS_IS_OK(status)) {
93 DEBUG(1,("Name refresh failure with WINS for %s - %s\n",
94 nbt_name_string(tmp_ctx, &iname->name), nt_errstr(status)));
99 if (io.out.rcode != 0) {
100 DEBUG(1,("WINS server %s rejected name refresh of %s - %s\n",
101 io.out.wins_server, nbt_name_string(tmp_ctx, &iname->name),
102 nt_errstr(nbt_rcode_to_ntstatus(io.out.rcode))));
103 iname->nb_flags |= NBT_NM_CONFLICT;
104 talloc_free(tmp_ctx);
108 DEBUG(4,("Refreshed name %s with WINS server %s\n",
109 nbt_name_string(tmp_ctx, &iname->name), iname->wins_server));
110 /* success - start a periodic name refresh */
111 iname->nb_flags |= NBT_NM_ACTIVE;
112 if (iname->wins_server) {
114 * talloc_free() would generate a warning,
115 * so steal it into the tmp context
117 talloc_steal(tmp_ctx, iname->wins_server);
119 iname->wins_server = talloc_steal(iname, io.out.wins_server);
121 iname->registration_time = timeval_current();
122 nbtd_wins_start_refresh_timer(iname);
124 talloc_free(tmp_ctx);
129 refresh a WINS name registration
131 static void nbtd_wins_refresh(struct event_context *ev, struct timed_event *te,
132 struct timeval t, void *private_data)
134 struct nbtd_iface_name *iname = talloc_get_type(private_data, struct nbtd_iface_name);
135 struct nbtd_interface *iface = iname->iface;
136 struct nbt_name_refresh_wins io;
137 struct composite_context *c;
138 TALLOC_CTX *tmp_ctx = talloc_new(iname);
140 /* setup a wins name refresh request */
141 io.in.name = iname->name;
142 io.in.wins_servers = str_list_make(tmp_ctx, iname->wins_server, NULL);
143 io.in.addresses = nbtd_address_list(iface, tmp_ctx);
144 io.in.nb_flags = iname->nb_flags;
145 io.in.ttl = iname->ttl;
147 if (!io.in.addresses) {
148 talloc_free(tmp_ctx);
152 c = nbt_name_refresh_wins_send(wins_socket(iface), &io);
154 talloc_free(tmp_ctx);
157 talloc_steal(c, io.in.addresses);
159 c->async.fn = nbtd_wins_refresh_handler;
160 c->async.private_data = iname;
162 talloc_free(tmp_ctx);
167 called when a wins name register has completed
169 static void nbtd_wins_register_handler(struct composite_context *c)
172 struct nbt_name_register_wins io;
173 struct nbtd_iface_name *iname = talloc_get_type(c->async.private_data,
174 struct nbtd_iface_name);
175 TALLOC_CTX *tmp_ctx = talloc_new(iname);
177 status = nbt_name_register_wins_recv(c, tmp_ctx, &io);
178 if (NT_STATUS_EQUAL(status, NT_STATUS_IO_TIMEOUT)) {
179 /* none of the WINS servers responded - try again
181 int wins_retry_time = lp_parm_int(-1, "nbtd", "wins_retry", 300);
182 event_add_timed(iname->iface->nbtsrv->task->event_ctx,
184 timeval_current_ofs(wins_retry_time, 0),
185 nbtd_wins_register_retry,
187 talloc_free(tmp_ctx);
191 if (!NT_STATUS_IS_OK(status)) {
192 DEBUG(1,("Name register failure with WINS for %s - %s\n",
193 nbt_name_string(tmp_ctx, &iname->name), nt_errstr(status)));
194 talloc_free(tmp_ctx);
198 if (io.out.rcode != 0) {
199 DEBUG(1,("WINS server %s rejected name register of %s - %s\n",
200 io.out.wins_server, nbt_name_string(tmp_ctx, &iname->name),
201 nt_errstr(nbt_rcode_to_ntstatus(io.out.rcode))));
202 iname->nb_flags |= NBT_NM_CONFLICT;
203 talloc_free(tmp_ctx);
207 /* success - start a periodic name refresh */
208 iname->nb_flags |= NBT_NM_ACTIVE;
209 if (iname->wins_server) {
211 * talloc_free() would generate a warning,
212 * so steal it into the tmp context
214 talloc_steal(tmp_ctx, iname->wins_server);
216 iname->wins_server = talloc_steal(iname, io.out.wins_server);
218 iname->registration_time = timeval_current();
219 nbtd_wins_start_refresh_timer(iname);
221 DEBUG(3,("Registered %s with WINS server %s\n",
222 nbt_name_string(tmp_ctx, &iname->name), iname->wins_server));
224 talloc_free(tmp_ctx);
228 register a name with our WINS servers
230 void nbtd_winsclient_register(struct nbtd_iface_name *iname)
232 struct nbtd_interface *iface = iname->iface;
233 struct nbt_name_register_wins io;
234 struct composite_context *c;
236 /* setup a wins name register request */
237 io.in.name = iname->name;
238 io.in.wins_servers = lp_wins_server_list();
239 io.in.addresses = nbtd_address_list(iface, iname);
240 io.in.nb_flags = iname->nb_flags;
241 io.in.ttl = iname->ttl;
243 if (!io.in.addresses) {
247 c = nbt_name_register_wins_send(wins_socket(iface), &io);
249 talloc_free(io.in.addresses);
252 talloc_steal(c, io.in.addresses);
254 c->async.fn = nbtd_wins_register_handler;
255 c->async.private_data = iname;