| 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 |
|
|---|
| 62 | static size_t size;
|
|---|
| 63 | static bool verbose;
|
|---|
| 64 | static sock_type_t type;
|
|---|
| 65 | static int sockets;
|
|---|
| 66 | static int messages;
|
|---|
| 67 | static int family;
|
|---|
| 68 | static uint16_t port;
|
|---|
| 69 |
|
|---|
| 70 | static 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 | */
|
|---|
| 107 | static 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 | */
|
|---|
| 126 | static 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 |
|
|---|
| 234 | int main(int argc, char *argv[])
|
|---|
| 235 | {
|
|---|
| 236 | struct sockaddr *address;
|
|---|
| 237 | struct sockaddr_in address_in;
|
|---|
| 238 | struct sockaddr_in6 address_in6;
|
|---|
| 239 | dnsr_hostinfo_t *hinfo;
|
|---|
| 240 | socklen_t addrlen;
|
|---|
| 241 | uint8_t *address_start;
|
|---|
| 242 |
|
|---|
| 243 | int *socket_ids;
|
|---|
| 244 | char *data;
|
|---|
| 245 | int index;
|
|---|
| 246 | struct timeval time_before;
|
|---|
| 247 | struct timeval time_after;
|
|---|
| 248 |
|
|---|
| 249 | int rc;
|
|---|
| 250 |
|
|---|
| 251 | size = 28;
|
|---|
| 252 | verbose = false;
|
|---|
| 253 | type = SOCK_DGRAM;
|
|---|
| 254 | sockets = 10;
|
|---|
| 255 | messages = 10;
|
|---|
| 256 | family = PF_INET;
|
|---|
| 257 | port = 7;
|
|---|
| 258 |
|
|---|
| 259 | /*
|
|---|
| 260 | * Parse the command line arguments.
|
|---|
| 261 | *
|
|---|
| 262 | * Stop before the last argument if it does not start with dash ('-')
|
|---|
| 263 | */
|
|---|
| 264 | for (index = 1; (index < argc - 1) || ((index == argc - 1) &&
|
|---|
| 265 | (argv[index][0] == '-')); ++index) {
|
|---|
| 266 |
|
|---|
| 267 | /* Options should start with dash ('-') */
|
|---|
| 268 | if (argv[index][0] == '-') {
|
|---|
| 269 | rc = nettest2_parse_opt(argc, argv, &index);
|
|---|
| 270 | if (rc != EOK)
|
|---|
| 271 | return rc;
|
|---|
| 272 | } else {
|
|---|
| 273 | nettest2_print_help();
|
|---|
| 274 | return EINVAL;
|
|---|
| 275 | }
|
|---|
| 276 | }
|
|---|
| 277 |
|
|---|
| 278 | /* If not before the last argument containing the host */
|
|---|
| 279 | if (index >= argc) {
|
|---|
| 280 | printf("Command line error: missing host name\n");
|
|---|
| 281 | nettest2_print_help();
|
|---|
| 282 | return EINVAL;
|
|---|
| 283 | }
|
|---|
| 284 |
|
|---|
| 285 | /* Prepare the address buffer */
|
|---|
| 286 |
|
|---|
| 287 | switch (family) {
|
|---|
| 288 | case PF_INET:
|
|---|
| 289 | address_in.sin_family = AF_INET;
|
|---|
| 290 | address_in.sin_port = htons(port);
|
|---|
| 291 | address = (struct sockaddr *) &address_in;
|
|---|
| 292 | addrlen = sizeof(address_in);
|
|---|
| 293 | address_start = (uint8_t *) &address_in.sin_addr.s_addr;
|
|---|
| 294 | break;
|
|---|
| 295 | case PF_INET6:
|
|---|
| 296 | address_in6.sin6_family = AF_INET6;
|
|---|
| 297 | address_in6.sin6_port = htons(port);
|
|---|
| 298 | address = (struct sockaddr *) &address_in6;
|
|---|
| 299 | addrlen = sizeof(address_in6);
|
|---|
| 300 | address_start = (uint8_t *) &address_in6.sin6_addr.s6_addr;
|
|---|
| 301 | break;
|
|---|
| 302 | default:
|
|---|
| 303 | fprintf(stderr, "Address family is not supported\n");
|
|---|
| 304 | return EAFNOSUPPORT;
|
|---|
| 305 | }
|
|---|
| 306 |
|
|---|
| 307 | /* Parse the last argument which should contain the host/address */
|
|---|
| 308 | rc = inet_pton(family, argv[argc - 1], address_start);
|
|---|
| 309 | if (rc != EOK) {
|
|---|
| 310 | /* Try interpreting as a host name */
|
|---|
| 311 | rc = dnsr_name2host(argv[argc - 1], &hinfo);
|
|---|
| 312 | if (rc != EOK) {
|
|---|
| 313 | printf("Error resolving host '%s'.\n", argv[argc - 1]);
|
|---|
| 314 | return rc;
|
|---|
| 315 | }
|
|---|
| 316 |
|
|---|
| 317 | rc = inet_addr_sockaddr_in(&hinfo->addr, &address_in);
|
|---|
| 318 | if (rc != EOK) {
|
|---|
| 319 | printf("Host '%s' not resolved as IPv4 address.\n", argv[argc - 1]);
|
|---|
| 320 | return rc;
|
|---|
| 321 | }
|
|---|
| 322 | }
|
|---|
| 323 |
|
|---|
| 324 | /* Check data buffer size. */
|
|---|
| 325 | if (size <= 0) {
|
|---|
| 326 | fprintf(stderr, "Data buffer size too small (%zu). Using 1024 "
|
|---|
| 327 | "bytes instead.\n", size);
|
|---|
| 328 | size = 1024;
|
|---|
| 329 | }
|
|---|
| 330 |
|
|---|
| 331 | /*
|
|---|
| 332 | * Prepare the buffer. Allocate size bytes plus one for terminating
|
|---|
| 333 | * null character.
|
|---|
| 334 | */
|
|---|
| 335 | data = (char *) malloc(size + 1);
|
|---|
| 336 | if (!data) {
|
|---|
| 337 | fprintf(stderr, "Failed to allocate data buffer.\n");
|
|---|
| 338 | return ENOMEM;
|
|---|
| 339 | }
|
|---|
| 340 |
|
|---|
| 341 | /* Fill buffer with a pattern. */
|
|---|
| 342 | nettest2_fill_buffer(data, size);
|
|---|
| 343 |
|
|---|
| 344 | /* Check socket count. */
|
|---|
| 345 | if (sockets <= 0) {
|
|---|
| 346 | fprintf(stderr, "Socket count too small (%d). Using "
|
|---|
| 347 | "2 instead.\n", sockets);
|
|---|
| 348 | sockets = 2;
|
|---|
| 349 | }
|
|---|
| 350 |
|
|---|
| 351 | /*
|
|---|
| 352 | * Prepare the socket buffer.
|
|---|
| 353 | * Allocate count entries plus the terminating null (\0)
|
|---|
| 354 | */
|
|---|
| 355 | socket_ids = (int *) malloc(sizeof(int) * (sockets + 1));
|
|---|
| 356 | if (!socket_ids) {
|
|---|
| 357 | fprintf(stderr, "Failed to allocate receive buffer.\n");
|
|---|
| 358 | return ENOMEM;
|
|---|
| 359 | }
|
|---|
| 360 | socket_ids[sockets] = 0;
|
|---|
| 361 |
|
|---|
| 362 | if (verbose)
|
|---|
| 363 | printf("Starting tests\n");
|
|---|
| 364 |
|
|---|
| 365 | rc = sockets_create(verbose, socket_ids, sockets, family, type);
|
|---|
| 366 | if (rc != EOK)
|
|---|
| 367 | return rc;
|
|---|
| 368 |
|
|---|
| 369 | if (type == SOCK_STREAM) {
|
|---|
| 370 | rc = sockets_connect(verbose, socket_ids, sockets,
|
|---|
| 371 | address, addrlen);
|
|---|
| 372 | if (rc != EOK)
|
|---|
| 373 | return rc;
|
|---|
| 374 | }
|
|---|
| 375 |
|
|---|
| 376 | if (verbose)
|
|---|
| 377 | printf("\n");
|
|---|
| 378 |
|
|---|
| 379 | rc = gettimeofday(&time_before, NULL);
|
|---|
| 380 | if (rc != EOK) {
|
|---|
| 381 | fprintf(stderr, "Get time of day error %d\n", rc);
|
|---|
| 382 | return rc;
|
|---|
| 383 | }
|
|---|
| 384 |
|
|---|
| 385 | rc = sockets_sendto_recvfrom(verbose, socket_ids, sockets, address,
|
|---|
| 386 | &addrlen, data, size, messages);
|
|---|
| 387 | if (rc != EOK)
|
|---|
| 388 | return rc;
|
|---|
| 389 |
|
|---|
| 390 | rc = gettimeofday(&time_after, NULL);
|
|---|
| 391 | if (rc != EOK) {
|
|---|
| 392 | fprintf(stderr, "Get time of day error %d\n", rc);
|
|---|
| 393 | return rc;
|
|---|
| 394 | }
|
|---|
| 395 |
|
|---|
| 396 | if (verbose)
|
|---|
| 397 | printf("\tOK\n");
|
|---|
| 398 |
|
|---|
| 399 | printf("sendto + recvfrom tested in %ld microseconds\n",
|
|---|
| 400 | tv_sub(&time_after, &time_before));
|
|---|
| 401 |
|
|---|
| 402 | rc = gettimeofday(&time_before, NULL);
|
|---|
| 403 | if (rc != EOK) {
|
|---|
| 404 | fprintf(stderr, "Get time of day error %d\n", rc);
|
|---|
| 405 | return rc;
|
|---|
| 406 | }
|
|---|
| 407 |
|
|---|
| 408 | rc = sockets_sendto(verbose, socket_ids, sockets, address, addrlen,
|
|---|
| 409 | data, size, messages);
|
|---|
| 410 | if (rc != EOK)
|
|---|
| 411 | return rc;
|
|---|
| 412 |
|
|---|
| 413 | rc = sockets_recvfrom(verbose, socket_ids, sockets, address, &addrlen,
|
|---|
| 414 | data, size, messages);
|
|---|
| 415 | if (rc != EOK)
|
|---|
| 416 | return rc;
|
|---|
| 417 |
|
|---|
| 418 | rc = gettimeofday(&time_after, NULL);
|
|---|
| 419 | if (rc != EOK) {
|
|---|
| 420 | fprintf(stderr, "Get time of day error %d\n", rc);
|
|---|
| 421 | return rc;
|
|---|
| 422 | }
|
|---|
| 423 |
|
|---|
| 424 | if (verbose)
|
|---|
| 425 | printf("\tOK\n");
|
|---|
| 426 |
|
|---|
| 427 | printf("sendto, recvfrom tested in %ld microseconds\n",
|
|---|
| 428 | tv_sub(&time_after, &time_before));
|
|---|
| 429 |
|
|---|
| 430 | rc = sockets_close(verbose, socket_ids, sockets);
|
|---|
| 431 | if (rc != EOK)
|
|---|
| 432 | return rc;
|
|---|
| 433 |
|
|---|
| 434 | if (verbose)
|
|---|
| 435 | printf("\nExiting\n");
|
|---|
| 436 |
|
|---|
| 437 | return EOK;
|
|---|
| 438 | }
|
|---|
| 439 |
|
|---|
| 440 | /** @}
|
|---|
| 441 | */
|
|---|