1 | /*
|
---|
2 | * Copyright (c) 2013 Jiri Svoboda
|
---|
3 | * All rights reserved.
|
---|
4 | *
|
---|
5 | * Redistribution and use in source and binary forms, with or without
|
---|
6 | * modification, are permitted provided that the following conditions
|
---|
7 | * are met:
|
---|
8 | *
|
---|
9 | * - Redistributions of source code must retain the above copyright
|
---|
10 | * notice, this list of conditions and the following disclaimer.
|
---|
11 | * - Redistributions in binary form must reproduce the above copyright
|
---|
12 | * notice, this list of conditions and the following disclaimer in the
|
---|
13 | * documentation and/or other materials provided with the distribution.
|
---|
14 | * - The name of the author may not be used to endorse or promote products
|
---|
15 | * derived from this software without specific prior written permission.
|
---|
16 | *
|
---|
17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
---|
18 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
---|
19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
---|
20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
---|
21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
---|
22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
---|
23 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
---|
24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
---|
25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
---|
26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
---|
27 | */
|
---|
28 |
|
---|
29 | /** @addtogroup dnsres
|
---|
30 | * @{
|
---|
31 | */
|
---|
32 | /** @file DNS query utility.
|
---|
33 | */
|
---|
34 |
|
---|
35 | #include <errno.h>
|
---|
36 | #include <inet/dnsr.h>
|
---|
37 | #include <stdio.h>
|
---|
38 | #include <stdlib.h>
|
---|
39 |
|
---|
40 | #define NAME "dnsres"
|
---|
41 |
|
---|
42 | static void print_syntax(void)
|
---|
43 | {
|
---|
44 | printf("syntax: " NAME " <host-name>\n");
|
---|
45 | }
|
---|
46 |
|
---|
47 | static int addr_format(inet_addr_t *addr, char **bufp)
|
---|
48 | {
|
---|
49 | int rc;
|
---|
50 |
|
---|
51 | rc = asprintf(bufp, "%d.%d.%d.%d", addr->ipv4 >> 24,
|
---|
52 | (addr->ipv4 >> 16) & 0xff, (addr->ipv4 >> 8) & 0xff,
|
---|
53 | addr->ipv4 & 0xff);
|
---|
54 |
|
---|
55 | if (rc < 0)
|
---|
56 | return ENOMEM;
|
---|
57 |
|
---|
58 | return EOK;
|
---|
59 | }
|
---|
60 |
|
---|
61 | int main(int argc, char *argv[])
|
---|
62 | {
|
---|
63 | int rc;
|
---|
64 | dnsr_hostinfo_t *hinfo;
|
---|
65 | char *saddr;
|
---|
66 |
|
---|
67 | rc = dnsr_init();
|
---|
68 | if (rc != EOK) {
|
---|
69 | printf(NAME ": Failed connecting to DNS resolution service "
|
---|
70 | "(%d).\n", rc);
|
---|
71 | return 1;
|
---|
72 | }
|
---|
73 |
|
---|
74 | if (argc != 2) {
|
---|
75 | print_syntax();
|
---|
76 | return 1;
|
---|
77 | }
|
---|
78 |
|
---|
79 | rc = dnsr_name2host(argv[1], &hinfo);
|
---|
80 | if (rc != EOK) {
|
---|
81 | printf(NAME ": Error resolving '%s'.\n", argv[1]);
|
---|
82 | return 1;
|
---|
83 | }
|
---|
84 |
|
---|
85 | rc = addr_format(&hinfo->addr, &saddr);
|
---|
86 | if (rc != EOK) {
|
---|
87 | dnsr_hostinfo_destroy(hinfo);
|
---|
88 | printf(NAME ": Out of memory.\n");
|
---|
89 | return 1;
|
---|
90 | }
|
---|
91 |
|
---|
92 | printf("Host name: %s address: %s\n", hinfo->name, saddr);
|
---|
93 | dnsr_hostinfo_destroy(hinfo);
|
---|
94 | free(saddr);
|
---|
95 |
|
---|
96 | return 0;
|
---|
97 | }
|
---|
98 |
|
---|
99 | /** @}
|
---|
100 | */
|
---|