6 #define FLX_QUERY_HISTORY_MSEC 100
7 #define FLX_QUERY_DEFER_MSEC 100
8 #define FLX_RESPONSE_HISTORY_MSEC 700
9 #define FLX_RESPONSE_DEFER_MSEC 20
10 #define FLX_RESPONSE_JITTER_MSEC 100
11 #define FLX_PROBE_DEFER_MSEC 20
13 flxPacketScheduler *flx_packet_scheduler_new(flxServer *server, flxInterface *i) {
14 flxPacketScheduler *s;
19 s = g_new(flxPacketScheduler, 1);
23 FLX_LLIST_HEAD_INIT(flxQueryJob, s->query_jobs);
24 FLX_LLIST_HEAD_INIT(flxResponseJob, s->response_jobs);
25 FLX_LLIST_HEAD_INIT(flxKnownAnswer, s->known_answers);
26 FLX_LLIST_HEAD_INIT(flxProbeJob, s->probe_jobs);
31 static void query_job_free(flxPacketScheduler *s, flxQueryJob *qj) {
35 flx_time_event_queue_remove(qj->scheduler->server->time_event_queue, qj->time_event);
37 FLX_LLIST_REMOVE(flxQueryJob, jobs, s->query_jobs, qj);
39 flx_key_unref(qj->key);
43 static void response_job_free(flxPacketScheduler *s, flxResponseJob *rj) {
47 flx_time_event_queue_remove(rj->scheduler->server->time_event_queue, rj->time_event);
49 FLX_LLIST_REMOVE(flxResponseJob, jobs, s->response_jobs, rj);
51 flx_record_unref(rj->record);
55 static void probe_job_free(flxPacketScheduler *s, flxProbeJob *pj) {
59 flx_time_event_queue_remove(pj->scheduler->server->time_event_queue, pj->time_event);
61 FLX_LLIST_REMOVE(flxProbeJob, jobs, s->probe_jobs, pj);
63 flx_record_unref(pj->record);
67 void flx_packet_scheduler_free(flxPacketScheduler *s) {
75 g_assert(!s->known_answers);
77 while ((qj = s->query_jobs))
78 query_job_free(s, qj);
79 while ((rj = s->response_jobs))
80 response_job_free(s, rj);
81 while ((pj = s->probe_jobs))
82 probe_job_free(s, pj);
87 static gpointer known_answer_walk_callback(flxCache *c, flxKey *pattern, flxCacheEntry *e, gpointer userdata) {
88 flxPacketScheduler *s = userdata;
96 if (flx_cache_entry_half_ttl(c, e))
99 ka = g_new0(flxKnownAnswer, 1);
101 ka->record = flx_record_ref(e->record);
103 FLX_LLIST_PREPEND(flxKnownAnswer, known_answer, s->known_answers, ka);
107 static guint8* packet_add_query_job(flxPacketScheduler *s, flxDnsPacket *p, flxQueryJob *qj) {
114 if ((d = flx_dns_packet_append_key(p, qj->key))) {
119 /* Drop query after some time from history */
120 flx_elapse_time(&tv, FLX_QUERY_HISTORY_MSEC, 0);
121 flx_time_event_queue_update(s->server->time_event_queue, qj->time_event, &tv);
123 g_get_current_time(&qj->delivery);
125 /* Add all matching known answers to the list */
126 flx_cache_walk(s->interface->cache, qj->key, known_answer_walk_callback, s);
132 static void append_known_answers_and_send(flxPacketScheduler *s, flxDnsPacket *p) {
140 while ((ka = s->known_answers)) {
142 while (!flx_dns_packet_append_record(p, ka->record, FALSE)) {
144 g_assert(!flx_dns_packet_is_empty(p));
146 flx_dns_packet_set_field(p, FLX_DNS_FIELD_FLAGS, flx_dns_packet_get_field(p, FLX_DNS_FIELD_FLAGS) | FLX_DNS_FLAG_TC);
147 flx_dns_packet_set_field(p, FLX_DNS_FIELD_ANCOUNT, n);
148 flx_interface_send_packet(s->interface, p);
149 flx_dns_packet_free(p);
151 p = flx_dns_packet_new_query(s->interface->hardware->mtu - 48);
155 FLX_LLIST_REMOVE(flxKnownAnswer, known_answer, s->known_answers, ka);
156 flx_record_unref(ka->record);
162 flx_dns_packet_set_field(p, FLX_DNS_FIELD_ANCOUNT, n);
163 flx_interface_send_packet(s->interface, p);
164 flx_dns_packet_free(p);
167 static void query_elapse(flxTimeEvent *e, gpointer data) {
168 flxQueryJob *qj = data;
169 flxPacketScheduler *s;
178 /* Lets remove it from the history */
179 query_job_free(s, qj);
183 g_assert(!s->known_answers);
185 p = flx_dns_packet_new_query(s->interface->hardware->mtu - 48);
186 d = packet_add_query_job(s, p, qj);
190 /* Try to fill up packet with more queries, if available */
191 for (qj = s->query_jobs; qj; qj = qj->jobs_next) {
196 if (!packet_add_query_job(s, p, qj))
202 flx_dns_packet_set_field(p, FLX_DNS_FIELD_QDCOUNT, n);
204 /* Now add known answers */
205 append_known_answers_and_send(s, p);
208 flxQueryJob* query_job_new(flxPacketScheduler *s, flxKey *key) {
214 qj = g_new(flxQueryJob, 1);
216 qj->key = flx_key_ref(key);
218 qj->time_event = NULL;
220 FLX_LLIST_PREPEND(flxQueryJob, jobs, s->query_jobs, qj);
225 void flx_packet_scheduler_post_query(flxPacketScheduler *s, flxKey *key, gboolean immediately) {
232 flx_elapse_time(&tv, immediately ? 0 : FLX_QUERY_DEFER_MSEC, 0);
234 for (qj = s->query_jobs; qj; qj = qj->jobs_next) {
236 if (flx_key_equal(qj->key, key)) {
238 glong d = flx_timeval_diff(&tv, &qj->delivery);
240 /* Duplicate questions suppression */
241 if (d >= 0 && d <= FLX_QUERY_HISTORY_MSEC*1000) {
242 g_message("WARNING! DUPLICATE QUERY SUPPRESSION ACTIVE!");
246 query_job_free(s, qj);
252 qj = query_job_new(s, key);
254 qj->time_event = flx_time_event_queue_add(s->server->time_event_queue, &qj->delivery, query_elapse, qj);
257 static guint8* packet_add_response_job(flxPacketScheduler *s, flxDnsPacket *p, flxResponseJob *rj) {
264 if ((d = flx_dns_packet_append_record(p, rj->record, rj->flush_cache))) {
269 /* Drop response after some time from history */
270 flx_elapse_time(&tv, FLX_RESPONSE_HISTORY_MSEC, 0);
271 flx_time_event_queue_update(s->server->time_event_queue, rj->time_event, &tv);
273 g_get_current_time(&rj->delivery);
279 static void send_response_packet(flxPacketScheduler *s, flxResponseJob *rj) {
285 p = flx_dns_packet_new_response(s->interface->hardware->mtu - 200);
288 /* If a job was specified, put it in the packet. */
291 d = packet_add_response_job(s, p, rj);
296 /* Try to fill up packet with more responses, if available */
297 for (rj = s->response_jobs; rj; rj = rj->jobs_next) {
302 if (!packet_add_response_job(s, p, rj))
308 flx_dns_packet_set_field(p, FLX_DNS_FIELD_ANCOUNT, n);
309 flx_interface_send_packet(s->interface, p);
310 flx_dns_packet_free(p);
313 static void response_elapse(flxTimeEvent *e, gpointer data) {
314 flxResponseJob *rj = data;
315 flxPacketScheduler *s;
321 /* Lets remove it from the history */
322 response_job_free(s, rj);
326 send_response_packet(s, rj);
329 static flxResponseJob* look_for_response(flxPacketScheduler *s, flxRecord *record) {
335 for (rj = s->response_jobs; rj; rj = rj->jobs_next)
336 if (flx_record_equal_no_ttl(rj->record, record))
342 static flxResponseJob* response_job_new(flxPacketScheduler *s, flxRecord *record) {
348 rj = g_new(flxResponseJob, 1);
350 rj->record = flx_record_ref(record);
352 rj->time_event = NULL;
353 rj->address_valid = FALSE;
354 rj->flush_cache = FALSE;
356 FLX_LLIST_PREPEND(flxResponseJob, jobs, s->response_jobs, rj);
361 void flx_packet_scheduler_post_response(flxPacketScheduler *s, const flxAddress *a, flxRecord *record, gboolean flush_cache, gboolean immediately) {
369 g_assert(!flx_key_is_pattern(record->key));
371 flx_elapse_time(&tv, immediately ? 0 : FLX_RESPONSE_DEFER_MSEC, immediately ? 0 : FLX_RESPONSE_JITTER_MSEC);
373 /* Don't send out duplicates */
375 if ((rj = look_for_response(s, record))) {
378 d = flx_timeval_diff(&tv, &rj->delivery);
380 /* If there's already a matching packet in our history or in
381 * the schedule, we do nothing. */
382 if (!!record->ttl == !!rj->record->ttl &&
383 d >= 0 && d <= FLX_RESPONSE_HISTORY_MSEC*1000) {
384 g_message("WARNING! DUPLICATE RESPONSE SUPPRESSION ACTIVE!");
386 /* This job is no longer specific to a single querier, so
387 * make sure it isn't suppressed by known answer
390 if (rj->address_valid && (!a || flx_address_cmp(a, &rj->address) != 0))
391 rj->address_valid = FALSE;
393 rj->flush_cache = flush_cache;
398 /* Either one was a goodbye packet, but the other was not, so
399 * let's drop the older one. */
400 response_job_free(s, rj);
403 g_message("ACCEPTED NEW RESPONSE [%s]", t = flx_record_to_string(record));
406 /* Create a new job and schedule it */
407 rj = response_job_new(s, record);
408 rj->flush_cache = flush_cache;
410 rj->time_event = flx_time_event_queue_add(s->server->time_event_queue, &rj->delivery, response_elapse, rj);
412 /* Store the address of the host this messages is intended to, so
413 that we can drop this job in case a truncated message with
414 known answer suppresion entries is recieved */
416 if ((rj->address_valid = !!a))
420 void flx_packet_scheduler_incoming_query(flxPacketScheduler *s, flxKey *key) {
427 /* This function is called whenever an incoming query was
428 * receieved. We drop all scheduled queries which match here. The
429 * keyword is "DUPLICATE QUESTION SUPPRESION". */
431 for (qj = s->query_jobs; qj; qj = qj->jobs_next)
432 if (flx_key_equal(qj->key, key)) {
441 /* No matching job was found. Add the query to the history */
442 qj = query_job_new(s, key);
447 /* Drop the query after some time */
448 flx_elapse_time(&tv, FLX_QUERY_HISTORY_MSEC, 0);
449 qj->time_event = flx_time_event_queue_add(s->server->time_event_queue, &tv, query_elapse, qj);
451 g_get_current_time(&qj->delivery);
454 void response_job_set_elapse_time(flxPacketScheduler *s, flxResponseJob *rj, guint msec, guint jitter) {
460 flx_elapse_time(&tv, msec, jitter);
463 flx_time_event_queue_update(s->server->time_event_queue, rj->time_event, &tv);
465 rj->time_event = flx_time_event_queue_add(s->server->time_event_queue, &tv, response_elapse, rj);
469 void flx_packet_scheduler_incoming_response(flxPacketScheduler *s, flxRecord *record) {
475 /* This function is called whenever an incoming response was
476 * receieved. We drop all scheduled responses which match
477 * here. The keyword is "DUPLICATE ANSWER SUPPRESION". */
479 for (rj = s->response_jobs; rj; rj = rj->jobs_next)
480 if (flx_record_equal_no_ttl(rj->record, record)) {
484 if (!!record->ttl == !!rj->record->ttl) {
485 /* An entry like this is already in our history,
486 * so let's get out of here! */
491 /* Either one was a goodbye packet but other was
492 * none. We remove the history entry, and add a
495 response_job_free(s, rj);
501 if (!!record->ttl == !!rj->record->ttl) {
503 /* The incoming packet matches our scheduled
504 * record, so let's mark that one as done */
510 /* Either one was a goodbye packet but other was
511 * none. We ignore the incoming packet. */
518 /* No matching job was found. Add the query to the history */
519 rj = response_job_new(s, record);
524 /* Drop response after 500ms from history */
525 response_job_set_elapse_time(s, rj, FLX_RESPONSE_HISTORY_MSEC, 0);
527 g_get_current_time(&rj->delivery);
530 void flx_packet_scheduler_incoming_known_answer(flxPacketScheduler *s, flxRecord *record, const flxAddress *a) {
537 for (rj = s->response_jobs; rj; rj = rj->jobs_next) {
539 g_assert(record->ttl > 0);
540 g_assert(rj->record->ttl/2);
542 if (flx_record_equal_no_ttl(rj->record, record))
543 if (rj->address_valid)
544 if (flx_address_cmp(&rj->address, a))
545 if (record->ttl >= rj->record->ttl/2) {
547 /* Let's suppress it */
549 response_job_free(s, rj);
555 void flx_packet_scheduler_flush_responses(flxPacketScheduler *s) {
560 /* Send all scheduled responses, ignoring the scheduled time */
562 for (rj = s->response_jobs; rj; rj = rj->jobs_next)
564 send_response_packet(s, rj);
567 static flxProbeJob* probe_job_new(flxPacketScheduler *s, flxRecord *record) {
573 pj = g_new(flxProbeJob, 1);
575 pj->record = flx_record_ref(record);
576 pj->time_event = NULL;
579 FLX_LLIST_PREPEND(flxProbeJob, jobs, s->probe_jobs, pj);
584 static guint8* packet_add_probe_query(flxPacketScheduler *s, flxDnsPacket *p, flxProbeJob *pj) {
593 g_assert(!pj->chosen);
595 /* Estimate the size for this record */
597 flx_key_get_estimate_size(pj->record->key) +
598 flx_record_get_estimate_size(pj->record);
601 if (size > flx_dns_packet_space(p))
604 /* Create the probe query */
605 k = flx_key_new(pj->record->key->name, pj->record->key->class, FLX_DNS_TYPE_ANY);
606 ret = flx_dns_packet_append_key(p, k);
609 /* Mark this job for addition to the packet */
612 /* Scan for more jobs whith matching key pattern */
613 for (pj = s->probe_jobs; pj; pj = pj->jobs_next) {
617 /* Does the record match the probe? */
618 if (k->class != pj->record->key->class || !flx_domain_equal(k->name, pj->record->key->name))
621 /* This job wouldn't fit in */
622 if (flx_record_get_estimate_size(pj->record) > flx_dns_packet_space(p))
625 /* Mark this job for addition to the packet */
634 static void probe_elapse(flxTimeEvent *e, gpointer data) {
635 flxProbeJob *pj = data, *next;
636 flxPacketScheduler *s;
644 p = flx_dns_packet_new_query(s->interface->hardware->mtu - 48);
646 /* Add the import probe */
647 if (!packet_add_probe_query(s, p, pj)) {
648 g_warning("Record too large! ---");
649 flx_dns_packet_free(p);
655 /* Try to fill up packet with more probes, if available */
656 for (pj = s->probe_jobs; pj; pj = pj->jobs_next) {
661 if (!packet_add_probe_query(s, p, pj))
667 flx_dns_packet_set_field(p, FLX_DNS_FIELD_QDCOUNT, n);
671 /* Now add the chosen records to the authorative section */
672 for (pj = s->probe_jobs; pj; pj = next) {
674 next = pj->jobs_next;
679 if (!flx_dns_packet_append_record(p, pj->record, TRUE)) {
680 g_warning("Bad probe size estimate!");
682 /* Unmark all following jobs */
683 for (; pj; pj = pj->jobs_next)
689 probe_job_free(s, pj);
694 flx_dns_packet_set_field(p, FLX_DNS_FIELD_NSCOUNT, n);
697 flx_interface_send_packet(s->interface, p);
698 flx_dns_packet_free(p);
701 void flx_packet_scheduler_post_probe(flxPacketScheduler *s, flxRecord *record, gboolean immediately) {
707 g_assert(!flx_key_is_pattern(record->key));
709 flx_elapse_time(&tv, immediately ? 0 : FLX_PROBE_DEFER_MSEC, 0);
711 /* Create a new job and schedule it */
712 pj = probe_job_new(s, record);
714 pj->time_event = flx_time_event_queue_add(s->server->time_event_queue, &pj->delivery, probe_elapse, pj);