2 Unix SMB/CIFS implementation.
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 3 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, see <http://www.gnu.org/licenses/>.
23 #include "lib/events/events.h"
24 #include "lib/socket/socket.h"
25 #include "libcli/resolve/resolve.h"
26 #include "system/network.h"
27 #include "lib/socket/netif.h"
28 #include "torture/torture.h"
29 #include "torture/nbt/proto.h"
36 const char *wins_server;
43 struct wins_state *state;
46 struct nbt_name generate_name(TALLOC_CTX *tctx, int idx)
49 name.name = talloc_asprintf(tctx, "WINSBench%6u", idx);
55 static void register_handler(struct nbt_name_request *req)
57 struct idx_state *istate = talloc_get_type(req->async.private, struct idx_state);
58 struct wins_state *state = istate->state;
59 struct nbt_name_register io;
62 status = nbt_name_register_recv(req, istate, &io);
63 if (!NT_STATUS_IS_OK(status) || io.out.rcode != NBT_RCODE_OK) {
67 state->registered[istate->idx] = true;
73 generate a registration
75 static void generate_register(struct nbt_name_socket *nbtsock, struct wins_state *state, int idx)
77 struct nbt_name_register io;
78 TALLOC_CTX *tmp_ctx = talloc_new(state);
79 struct nbt_name_request *req;
80 struct idx_state *istate;
82 istate = talloc(nbtsock, struct idx_state);
84 istate->state = state;
86 io.in.name = generate_name(tmp_ctx, idx);
87 io.in.dest_addr = state->wins_server;
88 io.in.address = state->my_ip;
89 io.in.nb_flags = NBT_NODE_H;
90 io.in.register_demand = false;
91 io.in.broadcast = false;
92 io.in.multi_homed = false;
93 io.in.ttl = state->ttl;
97 req = nbt_name_register_send(nbtsock, &io);
99 req->async.fn = register_handler;
100 req->async.private = istate;
102 talloc_free(tmp_ctx);
106 static void release_handler(struct nbt_name_request *req)
108 struct idx_state *istate = talloc_get_type(req->async.private, struct idx_state);
109 struct wins_state *state = istate->state;
110 struct nbt_name_release io;
113 status = nbt_name_release_recv(req, istate, &io);
114 if (state->registered[istate->idx] &&
115 (!NT_STATUS_IS_OK(status) || io.out.rcode != NBT_RCODE_OK)) {
119 state->registered[istate->idx] = false;
125 generate a name release
127 static void generate_release(struct nbt_name_socket *nbtsock, struct wins_state *state, int idx)
129 struct nbt_name_release io;
130 TALLOC_CTX *tmp_ctx = talloc_new(state);
131 struct nbt_name_request *req;
132 struct idx_state *istate;
134 istate = talloc(nbtsock, struct idx_state);
136 istate->state = state;
138 io.in.name = generate_name(tmp_ctx, idx);
139 io.in.dest_addr = state->wins_server;
140 io.in.address = state->my_ip;
141 io.in.nb_flags = NBT_NODE_H;
142 io.in.broadcast = false;
146 req = nbt_name_release_send(nbtsock, &io);
148 req->async.fn = release_handler;
149 req->async.private = istate;
151 talloc_free(tmp_ctx);
155 static void query_handler(struct nbt_name_request *req)
157 struct idx_state *istate = talloc_get_type(req->async.private, struct idx_state);
158 struct wins_state *state = istate->state;
159 struct nbt_name_query io;
162 status = nbt_name_query_recv(req, istate, &io);
163 if (!NT_STATUS_IS_OK(status) && state->registered[istate->idx]) {
172 generate a name query
174 static void generate_query(struct nbt_name_socket *nbtsock, struct wins_state *state, int idx)
176 struct nbt_name_query io;
177 TALLOC_CTX *tmp_ctx = talloc_new(state);
178 struct nbt_name_request *req;
179 struct idx_state *istate;
181 istate = talloc(nbtsock, struct idx_state);
183 istate->state = state;
185 io.in.name = generate_name(tmp_ctx, idx);
186 io.in.dest_addr = state->wins_server;
187 io.in.broadcast = false;
188 io.in.wins_lookup = true;
192 req = nbt_name_query_send(nbtsock, &io);
194 req->async.fn = query_handler;
195 req->async.private = istate;
197 talloc_free(tmp_ctx);
201 generate one WINS request
203 static void generate_request(struct nbt_name_socket *nbtsock, struct wins_state *state, int idx)
205 if (random() % 5 == 0) {
206 generate_register(nbtsock, state, idx);
210 if (random() % 20 == 0) {
211 generate_release(nbtsock, state, idx);
215 generate_query(nbtsock, state, idx);
219 benchmark simple name queries
221 static bool bench_wins(struct torture_context *tctx)
223 struct nbt_name_socket *nbtsock = nbt_name_socket_init(tctx, NULL);
225 struct timeval tv = timeval_current();
227 int timelimit = torture_setting_int(tctx, "timelimit", 5);
228 struct wins_state *state;
229 extern int torture_entries;
230 struct socket_address *my_ip;
231 struct nbt_name name;
234 if (!torture_nbt_get_name(tctx, &name, &address))
237 state = talloc_zero(nbtsock, struct wins_state);
239 state->num_names = torture_entries;
240 state->registered = talloc_zero_array(state, bool, state->num_names);
241 state->wins_server = address;
242 state->my_ip = talloc_strdup(tctx, iface_best_ip(address));
243 state->ttl = timelimit;
245 my_ip = socket_address_from_strings(nbtsock, nbtsock->sock->backend_name,
248 socket_listen(nbtsock->sock, my_ip, 0, 0);
250 torture_comment(tctx, "Running for %d seconds\n", timelimit);
251 while (timeval_elapsed(&tv) < timelimit) {
252 while (num_sent - (state->pass_count+state->fail_count) < 10) {
253 generate_request(nbtsock, state, num_sent % state->num_names);
255 if (num_sent % 50 == 0) {
256 if (torture_setting_bool(tctx, "progress", true)) {
257 torture_comment(tctx, "%.1f queries per second (%d failures) \r",
258 state->pass_count / timeval_elapsed(&tv),
265 event_loop_once(nbtsock->event_ctx);
268 while (num_sent != (state->pass_count + state->fail_count)) {
269 event_loop_once(nbtsock->event_ctx);
272 torture_comment(tctx, "%.1f queries per second (%d failures) \n",
273 state->pass_count / timeval_elapsed(&tv),
276 talloc_free(nbtsock);
282 benchmark how fast a WINS server can respond to a mixture of
283 registration/refresh/release and name query requests
285 struct torture_suite *torture_bench_wins(void)
287 struct torture_suite *suite = torture_suite_create(
288 talloc_autofree_context(),
291 torture_suite_add_simple_test(suite, "wins", bench_wins);