r5454: moved the WINS server code into its own directory
[samba.git] / source / nbt_server / wins / winsclient.c
1 /* 
2    Unix SMB/CIFS implementation.
3
4    wins client name registration and refresh
5
6    Copyright (C) Andrew Tridgell        2005
7    
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.
12    
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.
17    
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.
21 */
22
23 #include "includes.h"
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"
29
30
31 /* we send WINS client requests using our primary network interface 
32 */
33 static struct nbt_name_socket *wins_socket(struct nbtd_interface *iface)
34 {
35         struct nbtd_server *nbtsrv = iface->nbtsrv;
36         return nbtsrv->interfaces->nbtsock;
37 }
38
39
40 static void nbtd_wins_refresh(struct event_context *ev, struct timed_event *te,
41                               struct timeval t, void *private);
42
43 /*
44   retry a WINS name registration
45 */
46 static void nbtd_wins_register_retry(struct event_context *ev, struct timed_event *te,
47                                      struct timeval t, void *private)
48 {
49         struct nbtd_iface_name *iname = talloc_get_type(private, struct nbtd_iface_name);
50         nbtd_winsclient_register(iname);
51 }
52
53
54 /*
55   called when a wins name refresh has completed
56 */
57 static void nbtd_wins_refresh_handler(struct composite_context *c)
58 {
59         NTSTATUS status;
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);
64
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
68                    from scratch */
69                 DEBUG(2,("Failed to refresh %s with WINS server %s\n",
70                          nbt_name_string(tmp_ctx, &iname->name), iname->wins_server));
71                 talloc_free(tmp_ctx);
72                 nbtd_winsclient_register(iname);
73                 return;
74         }
75
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)));
79                 talloc_free(tmp_ctx);
80                 return;
81         }       
82
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;
88                 talloc_free(tmp_ctx);
89                 return;
90         }       
91
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);
98         }
99         iname->wins_server = talloc_steal(iname, io.out.wins_server);
100
101         iname->registration_time = timeval_current();
102         event_add_timed(iname->iface->nbtsrv->task->event_ctx, 
103                         iname,
104                         timeval_add(&iname->registration_time, iname->ttl/2, 0),
105                         nbtd_wins_refresh,
106                         iname);
107
108         talloc_free(tmp_ctx);
109 }
110
111
112 /*
113   refresh a WINS name registration
114 */
115 static void nbtd_wins_refresh(struct event_context *ev, struct timed_event *te,
116                               struct timeval t, void *private)
117 {
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);
123
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;
130
131         c = nbt_name_refresh_wins_send(wins_socket(iface), &io);
132         if (c == NULL) {
133                 talloc_free(tmp_ctx);
134                 return;
135         }
136         talloc_steal(c, io.in.addresses);
137
138         c->async.fn = nbtd_wins_refresh_handler;
139         c->async.private = iname;
140
141         talloc_free(tmp_ctx);
142 }
143
144
145 /*
146   called when a wins name register has completed
147 */
148 static void nbtd_wins_register_handler(struct composite_context *c)
149 {
150         NTSTATUS status;
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);
155
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 
159                    periodically */
160                 int wins_retry_time = lp_parm_int(-1, "nbtd", "wins_retry", 300);
161                 event_add_timed(iname->iface->nbtsrv->task->event_ctx, 
162                                 iname,
163                                 timeval_current_ofs(wins_retry_time, 0),
164                                 nbtd_wins_register_retry,
165                                 iname);
166                 talloc_free(tmp_ctx);
167                 return;
168         }
169
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);
174                 return;
175         }       
176
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);
183                 return;
184         }       
185
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);
190         }
191         iname->wins_server = talloc_steal(iname, io.out.wins_server);
192
193         iname->registration_time = timeval_current();
194         event_add_timed(iname->iface->nbtsrv->task->event_ctx, 
195                         iname,
196                         timeval_add(&iname->registration_time, iname->ttl/2, 0),
197                         nbtd_wins_refresh,
198                         iname);
199
200         DEBUG(3,("Registered %s with WINS server %s\n",
201                  nbt_name_string(tmp_ctx, &iname->name), iname->wins_server));
202
203         talloc_free(tmp_ctx);
204 }
205
206 /*
207   register a name with our WINS servers
208 */
209 void nbtd_winsclient_register(struct nbtd_iface_name *iname)
210 {
211         struct nbtd_interface *iface = iname->iface;
212         struct nbt_name_register_wins io;
213         struct composite_context *c;
214
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;
221
222         c = nbt_name_register_wins_send(wins_socket(iface), &io);
223         if (c == NULL) {
224                 talloc_free(io.in.addresses);
225                 return;
226         }
227         talloc_steal(c, io.in.addresses);
228
229         c->async.fn = nbtd_wins_register_handler;
230         c->async.private = iname;
231 }