res_mkquery.c revision 1.1
1/* $OpenBSD: res_mkquery.c,v 1.1 2012/09/08 11:08:21 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 <netinet/in.h> 20 21#include <arpa/nameser.h> /* for MAXDNAME */ 22#include <errno.h> 23#include <resolv.h> 24#include <string.h> 25 26#include "asr.h" 27#include "asr_private.h" 28 29/* This function is apparently needed by some ports. */ 30int 31res_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) 34{ 35 struct asr_ctx *ac; 36 struct packed p; 37 struct header h; 38 char fqdn[MAXDNAME]; 39 char dn[MAXDNAME]; 40 41 /* we currently only support QUERY */ 42 if (op != QUERY || data) 43 return (-1); 44 45 if (dname[0] == '\0' || dname[strlen(dname) - 1] != '.') { 46 strlcpy(fqdn, dname, sizeof fqdn); 47 if (strlcat(fqdn, ".", sizeof fqdn) >= sizeof fqdn) 48 return (-1); 49 dname = fqdn; 50 } 51 52 if (dname_from_fqdn(dname, dn, sizeof(dn)) == -1) 53 return (-1); 54 55 ac = asr_use_resolver(NULL); 56 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; 62 63 packed_init(&p, buf, buflen); 64 pack_header(&p, &h); 65 pack_query(&p, type, class, dn); 66 67 asr_ctx_unref(ac); 68 69 if (p.err) 70 return (-1); 71 72 return (p.offset); 73} 74 75/* 76 * This function is not documented, but used by sendmail. 77 * Put here because it uses asr_private.h too. 78 */ 79int 80res_querydomain(const char *name, 81 const char *domain, 82 int class, 83 int type, 84 u_char *answer, 85 int anslen) 86{ 87 char fqdn[MAXDNAME], ndom[MAXDNAME]; 88 size_t n; 89 90 /* we really want domain to end with a dot for now */ 91 if (domain && ((n = strlen(domain)) == 0 || domain[n - 1 ] != '.')) { 92 domain = ndom; 93 strlcpy(ndom, domain, sizeof ndom); 94 strlcat(ndom, ".", sizeof ndom); 95 } 96 97 if (asr_make_fqdn(name, domain, fqdn, sizeof fqdn) == 0) { 98 h_errno = NO_RECOVERY; 99 errno = EINVAL; 100 return (-1); 101 } 102 103 return (res_query(fqdn, class, type, answer, anslen)); 104} 105