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 "libcli/nbt/libnbt.h"
25 #include "librpc/gen_ndr/ndr_nbt.h"
26 #include "lib/socket/socket.h"
28 #define CHECK_VALUE(v, correct) do { \
29 if ((v) != (correct)) { \
30 printf("(%s) Incorrect value %s=%d - should be %d\n", \
31 __location__, #v, v, correct); \
35 #define CHECK_STRING(v, correct) do { \
36 if ((v) != (correct) && \
37 ((v)==NULL || (correct)==NULL || StrCaseCmp(v, correct) != 0)) { \
38 printf("(%s) Incorrect value %s='%s' - should be '%s'\n", \
39 __location__, #v, v, correct); \
43 #define CHECK_NAME(_name, correct) do { \
44 CHECK_STRING((_name).name, (correct).name); \
45 CHECK_VALUE((_name).type, (correct).type); \
46 CHECK_STRING((_name).scope, (correct).scope); \
51 test operations against a WINS server
53 static BOOL nbt_test_wins_name(TALLOC_CTX *mem_ctx, const char *address,
54 struct nbt_name *name, uint16_t nb_flags)
56 struct nbt_name_register_wins io;
57 struct nbt_name_query query;
58 struct nbt_name_refresh_wins refresh;
59 struct nbt_name_release release;
61 struct nbt_name_socket *nbtsock = nbt_name_socket_init(mem_ctx, NULL);
63 const char *myaddress = talloc_strdup(mem_ctx, iface_n_ip(0));
65 /* we do the listen here to ensure the WINS server receives the packets from
67 socket_listen(nbtsock->sock, myaddress, 0, 0, 0);
69 printf("Testing name registration to WINS with name %s at %s nb_flags=0x%x\n",
70 nbt_name_string(mem_ctx, name), myaddress, nb_flags);
72 printf("release the name\n");
73 release.in.name = *name;
74 release.in.dest_addr = address;
75 release.in.address = myaddress;
76 release.in.nb_flags = nb_flags;
77 release.in.broadcast = False;
78 release.in.timeout = 3;
79 release.in.retries = 0;
81 status = nbt_name_release(nbtsock, mem_ctx, &release);
82 if (NT_STATUS_EQUAL(status, NT_STATUS_IO_TIMEOUT)) {
83 printf("No response from %s for name release\n", address);
86 if (!NT_STATUS_IS_OK(status)) {
87 printf("Bad response from %s for name query - %s\n",
88 address, nt_errstr(status));
91 CHECK_VALUE(release.out.rcode, 0);
93 printf("register the name\n");
95 io.in.wins_servers = str_list_make(mem_ctx, address, NULL);
96 io.in.addresses = str_list_make(mem_ctx, myaddress, NULL);
97 io.in.nb_flags = nb_flags;
100 status = nbt_name_register_wins(nbtsock, mem_ctx, &io);
101 if (NT_STATUS_EQUAL(status, NT_STATUS_IO_TIMEOUT)) {
102 printf("No response from %s for name register\n", address);
105 if (!NT_STATUS_IS_OK(status)) {
106 printf("Bad response from %s for name register - %s\n",
107 address, nt_errstr(status));
111 CHECK_STRING(io.out.wins_server, address);
112 CHECK_VALUE(io.out.rcode, 0);
114 if (nb_flags & NBT_NM_GROUP) {
115 printf("Try to register as non-group\n");
116 io.in.nb_flags &= ~NBT_NM_GROUP;
117 status = nbt_name_register_wins(nbtsock, mem_ctx, &io);
118 if (!NT_STATUS_IS_OK(status)) {
119 printf("Bad response from %s for name register - %s\n",
120 address, nt_errstr(status));
123 CHECK_VALUE(io.out.rcode, NBT_RCODE_ACT);
126 printf("query the name to make sure its there\n");
127 query.in.name = *name;
128 query.in.dest_addr = address;
129 query.in.broadcast = False;
130 query.in.wins_lookup = True;
131 query.in.timeout = 3;
132 query.in.retries = 0;
134 status = nbt_name_query(nbtsock, mem_ctx, &query);
135 if (NT_STATUS_EQUAL(status, NT_STATUS_IO_TIMEOUT)) {
136 printf("No response from %s for name query\n", address);
139 if (!NT_STATUS_IS_OK(status)) {
140 printf("Bad response from %s for name query - %s\n",
141 address, nt_errstr(status));
145 CHECK_NAME(query.out.name, *name);
146 CHECK_VALUE(query.out.num_addrs, 1);
147 if (nb_flags & NBT_NM_GROUP) {
148 CHECK_STRING(query.out.reply_addrs[0], "255.255.255.255");
150 CHECK_STRING(query.out.reply_addrs[0], myaddress);
154 query.in.name.name = strupper_talloc(mem_ctx, name->name);
155 if (query.in.name.name &&
156 strcmp(query.in.name.name, name->name) != 0) {
157 printf("check case sensitivity\n");
158 status = nbt_name_query(nbtsock, mem_ctx, &query);
159 if (NT_STATUS_EQUAL(status, NT_STATUS_IO_TIMEOUT)) {
160 printf("No response from %s for name query\n", address);
163 if (!NT_STATUS_EQUAL(status, NT_STATUS_OBJECT_NAME_NOT_FOUND)) {
164 printf("Bad response from %s for name query - %s\n",
165 address, nt_errstr(status));
170 query.in.name = *name;
172 query.in.name.scope = strupper_talloc(mem_ctx, name->scope);
174 if (query.in.name.scope &&
175 strcmp(query.in.name.scope, name->scope) != 0) {
176 printf("check case sensitivity on scope\n");
177 status = nbt_name_query(nbtsock, mem_ctx, &query);
178 if (NT_STATUS_EQUAL(status, NT_STATUS_IO_TIMEOUT)) {
179 printf("No response from %s for name query\n", address);
182 if (!NT_STATUS_EQUAL(status, NT_STATUS_OBJECT_NAME_NOT_FOUND)) {
183 printf("Bad response from %s for name query - %s\n",
184 address, nt_errstr(status));
189 printf("refresh the name\n");
190 refresh.in.name = *name;
191 refresh.in.wins_servers = str_list_make(mem_ctx, address, NULL);
192 refresh.in.addresses = str_list_make(mem_ctx, myaddress, NULL);
193 refresh.in.nb_flags = nb_flags;
194 refresh.in.ttl = 12345;
196 status = nbt_name_refresh_wins(nbtsock, mem_ctx, &refresh);
197 if (NT_STATUS_EQUAL(status, NT_STATUS_IO_TIMEOUT)) {
198 printf("No response from %s for name refresh\n", address);
201 if (!NT_STATUS_IS_OK(status)) {
202 printf("Bad response from %s for name refresh - %s\n",
203 address, nt_errstr(status));
207 CHECK_STRING(refresh.out.wins_server, address);
208 CHECK_VALUE(refresh.out.rcode, 0);
210 printf("release the name\n");
211 release.in.name = *name;
212 release.in.dest_addr = address;
213 release.in.address = myaddress;
214 release.in.nb_flags = nb_flags;
215 release.in.broadcast = False;
216 release.in.timeout = 3;
217 release.in.retries = 0;
219 status = nbt_name_release(nbtsock, mem_ctx, &release);
220 if (NT_STATUS_EQUAL(status, NT_STATUS_IO_TIMEOUT)) {
221 printf("No response from %s for name release\n", address);
224 if (!NT_STATUS_IS_OK(status)) {
225 printf("Bad response from %s for name query - %s\n",
226 address, nt_errstr(status));
230 CHECK_NAME(release.out.name, *name);
231 CHECK_VALUE(release.out.rcode, 0);
234 printf("release again\n");
235 status = nbt_name_release(nbtsock, mem_ctx, &release);
236 if (NT_STATUS_EQUAL(status, NT_STATUS_IO_TIMEOUT)) {
237 printf("No response from %s for name release\n", address);
240 if (!NT_STATUS_IS_OK(status)) {
241 printf("Bad response from %s for name query - %s\n",
242 address, nt_errstr(status));
246 CHECK_NAME(release.out.name, *name);
247 CHECK_VALUE(release.out.rcode, 0);
250 printf("query the name to make sure its gone\n");
251 query.in.name = *name;
252 status = nbt_name_query(nbtsock, mem_ctx, &query);
253 if (nb_flags & NBT_NM_GROUP) {
254 if (!NT_STATUS_IS_OK(status)) {
255 printf("ERROR: Name query failed after group release - %s\n",
260 if (NT_STATUS_IS_OK(status)) {
261 printf("ERROR: Name query success after release\n");
264 if (!NT_STATUS_EQUAL(status, NT_STATUS_OBJECT_NAME_NOT_FOUND)) {
265 printf("Incorrect response to name query - %s\n", nt_errstr(status));
276 test operations against a WINS server
278 static BOOL nbt_test_wins(TALLOC_CTX *mem_ctx, const char *address)
280 struct nbt_name name;
282 uint32_t r = (unsigned)(random() % (100000));
284 name.name = talloc_asprintf(mem_ctx, "_TORTURE-%5u", r);
286 name.type = NBT_NAME_CLIENT;
288 ret &= nbt_test_wins_name(mem_ctx, address, &name, NBT_NODE_H);
290 name.scope = "example";
292 ret &= nbt_test_wins_name(mem_ctx, address, &name, NBT_NODE_H);
294 name.scope = "example";
296 ret &= nbt_test_wins_name(mem_ctx, address, &name, NBT_NODE_H | NBT_NM_GROUP);
298 name.scope = "foo.example.com";
300 ret &= nbt_test_wins_name(mem_ctx, address, &name, NBT_NODE_H);
302 name.name = talloc_asprintf(mem_ctx, "_T\01-%5u.foo", r);
303 ret &= nbt_test_wins_name(mem_ctx, address, &name, NBT_NODE_H);
306 ret &= nbt_test_wins_name(mem_ctx, address, &name, NBT_NODE_H);
308 name.name = talloc_asprintf(mem_ctx, ".");
309 ret &= nbt_test_wins_name(mem_ctx, address, &name, NBT_NODE_H);
311 name.name = talloc_asprintf(mem_ctx, "%5u-\377\200\300FOO", r);
312 ret &= nbt_test_wins_name(mem_ctx, address, &name, NBT_NODE_H);
320 BOOL torture_nbt_wins(void)
323 struct nbt_name name;
324 TALLOC_CTX *mem_ctx = talloc_new(NULL);
328 name.name = lp_parm_string(-1, "torture", "host");
329 name.type = NBT_NAME_SERVER;
332 /* do an initial name resolution to find its IP */
333 status = resolve_name(&name, mem_ctx, &address);
334 if (!NT_STATUS_IS_OK(status)) {
335 printf("Failed to resolve %s - %s\n",
336 name.name, nt_errstr(status));
337 talloc_free(mem_ctx);
341 ret &= nbt_test_wins(mem_ctx, address);
343 talloc_free(mem_ctx);