sync
[bitrig.git] / lib / libc / asr / res_mkquery.c
blobffd7a34a411525fb21b13d150771dc52d36229e4
1 /* $OpenBSD: res_mkquery.c,v 1.5 2013/04/01 20:22:27 eric Exp $ */
2 /*
3 * Copyright (c) 2012 Eric Faurot <eric@openbsd.org>
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.
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.
18 #include <sys/types.h>
19 #include <netinet/in.h>
20 #include <arpa/nameser.h> /* for MAXDNAME */
22 #include <errno.h>
23 #include <resolv.h>
24 #include <string.h>
26 #include "asr.h"
27 #include "asr_private.h"
29 /* This function is apparently needed by some ports. */
30 int
31 res_mkquery(int op, const char *dname, int class, int type,
32 const unsigned char *data, int datalen, const unsigned char *newrr,
33 unsigned char *buf, int buflen)
35 struct asr_ctx *ac;
36 struct pack p;
37 struct header h;
38 char fqdn[MAXDNAME];
39 char dn[MAXDNAME];
41 /* we currently only support QUERY */
42 if (op != QUERY || data)
43 return (-1);
45 if (dname[0] == '\0' || dname[strlen(dname) - 1] != '.') {
46 if (strlcpy(fqdn, dname, sizeof(fqdn)) >= sizeof(fqdn) ||
47 strlcat(fqdn, ".", sizeof(fqdn)) >= sizeof(fqdn))
48 return (-1);
49 dname = fqdn;
52 if (dname_from_fqdn(dname, dn, sizeof(dn)) == -1)
53 return (-1);
55 ac = asr_use_resolver(NULL);
57 memset(&h, 0, sizeof h);
58 h.id = res_randomid();
59 if (ac->ac_options & RES_RECURSE)
60 h.flags |= RD_MASK;
61 h.qdcount = 1;
63 pack_init(&p, buf, buflen);
64 pack_header(&p, &h);
65 pack_query(&p, type, class, dn);
67 asr_ctx_unref(ac);
69 if (p.err)
70 return (-1);
72 return (p.offset);
76 * This function is not documented, but used by sendmail.
77 * Put here because it uses asr_private.h too.
79 int
80 res_querydomain(const char *name,
81 const char *domain,
82 int class,
83 int type,
84 u_char *answer,
85 int anslen)
87 char fqdn[MAXDNAME], ndom[MAXDNAME];
88 size_t n;
90 /* we really want domain to end with a dot for now */
91 if (domain && ((n = strlen(domain)) == 0 || domain[n - 1 ] != '.')) {
92 if (strlcpy(ndom, domain, sizeof(ndom)) >= sizeof(ndom) ||
93 strlcat(ndom, ".", sizeof(ndom)) >= sizeof(ndom)) {
94 h_errno = NETDB_INTERNAL;
95 errno = EINVAL;
96 return (-1);
98 domain = ndom;
101 if (asr_make_fqdn(name, domain, fqdn, sizeof fqdn) == 0) {
102 h_errno = NETDB_INTERNAL;
103 errno = EINVAL;
104 return (-1);
107 return (res_query(fqdn, class, type, answer, anslen));