From 7df5b078831cbdcbc744b38fbfd55af6b79e22a6 Mon Sep 17 00:00:00 2001
From: Marek Vavrusa <marek.vavrusa@nic.cz>
Date: Tue, 9 Aug 2011 14:55:52 +0200
Subject: [PATCH] Fixes initialization of NOTIFY response max_size and debug
 assert.

refs #1103
---
 libknot/packet/packet.c       |  2 +-
 libknot/packet/response.c     |  3 ++-
 src/knot/server/notify.c      | 17 +++++++++++++----
 src/knot/server/udp-handler.c |  2 +-
 4 files changed, 17 insertions(+), 7 deletions(-)

diff --git a/libknot/packet/packet.c b/libknot/packet/packet.c
index 93a4485d68..0f8b01fb8c 100644
--- a/libknot/packet/packet.c
+++ b/libknot/packet/packet.c
@@ -1276,7 +1276,7 @@ void knot_packet_free(knot_packet_t **packet)
 static void knot_packet_dump_rrsets(const knot_rrset_t **rrsets,
                                       int count)
 {
-	assert(rrsets != NULL && *rrsets != NULL);
+	assert((rrsets != NULL && *rrsets != NULL) || count < 1);
 
 	for (int i = 0; i < count; ++i) {
 		knot_rrset_dump(rrsets[i], 0);
diff --git a/libknot/packet/response.c b/libknot/packet/response.c
index 57f27d452f..81c515fc3c 100644
--- a/libknot/packet/response.c
+++ b/libknot/packet/response.c
@@ -821,6 +821,7 @@ int knot_response_init(knot_packet_t *response)
 int knot_response_init_from_query(knot_packet_t *response,
                                     knot_packet_t *query)
 {
+
 	if (response == NULL || query == NULL) {
 		return KNOT_EBADARG;
 	}
@@ -843,9 +844,9 @@ int knot_response_init_from_query(knot_packet_t *response,
 	// copy the wireformat of Header and Question from the query
 	// TODO: get rid of the numeric constants
 	size_t to_copy = 12 + 4 + knot_dname_size(response->question.qname);
+
 	assert(response->max_size >= to_copy);
 	memcpy(response->wireformat, query->wireformat, to_copy);
-
 	response->size = to_copy;
 
 	// set the qr bit to 1
diff --git a/src/knot/server/notify.c b/src/knot/server/notify.c
index 3f169ad66e..8b7db6f46b 100644
--- a/src/knot/server/notify.c
+++ b/src/knot/server/notify.c
@@ -103,10 +103,11 @@ int notify_create_response(knot_packet_t *request, uint8_t *buffer,
 		knot_packet_new(KNOT_PACKET_PREALLOC_QUERY);
 	CHECK_ALLOC_LOG(response, KNOTD_ENOMEM);
 
+	/* Set maximum packet size. */
+	knot_packet_set_max_size(response, *size);
 	knot_response_init_from_query(response, request);
 
 	// TODO: copy the SOA in Answer section
-
 	uint8_t *wire = NULL;
 	size_t wire_size = 0;
 	int rc = knot_packet_to_wire(response, &wire, &wire_size);
@@ -124,7 +125,6 @@ int notify_create_response(knot_packet_t *request, uint8_t *buffer,
 	*size = wire_size;
 
 	knot_packet_dump(response);
-
 	knot_packet_free(&response);
 
 	return KNOTD_EOK;
@@ -153,6 +153,8 @@ static int notify_check_and_schedule(knot_nameserver_t *nameserver,
                                      sockaddr_t *from)
 {
 	if (zone == NULL || from == NULL || knot_zone_data(zone) == NULL) {
+		debug_notify("notify: invalid parameters for check and "
+			     "schedule\n");
 		return KNOTD_EINVAL;
 	}
 	
@@ -210,34 +212,41 @@ int notify_process_request(knot_nameserver_t *nameserver,
 
 	if (notify == NULL || nameserver == NULL || buffer == NULL 
 	    || size == NULL || from == NULL) {
+		debug_notify("notify: invalid parameters for query\n");
 		return KNOTD_EINVAL;
 	}
 
-	int ret;
+	int ret = KNOTD_EOK;
 
+	debug_notify("notify: parsing rest of the packet\n");
 	if (notify->parsed < notify->size) {
 		ret = knot_packet_parse_rest(notify);
 		if (ret != KNOT_EOK) {
+			debug_notify("notify: failed to parse NOTIFY query\n");
 			return KNOTD_EMALF;
 		}
 	}
 
 	// create NOTIFY response
+	debug_notify("notify: creating response\n");
 	ret = notify_create_response(notify, buffer, size);
 	if (ret != KNOTD_EOK) {
+		debug_notify("notify: failed to create NOTIFY response\n");
 		return KNOTD_ERROR;	/*! \todo Some other error. */
 	}
 
 	// find the zone
+	debug_notify("notify: looking up zone by name\n");
 	const knot_dname_t *qname = knot_packet_qname(notify);
 	const knot_zone_t *z = knot_zonedb_find_zone_for_name(
 			nameserver->zone_db, qname);
 	if (z == NULL) {
+		debug_notify("notify: failed to find zone by name\n");
 		return KNOTD_ERROR;	/*! \todo Some other error. */
 	}
 
 	notify_check_and_schedule(nameserver, z, from);
-	
+
 	return KNOTD_EOK;
 }
 
diff --git a/src/knot/server/udp-handler.c b/src/knot/server/udp-handler.c
index 8e89a4df20..81af4a5d7a 100644
--- a/src/knot/server/udp-handler.c
+++ b/src/knot/server/udp-handler.c
@@ -164,7 +164,7 @@ int udp_master(dthread_t *thread)
 			/*! \todo Send error, not available on UDP. */
 			break;
 		case KNOT_QUERY_NOTIFY:
-			rcu_read_lock();
+//			rcu_read_lock();
 //			const knot_zone_t *zone = NULL;
 //			res = knot_ns_answer_notify(ns, packet, qbuf, 
 //			                              &resp_len, &zone);
-- 
GitLab