Commit 6c847705 authored by Michael Graff's avatar Michael Graff
Browse files

checkpoint

parent bd7237e6
......@@ -25,6 +25,7 @@
#include <isc/app.h>
#include <isc/assertions.h>
#include <isc/error.h>
#include <isc/inet.h>
#include <isc/mem.h>
#include <isc/task.h>
#include <isc/thread.h>
......@@ -34,6 +35,11 @@
#include <dns/dispatch.h>
#include <dns/message.h>
#include <dns/rdatalist.h>
#include <dns/rdataset.h>
#include <dns/rdata.h>
#include <dns/rdataclass.h>
#include <dns/rdatatype.h>
#include <sys/types.h>
#include <sys/socket.h>
......@@ -49,11 +55,16 @@ isc_taskmgr_t *manager;
isc_socketmgr_t *socketmgr;
dns_dispatch_t *disp;
isc_task_t *t0, *t1, *t2;
isc_buffer_t render;
unsigned char render_buffer[1024];
dns_rdataset_t rdataset;
dns_rdatalist_t rdatalist;
void got_request(isc_task_t *, isc_event_t *);
void got_response(isc_task_t *, isc_event_t *);
void start_response(void);
static inline void CHECKRESULT(dns_result_t, char *);
void send_done(isc_task_t *, isc_event_t *);
static inline void
CHECKRESULT(dns_result_t result, char *msg)
......@@ -65,6 +76,28 @@ CHECKRESULT(dns_result_t result, char *msg)
}
}
void
send_done(isc_task_t *task, isc_event_t *ev_in)
{
isc_socketevent_t *ev = (isc_socketevent_t *)ev_in;
dns_dispentry_t *resp = (dns_dispentry_t *)ev_in->arg;
if (ev->result == ISC_R_SUCCESS) {
printf("Send done (SUCCESS)\n");
isc_event_free(&ev_in);
return;
}
CHECKRESULT(ev->result, "send_done got event");
isc_event_free(&ev_in);
printf("--- removing response (FAILURE)\n");
dns_dispatch_removeresponse(disp, &resp, NULL);
isc_app_shutdown();
}
void
start_response(void)
{
......@@ -72,17 +105,95 @@ start_response(void)
dns_messageid_t id;
isc_sockaddr_t from;
dns_message_t *msg;
isc_result_t result;
dns_name_t name;
unsigned char namebuf[255];
isc_buffer_t target;
isc_buffer_t source;
isc_region_t region;
#define QUESTION "flame.org."
isc_buffer_init(&source, QUESTION, strlen(QUESTION),
ISC_BUFFERTYPE_TEXT);
isc_buffer_add(&source, strlen(QUESTION));
isc_buffer_setactive(&source, strlen(QUESTION));
isc_buffer_init(&target, namebuf, sizeof(namebuf),
ISC_BUFFERTYPE_BINARY);
dns_name_init(&name, NULL);
result = dns_name_fromtext(&name, &source, dns_rootname, ISC_FALSE,
&target);
CHECKRESULT(result, "dns_name_fromtext()");
memset(&from, 0, sizeof(from));
from.length = sizeof(struct sockaddr_in);
from.type.sa.sa_len = sizeof(struct sockaddr_in);
from.type.sin.sin_port = htons(53);
from.type.sa.sa_family = AF_INET;
RUNTIME_CHECK(isc_inet_aton("204.152.184.97",
&from.type.sin.sin_addr) == 1);
msg = NULL;
result = dns_message_create(mctx, &msg, DNS_MESSAGE_INTENTRENDER);
CHECKRESULT(result, "dns_message_create()");
dns_message_addname(msg, &name, DNS_SECTION_QUESTION);
rdatalist.rdclass = dns_rdataclass_in;
rdatalist.type = dns_rdatatype_a;
rdatalist.ttl = 0;
ISC_LIST_INIT(rdatalist.rdata);
dns_rdataset_init(&rdataset);
result = dns_rdatalist_tordataset(&rdatalist, &rdataset);
CHECKRESULT(result, "dns_rdatalist_tordataset()");
ISC_LIST_APPEND(name.list, &rdataset, link);
result = printmessage(msg);
CHECKRESULT(result, "printmessage()");
isc_buffer_init(&render, render_buffer, sizeof(render_buffer),
ISC_BUFFERTYPE_BINARY);
result = dns_message_renderbegin(msg, &render);
CHECKRESULT(result, "dns_message_renderbegin()");
rdataset.attributes |= DNS_RDATASETATTR_QUESTION;
result = dns_message_rendersection(msg, DNS_SECTION_QUESTION, 0, 0);
CHECKRESULT(result, "dns_message_rendersection(QUESTION)");
result = dns_message_rendersection(msg, DNS_SECTION_ANSWER, 0, 0);
CHECKRESULT(result, "dns_message_rendersection(ANSWER)");
result = dns_message_rendersection(msg, DNS_SECTION_ADDITIONAL, 0, 0);
CHECKRESULT(result, "dns_message_rendersection(ADDITIONAL)");
result = dns_message_rendersection(msg, DNS_SECTION_AUTHORITY, 0, 0);
CHECKRESULT(result, "dns_message_rendersection(AUTHORITY)");
printf("--- adding response\n");
resp = NULL;
RUNTIME_CHECK(dns_dispatch_addresponse(disp, &from, t2,
got_response, NULL,
&id, &resp)
== ISC_R_SUCCESS);
result = dns_dispatch_addresponse(disp, &from, t2, got_response, NULL,
&id, &resp);
CHECKRESULT(result, "dns_dispatch_addresponse");
printf("Assigned MessageID %d\n", id);
printf("--- removing response\n");
dns_dispatch_removeresponse(disp, &resp, NULL);
isc_app_shutdown();
msg->opcode = dns_opcode_query;
msg->rcode = dns_rcode_noerror;
msg->flags = DNS_MESSAGEFLAG_RD;
msg->id = id;
result = dns_message_renderend(msg);
CHECKRESULT(result, "dns_message_renderend");
dns_message_destroy(&msg);
isc_buffer_used(&render, &region);
result = isc_socket_sendto(dns_dispatch_getsocket(disp), &region,
t2, send_done, resp, &from);
CHECKRESULT(result, "isc_socket_sendto()");
}
void
......@@ -90,8 +201,28 @@ got_response(isc_task_t *task, isc_event_t *ev_in)
{
dns_dispatchevent_t *ev = (dns_dispatchevent_t *)ev_in;
dns_dispentry_t *resp = ev->sender;
dns_message_t *msg;
isc_result_t result;
printf("App: Got response (id %d). Result: %s\n",
ev->id, isc_result_totext(ev->result));
msg = NULL;
result = dns_message_create(mctx, &msg, DNS_MESSAGE_INTENTPARSE);
CHECKRESULT(result, "dns_message_create() failed");
dns_dispatch_freeevent(disp, resp, &ev);
result = dns_message_parse(msg, &ev->buffer);
CHECKRESULT(result, "dns_message_parse() failed");
result = printmessage(msg);
CHECKRESULT(result, "printmessage() failed");
dns_message_destroy(&msg);
printf("--- removing response\n");
dns_dispatch_removeresponse(disp, &resp, &ev);
isc_app_shutdown();
}
void
......@@ -103,7 +234,7 @@ got_request(isc_task_t *task, isc_event_t *ev_in)
dns_message_t *msg;
dns_result_t result;
printf("App: Got packet. Result: %s\n",
printf("App: Got request. Result: %s\n",
isc_result_totext(ev->result));
if (ev->result != DNS_R_SUCCESS) {
......@@ -125,7 +256,7 @@ got_request(isc_task_t *task, isc_event_t *ev_in)
dns_message_destroy(&msg);
sleep (2);
sleep (1);
printf("App: Ready.\n");
cnt++;
......@@ -133,7 +264,6 @@ got_request(isc_task_t *task, isc_event_t *ev_in)
case 6:
printf("--- removing request\n");
dns_dispatch_removerequest(disp, &resp, &ev);
printf("--- destroying dispatcher\n");
start_response();
break;
......@@ -206,7 +336,7 @@ main(int argc, char *argv[])
*/
disp = NULL;
RUNTIME_CHECK(dns_dispatch_create(mctx, s0, t0, 512, 6, 1024,
16, &disp) == ISC_R_SUCCESS);
4, &disp) == ISC_R_SUCCESS);
resp = NULL;
RUNTIME_CHECK(dns_dispatch_addrequest(disp, t1, got_request, NULL,
......@@ -231,7 +361,7 @@ main(int argc, char *argv[])
fprintf(stderr, "Destroying task manager\n");
isc_taskmgr_destroy(&manager);
isc_mem_stats(mctx, stdout);
isc_mem_stats(mctx, stderr);
isc_mem_destroy(&mctx);
isc_app_finish();
......
Supports Markdown
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