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(iname
->iface
->nbtsrv
->task
->lp_ctx
, 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
= (const char **)str_list_make(tmp_ctx
, iname
->wins_server
, NULL
);
144 io
.in
.wins_port
= lp_nbt_port(iface
->nbtsrv
->task
->lp_ctx
);
145 io
.in
.addresses
= nbtd_address_list(iface
, tmp_ctx
);
146 io
.in
.nb_flags
= iname
->nb_flags
;
147 io
.in
.ttl
= iname
->ttl
;
149 if (!io
.in
.addresses
) {
150 talloc_free(tmp_ctx
);
154 c
= nbt_name_refresh_wins_send(wins_socket(iface
), &io
);
156 talloc_free(tmp_ctx
);
159 talloc_steal(c
, io
.in
.addresses
);
161 c
->async
.fn
= nbtd_wins_refresh_handler
;
162 c
->async
.private_data
= iname
;
164 talloc_free(tmp_ctx
);
169 called when a wins name register has completed
171 static void nbtd_wins_register_handler(struct composite_context
*c
)
174 struct nbt_name_register_wins io
;
175 struct nbtd_iface_name
*iname
= talloc_get_type(c
->async
.private_data
,
176 struct nbtd_iface_name
);
177 TALLOC_CTX
*tmp_ctx
= talloc_new(iname
);
179 status
= nbt_name_register_wins_recv(c
, tmp_ctx
, &io
);
180 if (NT_STATUS_EQUAL(status
, NT_STATUS_IO_TIMEOUT
)) {
181 /* none of the WINS servers responded - try again
183 int wins_retry_time
= lp_parm_int(iname
->iface
->nbtsrv
->task
->lp_ctx
, NULL
, "nbtd", "wins_retry", 300);
184 event_add_timed(iname
->iface
->nbtsrv
->task
->event_ctx
,
186 timeval_current_ofs(wins_retry_time
, 0),
187 nbtd_wins_register_retry
,
189 talloc_free(tmp_ctx
);
193 if (!NT_STATUS_IS_OK(status
)) {
194 DEBUG(1,("Name register failure with WINS for %s - %s\n",
195 nbt_name_string(tmp_ctx
, &iname
->name
), nt_errstr(status
)));
196 talloc_free(tmp_ctx
);
200 if (io
.out
.rcode
!= 0) {
201 DEBUG(1,("WINS server %s rejected name register of %s - %s\n",
202 io
.out
.wins_server
, nbt_name_string(tmp_ctx
, &iname
->name
),
203 nt_errstr(nbt_rcode_to_ntstatus(io
.out
.rcode
))));
204 iname
->nb_flags
|= NBT_NM_CONFLICT
;
205 talloc_free(tmp_ctx
);
209 /* success - start a periodic name refresh */
210 iname
->nb_flags
|= NBT_NM_ACTIVE
;
211 if (iname
->wins_server
) {
213 * talloc_free() would generate a warning,
214 * so steal it into the tmp context
216 talloc_steal(tmp_ctx
, iname
->wins_server
);
218 iname
->wins_server
= talloc_steal(iname
, io
.out
.wins_server
);
220 iname
->registration_time
= timeval_current();
221 nbtd_wins_start_refresh_timer(iname
);
223 DEBUG(3,("Registered %s with WINS server %s\n",
224 nbt_name_string(tmp_ctx
, &iname
->name
), iname
->wins_server
));
226 talloc_free(tmp_ctx
);
230 register a name with our WINS servers
232 void nbtd_winsclient_register(struct nbtd_iface_name
*iname
)
234 struct nbtd_interface
*iface
= iname
->iface
;
235 struct nbt_name_register_wins io
;
236 struct composite_context
*c
;
238 /* setup a wins name register request */
239 io
.in
.name
= iname
->name
;
240 io
.in
.wins_port
= lp_nbt_port(iname
->iface
->nbtsrv
->task
->lp_ctx
);
241 io
.in
.wins_servers
= lp_wins_server_list(iname
->iface
->nbtsrv
->task
->lp_ctx
);
242 io
.in
.addresses
= nbtd_address_list(iface
, iname
);
243 io
.in
.nb_flags
= iname
->nb_flags
;
244 io
.in
.ttl
= iname
->ttl
;
246 if (!io
.in
.addresses
) {
250 c
= nbt_name_register_wins_send(wins_socket(iface
), &io
);
252 talloc_free(io
.in
.addresses
);
255 talloc_steal(c
, io
.in
.addresses
);
257 c
->async
.fn
= nbtd_wins_register_handler
;
258 c
->async
.private_data
= iname
;