| 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 support functions implementation.
|
|---|
| 35 | */
|
|---|
| 36 |
|
|---|
| 37 | #include <stdio.h>
|
|---|
| 38 | #include <err.h>
|
|---|
| 39 |
|
|---|
| 40 | #include <net/socket.h>
|
|---|
| 41 |
|
|---|
| 42 | #include "nettest.h"
|
|---|
| 43 | #include "print_error.h"
|
|---|
| 44 |
|
|---|
| 45 |
|
|---|
| 46 | /** Creates new sockets.
|
|---|
| 47 | *
|
|---|
| 48 | * @param[in] verbose A value indicating whether to print out verbose information.
|
|---|
| 49 | * @param[out] socket_ids A field to store the socket identifiers.
|
|---|
| 50 | * @param[in] sockets The number of sockets to create. Should be at most the size of the field.
|
|---|
| 51 | * @param[in] family The socket address family.
|
|---|
| 52 | * @param[in] type The socket type.
|
|---|
| 53 | * @returns EOK on success.
|
|---|
| 54 | * @returns Other error codes as defined for the socket() function.
|
|---|
| 55 | */
|
|---|
| 56 | int sockets_create(int verbose, int *socket_ids, int sockets, int family, sock_type_t type)
|
|---|
| 57 | {
|
|---|
| 58 | int index;
|
|---|
| 59 |
|
|---|
| 60 | if (verbose)
|
|---|
| 61 | printf("Create\t");
|
|---|
| 62 |
|
|---|
| 63 | fflush(stdout);
|
|---|
| 64 |
|
|---|
| 65 | for (index = 0; index < sockets; index++) {
|
|---|
| 66 | socket_ids[index] = socket(family, type, 0);
|
|---|
| 67 | if (socket_ids[index] < 0) {
|
|---|
| 68 | printf("Socket %d (%d) error:\n", index, socket_ids[index]);
|
|---|
| 69 | socket_print_error(stderr, socket_ids[index], "Socket create: ", "\n");
|
|---|
| 70 | return socket_ids[index];
|
|---|
| 71 | }
|
|---|
| 72 | if (verbose)
|
|---|
| 73 | print_mark(index);
|
|---|
| 74 | }
|
|---|
| 75 |
|
|---|
| 76 | return EOK;
|
|---|
| 77 | }
|
|---|
| 78 |
|
|---|
| 79 | /** Closes sockets.
|
|---|
| 80 | *
|
|---|
| 81 | * @param[in] verbose A value indicating whether to print out verbose information.
|
|---|
| 82 | * @param[in] socket_ids A field of stored socket identifiers.
|
|---|
| 83 | * @param[in] sockets The number of sockets in the field. Should be at most the size of the field.
|
|---|
| 84 | * @returns EOK on success.
|
|---|
| 85 | * @returns Other error codes as defined for the closesocket() function.
|
|---|
| 86 | */
|
|---|
| 87 | int sockets_close(int verbose, int *socket_ids, int sockets)
|
|---|
| 88 | {
|
|---|
| 89 | ERROR_DECLARE;
|
|---|
| 90 |
|
|---|
| 91 | int index;
|
|---|
| 92 |
|
|---|
| 93 | if (verbose)
|
|---|
| 94 | printf("\tClose\t");
|
|---|
| 95 |
|
|---|
| 96 | fflush(stdout);
|
|---|
| 97 |
|
|---|
| 98 | for (index = 0; index < sockets; index++) {
|
|---|
| 99 | if (ERROR_OCCURRED(closesocket(socket_ids[index]))) {
|
|---|
| 100 | printf("Socket %d (%d) error:\n", index, socket_ids[index]);
|
|---|
| 101 | socket_print_error(stderr, ERROR_CODE, "Socket close: ", "\n");
|
|---|
| 102 | return ERROR_CODE;
|
|---|
| 103 | }
|
|---|
| 104 | if (verbose)
|
|---|
| 105 | print_mark(index);
|
|---|
| 106 | }
|
|---|
| 107 |
|
|---|
| 108 | return EOK;
|
|---|
| 109 | }
|
|---|
| 110 |
|
|---|
| 111 | /** Connects sockets.
|
|---|
| 112 | *
|
|---|
| 113 | * @param[in] verbose A value indicating whether to print out verbose information.
|
|---|
| 114 | * @param[in] socket_ids A field of stored socket identifiers.
|
|---|
| 115 | * @param[in] sockets The number of sockets in the field. Should be at most the size of the field.
|
|---|
| 116 | * @param[in] address The destination host address to connect to.
|
|---|
| 117 | * @param[in] addrlen The length of the destination address in bytes.
|
|---|
| 118 | * @returns EOK on success.
|
|---|
| 119 | * @returns Other error codes as defined for the connect() function.
|
|---|
| 120 | */
|
|---|
| 121 | int sockets_connect(int verbose, int *socket_ids, int sockets, struct sockaddr *address, socklen_t addrlen)
|
|---|
| 122 | {
|
|---|
| 123 | ERROR_DECLARE;
|
|---|
| 124 |
|
|---|
| 125 | int index;
|
|---|
| 126 |
|
|---|
| 127 | if (verbose)
|
|---|
| 128 | printf("\tConnect\t");
|
|---|
| 129 |
|
|---|
| 130 | fflush(stdout);
|
|---|
| 131 |
|
|---|
| 132 | for (index = 0; index < sockets; index++) {
|
|---|
| 133 | if (ERROR_OCCURRED(connect(socket_ids[index], address, addrlen))) {
|
|---|
| 134 | socket_print_error(stderr, ERROR_CODE, "Socket connect: ", "\n");
|
|---|
| 135 | return ERROR_CODE;
|
|---|
| 136 | }
|
|---|
| 137 | if (verbose)
|
|---|
| 138 | print_mark(index);
|
|---|
| 139 | }
|
|---|
| 140 |
|
|---|
| 141 | return EOK;
|
|---|
| 142 | }
|
|---|
| 143 |
|
|---|
| 144 | /** Sends data via sockets.
|
|---|
| 145 | *
|
|---|
| 146 | * @param[in] verbose A value indicating whether to print out verbose information.
|
|---|
| 147 | * @param[in] socket_ids A field of stored socket identifiers.
|
|---|
| 148 | * @param[in] sockets The number of sockets in the field. Should be at most the size of the field.
|
|---|
| 149 | * @param[in] address The destination host address to send data to.
|
|---|
| 150 | * @param[in] addrlen The length of the destination address in bytes.
|
|---|
| 151 | * @param[in] data The data to be sent.
|
|---|
| 152 | * @param[in] size The data size in bytes.
|
|---|
| 153 | * @param[in] messages The number of datagrams per socket to be sent.
|
|---|
| 154 | * @returns EOK on success.
|
|---|
| 155 | * @returns Other error codes as defined for the sendto() function.
|
|---|
| 156 | */
|
|---|
| 157 | int sockets_sendto(int verbose, int *socket_ids, int sockets, struct sockaddr *address, socklen_t addrlen, char *data, int size, int messages)
|
|---|
| 158 | {
|
|---|
| 159 | ERROR_DECLARE;
|
|---|
| 160 |
|
|---|
| 161 | int index;
|
|---|
| 162 | int message;
|
|---|
| 163 |
|
|---|
| 164 | if (verbose)
|
|---|
| 165 | printf("\tSendto\t");
|
|---|
| 166 |
|
|---|
| 167 | fflush(stdout);
|
|---|
| 168 |
|
|---|
| 169 | for (index = 0; index < sockets; index++) {
|
|---|
| 170 | for (message = 0; message < messages; message++) {
|
|---|
| 171 | if (ERROR_OCCURRED(sendto(socket_ids[index], data, size, 0, address, addrlen))) {
|
|---|
| 172 | printf("Socket %d (%d), message %d error:\n", index, socket_ids[index], message);
|
|---|
| 173 | socket_print_error(stderr, ERROR_CODE, "Socket send: ", "\n");
|
|---|
| 174 | return ERROR_CODE;
|
|---|
| 175 | }
|
|---|
| 176 | }
|
|---|
| 177 | if (verbose)
|
|---|
| 178 | print_mark(index);
|
|---|
| 179 | }
|
|---|
| 180 |
|
|---|
| 181 | return EOK;
|
|---|
| 182 | }
|
|---|
| 183 |
|
|---|
| 184 | /** Receives data via sockets.
|
|---|
| 185 | *
|
|---|
| 186 | * @param[in] verbose A value indicating whether to print out verbose information.
|
|---|
| 187 | * @param[in] socket_ids A field of stored socket identifiers.
|
|---|
| 188 | * @param[in] sockets The number of sockets in the field. Should be at most the size of the field.
|
|---|
| 189 | * @param[in] address The source host address of received datagrams.
|
|---|
| 190 | * @param[in,out] addrlen The maximum length of the source address in bytes. The actual size of the source address is set instead.
|
|---|
| 191 | * @param[out] data The received data.
|
|---|
| 192 | * @param[in] size The maximum data size in bytes.
|
|---|
| 193 | * @param[in] messages The number of datagrams per socket to be received.
|
|---|
| 194 | * @returns EOK on success.
|
|---|
| 195 | * @returns Other error codes as defined for the recvfrom() function.
|
|---|
| 196 | */
|
|---|
| 197 | int sockets_recvfrom(int verbose, int *socket_ids, int sockets, struct sockaddr *address, socklen_t *addrlen, char *data, int size, int messages)
|
|---|
| 198 | {
|
|---|
| 199 | int value;
|
|---|
| 200 | int index;
|
|---|
| 201 | int message;
|
|---|
| 202 |
|
|---|
| 203 | if (verbose)
|
|---|
| 204 | printf("\tRecvfrom\t");
|
|---|
| 205 |
|
|---|
| 206 | fflush(stdout);
|
|---|
| 207 |
|
|---|
| 208 | for (index = 0; index < sockets; index++) {
|
|---|
| 209 | for (message = 0; message < messages; message++) {
|
|---|
| 210 | value = recvfrom(socket_ids[index], data, size, 0, address, addrlen);
|
|---|
| 211 | if (value < 0) {
|
|---|
| 212 | printf("Socket %d (%d), message %d error:\n", index, socket_ids[index], message);
|
|---|
| 213 | socket_print_error(stderr, value, "Socket receive: ", "\n");
|
|---|
| 214 | return value;
|
|---|
| 215 | }
|
|---|
| 216 | }
|
|---|
| 217 | if (verbose)
|
|---|
| 218 | print_mark(index);
|
|---|
| 219 | }
|
|---|
| 220 | return EOK;
|
|---|
| 221 | }
|
|---|
| 222 |
|
|---|
| 223 | /** Sends and receives data via sockets.
|
|---|
| 224 | *
|
|---|
| 225 | * Each datagram is sent and a reply read consequently.
|
|---|
| 226 | * The next datagram is sent after the reply is received.
|
|---|
| 227 | *
|
|---|
| 228 | * @param[in] verbose A value indicating whether to print out verbose information.
|
|---|
| 229 | * @param[in] socket_ids A field of stored socket identifiers.
|
|---|
| 230 | * @param[in] sockets The number of sockets in the field. Should be at most the size of the field.
|
|---|
| 231 | * @param[in,out] address The destination host address to send data to. The source host address of received datagrams is set instead.
|
|---|
| 232 | * @param[in] addrlen The length of the destination address in bytes.
|
|---|
| 233 | * @param[in,out] data The data to be sent. The received data are set instead.
|
|---|
| 234 | * @param[in] size The data size in bytes.
|
|---|
| 235 | * @param[in] messages The number of datagrams per socket to be received.
|
|---|
| 236 | * @returns EOK on success.
|
|---|
| 237 | * @returns Other error codes as defined for the recvfrom() function.
|
|---|
| 238 | */
|
|---|
| 239 | int sockets_sendto_recvfrom(int verbose, int *socket_ids, int sockets, struct sockaddr *address, socklen_t *addrlen, char *data, int size, int messages)
|
|---|
| 240 | {
|
|---|
| 241 | ERROR_DECLARE;
|
|---|
| 242 |
|
|---|
| 243 | int value;
|
|---|
| 244 | int index;
|
|---|
| 245 | int message;
|
|---|
| 246 |
|
|---|
| 247 | if (verbose)
|
|---|
| 248 | printf("\tSendto and recvfrom\t");
|
|---|
| 249 |
|
|---|
| 250 | fflush(stdout);
|
|---|
| 251 |
|
|---|
| 252 | for (index = 0; index < sockets; index++) {
|
|---|
| 253 | for (message = 0; message < messages; message++) {
|
|---|
| 254 | if (ERROR_OCCURRED(sendto(socket_ids[index], data, size, 0, address, *addrlen))) {
|
|---|
| 255 | printf("Socket %d (%d), message %d error:\n", index, socket_ids[index], message);
|
|---|
| 256 | socket_print_error(stderr, ERROR_CODE, "Socket send: ", "\n");
|
|---|
| 257 | return ERROR_CODE;
|
|---|
| 258 | }
|
|---|
| 259 | value = recvfrom(socket_ids[index], data, size, 0, address, addrlen);
|
|---|
| 260 | if (value < 0) {
|
|---|
| 261 | printf("Socket %d (%d), message %d error:\n", index, socket_ids[index], message);
|
|---|
| 262 | socket_print_error(stderr, value, "Socket receive: ", "\n");
|
|---|
| 263 | return value;
|
|---|
| 264 | }
|
|---|
| 265 | }
|
|---|
| 266 | if (verbose)
|
|---|
| 267 | print_mark(index);
|
|---|
| 268 | }
|
|---|
| 269 |
|
|---|
| 270 | return EOK;
|
|---|
| 271 | }
|
|---|
| 272 |
|
|---|
| 273 | /** Prints a mark.
|
|---|
| 274 | *
|
|---|
| 275 | * If the index is a multiple of ten, a different mark is printed.
|
|---|
| 276 | *
|
|---|
| 277 | * @param[in] index The index of the mark to be printed.
|
|---|
| 278 | */
|
|---|
| 279 | void print_mark(int index)
|
|---|
| 280 | {
|
|---|
| 281 | if ((index + 1) % 10)
|
|---|
| 282 | printf("*");
|
|---|
| 283 | else
|
|---|
| 284 | printf("|");
|
|---|
| 285 | fflush(stdout);
|
|---|
| 286 | }
|
|---|
| 287 |
|
|---|
| 288 | /** @}
|
|---|
| 289 | */
|
|---|