X-Git-Url: http://git.meshlink.io/?a=blobdiff_plain;f=server.c;h=ecf44577ce2b45861d1cdd2d68359c684b452618;hb=d553a1c2d1cd3fcdd65ade64940b5bd3efc70675;hp=0aaaed1cb98bf762961732a52427bab7f3ffc408;hpb=ad1f9d3725a300f10eca071c6fe2f2c583f51436;p=catta diff --git a/server.c b/server.c index 0aaaed1..ecf4457 100644 --- a/server.c +++ b/server.c @@ -8,7 +8,7 @@ #include "util.h" #include "iface.h" #include "socket.h" - +#include "subscribe.h" static void handle_query_key(flxServer *s, flxKey *k, flxInterface *i, const flxAddress *a) { flxServerEntry *e; @@ -22,9 +22,24 @@ static void handle_query_key(flxServer *s, flxKey *k, flxInterface *i, const flx g_message("Handling query: %s", txt = flx_key_to_string(k)); g_free(txt); - for (e = g_hash_table_lookup(s->rrset_by_key, k); e; e = e->by_key_next) - if (flx_interface_match(i, e->interface, e->protocol)) - flx_interface_post_response(i, e->record); + flx_packet_scheduler_incoming_query(i->scheduler, k); + + if (k->type == FLX_DNS_TYPE_ANY) { + + /* Handle ANY query */ + + for (e = s->entries; e; e = e->entry_next) + if (flx_key_pattern_match(k, e->record->key)) + if (flx_interface_match(i, e->interface, e->protocol)) + flx_interface_post_response(i, e->record, FALSE); + } else { + + /* Handle all other queries */ + + for (e = g_hash_table_lookup(s->rrset_by_key, k); e; e = e->by_key_next) + if (flx_interface_match(i, e->interface, e->protocol)) + flx_interface_post_response(i, e->record, FALSE); + } } static void handle_query(flxServer *s, flxDnsPacket *p, flxInterface *i, const flxAddress *a) { @@ -67,12 +82,15 @@ static void handle_response(flxServer *s, flxDnsPacket *p, flxInterface *i, cons return; } - g_message("Handling response: %s", txt = flx_record_to_string(record)); - g_free(txt); - - flx_cache_update(i->cache, record, cache_flush, a); - flx_packet_scheduler_drop_response(i->scheduler, record); - flx_record_unref(record); + if (record->key->type != FLX_DNS_TYPE_ANY) { + g_message("Handling response: %s", txt = flx_record_to_string(record)); + g_free(txt); + + flx_cache_update(i->cache, record, cache_flush, a); + + flx_packet_scheduler_incoming_response(i->scheduler, record); + flx_record_unref(record); + } } } @@ -258,10 +276,13 @@ flxServer *flx_server_new(GMainContext *c) { s->context = g_main_context_default(); s->current_id = 1; + + FLX_LLIST_HEAD_INIT(flxServerEntry, s->entries); s->rrset_by_id = g_hash_table_new(g_int_hash, g_int_equal); s->rrset_by_key = g_hash_table_new((GHashFunc) flx_key_hash, (GEqualFunc) flx_key_equal); - FLX_LLIST_HEAD_INIT(flxServerEntry, s->entries); + FLX_LLIST_HEAD_INIT(flxSubscription, s->subscriptions); + s->subscription_hashtable = g_hash_table_new((GHashFunc) flx_key_hash, (GEqualFunc) flx_key_equal); s->monitor = flx_interface_monitor_new(s); s->time_event_queue = flx_time_event_queue_new(s->context); @@ -300,6 +321,10 @@ void flx_server_free(flxServer* s) { flx_interface_monitor_free(s->monitor); flx_server_remove(s, 0); + + while (s->subscriptions) + flx_subscription_free(s->subscriptions); + g_hash_table_destroy(s->subscription_hashtable); g_hash_table_destroy(s->rrset_by_id); g_hash_table_destroy(s->rrset_by_key); @@ -338,6 +363,8 @@ void flx_server_add( g_assert(s); g_assert(r); + g_assert(r->key->type != FLX_DNS_TYPE_ANY); + e = g_new(flxServerEntry, 1); e->record = flx_record_ref(r); e->id = id; @@ -532,60 +559,36 @@ void flx_server_add_text( flx_server_add_full(s, id, interface, protocol, unique, name, FLX_DNS_CLASS_IN, FLX_DNS_TYPE_TXT, buf, l+1, FLX_DEFAULT_TTL); } +static void post_query_callback(flxInterfaceMonitor *m, flxInterface *i, gpointer userdata) { + flxKey *k = userdata; + + g_assert(m); + g_assert(i); + g_assert(k); + + flx_interface_post_query(i, k, FALSE); +} + void flx_server_post_query(flxServer *s, gint interface, guchar protocol, flxKey *key) { g_assert(s); g_assert(key); - - if (interface > 0) { - if (protocol != AF_UNSPEC) { - flxInterface *i; - - if ((i = flx_interface_monitor_get_interface(s->monitor, interface, protocol))) - flx_interface_post_query(i, key); - } else { - flxHwInterface *hw; - flxInterface *i; - - if ((hw = flx_interface_monitor_get_hw_interface(s->monitor, interface))) - for (i = hw->interfaces; i; i = i->by_hardware_next) - if (flx_interface_match(i, interface, protocol)) - flx_interface_post_query(i, key); - } - - } else { - flxInterface *i; - - for (i = s->monitor->interfaces; i; i = i->interface_next) - if (flx_interface_match(i, interface, protocol)) - flx_interface_post_query(i, key); - } + + flx_interface_monitor_walk(s->monitor, interface, protocol, post_query_callback, key); +} + +static void post_response_callback(flxInterfaceMonitor *m, flxInterface *i, gpointer userdata) { + flxRecord *r = userdata; + + g_assert(m); + g_assert(i); + g_assert(r); + + flx_interface_post_response(i, r, FALSE); } void flx_server_post_response(flxServer *s, gint interface, guchar protocol, flxRecord *record) { g_assert(s); g_assert(record); - - if (interface > 0) { - if (protocol != AF_UNSPEC) { - flxInterface *i; - - if ((i = flx_interface_monitor_get_interface(s->monitor, interface, protocol))) - flx_interface_post_response(i, record); - } else { - flxHwInterface *hw; - flxInterface *i; - - if ((hw = flx_interface_monitor_get_hw_interface(s->monitor, interface))) - for (i = hw->interfaces; i; i = i->by_hardware_next) - if (flx_interface_match(i, interface, protocol)) - flx_interface_post_response(i, record); - } - - } else { - flxInterface *i; - - for (i = s->monitor->interfaces; i; i = i->interface_next) - if (flx_interface_match(i, interface, protocol)) - flx_interface_post_response(i, record); - } + + flx_interface_monitor_walk(s->monitor, interface, protocol, post_response_callback, record); }