Ignore:
File:
1 edited

Legend:

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

    rd4d74dc r26de91a  
    3838#include "print_error.h"
    3939
     40#include <assert.h>
    4041#include <malloc.h>
    4142#include <stdio.h>
     
    4647#include <arg_parse.h>
    4748
     49#include <inet/dnsr.h>
    4850#include <net/in.h>
    4951#include <net/in6.h>
     
    5355
    5456/** Echo module name. */
    55 #define NAME    "Nettest1"
     57#define NAME  "nettest1"
    5658
    5759/** Packet data pattern. */
    58 #define NETTEST1_TEXT   "Networking test 1 - sockets"
    59 
    60 static int family = PF_INET;
     60#define NETTEST1_TEXT  "Networking test 1 - sockets"
     61
     62static uint16_t family = AF_NONE;
    6163static sock_type_t type = SOCK_DGRAM;
    62 static char *data;
    6364static size_t size = 27;
    64 static int verbose = 0;
     65static bool verbose = false;
     66static int sockets = 10;
     67static int messages = 10;
     68static uint16_t port = 7;
    6569
    6670static struct sockaddr *address;
    6771static socklen_t addrlen;
    6872
    69 static int sockets;
    70 static int messages;
    71 static uint16_t port;
     73static char *data;
    7274
    7375static void nettest1_print_help(void)
     
    7577        printf(
    7678            "Network Networking test 1 aplication - sockets\n"
    77             "Usage: echo [options] numeric_address\n"
     79            "Usage: nettest1 [options] host\n"
    7880            "Where options are:\n"
    7981            "-f protocol_family | --family=protocol_family\n"
     
    113115        int value;
    114116        int rc;
    115 
     117       
    116118        switch (argv[*index][1]) {
    117119        /*
     
    119121         */
    120122        case 'f':
    121                 rc = arg_parse_name_int(argc, argv, index, &family, 0, socket_parse_protocol_family);
    122                 if (rc != EOK)
    123                         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;
    124129                break;
    125130        case 'h':
     
    130135                if (rc != EOK)
    131136                        return rc;
     137               
    132138                break;
    133139        case 'n':
     
    135141                if (rc != EOK)
    136142                        return rc;
     143               
    137144                break;
    138145        case 'p':
     
    140147                if (rc != EOK)
    141148                        return rc;
     149               
    142150                port = (uint16_t) value;
    143151                break;
     
    146154                if (rc != EOK)
    147155                        return rc;
     156               
    148157                size = (value >= 0) ? (size_t) value : 0;
    149158                break;
    150159        case 't':
    151                 rc = arg_parse_name_int(argc, argv, index, &value, 0, socket_parse_socket_type);
    152                 if (rc != EOK)
    153                         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               
    154165                type = (sock_type_t) value;
    155166                break;
     
    157168                verbose = 1;
    158169                break;
     170       
    159171        /*
    160172         * Long options with double dash ('-')
     
    162174        case '-':
    163175                if (str_lcmp(argv[*index] + 2, "family=", 7) == 0) {
    164                         rc = arg_parse_name_int(argc, argv, index, &family, 9,
     176                        rc = arg_parse_name_int(argc, argv, index, &value, 9,
    165177                            socket_parse_protocol_family);
    166178                        if (rc != EOK)
    167179                                return rc;
     180                       
     181                        family = (uint16_t) value;
    168182                } else if (str_lcmp(argv[*index] + 2, "help", 5) == 0) {
    169183                        nettest1_print_help();
     
    181195                        if (rc != EOK)
    182196                                return rc;
     197                       
    183198                        port = (uint16_t) value;
    184199                } else if (str_lcmp(argv[*index] + 2, "type=", 5) == 0) {
     
    187202                        if (rc != EOK)
    188203                                return rc;
     204                       
    189205                        type = (sock_type_t) value;
    190206                } else if (str_lcmp(argv[*index] + 2, "verbose", 8) == 0) {
     
    199215                return EINVAL;
    200216        }
    201 
     217       
    202218        return EOK;
    203219}
     
    210226static void nettest1_fill_buffer(char *buffer, size_t size)
    211227{
    212         size_t length;
    213 
    214         length = 0;
     228        size_t length = 0;
    215229        while (size > length + sizeof(NETTEST1_TEXT) - 1) {
    216230                memcpy(buffer + length, NETTEST1_TEXT,
     
    218232                length += sizeof(NETTEST1_TEXT) - 1;
    219233        }
    220 
     234       
    221235        memcpy(buffer + length, NETTEST1_TEXT, size - length);
    222236        buffer[size] = '\0';
     
    225239static int nettest1_test(int *socket_ids, int nsockets, int nmessages)
    226240{
    227         int rc;
    228 
    229241        if (verbose)
    230242                printf("%d sockets, %d messages\n", nsockets, nmessages);
    231 
    232         rc = sockets_create(verbose, socket_ids, nsockets, family, type);
    233         if (rc != EOK)
    234                 return rc;
    235 
     243       
     244        int rc = sockets_create(verbose, socket_ids, nsockets, family, type);
     245        if (rc != EOK)
     246                return rc;
     247       
    236248        if (type == SOCK_STREAM) {
    237249                rc = sockets_connect(verbose, socket_ids, nsockets, address,
     
    240252                        return rc;
    241253        }
    242 
     254       
    243255        rc = sockets_sendto_recvfrom(verbose, socket_ids, nsockets, address,
    244             &addrlen, data, size, nmessages);
    245         if (rc != EOK)
    246                 return rc;
    247 
     256            &addrlen, data, size, nmessages, type);
     257        if (rc != EOK)
     258                return rc;
     259       
    248260        rc = sockets_close(verbose, socket_ids, nsockets);
    249261        if (rc != EOK)
    250262                return rc;
    251 
     263       
    252264        if (verbose)
    253265                printf("\tOK\n");
    254 
     266       
    255267        /****/
    256 
     268       
    257269        rc = sockets_create(verbose, socket_ids, nsockets, family, type);
    258270        if (rc != EOK)
    259271                return rc;
    260 
     272       
    261273        if (type == SOCK_STREAM) {
    262274                rc = sockets_connect(verbose, socket_ids, nsockets, address,
     
    265277                        return rc;
    266278        }
    267 
     279       
    268280        rc = sockets_sendto(verbose, socket_ids, nsockets, address, addrlen,
    269             data, size, nmessages);
    270         if (rc != EOK)
    271                 return rc;
    272 
     281            data, size, nmessages, type);
     282        if (rc != EOK)
     283                return rc;
     284       
    273285        rc = sockets_recvfrom(verbose, socket_ids, nsockets, address, &addrlen,
    274286            data, size, nmessages);
    275287        if (rc != EOK)
    276288                return rc;
    277 
     289       
    278290        rc = sockets_close(verbose, socket_ids, nsockets);
    279291        if (rc != EOK)
    280292                return rc;
    281 
     293       
    282294        if (verbose)
    283295                printf("\tOK\n");
    284 
     296       
    285297        return EOK;
    286298}
     
    288300int main(int argc, char *argv[])
    289301{
    290         struct sockaddr_in address_in;
    291         struct sockaddr_in6 address_in6;
    292         uint8_t *address_start;
    293 
    294         int *socket_ids;
    295         int index;
    296         struct timeval time_before;
    297         struct timeval time_after;
    298 
    299         int rc;
    300 
    301         sockets = 10;
    302         messages = 10;
    303         port = 7;
    304 
    305302        /*
    306303         * Parse the command line arguments. Stop before the last argument
    307304         * if it does not start with dash ('-')
    308305         */
    309         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++) {
    310311                /* Options should start with dash ('-') */
    311312                if (argv[index][0] == '-') {
     
    318319                }
    319320        }
    320 
    321         /* If not before the last argument containing the address */
     321       
     322        /* The last argument containing the host */
    322323        if (index >= argc) {
    323                 printf("Command line error: missing address\n");
     324                printf("Host name missing.\n");
    324325                nettest1_print_help();
    325326                return EINVAL;
    326327        }
    327 
    328         /* Prepare the address buffer */
    329 
    330         switch (family) {
    331         case PF_INET:
    332                 address_in.sin_family = AF_INET;
    333                 address_in.sin_port = htons(port);
    334                 address = (struct sockaddr *) &address_in;
    335                 addrlen = sizeof(address_in);
    336                 address_start = (uint8_t *) &address_in.sin_addr.s_addr;
    337                 break;
    338         case PF_INET6:
    339                 address_in6.sin6_family = AF_INET6;
    340                 address_in6.sin6_port = htons(port);
    341                 address = (struct sockaddr *) &address_in6;
    342                 addrlen = sizeof(address_in6);
    343                 address_start = (uint8_t *) &address_in6.sin6_addr.s6_addr;
    344                 break;
    345         default:
    346                 fprintf(stderr, "Address family is not supported\n");
    347                 return EAFNOSUPPORT;
    348         }
    349 
    350         /* Parse the last argument which should contain the address */
    351         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       
    352335        if (rc != EOK) {
    353                 fprintf(stderr, "Address parse error %d\n", rc);
    354                 return rc;
    355         }
    356 
     336                /* Interpret as a host name */
     337                dnsr_hostinfo_t *hinfo = NULL;
     338                rc = dnsr_name2host(host, &hinfo, ipver_from_af(family));
     339               
     340                if (rc != EOK) {
     341                        printf("Error resolving host '%s'.\n", host);
     342                        return EINVAL;
     343                }
     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       
    357363        /* Check data buffer size */
    358364        if (size <= 0) {
     
    361367                size = 1024;
    362368        }
    363 
     369       
    364370        /*
    365371         * Prepare data buffer. Allocate size bytes plus one for the
     
    372378        }
    373379        nettest1_fill_buffer(data, size);
    374 
     380       
    375381        /* Check socket count */
    376382        if (sockets <= 0) {
     
    379385                sockets = 2;
    380386        }
    381 
     387       
    382388        /*
    383389         * Prepare socket buffer. Allocate count fields plus the terminating
    384390         * null (\0).
    385391         */
    386         socket_ids = (int *) malloc(sizeof(int) * (sockets + 1));
     392        int *socket_ids = (int *) malloc(sizeof(int) * (sockets + 1));
    387393        if (!socket_ids) {
    388394                fprintf(stderr, "Failed to allocate receive buffer.\n");
    389395                return ENOMEM;
    390396        }
     397       
    391398        socket_ids[sockets] = 0;
    392 
     399       
    393400        if (verbose)
    394401                printf("Starting tests\n");
    395 
     402       
     403        struct timeval time_before;
    396404        rc = gettimeofday(&time_before, NULL);
    397405        if (rc != EOK) {
     
    399407                return rc;
    400408        }
    401 
     409       
    402410        nettest1_test(socket_ids,       1,        1);
    403411        nettest1_test(socket_ids,       1, messages);
    404412        nettest1_test(socket_ids, sockets,        1);
    405413        nettest1_test(socket_ids, sockets, messages);
    406 
     414       
     415        struct timeval time_after;
    407416        rc = gettimeofday(&time_after, NULL);
    408417        if (rc != EOK) {
     
    410419                return rc;
    411420        }
    412 
     421       
    413422        printf("Tested in %ld microseconds\n", tv_sub(&time_after,
    414423            &time_before));
    415 
     424       
     425        free(address);
     426       
    416427        if (verbose)
    417428                printf("Exiting\n");
    418 
     429       
    419430        return EOK;
    420431}
    421432
    422 
    423433/** @}
    424434 */
Note: See TracChangeset for help on using the changeset viewer.