2N/A/*
2N/A * Copyright 2009 Sun Microsystems, Inc. All rights reserved.
2N/A * Use is subject to license terms.
2N/A */
2N/A
2N/A
2N/A/*
2N/A * Copyright (C) 2009 Internet Systems Consortium, Inc. ("ISC")
2N/A *
2N/A * Permission to use, copy, modify, and/or distribute this software for any
2N/A * purpose with or without fee is hereby granted, provided that the above
2N/A * copyright notice and this permission notice appear in all copies.
2N/A *
2N/A * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
2N/A * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
2N/A * AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
2N/A * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
2N/A * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
2N/A * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
2N/A * PERFORMANCE OF THIS SOFTWARE.
2N/A */
2N/A
2N/A#ifndef lint
2N/Astatic const char rcsid[] = "$Id: ns_newmsg.c,v 1.3 2009/02/26 10:48:57 marka Exp $";
2N/A#endif
2N/A
2N/A#include <port_before.h>
2N/A
2N/A#include <arpa/nameser.h>
2N/A
2N/A#include <assert.h>
2N/A#include <errno.h>
2N/A#include <string.h>
2N/A
2N/A#include <port_after.h>
2N/A
2N/Astatic int rdcpy(ns_newmsg *, ns_type, const u_char *, size_t);
2N/A
2N/A/* Initialize a "newmsg" object to empty.
2N/A */
2N/Aint
2N/Ans_newmsg_init(u_char *buffer, size_t bufsiz, ns_newmsg *handle) {
2N/A ns_msg *msg = &handle->msg;
2N/A
2N/A memset(handle, 0, sizeof *handle);
2N/A msg->_msg = buffer;
2N/A msg->_eom = buffer + bufsiz;
2N/A msg->_sect = ns_s_qd;
2N/A msg->_rrnum = 0;
2N/A msg->_msg_ptr = buffer + NS_HFIXEDSZ;
2N/A handle->dnptrs[0] = msg->_msg;
2N/A handle->dnptrs[1] = NULL;
2N/A handle->lastdnptr = &handle->dnptrs[sizeof handle->dnptrs /
2N/A sizeof handle->dnptrs[0] - 1];
2N/A return (0);
2N/A}
2N/A
2N/A/* Initialize a "newmsg" object by copying an existing parsed message.
2N/A */
2N/Aint
2N/Ans_newmsg_copy(ns_newmsg *handle, ns_msg *msg) {
2N/A ns_flag flag;
2N/A ns_sect sect;
2N/A
2N/A ns_newmsg_id(handle, ns_msg_id(*msg));
2N/A for (flag = ns_f_qr; flag < ns_f_max; flag++)
2N/A ns_newmsg_flag(handle, flag, ns_msg_getflag(*msg, flag));
2N/A for (sect = ns_s_qd; sect < ns_s_max; sect++) {
2N/A int i, count;
2N/A
2N/A count = ns_msg_count(*msg, sect);
2N/A for (i = 0; i < count; i++) {
2N/A ns_rr2 rr;
2N/A int x;
2N/A
2N/A if (ns_parserr2(msg, sect, i, &rr) < 0)
2N/A return (-1);
2N/A if (sect == ns_s_qd)
2N/A x = ns_newmsg_q(handle,
2N/A ns_rr_nname(rr),
2N/A ns_rr_type(rr),
2N/A ns_rr_class(rr));
2N/A else
2N/A x = ns_newmsg_rr(handle, sect,
2N/A ns_rr_nname(rr),
2N/A ns_rr_type(rr),
2N/A ns_rr_class(rr),
2N/A ns_rr_ttl(rr),
2N/A ns_rr_rdlen(rr),
2N/A ns_rr_rdata(rr));
2N/A if (x < 0)
2N/A return (-1);
2N/A }
2N/A }
2N/A return (0);
2N/A}
2N/A
2N/A/* Set the message-ID in a "newmsg" object.
2N/A */
2N/Avoid
2N/Ans_newmsg_id(ns_newmsg *handle, u_int16_t id) {
2N/A ns_msg *msg = &handle->msg;
2N/A
2N/A msg->_id = id;
2N/A}
2N/A
2N/A/* Set a flag (including rcode or opcode) in a "newmsg" object.
2N/A */
2N/Avoid
2N/Ans_newmsg_flag(ns_newmsg *handle, ns_flag flag, u_int value) {
2N/A extern struct _ns_flagdata _ns_flagdata[16];
2N/A struct _ns_flagdata *fd = &_ns_flagdata[flag];
2N/A ns_msg *msg = &handle->msg;
2N/A
2N/A assert(flag < ns_f_max);
2N/A msg->_flags &= (~fd->mask);
2N/A msg->_flags |= (value << fd->shift);
2N/A}
2N/A
2N/A/* Add a question (or zone, if it's an update) to a "newmsg" object.
2N/A */
2N/Aint
2N/Ans_newmsg_q(ns_newmsg *handle, ns_nname_ct qname,
2N/A ns_type qtype, ns_class qclass)
2N/A{
2N/A ns_msg *msg = &handle->msg;
2N/A u_char *t;
2N/A int n;
2N/A
2N/A if (msg->_sect != ns_s_qd) {
2N/A errno = ENODEV;
2N/A return (-1);
2N/A }
2N/A t = (u_char *) (unsigned long) msg->_msg_ptr;
2N/A if (msg->_rrnum == 0)
2N/A msg->_sections[ns_s_qd] = t;
2N/A n = ns_name_pack(qname, t, msg->_eom - t,
2N/A handle->dnptrs, handle->lastdnptr);
2N/A if (n < 0)
2N/A return (-1);
2N/A t += n;
2N/A if (t + QFIXEDSZ >= msg->_eom) {
2N/A errno = EMSGSIZE;
2N/A return (-1);
2N/A }
2N/A NS_PUT16(qtype, t);
2N/A NS_PUT16(qclass, t);
2N/A msg->_msg_ptr = t;
2N/A msg->_counts[ns_s_qd] = ++msg->_rrnum;
2N/A return (0);
2N/A}
2N/A
2N/A/* Add an RR to a "newmsg" object.
2N/A */
2N/Aint
2N/Ans_newmsg_rr(ns_newmsg *handle, ns_sect sect,
2N/A ns_nname_ct name, ns_type type,
2N/A ns_class rr_class, u_int32_t ttl,
2N/A u_int16_t rdlen, const u_char *rdata)
2N/A{
2N/A ns_msg *msg = &handle->msg;
2N/A u_char *t;
2N/A int n;
2N/A
2N/A if (sect < msg->_sect) {
2N/A errno = ENODEV;
2N/A return (-1);
2N/A }
2N/A t = (u_char *) (unsigned long) msg->_msg_ptr;
2N/A if (sect > msg->_sect) {
2N/A msg->_sect = sect;
2N/A msg->_sections[sect] = t;
2N/A msg->_rrnum = 0;
2N/A }
2N/A n = ns_name_pack(name, t, msg->_eom - t,
2N/A handle->dnptrs, handle->lastdnptr);
2N/A if (n < 0)
2N/A return (-1);
2N/A t += n;
2N/A if (t + RRFIXEDSZ + rdlen >= msg->_eom) {
2N/A errno = EMSGSIZE;
2N/A return (-1);
2N/A }
2N/A NS_PUT16(type, t);
2N/A NS_PUT16(rr_class, t);
2N/A NS_PUT32(ttl, t);
2N/A msg->_msg_ptr = t;
2N/A if (rdcpy(handle, type, rdata, rdlen) < 0)
2N/A return (-1);
2N/A msg->_counts[sect] = ++msg->_rrnum;
2N/A return (0);
2N/A}
2N/A
2N/A/* Complete a "newmsg" object and return its size for use in write().
2N/A * (Note: the "newmsg" object is also made ready for ns_parserr() etc.)
2N/A */
2N/Asize_t
2N/Ans_newmsg_done(ns_newmsg *handle) {
2N/A ns_msg *msg = &handle->msg;
2N/A ns_sect sect;
2N/A u_char *t;
2N/A
2N/A t = (u_char *) (unsigned long) msg->_msg;
2N/A NS_PUT16(msg->_id, t);
2N/A NS_PUT16(msg->_flags, t);
2N/A for (sect = 0; sect < ns_s_max; sect++)
2N/A NS_PUT16(msg->_counts[sect], t);
2N/A msg->_eom = msg->_msg_ptr;
2N/A msg->_sect = ns_s_max;
2N/A msg->_rrnum = -1;
2N/A msg->_msg_ptr = NULL;
2N/A return (msg->_eom - msg->_msg);
2N/A}
2N/A
2N/A/* Private. */
2N/A
2N/A/* Copy an RDATA, using compression pointers where RFC1035 permits.
2N/A */
2N/Astatic int
2N/Ardcpy(ns_newmsg *handle, ns_type type, const u_char *rdata, size_t rdlen) {
2N/A ns_msg *msg = &handle->msg;
2N/A u_char *p = (u_char *) (unsigned long) msg->_msg_ptr;
2N/A u_char *t = p + NS_INT16SZ;
2N/A u_char *s = t;
2N/A int n;
2N/A
2N/A switch (type) {
2N/A case ns_t_soa:
2N/A /* MNAME. */
2N/A n = ns_name_pack(rdata, t, msg->_eom - t,
2N/A handle->dnptrs, handle->lastdnptr);
2N/A if (n < 0)
2N/A return (-1);
2N/A t += n;
2N/A if (ns_name_skip(&rdata, msg->_eom) < 0)
2N/A return (-1);
2N/A
2N/A /* ANAME. */
2N/A n = ns_name_pack(rdata, t, msg->_eom - t,
2N/A handle->dnptrs, handle->lastdnptr);
2N/A if (n < 0)
2N/A return (-1);
2N/A t += n;
2N/A if (ns_name_skip(&rdata, msg->_eom) < 0)
2N/A return (-1);
2N/A
2N/A /* Serial, Refresh, Retry, Expiry, and Minimum. */
2N/A if ((msg->_eom - t) < (NS_INT32SZ * 5)) {
2N/A errno = EMSGSIZE;
2N/A return (-1);
2N/A }
2N/A memcpy(t, rdata, NS_INT32SZ * 5);
2N/A t += (NS_INT32SZ * 5);
2N/A break;
2N/A case ns_t_ptr:
2N/A case ns_t_cname:
2N/A case ns_t_ns:
2N/A /* PTRDNAME, CNAME, or NSDNAME. */
2N/A n = ns_name_pack(rdata, t, msg->_eom - t,
2N/A handle->dnptrs, handle->lastdnptr);
2N/A if (n < 0)
2N/A return (-1);
2N/A t += n;
2N/A break;
2N/A default:
2N/A memcpy(t, rdata, rdlen);
2N/A t += rdlen;
2N/A }
2N/A NS_PUT16(t - s, p);
2N/A msg->_msg_ptr = t;
2N/A return (0);
2N/A}
2N/A