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

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

Memory freeing, error paths.

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