1/*	$NetBSD$	*/
2
3/*
4 * Copyright (C) 2004, 2005, 2007  Internet Systems Consortium, Inc. ("ISC")
5 * Copyright (C) 2000, 2001  Internet Software Consortium.
6 *
7 * Permission to use, copy, modify, and/or 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 WITH
12 * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
13 * AND FITNESS.  IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
14 * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
15 * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
16 * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
17 * PERFORMANCE OF THIS SOFTWARE.
18 */
19
20/* Id: lwderror.c,v 1.12 2007/06/19 23:46:59 tbox Exp  */
21
22/*! \file */
23
24#include <config.h>
25
26#include <isc/socket.h>
27#include <isc/util.h>
28
29#include <named/types.h>
30#include <named/lwdclient.h>
31
32/*%
33 * Generate an error packet for the client, schedule a send, and put us in
34 * the SEND state.
35 *
36 * The client->pkt structure will be modified to form an error return.
37 * The receiver needs to verify that it is in fact an error, and do the
38 * right thing with it.  The opcode will be unchanged.  The result needs
39 * to be set before calling this function.
40 *
41 * The only change this code makes is to set the receive buffer size to the
42 * size we use, set the reply bit, and recompute any security information.
43 */
44void
45ns_lwdclient_errorpktsend(ns_lwdclient_t *client, isc_uint32_t _result) {
46	isc_result_t result;
47	int lwres;
48	isc_region_t r;
49	lwres_buffer_t b;
50
51	REQUIRE(NS_LWDCLIENT_ISRUNNING(client));
52
53	/*
54	 * Since we are only sending the packet header, we can safely toss
55	 * the receive buffer.  This means we won't need to allocate space
56	 * for sending an error reply.  This is a Good Thing.
57	 */
58	client->pkt.length = LWRES_LWPACKET_LENGTH;
59	client->pkt.pktflags |= LWRES_LWPACKETFLAG_RESPONSE;
60	client->pkt.recvlength = LWRES_RECVLENGTH;
61	client->pkt.authtype = 0; /* XXXMLG */
62	client->pkt.authlength = 0;
63	client->pkt.result = _result;
64
65	lwres_buffer_init(&b, client->buffer, LWRES_RECVLENGTH);
66	lwres = lwres_lwpacket_renderheader(&b, &client->pkt);
67	if (lwres != LWRES_R_SUCCESS) {
68		ns_lwdclient_stateidle(client);
69		return;
70	}
71
72	r.base = client->buffer;
73	r.length = b.used;
74	client->sendbuf = client->buffer;
75	result = ns_lwdclient_sendreply(client, &r);
76	if (result != ISC_R_SUCCESS) {
77		ns_lwdclient_stateidle(client);
78		return;
79	}
80
81	NS_LWDCLIENT_SETSEND(client);
82}
83