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