]>
git.ipfire.org Git - thirdparty/u-boot.git/blob - net/dns.c
4 * Copyright (c) 2008 Pieter Voorthuijsen <pieter.voorthuijsen@prodrive.nl>
5 * Copyright (c) 2009 Robin Getz <rgetz@blackfin.uclinux.org>
7 * This is a simple DNS implementation for U-Boot. It will use the first IP
8 * in the DNS response as net_server_ip. This can then be used for any other
9 * network related activities.
11 * The packet handling is partly based on TADNS, original copyrights
17 * Copyright (c) 2004-2005 Sergey Lyubka <valenok@gmail.com>
19 * "THE BEER-WARE LICENSE" (Revision 42):
20 * Sergey Lyubka wrote this file. As long as you retain this notice you
21 * can do whatever you want with this stuff. If we meet some day, and you think
22 * this stuff is worth it, you can buy me a beer in return.
30 #include <asm/unaligned.h>
34 char *net_dns_resolve
; /* The host to resolve */
35 char *net_dns_env_var
; /* The envvar to store the answer in */
37 static int dns_our_port
;
40 * make port a little random (1024-17407)
41 * This keeps the math somewhat trivial to compute, and seems to work with
42 * all supported protocols/clients/servers
44 static unsigned int random_port(void)
46 return 1024 + (get_timer(0) % 0x4000);
49 static void dns_send(void)
51 struct header
*header
;
56 enum dns_query_type qtype
= DNS_A_RECORD
;
58 name
= net_dns_resolve
;
59 pkt
= (uchar
*)(net_tx_packet
+ net_eth_hdr_size() + IP_UDP_HDR_SIZE
);
62 /* Prepare DNS packet header */
63 header
= (struct header
*)pkt
;
65 header
->flags
= htons(0x100); /* standard query */
66 header
->nqueries
= htons(1); /* Just one query */
72 name_len
= strlen(name
);
73 p
= (uchar
*)&header
->data
; /* For encoding host name into packet */
76 s
= strchr(name
, '.');
80 n
= s
- name
; /* Chunk length */
81 *p
++ = n
; /* Copy length */
82 memcpy(p
, name
, n
); /* Copy chunk */
92 *p
++ = 0; /* Mark end of host name */
93 *p
++ = 0; /* Some servers require double null */
94 *p
++ = (unsigned char) qtype
; /* Query Type */
97 *p
++ = 1; /* Class: inet, 0x0001 */
99 n
= p
- pkt
; /* Total packet length */
100 debug("Packet size %d\n", n
);
102 dns_our_port
= random_port();
104 net_send_udp_packet(net_server_ethaddr
, net_dns_server
,
105 DNS_SERVICE_PORT
, dns_our_port
, n
);
106 debug("DNS packet sent\n");
109 static void dns_timeout_handler(void)
112 net_set_state(NETLOOP_FAIL
);
115 static void dns_handler(uchar
*pkt
, unsigned dest
, struct in_addr sip
,
116 unsigned src
, unsigned len
)
118 struct header
*header
;
119 const unsigned char *p
, *e
, *s
;
121 int found
, stop
, dlen
;
123 struct in_addr ip_addr
;
126 debug("%s\n", __func__
);
127 if (dest
!= dns_our_port
)
130 for (i
= 0; i
< len
; i
+= 4)
131 debug("0x%p - 0x%.2x 0x%.2x 0x%.2x 0x%.2x\n",
132 pkt
+i
, pkt
[i
], pkt
[i
+1], pkt
[i
+2], pkt
[i
+3]);
134 /* We sent one query. We want to have a single answer: */
135 header
= (struct header
*)pkt
;
136 if (ntohs(header
->nqueries
) != 1)
139 /* Received 0 answers */
140 if (header
->nanswers
== 0) {
141 puts("DNS: host not found\n");
142 net_set_state(NETLOOP_SUCCESS
);
147 s
= &header
->data
[0];
149 for (p
= s
; p
< e
&& *p
!= '\0'; p
++)
152 /* We sent query class 1, query type 1 */
153 if (&p
[5] > e
|| get_unaligned_be16(p
+1) != DNS_A_RECORD
) {
154 puts("DNS: response was not an A record\n");
155 net_set_state(NETLOOP_SUCCESS
);
159 /* Go to the first answer section */
162 /* Loop through the answers, we want A type answer */
163 for (found
= stop
= 0; !stop
&& &p
[12] < e
; ) {
164 /* Skip possible name in CNAME answer */
166 while (*p
&& &p
[12] < e
)
170 debug("Name (Offset in header): %d\n", p
[1]);
172 type
= get_unaligned_be16(p
+2);
173 debug("type = %d\n", type
);
174 if (type
== DNS_CNAME_RECORD
) {
175 /* CNAME answer. shift to the next section */
176 debug("Found canonical name\n");
177 dlen
= get_unaligned_be16(p
+10);
178 debug("dlen = %d\n", dlen
);
180 } else if (type
== DNS_A_RECORD
) {
181 debug("Found A-record\n");
185 debug("Unknown type\n");
190 if (found
&& &p
[12] < e
) {
191 dlen
= get_unaligned_be16(p
+10);
193 memcpy(&ip_addr
, p
, 4);
196 ip_to_string(ip_addr
, ip_str
);
197 printf("%s\n", ip_str
);
199 env_set(net_dns_env_var
, ip_str
);
201 puts("server responded with invalid IP number\n");
205 net_set_state(NETLOOP_SUCCESS
);
210 debug("%s\n", __func__
);
212 net_set_timeout_handler(DNS_TIMEOUT
, dns_timeout_handler
);
213 net_set_udp_handler(dns_handler
);
215 /* Clear a previous MAC address, the server IP might have changed. */
216 memset(net_server_ethaddr
, 0, sizeof(net_server_ethaddr
));