Changeset 02a09ed in mainline for uspace/app


Ignore:
Timestamp:
2013-06-28T20:20:03Z (12 years ago)
Author:
Martin Decky <martin@…>
Branches:
lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
Children:
1d24ad3
Parents:
edf0d27
Message:

add basic infrastructure for IPv6 (inactive)
make inet_addr_t a universal address type

Location:
uspace/app
Files:
5 edited

Legend:

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

    redf0d27 r02a09ed  
    5959#define NETTEST1_TEXT  "Networking test 1 - sockets"
    6060
    61 static int family = PF_INET;
     61static uint16_t family = AF_INET;
    6262static sock_type_t type = SOCK_DGRAM;
    63 static char *data;
    6463static size_t size = 27;
    65 static int verbose = 0;
     64static bool verbose = false;
     65static int sockets = 10;
     66static int messages = 10;
     67static uint16_t port = 7;
    6668
    6769static struct sockaddr *address;
    6870static socklen_t addrlen;
    6971
    70 static int sockets;
    71 static int messages;
    72 static uint16_t port;
     72static char *data;
    7373
    7474static void nettest1_print_help(void)
     
    299299int main(int argc, char *argv[])
    300300{
    301         struct sockaddr_in address_in;
    302         struct sockaddr_in6 address_in6;
    303         dnsr_hostinfo_t *hinfo;
    304         uint8_t *address_start;
    305 
    306         int *socket_ids;
    307         int index;
    308         struct timeval time_before;
    309         struct timeval time_after;
    310 
    311         int rc;
    312 
    313         sockets = 10;
    314         messages = 10;
    315         port = 7;
    316 
    317301        /*
    318302         * Parse the command line arguments. Stop before the last argument
    319303         * if it does not start with dash ('-')
    320304         */
    321         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++) {
    322310                /* Options should start with dash ('-') */
    323311                if (argv[index][0] == '-') {
     
    331319        }
    332320       
    333         /* If not before the last argument containing the host */
     321        /* The last argument containing the host */
    334322        if (index >= argc) {
    335                 printf("Command line error: missing host name\n");
     323                printf("Host name missing.\n");
    336324                nettest1_print_help();
    337325                return EINVAL;
    338326        }
    339 
     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       
    340356        /* Prepare the address buffer */
    341 
    342         switch (family) {
    343         case PF_INET:
    344                 address_in.sin_family = AF_INET;
    345                 address_in.sin_port = htons(port);
    346                 address = (struct sockaddr *) &address_in;
    347                 addrlen = sizeof(address_in);
    348                 address_start = (uint8_t *) &address_in.sin_addr.s_addr;
    349                 break;
    350         case PF_INET6:
    351                 address_in6.sin6_family = AF_INET6;
    352                 address_in6.sin6_port = htons(port);
    353                 address = (struct sockaddr *) &address_in6;
    354                 addrlen = sizeof(address_in6);
    355                 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);
    356368                break;
    357369        default:
    358370                fprintf(stderr, "Address family is not supported\n");
    359371                return EAFNOSUPPORT;
    360         }
    361 
    362         /* Parse the last argument which should contain the host/address */
    363         rc = inet_pton(family, argv[argc - 1], address_start);
    364         if (rc != EOK) {
    365                 /* Try interpreting as a host name */
    366                 rc = dnsr_name2host(argv[argc - 1], &hinfo);
    367                 if (rc != EOK) {
    368                         printf("Error resolving host '%s'.\n", argv[argc - 1]);
    369                         return rc;
    370                 }
    371                
    372                 rc = inet_addr_sockaddr_in(&hinfo->addr, &address_in);
    373                 if (rc != EOK) {
    374                         printf("Host '%s' not resolved as IPv4 address.\n", argv[argc - 1]);
    375                         return rc;
    376                 }
    377372        }
    378373       
     
    406401         * null (\0).
    407402         */
    408         socket_ids = (int *) malloc(sizeof(int) * (sockets + 1));
     403        int *socket_ids = (int *) malloc(sizeof(int) * (sockets + 1));
    409404        if (!socket_ids) {
    410405                fprintf(stderr, "Failed to allocate receive buffer.\n");
     
    417412                printf("Starting tests\n");
    418413       
     414        struct timeval time_before;
    419415        rc = gettimeofday(&time_before, NULL);
    420416        if (rc != EOK) {
     
    428424        nettest1_test(socket_ids, sockets, messages);
    429425       
     426        struct timeval time_after;
    430427        rc = gettimeofday(&time_after, NULL);
    431428        if (rc != EOK) {
  • uspace/app/nettest2/nettest2.c

    redf0d27 r02a09ed  
    6060#define NETTEST2_TEXT  "Networking test 2 - transfer"
    6161
    62 static size_t size;
    63 static bool verbose;
    64 static sock_type_t type;
    65 static int sockets;
    66 static int messages;
    67 static int family;
    68 static uint16_t port;
     62static uint16_t family = PF_INET;
     63static size_t size = 28;
     64static bool verbose = false;
     65static sock_type_t type = SOCK_DGRAM;
     66static int sockets = 10;
     67static int messages = 10;
     68static uint16_t port = 7;
    6969
    7070static void nettest2_print_help(void)
     
    234234int main(int argc, char *argv[])
    235235{
    236         struct sockaddr *address;
    237         struct sockaddr_in address_in;
    238         struct sockaddr_in6 address_in6;
    239         dnsr_hostinfo_t *hinfo;
    240         socklen_t addrlen;
    241         uint8_t *address_start;
    242 
    243         int *socket_ids;
    244         char *data;
    245236        int index;
    246         struct timeval time_before;
    247         struct timeval time_after;
    248 
    249237        int rc;
    250 
    251         size = 28;
    252         verbose = false;
    253         type = SOCK_DGRAM;
    254         sockets = 10;
    255         messages = 10;
    256         family = PF_INET;
    257         port = 7;
    258 
     238       
    259239        /*
    260240         * Parse the command line arguments.
     
    264244        for (index = 1; (index < argc - 1) || ((index == argc - 1) &&
    265245            (argv[index][0] == '-')); ++index) {
    266 
    267246                /* Options should start with dash ('-') */
    268247                if (argv[index][0] == '-') {
     
    276255        }
    277256       
    278         /* If not before the last argument containing the host */
     257        /* The last argument containing the host */
    279258        if (index >= argc) {
    280                 printf("Command line error: missing host name\n");
     259                printf("Host name missing.\n");
    281260                nettest2_print_help();
    282261                return EINVAL;
    283262        }
    284 
     263       
     264        char *addr_s = argv[argc - 1];
     265       
     266        /* Interpret as address */
     267        inet_addr_t addr_addr;
     268        rc = inet_addr_parse(addr_s, &addr_addr);
     269       
     270        if (rc != EOK) {
     271                /* Interpret as a host name */
     272                dnsr_hostinfo_t *hinfo = NULL;
     273                rc = dnsr_name2host(addr_s, &hinfo);
     274               
     275                if (rc != EOK) {
     276                        printf("Error resolving host '%s'.\n", addr_s);
     277                        return EINVAL;
     278                }
     279               
     280                addr_addr = hinfo->addr;
     281        }
     282       
     283        struct sockaddr_in addr;
     284        struct sockaddr_in6 addr6;
     285        uint16_t af = inet_addr_sockaddr_in(&addr_addr, &addr, &addr6);
     286       
     287        if (af != family) {
     288                printf("Address family does not match explicitly set family.\n");
     289                return EINVAL;
     290        }
     291       
    285292        /* Prepare the address buffer */
    286 
    287         switch (family) {
    288         case PF_INET:
    289                 address_in.sin_family = AF_INET;
    290                 address_in.sin_port = htons(port);
    291                 address = (struct sockaddr *) &address_in;
    292                 addrlen = sizeof(address_in);
    293                 address_start = (uint8_t *) &address_in.sin_addr.s_addr;
    294                 break;
    295         case PF_INET6:
    296                 address_in6.sin6_family = AF_INET6;
    297                 address_in6.sin6_port = htons(port);
    298                 address = (struct sockaddr *) &address_in6;
    299                 addrlen = sizeof(address_in6);
    300                 address_start = (uint8_t *) &address_in6.sin6_addr.s6_addr;
     293       
     294        struct sockaddr *address;
     295        socklen_t addrlen;
     296       
     297        switch (af) {
     298        case AF_INET:
     299                addr.sin_port = htons(port);
     300                address = (struct sockaddr *) &addr;
     301                addrlen = sizeof(addr);
     302                break;
     303        case AF_INET6:
     304                addr6.sin6_port = htons(port);
     305                address = (struct sockaddr *) &addr6;
     306                addrlen = sizeof(addr6);
    301307                break;
    302308        default:
    303309                fprintf(stderr, "Address family is not supported\n");
    304310                return EAFNOSUPPORT;
    305         }
    306 
    307         /* Parse the last argument which should contain the host/address */
    308         rc = inet_pton(family, argv[argc - 1], address_start);
    309         if (rc != EOK) {
    310                 /* Try interpreting as a host name */
    311                 rc = dnsr_name2host(argv[argc - 1], &hinfo);
    312                 if (rc != EOK) {
    313                         printf("Error resolving host '%s'.\n", argv[argc - 1]);
    314                         return rc;
    315                 }
    316                
    317                 rc = inet_addr_sockaddr_in(&hinfo->addr, &address_in);
    318                 if (rc != EOK) {
    319                         printf("Host '%s' not resolved as IPv4 address.\n", argv[argc - 1]);
    320                         return rc;
    321                 }
    322311        }
    323312       
     
    333322         * null character.
    334323         */
    335         data = (char *) malloc(size + 1);
     324        char *data = (char *) malloc(size + 1);
    336325        if (!data) {
    337326                fprintf(stderr, "Failed to allocate data buffer.\n");
     
    353342         * Allocate count entries plus the terminating null (\0)
    354343         */
    355         socket_ids = (int *) malloc(sizeof(int) * (sockets + 1));
     344        int *socket_ids = (int *) malloc(sizeof(int) * (sockets + 1));
    356345        if (!socket_ids) {
    357346                fprintf(stderr, "Failed to allocate receive buffer.\n");
    358347                return ENOMEM;
    359348        }
     349       
    360350        socket_ids[sockets] = 0;
    361351       
     
    377367                printf("\n");
    378368       
     369        struct timeval time_before;
    379370        rc = gettimeofday(&time_before, NULL);
    380371        if (rc != EOK) {
     
    388379                return rc;
    389380       
     381        struct timeval time_after;
    390382        rc = gettimeofday(&time_after, NULL);
    391383        if (rc != EOK) {
  • uspace/app/nettest3/nettest3.c

    redf0d27 r02a09ed  
    8484                        }
    8585                       
    86                         rc = inet_addr_sockaddr_in(&hinfo->addr, &addr);
    87                         if (rc != EOK) {
     86                        uint16_t af = inet_addr_sockaddr_in(&hinfo->addr, &addr, NULL);
     87                        if (af != AF_INET) {
    8888                                printf("Host '%s' not resolved as IPv4 address.\n", argv[1]);
    8989                                return rc;
  • uspace/app/nterm/conn.c

    redf0d27 r02a09ed  
    7575int conn_open(const char *addr_s, const char *port_s)
    7676{
    77         struct sockaddr_in addr;
    78         dnsr_hostinfo_t *hinfo = NULL;
    79         int rc;
    80         char *endptr;
    81 
    82         addr.sin_family = AF_INET;
    83 
    84         rc = inet_pton(addr.sin_family, addr_s, (uint8_t *)&addr.sin_addr);
     77        int conn_fd = -1;
     78       
     79        /* Interpret as address */
     80        inet_addr_t addr_addr;
     81        int rc = inet_addr_parse(addr_s, &addr_addr);
     82       
    8583        if (rc != EOK) {
    86                 /* Try interpreting as a host name */
     84                /* Interpret as a host name */
     85                dnsr_hostinfo_t *hinfo = NULL;
    8786                rc = dnsr_name2host(addr_s, &hinfo);
     87               
    8888                if (rc != EOK) {
    8989                        printf("Error resolving host '%s'.\n", addr_s);
     
    9191                }
    9292               
    93                 rc = inet_addr_sockaddr_in(&hinfo->addr, &addr);
    94                 if (rc != EOK) {
    95                         printf("Host '%s' not resolved as IPv4 address.\n", addr_s);
    96                         return rc;
    97                 }
     93                addr_addr = hinfo->addr;
    9894        }
    99 
    100         addr.sin_port = htons(strtol(port_s, &endptr, 10));
     95       
     96        struct sockaddr_in addr;
     97        struct sockaddr_in6 addr6;
     98        uint16_t af = inet_addr_sockaddr_in(&addr_addr, &addr, &addr6);
     99       
     100        char *endptr;
     101        uint16_t port = strtol(port_s, &endptr, 10);
    101102        if (*endptr != '\0') {
    102103                printf("Invalid port number %s\n", port_s);
     
    104105        }
    105106       
     107        printf("Connecting to host %s port %u\n", addr_s, port);
     108       
    106109        conn_fd = socket(PF_INET, SOCK_STREAM, 0);
    107110        if (conn_fd < 0)
    108111                goto error;
    109112       
    110         printf("Connecting to host %s port %u\n", addr_s, ntohs(addr.sin_port));
     113        switch (af) {
     114        case AF_INET:
     115                addr.sin_port = htons(port);
     116                rc = connect(conn_fd, (struct sockaddr *) &addr, sizeof(addr));
     117                break;
     118        case AF_INET6:
     119                addr6.sin6_port = htons(port);
     120                rc = connect(conn_fd, (struct sockaddr *) &addr6, sizeof(addr6));
     121                break;
     122        default:
     123                printf("Unknown address family.\n");
     124                goto error;
     125        }
    111126       
    112         rc = connect(conn_fd, (struct sockaddr *)&addr, sizeof(addr));
    113127        if (rc != EOK)
    114128                goto error;
  • uspace/app/ping/ping.c

    redf0d27 r02a09ed  
    3737#include <errno.h>
    3838#include <fibril_synch.h>
     39#include <net/socket_codes.h>
    3940#include <inet/dnsr.h>
    4041#include <inet/addr.h>
     
    6364};
    6465
    65 static uint32_t src;
    66 static uint32_t dest;
     66static addr32_t src;
     67static addr32_t dest;
    6768
    6869static bool ping_repeat = false;
     
    8485{
    8586        inet_addr_t src_addr;
    86         inet_addr_unpack(sdu->src, &src_addr);
     87        inet_addr_set(sdu->src, &src_addr);
    8788       
    8889        inet_addr_t dest_addr;
    89         inet_addr_unpack(sdu->dest, &dest_addr);
     90        inet_addr_set(sdu->dest, &dest_addr);
    9091       
    9192        char *asrc;
     
    220221        }
    221222       
    222         rc = inet_addr_pack(&dest_addr, &dest);
    223         if (rc != EOK) {
     223        uint16_t af = inet_addr_get(&dest_addr, &dest, NULL);
     224        if (af != AF_INET) {
    224225                printf(NAME ": Destination '%s' is not an IPv4 address.\n",
    225226                    argv[argi]);
     
    235236       
    236237        inet_addr_t src_addr;
    237         inet_addr_unpack(src, &src_addr);
     238        inet_addr_set(src, &src_addr);
    238239       
    239240        rc = inet_addr_format(&src_addr, &asrc);
Note: See TracChangeset for help on using the changeset viewer.