nettest.c

Go to the documentation of this file.
00001 /*
00002  * Copyright (c) 2009 Lukas Mejdrech
00003  * All rights reserved.
00004  *
00005  * Redistribution and use in source and binary forms, with or without
00006  * modification, are permitted provided that the following conditions
00007  * are met:
00008  *
00009  * - Redistributions of source code must retain the above copyright
00010  *   notice, this list of conditions and the following disclaimer.
00011  * - Redistributions in binary form must reproduce the above copyright
00012  *   notice, this list of conditions and the following disclaimer in the
00013  *   documentation and/or other materials provided with the distribution.
00014  * - The name of the author may not be used to endorse or promote products
00015  *   derived from this software without specific prior written permission.
00016  *
00017  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
00018  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
00019  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
00020  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
00021  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
00022  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
00023  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
00024  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
00025  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
00026  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00027  */
00028 
00037 #include <stdio.h>
00038 #include <net/socket.h>
00039 
00040 #include "nettest.h"
00041 #include "print_error.h"
00042 
00043 
00054 int sockets_create(int verbose, int *socket_ids, int sockets, int family, sock_type_t type)
00055 {
00056         int index;
00057 
00058         if (verbose)
00059                 printf("Create\t");
00060                 
00061         fflush(stdout);
00062         
00063         for (index = 0; index < sockets; index++) {
00064                 socket_ids[index] = socket(family, type, 0);
00065                 if (socket_ids[index] < 0) {
00066                         printf("Socket %d (%d) error:\n", index, socket_ids[index]);
00067                         socket_print_error(stderr, socket_ids[index], "Socket create: ", "\n");
00068                         return socket_ids[index];
00069                 }
00070                 if (verbose)
00071                         print_mark(index);
00072         }
00073         
00074         return EOK;
00075 }
00076 
00085 int sockets_close(int verbose, int *socket_ids, int sockets)
00086 {
00087         int index;
00088         int rc;
00089 
00090         if (verbose)
00091                 printf("\tClose\t");
00092 
00093         fflush(stdout);
00094         
00095         for (index = 0; index < sockets; index++) {
00096                 rc = closesocket(socket_ids[index]);
00097                 if (rc != EOK) {
00098                         printf("Socket %d (%d) error:\n", index, socket_ids[index]);
00099                         socket_print_error(stderr, rc, "Socket close: ", "\n");
00100                         return rc;
00101                 }
00102                 if (verbose)
00103                         print_mark(index);
00104         }
00105         
00106         return EOK;
00107 }
00108 
00119 int sockets_connect(int verbose, int *socket_ids, int sockets, struct sockaddr *address, socklen_t addrlen)
00120 {
00121         int index;
00122         int rc;
00123 
00124         if (verbose)
00125                 printf("\tConnect\t");
00126         
00127         fflush(stdout);
00128         
00129         for (index = 0; index < sockets; index++) {
00130                 rc = connect(socket_ids[index], address, addrlen);
00131                 if (rc != EOK) {
00132                         socket_print_error(stderr, rc, "Socket connect: ", "\n");
00133                         return rc;
00134                 }
00135                 if (verbose)
00136                         print_mark(index);
00137         }
00138         
00139         return EOK;
00140 }
00141 
00155 int sockets_sendto(int verbose, int *socket_ids, int sockets, struct sockaddr *address, socklen_t addrlen, char *data, int size, int messages)
00156 {
00157         int index;
00158         int message;
00159         int rc;
00160 
00161         if (verbose)
00162                 printf("\tSendto\t");
00163 
00164         fflush(stdout);
00165         
00166         for (index = 0; index < sockets; index++) {
00167                 for (message = 0; message < messages; message++) {
00168                         rc = sendto(socket_ids[index], data, size, 0, address, addrlen);
00169                         if (rc != EOK) {
00170                                 printf("Socket %d (%d), message %d error:\n", index, socket_ids[index], message);
00171                                 socket_print_error(stderr, rc, "Socket send: ", "\n");
00172                                 return rc;
00173                         }
00174                 }
00175                 if (verbose)
00176                         print_mark(index);
00177         }
00178         
00179         return EOK;
00180 }
00181 
00195 int sockets_recvfrom(int verbose, int *socket_ids, int sockets, struct sockaddr *address, socklen_t *addrlen, char *data, int size, int messages)
00196 {
00197         int value;
00198         int index;
00199         int message;
00200 
00201         if (verbose)
00202                 printf("\tRecvfrom\t");
00203         
00204         fflush(stdout);
00205         
00206         for (index = 0; index < sockets; index++) {
00207                 for (message = 0; message < messages; message++) {
00208                         value = recvfrom(socket_ids[index], data, size, 0, address, addrlen);
00209                         if (value < 0) {
00210                                 printf("Socket %d (%d), message %d error:\n", index, socket_ids[index], message);
00211                                 socket_print_error(stderr, value, "Socket receive: ", "\n");
00212                                 return value;
00213                         }
00214                 }
00215                 if (verbose)
00216                         print_mark(index);
00217         }
00218         return EOK;
00219 }
00220 
00237 int sockets_sendto_recvfrom(int verbose, int *socket_ids, int sockets, struct sockaddr *address, socklen_t *addrlen, char *data, int size, int messages)
00238 {
00239         int value;
00240         int index;
00241         int message;
00242         int rc;
00243 
00244         if (verbose)
00245                 printf("\tSendto and recvfrom\t");
00246 
00247         fflush(stdout);
00248         
00249         for (index = 0; index < sockets; index++) {
00250                 for (message = 0; message < messages; message++) {
00251                         rc = sendto(socket_ids[index], data, size, 0, address, *addrlen);
00252                         if (rc != EOK) {
00253                                 printf("Socket %d (%d), message %d error:\n", index, socket_ids[index], message);
00254                                 socket_print_error(stderr, rc, "Socket send: ", "\n");
00255                                 return rc;
00256                         }
00257                         value = recvfrom(socket_ids[index], data, size, 0, address, addrlen);
00258                         if (value < 0) {
00259                                 printf("Socket %d (%d), message %d error:\n", index, socket_ids[index], message);
00260                                 socket_print_error(stderr, value, "Socket receive: ", "\n");
00261                                 return value;
00262                         }
00263                 }
00264                 if (verbose)
00265                         print_mark(index);
00266         }
00267         
00268         return EOK;
00269 }
00270 
00277 void print_mark(int index)
00278 {
00279         if ((index + 1) % 10)
00280                 printf("*");
00281         else
00282                 printf("|");
00283         fflush(stdout);
00284 }
00285 

Generated on Thu Jun 2 07:45:42 2011 for HelenOS/USB by  doxygen 1.4.7