1 /* Temporary, thread-local resolver state.
2 Copyright (C) 2017-2018 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
10 The GNU C Library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; if not, see
17 <http://www.gnu.org/licenses/>. */
19 /* struct resolv_context objects are allocated on the heap,
20 initialized by __resolv_context_get (and its variants), and
21 destroyed by __resolv_context_put.
23 A nested call to __resolv_context_get (after another call to
24 __resolv_context_get without a matching __resolv_context_put call,
25 on the same thread) returns the original pointer, instead of
26 allocating a new context. This prevents unexpected reloading of
27 the resolver configuration. Care is taken to keep the context in
28 sync with the thread-local _res object. (This does not happen with
29 __resolv_context_get_override, and __resolv_context_get_no_inet6 may
30 also interpose another context object if RES_USE_INET6 needs to be
33 In contrast to struct __res_state, struct resolv_context is not
34 affected by ABI compatibility concerns.
36 For the benefit of the res_n* functions, a struct __res_state
37 pointer is included in the context object, and a separate
38 initialization function is provided. */
40 #ifndef _RESOLV_CONTEXT_H
41 #define _RESOLV_CONTEXT_H
43 #include <bits/types/res_state.h>
44 #include <resolv/resolv_conf.h>
48 /* Temporary resolver state. */
51 struct __res_state
*resp
; /* Backing resolver state. */
53 /* Extended resolver state. This is set to NULL if the
54 __resolv_context_get functions are unable to locate an associated
55 extended state. In this case, the configuration data in *resp
56 has to be used; otherwise, the data from *conf should be
57 preferred (because it is a superset). */
58 struct resolv_conf
*conf
;
60 /* The following fields are for internal use within the
61 resolv_context module. */
62 size_t __refcount
; /* Count of reusages by the get functions. */
63 bool __from_res
; /* True if created from _res. */
65 /* If RES_USE_INET6 was disabled at this level, this field points to
66 the previous context. */
67 struct resolv_context
*__next
;
70 /* Return the current temporary resolver context, or NULL if there was
71 an error (indicated by errno). A call to this function must be
72 paired with a call to __resolv_context_put. */
73 struct resolv_context
*__resolv_context_get (void)
74 __attribute__ ((warn_unused_result
));
75 libc_hidden_proto (__resolv_context_get
)
77 /* Deallocate the temporary resolver context. Converse of
78 __resolv_context_get. Restore the RES_USE_INET6 flag if necessary.
79 Do nothing if CTX is NULL. */
80 void __resolv_context_put (struct resolv_context
*ctx
);
81 libc_hidden_proto (__resolv_context_put
)
83 /* Like __resolv_context_get, but the _res structure can be partially
84 initialzed and those changes will not be overwritten. */
85 struct resolv_context
*__resolv_context_get_preinit (void)
86 __attribute__ ((warn_unused_result
));
87 libc_hidden_proto (__resolv_context_get_preinit
)
89 /* Wrap a struct __res_state object in a struct resolv_context object.
90 A call to this function must be paired with a call to
91 __resolv_context_put. */
92 struct resolv_context
*__resolv_context_get_override (struct __res_state
*)
93 __attribute__ ((nonnull (1), warn_unused_result
));
94 libc_hidden_proto (__resolv_context_get_override
)
96 /* Return the search path entry at INDEX, or NULL if there are fewer
97 than INDEX entries. */
98 static __attribute__ ((nonnull (1), unused
)) const char *
99 __resolv_context_search_list (const struct resolv_context
*ctx
, size_t index
)
101 if (ctx
->conf
!= NULL
)
103 if (index
< ctx
->conf
->search_list_size
)
104 return ctx
->conf
->search_list
[index
];
108 /* Fallback. ctx->resp->dnsrch is a NULL-terminated array. */
109 for (size_t i
= 0; ctx
->resp
->dnsrch
[i
] != NULL
&& i
< MAXDNSRCH
; ++i
)
111 return ctx
->resp
->dnsrch
[i
];
115 /* Return the number of name servers. */
116 static __attribute__ ((nonnull (1), unused
)) size_t
117 __resolv_context_nameserver_count (const struct resolv_context
*ctx
)
119 if (ctx
->conf
!= NULL
)
120 return ctx
->conf
->nameserver_list_size
;
122 return ctx
->resp
->nscount
;
125 /* Return a pointer to the socket address of the name server INDEX, or
126 NULL if the index is out of bounds. */
127 static __attribute__ ((nonnull (1), unused
)) const struct sockaddr
*
128 __resolv_context_nameserver (const struct resolv_context
*ctx
, size_t index
)
130 if (ctx
->conf
!= NULL
)
132 if (index
< ctx
->conf
->nameserver_list_size
)
133 return ctx
->conf
->nameserver_list
[index
];
136 if (index
< ctx
->resp
->nscount
)
138 if (ctx
->resp
->nsaddr_list
[index
].sin_family
!= 0)
139 return (const struct sockaddr
*) &ctx
->resp
->nsaddr_list
[index
];
141 return (const struct sockaddr
*) &ctx
->resp
->_u
._ext
.nsaddrs
[index
];
146 /* Return the number of sort list entries. */
147 static __attribute__ ((nonnull (1), unused
)) size_t
148 __resolv_context_sort_count (const struct resolv_context
*ctx
)
150 if (ctx
->conf
!= NULL
)
151 return ctx
->conf
->sort_list_size
;
153 return ctx
->resp
->nsort
;
156 /* Return the sort list entry at INDEX. */
157 static __attribute__ ((nonnull (1), unused
)) struct resolv_sortlist_entry
158 __resolv_context_sort_entry (const struct resolv_context
*ctx
, size_t index
)
160 if (ctx
->conf
!= NULL
)
162 if (index
< ctx
->conf
->sort_list_size
)
163 return ctx
->conf
->sort_list
[index
];
166 else if (index
< ctx
->resp
->nsort
)
167 return (struct resolv_sortlist_entry
)
169 .addr
= ctx
->resp
->sort_list
[index
].addr
,
170 .mask
= ctx
->resp
->sort_list
[index
].mask
,
173 return (struct resolv_sortlist_entry
) { .mask
= 0, };
176 /* Called during thread shutdown to free the associated resolver
177 context (mostly in response to cancellation, otherwise the
178 __resolv_context_get/__resolv_context_put pairing will already have
179 deallocated the context object). */
180 void __resolv_context_freeres (void) attribute_hidden
;
182 #endif /* _RESOLV_CONTEXT_H */