2 Unix SMB/CIFS implementation.
4 NBT WINS server testing
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 "lib/socket/socket.h"
25 #include "libcli/resolve/resolve.h"
26 #include "system/network.h"
27 #include "netif/netif.h"
29 #define CHECK_VALUE(v, correct) do { \
30 if ((v) != (correct)) { \
31 printf("(%s) Incorrect value %s=%d (0x%X) - should be %d (0x%X)\n", \
32 __location__, #v, v, v, correct, correct); \
36 #define CHECK_STRING(v, correct) do { \
37 if ((v) != (correct) && \
38 ((v)==NULL || (correct)==NULL || strcasecmp_m(v, correct) != 0)) { \
39 printf("(%s) Incorrect value %s='%s' - should be '%s'\n", \
40 __location__, #v, v, correct); \
44 #define CHECK_NAME(_name, correct) do { \
45 CHECK_STRING((_name).name, (correct).name); \
46 CHECK_VALUE((uint8_t)(_name).type, (uint8_t)(correct).type); \
47 CHECK_STRING((_name).scope, (correct).scope); \
52 test operations against a WINS server
54 static BOOL nbt_test_wins_name(TALLOC_CTX *mem_ctx, const char *address,
55 struct nbt_name *name, uint16_t nb_flags)
57 struct nbt_name_register_wins io;
58 struct nbt_name_query query;
59 struct nbt_name_refresh_wins refresh;
60 struct nbt_name_release release;
62 struct nbt_name_socket *nbtsock = nbt_name_socket_init(mem_ctx, NULL);
64 const char *myaddress = talloc_strdup(mem_ctx, iface_best_ip(address));
65 struct socket_address *socket_address;
67 socket_address = socket_address_from_strings(mem_ctx,
68 nbtsock->sock->backend_name,
70 if (!socket_address) {
74 /* we do the listen here to ensure the WINS server receives the packets from
76 status = socket_listen(nbtsock->sock, socket_address, 0, 0);
77 if (!NT_STATUS_IS_OK(status)) {
78 printf("socket_listen for WINS failed: %s\n", nt_errstr(status));
81 talloc_free(socket_address);
83 printf("Testing name registration to WINS with name %s at %s nb_flags=0x%x\n",
84 nbt_name_string(mem_ctx, name), myaddress, nb_flags);
86 printf("release the name\n");
87 release.in.name = *name;
88 release.in.dest_addr = address;
89 release.in.address = myaddress;
90 release.in.nb_flags = nb_flags;
91 release.in.broadcast = False;
92 release.in.timeout = 3;
93 release.in.retries = 0;
95 status = nbt_name_release(nbtsock, mem_ctx, &release);
96 if (NT_STATUS_EQUAL(status, NT_STATUS_IO_TIMEOUT)) {
97 printf("No response from %s for name release\n", address);
100 if (!NT_STATUS_IS_OK(status)) {
101 printf("Bad response from %s for name query - %s\n",
102 address, nt_errstr(status));
105 CHECK_VALUE(release.out.rcode, 0);
107 printf("register the name\n");
109 io.in.wins_servers = str_list_make(mem_ctx, address, NULL);
110 io.in.addresses = str_list_make(mem_ctx, myaddress, NULL);
111 io.in.nb_flags = nb_flags;
114 status = nbt_name_register_wins(nbtsock, mem_ctx, &io);
115 if (NT_STATUS_EQUAL(status, NT_STATUS_IO_TIMEOUT)) {
116 printf("No response from %s for name register\n", address);
119 if (!NT_STATUS_IS_OK(status)) {
120 printf("Bad response from %s for name register - %s\n",
121 address, nt_errstr(status));
125 CHECK_STRING(io.out.wins_server, address);
126 CHECK_VALUE(io.out.rcode, 0);
128 if (name->type != NBT_NAME_MASTER &&
129 name->type != NBT_NAME_LOGON &&
130 name->type != NBT_NAME_BROWSER &&
131 (nb_flags & NBT_NM_GROUP)) {
132 printf("Try to register as non-group\n");
133 io.in.nb_flags &= ~NBT_NM_GROUP;
134 status = nbt_name_register_wins(nbtsock, mem_ctx, &io);
135 if (!NT_STATUS_IS_OK(status)) {
136 printf("Bad response from %s for name register - %s\n",
137 address, nt_errstr(status));
140 CHECK_VALUE(io.out.rcode, NBT_RCODE_ACT);
143 printf("query the name to make sure its there\n");
144 query.in.name = *name;
145 query.in.dest_addr = address;
146 query.in.broadcast = False;
147 query.in.wins_lookup = True;
148 query.in.timeout = 3;
149 query.in.retries = 0;
151 status = nbt_name_query(nbtsock, mem_ctx, &query);
152 if (name->type == NBT_NAME_MASTER) {
153 if (!NT_STATUS_EQUAL(status, NT_STATUS_OBJECT_NAME_NOT_FOUND)) {
154 printf("Bad response from %s for name query - %s\n",
155 address, nt_errstr(status));
160 if (NT_STATUS_EQUAL(status, NT_STATUS_IO_TIMEOUT)) {
161 printf("No response from %s for name query\n", address);
164 if (!NT_STATUS_IS_OK(status)) {
165 printf("Bad response from %s for name query - %s\n",
166 address, nt_errstr(status));
170 CHECK_NAME(query.out.name, *name);
171 CHECK_VALUE(query.out.num_addrs, 1);
172 if (name->type != NBT_NAME_LOGON &&
173 (nb_flags & NBT_NM_GROUP)) {
174 CHECK_STRING(query.out.reply_addrs[0], "255.255.255.255");
176 CHECK_STRING(query.out.reply_addrs[0], myaddress);
180 query.in.name.name = strupper_talloc(mem_ctx, name->name);
181 if (query.in.name.name &&
182 strcmp(query.in.name.name, name->name) != 0) {
183 printf("check case sensitivity\n");
184 status = nbt_name_query(nbtsock, mem_ctx, &query);
185 if (NT_STATUS_EQUAL(status, NT_STATUS_IO_TIMEOUT)) {
186 printf("No response from %s for name query\n", address);
189 if (!NT_STATUS_EQUAL(status, NT_STATUS_OBJECT_NAME_NOT_FOUND)) {
190 printf("Bad response from %s for name query - %s\n",
191 address, nt_errstr(status));
196 query.in.name = *name;
198 query.in.name.scope = strupper_talloc(mem_ctx, name->scope);
200 if (query.in.name.scope &&
201 strcmp(query.in.name.scope, name->scope) != 0) {
202 printf("check case sensitivity on scope\n");
203 status = nbt_name_query(nbtsock, mem_ctx, &query);
204 if (NT_STATUS_EQUAL(status, NT_STATUS_IO_TIMEOUT)) {
205 printf("No response from %s for name query\n", address);
208 if (!NT_STATUS_EQUAL(status, NT_STATUS_OBJECT_NAME_NOT_FOUND)) {
209 printf("Bad response from %s for name query - %s\n",
210 address, nt_errstr(status));
215 printf("refresh the name\n");
216 refresh.in.name = *name;
217 refresh.in.wins_servers = str_list_make(mem_ctx, address, NULL);
218 refresh.in.addresses = str_list_make(mem_ctx, myaddress, NULL);
219 refresh.in.nb_flags = nb_flags;
220 refresh.in.ttl = 12345;
222 status = nbt_name_refresh_wins(nbtsock, mem_ctx, &refresh);
223 if (NT_STATUS_EQUAL(status, NT_STATUS_IO_TIMEOUT)) {
224 printf("No response from %s for name refresh\n", address);
227 if (!NT_STATUS_IS_OK(status)) {
228 printf("Bad response from %s for name refresh - %s\n",
229 address, nt_errstr(status));
233 CHECK_STRING(refresh.out.wins_server, address);
234 CHECK_VALUE(refresh.out.rcode, 0);
236 printf("release the name\n");
237 release.in.name = *name;
238 release.in.dest_addr = address;
239 release.in.address = myaddress;
240 release.in.nb_flags = nb_flags;
241 release.in.broadcast = False;
242 release.in.timeout = 3;
243 release.in.retries = 0;
245 status = nbt_name_release(nbtsock, mem_ctx, &release);
246 if (NT_STATUS_EQUAL(status, NT_STATUS_IO_TIMEOUT)) {
247 printf("No response from %s for name release\n", address);
250 if (!NT_STATUS_IS_OK(status)) {
251 printf("Bad response from %s for name query - %s\n",
252 address, nt_errstr(status));
256 CHECK_NAME(release.out.name, *name);
257 CHECK_VALUE(release.out.rcode, 0);
260 printf("release again\n");
261 status = nbt_name_release(nbtsock, mem_ctx, &release);
262 if (NT_STATUS_EQUAL(status, NT_STATUS_IO_TIMEOUT)) {
263 printf("No response from %s for name release\n", address);
266 if (!NT_STATUS_IS_OK(status)) {
267 printf("Bad response from %s for name query - %s\n",
268 address, nt_errstr(status));
272 CHECK_NAME(release.out.name, *name);
273 CHECK_VALUE(release.out.rcode, 0);
276 printf("query the name to make sure its gone\n");
277 query.in.name = *name;
278 status = nbt_name_query(nbtsock, mem_ctx, &query);
279 if (name->type != NBT_NAME_LOGON &&
280 (nb_flags & NBT_NM_GROUP)) {
281 if (!NT_STATUS_IS_OK(status)) {
282 printf("ERROR: Name query failed after group release - %s\n",
287 if (NT_STATUS_IS_OK(status)) {
288 printf("ERROR: Name query success after release\n");
291 if (!NT_STATUS_EQUAL(status, NT_STATUS_OBJECT_NAME_NOT_FOUND)) {
292 printf("Incorrect response to name query - %s\n", nt_errstr(status));
303 test operations against a WINS server
305 static BOOL nbt_test_wins(TALLOC_CTX *mem_ctx, const char *address)
307 struct nbt_name name;
309 uint32_t r = (uint32_t)(random() % (100000));
311 name.name = talloc_asprintf(mem_ctx, "_TORTURE-%5u", r);
313 name.type = NBT_NAME_CLIENT;
315 ret &= nbt_test_wins_name(mem_ctx, address, &name, NBT_NODE_H);
317 name.type = NBT_NAME_MASTER;
318 ret &= nbt_test_wins_name(mem_ctx, address, &name, NBT_NODE_H);
320 ret &= nbt_test_wins_name(mem_ctx, address, &name, NBT_NODE_H | NBT_NM_GROUP);
322 name.type = NBT_NAME_SERVER;
323 ret &= nbt_test_wins_name(mem_ctx, address, &name, NBT_NODE_H);
325 name.type = NBT_NAME_LOGON;
326 ret &= nbt_test_wins_name(mem_ctx, address, &name, NBT_NODE_H | NBT_NM_GROUP);
328 name.type = NBT_NAME_BROWSER;
329 ret &= nbt_test_wins_name(mem_ctx, address, &name, NBT_NODE_H | NBT_NM_GROUP);
331 name.type = NBT_NAME_PDC;
332 ret &= nbt_test_wins_name(mem_ctx, address, &name, NBT_NODE_H);
335 ret &= nbt_test_wins_name(mem_ctx, address, &name, NBT_NODE_H);
338 ret &= nbt_test_wins_name(mem_ctx, address, &name, NBT_NODE_H);
340 name.scope = "example";
342 ret &= nbt_test_wins_name(mem_ctx, address, &name, NBT_NODE_H);
344 name.scope = "example";
346 ret &= nbt_test_wins_name(mem_ctx, address, &name, NBT_NODE_H | NBT_NM_GROUP);
348 name.scope = "foo.example.com";
350 ret &= nbt_test_wins_name(mem_ctx, address, &name, NBT_NODE_H);
352 name.name = talloc_asprintf(mem_ctx, "_T\01-%5u.foo", r);
353 ret &= nbt_test_wins_name(mem_ctx, address, &name, NBT_NODE_H);
356 ret &= nbt_test_wins_name(mem_ctx, address, &name, NBT_NODE_H);
358 name.name = talloc_asprintf(mem_ctx, ".");
359 ret &= nbt_test_wins_name(mem_ctx, address, &name, NBT_NODE_H);
361 name.name = talloc_asprintf(mem_ctx, "%5u-\377\200\300FOO", r);
362 ret &= nbt_test_wins_name(mem_ctx, address, &name, NBT_NODE_H);
370 BOOL torture_nbt_wins(void)
373 struct nbt_name name;
374 TALLOC_CTX *mem_ctx = talloc_new(NULL);
378 make_nbt_name_server(&name, lp_parm_string(-1, "torture", "host"));
380 /* do an initial name resolution to find its IP */
381 status = resolve_name(&name, mem_ctx, &address, NULL);
382 if (!NT_STATUS_IS_OK(status)) {
383 printf("Failed to resolve %s - %s\n",
384 name.name, nt_errstr(status));
385 talloc_free(mem_ctx);
389 ret &= nbt_test_wins(mem_ctx, address);
391 talloc_free(mem_ctx);