source: mainline/uspace/srv/net/dnsres/query.c@ 7262f89

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 7262f89 was 7262f89, checked in by Jiri Svoboda <jiri@…>, 13 years ago

Construct domain names, fix some bugs. Parse answer, print resulting hostname and IP address.

  • Property mode set to 100644
File size: 2.8 KB
Line 
1/*
2 * Copyright (c) 2012 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/**
33 * @file
34 */
35
36#include <errno.h>
37#include <mem.h>
38#include <str.h>
39
40#include "dns_msg.h"
41#include "dns_std.h"
42#include "dns_type.h"
43#include "query.h"
44#include "transport.h"
45
46static uint16_t msg_id;
47
48#include <stdio.h>
49int dns_name2host(const char *name, dns_host_info_t *info)
50{
51 dns_message_t msg;
52 dns_message_t *amsg;
53 dns_question_t question;
54 int rc;
55
56 question.qname = (char *)name;
57 question.qtype = DTYPE_A;
58 question.qclass = DC_IN;
59
60 memset(&msg, 0, sizeof(msg));
61
62 list_initialize(&msg.question);
63 list_append(&question.msg, &msg.question);
64
65 msg.id = msg_id++;
66 msg.qr = QR_QUERY;
67 msg.opcode = OPC_QUERY;
68 msg.aa = false;
69 msg.tc = false;
70 msg.rd = true;
71 msg.ra = false;
72
73 rc = dns_request(&msg, &amsg);
74 if (rc != EOK)
75 return rc;
76
77 list_foreach(amsg->answer, link) {
78 dns_rr_t *rr = list_get_instance(link, dns_rr_t, msg);
79
80 printf(" - '%s' %u/%u, dsize %u\n",
81 rr->name, rr->rtype, rr->rclass, rr->rdata_size);
82
83 if (rr->rtype == DTYPE_A && rr->rclass == DC_IN) {
84 if (rr->rdata_size != sizeof(uint32_t)) {
85 printf("rdata_size = %u - fail\n", rr->rdata_size);
86 return EIO;
87 }
88
89 info->name = str_dup(rr->name);
90 info->addr.ipv4 = dns_uint32_t_decode(rr->rdata, rr->rdata_size);
91 printf("info->addr = %x\n", info->addr.ipv4);
92 return EOK;
93 }
94 }
95
96 printf("no A/IN found, fail\n");
97
98 return EIO;
99}
100
101/** @}
102 */
Note: See TracBrowser for help on using the repository browser.