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); \
50 test operations against a WINS server
52 static BOOL nbt_test_wins_name(TALLOC_CTX *mem_ctx, const char *address,
53 struct nbt_name *name)
55 struct nbt_name_register_wins io;
56 struct nbt_name_query query;
57 struct nbt_name_refresh_wins refresh;
58 struct nbt_name_release release;
60 struct nbt_name_socket *nbtsock = nbt_name_socket_init(mem_ctx, NULL);
62 const char *myaddress = talloc_strdup(mem_ctx, iface_n_ip(0));
64 /* we do the listen here to ensure the WINS server receives the packets from
66 socket_listen(nbtsock->sock, myaddress, 0, 0, 0);
68 printf("Testing name registration to WINS with name %s<%02x> at %s\n",
69 name->name, name->type, myaddress);
71 printf("scope is %s\n", name->scope);
74 printf("release the name\n");
75 release.in.name = *name;
76 release.in.dest_addr = address;
77 release.in.address = myaddress;
78 release.in.nb_flags = NBT_NODE_H;
79 release.in.broadcast = False;
80 release.in.timeout = 3;
81 release.in.retries = 0;
83 status = nbt_name_release(nbtsock, mem_ctx, &release);
84 if (NT_STATUS_EQUAL(status, NT_STATUS_IO_TIMEOUT)) {
85 printf("No response from %s for name release\n", address);
88 if (!NT_STATUS_IS_OK(status)) {
89 printf("Bad response from %s for name query - %s\n",
90 address, nt_errstr(status));
93 CHECK_VALUE(release.out.rcode, 0);
95 printf("register the name\n");
97 io.in.wins_servers = str_list_make(mem_ctx, address, NULL);
98 io.in.addresses = str_list_make(mem_ctx, myaddress, NULL);
99 io.in.nb_flags = NBT_NODE_H;
102 status = nbt_name_register_wins(nbtsock, mem_ctx, &io);
103 if (NT_STATUS_EQUAL(status, NT_STATUS_IO_TIMEOUT)) {
104 printf("No response from %s for name register\n", address);
107 if (!NT_STATUS_IS_OK(status)) {
108 printf("Bad response from %s for name register - %s\n",
109 address, nt_errstr(status));
113 CHECK_STRING(io.out.wins_server, address);
114 CHECK_VALUE(io.out.rcode, 0);
116 printf("query the name to make sure its there\n");
117 query.in.name = *name;
118 query.in.dest_addr = address;
119 query.in.broadcast = False;
120 query.in.wins_lookup = True;
121 query.in.timeout = 3;
122 query.in.retries = 0;
124 status = nbt_name_query(nbtsock, mem_ctx, &query);
125 if (NT_STATUS_EQUAL(status, NT_STATUS_IO_TIMEOUT)) {
126 printf("No response from %s for name query\n", address);
129 if (!NT_STATUS_IS_OK(status)) {
130 printf("Bad response from %s for name query - %s\n",
131 address, nt_errstr(status));
135 CHECK_NAME(query.out.name, *name);
136 CHECK_VALUE(query.out.num_addrs, 1);
137 CHECK_STRING(query.out.reply_addrs[0], myaddress);
140 query.in.name.name = strupper_talloc(mem_ctx, name->name);
141 if (query.in.name.name &&
142 strcmp(query.in.name.name, name->name) != 0) {
143 printf("check case sensitivity\n");
144 status = nbt_name_query(nbtsock, mem_ctx, &query);
145 if (NT_STATUS_EQUAL(status, NT_STATUS_IO_TIMEOUT)) {
146 printf("No response from %s for name query\n", address);
149 if (!NT_STATUS_EQUAL(status, NT_STATUS_OBJECT_NAME_NOT_FOUND)) {
150 printf("Bad response from %s for name query - %s\n",
151 address, nt_errstr(status));
156 query.in.name = *name;
158 query.in.name.scope = strupper_talloc(mem_ctx, name->scope);
160 if (query.in.name.scope &&
161 strcmp(query.in.name.scope, name->scope) != 0) {
162 printf("check case sensitivity on scope\n");
163 status = nbt_name_query(nbtsock, mem_ctx, &query);
164 if (NT_STATUS_EQUAL(status, NT_STATUS_IO_TIMEOUT)) {
165 printf("No response from %s for name query\n", address);
168 if (!NT_STATUS_EQUAL(status, NT_STATUS_OBJECT_NAME_NOT_FOUND)) {
169 printf("Bad response from %s for name query - %s\n",
170 address, nt_errstr(status));
175 printf("refresh the name\n");
176 refresh.in.name = *name;
177 refresh.in.wins_servers = str_list_make(mem_ctx, address, NULL);
178 refresh.in.addresses = str_list_make(mem_ctx, myaddress, NULL);
179 refresh.in.nb_flags = NBT_NODE_H;
180 refresh.in.ttl = 12345;
182 status = nbt_name_refresh_wins(nbtsock, mem_ctx, &refresh);
183 if (NT_STATUS_EQUAL(status, NT_STATUS_IO_TIMEOUT)) {
184 printf("No response from %s for name refresh\n", address);
187 if (!NT_STATUS_IS_OK(status)) {
188 printf("Bad response from %s for name refresh - %s\n",
189 address, nt_errstr(status));
193 CHECK_STRING(io.out.wins_server, address);
194 CHECK_VALUE(io.out.rcode, 0);
196 printf("release the name\n");
197 release.in.name = *name;
198 release.in.dest_addr = address;
199 release.in.address = myaddress;
200 release.in.nb_flags = NBT_NODE_H;
201 release.in.broadcast = False;
202 release.in.timeout = 3;
203 release.in.retries = 0;
205 status = nbt_name_release(nbtsock, mem_ctx, &release);
206 if (NT_STATUS_EQUAL(status, NT_STATUS_IO_TIMEOUT)) {
207 printf("No response from %s for name release\n", address);
210 if (!NT_STATUS_IS_OK(status)) {
211 printf("Bad response from %s for name query - %s\n",
212 address, nt_errstr(status));
216 CHECK_NAME(release.out.name, *name);
217 CHECK_VALUE(release.out.rcode, 0);
220 printf("release again\n");
221 status = nbt_name_release(nbtsock, mem_ctx, &release);
222 if (NT_STATUS_EQUAL(status, NT_STATUS_IO_TIMEOUT)) {
223 printf("No response from %s for name release\n", address);
226 if (!NT_STATUS_IS_OK(status)) {
227 printf("Bad response from %s for name query - %s\n",
228 address, nt_errstr(status));
232 CHECK_NAME(release.out.name, *name);
233 CHECK_VALUE(release.out.rcode, 0);
236 printf("query the name to make sure its gone\n");
237 query.in.name = *name;
238 status = nbt_name_query(nbtsock, mem_ctx, &query);
239 if (NT_STATUS_IS_OK(status)) {
240 printf("ERROR: Name query success after release\n");
243 if (!NT_STATUS_EQUAL(status, NT_STATUS_OBJECT_NAME_NOT_FOUND)) {
244 printf("Incorrect response to name query - %s\n", nt_errstr(status));
254 test operations against a WINS server
256 static BOOL nbt_test_wins(TALLOC_CTX *mem_ctx, const char *address)
258 struct nbt_name name;
260 uint32_t r = (unsigned)(random() % (100000));
262 name.name = talloc_asprintf(mem_ctx, "_TORTURE-%5u", r);
264 name.type = NBT_NAME_CLIENT;
266 ret &= nbt_test_wins_name(mem_ctx, address, &name);
268 name.scope = "example";
270 ret &= nbt_test_wins_name(mem_ctx, address, &name);
272 name.scope = "foo.example.com";
273 ret &= nbt_test_wins_name(mem_ctx, address, &name);
275 name.name = talloc_asprintf(mem_ctx, "_T\01-%5u.foo", r);
276 ret &= nbt_test_wins_name(mem_ctx, address, &name);
279 ret &= nbt_test_wins_name(mem_ctx, address, &name);
281 name.name = talloc_asprintf(mem_ctx, ".");
282 ret &= nbt_test_wins_name(mem_ctx, address, &name);
284 name.name = talloc_asprintf(mem_ctx, "%5u-\377\200\300FOO", r);
285 ret &= nbt_test_wins_name(mem_ctx, address, &name);
293 BOOL torture_nbt_wins(void)
296 struct nbt_name name;
297 TALLOC_CTX *mem_ctx = talloc_new(NULL);
301 name.name = lp_parm_string(-1, "torture", "host");
302 name.type = NBT_NAME_SERVER;
305 /* do an initial name resolution to find its IP */
306 status = resolve_name(&name, mem_ctx, &address);
307 if (!NT_STATUS_IS_OK(status)) {
308 printf("Failed to resolve %s - %s\n",
309 name.name, nt_errstr(status));
310 talloc_free(mem_ctx);
314 ret &= nbt_test_wins(mem_ctx, address);
316 talloc_free(mem_ctx);