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 2 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, write to the Free Software
20 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
24 #include "nbt_server/nbt_server.h"
25 #include "libcli/raw/libcliraw.h"
26 #include "libcli/composite/composite.h"
27 #include "lib/events/events.h"
28 #include "smbd/service_task.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 event_context *ev, struct timed_event *te,
41 struct timeval t, void *private);
44 retry a WINS name registration
46 static void nbtd_wins_register_retry(struct event_context *ev, struct timed_event *te,
47 struct timeval t, void *private)
49 struct nbtd_iface_name *iname = talloc_get_type(private, struct nbtd_iface_name);
50 nbtd_winsclient_register(iname);
55 called when a wins name refresh has completed
57 static void nbtd_wins_refresh_handler(struct composite_context *c)
60 struct nbt_name_refresh_wins io;
61 struct nbtd_iface_name *iname = talloc_get_type(c->async.private,
62 struct nbtd_iface_name);
63 TALLOC_CTX *tmp_ctx = talloc_new(iname);
65 status = nbt_name_refresh_wins_recv(c, tmp_ctx, &io);
66 if (NT_STATUS_EQUAL(status, NT_STATUS_IO_TIMEOUT)) {
67 /* our WINS server is dead - start registration over
69 DEBUG(2,("Failed to refresh %s with WINS server %s\n",
70 nbt_name_string(tmp_ctx, &iname->name), iname->wins_server));
72 nbtd_winsclient_register(iname);
76 if (!NT_STATUS_IS_OK(status)) {
77 DEBUG(1,("Name refresh failure with WINS for %s - %s\n",
78 nbt_name_string(tmp_ctx, &iname->name), nt_errstr(status)));
83 if (io.out.rcode != 0) {
84 DEBUG(1,("WINS server %s rejected name refresh of %s - %s\n",
85 io.out.wins_server, nbt_name_string(tmp_ctx, &iname->name),
86 nt_errstr(nbt_rcode_to_ntstatus(io.out.rcode))));
87 iname->nb_flags |= NBT_NM_CONFLICT;
92 DEBUG(4,("Refreshed name %s with WINS server %s\n",
93 nbt_name_string(tmp_ctx, &iname->name), iname->wins_server));
94 /* success - start a periodic name refresh */
95 iname->nb_flags |= NBT_NM_ACTIVE;
96 if (iname->wins_server) {
97 talloc_free(iname->wins_server);
99 iname->wins_server = talloc_steal(iname, io.out.wins_server);
101 iname->registration_time = timeval_current();
102 event_add_timed(iname->iface->nbtsrv->task->event_ctx,
104 timeval_add(&iname->registration_time, iname->ttl/2, 0),
108 talloc_free(tmp_ctx);
113 refresh a WINS name registration
115 static void nbtd_wins_refresh(struct event_context *ev, struct timed_event *te,
116 struct timeval t, void *private)
118 struct nbtd_iface_name *iname = talloc_get_type(private, struct nbtd_iface_name);
119 struct nbtd_interface *iface = iname->iface;
120 struct nbt_name_refresh_wins io;
121 struct composite_context *c;
122 TALLOC_CTX *tmp_ctx = talloc_new(iname);
124 /* setup a wins name refresh request */
125 io.in.name = iname->name;
126 io.in.wins_servers = str_list_make(tmp_ctx, iname->wins_server, NULL);
127 io.in.addresses = nbtd_address_list(iface, tmp_ctx);
128 io.in.nb_flags = iname->nb_flags;
129 io.in.ttl = iname->ttl;
131 c = nbt_name_refresh_wins_send(wins_socket(iface), &io);
133 talloc_free(tmp_ctx);
136 talloc_steal(c, io.in.addresses);
138 c->async.fn = nbtd_wins_refresh_handler;
139 c->async.private = iname;
141 talloc_free(tmp_ctx);
146 called when a wins name register has completed
148 static void nbtd_wins_register_handler(struct composite_context *c)
151 struct nbt_name_register_wins io;
152 struct nbtd_iface_name *iname = talloc_get_type(c->async.private,
153 struct nbtd_iface_name);
154 TALLOC_CTX *tmp_ctx = talloc_new(iname);
156 status = nbt_name_register_wins_recv(c, tmp_ctx, &io);
157 if (NT_STATUS_EQUAL(status, NT_STATUS_IO_TIMEOUT)) {
158 /* none of the WINS servers responded - try again
160 int wins_retry_time = lp_parm_int(-1, "nbtd", "wins_retry", 300);
161 event_add_timed(iname->iface->nbtsrv->task->event_ctx,
163 timeval_current_ofs(wins_retry_time, 0),
164 nbtd_wins_register_retry,
166 talloc_free(tmp_ctx);
170 if (!NT_STATUS_IS_OK(status)) {
171 DEBUG(1,("Name register failure with WINS for %s - %s\n",
172 nbt_name_string(tmp_ctx, &iname->name), nt_errstr(status)));
173 talloc_free(tmp_ctx);
177 if (io.out.rcode != 0) {
178 DEBUG(1,("WINS server %s rejected name register of %s - %s\n",
179 io.out.wins_server, nbt_name_string(tmp_ctx, &iname->name),
180 nt_errstr(nbt_rcode_to_ntstatus(io.out.rcode))));
181 iname->nb_flags |= NBT_NM_CONFLICT;
182 talloc_free(tmp_ctx);
186 /* success - start a periodic name refresh */
187 iname->nb_flags |= NBT_NM_ACTIVE;
188 if (iname->wins_server) {
189 talloc_free(iname->wins_server);
191 iname->wins_server = talloc_steal(iname, io.out.wins_server);
193 iname->registration_time = timeval_current();
194 event_add_timed(iname->iface->nbtsrv->task->event_ctx,
196 timeval_add(&iname->registration_time, iname->ttl/2, 0),
200 DEBUG(3,("Registered %s with WINS server %s\n",
201 nbt_name_string(tmp_ctx, &iname->name), iname->wins_server));
203 talloc_free(tmp_ctx);
207 register a name with our WINS servers
209 void nbtd_winsclient_register(struct nbtd_iface_name *iname)
211 struct nbtd_interface *iface = iname->iface;
212 struct nbt_name_register_wins io;
213 struct composite_context *c;
215 /* setup a wins name register request */
216 io.in.name = iname->name;
217 io.in.wins_servers = lp_wins_server_list();
218 io.in.addresses = nbtd_address_list(iface, iname);
219 io.in.nb_flags = iname->nb_flags;
220 io.in.ttl = iname->ttl;
222 c = nbt_name_register_wins_send(wins_socket(iface), &io);
224 talloc_free(io.in.addresses);
227 talloc_steal(c, io.in.addresses);
229 c->async.fn = nbtd_wins_register_handler;
230 c->async.private = iname;