GCC Code Coverage Report
Directory: ./ Exec Total Coverage
File: lib/libc/asr/res_init.c Lines: 0 25 0.0 %
Date: 2017-11-13 Branches: 0 18 0.0 %

Line Branch Exec Source
1
/*	$OpenBSD: res_init.c,v 1.10 2016/04/05 04:29:21 guenther 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.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; i < ac->ac_nscount && i < MAXNS; i++) {
61
			/*
62
			 * No need to check for length since we copy to a
63
			 * struct sockaddr_storage with a size of 256 bytes
64
			 * and sa_len has only 8 bits.
65
			 */
66
			memcpy(&_res_ext.nsaddr_list[i], ac->ac_ns[i],
67
			    ac->ac_ns[i]->sa_len);
68
			if (ac->ac_ns[i]->sa_len <= sizeof(_res.nsaddr_list[i]))
69
				memcpy(&_res.nsaddr_list[i], ac->ac_ns[i],
70
				    ac->ac_ns[i]->sa_len);
71
			else
72
				memset(&_res.nsaddr_list[i], 0,
73
				    sizeof(_res.nsaddr_list[i]));
74
		}
75
		_res.nscount = i;
76
		_res.options |= RES_INIT;
77
	}
78
	_MUTEX_UNLOCK(&resinit_mutex);
79
80
	/*
81
	 * If the program is not threaded, we want to reflect (some) changes
82
	 * made by the user to the global _res structure.
83
	 * This is a bit of a hack: if there is already an async query on
84
	 * this context, it might change things in its back.  It is ok
85
	 * as long as the user only uses the blocking resolver API.
86
	 * If needed we could consider cloning the context if there is
87
	 * a running query.
88
	 */
89
	if (!__isthreaded) {
90
		ac->ac_nsretries = _res.retry;
91
		ac->ac_options = _res.options;
92
		strlcpy(ac->ac_db, _res.lookups, sizeof(ac->ac_db));
93
		ac->ac_dbcount = strlen(ac->ac_db);
94
	}
95
96
	_asr_ctx_unref(ac);
97
98
	return (0);
99
}
100
DEF_WEAK(res_init);