1/*  *********************************************************************
2    *  Broadcom Common Firmware Environment (CFE)
3    *
4    *  Domain Name System Resolver		File: net_dns.c
5    *
6    *  This module provides minimal support for looking up IP addresses
7    *  from DNS name servers (RFCxxxx)
8    *
9    *  Author:  Mitch Lichtenberg
10    *
11    *********************************************************************
12    *
13    *  Copyright 2000,2001,2002,2003
14    *  Broadcom Corporation. All rights reserved.
15    *
16    *  This software is furnished under license and may be used and
17    *  copied only in accordance with the following terms and
18    *  conditions.  Subject to these conditions, you may download,
19    *  copy, install, use, modify and distribute modified or unmodified
20    *  copies of this software in source and/or binary form.  No title
21    *  or ownership is transferred hereby.
22    *
23    *  1) Any source code used, modified or distributed must reproduce
24    *     and retain this copyright notice and list of conditions
25    *     as they appear in the source file.
26    *
27    *  2) No right is granted to use any trade name, trademark, or
28    *     logo of Broadcom Corporation.  The "Broadcom Corporation"
29    *     name may not be used to endorse or promote products derived
30    *     from this software without the prior written permission of
31    *     Broadcom Corporation.
32    *
33    *  3) THIS SOFTWARE IS PROVIDED "AS-IS" AND ANY EXPRESS OR
34    *     IMPLIED WARRANTIES, INCLUDING BUT NOT LIMITED TO, ANY IMPLIED
35    *     WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
36    *     PURPOSE, OR NON-INFRINGEMENT ARE DISCLAIMED. IN NO EVENT
37    *     SHALL BROADCOM BE LIABLE FOR ANY DAMAGES WHATSOEVER, AND IN
38    *     PARTICULAR, BROADCOM SHALL NOT BE LIABLE FOR DIRECT, INDIRECT,
39    *     INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
40    *     (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
41    *     GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
42    *     BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
43    *     OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
44    *     TORT (INCLUDING NEGLIGENCE OR OTHERWISE), EVEN IF ADVISED OF
45    *     THE POSSIBILITY OF SUCH DAMAGE.
46    ********************************************************************* */
47
48
49#include "cfe.h"
50
51#include "net_ebuf.h"
52#include "net_ether.h"
53
54#include "net_api.h"
55
56/*  *********************************************************************
57    *  Macros
58    ********************************************************************* */
59
60#define ip_addriszero(a) (((a)[0]|(a)[1]|(a)[2]|(a)[3]) == 0)
61
62/*  *********************************************************************
63    *  Constants
64    ********************************************************************* */
65
66#define UDP_PORT_DNS	53
67
68#define DNS_FLG_QUERY	 0x0000
69#define DNS_FLG_RESPONSE 0x8000
70#define DNS_OPCODE_QUERY 0x0000
71#define DNS_FLG_AA       0x0400
72#define DNS_FLG_TC       0x0200
73#define DNS_FLG_RD       0x0100
74#define DNS_FLG_RA       0x0080
75#define DNS_RCODE_MASK   0x000F
76#define DNS_RCODE_OK	 0x0000
77#define DNS_RCODE_NAMEERR 0x0003
78
79
80#define QTYPE_HOSTADDR	1
81#define QCLASS_INTERNET	1
82
83#define DNS_QUERY_TIMEOUT  1 /* seconds */
84#define DNS_RETRY_COUNT    8
85
86
87/*  *********************************************************************
88    *  dns_dolookup(s,hostname,ipaddr)
89    *
90    *  Look up a host name and return its IP address.
91    *
92    *  Input parameters:
93    * 	   s - udp socket
94    *      server - name server to query
95    *  	   hostname - host name to find
96    *  	   ipaddr - buffer to place IP address
97    *
98    *  Return value:
99    *  	   0 if no responses found
100    *      >0 to indicate number of response records (usually 1)
101    *  	   else error code
102    ********************************************************************* */
103
104static int dns_dolookup(int s,uint8_t *server,char *hostname,uint8_t *ipaddr)
105{
106    ebuf_t *buf;
107    uint16_t id;
108    uint16_t tmp;
109    char *tok;
110    char *ptr;
111    int64_t timer;
112    int nres = 0;
113    uint16_t nqr,nar,nns,nxr;
114    uint8_t tmpb;
115    int expired;
116
117
118    /*
119     * Use the current time for our request ID
120     */
121
122    id = (uint16_t) cfe_ticks;
123
124    /*
125     * Get a buffer and fill it in.
126     */
127
128    buf = udp_alloc();
129
130    ebuf_append_u16_be(buf,id);
131    ebuf_append_u16_be(buf,(DNS_FLG_QUERY | DNS_OPCODE_QUERY | DNS_FLG_RD));
132    ebuf_append_u16_be(buf,1);		/* one question */
133    ebuf_append_u16_be(buf,0);		/* no answers */
134    ebuf_append_u16_be(buf,0);		/* no server resource records */
135    ebuf_append_u16_be(buf,0);		/* no additional records */
136
137    /*
138     * Chop up the hostname into pieces, basically replacing
139     * the dots with length indicators.
140     */
141
142    ptr = hostname;
143
144    while ((tok = strchr(ptr,'.'))) {
145	ebuf_append_u8(buf,(tok-ptr));
146	ebuf_append_bytes(buf,ptr,(tok-ptr));
147	ptr = tok + 1;
148	}
149
150    ebuf_append_u8(buf,strlen(ptr));
151    ebuf_append_bytes(buf,ptr,strlen(ptr));
152    ebuf_append_u8(buf,0);
153    ebuf_append_u16_be(buf,QTYPE_HOSTADDR);
154    ebuf_append_u16_be(buf,QCLASS_INTERNET);
155
156    /*
157     * Send the request to the name server
158     */
159
160    udp_send(s,buf,server);
161
162    /*
163     * Set a timer for the response
164     */
165
166    TIMER_SET(timer,DNS_QUERY_TIMEOUT*CFE_HZ);
167
168    /*
169     * Start waiting...
170     */
171
172    while (!(expired = TIMER_EXPIRED(timer))) {
173	POLL();
174
175	buf = udp_recv(s);
176	if (!buf) continue;
177
178	/* IDs must match */
179
180	ebuf_get_u16_be(buf,tmp);
181	if (id != tmp) goto drop;
182
183	/* It must be a response */
184
185	ebuf_get_u16_be(buf,tmp);
186
187	if ((tmp & DNS_FLG_RESPONSE) == 0)  goto drop;
188
189	if ((tmp & DNS_RCODE_MASK) != DNS_RCODE_OK) {
190	    udp_free(buf);
191	    /* name error */
192	    break;
193	    }
194
195	ebuf_get_u16_be(buf,nqr);
196	ebuf_get_u16_be(buf,nar);
197	ebuf_get_u16_be(buf,nns);
198	ebuf_get_u16_be(buf,nxr);
199
200	if (nar == 0) {
201	    goto drop;
202	    }
203
204	while (nqr > 0) {
205	    if (ebuf_length(buf) <= 0) {
206		goto drop;
207		}
208	    for (;;) {
209		ebuf_get_u8(buf,tmpb);
210		if (tmpb == 0) break;
211		ebuf_skip(buf,tmpb);
212		if (ebuf_length(buf) <= 0) {
213		    goto drop;
214		    }
215		}
216	    ebuf_skip(buf,2);	/* skip QTYPE */
217	    ebuf_skip(buf,2);	/* skip QCLASS */
218	    nqr--;		/* next question record */
219	    }
220
221	/*
222	 * Loop through the answer records to find
223	 * a HOSTADDR record.  Ignore any other records
224	 * we find.
225	 */
226
227	while (nar > 0) {
228	    uint16_t rname,rtype,rclass,dlen;
229
230	    ebuf_get_u16_be(buf,rname);	/* resource name */
231
232	    ebuf_get_u16_be(buf,rtype);	/* resource type */
233
234	    ebuf_get_u16_be(buf,rclass);	/* resource class */
235
236	    ebuf_skip(buf,4);		/* time to live */
237
238	    ebuf_get_u16_be(buf,dlen);	/* length of data */
239
240	    if (rtype != QTYPE_HOSTADDR) {
241		ebuf_skip(buf,dlen);
242		nar--;
243		continue;
244		}
245	    if (rclass != QCLASS_INTERNET) {
246		ebuf_skip(buf,dlen);
247		nar--;
248		continue;
249		}
250
251	    if (dlen != IP_ADDR_LEN) {
252		ebuf_skip(buf,dlen);
253		nar--;
254		continue;
255		}
256
257	    ebuf_get_bytes(buf,ipaddr,IP_ADDR_LEN);
258	    break;
259	    }
260
261	if (nar == 0) goto drop;
262
263	udp_free(buf);
264	nres++;
265	break;
266
267    drop:
268	udp_free(buf);
269	}
270
271    if (expired) return CFE_ERR_TIMEOUT;
272    if (nres == 0) return CFE_ERR_HOSTUNKNOWN;
273    return nres;
274}
275
276
277/*  *********************************************************************
278    *  dns_lookup(hostname,ipaddr)
279    *
280    *  Look up a host name and return its IP address.
281    *
282    *  Input parameters:
283    *  	   hostname - host name to find
284    *  	   ipaddr - buffer to place IP address
285    *
286    *  Return value:
287    *  	   0 if no responses found
288    *      >0 to indicate number of response records (usually 1)
289    *  	   else error code
290    ********************************************************************* */
291
292int dns_lookup(char *hostname,uint8_t *ipaddr)
293{
294    int s;
295    int nres = 0;
296    int retries;
297    char temphostname[100];
298    uint8_t *server;
299    char *tok;
300
301    /*
302     * If it's a valid IP address, don't look it up.
303     */
304
305    if (parseipaddr(hostname,ipaddr) == 0) return 1;
306
307    /*
308     * Add default domain if no domain was specified
309     */
310
311    if (strchr(hostname,'.') == NULL) {
312	tok = (char *)net_getparam(NET_DOMAIN);
313	if (tok) {
314	    xsprintf(temphostname,"%s.%s",hostname,tok);
315	    hostname = temphostname;
316	    }
317	}
318
319    /*
320     * Figure out who the name server is
321     */
322
323    server = net_getparam(NET_NAMESERVER);
324
325    if (!server) return CFE_ERR_NETDOWN;
326    if (ip_addriszero(server)) return CFE_ERR_NONAMESERVER;
327
328    /*
329     * Go do the name server lookup
330     */
331
332    s = udp_socket(UDP_PORT_DNS);
333    if (s < 0) return CFE_ERR_NOHANDLES;
334
335    for (retries = 0; retries < DNS_RETRY_COUNT; retries++) {
336	nres = dns_dolookup(s,server,hostname,ipaddr);
337	if (nres == CFE_ERR_TIMEOUT) continue;
338	if (nres >= 0) break;
339	}
340
341    udp_close(s);
342
343    return nres;
344
345}
346