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"
28 #include "torture/torture.h"
30 #define CHECK_VALUE(v, correct) do { \
31 if ((v) != (correct)) { \
32 printf("(%s) Incorrect value %s=%d - should be %d\n", \
33 __location__, #v, (int)v, (int)correct); \
37 #define CHECK_STRING(v, correct) do { \
38 if (strcasecmp_m(v, correct) != 0) { \
39 printf("(%s) Incorrect value %s='%s' - should be '%s'\n", \
40 __location__, #v, v, correct); \
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);
54 const char *myaddress = iface_best_ip(address);
55 struct socket_address *socket_address;
57 socket_address = socket_address_from_strings(mem_ctx, nbtsock->sock->backend_name,
59 if (!socket_address) {
63 status = socket_listen(nbtsock->sock, socket_address, 0, 0);
64 if (!NT_STATUS_IS_OK(status)) {
65 printf("socket_listen for nbt_register_own failed: %s\n", nt_errstr(status));
69 printf("Testing name defense to name registration\n");
72 io.in.dest_addr = address;
73 io.in.address = myaddress;
74 io.in.nb_flags = NBT_NODE_B | NBT_NM_ACTIVE;
75 io.in.register_demand = False;
76 io.in.broadcast = True;
77 io.in.multi_homed = False;
82 status = nbt_name_register(nbtsock, mem_ctx, &io);
83 if (NT_STATUS_EQUAL(status, NT_STATUS_IO_TIMEOUT)) {
84 printf("No response from %s for name register\n", address);
87 if (!NT_STATUS_IS_OK(status)) {
88 printf("Bad response from %s for name register - %s\n",
89 address, nt_errstr(status));
93 CHECK_STRING(io.out.name.name, name->name);
94 CHECK_VALUE(io.out.name.type, name->type);
95 CHECK_VALUE(io.out.rcode, NBT_RCODE_ACT);
97 /* check a register demand */
98 io.in.address = myaddress;
99 io.in.register_demand = True;
101 status = nbt_name_register(nbtsock, mem_ctx, &io);
102 if (NT_STATUS_EQUAL(status, NT_STATUS_IO_TIMEOUT)) {
103 printf("No response from %s for name register demand\n", address);
106 if (!NT_STATUS_IS_OK(status)) {
107 printf("Bad response from %s for name register demand - %s\n",
108 address, nt_errstr(status));
112 CHECK_STRING(io.out.name.name, name->name);
113 CHECK_VALUE(io.out.name.type, name->type);
114 CHECK_VALUE(io.out.rcode, NBT_RCODE_ACT);
121 test that a server responds correctly to attempted name refresh requests
123 static BOOL nbt_refresh_own(TALLOC_CTX *mem_ctx, struct nbt_name *name,
126 struct nbt_name_refresh io;
128 struct nbt_name_socket *nbtsock = nbt_name_socket_init(mem_ctx, NULL);
130 const char *myaddress = iface_best_ip(address);
131 struct socket_address *socket_address;
133 socket_address = socket_address_from_strings(mem_ctx, nbtsock->sock->backend_name,
135 if (!socket_address) {
139 status = socket_listen(nbtsock->sock, socket_address, 0, 0);
140 if (!NT_STATUS_IS_OK(status)) {
141 printf("socket_listen for nbt_referesh_own failed: %s\n", nt_errstr(status));
145 printf("Testing name defense to name refresh\n");
148 io.in.dest_addr = address;
149 io.in.address = myaddress;
150 io.in.nb_flags = NBT_NODE_B | NBT_NM_ACTIVE;
151 io.in.broadcast = False;
156 status = nbt_name_refresh(nbtsock, mem_ctx, &io);
157 if (NT_STATUS_EQUAL(status, NT_STATUS_IO_TIMEOUT)) {
158 printf("No response from %s for name refresh\n", address);
161 if (!NT_STATUS_IS_OK(status)) {
162 printf("Bad response from %s for name refresh - %s\n",
163 address, nt_errstr(status));
167 CHECK_STRING(io.out.name.name, name->name);
168 CHECK_VALUE(io.out.name.type, name->type);
169 CHECK_VALUE(io.out.rcode, NBT_RCODE_ACT);
177 test name registration to a server
179 BOOL torture_nbt_register(struct torture_context *torture)
182 struct nbt_name name;
183 TALLOC_CTX *mem_ctx = talloc_new(NULL);
187 make_nbt_name_server(&name, strupper_talloc(mem_ctx, lp_parm_string(-1, "torture", "host")));
189 /* do an initial name resolution to find its IP */
190 status = resolve_name(&name, mem_ctx, &address, NULL);
191 if (!NT_STATUS_IS_OK(status)) {
192 printf("Failed to resolve %s - %s\n",
193 name.name, nt_errstr(status));
194 talloc_free(mem_ctx);
198 ret &= nbt_register_own(mem_ctx, &name, address);
199 ret &= nbt_refresh_own(mem_ctx, &name, address);
201 talloc_free(mem_ctx);