| 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 <net/socket.h> | 
|---|
| 39 |  | 
|---|
| 40 | #include "nettest.h" | 
|---|
| 41 | #include "print_error.h" | 
|---|
| 42 |  | 
|---|
| 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. | 
|---|
| 51 | * @return EOK on success. | 
|---|
| 52 | * @return Other error codes as defined for the socket() function. | 
|---|
| 53 | */ | 
|---|
| 54 | int sockets_create(int verbose, int *socket_ids, int sockets, int family, sock_type_t type) | 
|---|
| 55 | { | 
|---|
| 56 | int index; | 
|---|
| 57 |  | 
|---|
| 58 | if (verbose) | 
|---|
| 59 | printf("Create\t"); | 
|---|
| 60 |  | 
|---|
| 61 | fflush(stdout); | 
|---|
| 62 |  | 
|---|
| 63 | for (index = 0; index < sockets; index++) { | 
|---|
| 64 | socket_ids[index] = socket(family, type, 0); | 
|---|
| 65 | if (socket_ids[index] < 0) { | 
|---|
| 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 | } | 
|---|
| 70 | if (verbose) | 
|---|
| 71 | print_mark(index); | 
|---|
| 72 | } | 
|---|
| 73 |  | 
|---|
| 74 | return EOK; | 
|---|
| 75 | } | 
|---|
| 76 |  | 
|---|
| 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. | 
|---|
| 82 | * @return EOK on success. | 
|---|
| 83 | * @return Other error codes as defined for the closesocket() function. | 
|---|
| 84 | */ | 
|---|
| 85 | int sockets_close(int verbose, int *socket_ids, int sockets) | 
|---|
| 86 | { | 
|---|
| 87 | int index; | 
|---|
| 88 | int rc; | 
|---|
| 89 |  | 
|---|
| 90 | if (verbose) | 
|---|
| 91 | printf("\tClose\t"); | 
|---|
| 92 |  | 
|---|
| 93 | fflush(stdout); | 
|---|
| 94 |  | 
|---|
| 95 | for (index = 0; index < sockets; index++) { | 
|---|
| 96 | rc = closesocket(socket_ids[index]); | 
|---|
| 97 | if (rc != EOK) { | 
|---|
| 98 | printf("Socket %d (%d) error:\n", index, socket_ids[index]); | 
|---|
| 99 | socket_print_error(stderr, rc, "Socket close: ", "\n"); | 
|---|
| 100 | return rc; | 
|---|
| 101 | } | 
|---|
| 102 | if (verbose) | 
|---|
| 103 | print_mark(index); | 
|---|
| 104 | } | 
|---|
| 105 |  | 
|---|
| 106 | return EOK; | 
|---|
| 107 | } | 
|---|
| 108 |  | 
|---|
| 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. | 
|---|
| 116 | * @return EOK on success. | 
|---|
| 117 | * @return Other error codes as defined for the connect() function. | 
|---|
| 118 | */ | 
|---|
| 119 | int sockets_connect(int verbose, int *socket_ids, int sockets, struct sockaddr *address, socklen_t addrlen) | 
|---|
| 120 | { | 
|---|
| 121 | int index; | 
|---|
| 122 | int rc; | 
|---|
| 123 |  | 
|---|
| 124 | if (verbose) | 
|---|
| 125 | printf("\tConnect\t"); | 
|---|
| 126 |  | 
|---|
| 127 | fflush(stdout); | 
|---|
| 128 |  | 
|---|
| 129 | for (index = 0; index < sockets; index++) { | 
|---|
| 130 | rc = connect(socket_ids[index], address, addrlen); | 
|---|
| 131 | if (rc != EOK) { | 
|---|
| 132 | socket_print_error(stderr, rc, "Socket connect: ", "\n"); | 
|---|
| 133 | return rc; | 
|---|
| 134 | } | 
|---|
| 135 | if (verbose) | 
|---|
| 136 | print_mark(index); | 
|---|
| 137 | } | 
|---|
| 138 |  | 
|---|
| 139 | return EOK; | 
|---|
| 140 | } | 
|---|
| 141 |  | 
|---|
| 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. | 
|---|
| 152 | * @return EOK on success. | 
|---|
| 153 | * @return Other error codes as defined for the sendto() function. | 
|---|
| 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 | { | 
|---|
| 157 | int index; | 
|---|
| 158 | int message; | 
|---|
| 159 | int rc; | 
|---|
| 160 |  | 
|---|
| 161 | if (verbose) | 
|---|
| 162 | printf("\tSendto\t"); | 
|---|
| 163 |  | 
|---|
| 164 | fflush(stdout); | 
|---|
| 165 |  | 
|---|
| 166 | for (index = 0; index < sockets; index++) { | 
|---|
| 167 | for (message = 0; message < messages; message++) { | 
|---|
| 168 | rc = sendto(socket_ids[index], data, size, 0, address, addrlen); | 
|---|
| 169 | if (rc != EOK) { | 
|---|
| 170 | printf("Socket %d (%d), message %d error:\n", index, socket_ids[index], message); | 
|---|
| 171 | socket_print_error(stderr, rc, "Socket send: ", "\n"); | 
|---|
| 172 | return rc; | 
|---|
| 173 | } | 
|---|
| 174 | } | 
|---|
| 175 | if (verbose) | 
|---|
| 176 | print_mark(index); | 
|---|
| 177 | } | 
|---|
| 178 |  | 
|---|
| 179 | return EOK; | 
|---|
| 180 | } | 
|---|
| 181 |  | 
|---|
| 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. | 
|---|
| 192 | * @return EOK on success. | 
|---|
| 193 | * @return Other error codes as defined for the recvfrom() function. | 
|---|
| 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 | { | 
|---|
| 197 | int value; | 
|---|
| 198 | int index; | 
|---|
| 199 | int message; | 
|---|
| 200 |  | 
|---|
| 201 | if (verbose) | 
|---|
| 202 | printf("\tRecvfrom\t"); | 
|---|
| 203 |  | 
|---|
| 204 | fflush(stdout); | 
|---|
| 205 |  | 
|---|
| 206 | for (index = 0; index < sockets; index++) { | 
|---|
| 207 | for (message = 0; message < messages; message++) { | 
|---|
| 208 | value = recvfrom(socket_ids[index], data, size, 0, address, addrlen); | 
|---|
| 209 | if (value < 0) { | 
|---|
| 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 | } | 
|---|
| 215 | if (verbose) | 
|---|
| 216 | print_mark(index); | 
|---|
| 217 | } | 
|---|
| 218 | return EOK; | 
|---|
| 219 | } | 
|---|
| 220 |  | 
|---|
| 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. | 
|---|
| 234 | * @return EOK on success. | 
|---|
| 235 | * @return Other error codes as defined for the recvfrom() function. | 
|---|
| 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 | { | 
|---|
| 239 | int value; | 
|---|
| 240 | int index; | 
|---|
| 241 | int message; | 
|---|
| 242 | int rc; | 
|---|
| 243 |  | 
|---|
| 244 | if (verbose) | 
|---|
| 245 | printf("\tSendto and recvfrom\t"); | 
|---|
| 246 |  | 
|---|
| 247 | fflush(stdout); | 
|---|
| 248 |  | 
|---|
| 249 | for (index = 0; index < sockets; index++) { | 
|---|
| 250 | for (message = 0; message < messages; message++) { | 
|---|
| 251 | rc = sendto(socket_ids[index], data, size, 0, address, *addrlen); | 
|---|
| 252 | if (rc != EOK) { | 
|---|
| 253 | printf("Socket %d (%d), message %d error:\n", index, socket_ids[index], message); | 
|---|
| 254 | socket_print_error(stderr, rc, "Socket send: ", "\n"); | 
|---|
| 255 | return rc; | 
|---|
| 256 | } | 
|---|
| 257 | value = recvfrom(socket_ids[index], data, size, 0, address, addrlen); | 
|---|
| 258 | if (value < 0) { | 
|---|
| 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 | } | 
|---|
| 264 | if (verbose) | 
|---|
| 265 | print_mark(index); | 
|---|
| 266 | } | 
|---|
| 267 |  | 
|---|
| 268 | return EOK; | 
|---|
| 269 | } | 
|---|
| 270 |  | 
|---|
| 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) | 
|---|
| 280 | printf("*"); | 
|---|
| 281 | else | 
|---|
| 282 | printf("|"); | 
|---|
| 283 | fflush(stdout); | 
|---|
| 284 | } | 
|---|
| 285 |  | 
|---|
| 286 | /** @} | 
|---|
| 287 | */ | 
|---|