source: mainline/uspace/app/nettest1/nettest1.c@ 7f9d97f3

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 7f9d97f3 was 7f9d97f3, checked in by Jakub Jermar <jakub@…>, 10 years ago

Make gettimeofday() return the actual microseconds.

Enhance struct tm to also have a field to hold microseconds and make
sure that this information propagates from the RTC driver.

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