| 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 | /**
|
|---|
| 33 | * @file
|
|---|
| 34 | */
|
|---|
| 35 |
|
|---|
| 36 | #include <errno.h>
|
|---|
| 37 | #include <mem.h>
|
|---|
| 38 | #include <stdlib.h>
|
|---|
| 39 | #include <str.h>
|
|---|
| 40 |
|
|---|
| 41 | #include "dns_msg.h"
|
|---|
| 42 | #include "dns_std.h"
|
|---|
| 43 | #include "dns_type.h"
|
|---|
| 44 | #include "query.h"
|
|---|
| 45 | #include "transport.h"
|
|---|
| 46 |
|
|---|
| 47 | static uint16_t msg_id;
|
|---|
| 48 |
|
|---|
| 49 | #include <stdio.h>
|
|---|
| 50 | int dns_name2host(const char *name, dns_host_info_t **rinfo)
|
|---|
| 51 | {
|
|---|
| 52 | dns_message_t msg;
|
|---|
| 53 | dns_message_t *amsg;
|
|---|
| 54 | dns_question_t question;
|
|---|
| 55 | dns_host_info_t *info;
|
|---|
| 56 | int rc;
|
|---|
| 57 |
|
|---|
| 58 | question.qname = (char *)name;
|
|---|
| 59 | question.qtype = DTYPE_A;
|
|---|
| 60 | question.qclass = DC_IN;
|
|---|
| 61 |
|
|---|
| 62 | memset(&msg, 0, sizeof(msg));
|
|---|
| 63 |
|
|---|
| 64 | list_initialize(&msg.question);
|
|---|
| 65 | list_append(&question.msg, &msg.question);
|
|---|
| 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 |
|
|---|
| 75 | rc = dns_request(&msg, &amsg);
|
|---|
| 76 | if (rc != EOK)
|
|---|
| 77 | return rc;
|
|---|
| 78 |
|
|---|
| 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 |
|
|---|
| 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;
|
|---|
| 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);
|
|---|
| 97 |
|
|---|
| 98 | dns_message_destroy(amsg);
|
|---|
| 99 | *rinfo = info;
|
|---|
| 100 | return EOK;
|
|---|
| 101 | }
|
|---|
| 102 | }
|
|---|
| 103 |
|
|---|
| 104 | dns_message_destroy(amsg);
|
|---|
| 105 | printf("no A/IN found, fail\n");
|
|---|
| 106 |
|
|---|
| 107 | return EIO;
|
|---|
| 108 | }
|
|---|
| 109 |
|
|---|
| 110 | void dns_hostinfo_destroy(dns_host_info_t *info)
|
|---|
| 111 | {
|
|---|
| 112 | free(info->name);
|
|---|
| 113 | free(info);
|
|---|
| 114 | }
|
|---|
| 115 |
|
|---|
| 116 | /** @}
|
|---|
| 117 | */
|
|---|