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 "lib/socket/socket.h"
25 #include "libcli/resolve/resolve.h"
26 #include "system/network.h"
27 #include "netif/netif.h"
29 #define CHECK_VALUE(v, correct) do { \
30 if ((v) != (correct)) { \
31 printf("(%s) Incorrect value %s=%d - should be %d\n", \
32 __location__, #v, (int)v, (int)correct); \
36 #define CHECK_STRING(v, correct) do { \
37 if (strcasecmp_m(v, correct) != 0) { \
38 printf("(%s) Incorrect value %s='%s' - should be '%s'\n", \
39 __location__, #v, v, correct); \
44 test that a server responds correctly to attempted registrations of its name
46 static BOOL nbt_register_own(TALLOC_CTX *mem_ctx, struct nbt_name *name,
49 struct nbt_name_register io;
51 struct nbt_name_socket *nbtsock = nbt_name_socket_init(mem_ctx, NULL);
53 const char *myaddress = iface_best_ip(address);
54 struct socket_address *socket_address;
56 socket_address = socket_address_from_strings(mem_ctx, nbtsock->sock->backend_name,
58 if (!socket_address) {
62 status = socket_listen(nbtsock->sock, socket_address, 0, 0);
63 if (!NT_STATUS_IS_OK(status)) {
64 printf("socket_listen for nbt_register_own failed: %s\n", nt_errstr(status));
68 printf("Testing name defense to name registration\n");
71 io.in.dest_addr = address;
72 io.in.address = myaddress;
73 io.in.nb_flags = NBT_NODE_B | NBT_NM_ACTIVE;
74 io.in.register_demand = False;
75 io.in.broadcast = True;
76 io.in.multi_homed = False;
81 status = nbt_name_register(nbtsock, mem_ctx, &io);
82 if (NT_STATUS_EQUAL(status, NT_STATUS_IO_TIMEOUT)) {
83 printf("No response from %s for name register\n", address);
86 if (!NT_STATUS_IS_OK(status)) {
87 printf("Bad response from %s for name register - %s\n",
88 address, nt_errstr(status));
92 CHECK_STRING(io.out.name.name, name->name);
93 CHECK_VALUE(io.out.name.type, name->type);
94 CHECK_VALUE(io.out.rcode, NBT_RCODE_ACT);
96 /* check a register demand */
97 io.in.address = myaddress;
98 io.in.register_demand = True;
100 status = nbt_name_register(nbtsock, mem_ctx, &io);
101 if (NT_STATUS_EQUAL(status, NT_STATUS_IO_TIMEOUT)) {
102 printf("No response from %s for name register demand\n", address);
105 if (!NT_STATUS_IS_OK(status)) {
106 printf("Bad response from %s for name register demand - %s\n",
107 address, nt_errstr(status));
111 CHECK_STRING(io.out.name.name, name->name);
112 CHECK_VALUE(io.out.name.type, name->type);
113 CHECK_VALUE(io.out.rcode, NBT_RCODE_ACT);
120 test that a server responds correctly to attempted name refresh requests
122 static BOOL nbt_refresh_own(TALLOC_CTX *mem_ctx, struct nbt_name *name,
125 struct nbt_name_refresh io;
127 struct nbt_name_socket *nbtsock = nbt_name_socket_init(mem_ctx, NULL);
129 const char *myaddress = iface_best_ip(address);
130 struct socket_address *socket_address;
132 socket_address = socket_address_from_strings(mem_ctx, nbtsock->sock->backend_name,
134 if (!socket_address) {
138 status = socket_listen(nbtsock->sock, socket_address, 0, 0);
139 if (!NT_STATUS_IS_OK(status)) {
140 printf("socket_listen for nbt_referesh_own failed: %s\n", nt_errstr(status));
144 printf("Testing name defense to name refresh\n");
147 io.in.dest_addr = address;
148 io.in.address = myaddress;
149 io.in.nb_flags = NBT_NODE_B | NBT_NM_ACTIVE;
150 io.in.broadcast = False;
155 status = nbt_name_refresh(nbtsock, mem_ctx, &io);
156 if (NT_STATUS_EQUAL(status, NT_STATUS_IO_TIMEOUT)) {
157 printf("No response from %s for name refresh\n", address);
160 if (!NT_STATUS_IS_OK(status)) {
161 printf("Bad response from %s for name refresh - %s\n",
162 address, nt_errstr(status));
166 CHECK_STRING(io.out.name.name, name->name);
167 CHECK_VALUE(io.out.name.type, name->type);
168 CHECK_VALUE(io.out.rcode, NBT_RCODE_ACT);
176 test name registration to a server
178 BOOL torture_nbt_register(void)
181 struct nbt_name name;
182 TALLOC_CTX *mem_ctx = talloc_new(NULL);
186 make_nbt_name_server(&name, strupper_talloc(mem_ctx, lp_parm_string(-1, "torture", "host")));
188 /* do an initial name resolution to find its IP */
189 status = resolve_name(&name, mem_ctx, &address, NULL);
190 if (!NT_STATUS_IS_OK(status)) {
191 printf("Failed to resolve %s - %s\n",
192 name.name, nt_errstr(status));
193 talloc_free(mem_ctx);
197 ret &= nbt_register_own(mem_ctx, &name, address);
198 ret &= nbt_refresh_own(mem_ctx, &name, address);
200 talloc_free(mem_ctx);