source: rtems-libbsd/freebsd/lib/libc/resolv/res_data.c @ f41a394

55-freebsd-126-freebsd-12freebsd-9.3
Last change on this file since f41a394 was 3d1e767, checked in by Sebastian Huber <sebastian.huber@…>, on 04/27/16 at 08:25:22

Directly use <sys/types.h> provided by Newlib

  • Property mode set to 100644
File size: 8.1 KB
Line 
1#include <machine/rtems-bsd-user-space.h>
2
3/*
4 * Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC")
5 * Copyright (c) 1995-1999 by Internet Software Consortium.
6 *
7 * Permission to use, copy, modify, and distribute this software for any
8 * purpose with or without fee is hereby granted, provided that the above
9 * copyright notice and this permission notice appear in all copies.
10 *
11 * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES
12 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
13 * MERCHANTABILITY AND FITNESS.  IN NO EVENT SHALL ISC BE LIABLE FOR
14 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
15 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
16 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
17 * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18 */
19
20#if defined(LIBC_SCCS) && !defined(lint)
21static const char rcsid[] = "$Id: res_data.c,v 1.7 2008/12/11 09:59:00 marka Exp $";
22#endif /* LIBC_SCCS and not lint */
23#include <sys/cdefs.h>
24__FBSDID("$FreeBSD$");
25
26#include "port_before.h"
27
28#include <sys/types.h>
29#include <rtems/bsd/sys/param.h>
30#include <sys/socket.h>
31#include <sys/time.h>
32
33#include <netinet/in.h>
34#include <arpa/inet.h>
35#include <arpa/nameser.h>
36
37#include <ctype.h>
38#include <netdb.h>
39#include <resolv.h>
40#include <res_update.h>
41#include <stdio.h>
42#include <stdlib.h>
43#include <string.h>
44#include <unistd.h>
45
46#include "port_after.h"
47
48const char *_res_opcodes[] = {
49        "QUERY",
50        "IQUERY",
51        "CQUERYM",
52        "CQUERYU",      /*%< experimental */
53        "NOTIFY",       /*%< experimental */
54        "UPDATE",
55        "6",
56        "7",
57        "8",
58        "9",
59        "10",
60        "11",
61        "12",
62        "13",
63        "ZONEINIT",
64        "ZONEREF",
65};
66
67#ifdef BIND_UPDATE
68const char *_res_sectioncodes[] = {
69        "ZONE",
70        "PREREQUISITES",
71        "UPDATE",
72        "ADDITIONAL",
73};
74#endif
75
76#ifndef __BIND_NOSTATIC
77
78/* Proto. */
79
80int  res_ourserver_p(const res_state, const struct sockaddr_in *);
81
82int
83res_init(void) {
84        extern int __res_vinit(res_state, int);
85
86        /*
87         * These three fields used to be statically initialized.  This made
88         * it hard to use this code in a shared library.  It is necessary,
89         * now that we're doing dynamic initialization here, that we preserve
90         * the old semantics: if an application modifies one of these three
91         * fields of _res before res_init() is called, res_init() will not
92         * alter them.  Of course, if an application is setting them to
93         * _zero_ before calling res_init(), hoping to override what used
94         * to be the static default, we can't detect it and unexpected results
95         * will follow.  Zero for any of these fields would make no sense,
96         * so one can safely assume that the applications were already getting
97         * unexpected results.
98         *
99         * _res.options is tricky since some apps were known to diddle the bits
100         * before res_init() was first called. We can't replicate that semantic
101         * with dynamic initialization (they may have turned bits off that are
102         * set in RES_DEFAULT).  Our solution is to declare such applications
103         * "broken".  They could fool us by setting RES_INIT but none do (yet).
104         */
105        if (!_res.retrans)
106                _res.retrans = RES_TIMEOUT;
107        if (!_res.retry)
108                _res.retry = RES_DFLRETRY;
109        if (!(_res.options & RES_INIT))
110                _res.options = RES_DEFAULT;
111
112        return (__res_vinit(&_res, 1));
113}
114
115void
116p_query(const u_char *msg) {
117        fp_query(msg, stdout);
118}
119
120void
121fp_query(const u_char *msg, FILE *file) {
122        fp_nquery(msg, PACKETSZ, file);
123}
124
125void
126fp_nquery(const u_char *msg, int len, FILE *file) {
127        if ((_res.options & RES_INIT) == 0U && res_init() == -1)
128                return;
129
130        res_pquery(&_res, msg, len, file);
131}
132
133int
134res_mkquery(int op,                     /*!< opcode of query  */
135            const char *dname,          /*!< domain name  */
136            int class, int type,        /*!< class and type of query  */
137            const u_char *data,         /*!< resource record data  */
138            int datalen,                /*!< length of data  */
139            const u_char *newrr_in,     /*!< new rr for modify or append  */
140            u_char *buf,                /*!< buffer to put query  */
141            int buflen)                 /*!< size of buffer  */
142{
143        if ((_res.options & RES_INIT) == 0U && res_init() == -1) {
144                RES_SET_H_ERRNO(&_res, NETDB_INTERNAL);
145                return (-1);
146        }
147        return (res_nmkquery(&_res, op, dname, class, type,
148                             data, datalen,
149                             newrr_in, buf, buflen));
150}
151
152int
153res_mkupdate(ns_updrec *rrecp_in, u_char *buf, int buflen) {
154        if ((_res.options & RES_INIT) == 0U && res_init() == -1) {
155                RES_SET_H_ERRNO(&_res, NETDB_INTERNAL);
156                return (-1);
157        }
158
159        return (res_nmkupdate(&_res, rrecp_in, buf, buflen));
160}
161
162int
163res_query(const char *name,     /*!< domain name  */
164          int class, int type,  /*!< class and type of query  */
165          u_char *answer,       /*!< buffer to put answer  */
166          int anslen)           /*!< size of answer buffer  */
167{
168        if ((_res.options & RES_INIT) == 0U && res_init() == -1) {
169                RES_SET_H_ERRNO(&_res, NETDB_INTERNAL);
170                return (-1);
171        }
172        return (res_nquery(&_res, name, class, type, answer, anslen));
173}
174
175#ifndef _LIBC
176void
177res_send_setqhook(res_send_qhook hook) {
178        _res.qhook = hook;
179}
180
181void
182res_send_setrhook(res_send_rhook hook) {
183        _res.rhook = hook;
184}
185#endif
186
187int
188res_isourserver(const struct sockaddr_in *inp) {
189        return (res_ourserver_p(&_res, inp));
190}
191
192int
193res_send(const u_char *buf, int buflen, u_char *ans, int anssiz) {
194        if ((_res.options & RES_INIT) == 0U && res_init() == -1) {
195                /* errno should have been set by res_init() in this case. */
196                return (-1);
197        }
198
199        return (res_nsend(&_res, buf, buflen, ans, anssiz));
200}
201
202#ifndef _LIBC
203int
204res_sendsigned(const u_char *buf, int buflen, ns_tsig_key *key,
205               u_char *ans, int anssiz)
206{
207        if ((_res.options & RES_INIT) == 0U && res_init() == -1) {
208                /* errno should have been set by res_init() in this case. */
209                return (-1);
210        }
211
212        return (res_nsendsigned(&_res, buf, buflen, key, ans, anssiz));
213}
214#endif
215
216void
217res_close(void) {
218        res_nclose(&_res);
219}
220
221int
222res_update(ns_updrec *rrecp_in) {
223        if ((_res.options & RES_INIT) == 0U && res_init() == -1) {
224                RES_SET_H_ERRNO(&_res, NETDB_INTERNAL);
225                return (-1);
226        }
227
228        return (res_nupdate(&_res, rrecp_in, NULL));
229}
230
231int
232res_search(const char *name,    /*!< domain name  */
233           int class, int type, /*!< class and type of query  */
234           u_char *answer,      /*!< buffer to put answer  */
235           int anslen)          /*!< size of answer  */
236{
237        if ((_res.options & RES_INIT) == 0U && res_init() == -1) {
238                RES_SET_H_ERRNO(&_res, NETDB_INTERNAL);
239                return (-1);
240        }
241
242        return (res_nsearch(&_res, name, class, type, answer, anslen));
243}
244
245int
246res_querydomain(const char *name,
247                const char *domain,
248                int class, int type,    /*!< class and type of query  */
249                u_char *answer,         /*!< buffer to put answer  */
250                int anslen)             /*!< size of answer  */
251{
252        if ((_res.options & RES_INIT) == 0U && res_init() == -1) {
253                RES_SET_H_ERRNO(&_res, NETDB_INTERNAL);
254                return (-1);
255        }
256
257        return (res_nquerydomain(&_res, name, domain,
258                                 class, type,
259                                 answer, anslen));
260}
261
262u_int
263res_randomid(void) {
264        if ((_res.options & RES_INIT) == 0U && res_init() == -1) {
265                RES_SET_H_ERRNO(&_res, NETDB_INTERNAL);
266                return (-1);
267        }
268
269        return (res_nrandomid(&_res));
270}
271
272int
273res_opt(int n0, u_char *buf, int buflen, int anslen)
274{
275        return (res_nopt(&_res, n0, buf, buflen, anslen));
276}
277
278const char *
279hostalias(const char *name) {
280        static char abuf[MAXDNAME];
281
282        return (res_hostalias(&_res, name, abuf, sizeof abuf));
283}
284
285#ifdef ultrix
286int
287local_hostname_length(const char *hostname) {
288        int len_host, len_domain;
289
290        if (!*_res.defdname)
291                res_init();
292        len_host = strlen(hostname);
293        len_domain = strlen(_res.defdname);
294        if (len_host > len_domain &&
295            !strcasecmp(hostname + len_host - len_domain, _res.defdname) &&
296            hostname[len_host - len_domain - 1] == '.')
297                return (len_host - len_domain - 1);
298        return (0);
299}
300#endif /*ultrix*/
301
302/*
303 * Weak aliases for applications that use certain private entry points,
304 * and fail to include <resolv.h>.
305 */
306#undef res_init
307__weak_reference(__res_init, res_init);
308#undef p_query
309__weak_reference(__p_query, p_query);
310#undef res_mkquery
311__weak_reference(__res_mkquery, res_mkquery);
312#undef res_query
313__weak_reference(__res_query, res_query);
314#undef res_send
315__weak_reference(__res_send, res_send);
316#undef res_close
317__weak_reference(__res_close, _res_close);
318#undef res_search
319__weak_reference(__res_search, res_search);
320#undef res_querydomain
321__weak_reference(__res_querydomain, res_querydomain);
322
323#endif
324
325/*! \file */
Note: See TracBrowser for help on using the repository browser.