1*ecb0cd2cSotto /* $OpenBSD: res_init.c,v 1.11 2019/06/17 05:54:45 otto Exp $ */
28082e013Seric /*
38082e013Seric * Copyright (c) 2012 Eric Faurot <eric@openbsd.org>
48082e013Seric *
58082e013Seric * Permission to use, copy, modify, and distribute this software for any
68082e013Seric * purpose with or without fee is hereby granted, provided that the above
78082e013Seric * copyright notice and this permission notice appear in all copies.
88082e013Seric *
98082e013Seric * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
108082e013Seric * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
118082e013Seric * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
128082e013Seric * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
138082e013Seric * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
148082e013Seric * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
158082e013Seric * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
168082e013Seric */
178082e013Seric
188082e013Seric #include <sys/types.h>
19d216d6b1Seric #include <sys/socket.h>
201ed934d0Seric #include <arpa/nameser.h>
218082e013Seric #include <netinet/in.h>
22d216d6b1Seric #include <netdb.h>
238082e013Seric
24d216d6b1Seric #include <asr.h>
258082e013Seric #include <resolv.h>
261ed934d0Seric #include <string.h>
278082e013Seric
281ed934d0Seric #include "asr_private.h"
291ed934d0Seric #include "thread_private.h"
308082e013Seric
318082e013Seric
328082e013Seric struct __res_state _res;
338082e013Seric struct __res_state_ext _res_ext;
348082e013Seric
358082e013Seric int h_errno;
368082e013Seric
378082e013Seric int
res_init(void)388082e013Seric res_init(void)
398082e013Seric {
40c6cbd355Sguenther static void *resinit_mutex;
411ed934d0Seric struct asr_ctx *ac;
42a3b56213Schrisz int i;
431ed934d0Seric
44253ef892Sderaadt ac = _asr_use_resolver(NULL);
451ed934d0Seric
461ed934d0Seric /*
471ed934d0Seric * The first thread to call res_init() will setup the global _res
481ed934d0Seric * structure from the async context, not overriding fields set early
491ed934d0Seric * by the user.
501ed934d0Seric */
51c6cbd355Sguenther _MUTEX_LOCK(&resinit_mutex);
521ed934d0Seric if (!(_res.options & RES_INIT)) {
531ed934d0Seric if (_res.retry == 0)
541ed934d0Seric _res.retry = ac->ac_nsretries;
55*ecb0cd2cSotto if (_res.retrans == 0)
56*ecb0cd2cSotto _res.retrans = ac->ac_nstimeout;
571ed934d0Seric if (_res.options == 0)
581ed934d0Seric _res.options = ac->ac_options;
591ed934d0Seric if (_res.lookups[0] == '\0')
601ed934d0Seric strlcpy(_res.lookups, ac->ac_db, sizeof(_res.lookups));
611ed934d0Seric
62a3b56213Schrisz for (i = 0; i < ac->ac_nscount && i < MAXNS; i++) {
63a3b56213Schrisz /*
64a3b56213Schrisz * No need to check for length since we copy to a
65a3b56213Schrisz * struct sockaddr_storage with a size of 256 bytes
66a3b56213Schrisz * and sa_len has only 8 bits.
67a3b56213Schrisz */
68a3b56213Schrisz memcpy(&_res_ext.nsaddr_list[i], ac->ac_ns[i],
6938a63ed1Ssthen ac->ac_ns[i]->sa_len);
70a3b56213Schrisz if (ac->ac_ns[i]->sa_len <= sizeof(_res.nsaddr_list[i]))
71a3b56213Schrisz memcpy(&_res.nsaddr_list[i], ac->ac_ns[i],
72a3b56213Schrisz ac->ac_ns[i]->sa_len);
73a3b56213Schrisz else
74a3b56213Schrisz memset(&_res.nsaddr_list[i], 0,
75a3b56213Schrisz sizeof(_res.nsaddr_list[i]));
7638a63ed1Ssthen }
77a3b56213Schrisz _res.nscount = i;
781ed934d0Seric _res.options |= RES_INIT;
791ed934d0Seric }
80c6cbd355Sguenther _MUTEX_UNLOCK(&resinit_mutex);
811ed934d0Seric
821ed934d0Seric /*
831ed934d0Seric * If the program is not threaded, we want to reflect (some) changes
841ed934d0Seric * made by the user to the global _res structure.
851ed934d0Seric * This is a bit of a hack: if there is already an async query on
861ed934d0Seric * this context, it might change things in its back. It is ok
871ed934d0Seric * as long as the user only uses the blocking resolver API.
881ed934d0Seric * If needed we could consider cloning the context if there is
891ed934d0Seric * a running query.
901ed934d0Seric */
911ed934d0Seric if (!__isthreaded) {
921ed934d0Seric ac->ac_nsretries = _res.retry;
93*ecb0cd2cSotto ac->ac_nstimeout = _res.retrans;
941ed934d0Seric ac->ac_options = _res.options;
951ed934d0Seric strlcpy(ac->ac_db, _res.lookups, sizeof(ac->ac_db));
961ed934d0Seric ac->ac_dbcount = strlen(ac->ac_db);
971ed934d0Seric }
981ed934d0Seric
99253ef892Sderaadt _asr_ctx_unref(ac);
1008082e013Seric
1018082e013Seric return (0);
1028082e013Seric }
103c8503c07Sguenther DEF_WEAK(res_init);
104