Changeset 69e0d6d in mainline


Ignore:
Timestamp:
2010-11-07T13:36:01Z (13 years ago)
Author:
Jiri Svoboda <jiri@…>
Branches:
lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
Children:
3da12d74, cb569e6
Parents:
987930f
Message:

Slightly clean up nettest1.

Location:
uspace/app
Files:
2 edited

Legend:

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

    r987930f r69e0d6d  
    5757#define NETTEST1_TEXT   "Networking test 1 - sockets"
    5858
    59 int family = PF_INET;
    60 sock_type_t type = SOCK_DGRAM;
    61 char *data;
    62 size_t size = 27;
    63 int verbose = 0;
    64 
    65 struct sockaddr *address;
    66 socklen_t addrlen;
     59static int family = PF_INET;
     60static sock_type_t type = SOCK_DGRAM;
     61static char *data;
     62static size_t size = 27;
     63static int verbose = 0;
     64
     65static struct sockaddr *address;
     66static socklen_t addrlen;
     67
     68static int sockets;
     69static int messages;
     70static uint16_t port;
    6771
    6872static void nettest1_print_help(void)
    6973{
    7074        printf(
    71                 "Network Networking test 1 aplication - sockets\n" \
    72                 "Usage: echo [options] numeric_address\n" \
    73                 "Where options are:\n" \
    74                 "-f protocol_family | --family=protocol_family\n" \
    75                 "\tThe listenning socket protocol family. Only the PF_INET and PF_INET6 are supported.\n"
    76                 "\n" \
    77                 "-h | --help\n" \
    78                 "\tShow this application help.\n"
    79                 "\n" \
    80                 "-m count | --messages=count\n" \
    81                 "\tThe number of messages to send and receive per socket. The default is 10.\n" \
    82                 "\n" \
    83                 "-n sockets | --sockets=count\n" \
    84                 "\tThe number of sockets to use. The default is 10.\n" \
    85                 "\n" \
    86                 "-p port_number | --port=port_number\n" \
    87                 "\tThe port number the application should send messages to. The default is 7.\n" \
    88                 "\n" \
    89                 "-s packet_size | --size=packet_size\n" \
    90                 "\tThe packet data size the application sends. The default is 28 bytes.\n" \
    91                 "\n" \
    92                 "-v | --verbose\n" \
    93                 "\tShow all output messages.\n"
    94         );
     75            "Network Networking test 1 aplication - sockets\n"
     76            "Usage: echo [options] numeric_address\n"
     77            "Where options are:\n"
     78            "-f protocol_family | --family=protocol_family\n"
     79            "\tThe listenning socket protocol family. Only the PF_INET and "
     80            "PF_INET6 are supported.\n"
     81            "\n"
     82            "-h | --help\n"
     83            "\tShow this application help.\n"
     84            "\n"
     85            "-m count | --messages=count\n"
     86            "\tThe number of messages to send and receive per socket. The "
     87            "default is 10.\n"
     88            "\n"
     89            "-n sockets | --sockets=count\n"
     90            "\tThe number of sockets to use. The default is 10.\n"
     91            "\n"
     92            "-p port_number | --port=port_number\n"
     93            "\tThe port number the application should send messages to. The "
     94            "default is 7.\n"
     95            "\n"
     96            "-s packet_size | --size=packet_size\n"
     97            "\tThe packet data size the application sends. The default is "
     98            "28 bytes.\n"
     99            "\n"
     100            "-v | --verbose\n"
     101            "\tShow all output messages.\n");
    95102}
    96103
    97 /** Refreshes the data.
     104/** Parse one command-line option.
    98105 *
    99  * Fills the data block with the NETTEST1_TEXT pattern.
     106 * @param argc          Number of all command-line arguments.
     107 * @param argv          All command-line arguments.
     108 * @param index         Current argument index (in, out).
     109 */
     110static int nettest1_parse_opt(int argc, char *argv[], int *index)
     111{
     112        int value;
     113        int rc;
     114
     115        switch (argv[*index][1]) {
     116        /*
     117         * Short options with only one letter
     118         */
     119        case 'f':
     120                rc = arg_parse_name_int(argc, argv, index, &family, 0, socket_parse_protocol_family);
     121                if (rc != EOK)
     122                        return rc;
     123                break;
     124        case 'h':
     125                nettest1_print_help();
     126                return EOK;
     127        case 'm':
     128                rc = arg_parse_int(argc, argv, index, &messages, 0);
     129                if (rc != EOK)
     130                        return rc;
     131                break;
     132        case 'n':
     133                rc = arg_parse_int(argc, argv, index, &sockets, 0);
     134                if (rc != EOK)
     135                        return rc;
     136                break;
     137        case 'p':
     138                rc = arg_parse_int(argc, argv, index, &value, 0);
     139                if (rc != EOK)
     140                        return rc;
     141                port = (uint16_t) value;
     142                break;
     143        case 's':
     144                rc = arg_parse_int(argc, argv, index, &value, 0);
     145                if (rc != EOK)
     146                        return rc;
     147                size = (value >= 0) ? (size_t) value : 0;
     148                break;
     149        case 't':
     150                rc = arg_parse_name_int(argc, argv, index, &value, 0, socket_parse_socket_type);
     151                if (rc != EOK)
     152                        return rc;
     153                type = (sock_type_t) value;
     154                break;
     155        case 'v':
     156                verbose = 1;
     157                break;
     158        /*
     159         * Long options with double dash ('-')
     160         */
     161        case '-':
     162                if (str_lcmp(argv[*index] + 2, "family=", 7) == 0) {
     163                        rc = arg_parse_name_int(argc, argv, index, &family, 9,
     164                            socket_parse_protocol_family);
     165                        if (rc != EOK)
     166                                return rc;
     167                } else if (str_lcmp(argv[*index] + 2, "help", 5) == 0) {
     168                        nettest1_print_help();
     169                        return EOK;
     170                } else if (str_lcmp(argv[*index] + 2, "messages=", 6) == 0) {
     171                        rc = arg_parse_int(argc, argv, index, &messages, 8);
     172                        if (rc != EOK)
     173                                return rc;
     174                } else if (str_lcmp(argv[*index] + 2, "sockets=", 6) == 0) {
     175                        rc = arg_parse_int(argc, argv, index, &sockets, 8);
     176                        if (rc != EOK)
     177                                return rc;
     178                } else if (str_lcmp(argv[*index] + 2, "port=", 5) == 0) {
     179                        rc = arg_parse_int(argc, argv, index, &value, 7);
     180                        if (rc != EOK)
     181                                return rc;
     182                        port = (uint16_t) value;
     183                } else if (str_lcmp(argv[*index] + 2, "type=", 5) == 0) {
     184                        rc = arg_parse_name_int(argc, argv, index, &value, 7,
     185                            socket_parse_socket_type);
     186                        if (rc != EOK)
     187                                return rc;
     188                        type = (sock_type_t) value;
     189                } else if (str_lcmp(argv[*index] + 2, "verbose", 8) == 0) {
     190                        verbose = 1;
     191                } else {
     192                        nettest1_print_help();
     193                        return EINVAL;
     194                }
     195                break;
     196        default:
     197                nettest1_print_help();
     198                return EINVAL;
     199        }
     200
     201        return EOK;
     202}
     203
     204/** Fill buffer with the NETTEST1_TEXT pattern.
    100205 *
    101  * @param[out] data The data block.
    102  * @param[in] size The data block size in bytes.
    103  */
    104 static void nettest1_refresh_data(char *data, size_t size)
     206 * @param buffer        Data buffer.
     207 * @param size          Buffer size in bytes.
     208 */
     209static void nettest1_fill_buffer(char *buffer, size_t size)
    105210{
    106211        size_t length;
    107212
    108         // fill the data
    109213        length = 0;
    110214        while (size > length + sizeof(NETTEST1_TEXT) - 1) {
    111                 memcpy(data + length, NETTEST1_TEXT, sizeof(NETTEST1_TEXT) - 1);
     215                memcpy(buffer + length, NETTEST1_TEXT,
     216                    sizeof(NETTEST1_TEXT) - 1);
    112217                length += sizeof(NETTEST1_TEXT) - 1;
    113218        }
    114         memcpy(data + length, NETTEST1_TEXT, size - length);
    115         data[size] = '\0';
     219
     220        memcpy(buffer + length, NETTEST1_TEXT, size - length);
     221        buffer[size] = '\0';
    116222}
    117223
     
    128234
    129235        if (type == SOCK_STREAM) {
    130                 rc = sockets_connect(verbose, socket_ids, nsockets, address, addrlen);
    131                 if (rc != EOK)
    132                         return rc;
    133         }
    134 
    135         rc = sockets_sendto_recvfrom(verbose, socket_ids, nsockets, address, &addrlen, data, size, nmessages);
     236                rc = sockets_connect(verbose, socket_ids, nsockets, address,
     237                    addrlen);
     238                if (rc != EOK)
     239                        return rc;
     240        }
     241
     242        rc = sockets_sendto_recvfrom(verbose, socket_ids, nsockets, address,
     243            &addrlen, data, size, nmessages);
    136244        if (rc != EOK)
    137245                return rc;
     
    151259
    152260        if (type == SOCK_STREAM) {
    153                 rc = sockets_connect(verbose, socket_ids, nsockets, address, addrlen);
    154                 if (rc != EOK)
    155                         return rc;
    156         }
    157 
    158         rc = sockets_sendto(verbose, socket_ids, nsockets, address, addrlen, data, size, nmessages);
    159         if (rc != EOK)
    160                 return rc;
    161 
    162         rc = sockets_recvfrom(verbose, socket_ids, nsockets, address, &addrlen, data, size, nmessages);
     261                rc = sockets_connect(verbose, socket_ids, nsockets, address,
     262                    addrlen);
     263                if (rc != EOK)
     264                        return rc;
     265        }
     266
     267        rc = sockets_sendto(verbose, socket_ids, nsockets, address, addrlen,
     268            data, size, nmessages);
     269        if (rc != EOK)
     270                return rc;
     271
     272        rc = sockets_recvfrom(verbose, socket_ids, nsockets, address, &addrlen,
     273            data, size, nmessages);
    163274        if (rc != EOK)
    164275                return rc;
     
    176287int main(int argc, char *argv[])
    177288{
    178         int sockets = 10;
    179         int messages = 10;
    180         uint16_t port = 7;
    181289
    182290        socklen_t max_length;
     
    187295
    188296        int *socket_ids;
    189         int value;
    190297        int index;
    191298        struct timeval time_before;
     
    199306        address_in6 = (struct sockaddr_in6 *) address;
    200307
    201         // parse the command line arguments
    202         // stop before the last argument if it does not start with the minus sign ('-')
     308        sockets = 10;
     309        messages = 10;
     310        port = 7;
     311
     312        /*
     313         * Parse the command line arguments. Stop before the last argument
     314         * if it does not start with dash ('-')
     315         */
    203316        for (index = 1; (index < argc - 1) || ((index == argc - 1) && (argv[index][0] == '-')); index++) {
    204                 // options should start with the minus sign ('-')
     317                /* Options should start with dash ('-') */
    205318                if (argv[index][0] == '-') {
    206                         switch (argv[index][1]) {
    207                         // short options with only one letter
    208                         case 'f':
    209                                 rc = arg_parse_name_int(argc, argv, &index, &family, 0, socket_parse_protocol_family);
    210                                 if (rc != EOK)
    211                                         return rc;
    212                                 break;
    213                         case 'h':
    214                                 nettest1_print_help();
    215                                 return EOK;
    216                         case 'm':
    217                                 rc = arg_parse_int(argc, argv, &index, &messages, 0);
    218                                 if (rc != EOK)
    219                                         return rc;
    220                                 break;
    221                         case 'n':
    222                                 rc = arg_parse_int(argc, argv, &index, &sockets, 0);
    223                                 if (rc != EOK)
    224                                         return rc;
    225                                 break;
    226                         case 'p':
    227                                 rc = arg_parse_int(argc, argv, &index, &value, 0);
    228                                 if (rc != EOK)
    229                                         return rc;
    230                                 port = (uint16_t) value;
    231                                 break;
    232                         case 's':
    233                                 rc = arg_parse_int(argc, argv, &index, &value, 0);
    234                                 if (rc != EOK)
    235                                         return rc;
    236                                 size = (value >= 0) ? (size_t) value : 0;
    237                                 break;
    238                         case 't':
    239                                 rc = arg_parse_name_int(argc, argv, &index, &value, 0, socket_parse_socket_type);
    240                                 if (rc != EOK)
    241                                         return rc;
    242                                 type = (sock_type_t) value;
    243                                 break;
    244                         case 'v':
    245                                 verbose = 1;
    246                                 break;
    247                         // long options with the double minus sign ('-')
    248                         case '-':
    249                                 if (str_lcmp(argv[index] + 2, "family=", 7) == 0) {
    250                                         rc = arg_parse_name_int(argc, argv, &index, &family, 9, socket_parse_protocol_family);
    251                                         if (rc != EOK)
    252                                                 return rc;
    253                                 } else if (str_lcmp(argv[index] + 2, "help", 5) == 0) {
    254                                         nettest1_print_help();
    255                                         return EOK;
    256                                 } else if (str_lcmp(argv[index] + 2, "messages=", 6) == 0) {
    257                                         rc = arg_parse_int(argc, argv, &index, &messages, 8);
    258                                         if (rc != EOK)
    259                                                 return rc;
    260                                 } else if (str_lcmp(argv[index] + 2, "sockets=", 6) == 0) {
    261                                         rc = arg_parse_int(argc, argv, &index, &sockets, 8);
    262                                         if (rc != EOK)
    263                                                 return rc;
    264                                 } else if (str_lcmp(argv[index] + 2, "port=", 5) == 0) {
    265                                         rc = arg_parse_int(argc, argv, &index, &value, 7);
    266                                         if (rc != EOK)
    267                                                 return rc;
    268                                         port = (uint16_t) value;
    269                                 } else if (str_lcmp(argv[index] + 2, "type=", 5) == 0) {
    270                                         rc = arg_parse_name_int(argc, argv, &index, &value, 7, socket_parse_socket_type);
    271                                         if (rc != EOK)
    272                                                 return rc;
    273                                         type = (sock_type_t) value;
    274                                 } else if (str_lcmp(argv[index] + 2, "verbose", 8) == 0) {
    275                                         verbose = 1;
    276                                 } else {
    277                                         nettest1_print_help();
    278                                         return EINVAL;
    279                                 }
    280                                 break;
    281                         default:
    282                                 nettest1_print_help();
    283                                 return EINVAL;
    284                         }
     319                        rc = nettest1_parse_opt(argc, argv, &index);
     320                        if (rc != EOK)
     321                                return rc;
    285322                } else {
    286323                        nettest1_print_help();
     
    289326        }
    290327
    291         // if not before the last argument containing the address
     328        /* If not before the last argument containing the address */
    292329        if (index >= argc) {
    293330                printf("Command line error: missing address\n");
     
    296333        }
    297334
    298         // prepare the address buffer
     335        /* Prepare the address buffer */
    299336        bzero(address_data, max_length);
     337
    300338        switch (family) {
    301339        case PF_INET:
     
    316354        }
    317355
    318         // parse the last argument which should contain the address
     356        /* Parse the last argument which should contain the address */
    319357        rc = inet_pton(family, argv[argc - 1], address_start);
    320358        if (rc != EOK) {
     
    323361        }
    324362
    325         // check the buffer size
     363        /* Check data buffer size */
    326364        if (size <= 0) {
    327                 fprintf(stderr, "Data buffer size too small (%d). Using 1024 bytes instead.\n", size);
     365                fprintf(stderr, "Data buffer size too small (%d). Using 1024 "
     366                    "bytes instead.\n", size);
    328367                size = 1024;
    329368        }
    330369
    331         // prepare the buffer
    332         // size plus the terminating null (\0)
     370        /*
     371         * Prepare data buffer. Allocate size bytes plus one for the
     372         * trailing null character.
     373         */
    333374        data = (char *) malloc(size + 1);
    334375        if (!data) {
     
    336377                return ENOMEM;
    337378        }
    338         nettest1_refresh_data(data, size);
    339 
    340         // check the socket count
     379        nettest1_fill_buffer(data, size);
     380
     381        /* Check socket count */
    341382        if (sockets <= 0) {
    342                 fprintf(stderr, "Socket count too small (%d). Using 2 instead.\n", sockets);
     383                fprintf(stderr, "Socket count too small (%d). Using "
     384                    "2 instead.\n", sockets);
    343385                sockets = 2;
    344386        }
    345387
    346         // prepare the socket buffer
    347         // count plus the terminating null (\0)
     388        /*
     389         * Prepare socket buffer. Allocate count fields plus the terminating
     390         * null (\0).
     391         */
    348392        socket_ids = (int *) malloc(sizeof(int) * (sockets + 1));
    349393        if (!socket_ids) {
     
    373417        }
    374418
    375         printf("Tested in %d microseconds\n", tv_sub(&time_after, &time_before));
     419        printf("Tested in %d microseconds\n", tv_sub(&time_after,
     420            &time_before));
    376421
    377422        if (verbose)
  • uspace/app/nettest2/nettest2.c

    r987930f r69e0d6d  
    9898}
    9999
    100 /** Fill buffer with the NETTEST1_TEXT pattern.
     100/** Fill buffer with the NETTEST2_TEXT pattern.
    101101 *
    102102 * @param buffer        Data buffer.
     
    128128        int value;
    129129        int rc;
    130 
    131         rc = EOK;
    132130
    133131        switch (argv[*index][1]) {
Note: See TracChangeset for help on using the changeset viewer.