source: mainline/uspace/app/nettest2/nettest2.c@ 8a8a08d1

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 8a8a08d1 was 02a09ed, checked in by Martin Decky <martin@…>, 12 years ago

add basic infrastructure for IPv6 (inactive)
make inet_addr_t a universal address type

  • Property mode set to 100644
File size: 10.4 KB
Line 
1/*
2 * Copyright (c) 2009 Lukas Mejdrech
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 *
9 * - Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * - Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * - The name of the author may not be used to endorse or promote products
15 * derived from this software without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 */
28
29/** @addtogroup nettest
30 * @{
31 */
32
33/** @file
34 * Networking test 2 application - transfer.
35 */
36
37#include "nettest.h"
38#include "print_error.h"
39
40#include <malloc.h>
41#include <stdio.h>
42#include <unistd.h>
43#include <str.h>
44#include <task.h>
45#include <time.h>
46#include <arg_parse.h>
47#include <stdbool.h>
48
49#include <inet/dnsr.h>
50#include <net/in.h>
51#include <net/in6.h>
52#include <net/inet.h>
53#include <net/socket.h>
54#include <net/socket_parse.h>
55
56/** Echo module name. */
57#define NAME "nettest2"
58
59/** Packet data pattern. */
60#define NETTEST2_TEXT "Networking test 2 - transfer"
61
62static uint16_t family = PF_INET;
63static size_t size = 28;
64static bool verbose = false;
65static sock_type_t type = SOCK_DGRAM;
66static int sockets = 10;
67static int messages = 10;
68static uint16_t port = 7;
69
70static void nettest2_print_help(void)
71{
72 printf(
73 "Network Networking test 2 aplication - UDP transfer\n"
74 "Usage: nettest2 [options] host\n"
75 "Where options are:\n"
76 "-f protocol_family | --family=protocol_family\n"
77 "\tThe listenning socket protocol family. Only the PF_INET and "
78 "PF_INET6 are supported.\n"
79 "\n"
80 "-h | --help\n"
81 "\tShow this application help.\n"
82 "\n"
83 "-m count | --messages=count\n"
84 "\tThe number of messages to send and receive per socket. The "
85 "default is 10.\n"
86 "\n"
87 "-n sockets | --sockets=count\n"
88 "\tThe number of sockets to use. The default is 10.\n"
89 "\n"
90 "-p port_number | --port=port_number\n"
91 "\tThe port number the application should send messages to. The "
92 "default is 7.\n"
93 "\n"
94 "-s packet_size | --size=packet_size\n"
95 "\tThe packet data size the application sends. The default is 29 "
96 "bytes.\n"
97 "\n"
98 "-v | --verbose\n"
99 "\tShow all output messages.\n");
100}
101
102/** Fill buffer with the NETTEST2_TEXT pattern.
103 *
104 * @param buffer Data buffer.
105 * @param size Buffer size in bytes.
106 */
107static void nettest2_fill_buffer(char *buffer, size_t size)
108{
109 size_t length = 0;
110 while (size > length + sizeof(NETTEST2_TEXT) - 1) {
111 memcpy(buffer + length, NETTEST2_TEXT,
112 sizeof(NETTEST2_TEXT) - 1);
113 length += sizeof(NETTEST2_TEXT) - 1;
114 }
115
116 memcpy(buffer + length, NETTEST2_TEXT, size - length);
117 buffer[size] = '\0';
118}
119
120/** Parse one command-line option.
121 *
122 * @param argc Number of all command-line arguments.
123 * @param argv All command-line arguments.
124 * @param index Current argument index (in, out).
125 */
126static int nettest2_parse_opt(int argc, char *argv[], int *index)
127{
128 int value;
129 int rc;
130
131 switch (argv[*index][1]) {
132 /*
133 * Short options with only one letter
134 */
135 case 'f':
136 rc = arg_parse_name_int(argc, argv, index, &value, 0,
137 socket_parse_protocol_family);
138 if (rc != EOK)
139 return rc;
140
141 family = (uint16_t) value;
142 break;
143 case 'h':
144 nettest2_print_help();
145 return EOK;
146 case 'm':
147 rc = arg_parse_int(argc, argv, index, &messages, 0);
148 if (rc != EOK)
149 return rc;
150
151 break;
152 case 'n':
153 rc = arg_parse_int(argc, argv, index, &sockets, 0);
154 if (rc != EOK)
155 return rc;
156
157 break;
158 case 'p':
159 rc = arg_parse_int(argc, argv, index, &value, 0);
160 if (rc != EOK)
161 return rc;
162
163 port = (uint16_t) value;
164 break;
165 case 's':
166 rc = arg_parse_int(argc, argv, index, &value, 0);
167 if (rc != EOK)
168 return rc;
169
170 size = (value >= 0) ? (size_t) value : 0;
171 break;
172 case 't':
173 rc = arg_parse_name_int(argc, argv, index, &value, 0,
174 socket_parse_socket_type);
175 if (rc != EOK)
176 return rc;
177
178 type = (sock_type_t) value;
179 break;
180 case 'v':
181 verbose = true;
182 break;
183
184 /*
185 * Long options with double dash ('-')
186 */
187 case '-':
188 if (str_lcmp(argv[*index] + 2, "family=", 7) == 0) {
189 rc = arg_parse_name_int(argc, argv, index, &value, 9,
190 socket_parse_protocol_family);
191 if (rc != EOK)
192 return rc;
193
194 family = (uint16_t) value;
195 } else if (str_lcmp(argv[*index] + 2, "help", 5) == 0) {
196 nettest2_print_help();
197 return EOK;
198 } else if (str_lcmp(argv[*index] + 2, "messages=", 6) == 0) {
199 rc = arg_parse_int(argc, argv, index, &messages, 8);
200 if (rc != EOK)
201 return rc;
202 } else if (str_lcmp(argv[*index] + 2, "sockets=", 6) == 0) {
203 rc = arg_parse_int(argc, argv, index, &sockets, 8);
204 if (rc != EOK)
205 return rc;
206 } else if (str_lcmp(argv[*index] + 2, "port=", 5) == 0) {
207 rc = arg_parse_int(argc, argv, index, &value, 7);
208 if (rc != EOK)
209 return rc;
210
211 port = (uint16_t) value;
212 } else if (str_lcmp(argv[*index] + 2, "type=", 5) == 0) {
213 rc = arg_parse_name_int(argc, argv, index, &value, 7,
214 socket_parse_socket_type);
215 if (rc != EOK)
216 return rc;
217
218 type = (sock_type_t) value;
219 } else if (str_lcmp(argv[*index] + 2, "verbose", 8) == 0) {
220 verbose = 1;
221 } else {
222 nettest2_print_help();
223 return EINVAL;
224 }
225 break;
226 default:
227 nettest2_print_help();
228 return EINVAL;
229 }
230
231 return EOK;
232}
233
234int main(int argc, char *argv[])
235{
236 int index;
237 int rc;
238
239 /*
240 * Parse the command line arguments.
241 *
242 * Stop before the last argument if it does not start with dash ('-')
243 */
244 for (index = 1; (index < argc - 1) || ((index == argc - 1) &&
245 (argv[index][0] == '-')); ++index) {
246 /* Options should start with dash ('-') */
247 if (argv[index][0] == '-') {
248 rc = nettest2_parse_opt(argc, argv, &index);
249 if (rc != EOK)
250 return rc;
251 } else {
252 nettest2_print_help();
253 return EINVAL;
254 }
255 }
256
257 /* The last argument containing the host */
258 if (index >= argc) {
259 printf("Host name missing.\n");
260 nettest2_print_help();
261 return EINVAL;
262 }
263
264 char *addr_s = argv[argc - 1];
265
266 /* Interpret as address */
267 inet_addr_t addr_addr;
268 rc = inet_addr_parse(addr_s, &addr_addr);
269
270 if (rc != EOK) {
271 /* Interpret as a host name */
272 dnsr_hostinfo_t *hinfo = NULL;
273 rc = dnsr_name2host(addr_s, &hinfo);
274
275 if (rc != EOK) {
276 printf("Error resolving host '%s'.\n", addr_s);
277 return EINVAL;
278 }
279
280 addr_addr = hinfo->addr;
281 }
282
283 struct sockaddr_in addr;
284 struct sockaddr_in6 addr6;
285 uint16_t af = inet_addr_sockaddr_in(&addr_addr, &addr, &addr6);
286
287 if (af != family) {
288 printf("Address family does not match explicitly set family.\n");
289 return EINVAL;
290 }
291
292 /* Prepare the address buffer */
293
294 struct sockaddr *address;
295 socklen_t addrlen;
296
297 switch (af) {
298 case AF_INET:
299 addr.sin_port = htons(port);
300 address = (struct sockaddr *) &addr;
301 addrlen = sizeof(addr);
302 break;
303 case AF_INET6:
304 addr6.sin6_port = htons(port);
305 address = (struct sockaddr *) &addr6;
306 addrlen = sizeof(addr6);
307 break;
308 default:
309 fprintf(stderr, "Address family is not supported\n");
310 return EAFNOSUPPORT;
311 }
312
313 /* Check data buffer size. */
314 if (size <= 0) {
315 fprintf(stderr, "Data buffer size too small (%zu). Using 1024 "
316 "bytes instead.\n", size);
317 size = 1024;
318 }
319
320 /*
321 * Prepare the buffer. Allocate size bytes plus one for terminating
322 * null character.
323 */
324 char *data = (char *) malloc(size + 1);
325 if (!data) {
326 fprintf(stderr, "Failed to allocate data buffer.\n");
327 return ENOMEM;
328 }
329
330 /* Fill buffer with a pattern. */
331 nettest2_fill_buffer(data, size);
332
333 /* Check socket count. */
334 if (sockets <= 0) {
335 fprintf(stderr, "Socket count too small (%d). Using "
336 "2 instead.\n", sockets);
337 sockets = 2;
338 }
339
340 /*
341 * Prepare the socket buffer.
342 * Allocate count entries plus the terminating null (\0)
343 */
344 int *socket_ids = (int *) malloc(sizeof(int) * (sockets + 1));
345 if (!socket_ids) {
346 fprintf(stderr, "Failed to allocate receive buffer.\n");
347 return ENOMEM;
348 }
349
350 socket_ids[sockets] = 0;
351
352 if (verbose)
353 printf("Starting tests\n");
354
355 rc = sockets_create(verbose, socket_ids, sockets, family, type);
356 if (rc != EOK)
357 return rc;
358
359 if (type == SOCK_STREAM) {
360 rc = sockets_connect(verbose, socket_ids, sockets,
361 address, addrlen);
362 if (rc != EOK)
363 return rc;
364 }
365
366 if (verbose)
367 printf("\n");
368
369 struct timeval time_before;
370 rc = gettimeofday(&time_before, NULL);
371 if (rc != EOK) {
372 fprintf(stderr, "Get time of day error %d\n", rc);
373 return rc;
374 }
375
376 rc = sockets_sendto_recvfrom(verbose, socket_ids, sockets, address,
377 &addrlen, data, size, messages);
378 if (rc != EOK)
379 return rc;
380
381 struct timeval time_after;
382 rc = gettimeofday(&time_after, NULL);
383 if (rc != EOK) {
384 fprintf(stderr, "Get time of day error %d\n", rc);
385 return rc;
386 }
387
388 if (verbose)
389 printf("\tOK\n");
390
391 printf("sendto + recvfrom tested in %ld microseconds\n",
392 tv_sub(&time_after, &time_before));
393
394 rc = gettimeofday(&time_before, NULL);
395 if (rc != EOK) {
396 fprintf(stderr, "Get time of day error %d\n", rc);
397 return rc;
398 }
399
400 rc = sockets_sendto(verbose, socket_ids, sockets, address, addrlen,
401 data, size, messages);
402 if (rc != EOK)
403 return rc;
404
405 rc = sockets_recvfrom(verbose, socket_ids, sockets, address, &addrlen,
406 data, size, messages);
407 if (rc != EOK)
408 return rc;
409
410 rc = gettimeofday(&time_after, NULL);
411 if (rc != EOK) {
412 fprintf(stderr, "Get time of day error %d\n", rc);
413 return rc;
414 }
415
416 if (verbose)
417 printf("\tOK\n");
418
419 printf("sendto, recvfrom tested in %ld microseconds\n",
420 tv_sub(&time_after, &time_before));
421
422 rc = sockets_close(verbose, socket_ids, sockets);
423 if (rc != EOK)
424 return rc;
425
426 if (verbose)
427 printf("\nExiting\n");
428
429 return EOK;
430}
431
432/** @}
433 */
Note: See TracBrowser for help on using the repository browser.