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"
30 #include "param/param.h"
37 const char *wins_server;
44 struct wins_state *state;
47 static struct nbt_name generate_name(TALLOC_CTX *tctx, int idx)
50 name.name = talloc_asprintf(tctx, "WINSBench%6u", idx);
56 static void register_handler(struct nbt_name_request *req)
58 struct idx_state *istate = talloc_get_type(req->async.private, struct idx_state);
59 struct wins_state *state = istate->state;
60 struct nbt_name_register io;
63 status = nbt_name_register_recv(req, istate, &io);
64 if (!NT_STATUS_IS_OK(status) || io.out.rcode != NBT_RCODE_OK) {
68 state->registered[istate->idx] = true;
74 generate a registration
76 static void generate_register(struct nbt_name_socket *nbtsock, struct wins_state *state, int idx)
78 struct nbt_name_register io;
79 TALLOC_CTX *tmp_ctx = talloc_new(state);
80 struct nbt_name_request *req;
81 struct idx_state *istate;
83 istate = talloc(nbtsock, struct idx_state);
85 istate->state = state;
87 io.in.name = generate_name(tmp_ctx, idx);
88 io.in.dest_addr = state->wins_server;
89 io.in.address = state->my_ip;
90 io.in.nb_flags = NBT_NODE_H;
91 io.in.register_demand = false;
92 io.in.broadcast = false;
93 io.in.multi_homed = false;
94 io.in.ttl = state->ttl;
98 req = nbt_name_register_send(nbtsock, &io);
100 req->async.fn = register_handler;
101 req->async.private = istate;
103 talloc_free(tmp_ctx);
107 static void release_handler(struct nbt_name_request *req)
109 struct idx_state *istate = talloc_get_type(req->async.private, struct idx_state);
110 struct wins_state *state = istate->state;
111 struct nbt_name_release io;
114 status = nbt_name_release_recv(req, istate, &io);
115 if (state->registered[istate->idx] &&
116 (!NT_STATUS_IS_OK(status) || io.out.rcode != NBT_RCODE_OK)) {
120 state->registered[istate->idx] = false;
126 generate a name release
128 static void generate_release(struct nbt_name_socket *nbtsock, struct wins_state *state, int idx)
130 struct nbt_name_release io;
131 TALLOC_CTX *tmp_ctx = talloc_new(state);
132 struct nbt_name_request *req;
133 struct idx_state *istate;
135 istate = talloc(nbtsock, struct idx_state);
137 istate->state = state;
139 io.in.name = generate_name(tmp_ctx, idx);
140 io.in.dest_addr = state->wins_server;
141 io.in.address = state->my_ip;
142 io.in.nb_flags = NBT_NODE_H;
143 io.in.broadcast = false;
147 req = nbt_name_release_send(nbtsock, &io);
149 req->async.fn = release_handler;
150 req->async.private = istate;
152 talloc_free(tmp_ctx);
156 static void query_handler(struct nbt_name_request *req)
158 struct idx_state *istate = talloc_get_type(req->async.private, struct idx_state);
159 struct wins_state *state = istate->state;
160 struct nbt_name_query io;
163 status = nbt_name_query_recv(req, istate, &io);
164 if (!NT_STATUS_IS_OK(status) && state->registered[istate->idx]) {
173 generate a name query
175 static void generate_query(struct nbt_name_socket *nbtsock, struct wins_state *state, int idx)
177 struct nbt_name_query io;
178 TALLOC_CTX *tmp_ctx = talloc_new(state);
179 struct nbt_name_request *req;
180 struct idx_state *istate;
182 istate = talloc(nbtsock, struct idx_state);
184 istate->state = state;
186 io.in.name = generate_name(tmp_ctx, idx);
187 io.in.dest_addr = state->wins_server;
188 io.in.dest_port = lp_nbt_port(global_loadparm);
189 io.in.broadcast = false;
190 io.in.wins_lookup = true;
194 req = nbt_name_query_send(nbtsock, &io);
196 req->async.fn = query_handler;
197 req->async.private = istate;
199 talloc_free(tmp_ctx);
203 generate one WINS request
205 static void generate_request(struct nbt_name_socket *nbtsock, struct wins_state *state, int idx)
207 if (random() % 5 == 0) {
208 generate_register(nbtsock, state, idx);
212 if (random() % 20 == 0) {
213 generate_release(nbtsock, state, idx);
217 generate_query(nbtsock, state, idx);
221 benchmark simple name queries
223 static bool bench_wins(struct torture_context *tctx)
225 struct nbt_name_socket *nbtsock = nbt_name_socket_init(tctx, NULL);
227 struct timeval tv = timeval_current();
229 int timelimit = torture_setting_int(tctx, "timelimit", 5);
230 struct wins_state *state;
231 extern int torture_entries;
232 struct socket_address *my_ip;
233 struct nbt_name name;
236 if (!torture_nbt_get_name(tctx, &name, &address))
239 state = talloc_zero(nbtsock, struct wins_state);
241 state->num_names = torture_entries;
242 state->registered = talloc_zero_array(state, bool, state->num_names);
243 state->wins_server = address;
244 state->my_ip = talloc_strdup(tctx, iface_best_ip(address));
245 state->ttl = timelimit;
247 my_ip = socket_address_from_strings(nbtsock, nbtsock->sock->backend_name,
250 socket_listen(nbtsock->sock, my_ip, 0, 0);
252 torture_comment(tctx, "Running for %d seconds\n", timelimit);
253 while (timeval_elapsed(&tv) < timelimit) {
254 while (num_sent - (state->pass_count+state->fail_count) < 10) {
255 generate_request(nbtsock, state, num_sent % state->num_names);
257 if (num_sent % 50 == 0) {
258 if (torture_setting_bool(tctx, "progress", true)) {
259 torture_comment(tctx, "%.1f queries per second (%d failures) \r",
260 state->pass_count / timeval_elapsed(&tv),
267 event_loop_once(nbtsock->event_ctx);
270 while (num_sent != (state->pass_count + state->fail_count)) {
271 event_loop_once(nbtsock->event_ctx);
274 torture_comment(tctx, "%.1f queries per second (%d failures) \n",
275 state->pass_count / timeval_elapsed(&tv),
278 talloc_free(nbtsock);
284 benchmark how fast a WINS server can respond to a mixture of
285 registration/refresh/release and name query requests
287 struct torture_suite *torture_bench_wins(TALLOC_CTX *mem_ctx)
289 struct torture_suite *suite = torture_suite_create(mem_ctx,
292 torture_suite_add_simple_test(suite, "wins", bench_wins);