Ignore:
File:
1 edited

Legend:

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

    rfff7ef4 r26de91a  
    3838#include "print_error.h"
    3939
     40#include <assert.h>
    4041#include <malloc.h>
    4142#include <stdio.h>
     
    5455
    5556/** Echo module name. */
    56 #define NAME    "Nettest1"
     57#define NAME  "nettest1"
    5758
    5859/** Packet data pattern. */
    59 #define NETTEST1_TEXT   "Networking test 1 - sockets"
    60 
    61 static int family = PF_INET;
     60#define NETTEST1_TEXT  "Networking test 1 - sockets"
     61
     62static uint16_t family = AF_NONE;
    6263static sock_type_t type = SOCK_DGRAM;
    63 static char *data;
    6464static size_t size = 27;
    65 static int verbose = 0;
     65static bool verbose = false;
     66static int sockets = 10;
     67static int messages = 10;
     68static uint16_t port = 7;
    6669
    6770static struct sockaddr *address;
    6871static socklen_t addrlen;
    6972
    70 static int sockets;
    71 static int messages;
    72 static uint16_t port;
     73static char *data;
    7374
    7475static void nettest1_print_help(void)
     
    114115        int value;
    115116        int rc;
    116 
     117       
    117118        switch (argv[*index][1]) {
    118119        /*
     
    120121         */
    121122        case 'f':
    122                 rc = arg_parse_name_int(argc, argv, index, &family, 0, socket_parse_protocol_family);
    123                 if (rc != EOK)
    124                         return rc;
     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;
    125129                break;
    126130        case 'h':
     
    131135                if (rc != EOK)
    132136                        return rc;
     137               
    133138                break;
    134139        case 'n':
     
    136141                if (rc != EOK)
    137142                        return rc;
     143               
    138144                break;
    139145        case 'p':
     
    141147                if (rc != EOK)
    142148                        return rc;
     149               
    143150                port = (uint16_t) value;
    144151                break;
     
    147154                if (rc != EOK)
    148155                        return rc;
     156               
    149157                size = (value >= 0) ? (size_t) value : 0;
    150158                break;
    151159        case 't':
    152                 rc = arg_parse_name_int(argc, argv, index, &value, 0, socket_parse_socket_type);
    153                 if (rc != EOK)
    154                         return rc;
     160                rc = arg_parse_name_int(argc, argv, index, &value, 0,
     161                    socket_parse_socket_type);
     162                if (rc != EOK)
     163                        return rc;
     164               
    155165                type = (sock_type_t) value;
    156166                break;
     
    158168                verbose = 1;
    159169                break;
     170       
    160171        /*
    161172         * Long options with double dash ('-')
     
    163174        case '-':
    164175                if (str_lcmp(argv[*index] + 2, "family=", 7) == 0) {
    165                         rc = arg_parse_name_int(argc, argv, index, &family, 9,
     176                        rc = arg_parse_name_int(argc, argv, index, &value, 9,
    166177                            socket_parse_protocol_family);
    167178                        if (rc != EOK)
    168179                                return rc;
     180                       
     181                        family = (uint16_t) value;
    169182                } else if (str_lcmp(argv[*index] + 2, "help", 5) == 0) {
    170183                        nettest1_print_help();
     
    182195                        if (rc != EOK)
    183196                                return rc;
     197                       
    184198                        port = (uint16_t) value;
    185199                } else if (str_lcmp(argv[*index] + 2, "type=", 5) == 0) {
     
    188202                        if (rc != EOK)
    189203                                return rc;
     204                       
    190205                        type = (sock_type_t) value;
    191206                } else if (str_lcmp(argv[*index] + 2, "verbose", 8) == 0) {
     
    200215                return EINVAL;
    201216        }
    202 
     217       
    203218        return EOK;
    204219}
     
    211226static void nettest1_fill_buffer(char *buffer, size_t size)
    212227{
    213         size_t length;
    214 
    215         length = 0;
     228        size_t length = 0;
    216229        while (size > length + sizeof(NETTEST1_TEXT) - 1) {
    217230                memcpy(buffer + length, NETTEST1_TEXT,
     
    219232                length += sizeof(NETTEST1_TEXT) - 1;
    220233        }
    221 
     234       
    222235        memcpy(buffer + length, NETTEST1_TEXT, size - length);
    223236        buffer[size] = '\0';
     
    226239static int nettest1_test(int *socket_ids, int nsockets, int nmessages)
    227240{
    228         int rc;
    229 
    230241        if (verbose)
    231242                printf("%d sockets, %d messages\n", nsockets, nmessages);
    232 
    233         rc = sockets_create(verbose, socket_ids, nsockets, family, type);
    234         if (rc != EOK)
    235                 return rc;
    236 
     243       
     244        int rc = sockets_create(verbose, socket_ids, nsockets, family, type);
     245        if (rc != EOK)
     246                return rc;
     247       
    237248        if (type == SOCK_STREAM) {
    238249                rc = sockets_connect(verbose, socket_ids, nsockets, address,
     
    241252                        return rc;
    242253        }
    243 
     254       
    244255        rc = sockets_sendto_recvfrom(verbose, socket_ids, nsockets, address,
    245             &addrlen, data, size, nmessages);
    246         if (rc != EOK)
    247                 return rc;
    248 
     256            &addrlen, data, size, nmessages, type);
     257        if (rc != EOK)
     258                return rc;
     259       
    249260        rc = sockets_close(verbose, socket_ids, nsockets);
    250261        if (rc != EOK)
    251262                return rc;
    252 
     263       
    253264        if (verbose)
    254265                printf("\tOK\n");
    255 
     266       
    256267        /****/
    257 
     268       
    258269        rc = sockets_create(verbose, socket_ids, nsockets, family, type);
    259270        if (rc != EOK)
    260271                return rc;
    261 
     272       
    262273        if (type == SOCK_STREAM) {
    263274                rc = sockets_connect(verbose, socket_ids, nsockets, address,
     
    266277                        return rc;
    267278        }
    268 
     279       
    269280        rc = sockets_sendto(verbose, socket_ids, nsockets, address, addrlen,
    270             data, size, nmessages);
    271         if (rc != EOK)
    272                 return rc;
    273 
     281            data, size, nmessages, type);
     282        if (rc != EOK)
     283                return rc;
     284       
    274285        rc = sockets_recvfrom(verbose, socket_ids, nsockets, address, &addrlen,
    275286            data, size, nmessages);
    276287        if (rc != EOK)
    277288                return rc;
    278 
     289       
    279290        rc = sockets_close(verbose, socket_ids, nsockets);
    280291        if (rc != EOK)
    281292                return rc;
    282 
     293       
    283294        if (verbose)
    284295                printf("\tOK\n");
    285 
     296       
    286297        return EOK;
    287298}
     
    289300int main(int argc, char *argv[])
    290301{
    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 
    307302        /*
    308303         * Parse the command line arguments. Stop before the last argument
    309304         * if it does not start with dash ('-')
    310305         */
    311         for (index = 1; (index < argc - 1) || ((index == argc - 1) && (argv[index][0] == '-')); index++) {
     306        int index;
     307        int rc;
     308       
     309        for (index = 1; (index < argc - 1) || ((index == argc - 1) &&
     310            (argv[index][0] == '-')); index++) {
    312311                /* Options should start with dash ('-') */
    313312                if (argv[index][0] == '-') {
     
    320319                }
    321320        }
    322 
    323         /* If not before the last argument containing the host */
     321       
     322        /* The last argument containing the host */
    324323        if (index >= argc) {
    325                 printf("Command line error: missing host name\n");
     324                printf("Host name missing.\n");
    326325                nettest1_print_help();
    327326                return EINVAL;
    328327        }
    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);
     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       
    354335        if (rc != EOK) {
    355                 /* Try interpreting as a host name */
    356                 rc = dnsr_name2host(argv[argc - 1], &hinfo);
     336                /* Interpret as a host name */
     337                dnsr_hostinfo_t *hinfo = NULL;
     338                rc = dnsr_name2host(host, &hinfo, ipver_from_af(family));
     339               
    357340                if (rc != EOK) {
    358                         printf("Error resolving host '%s'.\n", argv[argc - 1]);
    359                         return rc;
     341                        printf("Error resolving host '%s'.\n", host);
     342                        return EINVAL;
    360343                }
    361 
    362                 address_in.sin_addr.s_addr = host2uint32_t_be(hinfo->addr.ipv4);
    363         }
    364 
     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       
    365363        /* Check data buffer size */
    366364        if (size <= 0) {
     
    369367                size = 1024;
    370368        }
    371 
     369       
    372370        /*
    373371         * Prepare data buffer. Allocate size bytes plus one for the
     
    380378        }
    381379        nettest1_fill_buffer(data, size);
    382 
     380       
    383381        /* Check socket count */
    384382        if (sockets <= 0) {
     
    387385                sockets = 2;
    388386        }
    389 
     387       
    390388        /*
    391389         * Prepare socket buffer. Allocate count fields plus the terminating
    392390         * null (\0).
    393391         */
    394         socket_ids = (int *) malloc(sizeof(int) * (sockets + 1));
     392        int *socket_ids = (int *) malloc(sizeof(int) * (sockets + 1));
    395393        if (!socket_ids) {
    396394                fprintf(stderr, "Failed to allocate receive buffer.\n");
    397395                return ENOMEM;
    398396        }
     397       
    399398        socket_ids[sockets] = 0;
    400 
     399       
    401400        if (verbose)
    402401                printf("Starting tests\n");
    403 
     402       
     403        struct timeval time_before;
    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 
     414       
     415        struct timeval time_after;
    415416        rc = gettimeofday(&time_after, NULL);
    416417        if (rc != EOK) {
     
    418419                return rc;
    419420        }
    420 
     421       
    421422        printf("Tested in %ld microseconds\n", tv_sub(&time_after,
    422423            &time_before));
    423 
     424       
     425        free(address);
     426       
    424427        if (verbose)
    425428                printf("Exiting\n");
    426 
     429       
    427430        return EOK;
    428431}
    429432
    430 
    431433/** @}
    432434 */
Note: See TracChangeset for help on using the changeset viewer.