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);
117 printf("refresh the name\n");
118 refresh.in.name = *name;
119 refresh.in.wins_servers = str_list_make(mem_ctx, address, NULL);
120 refresh.in.addresses = str_list_make(mem_ctx, myaddress, NULL);
121 refresh.in.nb_flags = NBT_NODE_H;
122 refresh.in.ttl = 12345;
124 status = nbt_name_refresh_wins(nbtsock, mem_ctx, &refresh);
125 if (NT_STATUS_EQUAL(status, NT_STATUS_IO_TIMEOUT)) {
126 printf("No response from %s for name refresh\n", address);
129 if (!NT_STATUS_IS_OK(status)) {
130 printf("Bad response from %s for name refresh - %s\n",
131 address, nt_errstr(status));
135 CHECK_STRING(io.out.wins_server, address);
136 CHECK_VALUE(io.out.rcode, 0);
138 printf("release the name\n");
139 release.in.name = *name;
140 release.in.dest_addr = address;
141 release.in.address = myaddress;
142 release.in.nb_flags = NBT_NODE_H;
143 release.in.broadcast = False;
144 release.in.timeout = 3;
145 release.in.retries = 0;
147 status = nbt_name_release(nbtsock, mem_ctx, &release);
148 if (NT_STATUS_EQUAL(status, NT_STATUS_IO_TIMEOUT)) {
149 printf("No response from %s for name release\n", address);
152 if (!NT_STATUS_IS_OK(status)) {
153 printf("Bad response from %s for name query - %s\n",
154 address, nt_errstr(status));
158 CHECK_NAME(release.out.name, *name);
159 CHECK_VALUE(release.out.rcode, 0);
162 printf("release again\n");
163 status = nbt_name_release(nbtsock, mem_ctx, &release);
164 if (NT_STATUS_EQUAL(status, NT_STATUS_IO_TIMEOUT)) {
165 printf("No response from %s for name release\n", address);
168 if (!NT_STATUS_IS_OK(status)) {
169 printf("Bad response from %s for name query - %s\n",
170 address, nt_errstr(status));
174 CHECK_NAME(release.out.name, *name);
175 CHECK_VALUE(release.out.rcode, 0);
178 printf("query the name to make sure its gone\n");
179 status = nbt_name_query(nbtsock, mem_ctx, &query);
180 if (NT_STATUS_IS_OK(status)) {
181 printf("ERROR: Name query success after release\n");
184 if (!NT_STATUS_EQUAL(status, NT_STATUS_OBJECT_NAME_NOT_FOUND)) {
185 printf("Incorrect response to name query - %s\n", nt_errstr(status));
195 test operations against a WINS server
197 static BOOL nbt_test_wins(TALLOC_CTX *mem_ctx, const char *address)
199 struct nbt_name name;
201 uint32_t r = (unsigned)(random() % (100000));
203 name.name = talloc_asprintf(mem_ctx, "_TORTURE-%5u", r);
205 name.type = NBT_NAME_CLIENT;
207 ret &= nbt_test_wins_name(mem_ctx, address, &name);
209 name.scope = "example";
211 ret &= nbt_test_wins_name(mem_ctx, address, &name);
213 name.scope = "foo.example.com";
214 ret &= nbt_test_wins_name(mem_ctx, address, &name);
216 name.name = talloc_asprintf(mem_ctx, "_T\01-%5u.foo", r);
217 ret &= nbt_test_wins_name(mem_ctx, address, &name);
220 ret &= nbt_test_wins_name(mem_ctx, address, &name);
222 name.name = talloc_asprintf(mem_ctx, ".");
223 ret &= nbt_test_wins_name(mem_ctx, address, &name);
231 BOOL torture_nbt_wins(void)
234 struct nbt_name name;
235 TALLOC_CTX *mem_ctx = talloc_new(NULL);
239 name.name = lp_parm_string(-1, "torture", "host");
240 name.type = NBT_NAME_SERVER;
243 /* do an initial name resolution to find its IP */
244 status = resolve_name(&name, mem_ctx, &address);
245 if (!NT_STATUS_IS_OK(status)) {
246 printf("Failed to resolve %s - %s\n",
247 name.name, nt_errstr(status));
248 talloc_free(mem_ctx);
252 ret &= nbt_test_wins(mem_ctx, address);
254 talloc_free(mem_ctx);