tools/adflib: build only host variant which is used by Sam440 target
[AROS.git] / workbench / network / stacks / AROSTCP / bsdsocket / api / res_init.c
blobe60f1421cdff4d112d8eb58701b73a52c8859706
1 /*
2 * Copyright (C) 1993 AmiTCP/IP Group, <amitcp-group@hut.fi>
3 * Helsinki University of Technology, Finland.
4 * All rights reserved.
5 * Copyright (C) 2005 - 2010 The AROS Dev Team
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2 as
9 * published by the Free Software Foundation.
11 * This program is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place - Suite 330, Boston,
19 * MA 02111-1307, USA.
23 #include <conf.h>
25 #include <sys/param.h>
26 #include <kern/amiga_includes.h>
27 #include <kern/amiga_netdb.h>
28 #include <api/resolv.h>
30 #ifndef AMITCP /* AmiTCP has this in the SocketBase */
31 struct state _res;
32 #endif
34 void res_cleanup_db(struct state *state)
36 char **domain = NULL;
38 #if defined(__AROS__)
39 D(bug("[AROSTCP](res_init.c) res_cleanup_db()\n"));
40 #endif
42 if (state->dnsrch) {
43 for (domain = state->dnsrch; *domain; domain++)
44 bsd_free(*domain, NULL);
45 bsd_free(state->dnsrch, NULL);
46 state->dnsrch = NULL;
48 if (state->nsaddr_list)
49 bsd_free(state->nsaddr_list, NULL);
50 state->options = 0;
53 int res_update_db(struct state *state)
55 struct DomainentNode *domain = NULL;
56 struct NameserventNode *ns = NULL;
57 int l;
58 long opts;
59 ULONG n = 1;
61 #if defined(__AROS__)
62 D(bug("[AROSTCP](res_init.c) res_update_db()\n"));
63 #endif
65 opts = state->options;
66 res_cleanup_db(state);
67 LOCK_R_NDB(NDB);
68 /* Count number of domain names in the NetDB */
69 for (domain = (struct DomainentNode *)NDB->ndb_Domains.mlh_Head;
70 domain->dn_Node.mln_Succ;
71 domain = (struct DomainentNode *)domain->dn_Node.mln_Succ)
73 n++;
75 #if defined(__AROS__)
76 D(bug("[AROSTCP](res_init.c) res_update_db: %d Domains in NetDB\n", n-1));
77 D(ULONG tmp_n = n);
78 #endif
80 ObtainSemaphoreShared(&DynDB.dyn_Lock);
81 /* Add domain names from dynamic entries list */
82 for (domain = (struct DomainentNode *)DynDB.dyn_Domains.mlh_Head;
83 domain->dn_Node.mln_Succ;
84 domain = (struct DomainentNode *)domain->dn_Node.mln_Succ)
86 n++;
89 #if defined(__AROS__)
90 D(bug("[AROSTCP](res_init.c) res_update_db: %d Domains in DynDB\n", n-tmp_n));
91 #endif
93 /* Allocate space for the array */
94 state->dnsrch = bsd_malloc(n*sizeof(char *), NULL, NULL);
95 if (!state->dnsrch) {
96 #if defined(__AROS__)
97 D(bug("[AROSTCP](res_init.c) res_update_db: Failed to allocate array for dnsrch pointers\n"));
98 #endif
99 UNLOCK_NDB(NDB);
100 ReleaseSemaphore(&DynDB.dyn_Lock);
101 return -1;
103 /* Copy entries themselves, fill in the array */
104 n = 0;
105 for (domain = (struct DomainentNode *)NDB->ndb_Domains.mlh_Head;
106 domain->dn_Node.mln_Succ;
107 domain = (struct DomainentNode *)domain->dn_Node.mln_Succ) {
108 l = strlen(domain->dn_Ent.d_name)+1;
109 state->dnsrch[n] = bsd_malloc(l, NULL, NULL);
110 if (!state->dnsrch[n]) {
111 #if defined(__AROS__)
112 D(bug("[AROSTCP](res_init.c) res_update_db: Failed to allocate space for entry %d from NetDB entry '%s' name\n", n,
113 domain->dn_Ent.d_name));
114 #endif
115 UNLOCK_NDB(NDB);
116 ReleaseSemaphore(&DynDB.dyn_Lock);
117 res_cleanup_db(state);
118 return -1;
120 strcpy(state->dnsrch[n++], domain->dn_Ent.d_name);
122 for (domain = (struct DomainentNode *)DynDB.dyn_Domains.mlh_Head;
123 domain->dn_Node.mln_Succ;
124 domain = (struct DomainentNode *)domain->dn_Node.mln_Succ) {
125 l = strlen(domain->dn_Ent.d_name)+1;
126 state->dnsrch[n] = bsd_malloc(l, NULL, NULL);
127 if (!state->dnsrch[n]) {
128 #if defined(__AROS__)
129 D(bug("[AROSTCP](res_init.c) res_update_db: Failed to allocate space for entry %d from DynDB entry '%s' name\n", n, domain->dn_Ent.d_name));
130 #endif
131 UNLOCK_NDB(NDB);
132 ReleaseSemaphore(&DynDB.dyn_Lock);
134 res_cleanup_db(state);
136 return -1;
138 strcpy(state->dnsrch[n++], domain->dn_Ent.d_name);
139 } /* Terminate the array */
141 state->dnsrch[n] = NULL;
142 #if defined(__AROS__)
143 D(bug("[AROSTCP](res_init.c) res_update_db: Last dnsrch array pointer (%d) marked empty\n", n));
144 #endif
146 /* Count nameservers in the NetDB */
147 n = 1;
148 for (ns = (struct NameserventNode *)NDB->ndb_NameServers.mlh_Head;
149 ns->nsn_Node.mln_Succ;
150 ns = (struct NameserventNode *)ns->nsn_Node.mln_Succ)
152 n++;
155 #if defined(__AROS__)
156 D(bug("[AROSTCP](res_init.c) res_update_db: %d Nameservers in NetDB\n", n-1));
157 D(tmp_n = n);
158 #endif
160 for (ns = (struct NameserventNode *)DynDB.dyn_NameServers.mlh_Head;
161 ns->nsn_Node.mln_Succ;
162 ns = (struct NameserventNode *)ns->nsn_Node.mln_Succ)
164 n++;
166 #if defined(__AROS__)
167 D(bug("[AROSTCP](res_init.c) res_update_db: %d Nameservers in DynDB\n", n-tmp_n-1));
168 #endif
170 /* Allocate space for the array */
171 state->nsaddr_list = bsd_malloc(n*sizeof(struct in_addr), NULL, NULL);
172 if (!state->nsaddr_list) {
173 #if defined(__AROS__)
174 D(bug("[AROSTCP](res_init.c) res_update_db: Failed to allocate array for nsaddr_list pointers\n"));
175 #endif
176 UNLOCK_NDB(NDB);
177 ReleaseSemaphore(&DynDB.dyn_Lock);
178 return -1;
181 /* Copy nameserver entries */
182 n = 0;
183 for (ns = (struct NameserventNode *)NDB->ndb_NameServers.mlh_Head;
184 ns->nsn_Node.mln_Succ;
185 ns = (struct NameserventNode *)ns->nsn_Node.mln_Succ)
187 state->nsaddr_list[n++].s_addr = ns->nsn_Ent.ns_addr.s_addr;
189 UNLOCK_NDB(NDB);
191 for (ns = (struct NameserventNode *)DynDB.dyn_NameServers.mlh_Head;
192 ns->nsn_Node.mln_Succ;
193 ns = (struct NameserventNode *)ns->nsn_Node.mln_Succ)
195 state->nsaddr_list[n++].s_addr = ns->nsn_Ent.ns_addr.s_addr;
197 ReleaseSemaphore(&DynDB.dyn_Lock);
198 /* Terminale the array */
199 state->nsaddr_list[n].s_addr = 0;
200 /* Remember NetDB update count */
201 state->dbserial = ndb_Serial;
202 state->options = opts;
203 return 0;
207 res_init(struct state *state)
209 /* Fill in domain names and nameserver addresses */
210 if (res_update_db(state)) return -1;
212 /* Set up default options */
213 state->retrans = RES_TIMEOUT;
214 state->retry = 3;
215 state->options = RES_DEFAULT;
216 return 0;