2 * Copyright (C) 1993 AmiTCP/IP Group, <amitcp-group@hut.fi>
3 * Helsinki University of Technology, Finland.
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,
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 */
34 void res_cleanup_db(struct state
*state
)
39 D(bug("[AROSTCP](res_init.c) res_cleanup_db()\n"));
43 for (domain
= state
->dnsrch
; *domain
; domain
++)
44 bsd_free(*domain
, NULL
);
45 bsd_free(state
->dnsrch
, NULL
);
48 if (state
->nsaddr_list
)
49 bsd_free(state
->nsaddr_list
, NULL
);
53 int res_update_db(struct state
*state
)
55 struct DomainentNode
*domain
= NULL
;
56 struct NameserventNode
*ns
= NULL
;
62 D(bug("[AROSTCP](res_init.c) res_update_db()\n"));
65 opts
= state
->options
;
66 res_cleanup_db(state
);
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
)
76 D(bug("[AROSTCP](res_init.c) res_update_db: %d Domains in NetDB\n", n
-1));
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
)
90 D(bug("[AROSTCP](res_init.c) res_update_db: %d Domains in DynDB\n", n
-tmp_n
));
93 /* Allocate space for the array */
94 state
->dnsrch
= bsd_malloc(n
*sizeof(char *), NULL
, NULL
);
97 D(bug("[AROSTCP](res_init.c) res_update_db: Failed to allocate array for dnsrch pointers\n"));
100 ReleaseSemaphore(&DynDB
.dyn_Lock
);
103 /* Copy entries themselves, fill in the array */
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
));
116 ReleaseSemaphore(&DynDB
.dyn_Lock
);
117 res_cleanup_db(state
);
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
));
132 ReleaseSemaphore(&DynDB
.dyn_Lock
);
134 res_cleanup_db(state
);
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
));
146 /* Count nameservers in the NetDB */
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
)
155 #if defined(__AROS__)
156 D(bug("[AROSTCP](res_init.c) res_update_db: %d Nameservers in NetDB\n", n
-1));
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
)
166 #if defined(__AROS__)
167 D(bug("[AROSTCP](res_init.c) res_update_db: %d Nameservers in DynDB\n", n
-tmp_n
-1));
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"));
177 ReleaseSemaphore(&DynDB
.dyn_Lock
);
181 /* Copy nameserver entries */
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
;
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
;
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
;
215 state
->options
= RES_DEFAULT
;