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"
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 (StrCaseCmp(v, correct) != 0) { \
37 printf("(%s) Incorrect value %s='%s' - should be '%s'\n", \
38 __location__, #v, v, correct); \
43 test that a server responds correctly to attempted registrations of its name
45 static BOOL nbt_register_own(TALLOC_CTX *mem_ctx, struct nbt_name *name,
48 struct nbt_name_register io;
50 struct nbt_name_socket *nbtsock = nbt_name_socket_init(mem_ctx, NULL);
52 const char *myaddress = iface_best_ip(address);
54 socket_listen(nbtsock->sock, myaddress, 0, 0, 0);
56 printf("Testing name defense to name registration\n");
59 io.in.dest_addr = address;
60 io.in.address = myaddress;
61 io.in.nb_flags = NBT_NODE_B | NBT_NM_ACTIVE;
62 io.in.register_demand = False;
63 io.in.broadcast = True;
64 io.in.multi_homed = False;
69 status = nbt_name_register(nbtsock, mem_ctx, &io);
70 if (NT_STATUS_EQUAL(status, NT_STATUS_IO_TIMEOUT)) {
71 printf("No response from %s for name register\n", address);
74 if (!NT_STATUS_IS_OK(status)) {
75 printf("Bad response from %s for name register - %s\n",
76 address, nt_errstr(status));
80 CHECK_STRING(io.out.name.name, name->name);
81 CHECK_VALUE(io.out.name.type, name->type);
82 CHECK_VALUE(io.out.rcode, NBT_RCODE_ACT);
84 /* check a register demand */
85 io.in.address = myaddress;
86 io.in.register_demand = True;
88 status = nbt_name_register(nbtsock, mem_ctx, &io);
89 if (NT_STATUS_EQUAL(status, NT_STATUS_IO_TIMEOUT)) {
90 printf("No response from %s for name register\n", address);
93 if (!NT_STATUS_IS_OK(status)) {
94 printf("Bad response from %s for name register - %s\n",
95 address, nt_errstr(status));
99 CHECK_STRING(io.out.name.name, name->name);
100 CHECK_VALUE(io.out.name.type, name->type);
101 CHECK_VALUE(io.out.rcode, NBT_RCODE_ACT);
108 test that a server responds correctly to attempted name refresh requests
110 static BOOL nbt_refresh_own(TALLOC_CTX *mem_ctx, struct nbt_name *name,
113 struct nbt_name_refresh io;
115 struct nbt_name_socket *nbtsock = nbt_name_socket_init(mem_ctx, NULL);
117 const char *myaddress = iface_best_ip(address);
119 socket_listen(nbtsock->sock, myaddress, 0, 0, 0);
121 printf("Testing name defense to name refresh\n");
124 io.in.dest_addr = address;
125 io.in.address = myaddress;
126 io.in.nb_flags = NBT_NODE_B | NBT_NM_ACTIVE;
127 io.in.broadcast = False;
132 status = nbt_name_refresh(nbtsock, mem_ctx, &io);
133 if (NT_STATUS_EQUAL(status, NT_STATUS_IO_TIMEOUT)) {
134 printf("No response from %s for name refresh\n", address);
137 if (!NT_STATUS_IS_OK(status)) {
138 printf("Bad response from %s for name refresh - %s\n",
139 address, nt_errstr(status));
143 CHECK_STRING(io.out.name.name, name->name);
144 CHECK_VALUE(io.out.name.type, name->type);
145 CHECK_VALUE(io.out.rcode, NBT_RCODE_ACT);
153 test name registration to a server
155 BOOL torture_nbt_register(void)
158 struct nbt_name name;
159 TALLOC_CTX *mem_ctx = talloc_new(NULL);
163 name.name = strupper_talloc(mem_ctx, lp_parm_string(-1, "torture", "host"));
164 name.type = NBT_NAME_SERVER;
167 /* do an initial name resolution to find its IP */
168 status = resolve_name(&name, mem_ctx, &address);
169 if (!NT_STATUS_IS_OK(status)) {
170 printf("Failed to resolve %s - %s\n",
171 name.name, nt_errstr(status));
172 talloc_free(mem_ctx);
176 ret &= nbt_register_own(mem_ctx, &name, address);
177 ret &= nbt_refresh_own(mem_ctx, &name, address);
179 talloc_free(mem_ctx);