Changeset 26de91a in mainline for uspace/app/nettest1/nettest1.c


Ignore:
Timestamp:
2013-10-04T17:19:20Z (11 years ago)
Author:
Jiri Svoboda <jiri@…>
Branches:
lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
Children:
9749e47
Parents:
e2839d7
Message:

IPv4 and v6 should not need separate handling by a simple client that is just connecting to a host/address. Add IPv6/DNS support in applications where missing.

File:
1 edited

Legend:

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

    re2839d7 r26de91a  
    3838#include "print_error.h"
    3939
     40#include <assert.h>
    4041#include <malloc.h>
    4142#include <stdio.h>
     
    326327        }
    327328       
    328         char *addr_s = argv[argc - 1];
     329        char *host = argv[argc - 1];
    329330       
    330331        /* Interpret as address */
    331         inet_addr_t addr_addr;
    332         rc = inet_addr_parse(addr_s, &addr_addr);
     332        inet_addr_t iaddr;
     333        rc = inet_addr_parse(host, &iaddr);
    333334       
    334335        if (rc != EOK) {
    335336                /* Interpret as a host name */
    336337                dnsr_hostinfo_t *hinfo = NULL;
    337                 rc = dnsr_name2host(addr_s, &hinfo, ipver_from_af(family));
     338                rc = dnsr_name2host(host, &hinfo, ipver_from_af(family));
    338339               
    339340                if (rc != EOK) {
    340                         printf("Error resolving host '%s'.\n", addr_s);
     341                        printf("Error resolving host '%s'.\n", host);
    341342                        return EINVAL;
    342343                }
    343344               
    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);
     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        }
    350354       
    351355        if (family == AF_NONE)
    352                 family = af;
    353        
    354         if (af != family) {
     356                family = address->sa_family;
     357       
     358        if (address->sa_family != family) {
    355359                printf("Address family does not match explicitly set family.\n");
    356360                return EINVAL;
    357         }
    358        
    359         /* Prepare the address buffer */
    360        
    361         switch (af) {
    362         case AF_INET:
    363                 addr.sin_port = htons(port);
    364                 address = (struct sockaddr *) &addr;
    365                 addrlen = sizeof(addr);
    366                 break;
    367         case AF_INET6:
    368                 addr6.sin6_port = htons(port);
    369                 address = (struct sockaddr *) &addr6;
    370                 addrlen = sizeof(addr6);
    371                 break;
    372         default:
    373                 fprintf(stderr, "Address family is not supported\n");
    374                 return EAFNOSUPPORT;
    375361        }
    376362       
     
    437423            &time_before));
    438424       
     425        free(address);
     426       
    439427        if (verbose)
    440428                printf("Exiting\n");
Note: See TracChangeset for help on using the changeset viewer.