Ignore:
File:
1 edited

Legend:

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

    r7e752b2 r02a09ed  
    4040#include <malloc.h>
    4141#include <stdio.h>
     42#include <unistd.h>
    4243#include <str.h>
    4344#include <task.h>
     
    4546#include <arg_parse.h>
    4647
     48#include <inet/dnsr.h>
    4749#include <net/in.h>
    4850#include <net/in6.h>
     
    5254
    5355/** Echo module name. */
    54 #define NAME    "Nettest1"
     56#define NAME  "nettest1"
    5557
    5658/** Packet data pattern. */
    57 #define NETTEST1_TEXT   "Networking test 1 - sockets"
    58 
    59 static int family = PF_INET;
     59#define NETTEST1_TEXT  "Networking test 1 - sockets"
     60
     61static uint16_t family = AF_INET;
    6062static sock_type_t type = SOCK_DGRAM;
    61 static char *data;
    6263static size_t size = 27;
    63 static int verbose = 0;
     64static bool verbose = false;
     65static int sockets = 10;
     66static int messages = 10;
     67static uint16_t port = 7;
    6468
    6569static struct sockaddr *address;
    6670static socklen_t addrlen;
    6771
    68 static int sockets;
    69 static int messages;
    70 static uint16_t port;
     72static char *data;
    7173
    7274static void nettest1_print_help(void)
     
    7476        printf(
    7577            "Network Networking test 1 aplication - sockets\n"
    76             "Usage: echo [options] numeric_address\n"
     78            "Usage: nettest1 [options] host\n"
    7779            "Where options are:\n"
    7880            "-f protocol_family | --family=protocol_family\n"
     
    112114        int value;
    113115        int rc;
    114 
     116       
    115117        switch (argv[*index][1]) {
    116118        /*
     
    118120         */
    119121        case 'f':
    120                 rc = arg_parse_name_int(argc, argv, index, &family, 0, socket_parse_protocol_family);
    121                 if (rc != EOK)
    122                         return rc;
     122                rc = arg_parse_name_int(argc, argv, index, &value, 0,
     123                    socket_parse_protocol_family);
     124                if (rc != EOK)
     125                        return rc;
     126               
     127                family = (uint16_t) value;
    123128                break;
    124129        case 'h':
     
    129134                if (rc != EOK)
    130135                        return rc;
     136               
    131137                break;
    132138        case 'n':
     
    134140                if (rc != EOK)
    135141                        return rc;
     142               
    136143                break;
    137144        case 'p':
     
    139146                if (rc != EOK)
    140147                        return rc;
     148               
    141149                port = (uint16_t) value;
    142150                break;
     
    145153                if (rc != EOK)
    146154                        return rc;
     155               
    147156                size = (value >= 0) ? (size_t) value : 0;
    148157                break;
    149158        case 't':
    150                 rc = arg_parse_name_int(argc, argv, index, &value, 0, socket_parse_socket_type);
    151                 if (rc != EOK)
    152                         return rc;
     159                rc = arg_parse_name_int(argc, argv, index, &value, 0,
     160                    socket_parse_socket_type);
     161                if (rc != EOK)
     162                        return rc;
     163               
    153164                type = (sock_type_t) value;
    154165                break;
     
    156167                verbose = 1;
    157168                break;
     169       
    158170        /*
    159171         * Long options with double dash ('-')
     
    161173        case '-':
    162174                if (str_lcmp(argv[*index] + 2, "family=", 7) == 0) {
    163                         rc = arg_parse_name_int(argc, argv, index, &family, 9,
     175                        rc = arg_parse_name_int(argc, argv, index, &value, 9,
    164176                            socket_parse_protocol_family);
    165177                        if (rc != EOK)
    166178                                return rc;
     179                       
     180                        family = (uint16_t) value;
    167181                } else if (str_lcmp(argv[*index] + 2, "help", 5) == 0) {
    168182                        nettest1_print_help();
     
    180194                        if (rc != EOK)
    181195                                return rc;
     196                       
    182197                        port = (uint16_t) value;
    183198                } else if (str_lcmp(argv[*index] + 2, "type=", 5) == 0) {
     
    186201                        if (rc != EOK)
    187202                                return rc;
     203                       
    188204                        type = (sock_type_t) value;
    189205                } else if (str_lcmp(argv[*index] + 2, "verbose", 8) == 0) {
     
    198214                return EINVAL;
    199215        }
    200 
     216       
    201217        return EOK;
    202218}
     
    209225static void nettest1_fill_buffer(char *buffer, size_t size)
    210226{
    211         size_t length;
    212 
    213         length = 0;
     227        size_t length = 0;
    214228        while (size > length + sizeof(NETTEST1_TEXT) - 1) {
    215229                memcpy(buffer + length, NETTEST1_TEXT,
     
    217231                length += sizeof(NETTEST1_TEXT) - 1;
    218232        }
    219 
     233       
    220234        memcpy(buffer + length, NETTEST1_TEXT, size - length);
    221235        buffer[size] = '\0';
     
    224238static int nettest1_test(int *socket_ids, int nsockets, int nmessages)
    225239{
    226         int rc;
    227 
    228240        if (verbose)
    229241                printf("%d sockets, %d messages\n", nsockets, nmessages);
    230 
    231         rc = sockets_create(verbose, socket_ids, nsockets, family, type);
    232         if (rc != EOK)
    233                 return rc;
    234 
     242       
     243        int rc = sockets_create(verbose, socket_ids, nsockets, family, type);
     244        if (rc != EOK)
     245                return rc;
     246       
    235247        if (type == SOCK_STREAM) {
    236248                rc = sockets_connect(verbose, socket_ids, nsockets, address,
     
    239251                        return rc;
    240252        }
    241 
     253       
    242254        rc = sockets_sendto_recvfrom(verbose, socket_ids, nsockets, address,
    243255            &addrlen, data, size, nmessages);
    244256        if (rc != EOK)
    245257                return rc;
    246 
     258       
    247259        rc = sockets_close(verbose, socket_ids, nsockets);
    248260        if (rc != EOK)
    249261                return rc;
    250 
     262       
    251263        if (verbose)
    252264                printf("\tOK\n");
    253 
     265       
    254266        /****/
    255 
     267       
    256268        rc = sockets_create(verbose, socket_ids, nsockets, family, type);
    257269        if (rc != EOK)
    258270                return rc;
    259 
     271       
    260272        if (type == SOCK_STREAM) {
    261273                rc = sockets_connect(verbose, socket_ids, nsockets, address,
     
    264276                        return rc;
    265277        }
    266 
     278       
    267279        rc = sockets_sendto(verbose, socket_ids, nsockets, address, addrlen,
    268280            data, size, nmessages);
    269281        if (rc != EOK)
    270282                return rc;
    271 
     283       
    272284        rc = sockets_recvfrom(verbose, socket_ids, nsockets, address, &addrlen,
    273285            data, size, nmessages);
    274286        if (rc != EOK)
    275287                return rc;
    276 
     288       
    277289        rc = sockets_close(verbose, socket_ids, nsockets);
    278290        if (rc != EOK)
    279291                return rc;
    280 
     292       
    281293        if (verbose)
    282294                printf("\tOK\n");
    283 
     295       
    284296        return EOK;
    285297}
     
    287299int main(int argc, char *argv[])
    288300{
    289         struct sockaddr_in address_in;
    290         struct sockaddr_in6 address_in6;
    291         uint8_t *address_start;
    292 
    293         int *socket_ids;
    294         int index;
    295         struct timeval time_before;
    296         struct timeval time_after;
    297 
    298         int rc;
    299 
    300         sockets = 10;
    301         messages = 10;
    302         port = 7;
    303 
    304301        /*
    305302         * Parse the command line arguments. Stop before the last argument
    306303         * if it does not start with dash ('-')
    307304         */
    308         for (index = 1; (index < argc - 1) || ((index == argc - 1) && (argv[index][0] == '-')); index++) {
     305        int index;
     306        int rc;
     307       
     308        for (index = 1; (index < argc - 1) || ((index == argc - 1) &&
     309            (argv[index][0] == '-')); index++) {
    309310                /* Options should start with dash ('-') */
    310311                if (argv[index][0] == '-') {
     
    317318                }
    318319        }
    319 
    320         /* If not before the last argument containing the address */
     320       
     321        /* The last argument containing the host */
    321322        if (index >= argc) {
    322                 printf("Command line error: missing address\n");
     323                printf("Host name missing.\n");
    323324                nettest1_print_help();
    324325                return EINVAL;
    325326        }
    326 
     327       
     328        char *addr_s = argv[argc - 1];
     329       
     330        /* Interpret as address */
     331        inet_addr_t addr_addr;
     332        rc = inet_addr_parse(addr_s, &addr_addr);
     333       
     334        if (rc != EOK) {
     335                /* Interpret as a host name */
     336                dnsr_hostinfo_t *hinfo = NULL;
     337                rc = dnsr_name2host(addr_s, &hinfo);
     338               
     339                if (rc != EOK) {
     340                        printf("Error resolving host '%s'.\n", addr_s);
     341                        return EINVAL;
     342                }
     343               
     344                addr_addr = hinfo->addr;
     345        }
     346       
     347        struct sockaddr_in addr;
     348        struct sockaddr_in6 addr6;
     349        uint16_t af = inet_addr_sockaddr_in(&addr_addr, &addr, &addr6);
     350       
     351        if (af != family) {
     352                printf("Address family does not match explicitly set family.\n");
     353                return EINVAL;
     354        }
     355       
    327356        /* Prepare the address buffer */
    328 
    329         switch (family) {
    330         case PF_INET:
    331                 address_in.sin_family = AF_INET;
    332                 address_in.sin_port = htons(port);
    333                 address = (struct sockaddr *) &address_in;
    334                 addrlen = sizeof(address_in);
    335                 address_start = (uint8_t *) &address_in.sin_addr.s_addr;
    336                 break;
    337         case PF_INET6:
    338                 address_in6.sin6_family = AF_INET6;
    339                 address_in6.sin6_port = htons(port);
    340                 address = (struct sockaddr *) &address_in6;
    341                 addrlen = sizeof(address_in6);
    342                 address_start = (uint8_t *) &address_in6.sin6_addr.s6_addr;
     357       
     358        switch (af) {
     359        case AF_INET:
     360                addr.sin_port = htons(port);
     361                address = (struct sockaddr *) &addr;
     362                addrlen = sizeof(addr);
     363                break;
     364        case AF_INET6:
     365                addr6.sin6_port = htons(port);
     366                address = (struct sockaddr *) &addr6;
     367                addrlen = sizeof(addr6);
    343368                break;
    344369        default:
     
    346371                return EAFNOSUPPORT;
    347372        }
    348 
    349         /* Parse the last argument which should contain the address */
    350         rc = inet_pton(family, argv[argc - 1], address_start);
    351         if (rc != EOK) {
    352                 fprintf(stderr, "Address parse error %d\n", rc);
    353                 return rc;
    354         }
    355 
     373       
    356374        /* Check data buffer size */
    357375        if (size <= 0) {
     
    360378                size = 1024;
    361379        }
    362 
     380       
    363381        /*
    364382         * Prepare data buffer. Allocate size bytes plus one for the
     
    371389        }
    372390        nettest1_fill_buffer(data, size);
    373 
     391       
    374392        /* Check socket count */
    375393        if (sockets <= 0) {
     
    378396                sockets = 2;
    379397        }
    380 
     398       
    381399        /*
    382400         * Prepare socket buffer. Allocate count fields plus the terminating
    383401         * null (\0).
    384402         */
    385         socket_ids = (int *) malloc(sizeof(int) * (sockets + 1));
     403        int *socket_ids = (int *) malloc(sizeof(int) * (sockets + 1));
    386404        if (!socket_ids) {
    387405                fprintf(stderr, "Failed to allocate receive buffer.\n");
    388406                return ENOMEM;
    389407        }
     408       
    390409        socket_ids[sockets] = 0;
    391 
     410       
    392411        if (verbose)
    393412                printf("Starting tests\n");
    394 
     413       
     414        struct timeval time_before;
    395415        rc = gettimeofday(&time_before, NULL);
    396416        if (rc != EOK) {
     
    398418                return rc;
    399419        }
    400 
     420       
    401421        nettest1_test(socket_ids,       1,        1);
    402422        nettest1_test(socket_ids,       1, messages);
    403423        nettest1_test(socket_ids, sockets,        1);
    404424        nettest1_test(socket_ids, sockets, messages);
    405 
     425       
     426        struct timeval time_after;
    406427        rc = gettimeofday(&time_after, NULL);
    407428        if (rc != EOK) {
     
    409430                return rc;
    410431        }
    411 
     432       
    412433        printf("Tested in %ld microseconds\n", tv_sub(&time_after,
    413434            &time_before));
    414 
     435       
    415436        if (verbose)
    416437                printf("Exiting\n");
    417 
     438       
    418439        return EOK;
    419440}
    420441
    421 
    422442/** @}
    423443 */
Note: See TracChangeset for help on using the changeset viewer.