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"
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); \
43 test operations against a WINS server
45 static BOOL nbt_test_wins(TALLOC_CTX *mem_ctx, struct nbt_name *name,
48 struct nbt_name_register_wins io;
49 struct nbt_name_query query;
50 struct nbt_name_refresh_wins refresh;
51 struct nbt_name_release release;
53 struct nbt_name_socket *nbtsock = nbt_name_socket_init(mem_ctx, NULL);
55 const char *myaddress = talloc_strdup(mem_ctx, iface_n_ip(0));
56 const char *tname = talloc_asprintf(mem_ctx, "_TORTURE-%5u",
57 (unsigned)(random() % (100000)));
59 /* we do the listen here to ensure the WINS server receives the packets from
61 socket_listen(nbtsock->sock, myaddress, 0, 0, 0);
63 printf("Testing name registration to WINS with name '%s' at %s\n", tname, myaddress);
65 io.in.name.name = tname;
66 io.in.name.type = NBT_NAME_CLIENT;
67 io.in.name.scope = NULL;
68 io.in.wins_servers = str_list_make(mem_ctx, address, NULL);
69 io.in.addresses = str_list_make(mem_ctx, myaddress, NULL);
70 io.in.nb_flags = NBT_NODE_H;
73 status = nbt_name_register_wins(nbtsock, mem_ctx, &io);
74 if (NT_STATUS_EQUAL(status, NT_STATUS_IO_TIMEOUT)) {
75 printf("No response from %s for name register\n", address);
78 if (!NT_STATUS_IS_OK(status)) {
79 printf("Bad response from %s for name register - %s\n",
80 address, nt_errstr(status));
84 CHECK_STRING(io.out.wins_server, address);
85 CHECK_VALUE(io.out.rcode, 0);
87 printf("query the name to make sure its there\n");
88 query.in.name = io.in.name;
89 query.in.dest_addr = address;
90 query.in.broadcast = False;
91 query.in.wins_lookup = True;
95 status = nbt_name_query(nbtsock, mem_ctx, &query);
96 if (NT_STATUS_EQUAL(status, NT_STATUS_IO_TIMEOUT)) {
97 printf("No response from %s for name query\n", address);
100 if (!NT_STATUS_IS_OK(status)) {
101 printf("Bad response from %s for name query - %s\n",
102 address, nt_errstr(status));
106 CHECK_STRING(query.out.name.name, tname);
107 CHECK_VALUE(query.out.name.type, NBT_NAME_CLIENT);
108 CHECK_VALUE(query.out.num_addrs, 1);
109 CHECK_STRING(query.out.reply_addrs[0], myaddress);
111 printf("refresh the name\n");
112 refresh.in.name.name = tname;
113 refresh.in.name.type = NBT_NAME_CLIENT;
114 refresh.in.name.scope = NULL;
115 refresh.in.wins_servers = str_list_make(mem_ctx, address, NULL);
116 refresh.in.addresses = str_list_make(mem_ctx, myaddress, NULL);
117 refresh.in.nb_flags = NBT_NODE_H;
118 refresh.in.ttl = 12345;
120 status = nbt_name_refresh_wins(nbtsock, mem_ctx, &refresh);
121 if (NT_STATUS_EQUAL(status, NT_STATUS_IO_TIMEOUT)) {
122 printf("No response from %s for name refresh\n", address);
125 if (!NT_STATUS_IS_OK(status)) {
126 printf("Bad response from %s for name refresh - %s\n",
127 address, nt_errstr(status));
131 CHECK_STRING(io.out.wins_server, address);
132 CHECK_VALUE(io.out.rcode, 0);
134 printf("release the name\n");
135 release.in.name = io.in.name;
136 release.in.dest_addr = address;
137 release.in.address = myaddress;
138 release.in.nb_flags = NBT_NODE_H;
139 release.in.broadcast = False;
140 release.in.timeout = 3;
141 release.in.retries = 0;
143 status = nbt_name_release(nbtsock, mem_ctx, &release);
144 if (NT_STATUS_EQUAL(status, NT_STATUS_IO_TIMEOUT)) {
145 printf("No response from %s for name release\n", address);
148 if (!NT_STATUS_IS_OK(status)) {
149 printf("Bad response from %s for name query - %s\n",
150 address, nt_errstr(status));
154 CHECK_STRING(release.out.name.name, tname);
155 CHECK_VALUE(release.out.name.type, NBT_NAME_CLIENT);
156 CHECK_VALUE(release.out.rcode, 0);
158 printf("release again\n");
159 status = nbt_name_release(nbtsock, mem_ctx, &release);
160 if (NT_STATUS_EQUAL(status, NT_STATUS_IO_TIMEOUT)) {
161 printf("No response from %s for name release\n", address);
164 if (!NT_STATUS_IS_OK(status)) {
165 printf("Bad response from %s for name query - %s\n",
166 address, nt_errstr(status));
170 CHECK_STRING(release.out.name.name, tname);
171 CHECK_VALUE(release.out.name.type, NBT_NAME_CLIENT);
172 CHECK_VALUE(release.out.rcode, 0);
175 printf("query the name to make sure its gone\n");
176 status = nbt_name_query(nbtsock, mem_ctx, &query);
177 if (NT_STATUS_IS_OK(status)) {
178 printf("ERROR: Name query success after release\n");
181 if (!NT_STATUS_EQUAL(status, NT_STATUS_OBJECT_NAME_NOT_FOUND)) {
182 printf("Incorrect response to name query - %s\n", nt_errstr(status));
193 BOOL torture_nbt_wins(void)
196 struct nbt_name name;
197 TALLOC_CTX *mem_ctx = talloc_new(NULL);
201 name.name = lp_parm_string(-1, "torture", "host");
202 name.type = NBT_NAME_SERVER;
205 /* do an initial name resolution to find its IP */
206 status = resolve_name(&name, mem_ctx, &address);
207 if (!NT_STATUS_IS_OK(status)) {
208 printf("Failed to resolve %s - %s\n",
209 name.name, nt_errstr(status));
210 talloc_free(mem_ctx);
214 ret &= nbt_test_wins(mem_ctx, &name, address);
216 talloc_free(mem_ctx);