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"
26 #define CHECK_VALUE(v, correct) do { \
27 if ((v) != (correct)) { \
28 printf("(%s) Incorrect value %s=%d - should be %d\n", \
29 __location__, #v, v, correct); \
33 #define CHECK_STRING(v, correct) do { \
34 if ((v) != (correct) && \
35 ((v)==NULL || (correct)==NULL || strcasecmp_m(v, correct) != 0)) { \
36 printf("(%s) Incorrect value %s='%s' - should be '%s'\n", \
37 __location__, #v, v, correct); \
41 #define CHECK_NAME(_name, correct) do { \
42 CHECK_STRING((_name).name, (correct).name); \
43 CHECK_VALUE((_name).type, (correct).type); \
44 CHECK_STRING((_name).scope, (correct).scope); \
49 test operations against a WINS server
51 static BOOL nbt_test_wins_name(TALLOC_CTX *mem_ctx, const char *address,
52 struct nbt_name *name, uint16_t nb_flags)
54 struct nbt_name_register_wins io;
55 struct nbt_name_query query;
56 struct nbt_name_refresh_wins refresh;
57 struct nbt_name_release release;
59 struct nbt_name_socket *nbtsock = nbt_name_socket_init(mem_ctx, NULL);
61 const char *myaddress = talloc_strdup(mem_ctx, iface_best_ip(address));
63 /* we do the listen here to ensure the WINS server receives the packets from
65 socket_listen(nbtsock->sock, myaddress, 0, 0, 0);
67 printf("Testing name registration to WINS with name %s at %s nb_flags=0x%x\n",
68 nbt_name_string(mem_ctx, name), myaddress, nb_flags);
70 printf("release the name\n");
71 release.in.name = *name;
72 release.in.dest_addr = address;
73 release.in.address = myaddress;
74 release.in.nb_flags = nb_flags;
75 release.in.broadcast = False;
76 release.in.timeout = 3;
77 release.in.retries = 0;
79 status = nbt_name_release(nbtsock, mem_ctx, &release);
80 if (NT_STATUS_EQUAL(status, NT_STATUS_IO_TIMEOUT)) {
81 printf("No response from %s for name release\n", address);
84 if (!NT_STATUS_IS_OK(status)) {
85 printf("Bad response from %s for name query - %s\n",
86 address, nt_errstr(status));
89 CHECK_VALUE(release.out.rcode, 0);
91 printf("register the name\n");
93 io.in.wins_servers = str_list_make(mem_ctx, address, NULL);
94 io.in.addresses = str_list_make(mem_ctx, myaddress, NULL);
95 io.in.nb_flags = nb_flags;
98 status = nbt_name_register_wins(nbtsock, mem_ctx, &io);
99 if (NT_STATUS_EQUAL(status, NT_STATUS_IO_TIMEOUT)) {
100 printf("No response from %s for name register\n", address);
103 if (!NT_STATUS_IS_OK(status)) {
104 printf("Bad response from %s for name register - %s\n",
105 address, nt_errstr(status));
109 CHECK_STRING(io.out.wins_server, address);
110 CHECK_VALUE(io.out.rcode, 0);
112 if (name->type != NBT_NAME_MASTER &&
113 name->type != NBT_NAME_LOGON &&
114 name->type != NBT_NAME_BROWSER &&
115 (nb_flags & NBT_NM_GROUP)) {
116 printf("Try to register as non-group\n");
117 io.in.nb_flags &= ~NBT_NM_GROUP;
118 status = nbt_name_register_wins(nbtsock, mem_ctx, &io);
119 if (!NT_STATUS_IS_OK(status)) {
120 printf("Bad response from %s for name register - %s\n",
121 address, nt_errstr(status));
124 CHECK_VALUE(io.out.rcode, NBT_RCODE_ACT);
127 printf("query the name to make sure its there\n");
128 query.in.name = *name;
129 query.in.dest_addr = address;
130 query.in.broadcast = False;
131 query.in.wins_lookup = True;
132 query.in.timeout = 3;
133 query.in.retries = 0;
135 status = nbt_name_query(nbtsock, mem_ctx, &query);
136 if (name->type == NBT_NAME_MASTER) {
137 if (!NT_STATUS_EQUAL(status, NT_STATUS_OBJECT_NAME_NOT_FOUND)) {
138 printf("Bad response from %s for name query - %s\n",
139 address, nt_errstr(status));
144 if (NT_STATUS_EQUAL(status, NT_STATUS_IO_TIMEOUT)) {
145 printf("No response from %s for name query\n", address);
148 if (!NT_STATUS_IS_OK(status)) {
149 printf("Bad response from %s for name query - %s\n",
150 address, nt_errstr(status));
154 CHECK_NAME(query.out.name, *name);
155 CHECK_VALUE(query.out.num_addrs, 1);
156 if (name->type != NBT_NAME_LOGON &&
157 (nb_flags & NBT_NM_GROUP)) {
158 CHECK_STRING(query.out.reply_addrs[0], "255.255.255.255");
160 CHECK_STRING(query.out.reply_addrs[0], myaddress);
164 query.in.name.name = strupper_talloc(mem_ctx, name->name);
165 if (query.in.name.name &&
166 strcmp(query.in.name.name, name->name) != 0) {
167 printf("check case sensitivity\n");
168 status = nbt_name_query(nbtsock, mem_ctx, &query);
169 if (NT_STATUS_EQUAL(status, NT_STATUS_IO_TIMEOUT)) {
170 printf("No response from %s for name query\n", address);
173 if (!NT_STATUS_EQUAL(status, NT_STATUS_OBJECT_NAME_NOT_FOUND)) {
174 printf("Bad response from %s for name query - %s\n",
175 address, nt_errstr(status));
180 query.in.name = *name;
182 query.in.name.scope = strupper_talloc(mem_ctx, name->scope);
184 if (query.in.name.scope &&
185 strcmp(query.in.name.scope, name->scope) != 0) {
186 printf("check case sensitivity on scope\n");
187 status = nbt_name_query(nbtsock, mem_ctx, &query);
188 if (NT_STATUS_EQUAL(status, NT_STATUS_IO_TIMEOUT)) {
189 printf("No response from %s for name query\n", address);
192 if (!NT_STATUS_EQUAL(status, NT_STATUS_OBJECT_NAME_NOT_FOUND)) {
193 printf("Bad response from %s for name query - %s\n",
194 address, nt_errstr(status));
199 printf("refresh the name\n");
200 refresh.in.name = *name;
201 refresh.in.wins_servers = str_list_make(mem_ctx, address, NULL);
202 refresh.in.addresses = str_list_make(mem_ctx, myaddress, NULL);
203 refresh.in.nb_flags = nb_flags;
204 refresh.in.ttl = 12345;
206 status = nbt_name_refresh_wins(nbtsock, mem_ctx, &refresh);
207 if (NT_STATUS_EQUAL(status, NT_STATUS_IO_TIMEOUT)) {
208 printf("No response from %s for name refresh\n", address);
211 if (!NT_STATUS_IS_OK(status)) {
212 printf("Bad response from %s for name refresh - %s\n",
213 address, nt_errstr(status));
217 CHECK_STRING(refresh.out.wins_server, address);
218 CHECK_VALUE(refresh.out.rcode, 0);
220 printf("release the name\n");
221 release.in.name = *name;
222 release.in.dest_addr = address;
223 release.in.address = myaddress;
224 release.in.nb_flags = nb_flags;
225 release.in.broadcast = False;
226 release.in.timeout = 3;
227 release.in.retries = 0;
229 status = nbt_name_release(nbtsock, mem_ctx, &release);
230 if (NT_STATUS_EQUAL(status, NT_STATUS_IO_TIMEOUT)) {
231 printf("No response from %s for name release\n", address);
234 if (!NT_STATUS_IS_OK(status)) {
235 printf("Bad response from %s for name query - %s\n",
236 address, nt_errstr(status));
240 CHECK_NAME(release.out.name, *name);
241 CHECK_VALUE(release.out.rcode, 0);
244 printf("release again\n");
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("query the name to make sure its gone\n");
261 query.in.name = *name;
262 status = nbt_name_query(nbtsock, mem_ctx, &query);
263 if (name->type != NBT_NAME_LOGON &&
264 (nb_flags & NBT_NM_GROUP)) {
265 if (!NT_STATUS_IS_OK(status)) {
266 printf("ERROR: Name query failed after group release - %s\n",
271 if (NT_STATUS_IS_OK(status)) {
272 printf("ERROR: Name query success after release\n");
275 if (!NT_STATUS_EQUAL(status, NT_STATUS_OBJECT_NAME_NOT_FOUND)) {
276 printf("Incorrect response to name query - %s\n", nt_errstr(status));
287 test operations against a WINS server
289 static BOOL nbt_test_wins(TALLOC_CTX *mem_ctx, const char *address)
291 struct nbt_name name;
293 uint32_t r = (uint32_t)(random() % (100000));
295 name.name = talloc_asprintf(mem_ctx, "_TORTURE-%5u", r);
297 name.type = NBT_NAME_CLIENT;
299 ret &= nbt_test_wins_name(mem_ctx, address, &name, NBT_NODE_H);
301 name.type = NBT_NAME_MASTER;
302 ret &= nbt_test_wins_name(mem_ctx, address, &name, NBT_NODE_H);
304 ret &= nbt_test_wins_name(mem_ctx, address, &name, NBT_NODE_H | NBT_NM_GROUP);
306 name.type = NBT_NAME_LOGON;
307 ret &= nbt_test_wins_name(mem_ctx, address, &name, NBT_NODE_H | NBT_NM_GROUP);
309 name.type = NBT_NAME_BROWSER;
310 ret &= nbt_test_wins_name(mem_ctx, address, &name, NBT_NODE_H | NBT_NM_GROUP);
312 name.scope = "example";
314 ret &= nbt_test_wins_name(mem_ctx, address, &name, NBT_NODE_H);
316 name.scope = "example";
318 ret &= nbt_test_wins_name(mem_ctx, address, &name, NBT_NODE_H | NBT_NM_GROUP);
320 name.scope = "foo.example.com";
322 ret &= nbt_test_wins_name(mem_ctx, address, &name, NBT_NODE_H);
324 name.name = talloc_asprintf(mem_ctx, "_T\01-%5u.foo", r);
325 ret &= nbt_test_wins_name(mem_ctx, address, &name, NBT_NODE_H);
328 ret &= nbt_test_wins_name(mem_ctx, address, &name, NBT_NODE_H);
330 name.name = talloc_asprintf(mem_ctx, ".");
331 ret &= nbt_test_wins_name(mem_ctx, address, &name, NBT_NODE_H);
333 name.name = talloc_asprintf(mem_ctx, "%5u-\377\200\300FOO", r);
334 ret &= nbt_test_wins_name(mem_ctx, address, &name, NBT_NODE_H);
342 BOOL torture_nbt_wins(void)
345 struct nbt_name name;
346 TALLOC_CTX *mem_ctx = talloc_new(NULL);
350 make_nbt_name_server(&name, lp_parm_string(-1, "torture", "host"));
352 /* do an initial name resolution to find its IP */
353 status = resolve_name(&name, mem_ctx, &address, NULL);
354 if (!NT_STATUS_IS_OK(status)) {
355 printf("Failed to resolve %s - %s\n",
356 name.name, nt_errstr(status));
357 talloc_free(mem_ctx);
361 ret &= nbt_test_wins(mem_ctx, address);
363 talloc_free(mem_ctx);