server.c revision 1b9d5f39251b89ccf8bd6c1c31a7a8706e267ce6
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews/*
7d32c065c7bb56f281651ae3dd2888f32ce4f1d9Bob Halley * Copyright (C) 1999 Internet Software Consortium.
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews *
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews * Permission to use, copy, modify, and distribute this software for any
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews * purpose with or without fee is hereby granted, provided that the above
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews * copyright notice and this permission notice appear in all copies.
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews *
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews * THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SOFTWARE CONSORTIUM DISCLAIMS
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews * ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL INTERNET SOFTWARE
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews * CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews * SOFTWARE.
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews */
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews
373ce67419680a398ba3dc51a14a486caaf0afb0Mark Andrews#include <config.h>
940e4ef72b64a8a82a1038fdf08b324eec6e43a9Michael Graff
940e4ef72b64a8a82a1038fdf08b324eec6e43a9Michael Graff#include <stdio.h>
3761c433912beabe43abeed2c3513b6201c59f64Mark Andrews#include <stdlib.h>
854d0238dbc2908490197984b3b9d558008a53dfMark Andrews#include <unistd.h>
854d0238dbc2908490197984b3b9d558008a53dfMark Andrews#include <string.h>
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews
6324997211a5e2d82528dcde98e8981190a35faeMichael Graff#include <isc/assertions.h>
6324997211a5e2d82528dcde98e8981190a35faeMichael Graff#include <isc/error.h>
3ddd814a97de1d152ba0913c592d6e6dc83d38a6Michael Graff#include <isc/mem.h>
d981ca645597116d227a48bf37cc5edc061c854dBob Halley#include <isc/task.h>
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews#include <isc/thread.h>
0e8cf9a887c70f96ac448b06c069d90b830215ccMark Andrews#include <isc/result.h>
0e8cf9a887c70f96ac448b06c069d90b830215ccMark Andrews#include <isc/socket.h>
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews#include <isc/timer.h>
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews#include <dns/types.h>
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews#include <dns/result.h>
940e4ef72b64a8a82a1038fdf08b324eec6e43a9Michael Graff#include <dns/name.h>
940e4ef72b64a8a82a1038fdf08b324eec6e43a9Michael Graff#include <dns/rdata.h>
3d5cad69ec20157912e95cf3b79316dfb0a314f3Mark Andrews#include <dns/rdatalist.h>
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews#include <dns/rdataset.h>
deaaf94332abbfdb3aff53675546acfed16e5eb6Mark Andrews#include <dns/compress.h>
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews#include <dns/db.h>
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews#include <dns/message.h>
6e49e91bd08778d7eae45a2229dcf41ed97cc636David Lawrence
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews#include <sys/types.h>
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews#include <sys/socket.h>
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews#include <netinet/in.h>
3ddd814a97de1d152ba0913c592d6e6dc83d38a6Michael Graff
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson#include <arpa/inet.h>
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson#include "confparser.h"
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews#include "udpclient.h"
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews#include "tcpclient.h"
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrewsisc_mem_t *mctx = NULL;
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrewsisc_boolean_t want_stats = ISC_FALSE;
3d5cad69ec20157912e95cf3b79316dfb0a314f3Mark Andrewsdns_db_t *db;
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrewsstatic inline isc_boolean_t
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark AndrewsCHECKRESULT(dns_result_t result, char *msg)
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews{
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews if ((result) != DNS_R_SUCCESS) {
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews printf("%s: %s\n", (msg), dns_result_totext(result));
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews return (ISC_TRUE);
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson }
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews return (ISC_FALSE);
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews}
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews
3ddd814a97de1d152ba0913c592d6e6dc83d38a6Michael Graffstatic void
d981ca645597116d227a48bf37cc5edc061c854dBob Halleymakename(isc_mem_t *mctx, char *text, dns_name_t *name, dns_name_t *origin) {
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews char b[255];
0e8cf9a887c70f96ac448b06c069d90b830215ccMark Andrews isc_buffer_t source, target;
0e8cf9a887c70f96ac448b06c069d90b830215ccMark Andrews size_t len;
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews isc_region_t r1, r2;
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews dns_result_t result;
940e4ef72b64a8a82a1038fdf08b324eec6e43a9Michael Graff
f7b99290c31abeb20c55fc55391510450ce60423Mark Andrews if (origin == NULL)
940e4ef72b64a8a82a1038fdf08b324eec6e43a9Michael Graff origin = dns_rootname;
1ef8965366d91e02a4672c35a187d30aa4a4c72cMark Andrews dns_name_init(name, NULL);
94a08e09db3dc844b6ee4841c368a2d7074a9c3fAndreas Gustafsson len = strlen(text);
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews isc_buffer_init(&source, text, len, ISC_BUFFERTYPE_TEXT);
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews isc_buffer_add(&source, len);
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews isc_buffer_init(&target, b, sizeof b, ISC_BUFFERTYPE_BINARY);
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews result = dns_name_fromtext(name, &source, origin, ISC_FALSE, &target);
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews RUNTIME_CHECK(result == DNS_R_SUCCESS);
3ddd814a97de1d152ba0913c592d6e6dc83d38a6Michael Graff dns_name_toregion(name, &r1);
940e4ef72b64a8a82a1038fdf08b324eec6e43a9Michael Graff r2.base = isc_mem_get(mctx, r1.length);
940e4ef72b64a8a82a1038fdf08b324eec6e43a9Michael Graff RUNTIME_CHECK(r2.base != NULL);
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews r2.length = r1.length;
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews memcpy(r2.base, r1.base, r1.length);
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews dns_name_fromregion(name, &r2);
3d5cad69ec20157912e95cf3b79316dfb0a314f3Mark Andrews}
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews
94a08e09db3dc844b6ee4841c368a2d7074a9c3fAndreas Gustafsson/*
52637f592f705ca93fadc218e403fd55e8ce4aeaMark Andrews * This is in bin/tests/wire_test.c, but should be in a debugging library.
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews */
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrewsextern dns_result_t
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrewsprintmessage(dns_message_t *);
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrewsdns_result_t
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrewsresolve_packet(isc_mem_t *mctx, dns_db_t *, dns_message_t *, isc_buffer_t *);
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews
4529cdaedaf1a0a5f8ff89aeca510b7a4475446cBob Halleydns_result_t
940e4ef72b64a8a82a1038fdf08b324eec6e43a9Michael Graffresolve_packet(isc_mem_t *mctx, dns_db_t *db, dns_message_t *query,
940e4ef72b64a8a82a1038fdf08b324eec6e43a9Michael Graff isc_buffer_t *target)
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews{
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews dns_message_t *message;
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews dns_result_t result;
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews dns_name_t *name, name2;
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews dns_rdataset_t *rds, rdataset;
3d5cad69ec20157912e95cf3b79316dfb0a314f3Mark Andrews dns_dbnode_t *node;
d981ca645597116d227a48bf37cc5edc061c854dBob Halley
3d5cad69ec20157912e95cf3b79316dfb0a314f3Mark Andrews
f7b99290c31abeb20c55fc55391510450ce60423Mark Andrews result = dns_message_create(mctx, &message, DNS_MESSAGE_INTENT_RENDER);
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews CHECKRESULT(result, "dns_message_create failed");
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews message->id = query->id;
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews message->rcode = dns_rcode_noerror;
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews message->flags = query->flags;
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews message->flags |= DNS_MESSAGEFLAG_QR;
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews /*
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews * XXX This is a total and disgusting hack. We need a way to add
7c0539bea56022274da04263eb41fbb5b8835c38Mark Andrews * a copy of a rdataset and a name to the new message, but for now
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews * I'll just steal the one from the existing query message, and
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews * make certain the query is not destroyed before our message is.
3ddd814a97de1d152ba0913c592d6e6dc83d38a6Michael Graff */
d981ca645597116d227a48bf37cc5edc061c854dBob Halley result = dns_message_firstname(query, DNS_SECTION_QUESTION);
0e8cf9a887c70f96ac448b06c069d90b830215ccMark Andrews if (result != DNS_R_SUCCESS)
0e8cf9a887c70f96ac448b06c069d90b830215ccMark Andrews return (result);
373ce67419680a398ba3dc51a14a486caaf0afb0Mark Andrews
373ce67419680a398ba3dc51a14a486caaf0afb0Mark Andrews name = NULL;
82ca33427bdd4f3bc4ed3431e86bd810fe751674Andreas Gustafsson dns_message_currentname(query, DNS_SECTION_QUESTION, &name);
373ce67419680a398ba3dc51a14a486caaf0afb0Mark Andrews rds = ISC_LIST_HEAD(name->list);
373ce67419680a398ba3dc51a14a486caaf0afb0Mark Andrews if (rds == NULL)
373ce67419680a398ba3dc51a14a486caaf0afb0Mark Andrews return (DNS_R_UNEXPECTED);
373ce67419680a398ba3dc51a14a486caaf0afb0Mark Andrews
f7b99290c31abeb20c55fc55391510450ce60423Mark Andrews dns_message_addname(message, name, DNS_SECTION_QUESTION);
373ce67419680a398ba3dc51a14a486caaf0afb0Mark Andrews
373ce67419680a398ba3dc51a14a486caaf0afb0Mark Andrews result = printmessage(message);
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews INSIST(result == DNS_R_SUCCESS); /* XXX not in a real server */
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews
3ddd814a97de1d152ba0913c592d6e6dc83d38a6Michael Graff /*
940e4ef72b64a8a82a1038fdf08b324eec6e43a9Michael Graff * Pull the name out of the database.
940e4ef72b64a8a82a1038fdf08b324eec6e43a9Michael Graff */
9281e7aa775026dc47c01745fdcc438645146877Mark Andrews if (!dns_name_issubdomain(name, dns_db_origin(db))) {
9281e7aa775026dc47c01745fdcc438645146877Mark Andrews message->rcode = dns_rcode_nxdomain;
9281e7aa775026dc47c01745fdcc438645146877Mark Andrews goto render;
9281e7aa775026dc47c01745fdcc438645146877Mark Andrews }
82ca33427bdd4f3bc4ed3431e86bd810fe751674Andreas Gustafsson
9281e7aa775026dc47c01745fdcc438645146877Mark Andrews dns_name_init(&name2, NULL);
82ca33427bdd4f3bc4ed3431e86bd810fe751674Andreas Gustafsson dns_name_clone(name, &name2);
9281e7aa775026dc47c01745fdcc438645146877Mark Andrews node = NULL;
9281e7aa775026dc47c01745fdcc438645146877Mark Andrews result = dns_db_findnode(db, &name2, ISC_FALSE, &node);
9281e7aa775026dc47c01745fdcc438645146877Mark Andrews if (result == DNS_R_NOTFOUND) {
f7b99290c31abeb20c55fc55391510450ce60423Mark Andrews message->flags |= DNS_MESSAGEFLAG_AA;
9281e7aa775026dc47c01745fdcc438645146877Mark Andrews message->rcode = dns_rcode_nxdomain;
9281e7aa775026dc47c01745fdcc438645146877Mark Andrews goto render;
9281e7aa775026dc47c01745fdcc438645146877Mark Andrews }
9281e7aa775026dc47c01745fdcc438645146877Mark Andrews if (result != DNS_R_SUCCESS) {
9281e7aa775026dc47c01745fdcc438645146877Mark Andrews message->rcode = dns_rcode_servfail;
9281e7aa775026dc47c01745fdcc438645146877Mark Andrews printf("Foo\n");
9281e7aa775026dc47c01745fdcc438645146877Mark Andrews goto render;
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews }
94a3bcd132e515b4baa0884ba9dd0f361d2e17bcMark Andrews
4529cdaedaf1a0a5f8ff89aeca510b7a4475446cBob Halley dns_rdataset_init(&rdataset);
940e4ef72b64a8a82a1038fdf08b324eec6e43a9Michael Graff result = dns_db_findrdataset(db, node, NULL, rds->type, 0, &rdataset);
940e4ef72b64a8a82a1038fdf08b324eec6e43a9Michael Graff dns_db_detachnode(db, &node);
9281e7aa775026dc47c01745fdcc438645146877Mark Andrews if (result == DNS_R_NOTFOUND) {
9281e7aa775026dc47c01745fdcc438645146877Mark Andrews message->flags |= DNS_MESSAGEFLAG_AA;
94a3bcd132e515b4baa0884ba9dd0f361d2e17bcMark Andrews message->rcode = dns_rcode_nxdomain;
9281e7aa775026dc47c01745fdcc438645146877Mark Andrews goto render;
82ca33427bdd4f3bc4ed3431e86bd810fe751674Andreas Gustafsson }
9281e7aa775026dc47c01745fdcc438645146877Mark Andrews if (result != DNS_R_SUCCESS) {
9281e7aa775026dc47c01745fdcc438645146877Mark Andrews message->rcode = dns_rcode_servfail;
9281e7aa775026dc47c01745fdcc438645146877Mark Andrews printf("Foo2\n");
9281e7aa775026dc47c01745fdcc438645146877Mark Andrews goto render;
9281e7aa775026dc47c01745fdcc438645146877Mark Andrews }
94a3bcd132e515b4baa0884ba9dd0f361d2e17bcMark Andrews
d981ca645597116d227a48bf37cc5edc061c854dBob Halley ISC_LIST_APPEND(name2.list, &rdataset, link);
3ddd814a97de1d152ba0913c592d6e6dc83d38a6Michael Graff dns_message_addname(message, &name2, DNS_SECTION_ANSWER);
d981ca645597116d227a48bf37cc5edc061c854dBob Halley message->flags |= DNS_MESSAGEFLAG_AA;
d981ca645597116d227a48bf37cc5edc061c854dBob Halley
d981ca645597116d227a48bf37cc5edc061c854dBob Halley render:
82ca33427bdd4f3bc4ed3431e86bd810fe751674Andreas Gustafsson
82ca33427bdd4f3bc4ed3431e86bd810fe751674Andreas Gustafsson result = dns_message_renderbegin(message, target);
82ca33427bdd4f3bc4ed3431e86bd810fe751674Andreas Gustafsson if (result != DNS_R_SUCCESS)
940e4ef72b64a8a82a1038fdf08b324eec6e43a9Michael Graff return (result);
940e4ef72b64a8a82a1038fdf08b324eec6e43a9Michael Graff
d981ca645597116d227a48bf37cc5edc061c854dBob Halley result = dns_message_rendersection(message, DNS_SECTION_QUESTION,
419590499823ce15b5d2ad4fe71eaf04bd5a86c0Michael Graff 0, 0);
d981ca645597116d227a48bf37cc5edc061c854dBob Halley if (result != DNS_R_SUCCESS)
d981ca645597116d227a48bf37cc5edc061c854dBob Halley return (result);
3ddd814a97de1d152ba0913c592d6e6dc83d38a6Michael Graff
940e4ef72b64a8a82a1038fdf08b324eec6e43a9Michael Graff result = dns_message_rendersection(message, DNS_SECTION_ANSWER,
940e4ef72b64a8a82a1038fdf08b324eec6e43a9Michael Graff 0, 0);
e27a69f8bd9538e08f775265167ba6cc5f47c587Bob Halley if (result != DNS_R_SUCCESS)
e27a69f8bd9538e08f775265167ba6cc5f47c587Bob Halley return (result);
e27a69f8bd9538e08f775265167ba6cc5f47c587Bob Halley
e27a69f8bd9538e08f775265167ba6cc5f47c587Bob Halley result = dns_message_rendersection(message, DNS_SECTION_AUTHORITY,
e27a69f8bd9538e08f775265167ba6cc5f47c587Bob Halley 0, 0);
e27a69f8bd9538e08f775265167ba6cc5f47c587Bob Halley if (result != DNS_R_SUCCESS)
e27a69f8bd9538e08f775265167ba6cc5f47c587Bob Halley return (result);
e27a69f8bd9538e08f775265167ba6cc5f47c587Bob Halley
e27a69f8bd9538e08f775265167ba6cc5f47c587Bob Halley result = dns_message_rendersection(message, DNS_SECTION_ADDITIONAL,
e27a69f8bd9538e08f775265167ba6cc5f47c587Bob Halley 0, 0);
e27a69f8bd9538e08f775265167ba6cc5f47c587Bob Halley if (result != DNS_R_SUCCESS)
e27a69f8bd9538e08f775265167ba6cc5f47c587Bob Halley return (result);
854d0238dbc2908490197984b3b9d558008a53dfMark Andrews
result = dns_message_rendersection(message, DNS_SECTION_OPT,
0, 0);
if (result != DNS_R_SUCCESS)
return (result);
result = dns_message_rendersection(message, DNS_SECTION_TSIG,
0, 0);
if (result != DNS_R_SUCCESS)
return (result);
result = dns_message_renderend(message);
dns_message_destroy(&message);
return (DNS_R_SUCCESS);
}
/*
* Process the wire format message given in r, and return a new packet to
* transmit.
*
* Return of DNS_R_SUCCESS means r->base is a newly allocated region of
* memory, and r->length is its length. The actual for-transmit packet
* begins at (r->length + reslen) to reserve (reslen) bytes at the front
* of the packet for transmission specific details.
*/
static dns_result_t
dispatch(isc_mem_t *mctx, isc_region_t *rxr, unsigned int reslen)
{
char t[512];
isc_buffer_t source;
isc_buffer_t target;
dns_result_t result;
isc_region_t txr;
dns_message_t *message;
/*
* Set up the input buffer from the contents of the region passed
* to us.
*/
isc_buffer_init(&source, rxr->base, rxr->length,
ISC_BUFFERTYPE_BINARY);
isc_buffer_add(&source, rxr->length);
result = dns_message_create(mctx, &message, DNS_MESSAGE_INTENT_PARSE);
if (CHECKRESULT(result, "dns_message_create failed")) {
return (result);
}
result = dns_message_parse(message, &source);
if (CHECKRESULT(result, "dns_message_parsed failed")) {
dns_message_destroy(&message);
return (result);
}
CHECKRESULT(result, "dns_message_parse failed");
result = printmessage(message);
if (CHECKRESULT(result, "printmessage failed")) {
dns_message_destroy(&message);
return (result);
}
isc_buffer_init(&target, t, sizeof(t), ISC_BUFFERTYPE_BINARY);
result = resolve_packet(mctx, db, message, &target);
if (result != DNS_R_SUCCESS) {
dns_message_destroy(&message);
return (result);
}
/*
* Copy the reply out, adjusting for reslen
*/
isc_buffer_used(&target, &txr);
txr.base = isc_mem_get(mctx, txr.length + reslen);
if (txr.base == NULL) {
dns_message_destroy(&message);
return (DNS_R_NOMEMORY);
}
memcpy(txr.base + reslen, t + reslen, txr.length);
rxr->base = txr.base;
rxr->length = txr.length + reslen;
printf("Base == %p, length == %u\n", txr.base, txr.length);
fflush(stdout);
if (want_stats)
isc_mem_stats(mctx, stdout);
dns_message_destroy(&message);
return (DNS_R_SUCCESS);
}
int
main(int argc, char *argv[])
{
isc_taskmgr_t *manager = NULL;
unsigned int workers;
isc_socketmgr_t *socketmgr;
isc_socket_t *so0, *so1;
isc_sockaddr_t sockaddr;
unsigned int addrlen;
udp_listener_t *ludp;
tcp_listener_t *ltcp;
dns_name_t base, *origin;
int ch;
char basetext[1000];
dns_rdatatype_t type = 2;
dns_result_t result;
#if 0 /* brister */
isc_cfgctx_t *configctx = NULL;
const char *conffile = "/etc/named.conf"; /* XXX hardwired */
#endif
/*+ XXX */
strcpy(basetext, "");
while ((ch = getopt(argc, argv, "z:t:s")) != -1) {
switch (ch) {
case 'z':
strcpy(basetext, optarg);
break;
case 't':
type = atoi(optarg);
break;
case 's':
want_stats = ISC_TRUE;
break;
}
}
argc -= optind;
argv += optind;
if (argc < 1) {
fprintf(stderr, "usage: named filename\n");
exit(1);
}
/*- XXX */
memset(&sockaddr, 0, sizeof(sockaddr));
sockaddr.type.sin.sin_port = htons(5544);
addrlen = sizeof(struct sockaddr_in);
workers = 2;
printf("%d workers\n", workers);
RUNTIME_CHECK(isc_mem_create(0, 0, &mctx) == ISC_R_SUCCESS);
#if 0 /* brister */
parser_init();
RUNTIME_CHECK(parse_configuration(conffile, mctx, &configctx) ==
ISC_R_SUCCESS);
#endif
/*+ XXX */
if (strcmp(basetext, "") == 0)
strcpy(basetext, "vix.com.");
makename(mctx, basetext, &base, NULL);
db = NULL;
result = dns_db_create(mctx, "rbt", &base, ISC_FALSE, 1, 0, NULL,
&db);
RUNTIME_CHECK(result == DNS_R_SUCCESS);
origin = &base;
printf("loading %s\n", argv[0]);
result = dns_db_load(db, argv[0]);
if (result != DNS_R_SUCCESS) {
printf("couldn't load master file: %s\n",
dns_result_totext(result));
exit(1);
}
/*- XXX */
RUNTIME_CHECK(isc_taskmgr_create(mctx, workers, 0, &manager) ==
ISC_R_SUCCESS);
/*
* Open up a database.
*/
socketmgr = NULL;
RUNTIME_CHECK(isc_socketmgr_create(mctx, &socketmgr) == ISC_R_SUCCESS);
/*
* open up a UDP socket
*/
so0 = NULL;
memset(&sockaddr, 0, sizeof(sockaddr));
sockaddr.type.sin.sin_family = AF_INET;
sockaddr.type.sin.sin_port = htons(5544);
addrlen = sizeof(struct sockaddr_in);
RUNTIME_CHECK(isc_socket_create(socketmgr, isc_socket_udp, &so0) ==
ISC_R_SUCCESS);
RUNTIME_CHECK(isc_socket_bind(so0, &sockaddr,
(int)addrlen) == ISC_R_SUCCESS);
ludp = udp_listener_allocate(mctx, workers);
RUNTIME_CHECK(udp_listener_start(ludp, so0, manager,
workers, workers, 0,
dispatch) == ISC_R_SUCCESS);
if (want_stats)
isc_mem_stats(mctx, stdout);
/*
* open up a TCP socket
*/
so1 = NULL;
memset(&sockaddr, 0, sizeof(sockaddr));
sockaddr.type.sin.sin_family = AF_INET;
sockaddr.type.sin.sin_port = htons(5544);
addrlen = sizeof(struct sockaddr_in);
RUNTIME_CHECK(isc_socket_create(socketmgr, isc_socket_tcp, &so1) ==
ISC_R_SUCCESS);
RUNTIME_CHECK(isc_socket_bind(so1, &sockaddr,
(int)addrlen) == ISC_R_SUCCESS);
ltcp = tcp_listener_allocate(mctx, workers);
RUNTIME_CHECK(tcp_listener_start(ltcp, so1, manager,
workers, workers, 0,
dispatch) == ISC_R_SUCCESS);
if (want_stats)
isc_mem_stats(mctx, stdout);
/*
* XXX Need to set up a condition variable here, and wait on it.
* For now, just semi-busy loop.
*/
for (;;)
sleep(10);
printf("Destroying socket manager\n");
isc_socketmgr_destroy(&socketmgr);
printf("Destroying task manager\n");
isc_taskmgr_destroy(&manager);
if (want_stats)
isc_mem_stats(mctx, stdout);
isc_mem_destroy(&mctx);
return (0);
}