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