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 /* we send WINS client requests using our primary network interface
33 static struct nbt_name_socket *wins_socket(struct nbtd_interface *iface)
35 struct nbtd_server *nbtsrv = iface->nbtsrv;
36 return nbtsrv->interfaces->nbtsock;
40 static void nbtd_wins_refresh(struct tevent_context *ev, struct tevent_timer *te,
41 struct timeval t, void *private_data);
44 retry a WINS name registration
46 static void nbtd_wins_register_retry(struct tevent_context *ev, struct tevent_timer *te,
47 struct timeval t, void *private_data)
49 struct nbtd_iface_name *iname = talloc_get_type(private_data, struct nbtd_iface_name);
50 nbtd_winsclient_register(iname);
54 start a timer to refresh this name
56 static void nbtd_wins_start_refresh_timer(struct nbtd_iface_name *iname)
58 uint32_t refresh_time;
59 uint32_t max_refresh_time = lpcfg_parm_int(iname->iface->nbtsrv->task->lp_ctx, NULL, "nbtd", "max_refresh_time", 7200);
61 refresh_time = MIN(max_refresh_time, iname->ttl/2);
63 event_add_timed(iname->iface->nbtsrv->task->event_ctx,
65 timeval_add(&iname->registration_time, refresh_time, 0),
66 nbtd_wins_refresh, iname);
69 struct nbtd_wins_refresh_state {
70 struct nbtd_iface_name *iname;
71 struct nbt_name_refresh_wins io;
75 called when a wins name refresh has completed
77 static void nbtd_wins_refresh_handler(struct tevent_req *subreq)
80 struct nbtd_wins_refresh_state *state =
81 tevent_req_callback_data(subreq,
82 struct nbtd_wins_refresh_state);
83 struct nbtd_iface_name *iname = state->iname;
85 status = nbt_name_refresh_wins_recv(subreq, state, &state->io);
87 if (NT_STATUS_EQUAL(status, NT_STATUS_IO_TIMEOUT)) {
88 /* our WINS server is dead - start registration over
90 DEBUG(2,("Failed to refresh %s with WINS server %s\n",
91 nbt_name_string(state, &iname->name), iname->wins_server));
93 nbtd_winsclient_register(iname);
97 if (!NT_STATUS_IS_OK(status)) {
98 DEBUG(1,("Name refresh failure with WINS for %s - %s\n",
99 nbt_name_string(state, &iname->name), nt_errstr(status)));
104 if (state->io.out.rcode != 0) {
105 DEBUG(1,("WINS server %s rejected name refresh of %s - %s\n",
106 state->io.out.wins_server,
107 nbt_name_string(state, &iname->name),
108 nt_errstr(nbt_rcode_to_ntstatus(state->io.out.rcode))));
109 iname->nb_flags |= NBT_NM_CONFLICT;
114 DEBUG(4,("Refreshed name %s with WINS server %s\n",
115 nbt_name_string(state, &iname->name), iname->wins_server));
116 /* success - start a periodic name refresh */
117 iname->nb_flags |= NBT_NM_ACTIVE;
118 if (iname->wins_server) {
120 * talloc_free() would generate a warning,
121 * so steal it into the tmp context
123 talloc_steal(state, iname->wins_server);
125 iname->wins_server = talloc_move(iname, &state->io.out.wins_server);
126 iname->registration_time = timeval_current();
130 nbtd_wins_start_refresh_timer(iname);
135 refresh a WINS name registration
137 static void nbtd_wins_refresh(struct tevent_context *ev, struct tevent_timer *te,
138 struct timeval t, void *private_data)
140 struct nbtd_iface_name *iname = talloc_get_type(private_data, struct nbtd_iface_name);
141 struct nbtd_interface *iface = iname->iface;
142 struct nbt_name_socket *nbtsock = wins_socket(iface);
143 struct tevent_req *subreq;
144 struct nbtd_wins_refresh_state *state;
146 state = talloc_zero(iname, struct nbtd_wins_refresh_state);
151 state->iname = iname;
153 /* setup a wins name refresh request */
154 state->io.in.name = iname->name;
155 state->io.in.wins_servers = (const char **)str_list_make_single(state, iname->wins_server);
156 state->io.in.wins_port = lpcfg_nbt_port(iface->nbtsrv->task->lp_ctx);
157 state->io.in.addresses = nbtd_address_list(iface, state);
158 state->io.in.nb_flags = iname->nb_flags;
159 state->io.in.ttl = iname->ttl;
161 if (!state->io.in.addresses) {
166 subreq = nbt_name_refresh_wins_send(state, ev, nbtsock, &state->io);
167 if (subreq == NULL) {
172 tevent_req_set_callback(subreq, nbtd_wins_refresh_handler, state);
177 called when a wins name register has completed
179 static void nbtd_wins_register_handler(struct composite_context *c)
182 struct nbt_name_register_wins io;
183 struct nbtd_iface_name *iname = talloc_get_type(c->async.private_data,
184 struct nbtd_iface_name);
185 TALLOC_CTX *tmp_ctx = talloc_new(iname);
187 status = nbt_name_register_wins_recv(c, tmp_ctx, &io);
188 if (NT_STATUS_EQUAL(status, NT_STATUS_IO_TIMEOUT)) {
189 /* none of the WINS servers responded - try again
191 int wins_retry_time = lpcfg_parm_int(iname->iface->nbtsrv->task->lp_ctx, NULL, "nbtd", "wins_retry", 300);
192 event_add_timed(iname->iface->nbtsrv->task->event_ctx,
194 timeval_current_ofs(wins_retry_time, 0),
195 nbtd_wins_register_retry,
197 talloc_free(tmp_ctx);
201 if (!NT_STATUS_IS_OK(status)) {
202 DEBUG(1,("Name register failure with WINS for %s - %s\n",
203 nbt_name_string(tmp_ctx, &iname->name), nt_errstr(status)));
204 talloc_free(tmp_ctx);
208 if (io.out.rcode != 0) {
209 DEBUG(1,("WINS server %s rejected name register of %s - %s\n",
210 io.out.wins_server, nbt_name_string(tmp_ctx, &iname->name),
211 nt_errstr(nbt_rcode_to_ntstatus(io.out.rcode))));
212 iname->nb_flags |= NBT_NM_CONFLICT;
213 talloc_free(tmp_ctx);
217 /* success - start a periodic name refresh */
218 iname->nb_flags |= NBT_NM_ACTIVE;
219 if (iname->wins_server) {
221 * talloc_free() would generate a warning,
222 * so steal it into the tmp context
224 talloc_steal(tmp_ctx, iname->wins_server);
226 iname->wins_server = talloc_steal(iname, io.out.wins_server);
228 iname->registration_time = timeval_current();
229 nbtd_wins_start_refresh_timer(iname);
231 DEBUG(3,("Registered %s with WINS server %s\n",
232 nbt_name_string(tmp_ctx, &iname->name), iname->wins_server));
234 talloc_free(tmp_ctx);
238 register a name with our WINS servers
240 void nbtd_winsclient_register(struct nbtd_iface_name *iname)
242 struct nbtd_interface *iface = iname->iface;
243 struct nbt_name_register_wins io;
244 struct composite_context *c;
246 /* setup a wins name register request */
247 io.in.name = iname->name;
248 io.in.wins_port = lpcfg_nbt_port(iname->iface->nbtsrv->task->lp_ctx);
249 io.in.wins_servers = lpcfg_wins_server_list(iname->iface->nbtsrv->task->lp_ctx);
250 io.in.addresses = nbtd_address_list(iface, iname);
251 io.in.nb_flags = iname->nb_flags;
252 io.in.ttl = iname->ttl;
254 if (!io.in.addresses) {
258 c = nbt_name_register_wins_send(wins_socket(iface), &io);
260 talloc_free(io.in.addresses);
263 talloc_steal(c, io.in.addresses);
265 c->async.fn = nbtd_wins_register_handler;
266 c->async.private_data = iname;