/* Copyright 1999-2007 The Apache Software Foundation or its licensors, as
 * applicable.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

/*
 * Original Copyright (c) Netmask.IT!® 2006-2007
 *
 * DNS Protocol module for Apache 2.x
 */

#include "mod_dns.h"

/** We need 4 bucket types: 3 for sending RRs (answer, authority, additional)
 *  which will just be pool buckets that can identify themselves later, and
 *  a 4th error type (we might steal normal apache error bucket for that)
 */

/** TODO: Currently, we ensure that the rr is transported correctly, but don't
 *  consider sub-allocated memory, like rr->name, rr->rdata (and fields inside
 *  rr->rdata).  We might want to serialize the rr in bucket_make and
 *  unserialize it in bucket_read to guarantee it never disappears */

static void dns_rr_bucket_destroy(void *data)
{
    apr_bucket_type_pool.destroy(data);
}

static
/** This should read out rr->name, rr->rdata from some other form... */
apr_status_t dns_rr_bucket_read(apr_bucket *e, const char **str,
                               apr_size_t *len, apr_read_type_e block)
{
    return apr_bucket_type_pool.read(e, str, len, block);
}

/** This needs to be improved to setaside subpools (rr->name, rr->rdata, etc) */
static
apr_status_t dns_rr_bucket_setaside(apr_bucket *data, apr_pool_t *reqpool)
{
    return apr_bucket_type_pool.setaside(data, reqpool);
}

static
apr_status_t dns_rr_bucket_split(apr_bucket *a, apr_size_t point)
{
    return apr_bucket_type_pool.split(a, point);
}

static
apr_status_t dns_rr_bucket_copy(apr_bucket *e, apr_bucket **c)
{
    return apr_bucket_type_pool.copy(e, c);
}

DNS_DECLARE_DATA const apr_bucket_type_t dns_rr_answer_bucket_type = {
    "DNS_RR_ANSWER", 5, APR_BUCKET_DATA,
    dns_rr_bucket_destroy,
    dns_rr_bucket_read,
    dns_rr_bucket_setaside,
    dns_rr_bucket_split,
    dns_rr_bucket_copy,
};

DNS_DECLARE_DATA const apr_bucket_type_t dns_rr_authority_bucket_type = {
    "DNS_RR_AUTHORITY", 5, APR_BUCKET_DATA,
    dns_rr_bucket_destroy,
    dns_rr_bucket_read,
    dns_rr_bucket_setaside,
    dns_rr_bucket_split,
    dns_rr_bucket_copy,
};

DNS_DECLARE_DATA const apr_bucket_type_t dns_rr_additional_bucket_type = {
    "DNS_RR_ADDITIONAL", 5, APR_BUCKET_DATA,
    dns_rr_bucket_destroy,
    dns_rr_bucket_read,
    dns_rr_bucket_setaside,
    dns_rr_bucket_split,
    dns_rr_bucket_copy,
};

DNS_DECLARE(apr_bucket *) dns_bucket_rr_answer_create (
                           const char *buf, apr_size_t length,
                           apr_pool_t *pool, apr_bucket_alloc_t *list)
{
    apr_bucket *e = apr_bucket_pool_create(buf, length, pool, list);
    e->type = &dns_rr_answer_bucket_type;
    return e;
}

DNS_DECLARE(apr_bucket *) dns_bucket_rr_answer_make (
                           apr_bucket *b, const char *buf,
                           apr_size_t length, apr_pool_t *pool)
{
    apr_bucket *e = apr_bucket_pool_make(b, buf, length, pool);
    e->type = &dns_rr_answer_bucket_type;
    return e;
}

DNS_DECLARE(apr_bucket *) dns_bucket_rr_authority_create (
                           const char *buf, apr_size_t length,
                           apr_pool_t *pool, apr_bucket_alloc_t *list)
{
    apr_bucket *e = apr_bucket_pool_create(buf, length, pool, list);
    e->type = &dns_rr_authority_bucket_type;
    return e;
}

DNS_DECLARE(apr_bucket *) dns_bucket_rr_authority_make (
                           apr_bucket *b, const char *buf,
                           apr_size_t length, apr_pool_t *pool)
{
    apr_bucket *e = apr_bucket_pool_make(b, buf, length, pool);
    e->type = &dns_rr_authority_bucket_type;
    return e;
}

DNS_DECLARE(apr_bucket *) dns_bucket_rr_additional_create (
                           const char *buf, apr_size_t length,
                           apr_pool_t *pool, apr_bucket_alloc_t *list)
{
    apr_bucket *e = apr_bucket_pool_create(buf, length, pool, list);
    e->type = &dns_rr_additional_bucket_type;
    return e;
}

DNS_DECLARE(apr_bucket *) dns_bucket_rr_additional_make (
                           apr_bucket *b, const char *buf,
                           apr_size_t length, apr_pool_t *pool)
{
    apr_bucket *e = apr_bucket_pool_make(b, buf, length, pool);
    e->type = &dns_rr_additional_bucket_type;
    return e;
}
