Commit e3b85192 authored by Marek Vavrusa's avatar Marek Vavrusa

Ported server to new pkt API.

parent 3cdcaca4
......@@ -21,14 +21,12 @@
#include "libknot/dname.h"
#include "common/descriptor.h"
#include "libknot/packet/packet.h"
#include "libknot/packet/pkt.h"
#include "libknot/rrset.h"
#include "libknot/packet/response.h"
#include "libknot/packet/query.h"
#include "libknot/consts.h"
#include "libknot/zone/zonedb.h"
#include "libknot/common.h"
#include "libknot/util/wire.h"
#include "libknot/packet/wire.h"
#include "knot/server/zones.h"
#include "common/acl.h"
#include "common/evsched.h"
......@@ -48,112 +46,41 @@
static int notify_request(const knot_rrset_t *rrset,
uint8_t *buffer, size_t *size)
{
knot_packet_t *pkt = knot_packet_new();
knot_pkt_t *pkt = knot_pkt_new(buffer, *size, NULL);
CHECK_ALLOC_LOG(pkt, KNOT_ENOMEM);
/*! \todo Get rid of the numeric constant. */
int rc = knot_packet_set_max_size(pkt, 512);
if (rc != KNOT_EOK) {
knot_packet_free(&pkt);
return KNOT_ERROR;
}
rc = knot_query_init(pkt);
if (rc != KNOT_EOK) {
knot_packet_free(&pkt);
return KNOT_ERROR;
}
rc = knot_query_set_question(pkt, rrset->owner, rrset->rclass, rrset->type);
if (rc != KNOT_EOK) {
knot_packet_free(&pkt);
return KNOT_ERROR;
}
knot_pkt_clear(pkt);
knot_wire_set_id(pkt->wire, knot_random_id());
knot_wire_set_aa(pkt->wire);
knot_wire_set_opcode(pkt->wire, KNOT_OPCODE_NOTIFY);
/* Set random query ID. */
knot_packet_set_random_id(pkt);
/*! \todo add the SOA RR to the Answer section as a hint */
/*! \todo this should not use response API!! */
// rc = knot_response_add_rrset_answer(pkt, rrset, 0, 0, 0);
// if (rc != KNOT_EOK) {
// knot_packet_free(&pkt);
// return rc;
// }
/*! \todo this should not use response API!! */
knot_response_set_aa(pkt);
knot_query_set_opcode(pkt, KNOT_OPCODE_NOTIFY);
/*! \todo OPT RR ?? */
uint8_t *wire = NULL;
size_t wire_size = 0;
rc = knot_packet_to_wire(pkt, &wire, &wire_size);
if (rc != KNOT_EOK) {
knot_packet_free(&pkt);
int ret = knot_pkt_put_question(pkt, rrset->owner, rrset->rclass, rrset->type);
if (ret != KNOT_EOK) {
knot_pkt_free(&pkt);
return KNOT_ERROR;
}
if (wire_size > *size) {
knot_packet_free(&pkt);
return KNOT_ESPACE;
}
memcpy(buffer, wire, wire_size);
*size = wire_size;
knot_packet_dump(pkt);
knot_packet_free(&pkt);
/* Write back size, #10 crappy API. */
*size = pkt->size;
knot_pkt_free(&pkt);
return KNOT_EOK;
}
/*----------------------------------------------------------------------------*/
int notify_create_response(knot_packet_t *request, uint8_t *buffer,
size_t *size)
int notify_create_response(knot_pkt_t *request, uint8_t *buffer, size_t *size)
{
knot_packet_t *response = knot_packet_new_mm(&request->mm);
knot_pkt_t *response = knot_pkt_new(buffer, *size, NULL);
CHECK_ALLOC_LOG(response, KNOT_ENOMEM);
/* Set maximum packet size. */
int rc = knot_packet_set_max_size(response, *size);
if (rc == KNOT_EOK) {
rc = knot_response_init_from_query(response, request);
}
/* Aggregated result check. */
if (rc != KNOT_EOK) {
dbg_notify("%s: failed to init response packet: %s",
"notify_create_response", knot_strerror(rc));
knot_packet_free(&response);
return KNOT_EINVAL;
}
// TODO: copy the SOA in Answer section
uint8_t *wire = NULL;
size_t wire_size = 0;
rc = knot_packet_to_wire(response, &wire, &wire_size);
if (rc != KNOT_EOK) {
knot_packet_free(&response);
return rc;
}
if (wire_size > *size) {
knot_packet_free(&response);
return KNOT_ESPACE;
}
/* Initialize response. */
int ret = knot_pkt_init_response(response, request);
memcpy(buffer, wire, wire_size);
*size = wire_size;
/* Free the packet, buffer is already written. */
knot_pkt_free(&response);
knot_packet_dump(response);
knot_packet_free(&response);
return KNOT_EOK;
return ret;
}
/*----------------------------------------------------------------------------*/
......@@ -208,7 +135,7 @@ static int notify_check_and_schedule(knot_nameserver_t *nameserver,
/*----------------------------------------------------------------------------*/
int notify_process_request(knot_nameserver_t *ns,
knot_packet_t *notify,
knot_pkt_t *notify,
sockaddr_t *from,
uint8_t *buffer, size_t *size)
{
......@@ -227,7 +154,7 @@ int notify_process_request(knot_nameserver_t *ns,
dbg_notify("notify: parsing rest of the packet\n");
if (notify->parsed < notify->size) {
if (knot_packet_parse_rest(notify, 0) != KNOT_EOK) {
if (knot_pkt_parse_payload(notify, 0) != KNOT_EOK) {
dbg_notify("notify: failed to parse NOTIFY query\n");
knot_ns_error_response_from_query(ns, notify,
KNOT_RCODE_FORMERR,
......@@ -237,7 +164,7 @@ int notify_process_request(knot_nameserver_t *ns,
}
// check if it makes sense - if the QTYPE is SOA
if (knot_packet_qtype(notify) != KNOT_RRTYPE_SOA) {
if (knot_pkt_qtype(notify) != KNOT_RRTYPE_SOA) {
// send back FORMERR
knot_ns_error_response_from_query(ns, notify,
KNOT_RCODE_FORMERR, buffer,
......@@ -259,13 +186,14 @@ int notify_process_request(knot_nameserver_t *ns,
/* Process notification. */
ret = KNOT_ENOZONE;
unsigned serial = 0;
const knot_dname_t *qname = knot_packet_qname(notify);
const knot_dname_t *qname = knot_pkt_qname(notify);
rcu_read_lock(); /* z */
const knot_zone_t *z = knot_zonedb_find_zone_for_name(ns->zone_db, qname);
const knot_pktsection_t *answer = knot_pkt_section(notify, KNOT_ANSWER);
if (z != NULL) {
ret = notify_check_and_schedule(ns, z, from);
const knot_rrset_t *soa_rr = NULL;
soa_rr = knot_packet_answer_rrset(notify, 0);
soa_rr = answer->rr[0];
if (soa_rr && knot_rrset_type(soa_rr) == KNOT_RRTYPE_SOA) {
serial = knot_rdata_soa_serial(soa_rr);
}
......@@ -298,15 +226,14 @@ int notify_process_request(knot_nameserver_t *ns,
/*----------------------------------------------------------------------------*/
int notify_process_response(knot_packet_t *notify, int msgid)
int notify_process_response(knot_pkt_t *notify, int msgid)
{
if (!notify) {
return KNOT_EINVAL;
}
/* Match ID against awaited. */
uint16_t pkt_id = knot_packet_id(notify);
if (pkt_id != msgid) {
if (knot_wire_get_id(notify->wire) != msgid) {
return KNOT_ERROR;
}
......
......@@ -31,7 +31,7 @@
#include <string.h>
#include "libknot/zone/zone.h"
#include "libknot/packet/packet.h"
#include "libknot/packet/pkt.h"
#include "libknot/zone/zonedb.h"
#include "common/lists.h"
#include "common/sockaddr.h"
......@@ -88,7 +88,7 @@ int notify_create_request(const knot_zone_contents_t *zone, uint8_t *buffer,
* \retval KNOT_ERROR
*/
int notify_process_request(knot_nameserver_t *nameserver,
knot_packet_t *notify,
knot_pkt_t *notify,
sockaddr_t *from,
uint8_t *buffer, size_t *size);
......@@ -102,7 +102,7 @@ int notify_process_request(knot_nameserver_t *nameserver,
* \retval KNOT_EINVAL on invalid parameters or packet.
* \retval KNOT_ERROR on message ID mismatch
*/
int notify_process_response(knot_packet_t *notify, int msgid);
int notify_process_response(knot_pkt_t *notify, int msgid);
#endif /* _KNOTD_NOTIFY_H_ */
......
......@@ -24,7 +24,7 @@
#include "knot/server/rrl.h"
#include "knot/knot.h"
#include "libknot/consts.h"
#include "libknot/util/wire.h"
#include "libknot/packet/wire.h"
#include "common/hattrie/murmurhash3.h"
#include "common/prng.h"
#include "common/descriptor.h"
......@@ -100,7 +100,7 @@ static uint8_t rrl_clsid(rrl_req_t *p)
/* Check query type for spec. classes. */
if (p->query) {
switch(knot_packet_qtype(p->query)) {
switch(knot_pkt_qtype(p->query)) {
case KNOT_RRTYPE_ANY: /* ANY spec. class */
return CLS_ANY;
break;
......@@ -146,7 +146,7 @@ static int rrl_clsname(char *dst, size_t maxlen, uint8_t cls,
default:
/* Use QNAME */
if (req->query)
dn = knot_packet_qname(req->query);
dn = knot_pkt_qname(req->query);
break;
}
......
......@@ -30,7 +30,7 @@
#include <stdint.h>
#include <pthread.h>
#include "common/sockaddr.h"
#include "libknot/packet/packet.h"
#include "libknot/packet/pkt.h"
#include "libknot/zone/zone.h"
/* Defaults */
......@@ -78,7 +78,7 @@ typedef struct rrl_req {
const uint8_t *w;
uint16_t len;
unsigned flags;
knot_packet_t *query;
knot_pkt_t *query;
} rrl_req_t;
/*!
......
......@@ -39,7 +39,7 @@
#include "knot/server/xfr-handler.h"
#include "knot/server/zones.h"
#include "libknot/nameserver/name-server.h"
#include "libknot/util/wire.h"
#include "libknot/packet/wire.h"
#include "libknot/dnssec/cleanup.h"
/*! \brief TCP worker data. */
......@@ -175,7 +175,7 @@ static int tcp_handle(tcp_worker_t *w, int fd, uint8_t *buf[], size_t qbuf_maxle
/* Parse query. */
size_t resp_len = qbuf_maxlen; // 64K
knot_packet_type_t qtype = KNOT_QUERY_NORMAL;
knot_packet_t *packet = knot_packet_new();
knot_pkt_t *packet = knot_pkt_new(buf[QBUF], n, NULL);
if (packet == NULL) {
int ret = knot_ns_error_response_from_query_wire(ns, buf[QBUF], n,
KNOT_RCODE_SERVFAIL,
......@@ -188,7 +188,7 @@ static int tcp_handle(tcp_worker_t *w, int fd, uint8_t *buf[], size_t qbuf_maxle
return KNOT_EOK;
}
int parse_res = knot_ns_parse_packet(buf[QBUF], n, packet, &qtype);
int parse_res = knot_ns_parse_packet(packet, &qtype);
if (knot_unlikely(parse_res != KNOT_EOK)) {
if (parse_res > 0) { /* Returned RCODE */
int ret = knot_ns_error_response_from_query(ns, packet,
......@@ -198,7 +198,7 @@ static int tcp_handle(tcp_worker_t *w, int fd, uint8_t *buf[], size_t qbuf_maxle
tcp_reply(fd, buf[QRBUF], resp_len);
}
}
knot_packet_free(&packet);
knot_pkt_free(&packet);
return KNOT_EOK;
}
......@@ -240,7 +240,7 @@ static int tcp_handle(tcp_worker_t *w, int fd, uint8_t *buf[], size_t qbuf_maxle
xfr->query = packet;
xfr_task_setaddr(xfr, &addr, NULL);
res = xfr_answer(ns, xfr);
knot_packet_free(&packet);
knot_pkt_free(&packet);
return res;
case KNOT_QUERY_UPDATE:
......@@ -281,7 +281,7 @@ static int tcp_handle(tcp_worker_t *w, int fd, uint8_t *buf[], size_t qbuf_maxle
qtype, fd, knot_strerror(res));;
}
knot_packet_free(&packet);
knot_pkt_free(&packet);
return res;
}
......
......@@ -35,7 +35,7 @@
#include "knot/server/udp-handler.h"
#include "knot/server/tcp-handler.h"
#include "libknot/updates/xfr-in.h"
#include "libknot/util/wire.h"
#include "libknot/packet/wire.h"
#include "knot/server/zones.h"
#include "libknot/tsig-op.h"
#include "common/evsched.h"
......@@ -297,7 +297,7 @@ static void xfr_task_cleanup(knot_ns_xfr_t *rq)
rq->data = NULL;
assert(rq->new_contents == NULL);
} else if (rq->type == XFR_TYPE_FORWARD) {
knot_packet_free(&rq->query);
knot_pkt_free(&rq->query);
}
/* Cleanup other data - so that the structure may be reused. */
......@@ -397,13 +397,13 @@ static int xfr_task_start(knot_ns_xfr_t *rq)
/* Create XFR query. */
switch(rq->type) {
case XFR_TYPE_AIN:
ret = xfrin_create_axfr_query(zone->name, rq, &rq->wire_size, add_tsig);
ret = xfrin_create_axfr_query(zone->name, rq);
break;
case XFR_TYPE_IIN:
ret = xfrin_create_ixfr_query(contents, rq, &rq->wire_size, add_tsig);
ret = xfrin_create_ixfr_query(contents, rq);
break;
case XFR_TYPE_SOA:
ret = xfrin_create_soa_query(zone->name, rq, &rq->wire_size);
ret = xfrin_create_soa_query(zone->name, rq);
break;
case XFR_TYPE_NOTIFY:
rq->wire_size = 0;
......@@ -624,15 +624,15 @@ static int xfr_task_finalize(xfrworker_t *w, knot_ns_xfr_t *rq)
static int xfr_task_resp(xfrworker_t *w, knot_ns_xfr_t *rq)
{
knot_nameserver_t *ns = w->master->ns;
knot_packet_t *re = knot_packet_new();
knot_pkt_t *re = knot_pkt_new(rq->wire, rq->wire_size, NULL);
if (re == NULL) {
return KNOT_ENOMEM;
}
knot_packet_type_t rt = KNOT_RESPONSE_NORMAL;
int ret = knot_ns_parse_packet(rq->wire, rq->wire_size, re, &rt);
int ret = knot_ns_parse_packet(re, &rt);
if (ret != KNOT_EOK) {
knot_packet_free(&re);
knot_pkt_free(&re);
return KNOT_EOK; /* Ignore */
}
......@@ -643,18 +643,18 @@ static int xfr_task_resp(xfrworker_t *w, knot_ns_xfr_t *rq)
case KNOT_RESPONSE_UPDATE:
break;
default:
knot_packet_free(&re);
knot_pkt_free(&re);
return KNOT_EOK; /* Ignore */
}
ret = knot_packet_parse_rest(re, 0);
ret = knot_pkt_parse_payload(re, 0);
if (ret != KNOT_EOK) {
knot_packet_free(&re);
knot_pkt_free(&re);
return KNOT_EOK; /* Ignore */
}
/* Check TSIG. */
const knot_rrset_t * tsig_rr = knot_packet_tsig(re);
const knot_rrset_t * tsig_rr = re->tsig_rr;
if (rq->tsig_key != NULL) {
/*! \todo Not sure about prev_time_signed, but this is the first
* reply and we should pass query sign time as the time
......@@ -665,7 +665,7 @@ static int xfr_task_resp(xfrworker_t *w, knot_ns_xfr_t *rq)
rq->tsig_key, 0);
if (ret != KNOT_EOK) {
log_server_error("%s %s\n", rq->msg, knot_strerror(ret));
knot_packet_free(&re);
knot_pkt_free(&re);
return KNOT_ECONNREFUSED;
}
......@@ -692,7 +692,7 @@ static int xfr_task_resp(xfrworker_t *w, knot_ns_xfr_t *rq)
break;
}
knot_packet_free(&re);
knot_pkt_free(&re);
if (ret == KNOT_EUPTODATE) { /* Check up-to-date zone. */
log_server_info("%s %s (serial %u)\n", rq->msg,
knot_strerror(ret),
......@@ -733,7 +733,7 @@ static int xfr_fallback_axfr(knot_ns_xfr_t *rq)
{
log_server_notice("%s Retrying with AXFR.\n", rq->msg);
rq->wire_size = rq->wire_maxlen; /* Reset maximum bufsize */
int ret = xfrin_create_axfr_query(rq->zone->name, rq, &rq->wire_size, 1);
int ret = xfrin_create_axfr_query(rq->zone->name, rq);
/* Send AXFR/IN query. */
if (ret == KNOT_EOK) {
ret = rq->send(rq->session, &rq->addr,
......@@ -894,37 +894,24 @@ static int xfr_check_tsig(knot_ns_xfr_t *xfr, knot_rcode_t *rcode, char **tag)
{
/* Parse rest of the packet. */
int ret = KNOT_EOK;
knot_packet_t *qry = xfr->query;
knot_pkt_t *qry = xfr->query;
knot_tsig_key_t *key = 0;
const knot_rrset_t *tsig_rr = 0;
const knot_rrset_t *tsig_rr = qry->tsig_rr;
/* Find TSIG key name from query. */
const knot_dname_t* kname = 0;
int tsig_pos = knot_packet_additional_rrset_count(qry) - 1;
if (tsig_pos >= 0) {
tsig_rr = knot_packet_additional_rrset(qry, tsig_pos);
if (knot_rrset_type(tsig_rr) == KNOT_RRTYPE_TSIG) {
dbg_xfr("xfr: found TSIG in AR\n");
kname = knot_rrset_owner(tsig_rr);
if (tag) {
*tag = knot_dname_to_str(kname);
}
} else {
tsig_rr = 0;
const knot_dname_t* kname = NULL;
if (tsig_rr) {
kname = knot_rrset_owner(tsig_rr);
if (tag) {
*tag = knot_dname_to_str(kname);
}
}
if (!kname) {
dbg_xfr("xfr: TSIG not found in AR\n");
char *name = knot_dname_to_str(
knot_zone_name(xfr->zone));
free(name);
} else {
// return REFUSED
xfr->tsig_key = 0;
xfr->tsig_key = NULL;
*rcode = KNOT_RCODE_REFUSED;
return KNOT_EDENIED;
}
if (tsig_rr) {
knot_tsig_algorithm_t alg = tsig_rdata_alg(tsig_rr);
if (knot_tsig_digest_length(alg) == 0) {
......@@ -975,8 +962,8 @@ static int xfr_check_tsig(knot_ns_xfr_t *xfr, knot_rcode_t *rcode, char **tag)
/* Check query TSIG. */
ret = knot_tsig_server_check(
tsig_rr,
knot_packet_wireformat(qry),
knot_packet_size(qry),
qry->wire,
qry->size,
key);
dbg_xfr("knot_tsig_server_check() returned %s\n",
knot_strerror(ret));
......@@ -1280,7 +1267,7 @@ int xfr_answer(knot_nameserver_t *ns, knot_ns_xfr_t *rq)
rcu_read_unlock(); /* Now, the zone is either refcounted or NULL. */
/* Use the QNAME as the zone name. */
const knot_dname_t *qname = knot_packet_qname(rq->query);
const knot_dname_t *qname = knot_pkt_qname(rq->query);
if (qname != NULL) {
rq->zname = knot_dname_to_str(qname);
} else {
......@@ -1359,7 +1346,7 @@ int xfr_answer(knot_nameserver_t *ns, knot_ns_xfr_t *rq)
}
/* Cleanup. */
knot_packet_free(&rq->response); /* Free response. */
knot_pkt_free(&rq->response); /* Free response. */
knot_changesets_free((knot_changesets_t **)(&rq->data));
free(rq->zname);
......
This diff is collapsed.
......@@ -126,7 +126,7 @@ int zones_xfr_check_zone(knot_ns_xfr_t *xfr, knot_rcode_t *rcode);
* \todo Document me.
*/
int zones_normal_query_answer(knot_nameserver_t *nameserver,
knot_packet_t *query, const sockaddr_t *addr,
knot_pkt_t *query, const sockaddr_t *addr,
uint8_t *response_wire, size_t *rsize,
knot_ns_transport_t transport);
......@@ -134,7 +134,7 @@ int zones_normal_query_answer(knot_nameserver_t *nameserver,
* \todo Document me.
*/
int zones_process_update(knot_nameserver_t *nameserver,
knot_packet_t *query, const sockaddr_t *addr,
knot_pkt_t *query, const sockaddr_t *addr,
uint8_t *resp_wire, size_t *rsize,
int fd, knot_ns_transport_t transport);
......@@ -155,7 +155,7 @@ int zones_process_update(knot_nameserver_t *nameserver,
int zones_process_response(knot_nameserver_t *nameserver,
int exp_msgid,
sockaddr_t *from,
knot_packet_t *packet, uint8_t *response_wire,
knot_pkt_t *packet, uint8_t *response_wire,
size_t *rsize);
/*!
......@@ -346,7 +346,7 @@ int zones_process_update_response(knot_ns_xfr_t *data, uint8_t *rwire, size_t *r
*
* \return KNOT_EOK if verified or error if not.
*/
int zones_verify_tsig_query(const knot_packet_t *query,
int zones_verify_tsig_query(const knot_pkt_t *query,
const knot_tsig_key_t *key,
knot_rcode_t *rcode, uint16_t *tsig_rcode,
uint64_t *tsig_prev_time_signed);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment