| 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 |
|
|---|
| 39 | #include "../include/socket.h"
|
|---|
| 40 |
|
|---|
| 41 | #include "../err.h"
|
|---|
| 42 |
|
|---|
| 43 | #include "nettest.h"
|
|---|
| 44 | #include "print_error.h"
|
|---|
| 45 |
|
|---|
| 46 | int sockets_create(int verbose, int * socket_ids, int sockets, int family, sock_type_t type){
|
|---|
| 47 | int index;
|
|---|
| 48 |
|
|---|
| 49 | if(verbose){
|
|---|
| 50 | printf("Create\t");
|
|---|
| 51 | }
|
|---|
| 52 | fflush(stdout);
|
|---|
| 53 | for(index = 0; index < sockets; ++ index){
|
|---|
| 54 | socket_ids[index] = socket(family, type, 0);
|
|---|
| 55 | if(socket_ids[index] < 0){
|
|---|
| 56 | printf("Socket %d (%d) error:\n", index, socket_ids[index]);
|
|---|
| 57 | socket_print_error(stderr, socket_ids[index], "Socket create: ", "\n");
|
|---|
| 58 | return socket_ids[index];
|
|---|
| 59 | }
|
|---|
| 60 | if(verbose){
|
|---|
| 61 | print_mark(index);
|
|---|
| 62 | }
|
|---|
| 63 | }
|
|---|
| 64 | return EOK;
|
|---|
| 65 | }
|
|---|
| 66 |
|
|---|
| 67 | int sockets_close(int verbose, int * socket_ids, int sockets){
|
|---|
| 68 | ERROR_DECLARE;
|
|---|
| 69 |
|
|---|
| 70 | int index;
|
|---|
| 71 |
|
|---|
| 72 | if(verbose){
|
|---|
| 73 | printf("\tClose\t");
|
|---|
| 74 | }
|
|---|
| 75 | fflush(stdout);
|
|---|
| 76 | for(index = 0; index < sockets; ++ index){
|
|---|
| 77 | if(ERROR_OCCURRED(closesocket(socket_ids[index]))){
|
|---|
| 78 | printf("Socket %d (%d) error:\n", index, socket_ids[index]);
|
|---|
| 79 | socket_print_error(stderr, ERROR_CODE, "Socket close: ", "\n");
|
|---|
| 80 | return ERROR_CODE;
|
|---|
| 81 | }
|
|---|
| 82 | if(verbose){
|
|---|
| 83 | print_mark(index);
|
|---|
| 84 | }
|
|---|
| 85 | }
|
|---|
| 86 | return EOK;
|
|---|
| 87 | }
|
|---|
| 88 |
|
|---|
| 89 | int sockets_connect(int verbose, int * socket_ids, int sockets, struct sockaddr * address, socklen_t addrlen){
|
|---|
| 90 | ERROR_DECLARE;
|
|---|
| 91 |
|
|---|
| 92 | int index;
|
|---|
| 93 |
|
|---|
| 94 | if(verbose){
|
|---|
| 95 | printf("\tConnect\t");
|
|---|
| 96 | }
|
|---|
| 97 | fflush(stdout);
|
|---|
| 98 | for(index = 0; index < sockets; ++ index){
|
|---|
| 99 | if(ERROR_OCCURRED(connect(socket_ids[index], address, addrlen))){
|
|---|
| 100 | socket_print_error(stderr, ERROR_CODE, "Socket connect: ", "\n");
|
|---|
| 101 | return ERROR_CODE;
|
|---|
| 102 | }
|
|---|
| 103 | if(verbose){
|
|---|
| 104 | print_mark(index);
|
|---|
| 105 | }
|
|---|
| 106 | }
|
|---|
| 107 | return EOK;
|
|---|
| 108 | }
|
|---|
| 109 |
|
|---|
| 110 | int sockets_sendto(int verbose, int * socket_ids, int sockets, struct sockaddr * address, socklen_t addrlen, char * data, int size, int messages){
|
|---|
| 111 | ERROR_DECLARE;
|
|---|
| 112 |
|
|---|
| 113 | int index;
|
|---|
| 114 | int message;
|
|---|
| 115 |
|
|---|
| 116 | if(verbose){
|
|---|
| 117 | printf("\tSendto\t");
|
|---|
| 118 | }
|
|---|
| 119 | fflush(stdout);
|
|---|
| 120 | for(index = 0; index < sockets; ++ index){
|
|---|
| 121 | for(message = 0; message < messages; ++ message){
|
|---|
| 122 | if(ERROR_OCCURRED(sendto(socket_ids[index], data, size, 0, address, addrlen))){
|
|---|
| 123 | printf("Socket %d (%d), message %d error:\n", index, socket_ids[index], message);
|
|---|
| 124 | socket_print_error(stderr, ERROR_CODE, "Socket send: ", "\n");
|
|---|
| 125 | return ERROR_CODE;
|
|---|
| 126 | }
|
|---|
| 127 | }
|
|---|
| 128 | if(verbose){
|
|---|
| 129 | print_mark(index);
|
|---|
| 130 | }
|
|---|
| 131 | }
|
|---|
| 132 | return EOK;
|
|---|
| 133 | }
|
|---|
| 134 |
|
|---|
| 135 | int sockets_recvfrom(int verbose, int * socket_ids, int sockets, struct sockaddr * address, socklen_t * addrlen, char * data, int size, int messages){
|
|---|
| 136 | int value;
|
|---|
| 137 | int index;
|
|---|
| 138 | int message;
|
|---|
| 139 |
|
|---|
| 140 | if(verbose){
|
|---|
| 141 | printf("\tRecvfrom\t");
|
|---|
| 142 | }
|
|---|
| 143 | fflush(stdout);
|
|---|
| 144 | for(index = 0; index < sockets; ++ index){
|
|---|
| 145 | for(message = 0; message < messages; ++ message){
|
|---|
| 146 | value = recvfrom(socket_ids[index], data, size, 0, address, addrlen);
|
|---|
| 147 | if(value < 0){
|
|---|
| 148 | printf("Socket %d (%d), message %d error:\n", index, socket_ids[index], message);
|
|---|
| 149 | socket_print_error(stderr, value, "Socket receive: ", "\n");
|
|---|
| 150 | return value;
|
|---|
| 151 | }
|
|---|
| 152 | }
|
|---|
| 153 | if(verbose){
|
|---|
| 154 | print_mark(index);
|
|---|
| 155 | }
|
|---|
| 156 | }
|
|---|
| 157 | return EOK;
|
|---|
| 158 | }
|
|---|
| 159 |
|
|---|
| 160 | int sockets_sendto_recvfrom(int verbose, int * socket_ids, int sockets, struct sockaddr * address, socklen_t * addrlen, char * data, int size, int messages){
|
|---|
| 161 | ERROR_DECLARE;
|
|---|
| 162 |
|
|---|
| 163 | int value;
|
|---|
| 164 | int index;
|
|---|
| 165 | int message;
|
|---|
| 166 |
|
|---|
| 167 | if(verbose){
|
|---|
| 168 | printf("\tSendto and recvfrom\t");
|
|---|
| 169 | }
|
|---|
| 170 | fflush(stdout);
|
|---|
| 171 | for(index = 0; index < sockets; ++ index){
|
|---|
| 172 | for(message = 0; message < messages; ++ message){
|
|---|
| 173 | if(ERROR_OCCURRED(sendto(socket_ids[index], data, size, 0, address, * addrlen))){
|
|---|
| 174 | printf("Socket %d (%d), message %d error:\n", index, socket_ids[index], message);
|
|---|
| 175 | socket_print_error(stderr, ERROR_CODE, "Socket send: ", "\n");
|
|---|
| 176 | return ERROR_CODE;
|
|---|
| 177 | }
|
|---|
| 178 | value = recvfrom(socket_ids[index], data, size, 0, address, addrlen);
|
|---|
| 179 | if(value < 0){
|
|---|
| 180 | printf("Socket %d (%d), message %d error:\n", index, socket_ids[index], message);
|
|---|
| 181 | socket_print_error(stderr, value, "Socket receive: ", "\n");
|
|---|
| 182 | return value;
|
|---|
| 183 | }
|
|---|
| 184 | }
|
|---|
| 185 | if(verbose){
|
|---|
| 186 | print_mark(index);
|
|---|
| 187 | }
|
|---|
| 188 | }
|
|---|
| 189 | return EOK;
|
|---|
| 190 | }
|
|---|
| 191 |
|
|---|
| 192 | void print_mark(int index){
|
|---|
| 193 | if((index + 1) % 10){
|
|---|
| 194 | printf("*");
|
|---|
| 195 | }else{
|
|---|
| 196 | printf("|");
|
|---|
| 197 | }
|
|---|
| 198 | fflush(stdout);
|
|---|
| 199 | }
|
|---|
| 200 |
|
|---|
| 201 | /** @}
|
|---|
| 202 | */
|
|---|