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