annotate packages/net/ns/dns/current/src/dns.c @ 1005:1f968b35cd30

* doc/dns.sgml: Updated to reflect changes for IPv6. * include/dns_impl.inl (setdomainname): Append a . to the end of the domainname if it does not have one. * cdl/dns.cdl: CYGOPT_NS_DNS_FIRST_FAMILTY to control order of results. * src/dns.c: Order the results from cyg_dns_getaddrinfo. * test/dns1.c: Re-written to perform better testing and for getaddrinfo and getnameinfo. * test/dns2.c: Removed. It does not add anything useful. * src/dns.c (cyg_dns_getnameinfo): New. Interface between getnameinfo and the DNS client. * src/dns.c (cyg_dns_getaddrinfo): New. Interface between getaddrinfo and the DNS client. This supports both IPv4 and IPv6 * test/dns1.c: Added tests for getnameinfo using both IPv4 and IPv6 addresses.
author asl
date Mon, 12 May 2003 10:06:05 +0000
parents 94b558c9fb67
children 1aaed612fe59
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
208
e0c0827131d1 Merge from eCos master repository on 2002-05-20-20:11:54-BST
jlarmour
parents:
diff changeset
1 //=============================================================================
e0c0827131d1 Merge from eCos master repository on 2002-05-20-20:11:54-BST
jlarmour
parents:
diff changeset
2 //
e0c0827131d1 Merge from eCos master repository on 2002-05-20-20:11:54-BST
jlarmour
parents:
diff changeset
3 // dns.c
e0c0827131d1 Merge from eCos master repository on 2002-05-20-20:11:54-BST
jlarmour
parents:
diff changeset
4 //
e0c0827131d1 Merge from eCos master repository on 2002-05-20-20:11:54-BST
jlarmour
parents:
diff changeset
5 // DNS client code
e0c0827131d1 Merge from eCos master repository on 2002-05-20-20:11:54-BST
jlarmour
parents:
diff changeset
6 //
e0c0827131d1 Merge from eCos master repository on 2002-05-20-20:11:54-BST
jlarmour
parents:
diff changeset
7 //=============================================================================
e0c0827131d1 Merge from eCos master repository on 2002-05-20-20:11:54-BST
jlarmour
parents:
diff changeset
8 //####ECOSGPLCOPYRIGHTBEGIN####
e0c0827131d1 Merge from eCos master repository on 2002-05-20-20:11:54-BST
jlarmour
parents:
diff changeset
9 // -------------------------------------------
e0c0827131d1 Merge from eCos master repository on 2002-05-20-20:11:54-BST
jlarmour
parents:
diff changeset
10 // This file is part of eCos, the Embedded Configurable Operating System.
e0c0827131d1 Merge from eCos master repository on 2002-05-20-20:11:54-BST
jlarmour
parents:
diff changeset
11 // Copyright (C) 1998, 1999, 2000, 2001, 2002 Red Hat, Inc.
e0c0827131d1 Merge from eCos master repository on 2002-05-20-20:11:54-BST
jlarmour
parents:
diff changeset
12 //
e0c0827131d1 Merge from eCos master repository on 2002-05-20-20:11:54-BST
jlarmour
parents:
diff changeset
13 // eCos is free software; you can redistribute it and/or modify it under
e0c0827131d1 Merge from eCos master repository on 2002-05-20-20:11:54-BST
jlarmour
parents:
diff changeset
14 // the terms of the GNU General Public License as published by the Free
e0c0827131d1 Merge from eCos master repository on 2002-05-20-20:11:54-BST
jlarmour
parents:
diff changeset
15 // Software Foundation; either version 2 or (at your option) any later version.
e0c0827131d1 Merge from eCos master repository on 2002-05-20-20:11:54-BST
jlarmour
parents:
diff changeset
16 //
e0c0827131d1 Merge from eCos master repository on 2002-05-20-20:11:54-BST
jlarmour
parents:
diff changeset
17 // eCos is distributed in the hope that it will be useful, but WITHOUT ANY
e0c0827131d1 Merge from eCos master repository on 2002-05-20-20:11:54-BST
jlarmour
parents:
diff changeset
18 // WARRANTY; without even the implied warranty of MERCHANTABILITY or
e0c0827131d1 Merge from eCos master repository on 2002-05-20-20:11:54-BST
jlarmour
parents:
diff changeset
19 // FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
e0c0827131d1 Merge from eCos master repository on 2002-05-20-20:11:54-BST
jlarmour
parents:
diff changeset
20 // for more details.
e0c0827131d1 Merge from eCos master repository on 2002-05-20-20:11:54-BST
jlarmour
parents:
diff changeset
21 //
e0c0827131d1 Merge from eCos master repository on 2002-05-20-20:11:54-BST
jlarmour
parents:
diff changeset
22 // You should have received a copy of the GNU General Public License along
e0c0827131d1 Merge from eCos master repository on 2002-05-20-20:11:54-BST
jlarmour
parents:
diff changeset
23 // with eCos; if not, write to the Free Software Foundation, Inc.,
e0c0827131d1 Merge from eCos master repository on 2002-05-20-20:11:54-BST
jlarmour
parents:
diff changeset
24 // 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
e0c0827131d1 Merge from eCos master repository on 2002-05-20-20:11:54-BST
jlarmour
parents:
diff changeset
25 //
e0c0827131d1 Merge from eCos master repository on 2002-05-20-20:11:54-BST
jlarmour
parents:
diff changeset
26 // As a special exception, if other files instantiate templates or use macros
e0c0827131d1 Merge from eCos master repository on 2002-05-20-20:11:54-BST
jlarmour
parents:
diff changeset
27 // or inline functions from this file, or you compile this file and link it
e0c0827131d1 Merge from eCos master repository on 2002-05-20-20:11:54-BST
jlarmour
parents:
diff changeset
28 // with other works to produce a work based on this file, this file does not
e0c0827131d1 Merge from eCos master repository on 2002-05-20-20:11:54-BST
jlarmour
parents:
diff changeset
29 // by itself cause the resulting work to be covered by the GNU General Public
e0c0827131d1 Merge from eCos master repository on 2002-05-20-20:11:54-BST
jlarmour
parents:
diff changeset
30 // License. However the source code for this file must still be made available
e0c0827131d1 Merge from eCos master repository on 2002-05-20-20:11:54-BST
jlarmour
parents:
diff changeset
31 // in accordance with section (3) of the GNU General Public License.
e0c0827131d1 Merge from eCos master repository on 2002-05-20-20:11:54-BST
jlarmour
parents:
diff changeset
32 //
e0c0827131d1 Merge from eCos master repository on 2002-05-20-20:11:54-BST
jlarmour
parents:
diff changeset
33 // This exception does not invalidate any other reasons why a work based on
e0c0827131d1 Merge from eCos master repository on 2002-05-20-20:11:54-BST
jlarmour
parents:
diff changeset
34 // this file might be covered by the GNU General Public License.
e0c0827131d1 Merge from eCos master repository on 2002-05-20-20:11:54-BST
jlarmour
parents:
diff changeset
35 //
e0c0827131d1 Merge from eCos master repository on 2002-05-20-20:11:54-BST
jlarmour
parents:
diff changeset
36 // Alternative licenses for eCos may be arranged by contacting Red Hat, Inc.
210
d2c90368aeef Merge from eCos master repository on 2002-05-23-21:39:14-BST
jlarmour
parents: 208
diff changeset
37 // at http://sources.redhat.com/ecos/ecos-license/
208
e0c0827131d1 Merge from eCos master repository on 2002-05-20-20:11:54-BST
jlarmour
parents:
diff changeset
38 // -------------------------------------------
e0c0827131d1 Merge from eCos master repository on 2002-05-20-20:11:54-BST
jlarmour
parents:
diff changeset
39 //####ECOSGPLCOPYRIGHTEND####
e0c0827131d1 Merge from eCos master repository on 2002-05-20-20:11:54-BST
jlarmour
parents:
diff changeset
40 //=============================================================================
e0c0827131d1 Merge from eCos master repository on 2002-05-20-20:11:54-BST
jlarmour
parents:
diff changeset
41 //#####DESCRIPTIONBEGIN####
e0c0827131d1 Merge from eCos master repository on 2002-05-20-20:11:54-BST
jlarmour
parents:
diff changeset
42 //
e0c0827131d1 Merge from eCos master repository on 2002-05-20-20:11:54-BST
jlarmour
parents:
diff changeset
43 // Author(s): andrew.lunn
e0c0827131d1 Merge from eCos master repository on 2002-05-20-20:11:54-BST
jlarmour
parents:
diff changeset
44 // Contributors:andrew.lunn, jskov
e0c0827131d1 Merge from eCos master repository on 2002-05-20-20:11:54-BST
jlarmour
parents:
diff changeset
45 // Date: 2001-09-18
e0c0827131d1 Merge from eCos master repository on 2002-05-20-20:11:54-BST
jlarmour
parents:
diff changeset
46 // Description: Provides DNS lookup as per RFC 1034/1035.
e0c0827131d1 Merge from eCos master repository on 2002-05-20-20:11:54-BST
jlarmour
parents:
diff changeset
47 //
e0c0827131d1 Merge from eCos master repository on 2002-05-20-20:11:54-BST
jlarmour
parents:
diff changeset
48 // Note: Does only support A and PTR lookups. Maybe add for other
e0c0827131d1 Merge from eCos master repository on 2002-05-20-20:11:54-BST
jlarmour
parents:
diff changeset
49 // types as well?
e0c0827131d1 Merge from eCos master repository on 2002-05-20-20:11:54-BST
jlarmour
parents:
diff changeset
50 //
e0c0827131d1 Merge from eCos master repository on 2002-05-20-20:11:54-BST
jlarmour
parents:
diff changeset
51 // parse_answer() only returns the first found record.
e0c0827131d1 Merge from eCos master repository on 2002-05-20-20:11:54-BST
jlarmour
parents:
diff changeset
52 //
e0c0827131d1 Merge from eCos master repository on 2002-05-20-20:11:54-BST
jlarmour
parents:
diff changeset
53 // Add tracing and assertions.
e0c0827131d1 Merge from eCos master repository on 2002-05-20-20:11:54-BST
jlarmour
parents:
diff changeset
54 //
e0c0827131d1 Merge from eCos master repository on 2002-05-20-20:11:54-BST
jlarmour
parents:
diff changeset
55 //####DESCRIPTIONEND####
e0c0827131d1 Merge from eCos master repository on 2002-05-20-20:11:54-BST
jlarmour
parents:
diff changeset
56 //
e0c0827131d1 Merge from eCos master repository on 2002-05-20-20:11:54-BST
jlarmour
parents:
diff changeset
57 //=============================================================================
e0c0827131d1 Merge from eCos master repository on 2002-05-20-20:11:54-BST
jlarmour
parents:
diff changeset
58
210
d2c90368aeef Merge from eCos master repository on 2002-05-23-21:39:14-BST
jlarmour
parents: 208
diff changeset
59 #include <pkgconf/system.h>
d2c90368aeef Merge from eCos master repository on 2002-05-23-21:39:14-BST
jlarmour
parents: 208
diff changeset
60 #ifdef CYGPKG_KERNEL
d2c90368aeef Merge from eCos master repository on 2002-05-23-21:39:14-BST
jlarmour
parents: 208
diff changeset
61 # include <pkgconf/kernel.h>
d2c90368aeef Merge from eCos master repository on 2002-05-23-21:39:14-BST
jlarmour
parents: 208
diff changeset
62 # include <cyg/kernel/kapi.h>
d2c90368aeef Merge from eCos master repository on 2002-05-23-21:39:14-BST
jlarmour
parents: 208
diff changeset
63 #endif
208
e0c0827131d1 Merge from eCos master repository on 2002-05-20-20:11:54-BST
jlarmour
parents:
diff changeset
64 #include <cyg/hal/drv_api.h>
e0c0827131d1 Merge from eCos master repository on 2002-05-20-20:11:54-BST
jlarmour
parents:
diff changeset
65 #include <cyg/infra/cyg_type.h>
e0c0827131d1 Merge from eCos master repository on 2002-05-20-20:11:54-BST
jlarmour
parents:
diff changeset
66 #include <cyg/infra/cyg_trac.h> /* Tracing support */
e0c0827131d1 Merge from eCos master repository on 2002-05-20-20:11:54-BST
jlarmour
parents:
diff changeset
67
e0c0827131d1 Merge from eCos master repository on 2002-05-20-20:11:54-BST
jlarmour
parents:
diff changeset
68 #include <netdb.h>
e0c0827131d1 Merge from eCos master repository on 2002-05-20-20:11:54-BST
jlarmour
parents:
diff changeset
69
e0c0827131d1 Merge from eCos master repository on 2002-05-20-20:11:54-BST
jlarmour
parents:
diff changeset
70 #include <sys/time.h>
e0c0827131d1 Merge from eCos master repository on 2002-05-20-20:11:54-BST
jlarmour
parents:
diff changeset
71 #include <sys/types.h>
e0c0827131d1 Merge from eCos master repository on 2002-05-20-20:11:54-BST
jlarmour
parents:
diff changeset
72 #include <sys/socket.h>
e0c0827131d1 Merge from eCos master repository on 2002-05-20-20:11:54-BST
jlarmour
parents:
diff changeset
73 #include <arpa/inet.h>
e0c0827131d1 Merge from eCos master repository on 2002-05-20-20:11:54-BST
jlarmour
parents:
diff changeset
74
e0c0827131d1 Merge from eCos master repository on 2002-05-20-20:11:54-BST
jlarmour
parents:
diff changeset
75 #include <ctype.h>
e0c0827131d1 Merge from eCos master repository on 2002-05-20-20:11:54-BST
jlarmour
parents:
diff changeset
76 #include <unistd.h>
e0c0827131d1 Merge from eCos master repository on 2002-05-20-20:11:54-BST
jlarmour
parents:
diff changeset
77 #include <stdio.h>
e0c0827131d1 Merge from eCos master repository on 2002-05-20-20:11:54-BST
jlarmour
parents:
diff changeset
78 #include <string.h>
e0c0827131d1 Merge from eCos master repository on 2002-05-20-20:11:54-BST
jlarmour
parents:
diff changeset
79 #include <stdlib.h>
e0c0827131d1 Merge from eCos master repository on 2002-05-20-20:11:54-BST
jlarmour
parents:
diff changeset
80
e0c0827131d1 Merge from eCos master repository on 2002-05-20-20:11:54-BST
jlarmour
parents:
diff changeset
81 #include <cyg/ns/dns/dns_priv.h>
e0c0827131d1 Merge from eCos master repository on 2002-05-20-20:11:54-BST
jlarmour
parents:
diff changeset
82
e0c0827131d1 Merge from eCos master repository on 2002-05-20-20:11:54-BST
jlarmour
parents:
diff changeset
83 static short id = 0; /* ID of the last query */
e0c0827131d1 Merge from eCos master repository on 2002-05-20-20:11:54-BST
jlarmour
parents:
diff changeset
84 static int s = -1; /* Socket to the DNS server */
e0c0827131d1 Merge from eCos master repository on 2002-05-20-20:11:54-BST
jlarmour
parents:
diff changeset
85 static cyg_drv_mutex_t dns_mutex; /* Mutex to stop multiple queries as once */
e0c0827131d1 Merge from eCos master repository on 2002-05-20-20:11:54-BST
jlarmour
parents:
diff changeset
86 static cyg_ucount32 ptdindex; /* Index for the per thread data */
e0c0827131d1 Merge from eCos master repository on 2002-05-20-20:11:54-BST
jlarmour
parents:
diff changeset
87 static char * domainname=NULL; /* Domain name used for queries */
e0c0827131d1 Merge from eCos master repository on 2002-05-20-20:11:54-BST
jlarmour
parents:
diff changeset
88
e0c0827131d1 Merge from eCos master repository on 2002-05-20-20:11:54-BST
jlarmour
parents:
diff changeset
89 /* Allocate space for string of length len. Return NULL on failure. */
e0c0827131d1 Merge from eCos master repository on 2002-05-20-20:11:54-BST
jlarmour
parents:
diff changeset
90 static inline char*
e0c0827131d1 Merge from eCos master repository on 2002-05-20-20:11:54-BST
jlarmour
parents:
diff changeset
91 alloc_string(int len)
e0c0827131d1 Merge from eCos master repository on 2002-05-20-20:11:54-BST
jlarmour
parents:
diff changeset
92 {
e0c0827131d1 Merge from eCos master repository on 2002-05-20-20:11:54-BST
jlarmour
parents:
diff changeset
93 return malloc(len);
e0c0827131d1 Merge from eCos master repository on 2002-05-20-20:11:54-BST
jlarmour
parents:
diff changeset
94 }
e0c0827131d1 Merge from eCos master repository on 2002-05-20-20:11:54-BST
jlarmour
parents:
diff changeset
95
e0c0827131d1 Merge from eCos master repository on 2002-05-20-20:11:54-BST
jlarmour
parents:
diff changeset
96 static inline void
e0c0827131d1 Merge from eCos master repository on 2002-05-20-20:11:54-BST
jlarmour
parents:
diff changeset
97 free_string(char* s)
e0c0827131d1 Merge from eCos master repository on 2002-05-20-20:11:54-BST
jlarmour
parents:
diff changeset
98 {
e0c0827131d1 Merge from eCos master repository on 2002-05-20-20:11:54-BST
jlarmour
parents:
diff changeset
99 free(s);
e0c0827131d1 Merge from eCos master repository on 2002-05-20-20:11:54-BST
jlarmour
parents:
diff changeset
100 }
e0c0827131d1 Merge from eCos master repository on 2002-05-20-20:11:54-BST
jlarmour
parents:
diff changeset
101
e0c0827131d1 Merge from eCos master repository on 2002-05-20-20:11:54-BST
jlarmour
parents:
diff changeset
102 /* Deallocate the memory taken to hold a hent structure */
e0c0827131d1 Merge from eCos master repository on 2002-05-20-20:11:54-BST
jlarmour
parents:
diff changeset
103 static void
e0c0827131d1 Merge from eCos master repository on 2002-05-20-20:11:54-BST
jlarmour
parents:
diff changeset
104 free_hent(struct hostent * hent)
e0c0827131d1 Merge from eCos master repository on 2002-05-20-20:11:54-BST
jlarmour
parents:
diff changeset
105 {
e0c0827131d1 Merge from eCos master repository on 2002-05-20-20:11:54-BST
jlarmour
parents:
diff changeset
106 if (hent->h_name) {
e0c0827131d1 Merge from eCos master repository on 2002-05-20-20:11:54-BST
jlarmour
parents:
diff changeset
107 free_string(hent->h_name);
e0c0827131d1 Merge from eCos master repository on 2002-05-20-20:11:54-BST
jlarmour
parents:
diff changeset
108 }
e0c0827131d1 Merge from eCos master repository on 2002-05-20-20:11:54-BST
jlarmour
parents:
diff changeset
109
e0c0827131d1 Merge from eCos master repository on 2002-05-20-20:11:54-BST
jlarmour
parents:
diff changeset
110 if (hent->h_addr_list) {
e0c0827131d1 Merge from eCos master repository on 2002-05-20-20:11:54-BST
jlarmour
parents:
diff changeset
111 int i = 0;
e0c0827131d1 Merge from eCos master repository on 2002-05-20-20:11:54-BST
jlarmour
parents:
diff changeset
112 while (hent->h_addr_list[i]) {
e0c0827131d1 Merge from eCos master repository on 2002-05-20-20:11:54-BST
jlarmour
parents:
diff changeset
113 free(hent->h_addr_list[i]);
e0c0827131d1 Merge from eCos master repository on 2002-05-20-20:11:54-BST
jlarmour
parents:
diff changeset
114 i++;
e0c0827131d1 Merge from eCos master repository on 2002-05-20-20:11:54-BST
jlarmour
parents:
diff changeset
115 }
e0c0827131d1 Merge from eCos master repository on 2002-05-20-20:11:54-BST
jlarmour
parents:
diff changeset
116 free(hent->h_addr_list);
e0c0827131d1 Merge from eCos master repository on 2002-05-20-20:11:54-BST
jlarmour
parents:
diff changeset
117 }
211
6eb55882e01c Merge from eCos master repository on 2002-05-29-16:32:18-BST
jlarmour
parents: 210
diff changeset
118 free(hent);
208
e0c0827131d1 Merge from eCos master repository on 2002-05-20-20:11:54-BST
jlarmour
parents:
diff changeset
119 }
e0c0827131d1 Merge from eCos master repository on 2002-05-20-20:11:54-BST
jlarmour
parents:
diff changeset
120
e0c0827131d1 Merge from eCos master repository on 2002-05-20-20:11:54-BST
jlarmour
parents:
diff changeset
121 /* Allocate hent structure with room for one in_addr. Returns NULL on
e0c0827131d1 Merge from eCos master repository on 2002-05-20-20:11:54-BST
jlarmour
parents:
diff changeset
122 failure. */
e0c0827131d1 Merge from eCos master repository on 2002-05-20-20:11:54-BST
jlarmour
parents:
diff changeset
123 static struct hostent*
e0c0827131d1 Merge from eCos master repository on 2002-05-20-20:11:54-BST
jlarmour
parents:
diff changeset
124 alloc_hent(void)
e0c0827131d1 Merge from eCos master repository on 2002-05-20-20:11:54-BST
jlarmour
parents:
diff changeset
125 {
e0c0827131d1 Merge from eCos master repository on 2002-05-20-20:11:54-BST
jlarmour
parents:
diff changeset
126 struct hostent *hent;
e0c0827131d1 Merge from eCos master repository on 2002-05-20-20:11:54-BST
jlarmour
parents:
diff changeset
127
e0c0827131d1 Merge from eCos master repository on 2002-05-20-20:11:54-BST
jlarmour
parents:
diff changeset
128 hent = malloc(sizeof(struct hostent));
e0c0827131d1 Merge from eCos master repository on 2002-05-20-20:11:54-BST
jlarmour
parents:
diff changeset
129 if (hent) {
e0c0827131d1 Merge from eCos master repository on 2002-05-20-20:11:54-BST
jlarmour
parents:
diff changeset
130 memset(hent, 0, sizeof(struct hostent));
e0c0827131d1 Merge from eCos master repository on 2002-05-20-20:11:54-BST
jlarmour
parents:
diff changeset
131 hent->h_addr_list = malloc(sizeof(char *)*2);
e0c0827131d1 Merge from eCos master repository on 2002-05-20-20:11:54-BST
jlarmour
parents:
diff changeset
132 if (!hent->h_addr_list) {
e0c0827131d1 Merge from eCos master repository on 2002-05-20-20:11:54-BST
jlarmour
parents:
diff changeset
133 free_hent(hent);
e0c0827131d1 Merge from eCos master repository on 2002-05-20-20:11:54-BST
jlarmour
parents:
diff changeset
134 return NULL;
e0c0827131d1 Merge from eCos master repository on 2002-05-20-20:11:54-BST
jlarmour
parents:
diff changeset
135 }
e0c0827131d1 Merge from eCos master repository on 2002-05-20-20:11:54-BST
jlarmour
parents:
diff changeset
136 hent->h_addr_list[0] = malloc(sizeof(struct in_addr));
e0c0827131d1 Merge from eCos master repository on 2002-05-20-20:11:54-BST
jlarmour
parents:
diff changeset
137 if (!hent->h_addr_list[0]) {
e0c0827131d1 Merge from eCos master repository on 2002-05-20-20:11:54-BST
jlarmour
parents:
diff changeset
138 free_hent(hent);
e0c0827131d1 Merge from eCos master repository on 2002-05-20-20:11:54-BST
jlarmour
parents:
diff changeset
139 return NULL;
e0c0827131d1 Merge from eCos master repository on 2002-05-20-20:11:54-BST
jlarmour
parents:
diff changeset
140 }
e0c0827131d1 Merge from eCos master repository on 2002-05-20-20:11:54-BST
jlarmour
parents:
diff changeset
141 hent->h_addr_list[1] = NULL;
e0c0827131d1 Merge from eCos master repository on 2002-05-20-20:11:54-BST
jlarmour
parents:
diff changeset
142 }
e0c0827131d1 Merge from eCos master repository on 2002-05-20-20:11:54-BST
jlarmour
parents:
diff changeset
143
e0c0827131d1 Merge from eCos master repository on 2002-05-20-20:11:54-BST
jlarmour
parents:
diff changeset
144 return hent;
e0c0827131d1 Merge from eCos master repository on 2002-05-20-20:11:54-BST
jlarmour
parents:
diff changeset
145 }
e0c0827131d1 Merge from eCos master repository on 2002-05-20-20:11:54-BST
jlarmour
parents:
diff changeset
146
210
d2c90368aeef Merge from eCos master repository on 2002-05-23-21:39:14-BST
jlarmour
parents: 208
diff changeset
147 /* Thread destructor used to free stuff stored in per-thread data slot. */
d2c90368aeef Merge from eCos master repository on 2002-05-23-21:39:14-BST
jlarmour
parents: 208
diff changeset
148 static void
d2c90368aeef Merge from eCos master repository on 2002-05-23-21:39:14-BST
jlarmour
parents: 208
diff changeset
149 thread_destructor(CYG_ADDRWORD data)
d2c90368aeef Merge from eCos master repository on 2002-05-23-21:39:14-BST
jlarmour
parents: 208
diff changeset
150 {
d2c90368aeef Merge from eCos master repository on 2002-05-23-21:39:14-BST
jlarmour
parents: 208
diff changeset
151 struct hostent *hent;
212
94b558c9fb67 Merge from eCos master repository on 2002-05-31-00:05:29-BST
jlarmour
parents: 211
diff changeset
152 hent = (struct hostent *)cyg_thread_get_data(ptdindex);
210
d2c90368aeef Merge from eCos master repository on 2002-05-23-21:39:14-BST
jlarmour
parents: 208
diff changeset
153 if (hent)
d2c90368aeef Merge from eCos master repository on 2002-05-23-21:39:14-BST
jlarmour
parents: 208
diff changeset
154 free_hent(hent);
d2c90368aeef Merge from eCos master repository on 2002-05-23-21:39:14-BST
jlarmour
parents: 208
diff changeset
155 return;
d2c90368aeef Merge from eCos master repository on 2002-05-23-21:39:14-BST
jlarmour
parents: 208
diff changeset
156 data=data;
d2c90368aeef Merge from eCos master repository on 2002-05-23-21:39:14-BST
jlarmour
parents: 208
diff changeset
157 }
d2c90368aeef Merge from eCos master repository on 2002-05-23-21:39:14-BST
jlarmour
parents: 208
diff changeset
158
d2c90368aeef Merge from eCos master repository on 2002-05-23-21:39:14-BST
jlarmour
parents: 208
diff changeset
159 /* Store the hent away in the per-thread data. */
d2c90368aeef Merge from eCos master repository on 2002-05-23-21:39:14-BST
jlarmour
parents: 208
diff changeset
160 static void
d2c90368aeef Merge from eCos master repository on 2002-05-23-21:39:14-BST
jlarmour
parents: 208
diff changeset
161 store_hent(struct hostent *hent)
d2c90368aeef Merge from eCos master repository on 2002-05-23-21:39:14-BST
jlarmour
parents: 208
diff changeset
162 {
d2c90368aeef Merge from eCos master repository on 2002-05-23-21:39:14-BST
jlarmour
parents: 208
diff changeset
163 // Prevent memory leaks by setting a destructor to be
d2c90368aeef Merge from eCos master repository on 2002-05-23-21:39:14-BST
jlarmour
parents: 208
diff changeset
164 // called on thread exit to free per-thread data.
d2c90368aeef Merge from eCos master repository on 2002-05-23-21:39:14-BST
jlarmour
parents: 208
diff changeset
165 cyg_thread_add_destructor( &thread_destructor, 0 );
212
94b558c9fb67 Merge from eCos master repository on 2002-05-31-00:05:29-BST
jlarmour
parents: 211
diff changeset
166 cyg_thread_set_data(ptdindex, (CYG_ADDRWORD)hent);
210
d2c90368aeef Merge from eCos master repository on 2002-05-23-21:39:14-BST
jlarmour
parents: 208
diff changeset
167 }
d2c90368aeef Merge from eCos master repository on 2002-05-23-21:39:14-BST
jlarmour
parents: 208
diff changeset
168
d2c90368aeef Merge from eCos master repository on 2002-05-23-21:39:14-BST
jlarmour
parents: 208
diff changeset
169 /* If there is an answer to an old query, free the memory it uses. */
d2c90368aeef Merge from eCos master repository on 2002-05-23-21:39:14-BST
jlarmour
parents: 208
diff changeset
170 static void
d2c90368aeef Merge from eCos master repository on 2002-05-23-21:39:14-BST
jlarmour
parents: 208
diff changeset
171 free_stored_hent(void)
d2c90368aeef Merge from eCos master repository on 2002-05-23-21:39:14-BST
jlarmour
parents: 208
diff changeset
172 {
d2c90368aeef Merge from eCos master repository on 2002-05-23-21:39:14-BST
jlarmour
parents: 208
diff changeset
173 struct hostent *hent;
212
94b558c9fb67 Merge from eCos master repository on 2002-05-31-00:05:29-BST
jlarmour
parents: 211
diff changeset
174 hent = (struct hostent *)cyg_thread_get_data(ptdindex);
210
d2c90368aeef Merge from eCos master repository on 2002-05-23-21:39:14-BST
jlarmour
parents: 208
diff changeset
175 if (hent) {
d2c90368aeef Merge from eCos master repository on 2002-05-23-21:39:14-BST
jlarmour
parents: 208
diff changeset
176 free_hent(hent);
212
94b558c9fb67 Merge from eCos master repository on 2002-05-31-00:05:29-BST
jlarmour
parents: 211
diff changeset
177 cyg_thread_set_data(ptdindex, (CYG_ADDRWORD)NULL);
210
d2c90368aeef Merge from eCos master repository on 2002-05-23-21:39:14-BST
jlarmour
parents: 208
diff changeset
178 cyg_thread_rem_destructor( &thread_destructor, 0 );
d2c90368aeef Merge from eCos master repository on 2002-05-23-21:39:14-BST
jlarmour
parents: 208
diff changeset
179 }
d2c90368aeef Merge from eCos master repository on 2002-05-23-21:39:14-BST
jlarmour
parents: 208
diff changeset
180 }
d2c90368aeef Merge from eCos master repository on 2002-05-23-21:39:14-BST
jlarmour
parents: 208
diff changeset
181
208
e0c0827131d1 Merge from eCos master repository on 2002-05-20-20:11:54-BST
jlarmour
parents:
diff changeset
182 /* Send the query to the server and read the response back. Return -1
e0c0827131d1 Merge from eCos master repository on 2002-05-20-20:11:54-BST
jlarmour
parents:
diff changeset
183 if it fails, otherwise put the response back in msg and return the
e0c0827131d1 Merge from eCos master repository on 2002-05-20-20:11:54-BST
jlarmour
parents:
diff changeset
184 length of the response. */
e0c0827131d1 Merge from eCos master repository on 2002-05-20-20:11:54-BST
jlarmour
parents:
diff changeset
185 static int
e0c0827131d1 Merge from eCos master repository on 2002-05-20-20:11:54-BST
jlarmour
parents:
diff changeset
186 send_recv(char * msg, int len, int msglen)
e0c0827131d1 Merge from eCos master repository on 2002-05-20-20:11:54-BST
jlarmour
parents:
diff changeset
187 {
e0c0827131d1 Merge from eCos master repository on 2002-05-20-20:11:54-BST
jlarmour
parents:
diff changeset
188 struct dns_header *dns_hdr;
e0c0827131d1 Merge from eCos master repository on 2002-05-20-20:11:54-BST
jlarmour
parents:
diff changeset
189 struct timeval timeout;
e0c0827131d1 Merge from eCos master repository on 2002-05-20-20:11:54-BST
jlarmour
parents:
diff changeset
190 int finished = false;
e0c0827131d1 Merge from eCos master repository on 2002-05-20-20:11:54-BST
jlarmour
parents:
diff changeset
191 int backoff = 1;
e0c0827131d1 Merge from eCos master repository on 2002-05-20-20:11:54-BST
jlarmour
parents:
diff changeset
192 fd_set readfds;
e0c0827131d1 Merge from eCos master repository on 2002-05-20-20:11:54-BST
jlarmour
parents:
diff changeset
193 int written;
e0c0827131d1 Merge from eCos master repository on 2002-05-20-20:11:54-BST
jlarmour
parents:
diff changeset
194 int ret;
e0c0827131d1 Merge from eCos master repository on 2002-05-20-20:11:54-BST
jlarmour
parents:
diff changeset
195
e0c0827131d1 Merge from eCos master repository on 2002-05-20-20:11:54-BST
jlarmour
parents:
diff changeset
196 CYG_REPORT_FUNCNAMETYPE( "send_recv", "returning %d" );
e0c0827131d1 Merge from eCos master repository on 2002-05-20-20:11:54-BST
jlarmour
parents:
diff changeset
197 CYG_REPORT_FUNCARG3( "msg=%08x, len=%d, msglen", msg, len, msglen );
e0c0827131d1 Merge from eCos master repository on 2002-05-20-20:11:54-BST
jlarmour
parents:
diff changeset
198
e0c0827131d1 Merge from eCos master repository on 2002-05-20-20:11:54-BST
jlarmour
parents:
diff changeset
199 CYG_CHECK_DATA_PTR( msg, "msg is not a valid pointer!" );
e0c0827131d1 Merge from eCos master repository on 2002-05-20-20:11:54-BST
jlarmour
parents:
diff changeset
200
e0c0827131d1 Merge from eCos master repository on 2002-05-20-20:11:54-BST
jlarmour
parents:
diff changeset
201 dns_hdr = (struct dns_header *) msg;
e0c0827131d1 Merge from eCos master repository on 2002-05-20-20:11:54-BST
jlarmour
parents:
diff changeset
202
e0c0827131d1 Merge from eCos master repository on 2002-05-20-20:11:54-BST
jlarmour
parents:
diff changeset
203 do {
e0c0827131d1 Merge from eCos master repository on 2002-05-20-20:11:54-BST
jlarmour
parents:
diff changeset
204 written = write(s, msg, len);
e0c0827131d1 Merge from eCos master repository on 2002-05-20-20:11:54-BST
jlarmour
parents:
diff changeset
205 if (written < 0) {
e0c0827131d1 Merge from eCos master repository on 2002-05-20-20:11:54-BST
jlarmour
parents:
diff changeset
206 ret = -1;
e0c0827131d1 Merge from eCos master repository on 2002-05-20-20:11:54-BST
jlarmour
parents:
diff changeset
207 break;
e0c0827131d1 Merge from eCos master repository on 2002-05-20-20:11:54-BST
jlarmour
parents:
diff changeset
208 }
e0c0827131d1 Merge from eCos master repository on 2002-05-20-20:11:54-BST
jlarmour
parents:
diff changeset
209
e0c0827131d1 Merge from eCos master repository on 2002-05-20-20:11:54-BST
jlarmour
parents:
diff changeset
210 FD_ZERO(&readfds);
e0c0827131d1 Merge from eCos master repository on 2002-05-20-20:11:54-BST
jlarmour
parents:
diff changeset
211 FD_SET(s, &readfds);
e0c0827131d1 Merge from eCos master repository on 2002-05-20-20:11:54-BST
jlarmour
parents:
diff changeset
212
e0c0827131d1 Merge from eCos master repository on 2002-05-20-20:11:54-BST
jlarmour
parents:
diff changeset
213 timeout.tv_sec = backoff;
e0c0827131d1 Merge from eCos master repository on 2002-05-20-20:11:54-BST
jlarmour
parents:
diff changeset
214 timeout.tv_usec = 0;
e0c0827131d1 Merge from eCos master repository on 2002-05-20-20:11:54-BST
jlarmour
parents:
diff changeset
215 backoff = backoff << 1;
e0c0827131d1 Merge from eCos master repository on 2002-05-20-20:11:54-BST
jlarmour
parents:
diff changeset
216
e0c0827131d1 Merge from eCos master repository on 2002-05-20-20:11:54-BST
jlarmour
parents:
diff changeset
217 ret = select(s+1, &readfds, NULL, NULL, &timeout);
e0c0827131d1 Merge from eCos master repository on 2002-05-20-20:11:54-BST
jlarmour
parents:
diff changeset
218 if (ret < 0) {
e0c0827131d1 Merge from eCos master repository on 2002-05-20-20:11:54-BST
jlarmour
parents:
diff changeset
219 ret = -1;
e0c0827131d1 Merge from eCos master repository on 2002-05-20-20:11:54-BST
jlarmour
parents:
diff changeset
220 break;
e0c0827131d1 Merge from eCos master repository on 2002-05-20-20:11:54-BST
jlarmour
parents:
diff changeset
221 }
e0c0827131d1 Merge from eCos master repository on 2002-05-20-20:11:54-BST
jlarmour
parents:
diff changeset
222 /* Timeout */
e0c0827131d1 Merge from eCos master repository on 2002-05-20-20:11:54-BST
jlarmour
parents:
diff changeset
223 if (ret == 0) {
e0c0827131d1 Merge from eCos master repository on 2002-05-20-20:11:54-BST
jlarmour
parents:
diff changeset
224 if (backoff > 16) {
e0c0827131d1 Merge from eCos master repository on 2002-05-20-20:11:54-BST
jlarmour
parents:
diff changeset
225 h_errno = TRY_AGAIN;
e0c0827131d1 Merge from eCos master repository on 2002-05-20-20:11:54-BST
jlarmour
parents:
diff changeset
226 ret = -1;
e0c0827131d1 Merge from eCos master repository on 2002-05-20-20:11:54-BST
jlarmour
parents:
diff changeset
227 break;
e0c0827131d1 Merge from eCos master repository on 2002-05-20-20:11:54-BST
jlarmour
parents:
diff changeset
228 }
e0c0827131d1 Merge from eCos master repository on 2002-05-20-20:11:54-BST
jlarmour
parents:
diff changeset
229 }
e0c0827131d1 Merge from eCos master repository on 2002-05-20-20:11:54-BST
jlarmour
parents:
diff changeset
230 if (ret == 1) {
e0c0827131d1 Merge from eCos master repository on 2002-05-20-20:11:54-BST
jlarmour
parents:
diff changeset
231 ret = read(s, msg, msglen);
e0c0827131d1 Merge from eCos master repository on 2002-05-20-20:11:54-BST
jlarmour
parents:
diff changeset
232 if (ret < 0) {
e0c0827131d1 Merge from eCos master repository on 2002-05-20-20:11:54-BST
jlarmour
parents:
diff changeset
233 ret = -1;
e0c0827131d1 Merge from eCos master repository on 2002-05-20-20:11:54-BST
jlarmour
parents:
diff changeset
234 break;
e0c0827131d1 Merge from eCos master repository on 2002-05-20-20:11:54-BST
jlarmour
parents:
diff changeset
235 }
e0c0827131d1 Merge from eCos master repository on 2002-05-20-20:11:54-BST
jlarmour
parents:
diff changeset
236
e0c0827131d1 Merge from eCos master repository on 2002-05-20-20:11:54-BST
jlarmour
parents:
diff changeset
237 /* Reply to an old query. Ignore it */
e0c0827131d1 Merge from eCos master repository on 2002-05-20-20:11:54-BST
jlarmour
parents:
diff changeset
238 if (ntohs(dns_hdr->id) != (id-1)) {
e0c0827131d1 Merge from eCos master repository on 2002-05-20-20:11:54-BST
jlarmour
parents:
diff changeset
239 continue;
e0c0827131d1 Merge from eCos master repository on 2002-05-20-20:11:54-BST
jlarmour
parents:
diff changeset
240 }
e0c0827131d1 Merge from eCos master repository on 2002-05-20-20:11:54-BST
jlarmour
parents:
diff changeset
241 finished = true;
e0c0827131d1 Merge from eCos master repository on 2002-05-20-20:11:54-BST
jlarmour
parents:
diff changeset
242 }
e0c0827131d1 Merge from eCos master repository on 2002-05-20-20:11:54-BST
jlarmour
parents:
diff changeset
243 } while (!finished);
e0c0827131d1 Merge from eCos master repository on 2002-05-20-20:11:54-BST
jlarmour
parents:
diff changeset
244
e0c0827131d1 Merge from eCos master repository on 2002-05-20-20:11:54-BST
jlarmour
parents:
diff changeset
245 CYG_REPORT_RETVAL( ret );
e0c0827131d1 Merge from eCos master repository on 2002-05-20-20:11:54-BST
jlarmour
parents:
diff changeset
246
e0c0827131d1 Merge from eCos master repository on 2002-05-20-20:11:54-BST
jlarmour
parents:
diff changeset
247 return ret;
e0c0827131d1 Merge from eCos master repository on 2002-05-20-20:11:54-BST
jlarmour
parents:
diff changeset
248 }
e0c0827131d1 Merge from eCos master repository on 2002-05-20-20:11:54-BST
jlarmour
parents:
diff changeset
249
e0c0827131d1 Merge from eCos master repository on 2002-05-20-20:11:54-BST
jlarmour
parents:
diff changeset
250 /* Include the DNS client implementation code */
e0c0827131d1 Merge from eCos master repository on 2002-05-20-20:11:54-BST
jlarmour
parents:
diff changeset
251 #include <cyg/ns/dns/dns_impl.inl>
e0c0827131d1 Merge from eCos master repository on 2002-05-20-20:11:54-BST
jlarmour
parents:
diff changeset
252
e0c0827131d1 Merge from eCos master repository on 2002-05-20-20:11:54-BST
jlarmour
parents:
diff changeset
253 /* Initialise the resolver. Open a socket and bind it to the address
e0c0827131d1 Merge from eCos master repository on 2002-05-20-20:11:54-BST
jlarmour
parents:
diff changeset
254 of the server. return -1 if something goes wrong, otherwise 0. If
e0c0827131d1 Merge from eCos master repository on 2002-05-20-20:11:54-BST
jlarmour
parents:
diff changeset
255 we are being called a second time we have to be careful to allow
e0c0827131d1 Merge from eCos master repository on 2002-05-20-20:11:54-BST
jlarmour
parents:
diff changeset
256 any ongoing lookups to finish before we close the socket and
e0c0827131d1 Merge from eCos master repository on 2002-05-20-20:11:54-BST
jlarmour
parents:
diff changeset
257 connect to a different DNS server. The danger here is that we may
e0c0827131d1 Merge from eCos master repository on 2002-05-20-20:11:54-BST
jlarmour
parents:
diff changeset
258 have to wait for upto 32 seconds if the DNS server is down.
e0c0827131d1 Merge from eCos master repository on 2002-05-20-20:11:54-BST
jlarmour
parents:
diff changeset
259 */
e0c0827131d1 Merge from eCos master repository on 2002-05-20-20:11:54-BST
jlarmour
parents:
diff changeset
260 int
e0c0827131d1 Merge from eCos master repository on 2002-05-20-20:11:54-BST
jlarmour
parents:
diff changeset
261 cyg_dns_res_init(struct in_addr *dns_server)
e0c0827131d1 Merge from eCos master repository on 2002-05-20-20:11:54-BST
jlarmour
parents:
diff changeset
262 {
e0c0827131d1 Merge from eCos master repository on 2002-05-20-20:11:54-BST
jlarmour
parents:
diff changeset
263 struct sockaddr_in server;
e0c0827131d1 Merge from eCos master repository on 2002-05-20-20:11:54-BST
jlarmour
parents:
diff changeset
264 struct servent *sent;
e0c0827131d1 Merge from eCos master repository on 2002-05-20-20:11:54-BST
jlarmour
parents:
diff changeset
265 static int init =0;
e0c0827131d1 Merge from eCos master repository on 2002-05-20-20:11:54-BST
jlarmour
parents:
diff changeset
266
e0c0827131d1 Merge from eCos master repository on 2002-05-20-20:11:54-BST
jlarmour
parents:
diff changeset
267 CYG_REPORT_FUNCNAMETYPE( "cyg_dns_res_init", "returning %d" );
e0c0827131d1 Merge from eCos master repository on 2002-05-20-20:11:54-BST
jlarmour
parents:
diff changeset
268 CYG_REPORT_FUNCARG1( "dns_server=%08x", dns_server );
e0c0827131d1 Merge from eCos master repository on 2002-05-20-20:11:54-BST
jlarmour
parents:
diff changeset
269
e0c0827131d1 Merge from eCos master repository on 2002-05-20-20:11:54-BST
jlarmour
parents:
diff changeset
270 CYG_CHECK_DATA_PTR( dns_server, "dns_server is not a valid pointer!" );
e0c0827131d1 Merge from eCos master repository on 2002-05-20-20:11:54-BST
jlarmour
parents:
diff changeset
271
e0c0827131d1 Merge from eCos master repository on 2002-05-20-20:11:54-BST
jlarmour
parents:
diff changeset
272 if (init) {
e0c0827131d1 Merge from eCos master repository on 2002-05-20-20:11:54-BST
jlarmour
parents:
diff changeset
273 cyg_drv_mutex_lock(&dns_mutex);
e0c0827131d1 Merge from eCos master repository on 2002-05-20-20:11:54-BST
jlarmour
parents:
diff changeset
274 cyg_thread_free_data_index(ptdindex);
e0c0827131d1 Merge from eCos master repository on 2002-05-20-20:11:54-BST
jlarmour
parents:
diff changeset
275 if (s >= 0) {
e0c0827131d1 Merge from eCos master repository on 2002-05-20-20:11:54-BST
jlarmour
parents:
diff changeset
276 close(s);
e0c0827131d1 Merge from eCos master repository on 2002-05-20-20:11:54-BST
jlarmour
parents:
diff changeset
277 }
e0c0827131d1 Merge from eCos master repository on 2002-05-20-20:11:54-BST
jlarmour
parents:
diff changeset
278 } else {
e0c0827131d1 Merge from eCos master repository on 2002-05-20-20:11:54-BST
jlarmour
parents:
diff changeset
279 init = 1;
e0c0827131d1 Merge from eCos master repository on 2002-05-20-20:11:54-BST
jlarmour
parents:
diff changeset
280 cyg_drv_mutex_init(&dns_mutex);
e0c0827131d1 Merge from eCos master repository on 2002-05-20-20:11:54-BST
jlarmour
parents:
diff changeset
281 cyg_drv_mutex_lock(&dns_mutex);
e0c0827131d1 Merge from eCos master repository on 2002-05-20-20:11:54-BST
jlarmour
parents:
diff changeset
282 }
e0c0827131d1 Merge from eCos master repository on 2002-05-20-20:11:54-BST
jlarmour
parents:
diff changeset
283
e0c0827131d1 Merge from eCos master repository on 2002-05-20-20:11:54-BST
jlarmour
parents:
diff changeset
284
e0c0827131d1 Merge from eCos master repository on 2002-05-20-20:11:54-BST
jlarmour
parents:
diff changeset
285 s = socket(PF_INET, SOCK_DGRAM, 0);
e0c0827131d1 Merge from eCos master repository on 2002-05-20-20:11:54-BST
jlarmour
parents:
diff changeset
286 if (s < 0) {
e0c0827131d1 Merge from eCos master repository on 2002-05-20-20:11:54-BST
jlarmour
parents:
diff changeset
287 cyg_drv_mutex_unlock(&dns_mutex);
e0c0827131d1 Merge from eCos master repository on 2002-05-20-20:11:54-BST
jlarmour
parents:
diff changeset
288 CYG_REPORT_RETVAL( -1 );
e0c0827131d1 Merge from eCos master repository on 2002-05-20-20:11:54-BST
jlarmour
parents:
diff changeset
289 return -1;
e0c0827131d1 Merge from eCos master repository on 2002-05-20-20:11:54-BST
jlarmour
parents:
diff changeset
290 }
e0c0827131d1 Merge from eCos master repository on 2002-05-20-20:11:54-BST
jlarmour
parents:
diff changeset
291
e0c0827131d1 Merge from eCos master repository on 2002-05-20-20:11:54-BST
jlarmour
parents:
diff changeset
292 sent = getservbyname("domain", "udp");
e0c0827131d1 Merge from eCos master repository on 2002-05-20-20:11:54-BST
jlarmour
parents:
diff changeset
293 if (sent == (struct servent *)0) {
e0c0827131d1 Merge from eCos master repository on 2002-05-20-20:11:54-BST
jlarmour
parents:
diff changeset
294 s = -1;
e0c0827131d1 Merge from eCos master repository on 2002-05-20-20:11:54-BST
jlarmour
parents:
diff changeset
295 cyg_drv_mutex_unlock(&dns_mutex);
e0c0827131d1 Merge from eCos master repository on 2002-05-20-20:11:54-BST
jlarmour
parents:
diff changeset
296 CYG_REPORT_RETVAL( -1 );
e0c0827131d1 Merge from eCos master repository on 2002-05-20-20:11:54-BST
jlarmour
parents:
diff changeset
297 return -1;
e0c0827131d1 Merge from eCos master repository on 2002-05-20-20:11:54-BST
jlarmour
parents:
diff changeset
298 }
e0c0827131d1 Merge from eCos master repository on 2002-05-20-20:11:54-BST
jlarmour
parents:
diff changeset
299
e0c0827131d1 Merge from eCos master repository on 2002-05-20-20:11:54-BST
jlarmour
parents:
diff changeset
300 memcpy((char *)&server.sin_addr, dns_server, sizeof(server.sin_addr));
e0c0827131d1 Merge from eCos master repository on 2002-05-20-20:11:54-BST
jlarmour
parents:
diff changeset
301 server.sin_port = sent->s_port;
e0c0827131d1 Merge from eCos master repository on 2002-05-20-20:11:54-BST
jlarmour
parents:
diff changeset
302 server.sin_family = AF_INET;
e0c0827131d1 Merge from eCos master repository on 2002-05-20-20:11:54-BST
jlarmour
parents:
diff changeset
303 server.sin_len = sizeof(server);
e0c0827131d1 Merge from eCos master repository on 2002-05-20-20:11:54-BST
jlarmour
parents:
diff changeset
304
e0c0827131d1 Merge from eCos master repository on 2002-05-20-20:11:54-BST
jlarmour
parents:
diff changeset
305 if (connect(s, (struct sockaddr *)&server, sizeof(server)) < 0) {
e0c0827131d1 Merge from eCos master repository on 2002-05-20-20:11:54-BST
jlarmour
parents:
diff changeset
306 s = -1;
e0c0827131d1 Merge from eCos master repository on 2002-05-20-20:11:54-BST
jlarmour
parents:
diff changeset
307 cyg_drv_mutex_unlock(&dns_mutex);
e0c0827131d1 Merge from eCos master repository on 2002-05-20-20:11:54-BST
jlarmour
parents:
diff changeset
308 CYG_REPORT_RETVAL( -1 );
e0c0827131d1 Merge from eCos master repository on 2002-05-20-20:11:54-BST
jlarmour
parents:
diff changeset
309 return -1;
e0c0827131d1 Merge from eCos master repository on 2002-05-20-20:11:54-BST
jlarmour
parents:
diff changeset
310 }
e0c0827131d1 Merge from eCos master repository on 2002-05-20-20:11:54-BST
jlarmour
parents:
diff changeset
311 ptdindex = cyg_thread_new_data_index();
e0c0827131d1 Merge from eCos master repository on 2002-05-20-20:11:54-BST
jlarmour
parents:
diff changeset
312
e0c0827131d1 Merge from eCos master repository on 2002-05-20-20:11:54-BST
jlarmour
parents:
diff changeset
313 cyg_drv_mutex_unlock(&dns_mutex);
e0c0827131d1 Merge from eCos master repository on 2002-05-20-20:11:54-BST
jlarmour
parents:
diff changeset
314
e0c0827131d1 Merge from eCos master repository on 2002-05-20-20:11:54-BST
jlarmour
parents:
diff changeset
315 CYG_REPORT_RETVAL( 0 );
e0c0827131d1 Merge from eCos master repository on 2002-05-20-20:11:54-BST
jlarmour
parents:
diff changeset
316 return 0;
e0c0827131d1 Merge from eCos master repository on 2002-05-20-20:11:54-BST
jlarmour
parents:
diff changeset
317 }
1005
1f968b35cd30 * doc/dns.sgml: Updated to reflect changes for IPv6.
asl
parents: 212
diff changeset
318
1f968b35cd30 * doc/dns.sgml: Updated to reflect changes for IPv6.
asl
parents: 212
diff changeset
319 /* add_answer checks to see if we already have this answer and if not,
1f968b35cd30 * doc/dns.sgml: Updated to reflect changes for IPv6.
asl
parents: 212
diff changeset
320 adds it to the answers. */
1f968b35cd30 * doc/dns.sgml: Updated to reflect changes for IPv6.
asl
parents: 212
diff changeset
321 static int
1f968b35cd30 * doc/dns.sgml: Updated to reflect changes for IPv6.
asl
parents: 212
diff changeset
322 add_answer(char *rdata, short rr_type, int family,
1f968b35cd30 * doc/dns.sgml: Updated to reflect changes for IPv6.
asl
parents: 212
diff changeset
323 struct sockaddr addrs[], int num, int used) {
1f968b35cd30 * doc/dns.sgml: Updated to reflect changes for IPv6.
asl
parents: 212
diff changeset
324 int i;
1f968b35cd30 * doc/dns.sgml: Updated to reflect changes for IPv6.
asl
parents: 212
diff changeset
325 int found = 0;
1f968b35cd30 * doc/dns.sgml: Updated to reflect changes for IPv6.
asl
parents: 212
diff changeset
326
1f968b35cd30 * doc/dns.sgml: Updated to reflect changes for IPv6.
asl
parents: 212
diff changeset
327 for (i = 0; i < used ; i++) {
1f968b35cd30 * doc/dns.sgml: Updated to reflect changes for IPv6.
asl
parents: 212
diff changeset
328 if ((addrs[i].sa_family == family) &&
1f968b35cd30 * doc/dns.sgml: Updated to reflect changes for IPv6.
asl
parents: 212
diff changeset
329 !memcmp(addrs[i].sa_data, rdata, addrs[i].sa_len)) {
1f968b35cd30 * doc/dns.sgml: Updated to reflect changes for IPv6.
asl
parents: 212
diff changeset
330 found = 1;
1f968b35cd30 * doc/dns.sgml: Updated to reflect changes for IPv6.
asl
parents: 212
diff changeset
331 break;
1f968b35cd30 * doc/dns.sgml: Updated to reflect changes for IPv6.
asl
parents: 212
diff changeset
332 }
1f968b35cd30 * doc/dns.sgml: Updated to reflect changes for IPv6.
asl
parents: 212
diff changeset
333 }
1f968b35cd30 * doc/dns.sgml: Updated to reflect changes for IPv6.
asl
parents: 212
diff changeset
334 if (!found) {
1f968b35cd30 * doc/dns.sgml: Updated to reflect changes for IPv6.
asl
parents: 212
diff changeset
335 memset(&addrs[used],0,sizeof(addrs[used]));
1f968b35cd30 * doc/dns.sgml: Updated to reflect changes for IPv6.
asl
parents: 212
diff changeset
336 addrs[used].sa_family = family;
1f968b35cd30 * doc/dns.sgml: Updated to reflect changes for IPv6.
asl
parents: 212
diff changeset
337
1f968b35cd30 * doc/dns.sgml: Updated to reflect changes for IPv6.
asl
parents: 212
diff changeset
338 switch(family) {
1f968b35cd30 * doc/dns.sgml: Updated to reflect changes for IPv6.
asl
parents: 212
diff changeset
339 case AF_INET: {
1f968b35cd30 * doc/dns.sgml: Updated to reflect changes for IPv6.
asl
parents: 212
diff changeset
340 struct sockaddr_in * addr = (struct sockaddr_in *) &addrs[used];
1f968b35cd30 * doc/dns.sgml: Updated to reflect changes for IPv6.
asl
parents: 212
diff changeset
341 addr->sin_len = sizeof(*addr);
1f968b35cd30 * doc/dns.sgml: Updated to reflect changes for IPv6.
asl
parents: 212
diff changeset
342 memcpy(&addr->sin_addr, rdata, sizeof(struct in_addr));
1f968b35cd30 * doc/dns.sgml: Updated to reflect changes for IPv6.
asl
parents: 212
diff changeset
343 used++;
1f968b35cd30 * doc/dns.sgml: Updated to reflect changes for IPv6.
asl
parents: 212
diff changeset
344 break;
1f968b35cd30 * doc/dns.sgml: Updated to reflect changes for IPv6.
asl
parents: 212
diff changeset
345 }
1f968b35cd30 * doc/dns.sgml: Updated to reflect changes for IPv6.
asl
parents: 212
diff changeset
346 #ifdef CYGPKG_NET_INET6
1f968b35cd30 * doc/dns.sgml: Updated to reflect changes for IPv6.
asl
parents: 212
diff changeset
347 case AF_INET6: {
1f968b35cd30 * doc/dns.sgml: Updated to reflect changes for IPv6.
asl
parents: 212
diff changeset
348 struct sockaddr_in6 * addr = (struct sockaddr_in6 *) &addrs[used];
1f968b35cd30 * doc/dns.sgml: Updated to reflect changes for IPv6.
asl
parents: 212
diff changeset
349 addr->sin6_len = sizeof(*addr);
1f968b35cd30 * doc/dns.sgml: Updated to reflect changes for IPv6.
asl
parents: 212
diff changeset
350 memcpy(&addr->sin6_addr, rdata, sizeof(struct in6_addr));
1f968b35cd30 * doc/dns.sgml: Updated to reflect changes for IPv6.
asl
parents: 212
diff changeset
351 used++;
1f968b35cd30 * doc/dns.sgml: Updated to reflect changes for IPv6.
asl
parents: 212
diff changeset
352 break;
1f968b35cd30 * doc/dns.sgml: Updated to reflect changes for IPv6.
asl
parents: 212
diff changeset
353 }
1f968b35cd30 * doc/dns.sgml: Updated to reflect changes for IPv6.
asl
parents: 212
diff changeset
354 #endif
1f968b35cd30 * doc/dns.sgml: Updated to reflect changes for IPv6.
asl
parents: 212
diff changeset
355 default:
1f968b35cd30 * doc/dns.sgml: Updated to reflect changes for IPv6.
asl
parents: 212
diff changeset
356 used = -EAI_FAMILY;
1f968b35cd30 * doc/dns.sgml: Updated to reflect changes for IPv6.
asl
parents: 212
diff changeset
357 }
1f968b35cd30 * doc/dns.sgml: Updated to reflect changes for IPv6.
asl
parents: 212
diff changeset
358 }
1f968b35cd30 * doc/dns.sgml: Updated to reflect changes for IPv6.
asl
parents: 212
diff changeset
359 return used;
1f968b35cd30 * doc/dns.sgml: Updated to reflect changes for IPv6.
asl
parents: 212
diff changeset
360 }
1f968b35cd30 * doc/dns.sgml: Updated to reflect changes for IPv6.
asl
parents: 212
diff changeset
361
1f968b35cd30 * doc/dns.sgml: Updated to reflect changes for IPv6.
asl
parents: 212
diff changeset
362 /* This decodes the answer and puts the results into the addrs
1f968b35cd30 * doc/dns.sgml: Updated to reflect changes for IPv6.
asl
parents: 212
diff changeset
363 array. This function can deal with IPv6 AAAA records as well as A
1f968b35cd30 * doc/dns.sgml: Updated to reflect changes for IPv6.
asl
parents: 212
diff changeset
364 records. Thus its more complex than the parse_answer function in
1f968b35cd30 * doc/dns.sgml: Updated to reflect changes for IPv6.
asl
parents: 212
diff changeset
365 the inline code. This complexity is only needed by getaddrinfo, so
1f968b35cd30 * doc/dns.sgml: Updated to reflect changes for IPv6.
asl
parents: 212
diff changeset
366 i decided to leave parse_anser alone. */
1f968b35cd30 * doc/dns.sgml: Updated to reflect changes for IPv6.
asl
parents: 212
diff changeset
367
1f968b35cd30 * doc/dns.sgml: Updated to reflect changes for IPv6.
asl
parents: 212
diff changeset
368 static int
1f968b35cd30 * doc/dns.sgml: Updated to reflect changes for IPv6.
asl
parents: 212
diff changeset
369 decode(char *msg, short rr_type, int family,
1f968b35cd30 * doc/dns.sgml: Updated to reflect changes for IPv6.
asl
parents: 212
diff changeset
370 struct sockaddr addrs[], int num, int used, char **canon) {
1f968b35cd30 * doc/dns.sgml: Updated to reflect changes for IPv6.
asl
parents: 212
diff changeset
371
1f968b35cd30 * doc/dns.sgml: Updated to reflect changes for IPv6.
asl
parents: 212
diff changeset
372 struct dns_header *dns_hdr;
1f968b35cd30 * doc/dns.sgml: Updated to reflect changes for IPv6.
asl
parents: 212
diff changeset
373 struct resource_record rr, *rr_p = NULL;
1f968b35cd30 * doc/dns.sgml: Updated to reflect changes for IPv6.
asl
parents: 212
diff changeset
374 char *qname = NULL;
1f968b35cd30 * doc/dns.sgml: Updated to reflect changes for IPv6.
asl
parents: 212
diff changeset
375 char *ptr;
1f968b35cd30 * doc/dns.sgml: Updated to reflect changes for IPv6.
asl
parents: 212
diff changeset
376
1f968b35cd30 * doc/dns.sgml: Updated to reflect changes for IPv6.
asl
parents: 212
diff changeset
377 dns_hdr = (struct dns_header *)msg;
1f968b35cd30 * doc/dns.sgml: Updated to reflect changes for IPv6.
asl
parents: 212
diff changeset
378
1f968b35cd30 * doc/dns.sgml: Updated to reflect changes for IPv6.
asl
parents: 212
diff changeset
379 if (DNS_REPLY_NAME_ERROR == dns_hdr->rcode) {
1f968b35cd30 * doc/dns.sgml: Updated to reflect changes for IPv6.
asl
parents: 212
diff changeset
380 h_errno = HOST_NOT_FOUND;
1f968b35cd30 * doc/dns.sgml: Updated to reflect changes for IPv6.
asl
parents: 212
diff changeset
381 return -EAI_NONAME;
1f968b35cd30 * doc/dns.sgml: Updated to reflect changes for IPv6.
asl
parents: 212
diff changeset
382 }
1f968b35cd30 * doc/dns.sgml: Updated to reflect changes for IPv6.
asl
parents: 212
diff changeset
383
1f968b35cd30 * doc/dns.sgml: Updated to reflect changes for IPv6.
asl
parents: 212
diff changeset
384 if ((dns_hdr->qr != 1) ||
1f968b35cd30 * doc/dns.sgml: Updated to reflect changes for IPv6.
asl
parents: 212
diff changeset
385 (dns_hdr->opcode != DNS_QUERY)) {
1f968b35cd30 * doc/dns.sgml: Updated to reflect changes for IPv6.
asl
parents: 212
diff changeset
386 return -EAI_FAIL;
1f968b35cd30 * doc/dns.sgml: Updated to reflect changes for IPv6.
asl
parents: 212
diff changeset
387 }
1f968b35cd30 * doc/dns.sgml: Updated to reflect changes for IPv6.
asl
parents: 212
diff changeset
388
1f968b35cd30 * doc/dns.sgml: Updated to reflect changes for IPv6.
asl
parents: 212
diff changeset
389 if (dns_hdr->rcode != DNS_REPLY_NOERR) {
1f968b35cd30 * doc/dns.sgml: Updated to reflect changes for IPv6.
asl
parents: 212
diff changeset
390 return -EAI_NONAME;
1f968b35cd30 * doc/dns.sgml: Updated to reflect changes for IPv6.
asl
parents: 212
diff changeset
391 }
1f968b35cd30 * doc/dns.sgml: Updated to reflect changes for IPv6.
asl
parents: 212
diff changeset
392
1f968b35cd30 * doc/dns.sgml: Updated to reflect changes for IPv6.
asl
parents: 212
diff changeset
393 dns_hdr->ancount = ntohs(dns_hdr->ancount);
1f968b35cd30 * doc/dns.sgml: Updated to reflect changes for IPv6.
asl
parents: 212
diff changeset
394 dns_hdr->qdcount = ntohs(dns_hdr->qdcount);
1f968b35cd30 * doc/dns.sgml: Updated to reflect changes for IPv6.
asl
parents: 212
diff changeset
395 ptr = (char *)&dns_hdr[1];
1f968b35cd30 * doc/dns.sgml: Updated to reflect changes for IPv6.
asl
parents: 212
diff changeset
396
1f968b35cd30 * doc/dns.sgml: Updated to reflect changes for IPv6.
asl
parents: 212
diff changeset
397 /* Skip over the query section */
1f968b35cd30 * doc/dns.sgml: Updated to reflect changes for IPv6.
asl
parents: 212
diff changeset
398 if (dns_hdr->qdcount > 0) {
1f968b35cd30 * doc/dns.sgml: Updated to reflect changes for IPv6.
asl
parents: 212
diff changeset
399 while (dns_hdr->qdcount) {
1f968b35cd30 * doc/dns.sgml: Updated to reflect changes for IPv6.
asl
parents: 212
diff changeset
400 ptr += qname_len(ptr);
1f968b35cd30 * doc/dns.sgml: Updated to reflect changes for IPv6.
asl
parents: 212
diff changeset
401 ptr += 4; /* skip type & class */
1f968b35cd30 * doc/dns.sgml: Updated to reflect changes for IPv6.
asl
parents: 212
diff changeset
402 dns_hdr->qdcount--;
1f968b35cd30 * doc/dns.sgml: Updated to reflect changes for IPv6.
asl
parents: 212
diff changeset
403 }
1f968b35cd30 * doc/dns.sgml: Updated to reflect changes for IPv6.
asl
parents: 212
diff changeset
404 }
1f968b35cd30 * doc/dns.sgml: Updated to reflect changes for IPv6.
asl
parents: 212
diff changeset
405
1f968b35cd30 * doc/dns.sgml: Updated to reflect changes for IPv6.
asl
parents: 212
diff changeset
406 /* Read the answers resource records to find an answer of the
1f968b35cd30 * doc/dns.sgml: Updated to reflect changes for IPv6.
asl
parents: 212
diff changeset
407 correct type. */
1f968b35cd30 * doc/dns.sgml: Updated to reflect changes for IPv6.
asl
parents: 212
diff changeset
408 while (dns_hdr->ancount && (used >= 0) && (used < num)) {
1f968b35cd30 * doc/dns.sgml: Updated to reflect changes for IPv6.
asl
parents: 212
diff changeset
409 qname = ptr;
1f968b35cd30 * doc/dns.sgml: Updated to reflect changes for IPv6.
asl
parents: 212
diff changeset
410 ptr += qname_len(ptr);
1f968b35cd30 * doc/dns.sgml: Updated to reflect changes for IPv6.
asl
parents: 212
diff changeset
411 rr_p = (struct resource_record *)ptr;
1f968b35cd30 * doc/dns.sgml: Updated to reflect changes for IPv6.
asl
parents: 212
diff changeset
412 memcpy(&rr, ptr, sizeof(rr));
1f968b35cd30 * doc/dns.sgml: Updated to reflect changes for IPv6.
asl
parents: 212
diff changeset
413 if ((rr.rr_type == htons(rr_type)) &&
1f968b35cd30 * doc/dns.sgml: Updated to reflect changes for IPv6.
asl
parents: 212
diff changeset
414 (rr.class == htons(DNS_CLASS_IN))) {
1f968b35cd30 * doc/dns.sgml: Updated to reflect changes for IPv6.
asl
parents: 212
diff changeset
415 used = add_answer(rr_p->rdata, rr_type, family, addrs, num, used);
1f968b35cd30 * doc/dns.sgml: Updated to reflect changes for IPv6.
asl
parents: 212
diff changeset
416 if (canon && !*canon) {
1f968b35cd30 * doc/dns.sgml: Updated to reflect changes for IPv6.
asl
parents: 212
diff changeset
417 *canon = real_name(msg,qname);
1f968b35cd30 * doc/dns.sgml: Updated to reflect changes for IPv6.
asl
parents: 212
diff changeset
418 }
1f968b35cd30 * doc/dns.sgml: Updated to reflect changes for IPv6.
asl
parents: 212
diff changeset
419 }
1f968b35cd30 * doc/dns.sgml: Updated to reflect changes for IPv6.
asl
parents: 212
diff changeset
420 ptr += sizeof(struct resource_record) -
1f968b35cd30 * doc/dns.sgml: Updated to reflect changes for IPv6.
asl
parents: 212
diff changeset
421 sizeof(rr.rdata) + ntohs(rr.rdlength);
1f968b35cd30 * doc/dns.sgml: Updated to reflect changes for IPv6.
asl
parents: 212
diff changeset
422 dns_hdr->ancount--;
1f968b35cd30 * doc/dns.sgml: Updated to reflect changes for IPv6.
asl
parents: 212
diff changeset
423 }
1f968b35cd30 * doc/dns.sgml: Updated to reflect changes for IPv6.
asl
parents: 212
diff changeset
424 if (used == 0) {
1f968b35cd30 * doc/dns.sgml: Updated to reflect changes for IPv6.
asl
parents: 212
diff changeset
425 return -EAI_NONAME;
1f968b35cd30 * doc/dns.sgml: Updated to reflect changes for IPv6.
asl
parents: 212
diff changeset
426 }
1f968b35cd30 * doc/dns.sgml: Updated to reflect changes for IPv6.
asl
parents: 212
diff changeset
427 return used;
1f968b35cd30 * doc/dns.sgml: Updated to reflect changes for IPv6.
asl
parents: 212
diff changeset
428 }
1f968b35cd30 * doc/dns.sgml: Updated to reflect changes for IPv6.
asl
parents: 212
diff changeset
429
1f968b35cd30 * doc/dns.sgml: Updated to reflect changes for IPv6.
asl
parents: 212
diff changeset
430 /* Do a lookup for a particular type of resource record. */
1f968b35cd30 * doc/dns.sgml: Updated to reflect changes for IPv6.
asl
parents: 212
diff changeset
431 static int
1f968b35cd30 * doc/dns.sgml: Updated to reflect changes for IPv6.
asl
parents: 212
diff changeset
432 do_lookup (const char * hostname,
1f968b35cd30 * doc/dns.sgml: Updated to reflect changes for IPv6.
asl
parents: 212
diff changeset
433 struct sockaddr addrs[], int num, int used,
1f968b35cd30 * doc/dns.sgml: Updated to reflect changes for IPv6.
asl
parents: 212
diff changeset
434 short rr_type, int family, char **canon) {
1f968b35cd30 * doc/dns.sgml: Updated to reflect changes for IPv6.
asl
parents: 212
diff changeset
435
1f968b35cd30 * doc/dns.sgml: Updated to reflect changes for IPv6.
asl
parents: 212
diff changeset
436 unsigned char msg[MAXDNSMSGSIZE];
1f968b35cd30 * doc/dns.sgml: Updated to reflect changes for IPv6.
asl
parents: 212
diff changeset
437 int error;
1f968b35cd30 * doc/dns.sgml: Updated to reflect changes for IPv6.
asl
parents: 212
diff changeset
438 int len;
1f968b35cd30 * doc/dns.sgml: Updated to reflect changes for IPv6.
asl
parents: 212
diff changeset
439
1f968b35cd30 * doc/dns.sgml: Updated to reflect changes for IPv6.
asl
parents: 212
diff changeset
440 /* First try the name as passed in */
1f968b35cd30 * doc/dns.sgml: Updated to reflect changes for IPv6.
asl
parents: 212
diff changeset
441 memset(msg, 0, sizeof(msg));
1f968b35cd30 * doc/dns.sgml: Updated to reflect changes for IPv6.
asl
parents: 212
diff changeset
442 len = build_query(msg, hostname, rr_type);
1f968b35cd30 * doc/dns.sgml: Updated to reflect changes for IPv6.
asl
parents: 212
diff changeset
443 if (len < 0) {
1f968b35cd30 * doc/dns.sgml: Updated to reflect changes for IPv6.
asl
parents: 212
diff changeset
444 return -EAI_FAIL;
1f968b35cd30 * doc/dns.sgml: Updated to reflect changes for IPv6.
asl
parents: 212
diff changeset
445 }
1f968b35cd30 * doc/dns.sgml: Updated to reflect changes for IPv6.
asl
parents: 212
diff changeset
446
1f968b35cd30 * doc/dns.sgml: Updated to reflect changes for IPv6.
asl
parents: 212
diff changeset
447 /* Send the query and wait for an answer */
1f968b35cd30 * doc/dns.sgml: Updated to reflect changes for IPv6.
asl
parents: 212
diff changeset
448 len = send_recv(msg, len, sizeof(msg));
1f968b35cd30 * doc/dns.sgml: Updated to reflect changes for IPv6.
asl
parents: 212
diff changeset
449 if (len < 0) {
1f968b35cd30 * doc/dns.sgml: Updated to reflect changes for IPv6.
asl
parents: 212
diff changeset
450 return -EAI_FAIL;
1f968b35cd30 * doc/dns.sgml: Updated to reflect changes for IPv6.
asl
parents: 212
diff changeset
451 }
1f968b35cd30 * doc/dns.sgml: Updated to reflect changes for IPv6.
asl
parents: 212
diff changeset
452
1f968b35cd30 * doc/dns.sgml: Updated to reflect changes for IPv6.
asl
parents: 212
diff changeset
453 /* Decode the answer */
1f968b35cd30 * doc/dns.sgml: Updated to reflect changes for IPv6.
asl
parents: 212
diff changeset
454 error = decode(msg, rr_type, family, addrs, num, used, canon);
1f968b35cd30 * doc/dns.sgml: Updated to reflect changes for IPv6.
asl
parents: 212
diff changeset
455 return error;
1f968b35cd30 * doc/dns.sgml: Updated to reflect changes for IPv6.
asl
parents: 212
diff changeset
456 }
1f968b35cd30 * doc/dns.sgml: Updated to reflect changes for IPv6.
asl
parents: 212
diff changeset
457
1f968b35cd30 * doc/dns.sgml: Updated to reflect changes for IPv6.
asl
parents: 212
diff changeset
458 static int do_lookups(const char * hostname,
1f968b35cd30 * doc/dns.sgml: Updated to reflect changes for IPv6.
asl
parents: 212
diff changeset
459 struct sockaddr addrs[], int num,
1f968b35cd30 * doc/dns.sgml: Updated to reflect changes for IPv6.
asl
parents: 212
diff changeset
460 int family, char ** canon) {
1f968b35cd30 * doc/dns.sgml: Updated to reflect changes for IPv6.
asl
parents: 212
diff changeset
461 int error;
1f968b35cd30 * doc/dns.sgml: Updated to reflect changes for IPv6.
asl
parents: 212
diff changeset
462 #ifdef CYGPKG_NET_INET6
1f968b35cd30 * doc/dns.sgml: Updated to reflect changes for IPv6.
asl
parents: 212
diff changeset
463 int error6;
1f968b35cd30 * doc/dns.sgml: Updated to reflect changes for IPv6.
asl
parents: 212
diff changeset
464 #endif
1f968b35cd30 * doc/dns.sgml: Updated to reflect changes for IPv6.
asl
parents: 212
diff changeset
465
1f968b35cd30 * doc/dns.sgml: Updated to reflect changes for IPv6.
asl
parents: 212
diff changeset
466 switch (family) {
1f968b35cd30 * doc/dns.sgml: Updated to reflect changes for IPv6.
asl
parents: 212
diff changeset
467 case AF_INET:
1f968b35cd30 * doc/dns.sgml: Updated to reflect changes for IPv6.
asl
parents: 212
diff changeset
468 error = do_lookup(hostname, addrs, num, 0, DNS_TYPE_A, AF_INET, canon);
1f968b35cd30 * doc/dns.sgml: Updated to reflect changes for IPv6.
asl
parents: 212
diff changeset
469 break;
1f968b35cd30 * doc/dns.sgml: Updated to reflect changes for IPv6.
asl
parents: 212
diff changeset
470 #ifdef CYGPKG_NET_INET6
1f968b35cd30 * doc/dns.sgml: Updated to reflect changes for IPv6.
asl
parents: 212
diff changeset
471 case AF_INET6:
1f968b35cd30 * doc/dns.sgml: Updated to reflect changes for IPv6.
asl
parents: 212
diff changeset
472 error = do_lookup(hostname, addrs, num, 0, DNS_TYPE_AAAA, AF_INET6, canon);
1f968b35cd30 * doc/dns.sgml: Updated to reflect changes for IPv6.
asl
parents: 212
diff changeset
473 break;
1f968b35cd30 * doc/dns.sgml: Updated to reflect changes for IPv6.
asl
parents: 212
diff changeset
474 #endif
1f968b35cd30 * doc/dns.sgml: Updated to reflect changes for IPv6.
asl
parents: 212
diff changeset
475 case PF_UNSPEC:
1f968b35cd30 * doc/dns.sgml: Updated to reflect changes for IPv6.
asl
parents: 212
diff changeset
476 #ifndef CYGPKG_NET_INET6
1f968b35cd30 * doc/dns.sgml: Updated to reflect changes for IPv6.
asl
parents: 212
diff changeset
477 error = do_lookup(hostname, addrs, num, 0, DNS_TYPE_A, AF_INET, canon);
1f968b35cd30 * doc/dns.sgml: Updated to reflect changes for IPv6.
asl
parents: 212
diff changeset
478 #else
1f968b35cd30 * doc/dns.sgml: Updated to reflect changes for IPv6.
asl
parents: 212
diff changeset
479 #ifdef CYGOPT_NS_DNS_FIRST_FAMILTY_AF_INET
1f968b35cd30 * doc/dns.sgml: Updated to reflect changes for IPv6.
asl
parents: 212
diff changeset
480 error = do_lookup(hostname, addrs, num, 0, DNS_TYPE_A, AF_INET, canon);
1f968b35cd30 * doc/dns.sgml: Updated to reflect changes for IPv6.
asl
parents: 212
diff changeset
481 if (error > 0 ) {
1f968b35cd30 * doc/dns.sgml: Updated to reflect changes for IPv6.
asl
parents: 212
diff changeset
482 error6 = do_lookup(hostname, addrs, num, error, DNS_TYPE_AAAA,
1f968b35cd30 * doc/dns.sgml: Updated to reflect changes for IPv6.
asl
parents: 212
diff changeset
483 AF_INET6, canon);
1f968b35cd30 * doc/dns.sgml: Updated to reflect changes for IPv6.
asl
parents: 212
diff changeset
484 } else {
1f968b35cd30 * doc/dns.sgml: Updated to reflect changes for IPv6.
asl
parents: 212
diff changeset
485 error6 = do_lookup(hostname, addrs, num, 0, DNS_TYPE_AAAA,
1f968b35cd30 * doc/dns.sgml: Updated to reflect changes for IPv6.
asl
parents: 212
diff changeset
486 AF_INET6, canon);
1f968b35cd30 * doc/dns.sgml: Updated to reflect changes for IPv6.
asl
parents: 212
diff changeset
487 }
1f968b35cd30 * doc/dns.sgml: Updated to reflect changes for IPv6.
asl
parents: 212
diff changeset
488 if (error6 > 0) {
1f968b35cd30 * doc/dns.sgml: Updated to reflect changes for IPv6.
asl
parents: 212
diff changeset
489 error = error6;
1f968b35cd30 * doc/dns.sgml: Updated to reflect changes for IPv6.
asl
parents: 212
diff changeset
490 }
1f968b35cd30 * doc/dns.sgml: Updated to reflect changes for IPv6.
asl
parents: 212
diff changeset
491 #else // CYGOPT_NS_DNS_FIRST_FAMILY_AF_INET
1f968b35cd30 * doc/dns.sgml: Updated to reflect changes for IPv6.
asl
parents: 212
diff changeset
492 error6 = do_lookup(hostname, addrs, num, 0, DNS_TYPE_AAAA,
1f968b35cd30 * doc/dns.sgml: Updated to reflect changes for IPv6.
asl
parents: 212
diff changeset
493 AF_INET6, canon);
1f968b35cd30 * doc/dns.sgml: Updated to reflect changes for IPv6.
asl
parents: 212
diff changeset
494 if (error6> 0 ) {
1f968b35cd30 * doc/dns.sgml: Updated to reflect changes for IPv6.
asl
parents: 212
diff changeset
495 error = do_lookup(hostname, addrs, num, error6, DNS_TYPE_A,
1f968b35cd30 * doc/dns.sgml: Updated to reflect changes for IPv6.
asl
parents: 212
diff changeset
496 AF_INET, canon);
1f968b35cd30 * doc/dns.sgml: Updated to reflect changes for IPv6.
asl
parents: 212
diff changeset
497 } else {
1f968b35cd30 * doc/dns.sgml: Updated to reflect changes for IPv6.
asl
parents: 212
diff changeset
498 error = do_lookup(hostname, addrs, num, 0, DNS_TYPE_A,
1f968b35cd30 * doc/dns.sgml: Updated to reflect changes for IPv6.
asl
parents: 212
diff changeset
499 AF_INET, canon);
1f968b35cd30 * doc/dns.sgml: Updated to reflect changes for IPv6.
asl
parents: 212
diff changeset
500 }
1f968b35cd30 * doc/dns.sgml: Updated to reflect changes for IPv6.
asl
parents: 212
diff changeset
501 #endif // CYGOPT_NS_DNS_FIRST_FAMILY_AF_INET
1f968b35cd30 * doc/dns.sgml: Updated to reflect changes for IPv6.
asl
parents: 212
diff changeset
502 #endif // CYGPKG_NET_INET6
1f968b35cd30 * doc/dns.sgml: Updated to reflect changes for IPv6.
asl
parents: 212
diff changeset
503 break;
1f968b35cd30 * doc/dns.sgml: Updated to reflect changes for IPv6.
asl
parents: 212
diff changeset
504 default:
1f968b35cd30 * doc/dns.sgml: Updated to reflect changes for IPv6.
asl
parents: 212
diff changeset
505 error = -EAI_FAMILY;
1f968b35cd30 * doc/dns.sgml: Updated to reflect changes for IPv6.
asl
parents: 212
diff changeset
506 }
1f968b35cd30 * doc/dns.sgml: Updated to reflect changes for IPv6.
asl
parents: 212
diff changeset
507 return error;
1f968b35cd30 * doc/dns.sgml: Updated to reflect changes for IPv6.
asl
parents: 212
diff changeset
508 }
1f968b35cd30 * doc/dns.sgml: Updated to reflect changes for IPv6.
asl
parents: 212
diff changeset
509
1f968b35cd30 * doc/dns.sgml: Updated to reflect changes for IPv6.
asl
parents: 212
diff changeset
510 /* This implements the interface between getaddrinfo and the dns
1f968b35cd30 * doc/dns.sgml: Updated to reflect changes for IPv6.
asl
parents: 212
diff changeset
511 client. hostent is not used here since that only works with IPv4
1f968b35cd30 * doc/dns.sgml: Updated to reflect changes for IPv6.
asl
parents: 212
diff changeset
512 addresses, where as this function needs to be protocol
1f968b35cd30 * doc/dns.sgml: Updated to reflect changes for IPv6.
asl
parents: 212
diff changeset
513 independent. */
1f968b35cd30 * doc/dns.sgml: Updated to reflect changes for IPv6.
asl
parents: 212
diff changeset
514 int
1f968b35cd30 * doc/dns.sgml: Updated to reflect changes for IPv6.
asl
parents: 212
diff changeset
515 cyg_dns_getaddrinfo(const char * hostname,
1f968b35cd30 * doc/dns.sgml: Updated to reflect changes for IPv6.
asl
parents: 212
diff changeset
516 struct sockaddr addrs[], int num,
1f968b35cd30 * doc/dns.sgml: Updated to reflect changes for IPv6.
asl
parents: 212
diff changeset
517 int family,
1f968b35cd30 * doc/dns.sgml: Updated to reflect changes for IPv6.
asl
parents: 212
diff changeset
518 char ** canon)
1f968b35cd30 * doc/dns.sgml: Updated to reflect changes for IPv6.
asl
parents: 212
diff changeset
519 {
1f968b35cd30 * doc/dns.sgml: Updated to reflect changes for IPv6.
asl
parents: 212
diff changeset
520 int error;
1f968b35cd30 * doc/dns.sgml: Updated to reflect changes for IPv6.
asl
parents: 212
diff changeset
521 char name[256];
1f968b35cd30 * doc/dns.sgml: Updated to reflect changes for IPv6.
asl
parents: 212
diff changeset
522 char * dot;
1f968b35cd30 * doc/dns.sgml: Updated to reflect changes for IPv6.
asl
parents: 212
diff changeset
523
1f968b35cd30 * doc/dns.sgml: Updated to reflect changes for IPv6.
asl
parents: 212
diff changeset
524 CYG_REPORT_FUNCNAMETYPE( "cyg_dns_getaddrinfo", "returning %08x" );
1f968b35cd30 * doc/dns.sgml: Updated to reflect changes for IPv6.
asl
parents: 212
diff changeset
525 CYG_REPORT_FUNCARG3( "hostname=%08x, addrs=%08x, num=%2d",
1f968b35cd30 * doc/dns.sgml: Updated to reflect changes for IPv6.
asl
parents: 212
diff changeset
526 hostname, addrs, num );
1f968b35cd30 * doc/dns.sgml: Updated to reflect changes for IPv6.
asl
parents: 212
diff changeset
527
1f968b35cd30 * doc/dns.sgml: Updated to reflect changes for IPv6.
asl
parents: 212
diff changeset
528 if ( !hostname || !addrs || !num ) {
1f968b35cd30 * doc/dns.sgml: Updated to reflect changes for IPv6.
asl
parents: 212
diff changeset
529 CYG_REPORT_RETVAL( NULL );
1f968b35cd30 * doc/dns.sgml: Updated to reflect changes for IPv6.
asl
parents: 212
diff changeset
530 return -EAI_FAIL;
1f968b35cd30 * doc/dns.sgml: Updated to reflect changes for IPv6.
asl
parents: 212
diff changeset
531 }
1f968b35cd30 * doc/dns.sgml: Updated to reflect changes for IPv6.
asl
parents: 212
diff changeset
532
1f968b35cd30 * doc/dns.sgml: Updated to reflect changes for IPv6.
asl
parents: 212
diff changeset
533 CYG_CHECK_DATA_PTR( hostname, "hostname is not a valid pointer!" );
1f968b35cd30 * doc/dns.sgml: Updated to reflect changes for IPv6.
asl
parents: 212
diff changeset
534 CYG_CHECK_DATA_PTR( addrs, "addrs is not a valid pointer!");
1f968b35cd30 * doc/dns.sgml: Updated to reflect changes for IPv6.
asl
parents: 212
diff changeset
535 CYG_ASSERT( num > 0, "Invalid number of sockaddr stuctures");
1f968b35cd30 * doc/dns.sgml: Updated to reflect changes for IPv6.
asl
parents: 212
diff changeset
536
1f968b35cd30 * doc/dns.sgml: Updated to reflect changes for IPv6.
asl
parents: 212
diff changeset
537 if (!valid_hostname(hostname)) {
1f968b35cd30 * doc/dns.sgml: Updated to reflect changes for IPv6.
asl
parents: 212
diff changeset
538 /* it could be a dot address */
1f968b35cd30 * doc/dns.sgml: Updated to reflect changes for IPv6.
asl
parents: 212
diff changeset
539 struct sockaddr_in * sa4 = (struct sockaddr_in *)&addrs[0];
1f968b35cd30 * doc/dns.sgml: Updated to reflect changes for IPv6.
asl
parents: 212
diff changeset
540 memset(&addrs[0],0,sizeof(struct sockaddr));
1f968b35cd30 * doc/dns.sgml: Updated to reflect changes for IPv6.
asl
parents: 212
diff changeset
541 if (inet_pton(AF_INET, hostname, (char *)&sa4->sin_addr.s_addr)) {
1f968b35cd30 * doc/dns.sgml: Updated to reflect changes for IPv6.
asl
parents: 212
diff changeset
542 sa4->sin_family = AF_INET;
1f968b35cd30 * doc/dns.sgml: Updated to reflect changes for IPv6.
asl
parents: 212
diff changeset
543 sa4->sin_len = sizeof(*sa4);
1f968b35cd30 * doc/dns.sgml: Updated to reflect changes for IPv6.
asl
parents: 212
diff changeset
544 CYG_REPORT_RETVAL (1);
1f968b35cd30 * doc/dns.sgml: Updated to reflect changes for IPv6.
asl
parents: 212
diff changeset
545 return 1;
1f968b35cd30 * doc/dns.sgml: Updated to reflect changes for IPv6.
asl
parents: 212
diff changeset
546 }
1f968b35cd30 * doc/dns.sgml: Updated to reflect changes for IPv6.
asl
parents: 212
diff changeset
547 #ifdef CYGPKG_NET_INET6
1f968b35cd30 * doc/dns.sgml: Updated to reflect changes for IPv6.
asl
parents: 212
diff changeset
548 {
1f968b35cd30 * doc/dns.sgml: Updated to reflect changes for IPv6.
asl
parents: 212
diff changeset
549 /* it could be a colon address */
1f968b35cd30 * doc/dns.sgml: Updated to reflect changes for IPv6.
asl
parents: 212
diff changeset
550 struct sockaddr_in6 * sa6 = (struct sockaddr_in6 *)&addrs[0];
1f968b35cd30 * doc/dns.sgml: Updated to reflect changes for IPv6.
asl
parents: 212
diff changeset
551 memset(&addrs[0],0,sizeof(struct sockaddr));
1f968b35cd30 * doc/dns.sgml: Updated to reflect changes for IPv6.
asl
parents: 212
diff changeset
552 if (inet_pton(AF_INET6, hostname, (char *)&sa6->sin6_addr.s6_addr)) {
1f968b35cd30 * doc/dns.sgml: Updated to reflect changes for IPv6.
asl
parents: 212
diff changeset
553 sa6->sin6_family = AF_INET6;
1f968b35cd30 * doc/dns.sgml: Updated to reflect changes for IPv6.
asl
parents: 212
diff changeset
554 sa6->sin6_len = sizeof(*sa6);
1f968b35cd30 * doc/dns.sgml: Updated to reflect changes for IPv6.
asl
parents: 212
diff changeset
555 CYG_REPORT_RETVAL (1);
1f968b35cd30 * doc/dns.sgml: Updated to reflect changes for IPv6.
asl
parents: 212
diff changeset
556 return 1;
1f968b35cd30 * doc/dns.sgml: Updated to reflect changes for IPv6.
asl
parents: 212
diff changeset
557 }
1f968b35cd30 * doc/dns.sgml: Updated to reflect changes for IPv6.
asl
parents: 212
diff changeset
558 }
1f968b35cd30 * doc/dns.sgml: Updated to reflect changes for IPv6.
asl
parents: 212
diff changeset
559 #endif
1f968b35cd30 * doc/dns.sgml: Updated to reflect changes for IPv6.
asl
parents: 212
diff changeset
560 CYG_REPORT_RETVAL (-EAI_NONAME);
1f968b35cd30 * doc/dns.sgml: Updated to reflect changes for IPv6.
asl
parents: 212
diff changeset
561 return -EAI_NONAME;
1f968b35cd30 * doc/dns.sgml: Updated to reflect changes for IPv6.
asl
parents: 212
diff changeset
562 }
1f968b35cd30 * doc/dns.sgml: Updated to reflect changes for IPv6.
asl
parents: 212
diff changeset
563
1f968b35cd30 * doc/dns.sgml: Updated to reflect changes for IPv6.
asl
parents: 212
diff changeset
564 /* Has the socket to the DNS server been opened? */
1f968b35cd30 * doc/dns.sgml: Updated to reflect changes for IPv6.
asl
parents: 212
diff changeset
565 if (s < 0) {
1f968b35cd30 * doc/dns.sgml: Updated to reflect changes for IPv6.
asl
parents: 212
diff changeset
566 CYG_REPORT_RETVAL( -EIA_FAIL );
1f968b35cd30 * doc/dns.sgml: Updated to reflect changes for IPv6.
asl
parents: 212
diff changeset
567 return -EAI_FAIL;
1f968b35cd30 * doc/dns.sgml: Updated to reflect changes for IPv6.
asl
parents: 212
diff changeset
568 }
1f968b35cd30 * doc/dns.sgml: Updated to reflect changes for IPv6.
asl
parents: 212
diff changeset
569
1f968b35cd30 * doc/dns.sgml: Updated to reflect changes for IPv6.
asl
parents: 212
diff changeset
570 if (domainname) {
1f968b35cd30 * doc/dns.sgml: Updated to reflect changes for IPv6.
asl
parents: 212
diff changeset
571 if ((strlen(hostname) + strlen(domainname)) > 254) {
1f968b35cd30 * doc/dns.sgml: Updated to reflect changes for IPv6.
asl
parents: 212
diff changeset
572 cyg_drv_mutex_unlock(&dns_mutex);
1f968b35cd30 * doc/dns.sgml: Updated to reflect changes for IPv6.
asl
parents: 212
diff changeset
573 CYG_REPORT_RETVAL( -EAI_FAIL );
1f968b35cd30 * doc/dns.sgml: Updated to reflect changes for IPv6.
asl
parents: 212
diff changeset
574 return -EAI_FAIL;
1f968b35cd30 * doc/dns.sgml: Updated to reflect changes for IPv6.
asl
parents: 212
diff changeset
575 }
1f968b35cd30 * doc/dns.sgml: Updated to reflect changes for IPv6.
asl
parents: 212
diff changeset
576 strcpy(name, hostname);
1f968b35cd30 * doc/dns.sgml: Updated to reflect changes for IPv6.
asl
parents: 212
diff changeset
577 strcat(name, ".");
1f968b35cd30 * doc/dns.sgml: Updated to reflect changes for IPv6.
asl
parents: 212
diff changeset
578 strcat(name, domainname);
1f968b35cd30 * doc/dns.sgml: Updated to reflect changes for IPv6.
asl
parents: 212
diff changeset
579 }
1f968b35cd30 * doc/dns.sgml: Updated to reflect changes for IPv6.
asl
parents: 212
diff changeset
580 cyg_drv_mutex_lock(&dns_mutex);
1f968b35cd30 * doc/dns.sgml: Updated to reflect changes for IPv6.
asl
parents: 212
diff changeset
581
1f968b35cd30 * doc/dns.sgml: Updated to reflect changes for IPv6.
asl
parents: 212
diff changeset
582 /* If the hostname ends with . it a FQDN. Don't bother adding the
1f968b35cd30 * doc/dns.sgml: Updated to reflect changes for IPv6.
asl
parents: 212
diff changeset
583 domainname. If it does not contain a . , try appending with the
1f968b35cd30 * doc/dns.sgml: Updated to reflect changes for IPv6.
asl
parents: 212
diff changeset
584 domainname first. If it does have a . , try without a domain name
1f968b35cd30 * doc/dns.sgml: Updated to reflect changes for IPv6.
asl
parents: 212
diff changeset
585 first. */
1f968b35cd30 * doc/dns.sgml: Updated to reflect changes for IPv6.
asl
parents: 212
diff changeset
586
1f968b35cd30 * doc/dns.sgml: Updated to reflect changes for IPv6.
asl
parents: 212
diff changeset
587 dot = rindex(hostname,'.');
1f968b35cd30 * doc/dns.sgml: Updated to reflect changes for IPv6.
asl
parents: 212
diff changeset
588 if (dot) {
1f968b35cd30 * doc/dns.sgml: Updated to reflect changes for IPv6.
asl
parents: 212
diff changeset
589 if (*(dot+1) == '\0') {
1f968b35cd30 * doc/dns.sgml: Updated to reflect changes for IPv6.
asl
parents: 212
diff changeset
590 /* FQDN */
1f968b35cd30 * doc/dns.sgml: Updated to reflect changes for IPv6.
asl
parents: 212
diff changeset
591 error = do_lookups(hostname, addrs, num, family, canon);
1f968b35cd30 * doc/dns.sgml: Updated to reflect changes for IPv6.
asl
parents: 212
diff changeset
592 } else {
1f968b35cd30 * doc/dns.sgml: Updated to reflect changes for IPv6.
asl
parents: 212
diff changeset
593 /* Dot somewhere */
1f968b35cd30 * doc/dns.sgml: Updated to reflect changes for IPv6.
asl
parents: 212
diff changeset
594 error = do_lookups(hostname, addrs, num, family, canon);
1f968b35cd30 * doc/dns.sgml: Updated to reflect changes for IPv6.
asl
parents: 212
diff changeset
595 if (domainname && (error == -EAI_NONAME)) {
1f968b35cd30 * doc/dns.sgml: Updated to reflect changes for IPv6.
asl
parents: 212
diff changeset
596 error = do_lookups(name, addrs, num, family, canon);
1f968b35cd30 * doc/dns.sgml: Updated to reflect changes for IPv6.
asl
parents: 212
diff changeset
597 }
1f968b35cd30 * doc/dns.sgml: Updated to reflect changes for IPv6.
asl
parents: 212
diff changeset
598 }
1f968b35cd30 * doc/dns.sgml: Updated to reflect changes for IPv6.
asl
parents: 212
diff changeset
599 } else {
1f968b35cd30 * doc/dns.sgml: Updated to reflect changes for IPv6.
asl
parents: 212
diff changeset
600 /* No Dot. Try adding domainname first */
1f968b35cd30 * doc/dns.sgml: Updated to reflect changes for IPv6.
asl
parents: 212
diff changeset
601 error = -EAI_NONAME;
1f968b35cd30 * doc/dns.sgml: Updated to reflect changes for IPv6.
asl
parents: 212
diff changeset
602 if (domainname) {
1f968b35cd30 * doc/dns.sgml: Updated to reflect changes for IPv6.
asl
parents: 212
diff changeset
603 error = do_lookups(name, addrs, num, family, canon);
1f968b35cd30 * doc/dns.sgml: Updated to reflect changes for IPv6.
asl
parents: 212
diff changeset
604 }
1f968b35cd30 * doc/dns.sgml: Updated to reflect changes for IPv6.
asl
parents: 212
diff changeset
605 if (error == -EAI_NONAME) {
1f968b35cd30 * doc/dns.sgml: Updated to reflect changes for IPv6.
asl
parents: 212
diff changeset
606 error = do_lookups(hostname, addrs, num, family, canon);
1f968b35cd30 * doc/dns.sgml: Updated to reflect changes for IPv6.
asl
parents: 212
diff changeset
607 }
1f968b35cd30 * doc/dns.sgml: Updated to reflect changes for IPv6.
asl
parents: 212
diff changeset
608 }
1f968b35cd30 * doc/dns.sgml: Updated to reflect changes for IPv6.
asl
parents: 212
diff changeset
609 cyg_drv_mutex_unlock(&dns_mutex);
1f968b35cd30 * doc/dns.sgml: Updated to reflect changes for IPv6.
asl
parents: 212
diff changeset
610 CYG_REPORT_RETVAL( error );
1f968b35cd30 * doc/dns.sgml: Updated to reflect changes for IPv6.
asl
parents: 212
diff changeset
611 return error;
1f968b35cd30 * doc/dns.sgml: Updated to reflect changes for IPv6.
asl
parents: 212
diff changeset
612 }
1f968b35cd30 * doc/dns.sgml: Updated to reflect changes for IPv6.
asl
parents: 212
diff changeset
613
1f968b35cd30 * doc/dns.sgml: Updated to reflect changes for IPv6.
asl
parents: 212
diff changeset
614 /* This implements the interface between getnameinfo and the dns
1f968b35cd30 * doc/dns.sgml: Updated to reflect changes for IPv6.
asl
parents: 212
diff changeset
615 client. */
1f968b35cd30 * doc/dns.sgml: Updated to reflect changes for IPv6.
asl
parents: 212
diff changeset
616 externC int
1f968b35cd30 * doc/dns.sgml: Updated to reflect changes for IPv6.
asl
parents: 212
diff changeset
617 cyg_dns_getnameinfo(const struct sockaddr * sa, char * host, size_t hostlen)
1f968b35cd30 * doc/dns.sgml: Updated to reflect changes for IPv6.
asl
parents: 212
diff changeset
618 {
1f968b35cd30 * doc/dns.sgml: Updated to reflect changes for IPv6.
asl
parents: 212
diff changeset
619 char hostname[80];
1f968b35cd30 * doc/dns.sgml: Updated to reflect changes for IPv6.
asl
parents: 212
diff changeset
620 unsigned char msg[MAXDNSMSGSIZE];
1f968b35cd30 * doc/dns.sgml: Updated to reflect changes for IPv6.
asl
parents: 212
diff changeset
621 struct hostent * hent;
1f968b35cd30 * doc/dns.sgml: Updated to reflect changes for IPv6.
asl
parents: 212
diff changeset
622 int len;
1f968b35cd30 * doc/dns.sgml: Updated to reflect changes for IPv6.
asl
parents: 212
diff changeset
623
1f968b35cd30 * doc/dns.sgml: Updated to reflect changes for IPv6.
asl
parents: 212
diff changeset
624 CYG_REPORT_FUNCNAMETYPE( "cyg_dns_getnameinfo", "returning %08x" );
1f968b35cd30 * doc/dns.sgml: Updated to reflect changes for IPv6.
asl
parents: 212
diff changeset
625 CYG_REPORT_FUNCARG3( "sa=%08x, host=%08x, hostlen=%3d",
1f968b35cd30 * doc/dns.sgml: Updated to reflect changes for IPv6.
asl
parents: 212
diff changeset
626 sa, host, hostlen );
1f968b35cd30 * doc/dns.sgml: Updated to reflect changes for IPv6.
asl
parents: 212
diff changeset
627
1f968b35cd30 * doc/dns.sgml: Updated to reflect changes for IPv6.
asl
parents: 212
diff changeset
628 CYG_CHECK_DATA_PTR( sa, "sa is not a valid pointer");
1f968b35cd30 * doc/dns.sgml: Updated to reflect changes for IPv6.
asl
parents: 212
diff changeset
629 CYG_CHECK_DATA_PTR( host, "host is not a valid data pointer");
1f968b35cd30 * doc/dns.sgml: Updated to reflect changes for IPv6.
asl
parents: 212
diff changeset
630 CYG_ASSERT(hostlen >0, "Invalid host length");
1f968b35cd30 * doc/dns.sgml: Updated to reflect changes for IPv6.
asl
parents: 212
diff changeset
631
1f968b35cd30 * doc/dns.sgml: Updated to reflect changes for IPv6.
asl
parents: 212
diff changeset
632 /* Has the socket to the DNS server been opened? */
1f968b35cd30 * doc/dns.sgml: Updated to reflect changes for IPv6.
asl
parents: 212
diff changeset
633 if (s < 0) {
1f968b35cd30 * doc/dns.sgml: Updated to reflect changes for IPv6.
asl
parents: 212
diff changeset
634 CYG_REPORT_RETVAL( -EIA_FAIL );
1f968b35cd30 * doc/dns.sgml: Updated to reflect changes for IPv6.
asl
parents: 212
diff changeset
635 return -EAI_FAIL;
1f968b35cd30 * doc/dns.sgml: Updated to reflect changes for IPv6.
asl
parents: 212
diff changeset
636 }
1f968b35cd30 * doc/dns.sgml: Updated to reflect changes for IPv6.
asl
parents: 212
diff changeset
637
1f968b35cd30 * doc/dns.sgml: Updated to reflect changes for IPv6.
asl
parents: 212
diff changeset
638 cyg_drv_mutex_lock(&dns_mutex);
1f968b35cd30 * doc/dns.sgml: Updated to reflect changes for IPv6.
asl
parents: 212
diff changeset
639
1f968b35cd30 * doc/dns.sgml: Updated to reflect changes for IPv6.
asl
parents: 212
diff changeset
640 switch (sa->sa_family) {
1f968b35cd30 * doc/dns.sgml: Updated to reflect changes for IPv6.
asl
parents: 212
diff changeset
641 case AF_INET: {
1f968b35cd30 * doc/dns.sgml: Updated to reflect changes for IPv6.
asl
parents: 212
diff changeset
642 struct sockaddr_in * sa4 = (struct sockaddr_in *)sa;
1f968b35cd30 * doc/dns.sgml: Updated to reflect changes for IPv6.
asl
parents: 212
diff changeset
643 unsigned char * addr = (char *)&sa4->sin_addr.s_addr;
1f968b35cd30 * doc/dns.sgml: Updated to reflect changes for IPv6.
asl
parents: 212
diff changeset
644 sprintf(hostname, "%d.%d.%d.%d.IN-ADDR.ARPA.",
1f968b35cd30 * doc/dns.sgml: Updated to reflect changes for IPv6.
asl
parents: 212
diff changeset
645 addr[3],addr[2],addr[1],addr[0]);
1f968b35cd30 * doc/dns.sgml: Updated to reflect changes for IPv6.
asl
parents: 212
diff changeset
646 break;
1f968b35cd30 * doc/dns.sgml: Updated to reflect changes for IPv6.
asl
parents: 212
diff changeset
647 }
1f968b35cd30 * doc/dns.sgml: Updated to reflect changes for IPv6.
asl
parents: 212
diff changeset
648 #ifdef CYGPKG_NET_INET6
1f968b35cd30 * doc/dns.sgml: Updated to reflect changes for IPv6.
asl
parents: 212
diff changeset
649 case AF_INET6: {
1f968b35cd30 * doc/dns.sgml: Updated to reflect changes for IPv6.
asl
parents: 212
diff changeset
650 struct sockaddr_in6 * sa6 = (struct sockaddr_in6 *)sa;
1f968b35cd30 * doc/dns.sgml: Updated to reflect changes for IPv6.
asl
parents: 212
diff changeset
651 int i;
1f968b35cd30 * doc/dns.sgml: Updated to reflect changes for IPv6.
asl
parents: 212
diff changeset
652
1f968b35cd30 * doc/dns.sgml: Updated to reflect changes for IPv6.
asl
parents: 212
diff changeset
653 for (i=15; i >= 0; i--) {
1f968b35cd30 * doc/dns.sgml: Updated to reflect changes for IPv6.
asl
parents: 212
diff changeset
654 sprintf(&hostname[(15*2*2) - (i*2*2)], "%x.%x.",
1f968b35cd30 * doc/dns.sgml: Updated to reflect changes for IPv6.
asl
parents: 212
diff changeset
655 sa6->sin6_addr.s6_addr[i] & 0x0f,
1f968b35cd30 * doc/dns.sgml: Updated to reflect changes for IPv6.
asl
parents: 212
diff changeset
656 (sa6->sin6_addr.s6_addr[i] & 0xf0) >> 4);
1f968b35cd30 * doc/dns.sgml: Updated to reflect changes for IPv6.
asl
parents: 212
diff changeset
657 }
1f968b35cd30 * doc/dns.sgml: Updated to reflect changes for IPv6.
asl
parents: 212
diff changeset
658 sprintf(&hostname[16*2*2],"IP6.INT");
1f968b35cd30 * doc/dns.sgml: Updated to reflect changes for IPv6.
asl
parents: 212
diff changeset
659 break;
1f968b35cd30 * doc/dns.sgml: Updated to reflect changes for IPv6.
asl
parents: 212
diff changeset
660 }
1f968b35cd30 * doc/dns.sgml: Updated to reflect changes for IPv6.
asl
parents: 212
diff changeset
661 #endif
1f968b35cd30 * doc/dns.sgml: Updated to reflect changes for IPv6.
asl
parents: 212
diff changeset
662 default:
1f968b35cd30 * doc/dns.sgml: Updated to reflect changes for IPv6.
asl
parents: 212
diff changeset
663 cyg_drv_mutex_lock(&dns_mutex);
1f968b35cd30 * doc/dns.sgml: Updated to reflect changes for IPv6.
asl
parents: 212
diff changeset
664 CYG_REPORT_RETVAL( -EAI_FAMILY);
1f968b35cd30 * doc/dns.sgml: Updated to reflect changes for IPv6.
asl
parents: 212
diff changeset
665 return -EAI_FAMILY;
1f968b35cd30 * doc/dns.sgml: Updated to reflect changes for IPv6.
asl
parents: 212
diff changeset
666 }
1f968b35cd30 * doc/dns.sgml: Updated to reflect changes for IPv6.
asl
parents: 212
diff changeset
667
1f968b35cd30 * doc/dns.sgml: Updated to reflect changes for IPv6.
asl
parents: 212
diff changeset
668 memset(msg, 0, sizeof(msg));
1f968b35cd30 * doc/dns.sgml: Updated to reflect changes for IPv6.
asl
parents: 212
diff changeset
669
1f968b35cd30 * doc/dns.sgml: Updated to reflect changes for IPv6.
asl
parents: 212
diff changeset
670 /* Build a PTR type request using the hostname */
1f968b35cd30 * doc/dns.sgml: Updated to reflect changes for IPv6.
asl
parents: 212
diff changeset
671 len = build_query(msg, hostname, DNS_TYPE_PTR);
1f968b35cd30 * doc/dns.sgml: Updated to reflect changes for IPv6.
asl
parents: 212
diff changeset
672 if (len < 0) {
1f968b35cd30 * doc/dns.sgml: Updated to reflect changes for IPv6.
asl
parents: 212
diff changeset
673 cyg_drv_mutex_unlock(&dns_mutex);
1f968b35cd30 * doc/dns.sgml: Updated to reflect changes for IPv6.
asl
parents: 212
diff changeset
674 CYG_REPORT_RETVAL( -EAI_FAIL );
1f968b35cd30 * doc/dns.sgml: Updated to reflect changes for IPv6.
asl
parents: 212
diff changeset
675 return -EAI_FAIL;
1f968b35cd30 * doc/dns.sgml: Updated to reflect changes for IPv6.
asl
parents: 212
diff changeset
676 }
1f968b35cd30 * doc/dns.sgml: Updated to reflect changes for IPv6.
asl
parents: 212
diff changeset
677
1f968b35cd30 * doc/dns.sgml: Updated to reflect changes for IPv6.
asl
parents: 212
diff changeset
678 /* Send the request and wait for an answer */
1f968b35cd30 * doc/dns.sgml: Updated to reflect changes for IPv6.
asl
parents: 212
diff changeset
679 len = send_recv(msg, len, sizeof(msg));
1f968b35cd30 * doc/dns.sgml: Updated to reflect changes for IPv6.
asl
parents: 212
diff changeset
680 if (len < 0) {
1f968b35cd30 * doc/dns.sgml: Updated to reflect changes for IPv6.
asl
parents: 212
diff changeset
681 cyg_drv_mutex_unlock(&dns_mutex);
1f968b35cd30 * doc/dns.sgml: Updated to reflect changes for IPv6.
asl
parents: 212
diff changeset
682 CYG_REPORT_RETVAL( -EAI_FAIL );
1f968b35cd30 * doc/dns.sgml: Updated to reflect changes for IPv6.
asl
parents: 212
diff changeset
683 return -EAI_FAIL;
1f968b35cd30 * doc/dns.sgml: Updated to reflect changes for IPv6.
asl
parents: 212
diff changeset
684 }
1f968b35cd30 * doc/dns.sgml: Updated to reflect changes for IPv6.
asl
parents: 212
diff changeset
685
1f968b35cd30 * doc/dns.sgml: Updated to reflect changes for IPv6.
asl
parents: 212
diff changeset
686 /* Parse the answer for the host name */
1f968b35cd30 * doc/dns.sgml: Updated to reflect changes for IPv6.
asl
parents: 212
diff changeset
687 hent = parse_answer(msg, DNS_TYPE_PTR);
1f968b35cd30 * doc/dns.sgml: Updated to reflect changes for IPv6.
asl
parents: 212
diff changeset
688
1f968b35cd30 * doc/dns.sgml: Updated to reflect changes for IPv6.
asl
parents: 212
diff changeset
689 /* If no name is known return an error */
1f968b35cd30 * doc/dns.sgml: Updated to reflect changes for IPv6.
asl
parents: 212
diff changeset
690 if (!hent) {
1f968b35cd30 * doc/dns.sgml: Updated to reflect changes for IPv6.
asl
parents: 212
diff changeset
691 cyg_drv_mutex_unlock(&dns_mutex);
1f968b35cd30 * doc/dns.sgml: Updated to reflect changes for IPv6.
asl
parents: 212
diff changeset
692 CYG_REPORT_RETVAL( -EAI_NONAME );
1f968b35cd30 * doc/dns.sgml: Updated to reflect changes for IPv6.
asl
parents: 212
diff changeset
693 return -EAI_NONAME;
1f968b35cd30 * doc/dns.sgml: Updated to reflect changes for IPv6.
asl
parents: 212
diff changeset
694 }
1f968b35cd30 * doc/dns.sgml: Updated to reflect changes for IPv6.
asl
parents: 212
diff changeset
695
1f968b35cd30 * doc/dns.sgml: Updated to reflect changes for IPv6.
asl
parents: 212
diff changeset
696 /* Otherwise copy it into our results buffer and tidy up */
1f968b35cd30 * doc/dns.sgml: Updated to reflect changes for IPv6.
asl
parents: 212
diff changeset
697 strncpy(host, hent->h_name,hostlen);
1f968b35cd30 * doc/dns.sgml: Updated to reflect changes for IPv6.
asl
parents: 212
diff changeset
698 free_hent(hent);
1f968b35cd30 * doc/dns.sgml: Updated to reflect changes for IPv6.
asl
parents: 212
diff changeset
699
1f968b35cd30 * doc/dns.sgml: Updated to reflect changes for IPv6.
asl
parents: 212
diff changeset
700 cyg_drv_mutex_unlock(&dns_mutex);
1f968b35cd30 * doc/dns.sgml: Updated to reflect changes for IPv6.
asl
parents: 212
diff changeset
701 CYG_REPORT_RETVAL( -EAI_NONE );
1f968b35cd30 * doc/dns.sgml: Updated to reflect changes for IPv6.
asl
parents: 212
diff changeset
702 return -EAI_NONE;
1f968b35cd30 * doc/dns.sgml: Updated to reflect changes for IPv6.
asl
parents: 212
diff changeset
703 }