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 100
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 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);
250 qj = query_job_new(s, key);
252 qj->time_event = flx_time_event_queue_add(s->server->time_event_queue, &qj->delivery, query_elapse, qj);
255 static guint8* packet_add_response_job(flxPacketScheduler *s, flxDnsPacket *p, flxResponseJob *rj) {
262 if ((d = flx_dns_packet_append_record(p, rj->record, rj->flush_cache))) {
267 /* Drop response after some time from history */
268 flx_elapse_time(&tv, FLX_RESPONSE_HISTORY_MSEC, 0);
269 flx_time_event_queue_update(s->server->time_event_queue, rj->time_event, &tv);
271 g_get_current_time(&rj->delivery);
277 static void send_response_packet(flxPacketScheduler *s, flxResponseJob *rj) {
283 p = flx_dns_packet_new_response(s->interface->hardware->mtu - 200);
286 /* If a job was specified, put it in the packet. */
289 d = packet_add_response_job(s, p, rj);
294 /* Try to fill up packet with more responses, if available */
295 for (rj = s->response_jobs; rj; rj = rj->jobs_next) {
300 if (!packet_add_response_job(s, p, rj))
306 flx_dns_packet_set_field(p, FLX_DNS_FIELD_ANCOUNT, n);
307 flx_interface_send_packet(s->interface, p);
308 flx_dns_packet_free(p);
311 static void response_elapse(flxTimeEvent *e, gpointer data) {
312 flxResponseJob *rj = data;
313 flxPacketScheduler *s;
319 /* Lets remove it from the history */
320 response_job_free(s, rj);
324 send_response_packet(s, rj);
327 static flxResponseJob* look_for_response(flxPacketScheduler *s, flxRecord *record) {
333 for (rj = s->response_jobs; rj; rj = rj->jobs_next)
334 if (flx_record_equal_no_ttl(rj->record, record))
340 static flxResponseJob* response_job_new(flxPacketScheduler *s, flxRecord *record) {
346 rj = g_new(flxResponseJob, 1);
348 rj->record = flx_record_ref(record);
350 rj->time_event = NULL;
351 rj->address_valid = FALSE;
352 rj->flush_cache = FALSE;
354 FLX_LLIST_PREPEND(flxResponseJob, jobs, s->response_jobs, rj);
359 void flx_packet_scheduler_post_response(flxPacketScheduler *s, const flxAddress *a, flxRecord *record, gboolean flush_cache, gboolean immediately) {
367 g_assert(!flx_key_is_pattern(record->key));
369 flx_elapse_time(&tv, immediately ? 0 : FLX_RESPONSE_DEFER_MSEC, immediately ? 0 : FLX_RESPONSE_JITTER_MSEC);
371 /* Don't send out duplicates */
373 if ((rj = look_for_response(s, record))) {
376 d = flx_timeval_diff(&tv, &rj->delivery);
378 /* If there's already a matching packet in our history or in
379 * the schedule, we do nothing. */
380 if (!!record->ttl == !!rj->record->ttl &&
381 d >= 0 && d <= FLX_RESPONSE_HISTORY_MSEC*1000) {
382 g_message("WARNING! DUPLICATE RESPONSE SUPPRESSION ACTIVE!");
384 /* This job is no longer specific to a single querier, so
385 * make sure it isn't suppressed by known answer
388 if (rj->address_valid && (!a || flx_address_cmp(a, &rj->address) != 0))
389 rj->address_valid = FALSE;
391 rj->flush_cache = flush_cache;
396 /* Either one was a goodbye packet, but the other was not, so
397 * let's drop the older one. */
398 response_job_free(s, rj);
401 g_message("ACCEPTED NEW RESPONSE [%s]", t = flx_record_to_string(record));
404 /* Create a new job and schedule it */
405 rj = response_job_new(s, record);
406 rj->flush_cache = flush_cache;
408 rj->time_event = flx_time_event_queue_add(s->server->time_event_queue, &rj->delivery, response_elapse, rj);
410 /* Store the address of the host this messages is intended to, so
411 that we can drop this job in case a truncated message with
412 known answer suppresion entries is recieved */
414 if ((rj->address_valid = !!a))
418 void flx_packet_scheduler_incoming_query(flxPacketScheduler *s, flxKey *key) {
425 /* This function is called whenever an incoming query was
426 * receieved. We drop all scheduled queries which match here. The
427 * keyword is "DUPLICATE QUESTION SUPPRESION". */
429 for (qj = s->query_jobs; qj; qj = qj->jobs_next)
430 if (flx_key_equal(qj->key, key)) {
439 /* No matching job was found. Add the query to the history */
440 qj = query_job_new(s, key);
445 /* Drop the query after some time */
446 flx_elapse_time(&tv, FLX_QUERY_HISTORY_MSEC, 0);
447 qj->time_event = flx_time_event_queue_add(s->server->time_event_queue, &tv, query_elapse, qj);
449 g_get_current_time(&qj->delivery);
452 void response_job_set_elapse_time(flxPacketScheduler *s, flxResponseJob *rj, guint msec, guint jitter) {
458 flx_elapse_time(&tv, msec, jitter);
461 flx_time_event_queue_update(s->server->time_event_queue, rj->time_event, &tv);
463 rj->time_event = flx_time_event_queue_add(s->server->time_event_queue, &tv, response_elapse, rj);
467 void flx_packet_scheduler_incoming_response(flxPacketScheduler *s, flxRecord *record) {
473 /* This function is called whenever an incoming response was
474 * receieved. We drop all scheduled responses which match
475 * here. The keyword is "DUPLICATE ANSWER SUPPRESION". */
477 for (rj = s->response_jobs; rj; rj = rj->jobs_next)
478 if (flx_record_equal_no_ttl(rj->record, record)) {
482 if (!!record->ttl == !!rj->record->ttl) {
483 /* An entry like this is already in our history,
484 * so let's get out of here! */
489 /* Either one was a goodbye packet but other was
490 * none. We remove the history entry, and add a
493 response_job_free(s, rj);
499 if (!!record->ttl == !!rj->record->ttl) {
501 /* The incoming packet matches our scheduled
502 * record, so let's mark that one as done */
508 /* Either one was a goodbye packet but other was
509 * none. We ignore the incoming packet. */
516 /* No matching job was found. Add the query to the history */
517 rj = response_job_new(s, record);
522 /* Drop response after 500ms from history */
523 response_job_set_elapse_time(s, rj, FLX_RESPONSE_HISTORY_MSEC, 0);
525 g_get_current_time(&rj->delivery);
528 void flx_packet_scheduler_incoming_known_answer(flxPacketScheduler *s, flxRecord *record, const flxAddress *a) {
535 for (rj = s->response_jobs; rj; rj = rj->jobs_next) {
537 g_assert(record->ttl > 0);
538 g_assert(rj->record->ttl/2);
540 if (flx_record_equal_no_ttl(rj->record, record))
541 if (rj->address_valid)
542 if (flx_address_cmp(&rj->address, a))
543 if (record->ttl >= rj->record->ttl/2) {
545 /* Let's suppress it */
547 response_job_free(s, rj);
553 void flx_packet_scheduler_flush_responses(flxPacketScheduler *s) {
558 /* Send all scheduled responses, ignoring the scheduled time */
560 for (rj = s->response_jobs; rj; rj = rj->jobs_next)
562 send_response_packet(s, rj);
565 static flxProbeJob* probe_job_new(flxPacketScheduler *s, flxRecord *record) {
571 pj = g_new(flxProbeJob, 1);
573 pj->record = flx_record_ref(record);
574 pj->time_event = NULL;
577 FLX_LLIST_PREPEND(flxProbeJob, jobs, s->probe_jobs, pj);
582 static guint8* packet_add_probe_query(flxPacketScheduler *s, flxDnsPacket *p, flxProbeJob *pj) {
591 g_assert(!pj->chosen);
593 /* Estimate the size for this record */
595 flx_key_get_estimate_size(pj->record->key) +
596 flx_record_get_estimate_size(pj->record);
599 if (size > flx_dns_packet_space(p))
602 /* Create the probe query */
603 k = flx_key_new(pj->record->key->name, pj->record->key->class, FLX_DNS_TYPE_ANY);
604 ret = flx_dns_packet_append_key(p, k);
607 /* Mark this job for addition to the packet */
610 /* Scan for more jobs whith matching key pattern */
611 for (pj = s->probe_jobs; pj; pj = pj->jobs_next) {
615 /* Does the record match the probe? */
616 if (k->class != pj->record->key->class || flx_domain_equal(k->name, pj->record->key->name))
619 /* This job wouldn't fit in */
620 if (flx_record_get_estimate_size(pj->record) > flx_dns_packet_space(p))
623 /* Mark this job for addition to the packet */
632 static void probe_elapse(flxTimeEvent *e, gpointer data) {
633 flxProbeJob *pj = data, *next;
634 flxPacketScheduler *s;
642 p = flx_dns_packet_new_query(s->interface->hardware->mtu - 48);
644 /* Add the import probe */
645 if (!packet_add_probe_query(s, p, pj)) {
646 g_warning("Record too large! ---");
647 flx_dns_packet_free(p);
653 /* Try to fill up packet with more probes, if available */
654 for (pj = s->probe_jobs; pj; pj = pj->jobs_next) {
659 if (!packet_add_probe_query(s, p, pj))
665 flx_dns_packet_set_field(p, FLX_DNS_FIELD_QDCOUNT, n);
669 /* Now add the chosen records to the authorative section */
670 for (pj = s->probe_jobs; pj; pj = next) {
672 next = pj->jobs_next;
677 if (!flx_dns_packet_append_record(p, pj->record, TRUE)) {
678 g_warning("Bad probe size estimate!");
680 /* Unmark all following jobs */
681 for (; pj; pj = pj->jobs_next)
687 probe_job_free(s, pj);
691 flx_dns_packet_set_field(p, FLX_DNS_FIELD_NSCOUNT, n);
694 flx_interface_send_packet(s->interface, p);
695 flx_dns_packet_free(p);
698 void flx_packet_scheduler_post_probe(flxPacketScheduler *s, flxRecord *record, gboolean immediately) {
704 g_assert(!flx_key_is_pattern(record->key));
706 flx_elapse_time(&tv, immediately ? 0 : FLX_PROBE_DEFER_MSEC, 0);
708 /* No duplication check here... */
709 /* Create a new job and schedule it */
710 pj = probe_job_new(s, record);
712 pj->time_event = flx_time_event_queue_add(s->server->time_event_queue, &pj->delivery, probe_elapse, pj);