source: mainline/uspace/app/nettest1/nettest1.c@ b5cf742a

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

cstyle (no change in functionality)

  • Property mode set to 100644
File size: 10.9 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 1 application - sockets.
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
48#include <inet/dnsr.h>
49#include <net/in.h>
50#include <net/in6.h>
51#include <net/inet.h>
52#include <net/socket.h>
53#include <net/socket_parse.h>
54
55/** Echo module name. */
56#define NAME "nettest1"
57
58/** Packet data pattern. */
59#define NETTEST1_TEXT "Networking test 1 - sockets"
60
61static int family = PF_INET;
62static sock_type_t type = SOCK_DGRAM;
63static char *data;
64static size_t size = 27;
65static int verbose = 0;
66
67static struct sockaddr *address;
68static socklen_t addrlen;
69
70static int sockets;
71static int messages;
72static uint16_t port;
73
74static void nettest1_print_help(void)
75{
76 printf(
77 "Network Networking test 1 aplication - sockets\n"
78 "Usage: nettest1 [options] host\n"
79 "Where options are:\n"
80 "-f protocol_family | --family=protocol_family\n"
81 "\tThe listenning socket protocol family. Only the PF_INET and "
82 "PF_INET6 are supported.\n"
83 "\n"
84 "-h | --help\n"
85 "\tShow this application help.\n"
86 "\n"
87 "-m count | --messages=count\n"
88 "\tThe number of messages to send and receive per socket. The "
89 "default is 10.\n"
90 "\n"
91 "-n sockets | --sockets=count\n"
92 "\tThe number of sockets to use. The default is 10.\n"
93 "\n"
94 "-p port_number | --port=port_number\n"
95 "\tThe port number the application should send messages to. The "
96 "default is 7.\n"
97 "\n"
98 "-s packet_size | --size=packet_size\n"
99 "\tThe packet data size the application sends. The default is "
100 "28 bytes.\n"
101 "\n"
102 "-v | --verbose\n"
103 "\tShow all output messages.\n");
104}
105
106/** Parse one command-line option.
107 *
108 * @param argc Number of all command-line arguments.
109 * @param argv All command-line arguments.
110 * @param index Current argument index (in, out).
111 */
112static int nettest1_parse_opt(int argc, char *argv[], int *index)
113{
114 int value;
115 int rc;
116
117 switch (argv[*index][1]) {
118 /*
119 * Short options with only one letter
120 */
121 case 'f':
122 rc = arg_parse_name_int(argc, argv, index, &value, 0,
123 socket_parse_protocol_family);
124 if (rc != EOK)
125 return rc;
126
127 family = (uint16_t) value;
128 break;
129 case 'h':
130 nettest1_print_help();
131 return EOK;
132 case 'm':
133 rc = arg_parse_int(argc, argv, index, &messages, 0);
134 if (rc != EOK)
135 return rc;
136
137 break;
138 case 'n':
139 rc = arg_parse_int(argc, argv, index, &sockets, 0);
140 if (rc != EOK)
141 return rc;
142
143 break;
144 case 'p':
145 rc = arg_parse_int(argc, argv, index, &value, 0);
146 if (rc != EOK)
147 return rc;
148
149 port = (uint16_t) value;
150 break;
151 case 's':
152 rc = arg_parse_int(argc, argv, index, &value, 0);
153 if (rc != EOK)
154 return rc;
155
156 size = (value >= 0) ? (size_t) value : 0;
157 break;
158 case 't':
159 rc = arg_parse_name_int(argc, argv, index, &value, 0,
160 socket_parse_socket_type);
161 if (rc != EOK)
162 return rc;
163
164 type = (sock_type_t) value;
165 break;
166 case 'v':
167 verbose = 1;
168 break;
169
170 /*
171 * Long options with double dash ('-')
172 */
173 case '-':
174 if (str_lcmp(argv[*index] + 2, "family=", 7) == 0) {
175 rc = arg_parse_name_int(argc, argv, index, &value, 9,
176 socket_parse_protocol_family);
177 if (rc != EOK)
178 return rc;
179
180 family = (uint16_t) value;
181 } else if (str_lcmp(argv[*index] + 2, "help", 5) == 0) {
182 nettest1_print_help();
183 return EOK;
184 } else if (str_lcmp(argv[*index] + 2, "messages=", 6) == 0) {
185 rc = arg_parse_int(argc, argv, index, &messages, 8);
186 if (rc != EOK)
187 return rc;
188 } else if (str_lcmp(argv[*index] + 2, "sockets=", 6) == 0) {
189 rc = arg_parse_int(argc, argv, index, &sockets, 8);
190 if (rc != EOK)
191 return rc;
192 } else if (str_lcmp(argv[*index] + 2, "port=", 5) == 0) {
193 rc = arg_parse_int(argc, argv, index, &value, 7);
194 if (rc != EOK)
195 return rc;
196
197 port = (uint16_t) value;
198 } else if (str_lcmp(argv[*index] + 2, "type=", 5) == 0) {
199 rc = arg_parse_name_int(argc, argv, index, &value, 7,
200 socket_parse_socket_type);
201 if (rc != EOK)
202 return rc;
203
204 type = (sock_type_t) value;
205 } else if (str_lcmp(argv[*index] + 2, "verbose", 8) == 0) {
206 verbose = 1;
207 } else {
208 nettest1_print_help();
209 return EINVAL;
210 }
211 break;
212 default:
213 nettest1_print_help();
214 return EINVAL;
215 }
216
217 return EOK;
218}
219
220/** Fill buffer with the NETTEST1_TEXT pattern.
221 *
222 * @param buffer Data buffer.
223 * @param size Buffer size in bytes.
224 */
225static void nettest1_fill_buffer(char *buffer, size_t size)
226{
227 size_t length = 0;
228 while (size > length + sizeof(NETTEST1_TEXT) - 1) {
229 memcpy(buffer + length, NETTEST1_TEXT,
230 sizeof(NETTEST1_TEXT) - 1);
231 length += sizeof(NETTEST1_TEXT) - 1;
232 }
233
234 memcpy(buffer + length, NETTEST1_TEXT, size - length);
235 buffer[size] = '\0';
236}
237
238static int nettest1_test(int *socket_ids, int nsockets, int nmessages)
239{
240 if (verbose)
241 printf("%d sockets, %d messages\n", nsockets, nmessages);
242
243 int rc = sockets_create(verbose, socket_ids, nsockets, family, type);
244 if (rc != EOK)
245 return rc;
246
247 if (type == SOCK_STREAM) {
248 rc = sockets_connect(verbose, socket_ids, nsockets, address,
249 addrlen);
250 if (rc != EOK)
251 return rc;
252 }
253
254 rc = sockets_sendto_recvfrom(verbose, socket_ids, nsockets, address,
255 &addrlen, data, size, nmessages);
256 if (rc != EOK)
257 return rc;
258
259 rc = sockets_close(verbose, socket_ids, nsockets);
260 if (rc != EOK)
261 return rc;
262
263 if (verbose)
264 printf("\tOK\n");
265
266 /****/
267
268 rc = sockets_create(verbose, socket_ids, nsockets, family, type);
269 if (rc != EOK)
270 return rc;
271
272 if (type == SOCK_STREAM) {
273 rc = sockets_connect(verbose, socket_ids, nsockets, address,
274 addrlen);
275 if (rc != EOK)
276 return rc;
277 }
278
279 rc = sockets_sendto(verbose, socket_ids, nsockets, address, addrlen,
280 data, size, nmessages);
281 if (rc != EOK)
282 return rc;
283
284 rc = sockets_recvfrom(verbose, socket_ids, nsockets, address, &addrlen,
285 data, size, nmessages);
286 if (rc != EOK)
287 return rc;
288
289 rc = sockets_close(verbose, socket_ids, nsockets);
290 if (rc != EOK)
291 return rc;
292
293 if (verbose)
294 printf("\tOK\n");
295
296 return EOK;
297}
298
299int main(int argc, char *argv[])
300{
301 struct sockaddr_in address_in;
302 struct sockaddr_in6 address_in6;
303 dnsr_hostinfo_t *hinfo;
304 uint8_t *address_start;
305
306 int *socket_ids;
307 int index;
308 struct timeval time_before;
309 struct timeval time_after;
310
311 int rc;
312
313 sockets = 10;
314 messages = 10;
315 port = 7;
316
317 /*
318 * Parse the command line arguments. Stop before the last argument
319 * if it does not start with dash ('-')
320 */
321 for (index = 1; (index < argc - 1) || ((index == argc - 1) && (argv[index][0] == '-')); index++) {
322 /* Options should start with dash ('-') */
323 if (argv[index][0] == '-') {
324 rc = nettest1_parse_opt(argc, argv, &index);
325 if (rc != EOK)
326 return rc;
327 } else {
328 nettest1_print_help();
329 return EINVAL;
330 }
331 }
332
333 /* If not before the last argument containing the host */
334 if (index >= argc) {
335 printf("Command line error: missing host name\n");
336 nettest1_print_help();
337 return EINVAL;
338 }
339
340 /* Prepare the address buffer */
341
342 switch (family) {
343 case PF_INET:
344 address_in.sin_family = AF_INET;
345 address_in.sin_port = htons(port);
346 address = (struct sockaddr *) &address_in;
347 addrlen = sizeof(address_in);
348 address_start = (uint8_t *) &address_in.sin_addr.s_addr;
349 break;
350 case PF_INET6:
351 address_in6.sin6_family = AF_INET6;
352 address_in6.sin6_port = htons(port);
353 address = (struct sockaddr *) &address_in6;
354 addrlen = sizeof(address_in6);
355 address_start = (uint8_t *) &address_in6.sin6_addr.s6_addr;
356 break;
357 default:
358 fprintf(stderr, "Address family is not supported\n");
359 return EAFNOSUPPORT;
360 }
361
362 /* Parse the last argument which should contain the host/address */
363 rc = inet_pton(family, argv[argc - 1], address_start);
364 if (rc != EOK) {
365 /* Try interpreting as a host name */
366 rc = dnsr_name2host(argv[argc - 1], &hinfo);
367 if (rc != EOK) {
368 printf("Error resolving host '%s'.\n", argv[argc - 1]);
369 return rc;
370 }
371
372 rc = inet_addr_sockaddr_in(&hinfo->addr, &address_in);
373 if (rc != EOK) {
374 printf("Host '%s' not resolved as IPv4 address.\n", argv[argc - 1]);
375 return rc;
376 }
377 }
378
379 /* Check data buffer size */
380 if (size <= 0) {
381 fprintf(stderr, "Data buffer size too small (%zu). Using 1024 "
382 "bytes instead.\n", size);
383 size = 1024;
384 }
385
386 /*
387 * Prepare data buffer. Allocate size bytes plus one for the
388 * trailing null character.
389 */
390 data = (char *) malloc(size + 1);
391 if (!data) {
392 fprintf(stderr, "Failed to allocate data buffer.\n");
393 return ENOMEM;
394 }
395 nettest1_fill_buffer(data, size);
396
397 /* Check socket count */
398 if (sockets <= 0) {
399 fprintf(stderr, "Socket count too small (%d). Using "
400 "2 instead.\n", sockets);
401 sockets = 2;
402 }
403
404 /*
405 * Prepare socket buffer. Allocate count fields plus the terminating
406 * null (\0).
407 */
408 socket_ids = (int *) malloc(sizeof(int) * (sockets + 1));
409 if (!socket_ids) {
410 fprintf(stderr, "Failed to allocate receive buffer.\n");
411 return ENOMEM;
412 }
413
414 socket_ids[sockets] = 0;
415
416 if (verbose)
417 printf("Starting tests\n");
418
419 rc = gettimeofday(&time_before, NULL);
420 if (rc != EOK) {
421 fprintf(stderr, "Get time of day error %d\n", rc);
422 return rc;
423 }
424
425 nettest1_test(socket_ids, 1, 1);
426 nettest1_test(socket_ids, 1, messages);
427 nettest1_test(socket_ids, sockets, 1);
428 nettest1_test(socket_ids, sockets, messages);
429
430 rc = gettimeofday(&time_after, NULL);
431 if (rc != EOK) {
432 fprintf(stderr, "Get time of day error %d\n", rc);
433 return rc;
434 }
435
436 printf("Tested in %ld microseconds\n", tv_sub(&time_after,
437 &time_before));
438
439 if (verbose)
440 printf("Exiting\n");
441
442 return EOK;
443}
444
445/** @}
446 */
Note: See TracBrowser for help on using the repository browser.