Changes in uspace/app/ping/ping.c [fff7ef4:3495654] in mainline


Ignore:
File:
1 edited

Legend:

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

    rfff7ef4 r3495654  
    3737#include <errno.h>
    3838#include <fibril_synch.h>
    39 #include <inet/dnsr.h>
     39#include <inet/addr.h>
    4040#include <inet/inetping.h>
    4141#include <io/console.h>
     
    6969static void print_syntax(void)
    7070{
    71         printf("syntax: " NAME " [-r] <host>\n");
    72 }
    73 
    74 static int addr_parse(const char *text, inet_addr_t *addr)
    75 {
    76         unsigned long a[4];
    77         char *cp = (char *)text;
    78         int i;
    79 
    80         for (i = 0; i < 3; i++) {
    81                 a[i] = strtoul(cp, &cp, 10);
    82                 if (*cp != '.')
    83                         return EINVAL;
    84                 ++cp;
    85         }
    86 
    87         a[3] = strtoul(cp, &cp, 10);
    88         if (*cp != '\0')
    89                 return EINVAL;
    90 
    91         addr->ipv4 = 0;
    92         for (i = 0; i < 4; i++) {
    93                 if (a[i] > 255)
    94                         return EINVAL;
    95                 addr->ipv4 = (addr->ipv4 << 8) | a[i];
    96         }
    97 
    98         return EOK;
    99 }
    100 
    101 static int addr_format(inet_addr_t *addr, char **bufp)
    102 {
    103         int rc;
    104 
    105         rc = asprintf(bufp, "%d.%d.%d.%d", addr->ipv4 >> 24,
    106             (addr->ipv4 >> 16) & 0xff, (addr->ipv4 >> 8) & 0xff,
    107             addr->ipv4 & 0xff);
    108 
    109         if (rc < 0)
    110                 return ENOMEM;
    111 
    112         return EOK;
     71        printf("syntax: " NAME " [-r] <addr>\n");
    11372}
    11473
     
    12685        int rc;
    12786
    128         rc = addr_format(&sdu->src, &asrc);
     87        rc = inet_addr_format(&sdu->src, &asrc);
    12988        if (rc != EOK)
    13089                return ENOMEM;
    13190
    132         rc = addr_format(&sdu->dest, &adest);
     91        rc = inet_addr_format(&sdu->dest, &adest);
    13392        if (rc != EOK) {
    13493                free(asrc);
     
    214173int main(int argc, char *argv[])
    215174{
    216         dnsr_hostinfo_t *hinfo = NULL;
    217         char *asrc = NULL;
    218         char *adest = NULL;
    219         char *sdest = NULL;
    220175        int rc;
    221176        int argi;
     
    225180                printf(NAME ": Failed connecting to internet ping service "
    226181                    "(%d).\n", rc);
    227                 goto error;
     182                return 1;
    228183        }
    229184
     
    238193        if (argc - argi != 1) {
    239194                print_syntax();
    240                 goto error;
     195                return 1;
    241196        }
    242197
    243198        /* Parse destination address */
    244         rc = addr_parse(argv[argi], &dest_addr);
    245         if (rc != EOK) {
    246                 /* Try interpreting as a host name */
    247                 rc = dnsr_name2host(argv[argi], &hinfo);
    248                 if (rc != EOK) {
    249                         printf(NAME ": Error resolving host '%s'.\n", argv[argi]);
    250                         goto error;
    251                 }
    252 
    253                 dest_addr = hinfo->addr;
     199        rc = inet_addr_parse(argv[argi], &dest_addr);
     200        if (rc != EOK) {
     201                printf(NAME ": Invalid address format.\n");
     202                print_syntax();
     203                return 1;
    254204        }
    255205
     
    258208        if (rc != EOK) {
    259209                printf(NAME ": Failed determining source address.\n");
    260                 goto error;
    261         }
    262 
    263         rc = addr_format(&src_addr, &asrc);
    264         if (rc != EOK) {
    265                 printf(NAME ": Out of memory.\n");
    266                 goto error;
    267         }
    268 
    269         rc = addr_format(&dest_addr, &adest);
    270         if (rc != EOK) {
    271                 printf(NAME ": Out of memory.\n");
    272                 goto error;
    273         }
    274 
    275         if (hinfo != NULL) {
    276                 rc = asprintf(&sdest, "%s (%s)", hinfo->name, adest);
    277                 if (rc < 0) {
    278                         printf(NAME ": Out of memory.\n");
    279                         goto error;
    280                 }
    281         } else {
    282                 sdest = adest;
    283                 adest = NULL;
    284         }
    285 
    286         printf("Sending ICMP echo request from %s to %s.\n",
    287             asrc, sdest);
     210                return 1;
     211        }
    288212
    289213        fid_t fid;
     
    293217                if (fid == 0) {
    294218                        printf(NAME ": Failed creating transmit fibril.\n");
    295                         goto error;
     219                        return 1;
    296220                }
    297221
     
    301225                if (fid == 0) {
    302226                        printf(NAME ": Failed creating input fibril.\n");
    303                         goto error;
     227                        return 1;
    304228                }
    305229
     
    319243        if (rc == ETIMEOUT) {
    320244                printf(NAME ": Echo request timed out.\n");
    321                 goto error;
    322         }
    323 
    324         free(asrc);
    325         free(adest);
    326         free(sdest);
    327         dnsr_hostinfo_destroy(hinfo);
     245                return 1;
     246        }
     247
    328248        return 0;
    329 error:
    330         free(asrc);
    331         free(adest);
    332         free(sdest);
    333         dnsr_hostinfo_destroy(hinfo);
    334         return 1;
    335249}
    336250
Note: See TracChangeset for help on using the changeset viewer.