Ignore:
File:
1 edited

Legend:

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

    r26de91a rfff7ef4  
    3838#include "print_error.h"
    3939
    40 #include <assert.h>
    4140#include <malloc.h>
    4241#include <stdio.h>
     
    5554
    5655/** Echo module name. */
    57 #define NAME  "nettest1"
     56#define NAME    "Nettest1"
    5857
    5958/** Packet data pattern. */
    60 #define NETTEST1_TEXT  "Networking test 1 - sockets"
    61 
    62 static uint16_t family = AF_NONE;
     59#define NETTEST1_TEXT   "Networking test 1 - sockets"
     60
     61static int family = PF_INET;
    6362static sock_type_t type = SOCK_DGRAM;
     63static char *data;
    6464static size_t size = 27;
    65 static bool verbose = false;
    66 static int sockets = 10;
    67 static int messages = 10;
    68 static uint16_t port = 7;
     65static int verbose = 0;
    6966
    7067static struct sockaddr *address;
    7168static socklen_t addrlen;
    7269
    73 static char *data;
     70static int sockets;
     71static int messages;
     72static uint16_t port;
    7473
    7574static void nettest1_print_help(void)
     
    115114        int value;
    116115        int rc;
    117        
     116
    118117        switch (argv[*index][1]) {
    119118        /*
     
    121120         */
    122121        case 'f':
    123                 rc = arg_parse_name_int(argc, argv, index, &value, 0,
    124                     socket_parse_protocol_family);
    125                 if (rc != EOK)
    126                         return rc;
    127                
    128                 family = (uint16_t) value;
     122                rc = arg_parse_name_int(argc, argv, index, &family, 0, socket_parse_protocol_family);
     123                if (rc != EOK)
     124                        return rc;
    129125                break;
    130126        case 'h':
     
    135131                if (rc != EOK)
    136132                        return rc;
    137                
    138133                break;
    139134        case 'n':
     
    141136                if (rc != EOK)
    142137                        return rc;
    143                
    144138                break;
    145139        case 'p':
     
    147141                if (rc != EOK)
    148142                        return rc;
    149                
    150143                port = (uint16_t) value;
    151144                break;
     
    154147                if (rc != EOK)
    155148                        return rc;
    156                
    157149                size = (value >= 0) ? (size_t) value : 0;
    158150                break;
    159151        case 't':
    160                 rc = arg_parse_name_int(argc, argv, index, &value, 0,
    161                     socket_parse_socket_type);
    162                 if (rc != EOK)
    163                         return rc;
    164                
     152                rc = arg_parse_name_int(argc, argv, index, &value, 0, socket_parse_socket_type);
     153                if (rc != EOK)
     154                        return rc;
    165155                type = (sock_type_t) value;
    166156                break;
     
    168158                verbose = 1;
    169159                break;
    170        
    171160        /*
    172161         * Long options with double dash ('-')
     
    174163        case '-':
    175164                if (str_lcmp(argv[*index] + 2, "family=", 7) == 0) {
    176                         rc = arg_parse_name_int(argc, argv, index, &value, 9,
     165                        rc = arg_parse_name_int(argc, argv, index, &family, 9,
    177166                            socket_parse_protocol_family);
    178167                        if (rc != EOK)
    179168                                return rc;
    180                        
    181                         family = (uint16_t) value;
    182169                } else if (str_lcmp(argv[*index] + 2, "help", 5) == 0) {
    183170                        nettest1_print_help();
     
    195182                        if (rc != EOK)
    196183                                return rc;
    197                        
    198184                        port = (uint16_t) value;
    199185                } else if (str_lcmp(argv[*index] + 2, "type=", 5) == 0) {
     
    202188                        if (rc != EOK)
    203189                                return rc;
    204                        
    205190                        type = (sock_type_t) value;
    206191                } else if (str_lcmp(argv[*index] + 2, "verbose", 8) == 0) {
     
    215200                return EINVAL;
    216201        }
    217        
     202
    218203        return EOK;
    219204}
     
    226211static void nettest1_fill_buffer(char *buffer, size_t size)
    227212{
    228         size_t length = 0;
     213        size_t length;
     214
     215        length = 0;
    229216        while (size > length + sizeof(NETTEST1_TEXT) - 1) {
    230217                memcpy(buffer + length, NETTEST1_TEXT,
     
    232219                length += sizeof(NETTEST1_TEXT) - 1;
    233220        }
    234        
     221
    235222        memcpy(buffer + length, NETTEST1_TEXT, size - length);
    236223        buffer[size] = '\0';
     
    239226static int nettest1_test(int *socket_ids, int nsockets, int nmessages)
    240227{
     228        int rc;
     229
    241230        if (verbose)
    242231                printf("%d sockets, %d messages\n", nsockets, nmessages);
    243        
    244         int rc = sockets_create(verbose, socket_ids, nsockets, family, type);
    245         if (rc != EOK)
    246                 return rc;
    247        
     232
     233        rc = sockets_create(verbose, socket_ids, nsockets, family, type);
     234        if (rc != EOK)
     235                return rc;
     236
    248237        if (type == SOCK_STREAM) {
    249238                rc = sockets_connect(verbose, socket_ids, nsockets, address,
     
    252241                        return rc;
    253242        }
    254        
     243
    255244        rc = sockets_sendto_recvfrom(verbose, socket_ids, nsockets, address,
    256             &addrlen, data, size, nmessages, type);
    257         if (rc != EOK)
    258                 return rc;
    259        
     245            &addrlen, data, size, nmessages);
     246        if (rc != EOK)
     247                return rc;
     248
    260249        rc = sockets_close(verbose, socket_ids, nsockets);
    261250        if (rc != EOK)
    262251                return rc;
    263        
     252
    264253        if (verbose)
    265254                printf("\tOK\n");
    266        
     255
    267256        /****/
    268        
     257
    269258        rc = sockets_create(verbose, socket_ids, nsockets, family, type);
    270259        if (rc != EOK)
    271260                return rc;
    272        
     261
    273262        if (type == SOCK_STREAM) {
    274263                rc = sockets_connect(verbose, socket_ids, nsockets, address,
     
    277266                        return rc;
    278267        }
    279        
     268
    280269        rc = sockets_sendto(verbose, socket_ids, nsockets, address, addrlen,
    281             data, size, nmessages, type);
    282         if (rc != EOK)
    283                 return rc;
    284        
     270            data, size, nmessages);
     271        if (rc != EOK)
     272                return rc;
     273
    285274        rc = sockets_recvfrom(verbose, socket_ids, nsockets, address, &addrlen,
    286275            data, size, nmessages);
    287276        if (rc != EOK)
    288277                return rc;
    289        
     278
    290279        rc = sockets_close(verbose, socket_ids, nsockets);
    291280        if (rc != EOK)
    292281                return rc;
    293        
     282
    294283        if (verbose)
    295284                printf("\tOK\n");
    296        
     285
    297286        return EOK;
    298287}
     
    300289int main(int argc, char *argv[])
    301290{
     291        struct sockaddr_in address_in;
     292        struct sockaddr_in6 address_in6;
     293        dnsr_hostinfo_t *hinfo;
     294        uint8_t *address_start;
     295
     296        int *socket_ids;
     297        int index;
     298        struct timeval time_before;
     299        struct timeval time_after;
     300
     301        int rc;
     302
     303        sockets = 10;
     304        messages = 10;
     305        port = 7;
     306
    302307        /*
    303308         * Parse the command line arguments. Stop before the last argument
    304309         * if it does not start with dash ('-')
    305310         */
    306         int index;
    307         int rc;
    308        
    309         for (index = 1; (index < argc - 1) || ((index == argc - 1) &&
    310             (argv[index][0] == '-')); index++) {
     311        for (index = 1; (index < argc - 1) || ((index == argc - 1) && (argv[index][0] == '-')); index++) {
    311312                /* Options should start with dash ('-') */
    312313                if (argv[index][0] == '-') {
     
    319320                }
    320321        }
    321        
    322         /* The last argument containing the host */
     322
     323        /* If not before the last argument containing the host */
    323324        if (index >= argc) {
    324                 printf("Host name missing.\n");
     325                printf("Command line error: missing host name\n");
    325326                nettest1_print_help();
    326327                return EINVAL;
    327328        }
    328        
    329         char *host = argv[argc - 1];
    330        
    331         /* Interpret as address */
    332         inet_addr_t iaddr;
    333         rc = inet_addr_parse(host, &iaddr);
    334        
     329
     330        /* Prepare the address buffer */
     331
     332        switch (family) {
     333        case PF_INET:
     334                address_in.sin_family = AF_INET;
     335                address_in.sin_port = htons(port);
     336                address = (struct sockaddr *) &address_in;
     337                addrlen = sizeof(address_in);
     338                address_start = (uint8_t *) &address_in.sin_addr.s_addr;
     339                break;
     340        case PF_INET6:
     341                address_in6.sin6_family = AF_INET6;
     342                address_in6.sin6_port = htons(port);
     343                address = (struct sockaddr *) &address_in6;
     344                addrlen = sizeof(address_in6);
     345                address_start = (uint8_t *) &address_in6.sin6_addr.s6_addr;
     346                break;
     347        default:
     348                fprintf(stderr, "Address family is not supported\n");
     349                return EAFNOSUPPORT;
     350        }
     351
     352        /* Parse the last argument which should contain the host/address */
     353        rc = inet_pton(family, argv[argc - 1], address_start);
    335354        if (rc != EOK) {
    336                 /* Interpret as a host name */
    337                 dnsr_hostinfo_t *hinfo = NULL;
    338                 rc = dnsr_name2host(host, &hinfo, ipver_from_af(family));
    339                
     355                /* Try interpreting as a host name */
     356                rc = dnsr_name2host(argv[argc - 1], &hinfo);
    340357                if (rc != EOK) {
    341                         printf("Error resolving host '%s'.\n", host);
    342                         return EINVAL;
     358                        printf("Error resolving host '%s'.\n", argv[argc - 1]);
     359                        return rc;
    343360                }
    344                
    345                 iaddr = hinfo->addr;
    346         }
    347        
    348         rc = inet_addr_sockaddr(&iaddr, port, &address, &addrlen);
    349         if (rc != EOK) {
    350                 assert(rc == ENOMEM);
    351                 printf("Out of memory.\n");
    352                 return ENOMEM;
    353         }
    354        
    355         if (family == AF_NONE)
    356                 family = address->sa_family;
    357        
    358         if (address->sa_family != family) {
    359                 printf("Address family does not match explicitly set family.\n");
    360                 return EINVAL;
    361         }
    362        
     361
     362                address_in.sin_addr.s_addr = host2uint32_t_be(hinfo->addr.ipv4);
     363        }
     364
    363365        /* Check data buffer size */
    364366        if (size <= 0) {
     
    367369                size = 1024;
    368370        }
    369        
     371
    370372        /*
    371373         * Prepare data buffer. Allocate size bytes plus one for the
     
    378380        }
    379381        nettest1_fill_buffer(data, size);
    380        
     382
    381383        /* Check socket count */
    382384        if (sockets <= 0) {
     
    385387                sockets = 2;
    386388        }
    387        
     389
    388390        /*
    389391         * Prepare socket buffer. Allocate count fields plus the terminating
    390392         * null (\0).
    391393         */
    392         int *socket_ids = (int *) malloc(sizeof(int) * (sockets + 1));
     394        socket_ids = (int *) malloc(sizeof(int) * (sockets + 1));
    393395        if (!socket_ids) {
    394396                fprintf(stderr, "Failed to allocate receive buffer.\n");
    395397                return ENOMEM;
    396398        }
    397        
    398399        socket_ids[sockets] = 0;
    399        
     400
    400401        if (verbose)
    401402                printf("Starting tests\n");
    402        
    403         struct timeval time_before;
     403
    404404        rc = gettimeofday(&time_before, NULL);
    405405        if (rc != EOK) {
     
    407407                return rc;
    408408        }
    409        
     409
    410410        nettest1_test(socket_ids,       1,        1);
    411411        nettest1_test(socket_ids,       1, messages);
    412412        nettest1_test(socket_ids, sockets,        1);
    413413        nettest1_test(socket_ids, sockets, messages);
    414        
    415         struct timeval time_after;
     414
    416415        rc = gettimeofday(&time_after, NULL);
    417416        if (rc != EOK) {
     
    419418                return rc;
    420419        }
    421        
     420
    422421        printf("Tested in %ld microseconds\n", tv_sub(&time_after,
    423422            &time_before));
    424        
    425         free(address);
    426        
     423
    427424        if (verbose)
    428425                printf("Exiting\n");
    429        
     426
    430427        return EOK;
    431428}
    432429
     430
    433431/** @}
    434432 */
Note: See TracChangeset for help on using the changeset viewer.