xref: /openbsd-src/lib/libc/asr/res_init.c (revision d0bb122bd699d1bc370b19a711dc1a5a5594920b)
1 /*	$OpenBSD: res_init.c,v 1.8 2015/11/05 23:59:47 bluhm Exp $	*/
2 /*
3  * Copyright (c) 2012 Eric Faurot <eric@openbsd.org>
4  *
5  * Permission to use, copy, modify, and distribute this software for any
6  * purpose with or without fee is hereby granted, provided that the above
7  * copyright notice and this permission notice appear in all copies.
8  *
9  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16  */
17 
18 #include <sys/types.h>
19 #include <sys/socket.h>
20 #include <arpa/nameser.h>
21 #include <netinet/in.h>
22 #include <netdb.h>
23 
24 #include <asr.h>
25 #include <resolv.h>
26 #include <string.h>
27 
28 #include "asr_private.h"
29 #include "thread_private.h"
30 
31 
32 struct __res_state _res;
33 struct __res_state_ext _res_ext;
34 
35 int h_errno;
36 
37 int
38 res_init(void)
39 {
40 	_THREAD_PRIVATE_MUTEX(init);
41 	struct asr_ctx	*ac;
42 	int i, j;
43 
44 	ac = _asr_use_resolver(NULL);
45 
46 	/*
47 	 * The first thread to call res_init() will setup the global _res
48 	 * structure from the async context, not overriding fields set early
49 	 * by the user.
50 	 */
51 	_THREAD_PRIVATE_MUTEX_LOCK(init);
52 	if (!(_res.options & RES_INIT)) {
53 		if (_res.retry == 0)
54 			_res.retry = ac->ac_nsretries;
55 		if (_res.options == 0)
56 			_res.options = ac->ac_options;
57 		if (_res.lookups[0] == '\0')
58 			strlcpy(_res.lookups, ac->ac_db, sizeof(_res.lookups));
59 
60 		for (i = 0, j = 0; i < ac->ac_nscount && j < MAXNS; i++) {
61 			if (ac->ac_ns[i]->sa_family != AF_INET ||
62 			    ac->ac_ns[i]->sa_len > sizeof(_res.nsaddr_list[j]))
63 				continue;
64 			memcpy(&_res.nsaddr_list[j], ac->ac_ns[i],
65 			    ac->ac_ns[i]->sa_len);
66 			j++;
67 		}
68 		_res.nscount = j;
69 		_res.options |= RES_INIT;
70 	}
71 	_THREAD_PRIVATE_MUTEX_UNLOCK(init);
72 
73 	/*
74 	 * If the program is not threaded, we want to reflect (some) changes
75 	 * made by the user to the global _res structure.
76 	 * This is a bit of a hack: if there is already an async query on
77 	 * this context, it might change things in its back.  It is ok
78 	 * as long as the user only uses the blocking resolver API.
79 	 * If needed we could consider cloning the context if there is
80 	 * a running query.
81 	 */
82 	if (!__isthreaded) {
83 		ac->ac_nsretries = _res.retry;
84 		ac->ac_options = _res.options;
85 		strlcpy(ac->ac_db, _res.lookups, sizeof(ac->ac_db));
86 		ac->ac_dbcount = strlen(ac->ac_db);
87 	}
88 
89 	_asr_ctx_unref(ac);
90 
91 	return (0);
92 }
93 DEF_WEAK(res_init);
94