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<%02x> with WINS server %s\n",
70 iname->name.name, iname->name.type, iname->wins_server));
71 nbtd_winsclient_register(iname);
75 if (!NT_STATUS_IS_OK(status)) {
76 DEBUG(1,("Name refresh failure with WINS for %s<%02x> - %s\n",
77 iname->name.name, iname->name.type, nt_errstr(status)));
82 if (io.out.rcode != 0) {
83 DEBUG(1,("WINS server %s rejected name refresh of %s<%02x> - %s\n",
84 io.out.wins_server, iname->name.name, iname->name.type,
85 nt_errstr(nbt_rcode_to_ntstatus(io.out.rcode))));
86 iname->nb_flags |= NBT_NM_CONFLICT;
91 DEBUG(4,("Refreshed name %s<%02x> with WINS server %s\n",
92 iname->name.name, iname->name.type, iname->wins_server));
93 /* success - start a periodic name refresh */
94 iname->nb_flags |= NBT_NM_ACTIVE;
95 if (iname->wins_server) {
96 talloc_free(iname->wins_server);
98 iname->wins_server = talloc_steal(iname, io.out.wins_server);
100 iname->registration_time = timeval_current();
101 event_add_timed(iname->iface->nbtsrv->task->event_ctx,
103 timeval_add(&iname->registration_time, iname->ttl/2, 0),
107 talloc_free(tmp_ctx);
112 refresh a WINS name registration
114 static void nbtd_wins_refresh(struct event_context *ev, struct timed_event *te,
115 struct timeval t, void *private)
117 struct nbtd_iface_name *iname = talloc_get_type(private, struct nbtd_iface_name);
118 struct nbtd_interface *iface = iname->iface;
119 struct nbt_name_refresh_wins io;
120 struct composite_context *c;
121 TALLOC_CTX *tmp_ctx = talloc_new(iname);
123 /* setup a wins name refresh request */
124 io.in.name = iname->name;
125 io.in.wins_servers = str_list_make(tmp_ctx, iname->wins_server, NULL);
126 io.in.addresses = nbtd_address_list(iface, tmp_ctx);
127 io.in.nb_flags = iname->nb_flags;
128 io.in.ttl = iname->ttl;
130 c = nbt_name_refresh_wins_send(wins_socket(iface), &io);
132 talloc_free(tmp_ctx);
135 talloc_steal(c, io.in.addresses);
137 c->async.fn = nbtd_wins_refresh_handler;
138 c->async.private = iname;
140 talloc_free(tmp_ctx);
145 called when a wins name register has completed
147 static void nbtd_wins_register_handler(struct composite_context *c)
150 struct nbt_name_register_wins io;
151 struct nbtd_iface_name *iname = talloc_get_type(c->async.private,
152 struct nbtd_iface_name);
153 TALLOC_CTX *tmp_ctx = talloc_new(iname);
155 status = nbt_name_register_wins_recv(c, tmp_ctx, &io);
156 if (NT_STATUS_EQUAL(status, NT_STATUS_IO_TIMEOUT)) {
157 /* none of the WINS servers responded - try again
159 int wins_retry_time = lp_parm_int(-1, "nbtd", "wins_retry", 300);
160 event_add_timed(iname->iface->nbtsrv->task->event_ctx,
162 timeval_current_ofs(wins_retry_time, 0),
163 nbtd_wins_register_retry,
165 talloc_free(tmp_ctx);
169 if (!NT_STATUS_IS_OK(status)) {
170 DEBUG(1,("Name register failure with WINS for %s<%02x> - %s\n",
171 iname->name.name, iname->name.type, nt_errstr(status)));
172 talloc_free(tmp_ctx);
176 if (io.out.rcode != 0) {
177 DEBUG(1,("WINS server %s rejected name register of %s<%02x> - %s\n",
178 io.out.wins_server, iname->name.name, iname->name.type,
179 nt_errstr(nbt_rcode_to_ntstatus(io.out.rcode))));
180 iname->nb_flags |= NBT_NM_CONFLICT;
181 talloc_free(tmp_ctx);
185 /* success - start a periodic name refresh */
186 iname->nb_flags |= NBT_NM_ACTIVE;
187 if (iname->wins_server) {
188 talloc_free(iname->wins_server);
190 iname->wins_server = talloc_steal(iname, io.out.wins_server);
192 iname->registration_time = timeval_current();
193 event_add_timed(iname->iface->nbtsrv->task->event_ctx,
195 timeval_add(&iname->registration_time, iname->ttl/2, 0),
199 DEBUG(3,("Registered %s<%02x> with WINS server %s\n",
200 iname->name.name, iname->name.type, iname->wins_server));
202 talloc_free(tmp_ctx);
206 register a name with our WINS servers
208 void nbtd_winsclient_register(struct nbtd_iface_name *iname)
210 struct nbtd_interface *iface = iname->iface;
211 struct nbt_name_register_wins io;
212 struct composite_context *c;
214 /* setup a wins name register request */
215 io.in.name = iname->name;
216 io.in.wins_servers = lp_wins_server_list();
217 io.in.addresses = nbtd_address_list(iface, iname);
218 io.in.nb_flags = iname->nb_flags;
219 io.in.ttl = iname->ttl;
221 c = nbt_name_register_wins_send(wins_socket(iface), &io);
223 talloc_free(io.in.addresses);
226 talloc_steal(c, io.in.addresses);
228 c->async.fn = nbtd_wins_register_handler;
229 c->async.private = iname;