Ignore:
File:
1 edited

Legend:

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

    r02a09ed r7e752b2  
    4040#include <malloc.h>
    4141#include <stdio.h>
    42 #include <unistd.h>
    4342#include <str.h>
    4443#include <task.h>
     
    4645#include <arg_parse.h>
    4746
    48 #include <inet/dnsr.h>
    4947#include <net/in.h>
    5048#include <net/in6.h>
     
    5452
    5553/** Echo module name. */
    56 #define NAME  "nettest1"
     54#define NAME    "Nettest1"
    5755
    5856/** Packet data pattern. */
    59 #define NETTEST1_TEXT  "Networking test 1 - sockets"
    60 
    61 static uint16_t family = AF_INET;
     57#define NETTEST1_TEXT   "Networking test 1 - sockets"
     58
     59static int family = PF_INET;
    6260static sock_type_t type = SOCK_DGRAM;
     61static char *data;
    6362static size_t size = 27;
    64 static bool verbose = false;
    65 static int sockets = 10;
    66 static int messages = 10;
    67 static uint16_t port = 7;
     63static int verbose = 0;
    6864
    6965static struct sockaddr *address;
    7066static socklen_t addrlen;
    7167
    72 static char *data;
     68static int sockets;
     69static int messages;
     70static uint16_t port;
    7371
    7472static void nettest1_print_help(void)
     
    7674        printf(
    7775            "Network Networking test 1 aplication - sockets\n"
    78             "Usage: nettest1 [options] host\n"
     76            "Usage: echo [options] numeric_address\n"
    7977            "Where options are:\n"
    8078            "-f protocol_family | --family=protocol_family\n"
     
    114112        int value;
    115113        int rc;
    116        
     114
    117115        switch (argv[*index][1]) {
    118116        /*
     
    120118         */
    121119        case 'f':
    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;
     120                rc = arg_parse_name_int(argc, argv, index, &family, 0, socket_parse_protocol_family);
     121                if (rc != EOK)
     122                        return rc;
    128123                break;
    129124        case 'h':
     
    134129                if (rc != EOK)
    135130                        return rc;
    136                
    137131                break;
    138132        case 'n':
     
    140134                if (rc != EOK)
    141135                        return rc;
    142                
    143136                break;
    144137        case 'p':
     
    146139                if (rc != EOK)
    147140                        return rc;
    148                
    149141                port = (uint16_t) value;
    150142                break;
     
    153145                if (rc != EOK)
    154146                        return rc;
    155                
    156147                size = (value >= 0) ? (size_t) value : 0;
    157148                break;
    158149        case 't':
    159                 rc = arg_parse_name_int(argc, argv, index, &value, 0,
    160                     socket_parse_socket_type);
    161                 if (rc != EOK)
    162                         return rc;
    163                
     150                rc = arg_parse_name_int(argc, argv, index, &value, 0, socket_parse_socket_type);
     151                if (rc != EOK)
     152                        return rc;
    164153                type = (sock_type_t) value;
    165154                break;
     
    167156                verbose = 1;
    168157                break;
    169        
    170158        /*
    171159         * Long options with double dash ('-')
     
    173161        case '-':
    174162                if (str_lcmp(argv[*index] + 2, "family=", 7) == 0) {
    175                         rc = arg_parse_name_int(argc, argv, index, &value, 9,
     163                        rc = arg_parse_name_int(argc, argv, index, &family, 9,
    176164                            socket_parse_protocol_family);
    177165                        if (rc != EOK)
    178166                                return rc;
    179                        
    180                         family = (uint16_t) value;
    181167                } else if (str_lcmp(argv[*index] + 2, "help", 5) == 0) {
    182168                        nettest1_print_help();
     
    194180                        if (rc != EOK)
    195181                                return rc;
    196                        
    197182                        port = (uint16_t) value;
    198183                } else if (str_lcmp(argv[*index] + 2, "type=", 5) == 0) {
     
    201186                        if (rc != EOK)
    202187                                return rc;
    203                        
    204188                        type = (sock_type_t) value;
    205189                } else if (str_lcmp(argv[*index] + 2, "verbose", 8) == 0) {
     
    214198                return EINVAL;
    215199        }
    216        
     200
    217201        return EOK;
    218202}
     
    225209static void nettest1_fill_buffer(char *buffer, size_t size)
    226210{
    227         size_t length = 0;
     211        size_t length;
     212
     213        length = 0;
    228214        while (size > length + sizeof(NETTEST1_TEXT) - 1) {
    229215                memcpy(buffer + length, NETTEST1_TEXT,
     
    231217                length += sizeof(NETTEST1_TEXT) - 1;
    232218        }
    233        
     219
    234220        memcpy(buffer + length, NETTEST1_TEXT, size - length);
    235221        buffer[size] = '\0';
     
    238224static int nettest1_test(int *socket_ids, int nsockets, int nmessages)
    239225{
     226        int rc;
     227
    240228        if (verbose)
    241229                printf("%d sockets, %d messages\n", nsockets, nmessages);
    242        
    243         int rc = sockets_create(verbose, socket_ids, nsockets, family, type);
    244         if (rc != EOK)
    245                 return rc;
    246        
     230
     231        rc = sockets_create(verbose, socket_ids, nsockets, family, type);
     232        if (rc != EOK)
     233                return rc;
     234
    247235        if (type == SOCK_STREAM) {
    248236                rc = sockets_connect(verbose, socket_ids, nsockets, address,
     
    251239                        return rc;
    252240        }
    253        
     241
    254242        rc = sockets_sendto_recvfrom(verbose, socket_ids, nsockets, address,
    255243            &addrlen, data, size, nmessages);
    256244        if (rc != EOK)
    257245                return rc;
    258        
     246
    259247        rc = sockets_close(verbose, socket_ids, nsockets);
    260248        if (rc != EOK)
    261249                return rc;
    262        
     250
    263251        if (verbose)
    264252                printf("\tOK\n");
    265        
     253
    266254        /****/
    267        
     255
    268256        rc = sockets_create(verbose, socket_ids, nsockets, family, type);
    269257        if (rc != EOK)
    270258                return rc;
    271        
     259
    272260        if (type == SOCK_STREAM) {
    273261                rc = sockets_connect(verbose, socket_ids, nsockets, address,
     
    276264                        return rc;
    277265        }
    278        
     266
    279267        rc = sockets_sendto(verbose, socket_ids, nsockets, address, addrlen,
    280268            data, size, nmessages);
    281269        if (rc != EOK)
    282270                return rc;
    283        
     271
    284272        rc = sockets_recvfrom(verbose, socket_ids, nsockets, address, &addrlen,
    285273            data, size, nmessages);
    286274        if (rc != EOK)
    287275                return rc;
    288        
     276
    289277        rc = sockets_close(verbose, socket_ids, nsockets);
    290278        if (rc != EOK)
    291279                return rc;
    292        
     280
    293281        if (verbose)
    294282                printf("\tOK\n");
    295        
     283
    296284        return EOK;
    297285}
     
    299287int main(int argc, char *argv[])
    300288{
     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
    301304        /*
    302305         * Parse the command line arguments. Stop before the last argument
    303306         * if it does not start with dash ('-')
    304307         */
    305         int index;
    306         int rc;
    307        
    308         for (index = 1; (index < argc - 1) || ((index == argc - 1) &&
    309             (argv[index][0] == '-')); index++) {
     308        for (index = 1; (index < argc - 1) || ((index == argc - 1) && (argv[index][0] == '-')); index++) {
    310309                /* Options should start with dash ('-') */
    311310                if (argv[index][0] == '-') {
     
    318317                }
    319318        }
    320        
    321         /* The last argument containing the host */
     319
     320        /* If not before the last argument containing the address */
    322321        if (index >= argc) {
    323                 printf("Host name missing.\n");
     322                printf("Command line error: missing address\n");
    324323                nettest1_print_help();
    325324                return EINVAL;
    326325        }
    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        
     326
    356327        /* Prepare the address buffer */
    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);
     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;
    368343                break;
    369344        default:
     
    371346                return EAFNOSUPPORT;
    372347        }
    373        
     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
    374356        /* Check data buffer size */
    375357        if (size <= 0) {
     
    378360                size = 1024;
    379361        }
    380        
     362
    381363        /*
    382364         * Prepare data buffer. Allocate size bytes plus one for the
     
    389371        }
    390372        nettest1_fill_buffer(data, size);
    391        
     373
    392374        /* Check socket count */
    393375        if (sockets <= 0) {
     
    396378                sockets = 2;
    397379        }
    398        
     380
    399381        /*
    400382         * Prepare socket buffer. Allocate count fields plus the terminating
    401383         * null (\0).
    402384         */
    403         int *socket_ids = (int *) malloc(sizeof(int) * (sockets + 1));
     385        socket_ids = (int *) malloc(sizeof(int) * (sockets + 1));
    404386        if (!socket_ids) {
    405387                fprintf(stderr, "Failed to allocate receive buffer.\n");
    406388                return ENOMEM;
    407389        }
    408        
    409390        socket_ids[sockets] = 0;
    410        
     391
    411392        if (verbose)
    412393                printf("Starting tests\n");
    413        
    414         struct timeval time_before;
     394
    415395        rc = gettimeofday(&time_before, NULL);
    416396        if (rc != EOK) {
     
    418398                return rc;
    419399        }
    420        
     400
    421401        nettest1_test(socket_ids,       1,        1);
    422402        nettest1_test(socket_ids,       1, messages);
    423403        nettest1_test(socket_ids, sockets,        1);
    424404        nettest1_test(socket_ids, sockets, messages);
    425        
    426         struct timeval time_after;
     405
    427406        rc = gettimeofday(&time_after, NULL);
    428407        if (rc != EOK) {
     
    430409                return rc;
    431410        }
    432        
     411
    433412        printf("Tested in %ld microseconds\n", tv_sub(&time_after,
    434413            &time_before));
    435        
     414
    436415        if (verbose)
    437416                printf("Exiting\n");
    438        
     417
    439418        return EOK;
    440419}
    441420
     421
    442422/** @}
    443423 */
Note: See TracChangeset for help on using the changeset viewer.