Changeset c55cbbf in mainline


Ignore:
Timestamp:
2013-04-24T06:52:50Z (11 years ago)
Author:
Jiri Svoboda <jiri@…>
Branches:
lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
Children:
287d729
Parents:
31e9fe0
Message:

Hostname resolution in ping command.

Location:
uspace
Files:
2 edited

Legend:

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

    r31e9fe0 rc55cbbf  
    11/*
    2  * Copyright (c) 2012 Jiri Svoboda
     2 * Copyright (c) 2013 Jiri Svoboda
    33 * All rights reserved.
    44 *
     
    3737#include <errno.h>
    3838#include <fibril_synch.h>
     39#include <inet/dnsr.h>
    3940#include <inet/inetping.h>
    4041#include <io/console.h>
     
    213214int main(int argc, char *argv[])
    214215{
     216        dnsr_hostinfo_t *hinfo = NULL;
     217        char *asrc = NULL;
     218        char *adest = NULL;
     219        char *sdest = NULL;
    215220        int rc;
    216221        int argi;
     
    220225                printf(NAME ": Failed connecting to internet ping service "
    221226                    "(%d).\n", rc);
    222                 return 1;
     227                goto error;
    223228        }
    224229
     
    233238        if (argc - argi != 1) {
    234239                print_syntax();
    235                 return 1;
     240                goto error;
    236241        }
    237242
     
    239244        rc = addr_parse(argv[argi], &dest_addr);
    240245        if (rc != EOK) {
    241                 printf(NAME ": Invalid address format.\n");
    242                 print_syntax();
    243                 return 1;
     246                /* Try interpreting as a host name */
     247                rc = dnsr_init();
     248                if (rc != EOK) {
     249                        printf(NAME ": Failed connecting DNS resolution "
     250                            "service (%d).\n", rc);
     251                        goto error;
     252                }
     253
     254                rc = dnsr_name2host(argv[argi], &hinfo);
     255                if (rc != EOK) {
     256                        printf(NAME ": Error resolving host '%s'.\n", argv[argi]);
     257                        goto error;
     258                }
     259
     260                dest_addr = hinfo->addr;
    244261        }
    245262
     
    248265        if (rc != EOK) {
    249266                printf(NAME ": Failed determining source address.\n");
    250                 return 1;
    251         }
     267                goto error;
     268        }
     269
     270        rc = addr_format(&src_addr, &asrc);
     271        if (rc != EOK) {
     272                printf(NAME ": Out of memory.\n");
     273                goto error;
     274        }
     275
     276        rc = addr_format(&dest_addr, &adest);
     277        if (rc != EOK) {
     278                printf(NAME ": Out of memory.\n");
     279                goto error;
     280        }
     281
     282        if (hinfo != NULL) {
     283                rc = asprintf(&sdest, "%s (%s)", hinfo->name, adest);
     284                if (rc < 0) {
     285                        printf(NAME ": Out of memory.\n");
     286                        goto error;
     287                }
     288        } else {
     289                sdest = adest;
     290                adest = NULL;
     291        }
     292
     293        printf("Sending ICMP echo request from %s to %s.\n",
     294            asrc, sdest);
    252295
    253296        fid_t fid;
     
    257300                if (fid == 0) {
    258301                        printf(NAME ": Failed creating transmit fibril.\n");
    259                         return 1;
     302                        goto error;
    260303                }
    261304
     
    265308                if (fid == 0) {
    266309                        printf(NAME ": Failed creating input fibril.\n");
    267                         return 1;
     310                        goto error;
    268311                }
    269312
     
    283326        if (rc == ETIMEOUT) {
    284327                printf(NAME ": Echo request timed out.\n");
    285                 return 1;
    286         }
    287 
     328                goto error;
     329        }
     330
     331        free(asrc);
     332        free(adest);
     333        free(sdest);
     334        dnsr_hostinfo_destroy(hinfo);
    288335        return 0;
     336error:
     337        free(asrc);
     338        free(adest);
     339        free(sdest);
     340        dnsr_hostinfo_destroy(hinfo);
     341        return 1;
    289342}
    290343
  • uspace/lib/c/generic/dnsr.c

    r31e9fe0 rc55cbbf  
    9292void dnsr_hostinfo_destroy(dnsr_hostinfo_t *info)
    9393{
     94        if (info == NULL)
     95                return;
     96
    9497        free(info->name);
    9598        free(info);
Note: See TracChangeset for help on using the changeset viewer.