From: Lennart Poettering Date: Tue, 22 Mar 2005 18:01:38 +0000 (+0000) Subject: make the daemon response to queries X-Git-Url: https://git.meshlink.io/?a=commitdiff_plain;h=54247ce93dc6f048c0ffea546f454653ce0e25a8;p=catta make the daemon response to queries git-svn-id: file:///home/lennart/svn/public/avahi/trunk@13 941a03a8-eaeb-0310-b9a0-b1bbd8fe43fe --- diff --git a/dns.c b/dns.c index 5c2180d..a787723 100644 --- a/dns.c +++ b/dns.c @@ -78,6 +78,31 @@ guint8* flx_dns_packet_append_uint16(flxDnsPacket *p, guint16 v) { return d; } +guint8 *flx_dns_packet_append_uint32(flxDnsPacket *p, guint32 v) { + guint8 *d; + + g_assert(p); + d = flx_dns_packet_extend(p, sizeof(guint32)); + *((guint32*) d) = g_htonl(v); + + return d; +} + +guint8 *flx_dns_packet_append_bytes(flxDnsPacket *p, gconstpointer b, guint l) { + guint8* d; + + g_assert(p); + g_assert(b); + g_assert(l); + + d = flx_dns_packet_extend(p, l); + g_assert(d); + memcpy(d, b, l); + + return d; +} + + guint8 *flx_dns_packet_extend(flxDnsPacket *p, guint l) { guint8 *d; @@ -321,6 +346,18 @@ guint8* flx_dns_packet_append_key(flxDnsPacket *p, flxKey *k) { } guint8* flx_dns_packet_append_record(flxDnsPacket *p, flxRecord *r, gboolean cache_flush) { + guint8 *t; - + g_assert(p); + g_assert(r); + + if (!(t = flx_dns_packet_append_name(p, r->key->name)) || + !flx_dns_packet_append_uint16(p, r->key->type) || + !flx_dns_packet_append_uint16(p, cache_flush ? (r->key->class | MDNS_CACHE_FLUSH) : (r->key->class &~ MDNS_CACHE_FLUSH)) || + !flx_dns_packet_append_uint32(p, r->ttl) || + !flx_dns_packet_append_uint16(p, r->size) || + !flx_dns_packet_append_bytes(p, r->data, r->size)) + return NULL; + + return t; } diff --git a/dns.h b/dns.h index a737536..bdb0f07 100644 --- a/dns.h +++ b/dns.h @@ -17,25 +17,29 @@ void flx_dns_packet_free(flxDnsPacket *p); void flx_dns_packet_set_field(flxDnsPacket *p, guint index, guint16 v); guint16 flx_dns_packet_get_field(flxDnsPacket *p, guint index); +guint8 *flx_dns_packet_extend(flxDnsPacket *p, guint l); + guint8 *flx_dns_packet_append_uint16(flxDnsPacket *p, guint16 v); +guint8 *flx_dns_packet_append_uint32(flxDnsPacket *p, guint32 v); guint8 *flx_dns_packet_append_name(flxDnsPacket *p, const gchar *name); guint8 *flx_dns_packet_append_name_compressed(flxDnsPacket *p, const gchar *name, guint8 *prev); -guint8 *flx_dns_packet_extend(flxDnsPacket *p, guint l); +guint8 *flx_dns_packet_append_bytes(flxDnsPacket *p, gconstpointer, guint l); +guint8* flx_dns_packet_append_key(flxDnsPacket *p, flxKey *k); +guint8* flx_dns_packet_append_record(flxDnsPacket *p, flxRecord *r, gboolean cache_flush); + gint flx_dns_packet_is_query(flxDnsPacket *p); gint flx_dns_packet_check_valid(flxDnsPacket *p); -gint flx_dns_packet_consume_name(flxDnsPacket *p, gchar *ret_name, guint l); gint flx_dns_packet_consume_uint16(flxDnsPacket *p, guint16 *ret_v); gint flx_dns_packet_consume_uint32(flxDnsPacket *p, guint32 *ret_v); +gint flx_dns_packet_consume_name(flxDnsPacket *p, gchar *ret_name, guint l); gint flx_dns_packet_consume_bytes(flxDnsPacket *p, gpointer ret_data, guint l); +flxKey* flx_dns_packet_consume_key(flxDnsPacket *p); +flxRecord* flx_dns_packet_consume_record(flxDnsPacket *p, gboolean *ret_cache_flush); gconstpointer flx_dns_packet_get_rptr(flxDnsPacket *p); -flxKey* flx_dns_packet_consume_key(flxDnsPacket *p); -flxRecord* flx_dns_packet_consume_record(flxDnsPacket *p, gboolean *ret_cache_flush); -guint8* flx_dns_packet_append_key(flxDnsPacket *p, flxKey *k); -guint8* flx_dns_packet_append_record(flxDnsPacket *p, flxRecord *r, gboolean cache_flush); gint flx_dns_packet_skip(flxDnsPacket *p, guint length); diff --git a/iface.c b/iface.c index 8c31d8f..9acef47 100644 --- a/iface.c +++ b/iface.c @@ -344,20 +344,10 @@ int flx_address_is_relevant(flxInterfaceAddress *a) { flx_interface_is_relevant(a->interface); } -void flx_interface_send_query(flxInterface *i, guchar protocol, flxKey *k) { - flxDnsPacket *p; +void flx_interface_send_packet(flxInterface *i, guchar protocol, flxDnsPacket *p) { g_assert(i); - g_assert(k); - - p = flx_dns_packet_new(); - flx_dns_packet_set_field(p, DNS_FIELD_FLAGS, DNS_FLAGS(0, 0, 0, 0, 0, 0, 0, 0, 0, 0)); - - flx_dns_packet_append_name(p, k->name); - flx_dns_packet_append_uint16(p, k->type); - flx_dns_packet_append_uint16(p, k->class); - - flx_dns_packet_set_field(p, DNS_FIELD_QDCOUNT, 1); - + g_assert(p); + if ((protocol == AF_INET || protocol == AF_UNSPEC) && i->n_ipv4_addrs > 0 && flx_interface_is_relevant(i)) { g_message("sending on '%s':IPv4", i->name); flx_send_dns_packet_ipv4(i->monitor->server->fd_ipv4, i->index, p); @@ -367,27 +357,36 @@ void flx_interface_send_query(flxInterface *i, guchar protocol, flxKey *k) { g_message("sending on '%s':IPv6", i->name); flx_send_dns_packet_ipv6(i->monitor->server->fd_ipv6, i->index, p); } +} + +void flx_interface_send_query(flxInterface *i, guchar protocol, flxKey *k) { + flxDnsPacket *p; + g_assert(i); + g_assert(k); + + p = flx_dns_packet_new(); + flx_dns_packet_set_field(p, DNS_FIELD_FLAGS, DNS_FLAGS(0, 0, 0, 0, 0, 0, 0, 0, 0, 0)); + flx_dns_packet_append_key(p, k); + flx_dns_packet_set_field(p, DNS_FIELD_QDCOUNT, 1); + flx_interface_send_packet(i, protocol, p); flx_dns_packet_free(p); } -void flx_interface_send_response(flxinterface *i, guchar protocol, flxRecord *rr) { - flxDnsPacket+p; +void flx_interface_send_response(flxInterface *i, guchar protocol, flxRecord *rr) { + flxDnsPacket *p; g_assert(i); g_assert(rr); p = flx_dns_packet_new(); flx_dns_packet_set_field(p, DNS_FIELD_FLAGS, DNS_FLAGS(1, 0, 0, 0, 0, 0, 0, 0, 0, 0)); - - flx_dns_packet_append_name(p, rr->key->name); - flx_dns_packet_append_uint16(p, rr->key->type); - flx_dns_packet_append_uint16(p, rr->key->class); - flx_dns_packet_append_uint16 + flx_dns_packet_append_record(p, rr, FALSE); + flx_dns_packet_set_field(p, DNS_FIELD_ANCOUNT, 1); + flx_interface_send_packet(i, protocol, p); + flx_dns_packet_free(p); } - - void flx_dump_caches(flxServer *s, FILE *f) { flxInterface *i; g_assert(s); diff --git a/iface.h b/iface.h index 207c0b2..2adab72 100644 --- a/iface.h +++ b/iface.h @@ -65,6 +65,7 @@ int flx_interface_is_relevant(flxInterface *i); int flx_address_is_relevant(flxInterfaceAddress *a); void flx_interface_send_query(flxInterface *i, guchar protocol, flxKey *k); +void flx_interface_send_response(flxInterface *i, guchar protocol, flxRecord *rr); void flx_dump_caches(flxServer *s, FILE *f); diff --git a/main.c b/main.c index 75d03ac..94d78ac 100644 --- a/main.c +++ b/main.c @@ -14,6 +14,9 @@ static gboolean send_timeout(gpointer data) { flxServer *flx = data; flxKey *k; + flx_server_dump(flx, stdout); + + k = flx_key_new("cocaine.local.", FLX_DNS_CLASS_IN, FLX_DNS_TYPE_A); flx_server_send_query(flx, 0, AF_UNSPEC, k); flx_key_unref(k); @@ -32,14 +35,13 @@ int main(int argc, char *argv[]) { loop = g_main_loop_new(NULL, FALSE); - g_timeout_add(1000*5, quit_timeout, loop); + /*g_timeout_add(1000*5, quit_timeout, loop);*/ g_timeout_add(1000, send_timeout, flx); g_main_loop_run(loop); g_main_loop_unref(loop); - flx_server_dump(flx, stdout); flx_server_free(flx); diff --git a/server.c b/server.c index d4dd4ac..6027f5f 100644 --- a/server.c +++ b/server.c @@ -10,6 +10,16 @@ #include "socket.h" static void post_response(flxServer *s, flxRecord *r, gint iface, const flxAddress *a) { + flxInterface *i; + + g_assert(s); + g_assert(r); + g_assert(iface > 0); + g_assert(a); + + if ((i = flx_interface_monitor_get_interface(s->monitor, iface))) + flx_interface_send_response(i, a->family, r); + } static void handle_query_key(flxServer *s, flxKey *k, gint iface, const flxAddress *a) { @@ -19,11 +29,13 @@ static void handle_query_key(flxServer *s, flxKey *k, gint iface, const flxAddre g_assert(k); g_assert(a); - for (e = g_hash_table_lookup(s->rrset_by_name, k); e; e = e->next_by_name) { + for (e = g_hash_table_lookup(s->rrset_by_name, k); e; e = e->by_name_next) { if ((e->interface <= 0 || e->interface == iface) && - (e->protocol == AF_UNSPEC || e->protocol == a->family)) + (e->protocol == AF_UNSPEC || e->protocol == a->family)) { post_response(s, e->record, iface, a); + + } } } @@ -38,7 +50,7 @@ static void handle_query(flxServer *s, flxDnsPacket *p, gint iface, const flxAdd flxKey *key; - if (!(key = flx_dns_packet_consume_query(p))) { + if (!(key = flx_dns_packet_consume_key(p))) { g_warning("Packet too short"); return; } @@ -59,7 +71,7 @@ static void add_response_to_cache(flxCache *c, flxDnsPacket *p, const flxAddress flxRecord *rr; gboolean cache_flush = FALSE; - if (!(rr = flx_dns_packet_consume_rr(p, &cache_flush))) { + if (!(rr = flx_dns_packet_consume_record(p, &cache_flush))) { g_warning("Packet too short"); return; } @@ -78,6 +90,8 @@ static void dispatch_packet(flxServer *s, flxDnsPacket *p, struct sockaddr *sa, g_assert(sa); g_assert(iface > 0); + g_message("new packet recieved."); + if (!(i = flx_interface_monitor_get_interface(s->monitor, iface))) { g_warning("Recieved packet from invalid interface."); return; @@ -112,7 +126,7 @@ static void dispatch_packet(flxServer *s, flxDnsPacket *p, struct sockaddr *sa, if (flx_dns_packet_is_query(p)) { if (flx_dns_packet_get_field(p, DNS_FIELD_QDCOUNT) == 0 || - flx_dns_packet_get_field(p, DNS_FIELD_ARCOUNT) != 0 + flx_dns_packet_get_field(p, DNS_FIELD_ARCOUNT) != 0 || flx_dns_packet_get_field(p, DNS_FIELD_NSCOUNT) != 0) { g_warning("Invalid query packet."); return; diff --git a/socket.c b/socket.c index 48cfb36..1887e1d 100644 --- a/socket.c +++ b/socket.c @@ -29,7 +29,7 @@ static void mdns_mcast_group_ipv4(struct sockaddr_in *ret_sa) { gint flx_open_socket_ipv4(void) { struct ip_mreqn mreq; struct sockaddr_in sa, local; - int fd = -1, ttl, yes, no; + int fd = -1, ttl, yes; mdns_mcast_group_ipv4(&sa); @@ -56,8 +56,8 @@ gint flx_open_socket_ipv4(void) { goto fail; } - no = 0; - if (setsockopt(fd, SOL_IP, IP_MULTICAST_LOOP, &no, sizeof(no)) < 0) { + yes = 1; + if (setsockopt(fd, SOL_IP, IP_MULTICAST_LOOP, &yes, sizeof(yes)) < 0) { g_warning("IP_MULTICAST_LOOP failed: %s\n", strerror(errno)); goto fail; } @@ -127,7 +127,7 @@ static void mdns_mcast_group_ipv6(struct sockaddr_in6 *ret_sa) { gint flx_open_socket_ipv6(void) { struct ipv6_mreq mreq; struct sockaddr_in6 sa, local; - int fd = -1, ttl, yes, no; + int fd = -1, ttl, yes; mdns_mcast_group_ipv6(&sa); @@ -160,8 +160,8 @@ gint flx_open_socket_ipv6(void) { goto fail; } - no = 0; - if (setsockopt(fd, SOL_IPV6, IPV6_MULTICAST_LOOP, &no, sizeof(no)) < 0) { + yes = 1; + if (setsockopt(fd, SOL_IPV6, IPV6_MULTICAST_LOOP, &yes, sizeof(yes)) < 0) { g_warning("IPV6_MULTICAST_LOOP failed: %s\n", strerror(errno)); goto fail; }