1999-01-19 06:32:53 +00:00
|
|
|
/*
|
1999-03-06 03:55:54 +00:00
|
|
|
* Copyright (C) 1999 Internet Software Consortium.
|
1999-01-19 06:32:53 +00:00
|
|
|
*
|
|
|
|
* Permission to use, copy, modify, and distribute this software for any
|
|
|
|
* purpose with or without fee is hereby granted, provided that the above
|
|
|
|
* copyright notice and this permission notice appear in all copies.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SOFTWARE CONSORTIUM DISCLAIMS
|
|
|
|
* ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES
|
|
|
|
* OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL INTERNET SOFTWARE
|
|
|
|
* CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
|
|
|
|
* DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
|
|
|
|
* PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
|
|
|
|
* ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
|
|
|
|
* SOFTWARE.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <config.h>
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
#include <isc/assertions.h>
|
|
|
|
#include <isc/error.h>
|
|
|
|
#include <isc/mem.h>
|
|
|
|
#include <isc/task.h>
|
|
|
|
#include <isc/thread.h>
|
|
|
|
#include <isc/result.h>
|
|
|
|
#include <isc/socket.h>
|
|
|
|
#include <isc/timer.h>
|
|
|
|
|
|
|
|
#include <dns/types.h>
|
|
|
|
#include <dns/result.h>
|
|
|
|
#include <dns/name.h>
|
|
|
|
#include <dns/rdata.h>
|
|
|
|
#include <dns/rdatalist.h>
|
|
|
|
#include <dns/rdataset.h>
|
|
|
|
#include <dns/compress.h>
|
1999-01-31 12:31:31 +00:00
|
|
|
#include <dns/db.h>
|
1999-04-30 05:42:06 +00:00
|
|
|
#include <dns/message.h>
|
1999-01-19 06:32:53 +00:00
|
|
|
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/socket.h>
|
|
|
|
|
|
|
|
#include <netinet/in.h>
|
|
|
|
|
|
|
|
#include <arpa/inet.h>
|
|
|
|
|
1999-02-24 00:14:57 +00:00
|
|
|
#include "confparser.h"
|
1999-01-20 03:54:54 +00:00
|
|
|
#include "udpclient.h"
|
1999-01-28 05:52:20 +00:00
|
|
|
#include "tcpclient.h"
|
1999-01-19 06:32:53 +00:00
|
|
|
|
1999-01-20 03:54:54 +00:00
|
|
|
isc_mem_t *mctx = NULL;
|
1999-02-01 21:11:10 +00:00
|
|
|
isc_boolean_t want_stats = ISC_FALSE;
|
1999-01-31 12:31:31 +00:00
|
|
|
dns_db_t *db;
|
|
|
|
|
1999-04-30 05:42:06 +00:00
|
|
|
static inline isc_boolean_t
|
|
|
|
CHECKRESULT(dns_result_t result, char *msg)
|
|
|
|
{
|
|
|
|
if ((result) != DNS_R_SUCCESS) {
|
|
|
|
printf("%s: %s\n", (msg), dns_result_totext(result));
|
|
|
|
return (ISC_TRUE);
|
|
|
|
}
|
|
|
|
|
|
|
|
return (ISC_FALSE);
|
|
|
|
}
|
1999-01-31 12:31:31 +00:00
|
|
|
|
|
|
|
static void
|
|
|
|
makename(isc_mem_t *mctx, char *text, dns_name_t *name, dns_name_t *origin) {
|
|
|
|
char b[255];
|
|
|
|
isc_buffer_t source, target;
|
|
|
|
size_t len;
|
|
|
|
isc_region_t r1, r2;
|
|
|
|
dns_result_t result;
|
|
|
|
|
|
|
|
if (origin == NULL)
|
|
|
|
origin = dns_rootname;
|
|
|
|
dns_name_init(name, NULL);
|
|
|
|
len = strlen(text);
|
|
|
|
isc_buffer_init(&source, text, len, ISC_BUFFERTYPE_TEXT);
|
|
|
|
isc_buffer_add(&source, len);
|
|
|
|
isc_buffer_init(&target, b, sizeof b, ISC_BUFFERTYPE_BINARY);
|
|
|
|
result = dns_name_fromtext(name, &source, origin, ISC_FALSE, &target);
|
|
|
|
RUNTIME_CHECK(result == DNS_R_SUCCESS);
|
|
|
|
dns_name_toregion(name, &r1);
|
|
|
|
r2.base = isc_mem_get(mctx, r1.length);
|
|
|
|
RUNTIME_CHECK(r2.base != NULL);
|
|
|
|
r2.length = r1.length;
|
|
|
|
memcpy(r2.base, r1.base, r1.length);
|
|
|
|
dns_name_fromregion(name, &r2);
|
|
|
|
}
|
|
|
|
|
1999-02-01 20:47:58 +00:00
|
|
|
/*
|
1999-04-30 05:42:06 +00:00
|
|
|
* This is in bin/tests/wire_test.c, but should be in a debugging library.
|
1999-02-01 20:47:58 +00:00
|
|
|
*/
|
1999-04-30 05:42:06 +00:00
|
|
|
extern dns_result_t
|
|
|
|
printmessage(dns_message_t *);
|
|
|
|
|
|
|
|
dns_result_t
|
|
|
|
resolve_packet(isc_mem_t *mctx, dns_db_t *, dns_message_t *,
|
|
|
|
dns_message_t **, isc_buffer_t *);
|
|
|
|
|
1999-02-01 20:47:58 +00:00
|
|
|
dns_result_t
|
1999-04-30 05:42:06 +00:00
|
|
|
resolve_packet(isc_mem_t *mctx, dns_db_t *db, dns_message_t *query,
|
|
|
|
dns_message_t **reply, isc_buffer_t *target)
|
|
|
|
{
|
|
|
|
dns_message_t *message;
|
|
|
|
dns_result_t result;
|
|
|
|
|
|
|
|
result = dns_message_create(mctx, &message, DNS_MESSAGE_INTENT_RENDER);
|
|
|
|
CHECKRESULT(result, "dns_message_create failed");
|
|
|
|
|
|
|
|
message->id = query->id;
|
|
|
|
message->rcode =
|
|
|
|
|
|
|
|
result = printmessage(message);
|
|
|
|
CHECKRESULT(result, "printmessage() failed");
|
|
|
|
|
|
|
|
return (DNS_R_NOSPACE);
|
|
|
|
}
|
1999-02-01 20:47:58 +00:00
|
|
|
|
1999-01-29 06:18:43 +00:00
|
|
|
/*
|
|
|
|
* 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)
|
|
|
|
{
|
1999-04-30 05:42:06 +00:00
|
|
|
char t[512];
|
1999-01-31 12:31:31 +00:00
|
|
|
isc_buffer_t source;
|
|
|
|
isc_buffer_t target;
|
|
|
|
dns_result_t result;
|
1999-01-29 06:18:43 +00:00
|
|
|
isc_region_t txr;
|
1999-04-30 05:42:06 +00:00
|
|
|
dns_message_t *message, *reply;
|
1999-01-31 12:31:31 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* 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);
|
|
|
|
|
1999-04-30 05:42:06 +00:00
|
|
|
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, &reply, &target);
|
|
|
|
dns_message_destroy(&message);
|
|
|
|
if (result != DNS_R_SUCCESS) {
|
|
|
|
if (reply != NULL)
|
|
|
|
dns_message_destroy(&reply);
|
1999-01-31 12:31:31 +00:00
|
|
|
return (result);
|
1999-04-30 05:42:06 +00:00
|
|
|
}
|
1999-01-29 06:18:43 +00:00
|
|
|
|
|
|
|
/*
|
1999-01-31 12:58:16 +00:00
|
|
|
* Copy the reply out, adjusting for reslen
|
1999-01-29 06:18:43 +00:00
|
|
|
*/
|
1999-01-31 12:31:31 +00:00
|
|
|
isc_buffer_used(&target, &txr);
|
1999-01-31 12:58:16 +00:00
|
|
|
txr.base = isc_mem_get(mctx, txr.length + reslen);
|
1999-04-30 05:42:06 +00:00
|
|
|
if (txr.base == NULL) {
|
|
|
|
dns_message_destroy(&reply);
|
|
|
|
|
1999-01-29 06:18:43 +00:00
|
|
|
return (DNS_R_NOMEMORY);
|
1999-04-30 05:42:06 +00:00
|
|
|
}
|
1999-01-29 06:18:43 +00:00
|
|
|
|
1999-01-31 12:58:16 +00:00
|
|
|
memcpy(txr.base + reslen, t + reslen, txr.length);
|
1999-01-29 06:18:43 +00:00
|
|
|
rxr->base = txr.base;
|
1999-01-31 12:58:16 +00:00
|
|
|
rxr->length = txr.length + reslen;
|
1999-01-29 06:18:43 +00:00
|
|
|
|
1999-01-31 12:31:31 +00:00
|
|
|
printf("Base == %p, length == %u\n", txr.base, txr.length);
|
|
|
|
fflush(stdout);
|
|
|
|
|
1999-02-01 21:11:10 +00:00
|
|
|
if (want_stats)
|
|
|
|
isc_mem_stats(mctx, stdout);
|
1999-01-29 06:18:43 +00:00
|
|
|
|
1999-04-30 05:42:06 +00:00
|
|
|
dns_message_destroy(&reply);
|
|
|
|
|
1999-01-29 06:18:43 +00:00
|
|
|
return (DNS_R_SUCCESS);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
1999-01-19 06:32:53 +00:00
|
|
|
int
|
|
|
|
main(int argc, char *argv[])
|
|
|
|
{
|
|
|
|
isc_taskmgr_t *manager = NULL;
|
|
|
|
unsigned int workers;
|
|
|
|
isc_socketmgr_t *socketmgr;
|
1999-01-28 05:52:20 +00:00
|
|
|
isc_socket_t *so0, *so1;
|
1999-01-19 06:32:53 +00:00
|
|
|
isc_sockaddr_t sockaddr;
|
|
|
|
unsigned int addrlen;
|
1999-01-28 05:52:20 +00:00
|
|
|
udp_listener_t *ludp;
|
|
|
|
tcp_listener_t *ltcp;
|
1999-01-31 12:31:31 +00:00
|
|
|
dns_name_t base, *origin;
|
|
|
|
int ch;
|
|
|
|
char basetext[1000];
|
|
|
|
dns_rdatatype_t type = 2;
|
|
|
|
dns_result_t result;
|
1999-02-24 00:14:57 +00:00
|
|
|
|
|
|
|
#if 0 /* brister */
|
1999-02-01 20:47:58 +00:00
|
|
|
isc_cfgctx_t *configctx = NULL;
|
|
|
|
const char *conffile = "/etc/named.conf"; /* XXX hardwired */
|
|
|
|
#endif
|
1999-01-31 12:31:31 +00:00
|
|
|
|
|
|
|
/*+ XXX */
|
|
|
|
strcpy(basetext, "");
|
1999-02-01 21:11:10 +00:00
|
|
|
while ((ch = getopt(argc, argv, "z:t:s")) != -1) {
|
1999-01-31 12:31:31 +00:00
|
|
|
switch (ch) {
|
|
|
|
case 'z':
|
|
|
|
strcpy(basetext, optarg);
|
|
|
|
break;
|
|
|
|
case 't':
|
|
|
|
type = atoi(optarg);
|
|
|
|
break;
|
1999-02-01 21:11:10 +00:00
|
|
|
case 's':
|
|
|
|
want_stats = ISC_TRUE;
|
|
|
|
break;
|
1999-01-31 12:31:31 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
argc -= optind;
|
|
|
|
argv += optind;
|
|
|
|
|
|
|
|
if (argc < 1) {
|
|
|
|
fprintf(stderr, "usage: named filename\n");
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*- XXX */
|
1999-01-19 06:32:53 +00:00
|
|
|
|
|
|
|
memset(&sockaddr, 0, sizeof(sockaddr));
|
|
|
|
sockaddr.type.sin.sin_port = htons(5544);
|
|
|
|
addrlen = sizeof(struct sockaddr_in);
|
|
|
|
|
1999-01-31 12:31:31 +00:00
|
|
|
workers = 2;
|
1999-01-19 06:32:53 +00:00
|
|
|
printf("%d workers\n", workers);
|
|
|
|
|
|
|
|
RUNTIME_CHECK(isc_mem_create(0, 0, &mctx) == ISC_R_SUCCESS);
|
|
|
|
|
1999-02-24 00:14:57 +00:00
|
|
|
#if 0 /* brister */
|
|
|
|
parser_init();
|
|
|
|
RUNTIME_CHECK(parse_configuration(conffile, mctx, &configctx) ==
|
|
|
|
ISC_R_SUCCESS);
|
1999-01-30 01:00:20 +00:00
|
|
|
#endif
|
1999-01-31 12:31:31 +00:00
|
|
|
|
|
|
|
/*+ 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);
|
1999-01-30 01:00:20 +00:00
|
|
|
|
1999-01-31 12:31:31 +00:00
|
|
|
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 */
|
|
|
|
|
1999-01-19 06:32:53 +00:00
|
|
|
RUNTIME_CHECK(isc_taskmgr_create(mctx, workers, 0, &manager) ==
|
|
|
|
ISC_R_SUCCESS);
|
|
|
|
|
1999-01-31 12:31:31 +00:00
|
|
|
/*
|
|
|
|
* Open up a database.
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
1999-01-19 06:32:53 +00:00
|
|
|
socketmgr = NULL;
|
|
|
|
RUNTIME_CHECK(isc_socketmgr_create(mctx, &socketmgr) == ISC_R_SUCCESS);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* open up a UDP socket
|
|
|
|
*/
|
1999-01-28 05:52:20 +00:00
|
|
|
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);
|
1999-01-29 06:18:43 +00:00
|
|
|
RUNTIME_CHECK(udp_listener_start(ludp, so0, manager,
|
|
|
|
workers, workers, 0,
|
|
|
|
dispatch) == ISC_R_SUCCESS);
|
1999-01-28 05:52:20 +00:00
|
|
|
|
1999-02-01 21:11:10 +00:00
|
|
|
if (want_stats)
|
|
|
|
isc_mem_stats(mctx, stdout);
|
1999-01-28 05:52:20 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* open up a TCP socket
|
|
|
|
*/
|
1999-01-19 06:32:53 +00:00
|
|
|
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);
|
1999-01-28 05:52:20 +00:00
|
|
|
RUNTIME_CHECK(isc_socket_create(socketmgr, isc_socket_tcp, &so1) ==
|
1999-01-19 06:32:53 +00:00
|
|
|
ISC_R_SUCCESS);
|
|
|
|
RUNTIME_CHECK(isc_socket_bind(so1, &sockaddr,
|
|
|
|
(int)addrlen) == ISC_R_SUCCESS);
|
|
|
|
|
1999-01-28 05:52:20 +00:00
|
|
|
ltcp = tcp_listener_allocate(mctx, workers);
|
1999-01-29 06:18:43 +00:00
|
|
|
RUNTIME_CHECK(tcp_listener_start(ltcp, so1, manager,
|
|
|
|
workers, workers, 0,
|
|
|
|
dispatch) == ISC_R_SUCCESS);
|
1999-01-19 06:32:53 +00:00
|
|
|
|
1999-02-01 21:11:10 +00:00
|
|
|
if (want_stats)
|
|
|
|
isc_mem_stats(mctx, stdout);
|
1999-01-19 06:32:53 +00:00
|
|
|
|
1999-02-11 06:38:12 +00:00
|
|
|
/*
|
|
|
|
* XXX Need to set up a condition variable here, and wait on it.
|
|
|
|
* For now, just semi-busy loop.
|
|
|
|
*/
|
1999-01-19 06:32:53 +00:00
|
|
|
for (;;)
|
|
|
|
sleep(10);
|
|
|
|
|
|
|
|
printf("Destroying socket manager\n");
|
|
|
|
isc_socketmgr_destroy(&socketmgr);
|
|
|
|
|
|
|
|
printf("Destroying task manager\n");
|
|
|
|
isc_taskmgr_destroy(&manager);
|
|
|
|
|
1999-02-01 21:11:10 +00:00
|
|
|
if (want_stats)
|
|
|
|
isc_mem_stats(mctx, stdout);
|
1999-01-19 06:32:53 +00:00
|
|
|
isc_mem_destroy(&mctx);
|
|
|
|
|
|
|
|
return (0);
|
|
|
|
}
|