| 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 <net/in.h>
|
|---|
| 38 | #include <net/inet.h>
|
|---|
| 39 | #include <net/socket.h>
|
|---|
| 40 | #include <stdlib.h>
|
|---|
| 41 |
|
|---|
| 42 | #include "dns_msg.h"
|
|---|
| 43 | #include "dns_type.h"
|
|---|
| 44 | #include "transport.h"
|
|---|
| 45 |
|
|---|
| 46 | #include <stdio.h>
|
|---|
| 47 |
|
|---|
| 48 | #define RECV_BUF_SIZE 4096
|
|---|
| 49 |
|
|---|
| 50 | static uint8_t recv_buf[RECV_BUF_SIZE];
|
|---|
| 51 |
|
|---|
| 52 | int dns_request(dns_message_t *req, dns_message_t **rresp)
|
|---|
| 53 | {
|
|---|
| 54 | dns_message_t *resp;
|
|---|
| 55 | int rc;
|
|---|
| 56 | void *req_data;
|
|---|
| 57 | size_t req_size;
|
|---|
| 58 | struct sockaddr_in addr;
|
|---|
| 59 | struct sockaddr_in laddr;
|
|---|
| 60 | struct sockaddr_in src_addr;
|
|---|
| 61 | socklen_t src_addr_size;
|
|---|
| 62 | size_t recv_size;
|
|---|
| 63 | int fd;
|
|---|
| 64 | int i;
|
|---|
| 65 |
|
|---|
| 66 | addr.sin_family = AF_INET;
|
|---|
| 67 | addr.sin_port = htons(53);
|
|---|
| 68 | addr.sin_addr.s_addr = htonl((10 << 24) | (0 << 16) | (0 << 8) | 138);
|
|---|
| 69 |
|
|---|
| 70 | laddr.sin_family = AF_INET;
|
|---|
| 71 | laddr.sin_port = htons(12345);
|
|---|
| 72 | laddr.sin_addr.s_addr = INADDR_ANY;
|
|---|
| 73 |
|
|---|
| 74 | req_data = NULL;
|
|---|
| 75 | fd = -1;
|
|---|
| 76 |
|
|---|
| 77 | rc = dns_message_encode(req, &req_data, &req_size);
|
|---|
| 78 | if (rc != EOK)
|
|---|
| 79 | goto error;
|
|---|
| 80 |
|
|---|
| 81 | fd = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP);
|
|---|
| 82 | if (fd < 0) {
|
|---|
| 83 | rc = EIO;
|
|---|
| 84 | goto error;
|
|---|
| 85 | }
|
|---|
| 86 |
|
|---|
| 87 | rc = bind(fd, (struct sockaddr *)&laddr, sizeof(laddr));
|
|---|
| 88 | if (rc != EOK)
|
|---|
| 89 | goto error;
|
|---|
| 90 |
|
|---|
| 91 | printf("fd=%d req_data=%p, req_size=%zu\n", fd, req_data, req_size);
|
|---|
| 92 | rc = sendto(fd, req_data, req_size, 0, (struct sockaddr *)&addr,
|
|---|
| 93 | sizeof(addr));
|
|---|
| 94 | if (rc != EOK)
|
|---|
| 95 | goto error;
|
|---|
| 96 |
|
|---|
| 97 | src_addr_size = sizeof(src_addr);
|
|---|
| 98 | rc = recvfrom(fd, recv_buf, RECV_BUF_SIZE, 0,
|
|---|
| 99 | (struct sockaddr *)&src_addr, &src_addr_size);
|
|---|
| 100 | if (rc < 0) {
|
|---|
| 101 | printf("recvfrom returns error - %d\n", rc);
|
|---|
| 102 | goto error;
|
|---|
| 103 | }
|
|---|
| 104 |
|
|---|
| 105 | recv_size = (size_t)rc;
|
|---|
| 106 |
|
|---|
| 107 | printf("received %d bytes\n", (int)recv_size);
|
|---|
| 108 | for (i = 0; i < (int)recv_size; i++) {
|
|---|
| 109 | if (recv_buf[i] >= 32 && recv_buf[i] < 127)
|
|---|
| 110 | printf("%c", recv_buf[i]);
|
|---|
| 111 | else
|
|---|
| 112 | printf("?");
|
|---|
| 113 | }
|
|---|
| 114 | printf("\n");
|
|---|
| 115 |
|
|---|
| 116 | printf("close socket\n");
|
|---|
| 117 | closesocket(fd);
|
|---|
| 118 | printf("free req_data\n");
|
|---|
| 119 | free(req_data);
|
|---|
| 120 |
|
|---|
| 121 | rc = dns_message_decode(recv_buf, recv_size, &resp);
|
|---|
| 122 | if (rc != EOK) {
|
|---|
| 123 | rc = EIO;
|
|---|
| 124 | goto error;
|
|---|
| 125 | }
|
|---|
| 126 |
|
|---|
| 127 | *rresp = resp;
|
|---|
| 128 | return EOK;
|
|---|
| 129 |
|
|---|
| 130 | error:
|
|---|
| 131 | if (req_data != NULL)
|
|---|
| 132 | free(req_data);
|
|---|
| 133 | if (fd >= 0)
|
|---|
| 134 | closesocket(fd);
|
|---|
| 135 | return rc;
|
|---|
| 136 | }
|
|---|
| 137 |
|
|---|
| 138 | /** @}
|
|---|
| 139 | */
|
|---|