2 Unix SMB/CIFS implementation.
4 NBT name registration 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"
27 #define CHECK_VALUE(v, correct) do { \
28 if ((v) != (correct)) { \
29 printf("(%s) Incorrect value %s=%d - should be %d\n", \
30 __location__, #v, v, correct); \
34 #define CHECK_STRING(v, correct) do { \
35 if (StrCaseCmp(v, correct) != 0) { \
36 printf("(%s) Incorrect value %s='%s' - should be '%s'\n", \
37 __location__, #v, v, correct); \
41 #define BOGUS_ADDRESS1 "255.255.255.254"
42 #define BOGUS_ADDRESS2 "255.255.255.253"
45 test that a server responds correctly to attempted registrations of its name
47 static BOOL nbt_register_own(TALLOC_CTX *mem_ctx, struct nbt_name *name,
50 struct nbt_name_register io;
52 struct nbt_name_socket *nbtsock = nbt_name_socket_init(mem_ctx, NULL);
55 printf("Testing name defense to name registration\n");
58 io.in.dest_addr = address;
59 io.in.address = BOGUS_ADDRESS1;
60 io.in.nb_flags = NBT_NODE_B | NBT_NM_ACTIVE;
61 io.in.register_demand = False;
62 io.in.broadcast = True;
63 io.in.multi_homed = False;
68 status = nbt_name_register(nbtsock, mem_ctx, &io);
69 if (NT_STATUS_EQUAL(status, NT_STATUS_IO_TIMEOUT)) {
70 printf("No response from %s for name register\n", address);
73 if (!NT_STATUS_IS_OK(status)) {
74 printf("Bad response from %s for name register - %s\n",
75 address, nt_errstr(status));
79 CHECK_STRING(io.out.name.name, name->name);
80 CHECK_VALUE(io.out.name.type, name->type);
81 CHECK_VALUE(io.out.rcode, NBT_RCODE_ACT);
83 /* check a register demand */
84 io.in.address = BOGUS_ADDRESS2;
85 io.in.register_demand = True;
87 status = nbt_name_register(nbtsock, mem_ctx, &io);
88 if (NT_STATUS_EQUAL(status, NT_STATUS_IO_TIMEOUT)) {
89 printf("No response from %s for name register\n", address);
92 if (!NT_STATUS_IS_OK(status)) {
93 printf("Bad response from %s for name register - %s\n",
94 address, nt_errstr(status));
98 CHECK_STRING(io.out.name.name, name->name);
99 CHECK_VALUE(io.out.name.type, name->type);
100 CHECK_VALUE(io.out.rcode, NBT_RCODE_ACT);
107 test that a server responds correctly to attempted name refresh requests
109 static BOOL nbt_refresh_own(TALLOC_CTX *mem_ctx, struct nbt_name *name,
112 struct nbt_name_refresh io;
114 struct nbt_name_socket *nbtsock = nbt_name_socket_init(mem_ctx, NULL);
117 printf("Testing name defense to name refresh\n");
120 io.in.dest_addr = address;
121 io.in.address = BOGUS_ADDRESS1;
122 io.in.nb_flags = NBT_NODE_B | NBT_NM_ACTIVE;
123 io.in.broadcast = True;
128 status = nbt_name_refresh(nbtsock, mem_ctx, &io);
129 if (NT_STATUS_EQUAL(status, NT_STATUS_IO_TIMEOUT)) {
130 printf("No response from %s for name refresh\n", address);
133 if (!NT_STATUS_IS_OK(status)) {
134 printf("Bad response from %s for name refresh - %s\n",
135 address, nt_errstr(status));
139 CHECK_STRING(io.out.name.name, name->name);
140 CHECK_VALUE(io.out.name.type, name->type);
141 CHECK_VALUE(io.out.rcode, NBT_RCODE_ACT);
148 register names with a WINS server
150 static BOOL nbt_register_wins(TALLOC_CTX *mem_ctx, struct nbt_name *name,
153 struct nbt_name_refresh_wins io;
154 struct nbt_name_query q;
156 struct nbt_name_socket *nbtsock = nbt_name_socket_init(mem_ctx, NULL);
159 printf("Testing name registration to WINS\n");
161 io.in.name.name = talloc_asprintf(mem_ctx, "_TORTURE-%5u",
162 (unsigned)(random() % (100000)));
163 io.in.name.type = NBT_NAME_CLIENT;
164 io.in.name.scope = NULL;
165 io.in.wins_servers = str_list_make(mem_ctx, address, NULL);
166 io.in.addresses = str_list_make(mem_ctx, BOGUS_ADDRESS1, NULL);
167 io.in.nb_flags = NBT_NODE_M | NBT_NM_ACTIVE;
170 status = nbt_name_refresh_wins(nbtsock, mem_ctx, &io);
171 if (NT_STATUS_EQUAL(status, NT_STATUS_IO_TIMEOUT)) {
172 printf("No response from %s for name register\n", address);
175 if (!NT_STATUS_IS_OK(status)) {
176 printf("Bad response from %s for name register - %s\n",
177 address, nt_errstr(status));
181 CHECK_STRING(io.out.wins_server, address);
182 CHECK_VALUE(io.out.rcode, 0);
184 /* query the name to make sure its there */
185 q.in.name = io.in.name;
186 q.in.dest_addr = address;
187 q.in.broadcast = False;
188 q.in.wins_lookup = True;
192 status = nbt_name_query(nbtsock, mem_ctx, &q);
193 if (NT_STATUS_EQUAL(status, NT_STATUS_IO_TIMEOUT)) {
194 printf("No response from %s for name query\n", address);
197 if (!NT_STATUS_IS_OK(status)) {
198 printf("Bad response from %s for name query - %s\n",
199 address, nt_errstr(status));
203 CHECK_STRING(q.out.name.name, q.in.name.name);
204 CHECK_VALUE(q.out.name.type, q.in.name.type);
205 CHECK_VALUE(q.out.num_addrs, 1);
206 CHECK_STRING(q.out.reply_addrs[0], BOGUS_ADDRESS1);
213 test name registration to a server
215 BOOL torture_nbt_register(void)
218 struct nbt_name name;
219 TALLOC_CTX *mem_ctx = talloc_new(NULL);
223 name.name = lp_parm_string(-1, "torture", "host");
224 name.type = NBT_NAME_SERVER;
227 /* do an initial name resolution to find its IP */
228 status = resolve_name(&name, mem_ctx, &address);
229 if (!NT_STATUS_IS_OK(status)) {
230 printf("Failed to resolve %s - %s\n",
231 name.name, nt_errstr(status));
232 talloc_free(mem_ctx);
236 ret &= nbt_register_own(mem_ctx, &name, address);
237 ret &= nbt_refresh_own(mem_ctx, &name, address);
238 ret &= nbt_register_wins(mem_ctx, &name, address);
240 talloc_free(mem_ctx);