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"
28 #include "librpc/gen_ndr/ndr_nbt.h"
30 #define CHECK_VALUE(v, correct) do { \
31 if ((v) != (correct)) { \
32 printf("(%s) Incorrect value %s=%d (0x%X) - should be %d (0x%X)\n", \
33 __location__, #v, v, v, correct, correct); \
37 #define CHECK_STRING(v, correct) do { \
38 if ((v) != (correct) && \
39 ((v)==NULL || (correct)==NULL || strcasecmp_m(v, correct) != 0)) { \
40 printf("(%s) Incorrect value %s='%s' - should be '%s'\n", \
41 __location__, #v, v, correct); \
45 #define CHECK_NAME(_name, correct) do { \
46 CHECK_STRING((_name).name, (correct).name); \
47 CHECK_VALUE((uint8_t)(_name).type, (uint8_t)(correct).type); \
48 CHECK_STRING((_name).scope, (correct).scope); \
53 test operations against a WINS server
55 static BOOL nbt_test_wins_name(TALLOC_CTX *mem_ctx, const char *address,
56 struct nbt_name *name, uint16_t nb_flags)
58 struct nbt_name_register_wins io;
59 struct nbt_name_query query;
60 struct nbt_name_refresh_wins refresh;
61 struct nbt_name_release release;
63 struct nbt_name_socket *nbtsock = nbt_name_socket_init(mem_ctx, NULL);
65 const char *myaddress = talloc_strdup(mem_ctx, iface_best_ip(address));
66 struct socket_address *socket_address;
68 socket_address = socket_address_from_strings(mem_ctx,
69 nbtsock->sock->backend_name,
71 if (!socket_address) {
75 /* we do the listen here to ensure the WINS server receives the packets from
77 status = socket_listen(nbtsock->sock, socket_address, 0, 0);
78 if (!NT_STATUS_IS_OK(status)) {
79 printf("socket_listen for WINS failed: %s\n", nt_errstr(status));
82 talloc_free(socket_address);
84 printf("Testing name registration to WINS with name %s at %s nb_flags=0x%x\n",
85 nbt_name_string(mem_ctx, name), myaddress, nb_flags);
87 printf("release the name\n");
88 release.in.name = *name;
89 release.in.dest_addr = address;
90 release.in.address = myaddress;
91 release.in.nb_flags = nb_flags;
92 release.in.broadcast = False;
93 release.in.timeout = 3;
94 release.in.retries = 0;
96 status = nbt_name_release(nbtsock, mem_ctx, &release);
97 if (NT_STATUS_EQUAL(status, NT_STATUS_IO_TIMEOUT)) {
98 printf("No response from %s for name release\n", address);
101 if (!NT_STATUS_IS_OK(status)) {
102 printf("Bad response from %s for name query - %s\n",
103 address, nt_errstr(status));
106 CHECK_VALUE(release.out.rcode, 0);
108 printf("register the name\n");
110 io.in.wins_servers = str_list_make(mem_ctx, address, NULL);
111 io.in.addresses = str_list_make(mem_ctx, myaddress, NULL);
112 io.in.nb_flags = nb_flags;
115 status = nbt_name_register_wins(nbtsock, mem_ctx, &io);
116 if (NT_STATUS_EQUAL(status, NT_STATUS_IO_TIMEOUT)) {
117 printf("No response from %s for name register\n", address);
120 if (!NT_STATUS_IS_OK(status)) {
121 printf("Bad response from %s for name register - %s\n",
122 address, nt_errstr(status));
126 CHECK_STRING(io.out.wins_server, address);
127 CHECK_VALUE(io.out.rcode, 0);
129 if (name->type != NBT_NAME_MASTER &&
130 name->type != NBT_NAME_LOGON &&
131 name->type != NBT_NAME_BROWSER &&
132 (nb_flags & NBT_NM_GROUP)) {
133 printf("Try to register as non-group\n");
134 io.in.nb_flags &= ~NBT_NM_GROUP;
135 status = nbt_name_register_wins(nbtsock, mem_ctx, &io);
136 if (!NT_STATUS_IS_OK(status)) {
137 printf("Bad response from %s for name register - %s\n",
138 address, nt_errstr(status));
141 CHECK_VALUE(io.out.rcode, NBT_RCODE_ACT);
144 printf("query the name to make sure its there\n");
145 query.in.name = *name;
146 query.in.dest_addr = address;
147 query.in.broadcast = False;
148 query.in.wins_lookup = True;
149 query.in.timeout = 3;
150 query.in.retries = 0;
152 status = nbt_name_query(nbtsock, mem_ctx, &query);
153 if (name->type == NBT_NAME_MASTER) {
154 if (!NT_STATUS_EQUAL(status, NT_STATUS_OBJECT_NAME_NOT_FOUND)) {
155 printf("Bad response from %s for name query - %s\n",
156 address, nt_errstr(status));
161 if (NT_STATUS_EQUAL(status, NT_STATUS_IO_TIMEOUT)) {
162 printf("No response from %s for name query\n", address);
165 if (!NT_STATUS_IS_OK(status)) {
166 printf("Bad response from %s for name query - %s\n",
167 address, nt_errstr(status));
171 CHECK_NAME(query.out.name, *name);
172 CHECK_VALUE(query.out.num_addrs, 1);
173 if (name->type != NBT_NAME_LOGON &&
174 (nb_flags & NBT_NM_GROUP)) {
175 CHECK_STRING(query.out.reply_addrs[0], "255.255.255.255");
177 CHECK_STRING(query.out.reply_addrs[0], myaddress);
181 query.in.name.name = strupper_talloc(mem_ctx, name->name);
182 if (query.in.name.name &&
183 strcmp(query.in.name.name, name->name) != 0) {
184 printf("check case sensitivity\n");
185 status = nbt_name_query(nbtsock, mem_ctx, &query);
186 if (NT_STATUS_EQUAL(status, NT_STATUS_IO_TIMEOUT)) {
187 printf("No response from %s for name query\n", address);
190 if (!NT_STATUS_EQUAL(status, NT_STATUS_OBJECT_NAME_NOT_FOUND)) {
191 printf("Bad response from %s for name query - %s\n",
192 address, nt_errstr(status));
197 query.in.name = *name;
199 query.in.name.scope = strupper_talloc(mem_ctx, name->scope);
201 if (query.in.name.scope &&
202 strcmp(query.in.name.scope, name->scope) != 0) {
203 printf("check case sensitivity on scope\n");
204 status = nbt_name_query(nbtsock, mem_ctx, &query);
205 if (NT_STATUS_EQUAL(status, NT_STATUS_IO_TIMEOUT)) {
206 printf("No response from %s for name query\n", address);
209 if (!NT_STATUS_EQUAL(status, NT_STATUS_OBJECT_NAME_NOT_FOUND)) {
210 printf("Bad response from %s for name query - %s\n",
211 address, nt_errstr(status));
216 printf("refresh the name\n");
217 refresh.in.name = *name;
218 refresh.in.wins_servers = str_list_make(mem_ctx, address, NULL);
219 refresh.in.addresses = str_list_make(mem_ctx, myaddress, NULL);
220 refresh.in.nb_flags = nb_flags;
221 refresh.in.ttl = 12345;
223 status = nbt_name_refresh_wins(nbtsock, mem_ctx, &refresh);
224 if (NT_STATUS_EQUAL(status, NT_STATUS_IO_TIMEOUT)) {
225 printf("No response from %s for name refresh\n", address);
228 if (!NT_STATUS_IS_OK(status)) {
229 printf("Bad response from %s for name refresh - %s\n",
230 address, nt_errstr(status));
234 CHECK_STRING(refresh.out.wins_server, address);
235 CHECK_VALUE(refresh.out.rcode, 0);
237 printf("release the name\n");
238 release.in.name = *name;
239 release.in.dest_addr = address;
240 release.in.address = myaddress;
241 release.in.nb_flags = nb_flags;
242 release.in.broadcast = False;
243 release.in.timeout = 3;
244 release.in.retries = 0;
246 status = nbt_name_release(nbtsock, mem_ctx, &release);
247 if (NT_STATUS_EQUAL(status, NT_STATUS_IO_TIMEOUT)) {
248 printf("No response from %s for name release\n", address);
251 if (!NT_STATUS_IS_OK(status)) {
252 printf("Bad response from %s for name query - %s\n",
253 address, nt_errstr(status));
257 CHECK_NAME(release.out.name, *name);
258 CHECK_VALUE(release.out.rcode, 0);
261 printf("release again\n");
262 status = nbt_name_release(nbtsock, mem_ctx, &release);
263 if (NT_STATUS_EQUAL(status, NT_STATUS_IO_TIMEOUT)) {
264 printf("No response from %s for name release\n", address);
267 if (!NT_STATUS_IS_OK(status)) {
268 printf("Bad response from %s for name query - %s\n",
269 address, nt_errstr(status));
273 CHECK_NAME(release.out.name, *name);
274 CHECK_VALUE(release.out.rcode, 0);
277 printf("query the name to make sure its gone\n");
278 query.in.name = *name;
279 status = nbt_name_query(nbtsock, mem_ctx, &query);
280 if (name->type != NBT_NAME_LOGON &&
281 (nb_flags & NBT_NM_GROUP)) {
282 if (!NT_STATUS_IS_OK(status)) {
283 printf("ERROR: Name query failed after group release - %s\n",
288 if (NT_STATUS_IS_OK(status)) {
289 printf("ERROR: Name query success after release\n");
292 if (!NT_STATUS_EQUAL(status, NT_STATUS_OBJECT_NAME_NOT_FOUND)) {
293 printf("Incorrect response to name query - %s\n", nt_errstr(status));
304 test operations against a WINS server
306 static BOOL nbt_test_wins(TALLOC_CTX *mem_ctx, const char *address)
308 struct nbt_name name;
310 uint32_t r = (uint32_t)(random() % (100000));
312 name.name = talloc_asprintf(mem_ctx, "_TORTURE-%5u", r);
314 name.type = NBT_NAME_CLIENT;
316 ret &= nbt_test_wins_name(mem_ctx, address, &name, NBT_NODE_H);
318 name.type = NBT_NAME_MASTER;
319 ret &= nbt_test_wins_name(mem_ctx, address, &name, NBT_NODE_H);
321 ret &= nbt_test_wins_name(mem_ctx, address, &name, NBT_NODE_H | NBT_NM_GROUP);
323 name.type = NBT_NAME_SERVER;
324 ret &= nbt_test_wins_name(mem_ctx, address, &name, NBT_NODE_H);
326 name.type = NBT_NAME_LOGON;
327 ret &= nbt_test_wins_name(mem_ctx, address, &name, NBT_NODE_H | NBT_NM_GROUP);
329 name.type = NBT_NAME_BROWSER;
330 ret &= nbt_test_wins_name(mem_ctx, address, &name, NBT_NODE_H | NBT_NM_GROUP);
332 name.type = NBT_NAME_PDC;
333 ret &= nbt_test_wins_name(mem_ctx, address, &name, NBT_NODE_H);
336 ret &= nbt_test_wins_name(mem_ctx, address, &name, NBT_NODE_H);
339 ret &= nbt_test_wins_name(mem_ctx, address, &name, NBT_NODE_H);
341 name.scope = "example";
343 ret &= nbt_test_wins_name(mem_ctx, address, &name, NBT_NODE_H);
345 name.scope = "example";
347 ret &= nbt_test_wins_name(mem_ctx, address, &name, NBT_NODE_H | NBT_NM_GROUP);
349 name.scope = "foo.example.com";
351 ret &= nbt_test_wins_name(mem_ctx, address, &name, NBT_NODE_H);
353 name.name = talloc_asprintf(mem_ctx, "_T\01-%5u.foo", r);
354 ret &= nbt_test_wins_name(mem_ctx, address, &name, NBT_NODE_H);
357 ret &= nbt_test_wins_name(mem_ctx, address, &name, NBT_NODE_H);
359 name.name = talloc_asprintf(mem_ctx, ".");
360 ret &= nbt_test_wins_name(mem_ctx, address, &name, NBT_NODE_H);
362 name.name = talloc_asprintf(mem_ctx, "%5u-\377\200\300FOO", r);
363 ret &= nbt_test_wins_name(mem_ctx, address, &name, NBT_NODE_H);
371 BOOL torture_nbt_wins(void)
374 struct nbt_name name;
375 TALLOC_CTX *mem_ctx = talloc_new(NULL);
379 make_nbt_name_server(&name, lp_parm_string(-1, "torture", "host"));
381 /* do an initial name resolution to find its IP */
382 status = resolve_name(&name, mem_ctx, &address, NULL);
383 if (!NT_STATUS_IS_OK(status)) {
384 printf("Failed to resolve %s - %s\n",
385 name.name, nt_errstr(status));
386 talloc_free(mem_ctx);
390 ret &= nbt_test_wins(mem_ctx, address);
392 talloc_free(mem_ctx);