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);
75 io.in.wins_servers = str_list_make(mem_ctx, address, NULL);
76 io.in.addresses = str_list_make(mem_ctx, myaddress, NULL);
77 io.in.nb_flags = NBT_NODE_H;
80 status = nbt_name_register_wins(nbtsock, mem_ctx, &io);
81 if (NT_STATUS_EQUAL(status, NT_STATUS_IO_TIMEOUT)) {
82 printf("No response from %s for name register\n", address);
85 if (!NT_STATUS_IS_OK(status)) {
86 printf("Bad response from %s for name register - %s\n",
87 address, nt_errstr(status));
91 CHECK_STRING(io.out.wins_server, address);
92 CHECK_VALUE(io.out.rcode, 0);
94 printf("query the name to make sure its there\n");
95 query.in.name = *name;
96 query.in.dest_addr = address;
97 query.in.broadcast = False;
98 query.in.wins_lookup = True;
100 query.in.retries = 0;
102 status = nbt_name_query(nbtsock, mem_ctx, &query);
103 if (NT_STATUS_EQUAL(status, NT_STATUS_IO_TIMEOUT)) {
104 printf("No response from %s for name query\n", address);
107 if (!NT_STATUS_IS_OK(status)) {
108 printf("Bad response from %s for name query - %s\n",
109 address, nt_errstr(status));
113 CHECK_NAME(query.out.name, *name);
114 CHECK_VALUE(query.out.num_addrs, 1);
115 CHECK_STRING(query.out.reply_addrs[0], myaddress);
118 query.in.name.name = strupper_talloc(mem_ctx, name->name);
119 if (query.in.name.name &&
120 strcmp(query.in.name.name, name->name) != 0) {
121 printf("check case sensitivity\n");
122 status = nbt_name_query(nbtsock, mem_ctx, &query);
123 if (NT_STATUS_EQUAL(status, NT_STATUS_IO_TIMEOUT)) {
124 printf("No response from %s for name query\n", address);
127 if (!NT_STATUS_EQUAL(status, NT_STATUS_OBJECT_NAME_NOT_FOUND)) {
128 printf("Bad response from %s for name query - %s\n",
129 address, nt_errstr(status));
134 query.in.name = *name;
136 query.in.name.scope = strupper_talloc(mem_ctx, name->scope);
138 if (query.in.name.scope &&
139 strcmp(query.in.name.scope, name->scope) != 0) {
140 printf("check case sensitivity on scope\n");
141 status = nbt_name_query(nbtsock, mem_ctx, &query);
142 if (NT_STATUS_EQUAL(status, NT_STATUS_IO_TIMEOUT)) {
143 printf("No response from %s for name query\n", address);
146 if (!NT_STATUS_EQUAL(status, NT_STATUS_OBJECT_NAME_NOT_FOUND)) {
147 printf("Bad response from %s for name query - %s\n",
148 address, nt_errstr(status));
153 printf("refresh the name\n");
154 refresh.in.name = *name;
155 refresh.in.wins_servers = str_list_make(mem_ctx, address, NULL);
156 refresh.in.addresses = str_list_make(mem_ctx, myaddress, NULL);
157 refresh.in.nb_flags = NBT_NODE_H;
158 refresh.in.ttl = 12345;
160 status = nbt_name_refresh_wins(nbtsock, mem_ctx, &refresh);
161 if (NT_STATUS_EQUAL(status, NT_STATUS_IO_TIMEOUT)) {
162 printf("No response from %s for name refresh\n", address);
165 if (!NT_STATUS_IS_OK(status)) {
166 printf("Bad response from %s for name refresh - %s\n",
167 address, nt_errstr(status));
171 CHECK_STRING(io.out.wins_server, address);
172 CHECK_VALUE(io.out.rcode, 0);
174 printf("release the name\n");
175 release.in.name = *name;
176 release.in.dest_addr = address;
177 release.in.address = myaddress;
178 release.in.nb_flags = NBT_NODE_H;
179 release.in.broadcast = False;
180 release.in.timeout = 3;
181 release.in.retries = 0;
183 status = nbt_name_release(nbtsock, mem_ctx, &release);
184 if (NT_STATUS_EQUAL(status, NT_STATUS_IO_TIMEOUT)) {
185 printf("No response from %s for name release\n", address);
188 if (!NT_STATUS_IS_OK(status)) {
189 printf("Bad response from %s for name query - %s\n",
190 address, nt_errstr(status));
194 CHECK_NAME(release.out.name, *name);
195 CHECK_VALUE(release.out.rcode, 0);
198 printf("release again\n");
199 status = nbt_name_release(nbtsock, mem_ctx, &release);
200 if (NT_STATUS_EQUAL(status, NT_STATUS_IO_TIMEOUT)) {
201 printf("No response from %s for name release\n", address);
204 if (!NT_STATUS_IS_OK(status)) {
205 printf("Bad response from %s for name query - %s\n",
206 address, nt_errstr(status));
210 CHECK_NAME(release.out.name, *name);
211 CHECK_VALUE(release.out.rcode, 0);
214 printf("query the name to make sure its gone\n");
215 query.in.name = *name;
216 status = nbt_name_query(nbtsock, mem_ctx, &query);
217 if (NT_STATUS_IS_OK(status)) {
218 printf("ERROR: Name query success after release\n");
221 if (!NT_STATUS_EQUAL(status, NT_STATUS_OBJECT_NAME_NOT_FOUND)) {
222 printf("Incorrect response to name query - %s\n", nt_errstr(status));
232 test operations against a WINS server
234 static BOOL nbt_test_wins(TALLOC_CTX *mem_ctx, const char *address)
236 struct nbt_name name;
238 uint32_t r = (unsigned)(random() % (100000));
240 name.name = talloc_asprintf(mem_ctx, "_TORTURE-%5u", r);
242 name.type = NBT_NAME_CLIENT;
244 ret &= nbt_test_wins_name(mem_ctx, address, &name);
246 name.scope = "example";
248 ret &= nbt_test_wins_name(mem_ctx, address, &name);
250 name.scope = "foo.example.com";
251 ret &= nbt_test_wins_name(mem_ctx, address, &name);
253 name.name = talloc_asprintf(mem_ctx, "_T\01-%5u.foo", r);
254 ret &= nbt_test_wins_name(mem_ctx, address, &name);
257 ret &= nbt_test_wins_name(mem_ctx, address, &name);
259 name.name = talloc_asprintf(mem_ctx, ".");
260 ret &= nbt_test_wins_name(mem_ctx, address, &name);
262 name.name = talloc_asprintf(mem_ctx, "%5u-\377\200\300FOO", r);
263 ret &= nbt_test_wins_name(mem_ctx, address, &name);
271 BOOL torture_nbt_wins(void)
274 struct nbt_name name;
275 TALLOC_CTX *mem_ctx = talloc_new(NULL);
279 name.name = lp_parm_string(-1, "torture", "host");
280 name.type = NBT_NAME_SERVER;
283 /* do an initial name resolution to find its IP */
284 status = resolve_name(&name, mem_ctx, &address);
285 if (!NT_STATUS_IS_OK(status)) {
286 printf("Failed to resolve %s - %s\n",
287 name.name, nt_errstr(status));
288 talloc_free(mem_ctx);
292 ret &= nbt_test_wins(mem_ctx, address);
294 talloc_free(mem_ctx);