xref: /openbsd/lib/libc/asr/res_init.c (revision 76d0caae)
1 /*	$OpenBSD: res_init.c,v 1.11 2019/06/17 05:54:45 otto 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 	static void *resinit_mutex;
41 	struct asr_ctx	*ac;
42 	int i;
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 	_MUTEX_LOCK(&resinit_mutex);
52 	if (!(_res.options & RES_INIT)) {
53 		if (_res.retry == 0)
54 			_res.retry = ac->ac_nsretries;
55 		if (_res.retrans == 0)
56 			_res.retrans = ac->ac_nstimeout;
57 		if (_res.options == 0)
58 			_res.options = ac->ac_options;
59 		if (_res.lookups[0] == '\0')
60 			strlcpy(_res.lookups, ac->ac_db, sizeof(_res.lookups));
61 
62 		for (i = 0; i < ac->ac_nscount && i < MAXNS; i++) {
63 			/*
64 			 * No need to check for length since we copy to a
65 			 * struct sockaddr_storage with a size of 256 bytes
66 			 * and sa_len has only 8 bits.
67 			 */
68 			memcpy(&_res_ext.nsaddr_list[i], ac->ac_ns[i],
69 			    ac->ac_ns[i]->sa_len);
70 			if (ac->ac_ns[i]->sa_len <= sizeof(_res.nsaddr_list[i]))
71 				memcpy(&_res.nsaddr_list[i], ac->ac_ns[i],
72 				    ac->ac_ns[i]->sa_len);
73 			else
74 				memset(&_res.nsaddr_list[i], 0,
75 				    sizeof(_res.nsaddr_list[i]));
76 		}
77 		_res.nscount = i;
78 		_res.options |= RES_INIT;
79 	}
80 	_MUTEX_UNLOCK(&resinit_mutex);
81 
82 	/*
83 	 * If the program is not threaded, we want to reflect (some) changes
84 	 * made by the user to the global _res structure.
85 	 * This is a bit of a hack: if there is already an async query on
86 	 * this context, it might change things in its back.  It is ok
87 	 * as long as the user only uses the blocking resolver API.
88 	 * If needed we could consider cloning the context if there is
89 	 * a running query.
90 	 */
91 	if (!__isthreaded) {
92 		ac->ac_nsretries = _res.retry;
93 		ac->ac_nstimeout = _res.retrans;
94 		ac->ac_options = _res.options;
95 		strlcpy(ac->ac_db, _res.lookups, sizeof(ac->ac_db));
96 		ac->ac_dbcount = strlen(ac->ac_db);
97 	}
98 
99 	_asr_ctx_unref(ac);
100 
101 	return (0);
102 }
103 DEF_WEAK(res_init);
104