1 /* $OpenBSD: res_init.c,v 1.4 2014/03/26 18:13:15 eric 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; 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 _res.nscount = ac->ac_nscount; 61 for (i = 0; i < ac->ac_nscount; i++) { 62 memcpy(&_res.nsaddr_list[i], ac->ac_ns[i], 63 ac->ac_ns[i]->sa_len); 64 } 65 _res.options |= RES_INIT; 66 } 67 _THREAD_PRIVATE_MUTEX_UNLOCK(init); 68 69 /* 70 * If the program is not threaded, we want to reflect (some) changes 71 * made by the user to the global _res structure. 72 * This is a bit of a hack: if there is already an async query on 73 * this context, it might change things in its back. It is ok 74 * as long as the user only uses the blocking resolver API. 75 * If needed we could consider cloning the context if there is 76 * a running query. 77 */ 78 if (!__isthreaded) { 79 ac->ac_nsretries = _res.retry; 80 ac->ac_options = _res.options; 81 strlcpy(ac->ac_db, _res.lookups, sizeof(ac->ac_db)); 82 ac->ac_dbcount = strlen(ac->ac_db); 83 } 84 85 asr_ctx_unref(ac); 86 87 return (0); 88 } 89