Changeset 8fb1bf82 in mainline for uspace/app/nettest1/nettest.c


Ignore:
Timestamp:
2010-11-25T13:42:50Z (13 years ago)
Author:
Jakub Jermar <jakub@…>
Branches:
lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
Children:
8df8415
Parents:
a93d79a (diff), eb667613 (diff)
Note: this is a merge changeset, the changes displayed below correspond to the merge itself.
Use the (diff) links above to see all the changes relative to each parent.
Message:

Merge mainline changes.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • uspace/app/nettest1/nettest.c

    ra93d79a r8fb1bf82  
    2828
    2929/** @addtogroup nettest
    30  *  @{
     30 * @{
    3131 */
    3232
    3333/** @file
    34  *  Networking test support functions implementation.
     34 * Networking test support functions implementation.
    3535 */
    3636
    3737#include <stdio.h>
    38 #include <err.h>
    39 
    4038#include <net/socket.h>
    4139
     
    4341#include "print_error.h"
    4442
    45 int sockets_create(int verbose, int * socket_ids, int sockets, int family, sock_type_t type){
    46         int index;
    47 
    48         if(verbose){
     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 */
     54int sockets_create(int verbose, int *socket_ids, int sockets, int family, sock_type_t type)
     55{
     56        int index;
     57
     58        if (verbose)
    4959                printf("Create\t");
    50         }
    51         fflush(stdout);
    52         for(index = 0; index < sockets; ++ index){
     60               
     61        fflush(stdout);
     62       
     63        for (index = 0; index < sockets; index++) {
    5364                socket_ids[index] = socket(family, type, 0);
    54                 if(socket_ids[index] < 0){
     65                if (socket_ids[index] < 0) {
    5566                        printf("Socket %d (%d) error:\n", index, socket_ids[index]);
    5667                        socket_print_error(stderr, socket_ids[index], "Socket create: ", "\n");
    5768                        return socket_ids[index];
    5869                }
    59                 if(verbose){
    60                         print_mark(index);
    61                 }
    62         }
    63         return EOK;
    64 }
    65 
    66 int sockets_close(int verbose, int * socket_ids, int sockets){
    67         ERROR_DECLARE;
    68 
    69         int index;
    70 
    71         if(verbose){
     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 */
     85int sockets_close(int verbose, int *socket_ids, int sockets)
     86{
     87        int index;
     88        int rc;
     89
     90        if (verbose)
    7291                printf("\tClose\t");
    73         }
    74         fflush(stdout);
    75         for(index = 0; index < sockets; ++ index){
    76                 if(ERROR_OCCURRED(closesocket(socket_ids[index]))){
     92
     93        fflush(stdout);
     94       
     95        for (index = 0; index < sockets; index++) {
     96                rc = closesocket(socket_ids[index]);
     97                if (rc != EOK) {
    7798                        printf("Socket %d (%d) error:\n", index, socket_ids[index]);
    78                         socket_print_error(stderr, ERROR_CODE, "Socket close: ", "\n");
    79                         return ERROR_CODE;
    80                 }
    81                 if(verbose){
    82                         print_mark(index);
    83                 }
    84         }
    85         return EOK;
    86 }
    87 
    88 int sockets_connect(int verbose, int * socket_ids, int sockets, struct sockaddr * address, socklen_t addrlen){
    89         ERROR_DECLARE;
    90 
    91         int index;
    92 
    93         if(verbose){
     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 */
     119int 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)
    94125                printf("\tConnect\t");
    95         }
    96         fflush(stdout);
    97         for(index = 0; index < sockets; ++ index){
    98                 if(ERROR_OCCURRED(connect(socket_ids[index], address, addrlen))){
    99                         socket_print_error(stderr, ERROR_CODE, "Socket connect: ", "\n");
    100                         return ERROR_CODE;
    101                 }
    102                 if(verbose){
    103                         print_mark(index);
    104                 }
    105         }
    106         return EOK;
    107 }
    108 
    109 int sockets_sendto(int verbose, int * socket_ids, int sockets, struct sockaddr * address, socklen_t addrlen, char * data, int size, int messages){
    110         ERROR_DECLARE;
    111 
     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 */
     155int sockets_sendto(int verbose, int *socket_ids, int sockets, struct sockaddr *address, socklen_t addrlen, char *data, int size, int messages)
     156{
    112157        int index;
    113158        int message;
    114 
    115         if(verbose){
     159        int rc;
     160
     161        if (verbose)
    116162                printf("\tSendto\t");
    117         }
    118         fflush(stdout);
    119         for(index = 0; index < sockets; ++ index){
    120                 for(message = 0; message < messages; ++ message){
    121                         if(ERROR_OCCURRED(sendto(socket_ids[index], data, size, 0, address, addrlen))){
    122                                 printf("Socket %d (%d), message %d error:\n", index, socket_ids[index], message);
    123                                 socket_print_error(stderr, ERROR_CODE, "Socket send: ", "\n");
    124                                 return ERROR_CODE;
    125                         }
    126                 }
    127                 if(verbose){
    128                         print_mark(index);
    129                 }
    130         }
    131         return EOK;
    132 }
    133 
    134 int sockets_recvfrom(int verbose, int * socket_ids, int sockets, struct sockaddr * address, socklen_t * addrlen, char * data, int size, int messages){
     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 */
     195int sockets_recvfrom(int verbose, int *socket_ids, int sockets, struct sockaddr *address, socklen_t *addrlen, char *data, int size, int messages)
     196{
    135197        int value;
    136198        int index;
    137199        int message;
    138200
    139         if(verbose){
     201        if (verbose)
    140202                printf("\tRecvfrom\t");
    141         }
    142         fflush(stdout);
    143         for(index = 0; index < sockets; ++ index){
    144                 for(message = 0; message < messages; ++ message){
     203       
     204        fflush(stdout);
     205       
     206        for (index = 0; index < sockets; index++) {
     207                for (message = 0; message < messages; message++) {
    145208                        value = recvfrom(socket_ids[index], data, size, 0, address, addrlen);
    146                         if(value < 0){
     209                        if (value < 0) {
    147210                                printf("Socket %d (%d), message %d error:\n", index, socket_ids[index], message);
    148211                                socket_print_error(stderr, value, "Socket receive: ", "\n");
     
    150213                        }
    151214                }
    152                 if(verbose){
    153                         print_mark(index);
    154                 }
    155         }
    156         return EOK;
    157 }
    158 
    159 int sockets_sendto_recvfrom(int verbose, int * socket_ids, int sockets, struct sockaddr * address, socklen_t * addrlen, char * data, int size, int messages){
    160         ERROR_DECLARE;
    161 
     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 */
     237int sockets_sendto_recvfrom(int verbose, int *socket_ids, int sockets, struct sockaddr *address, socklen_t *addrlen, char *data, int size, int messages)
     238{
    162239        int value;
    163240        int index;
    164241        int message;
    165 
    166         if(verbose){
     242        int rc;
     243
     244        if (verbose)
    167245                printf("\tSendto and recvfrom\t");
    168         }
    169         fflush(stdout);
    170         for(index = 0; index < sockets; ++ index){
    171                 for(message = 0; message < messages; ++ message){
    172                         if(ERROR_OCCURRED(sendto(socket_ids[index], data, size, 0, address, * addrlen))){
    173                                 printf("Socket %d (%d), message %d error:\n", index, socket_ids[index], message);
    174                                 socket_print_error(stderr, ERROR_CODE, "Socket send: ", "\n");
    175                                 return ERROR_CODE;
     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;
    176256                        }
    177257                        value = recvfrom(socket_ids[index], data, size, 0, address, addrlen);
    178                         if(value < 0){
     258                        if (value < 0) {
    179259                                printf("Socket %d (%d), message %d error:\n", index, socket_ids[index], message);
    180260                                socket_print_error(stderr, value, "Socket receive: ", "\n");
     
    182262                        }
    183263                }
    184                 if(verbose){
    185                         print_mark(index);
    186                 }
    187         }
    188         return EOK;
    189 }
    190 
    191 void print_mark(int index){
    192         if((index + 1) % 10){
     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 */
     277void print_mark(int index)
     278{
     279        if ((index + 1) % 10)
    193280                printf("*");
    194         }else{
     281        else
    195282                printf("|");
    196         }
    197283        fflush(stdout);
    198284}
Note: See TracChangeset for help on using the changeset viewer.