~jsvoboda/helenos/dnsr

« back to all changes in this revision

Viewing changes to uspace/srv/net/dnsres/dnsres.c

  • Committer: Jiri Svoboda
  • Date: 2013-04-20 10:42:13 UTC
  • Revision ID: jiri@wiwaxia-20130420104213-ah2nlkbgo7gba5zv
Construct domain names, fix some bugs. Parse answer, print resulting hostname and IP address.

Show diffs side-by-side

added added

removed removed

Lines of Context:
34
34
 */
35
35
 
36
36
#include <stdio.h>
 
37
#include <stdlib.h>
37
38
#include <errno.h>
38
39
 
39
40
#include "dns_msg.h"
42
43
 
43
44
#define NAME  "dnsres"
44
45
 
 
46
static int addr_format(inet_addr_t *addr, char **bufp)
 
47
{
 
48
        int rc;
 
49
 
 
50
        rc = asprintf(bufp, "%d.%d.%d.%d", addr->ipv4 >> 24,
 
51
            (addr->ipv4 >> 16) & 0xff, (addr->ipv4 >> 8) & 0xff,
 
52
            addr->ipv4 & 0xff);
 
53
 
 
54
        if (rc < 0)
 
55
                return ENOMEM;
 
56
 
 
57
        return EOK;
 
58
}
 
59
 
45
60
int main(int argc, char *argv[])
46
61
{
47
62
        dns_host_info_t hinfo;
 
63
        char *astr;
48
64
        int rc;
49
65
 
50
66
        printf("%s: DNS Resolution Service\n", NAME);
51
 
        rc = dns_name2host("helenos.org", &hinfo);
 
67
        rc = dns_name2host(argc < 2 ? "helenos.org" : argv[1], &hinfo);
52
68
        printf("dns_name2host() -> rc = %d\n", rc);
53
69
 
 
70
        if (rc == EOK) {
 
71
                rc = addr_format(&hinfo.addr, &astr);
 
72
                if (rc != EOK) {
 
73
                        printf("Out of memory\n");
 
74
                        return ENOMEM;
 
75
                }
 
76
 
 
77
                printf("hostname: %s\n", hinfo.name);
 
78
                printf("IPv4 address: %s\n", astr);
 
79
                free(astr);
 
80
        }
 
81
 
54
82
        return 0;
55
83
}
56
84