Changes in uspace/app/ping/ping.c [959d2ec:07b7c48] in mainline


Ignore:
File:
1 edited

Legend:

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

    r959d2ec r07b7c48  
    11/*
    2  * Copyright (c) 2013 Jiri Svoboda
     2 * Copyright (c) 2012 Jiri Svoboda
    33 * All rights reserved.
    44 *
     
    3737#include <errno.h>
    3838#include <fibril_synch.h>
    39 #include <inet/dnsr.h>
    40 #include <inet/addr.h>
    4139#include <inet/inetping.h>
    4240#include <io/console.h>
     
    7068static void print_syntax(void)
    7169{
    72         printf("syntax: " NAME " [-r] <host>\n");
     70        printf("syntax: " NAME " [-r] <addr>\n");
     71}
     72
     73static int addr_parse(const char *text, inet_addr_t *addr)
     74{
     75        unsigned long a[4];
     76        char *cp = (char *)text;
     77        int i;
     78
     79        for (i = 0; i < 3; i++) {
     80                a[i] = strtoul(cp, &cp, 10);
     81                if (*cp != '.')
     82                        return EINVAL;
     83                ++cp;
     84        }
     85
     86        a[3] = strtoul(cp, &cp, 10);
     87        if (*cp != '\0')
     88                return EINVAL;
     89
     90        addr->ipv4 = 0;
     91        for (i = 0; i < 4; i++) {
     92                if (a[i] > 255)
     93                        return EINVAL;
     94                addr->ipv4 = (addr->ipv4 << 8) | a[i];
     95        }
     96
     97        return EOK;
     98}
     99
     100static int addr_format(inet_addr_t *addr, char **bufp)
     101{
     102        int rc;
     103
     104        rc = asprintf(bufp, "%d.%d.%d.%d", addr->ipv4 >> 24,
     105            (addr->ipv4 >> 16) & 0xff, (addr->ipv4 >> 8) & 0xff,
     106            addr->ipv4 & 0xff);
     107
     108        if (rc < 0)
     109                return ENOMEM;
     110
     111        return EOK;
    73112}
    74113
     
    86125        int rc;
    87126
    88         rc = inet_addr_format(&sdu->src, &asrc);
     127        rc = addr_format(&sdu->src, &asrc);
    89128        if (rc != EOK)
    90129                return ENOMEM;
    91130
    92         rc = inet_addr_format(&sdu->dest, &adest);
     131        rc = addr_format(&sdu->dest, &adest);
    93132        if (rc != EOK) {
    94133                free(asrc);
     
    174213int main(int argc, char *argv[])
    175214{
    176         dnsr_hostinfo_t *hinfo = NULL;
    177         char *asrc = NULL;
    178         char *adest = NULL;
    179         char *sdest = NULL;
    180215        int rc;
    181216        int argi;
     
    185220                printf(NAME ": Failed connecting to internet ping service "
    186221                    "(%d).\n", rc);
    187                 goto error;
     222                return 1;
    188223        }
    189224
     
    198233        if (argc - argi != 1) {
    199234                print_syntax();
    200                 goto error;
     235                return 1;
    201236        }
    202237
    203238        /* Parse destination address */
    204         rc = inet_addr_parse(argv[argi], &dest_addr);
    205         if (rc != EOK) {
    206                 /* Try interpreting as a host name */
    207                 rc = dnsr_name2host(argv[argi], &hinfo);
    208                 if (rc != EOK) {
    209                         printf(NAME ": Error resolving host '%s'.\n", argv[argi]);
    210                         goto error;
    211                 }
    212 
    213                 dest_addr = hinfo->addr;
     239        rc = addr_parse(argv[argi], &dest_addr);
     240        if (rc != EOK) {
     241                printf(NAME ": Invalid address format.\n");
     242                print_syntax();
     243                return 1;
    214244        }
    215245
     
    218248        if (rc != EOK) {
    219249                printf(NAME ": Failed determining source address.\n");
    220                 goto error;
    221         }
    222 
    223         rc = inet_addr_format(&src_addr, &asrc);
    224         if (rc != EOK) {
    225                 printf(NAME ": Out of memory.\n");
    226                 goto error;
    227         }
    228 
    229         rc = inet_addr_format(&dest_addr, &adest);
    230         if (rc != EOK) {
    231                 printf(NAME ": Out of memory.\n");
    232                 goto error;
    233         }
    234 
    235         if (hinfo != NULL) {
    236                 rc = asprintf(&sdest, "%s (%s)", hinfo->cname, adest);
    237                 if (rc < 0) {
    238                         printf(NAME ": Out of memory.\n");
    239                         goto error;
    240                 }
    241         } else {
    242                 sdest = adest;
    243                 adest = NULL;
    244         }
    245 
    246         printf("Sending ICMP echo request from %s to %s.\n",
    247             asrc, sdest);
     250                return 1;
     251        }
    248252
    249253        fid_t fid;
     
    253257                if (fid == 0) {
    254258                        printf(NAME ": Failed creating transmit fibril.\n");
    255                         goto error;
     259                        return 1;
    256260                }
    257261
     
    261265                if (fid == 0) {
    262266                        printf(NAME ": Failed creating input fibril.\n");
    263                         goto error;
     267                        return 1;
    264268                }
    265269
     
    279283        if (rc == ETIMEOUT) {
    280284                printf(NAME ": Echo request timed out.\n");
    281                 goto error;
    282         }
    283 
    284         free(asrc);
    285         free(adest);
    286         free(sdest);
    287         dnsr_hostinfo_destroy(hinfo);
     285                return 1;
     286        }
     287
    288288        return 0;
    289 error:
    290         free(asrc);
    291         free(adest);
    292         free(sdest);
    293         dnsr_hostinfo_destroy(hinfo);
    294         return 1;
    295289}
    296290
Note: See TracChangeset for help on using the changeset viewer.