From 36c53c0c4c5764f4a5f14b2b49b9f3c7ef8d9adb Mon Sep 17 00:00:00 2001 From: Lennart Poettering Date: Fri, 24 Jun 2005 19:38:29 +0000 Subject: [PATCH] * update default TTL generation to match RFC git-svn-id: file:///home/lennart/svn/public/avahi/trunk@145 941a03a8-eaeb-0310-b9a0-b1bbd8fe43fe --- avahi-common/rr.c | 8 ++++---- avahi-common/rr.h | 10 ++++++--- avahi-core/core.h | 4 ++++ avahi-core/dns.c | 4 +--- avahi-core/server.c | 49 ++++++++++++++++++++++++++------------------- 5 files changed, 44 insertions(+), 31 deletions(-) diff --git a/avahi-common/rr.c b/avahi-common/rr.c index 13160fc..b0c03a9 100644 --- a/avahi-common/rr.c +++ b/avahi-common/rr.c @@ -70,7 +70,7 @@ void avahi_key_unref(AvahiKey *k) { } } -AvahiRecord *avahi_record_new(AvahiKey *k) { +AvahiRecord *avahi_record_new(AvahiKey *k, guint32 ttl) { AvahiRecord *r; g_assert(k); @@ -81,19 +81,19 @@ AvahiRecord *avahi_record_new(AvahiKey *k) { memset(&r->data, 0, sizeof(r->data)); - r->ttl = AVAHI_DEFAULT_TTL; + r->ttl = ttl != (guint32) -1 ? ttl : AVAHI_DEFAULT_TTL; return r; } -AvahiRecord *avahi_record_new_full(const gchar *name, guint16 class, guint16 type) { +AvahiRecord *avahi_record_new_full(const gchar *name, guint16 class, guint16 type, guint32 ttl) { AvahiRecord *r; AvahiKey *k; g_assert(name); k = avahi_key_new(name, class, type); - r = avahi_record_new(k); + r = avahi_record_new(k, ttl); avahi_key_unref(k); return r; diff --git a/avahi-common/rr.h b/avahi-common/rr.h index da0b285..13e6a4c 100644 --- a/avahi-common/rr.h +++ b/avahi-common/rr.h @@ -48,7 +48,11 @@ enum { AVAHI_DNS_UNICAST_RESPONSE = 0x8000 }; -#define AVAHI_DEFAULT_TTL (120*60) +/** The default TTL for RRs which contain a host name of some kind. */ +#define AVAHI_DEFAULT_TTL_HOST_NAME (120) + +/** The default TTL for all other records. */ +#define AVAHI_DEFAULT_TTL (75*60) typedef struct { guint ref; @@ -112,8 +116,8 @@ gboolean avahi_key_is_pattern(const AvahiKey *k); guint avahi_key_hash(const AvahiKey *k); -AvahiRecord *avahi_record_new(AvahiKey *k); -AvahiRecord *avahi_record_new_full(const gchar *name, guint16 class, guint16 type); +AvahiRecord *avahi_record_new(AvahiKey *k, guint32 ttl); +AvahiRecord *avahi_record_new_full(const gchar *name, guint16 class, guint16 type, guint32 ttl); AvahiRecord *avahi_record_ref(AvahiRecord *r); void avahi_record_unref(AvahiRecord *r); diff --git a/avahi-core/core.h b/avahi-core/core.h index e0b5b5c..164e71a 100644 --- a/avahi-core/core.h +++ b/avahi-core/core.h @@ -201,6 +201,7 @@ gint avahi_server_add_ptr( AvahiIfIndex interface, AvahiProtocol protocol, AvahiEntryFlags flags, + guint32 ttl, const gchar *name, const gchar *dest); @@ -219,6 +220,7 @@ gint avahi_server_add_text( AvahiIfIndex interface, AvahiProtocol protocol, AvahiEntryFlags flags, + guint32 ttl, const gchar *name, ... /* text records, terminated by NULL */); @@ -228,6 +230,7 @@ gint avahi_server_add_text_va( AvahiIfIndex interface, AvahiProtocol protocol, AvahiEntryFlags flags, + guint32 ttl, const gchar *name, va_list va); @@ -237,6 +240,7 @@ gint avahi_server_add_text_strlst( AvahiIfIndex interface, AvahiProtocol protocol, AvahiEntryFlags flags, + guint32 ttl, const gchar *name, AvahiStringList *strlst); diff --git a/avahi-core/dns.c b/avahi-core/dns.c index 5addf53..6a7fd58 100644 --- a/avahi-core/dns.c +++ b/avahi-core/dns.c @@ -482,7 +482,7 @@ AvahiRecord* avahi_dns_packet_consume_record(AvahiDnsPacket *p, gboolean *ret_ca start = avahi_dns_packet_get_rptr(p); - r = avahi_record_new_full(name, class, type); + r = avahi_record_new_full(name, class, type, ttl); switch (type) { case AVAHI_DNS_TYPE_PTR: @@ -577,8 +577,6 @@ AvahiRecord* avahi_dns_packet_consume_record(AvahiDnsPacket *p, gboolean *ret_ca /* Check if we read enough data */ if ((guint8*) avahi_dns_packet_get_rptr(p) - (guint8*) start != rdlength) goto fail; - - r->ttl = ttl; return r; diff --git a/avahi-core/server.c b/avahi-core/server.c index 19badd1..62f4358 100644 --- a/avahi-core/server.c +++ b/avahi-core/server.c @@ -1140,7 +1140,7 @@ static void register_hinfo(AvahiServer *s) { s->hinfo_entry_group = avahi_entry_group_new(s, avahi_host_rr_entry_group_callback, NULL); /* Fill in HINFO rr */ - r = avahi_record_new_full(s->host_name_fqdn, AVAHI_DNS_CLASS_IN, AVAHI_DNS_TYPE_HINFO); + r = avahi_record_new_full(s->host_name_fqdn, AVAHI_DNS_CLASS_IN, AVAHI_DNS_TYPE_HINFO, AVAHI_DEFAULT_TTL_HOST_NAME); uname(&utsname); r->data.hinfo.cpu = g_strdup(g_strup(utsname.machine)); r->data.hinfo.os = g_strdup(g_strup(utsname.sysname)); @@ -1169,7 +1169,7 @@ static void register_browse_domain(AvahiServer *s) { return; s->browse_domain_entry_group = avahi_entry_group_new(s, NULL, NULL); - avahi_server_add_ptr(s, s->browse_domain_entry_group, AVAHI_IF_UNSPEC, AVAHI_PROTO_UNSPEC, 0, "b._dns-sd._udp.local", s->domain_name); + avahi_server_add_ptr(s, s->browse_domain_entry_group, AVAHI_IF_UNSPEC, AVAHI_PROTO_UNSPEC, 0, AVAHI_DEFAULT_TTL, "b._dns-sd._udp.local", s->domain_name); avahi_entry_group_commit(s->browse_domain_entry_group); } @@ -1471,6 +1471,9 @@ gint avahi_server_add( g_assert(s); g_assert(r); + if (r->ttl == 0) + return -1; + if (avahi_key_is_pattern(r->key)) return -1; @@ -1548,6 +1551,7 @@ gint avahi_server_add_ptr( gint interface, guchar protocol, AvahiEntryFlags flags, + guint32 ttl, const gchar *name, const gchar *dest) { @@ -1556,7 +1560,7 @@ gint avahi_server_add_ptr( g_assert(dest); - r = avahi_record_new_full(name ? name : s->host_name_fqdn, AVAHI_DNS_CLASS_IN, AVAHI_DNS_TYPE_PTR); + r = avahi_record_new_full(name ? name : s->host_name_fqdn, AVAHI_DNS_CLASS_IN, AVAHI_DNS_TYPE_PTR, ttl); r->data.ptr.name = avahi_normalize_name(dest); ret = avahi_server_add(s, g, interface, protocol, flags, r); avahi_record_unref(r); @@ -1583,30 +1587,30 @@ gint avahi_server_add_address( gchar *reverse; AvahiRecord *r; - r = avahi_record_new_full(name, AVAHI_DNS_CLASS_IN, AVAHI_DNS_TYPE_A); + r = avahi_record_new_full(name, AVAHI_DNS_CLASS_IN, AVAHI_DNS_TYPE_A, AVAHI_DEFAULT_TTL_HOST_NAME); r->data.a.address = a->data.ipv4; ret = avahi_server_add(s, g, interface, protocol, flags | AVAHI_ENTRY_UNIQUE | AVAHI_ENTRY_ALLOWMUTIPLE, r); avahi_record_unref(r); reverse = avahi_reverse_lookup_name_ipv4(&a->data.ipv4); - ret |= avahi_server_add_ptr(s, g, interface, protocol, flags | AVAHI_ENTRY_UNIQUE, reverse, name); + ret |= avahi_server_add_ptr(s, g, interface, protocol, flags | AVAHI_ENTRY_UNIQUE, AVAHI_DEFAULT_TTL_HOST_NAME, reverse, name); g_free(reverse); } else { gchar *reverse; AvahiRecord *r; - r = avahi_record_new_full(name, AVAHI_DNS_CLASS_IN, AVAHI_DNS_TYPE_AAAA); + r = avahi_record_new_full(name, AVAHI_DNS_CLASS_IN, AVAHI_DNS_TYPE_AAAA, AVAHI_DEFAULT_TTL_HOST_NAME); r->data.aaaa.address = a->data.ipv6; ret = avahi_server_add(s, g, interface, protocol, flags | AVAHI_ENTRY_UNIQUE | AVAHI_ENTRY_ALLOWMUTIPLE, r); avahi_record_unref(r); reverse = avahi_reverse_lookup_name_ipv6_arpa(&a->data.ipv6); - ret |= avahi_server_add_ptr(s, g, interface, protocol, flags | AVAHI_ENTRY_UNIQUE, reverse, name); + ret |= avahi_server_add_ptr(s, g, interface, protocol, flags | AVAHI_ENTRY_UNIQUE, AVAHI_DEFAULT_TTL_HOST_NAME, reverse, name); g_free(reverse); reverse = avahi_reverse_lookup_name_ipv6_int(&a->data.ipv6); - ret |= avahi_server_add_ptr(s, g, interface, protocol, flags | AVAHI_ENTRY_UNIQUE, reverse, name); + ret |= avahi_server_add_ptr(s, g, interface, protocol, flags | AVAHI_ENTRY_UNIQUE, AVAHI_DEFAULT_TTL_HOST_NAME, reverse, name); g_free(reverse); } @@ -1621,6 +1625,7 @@ gint avahi_server_add_text_strlst( gint interface, guchar protocol, AvahiEntryFlags flags, + guint32 ttl, const gchar *name, AvahiStringList *strlst) { @@ -1629,7 +1634,7 @@ gint avahi_server_add_text_strlst( g_assert(s); - r = avahi_record_new_full(name ? name : s->host_name_fqdn, AVAHI_DNS_CLASS_IN, AVAHI_DNS_TYPE_TXT); + r = avahi_record_new_full(name ? name : s->host_name_fqdn, AVAHI_DNS_CLASS_IN, AVAHI_DNS_TYPE_TXT, ttl); r->data.txt.string_list = strlst; ret = avahi_server_add(s, g, interface, protocol, flags, r); avahi_record_unref(r); @@ -1643,12 +1648,13 @@ gint avahi_server_add_text_va( gint interface, guchar protocol, AvahiEntryFlags flags, + guint32 ttl, const gchar *name, va_list va) { g_assert(s); - return avahi_server_add_text_strlst(s, g, interface, protocol, flags, name, avahi_string_list_new_va(va)); + return avahi_server_add_text_strlst(s, g, interface, protocol, flags, ttl, name, avahi_string_list_new_va(va)); } gint avahi_server_add_text( @@ -1657,6 +1663,7 @@ gint avahi_server_add_text( gint interface, guchar protocol, AvahiEntryFlags flags, + guint32 ttl, const gchar *name, ...) { @@ -1666,7 +1673,7 @@ gint avahi_server_add_text( g_assert(s); va_start(va, name); - ret = avahi_server_add_text_va(s, g, interface, protocol, flags, name, va); + ret = avahi_server_add_text_va(s, g, interface, protocol, flags, ttl, name, va); va_end(va); return ret; @@ -1728,9 +1735,9 @@ gint avahi_server_add_service_strlst( snprintf(ptr_name, sizeof(ptr_name), "%s.%s", type, domain); snprintf(svc_name, sizeof(svc_name), "%s.%s.%s", ename, type, domain); - ret = avahi_server_add_ptr(s, g, interface, protocol, AVAHI_ENTRY_NULL, ptr_name, svc_name); + ret = avahi_server_add_ptr(s, g, interface, protocol, AVAHI_ENTRY_NULL, AVAHI_DEFAULT_TTL, ptr_name, svc_name); - r = avahi_record_new_full(svc_name, AVAHI_DNS_CLASS_IN, AVAHI_DNS_TYPE_SRV); + r = avahi_record_new_full(svc_name, AVAHI_DNS_CLASS_IN, AVAHI_DNS_TYPE_SRV, AVAHI_DEFAULT_TTL_HOST_NAME); r->data.srv.priority = 0; r->data.srv.weight = 0; r->data.srv.port = port; @@ -1738,10 +1745,10 @@ gint avahi_server_add_service_strlst( ret |= avahi_server_add(s, g, interface, protocol, AVAHI_ENTRY_UNIQUE, r); avahi_record_unref(r); - ret |= avahi_server_add_text_strlst(s, g, interface, protocol, AVAHI_ENTRY_UNIQUE, svc_name, strlst); + ret |= avahi_server_add_text_strlst(s, g, interface, protocol, AVAHI_ENTRY_UNIQUE, AVAHI_DEFAULT_TTL, svc_name, strlst); snprintf(enum_ptr, sizeof(enum_ptr), "_services._dns-sd._udp.%s", domain); - ret |=avahi_server_add_ptr(s, g, interface, protocol, AVAHI_ENTRY_NULL, enum_ptr, ptr_name); + ret |=avahi_server_add_ptr(s, g, interface, protocol, AVAHI_ENTRY_NULL, AVAHI_DEFAULT_TTL, enum_ptr, ptr_name); return ret; } @@ -1824,7 +1831,7 @@ gint avahi_server_add_dns_server_address( AvahiRecord *r; gint ret; - gchar n[64] = "ip"; + gchar n[64] = "ip-"; g_assert(s); g_assert(address); @@ -1832,12 +1839,12 @@ gint avahi_server_add_dns_server_address( g_assert(address->family == AVAHI_PROTO_INET || address->family == AVAHI_PROTO_INET6); if (address->family == AVAHI_PROTO_INET) { - hexstring(n+2, sizeof(n)-2, &address->data, 4); - r = avahi_record_new_full(n, AVAHI_DNS_CLASS_IN, AVAHI_DNS_TYPE_A); + hexstring(n+3, sizeof(n)-3, &address->data, 4); + r = avahi_record_new_full(n, AVAHI_DNS_CLASS_IN, AVAHI_DNS_TYPE_A, AVAHI_DEFAULT_TTL_HOST_NAME); r->data.a.address = address->data.ipv4; } else { - hexstring(n+2, sizeof(n)-2, &address->data, 6); - r = avahi_record_new_full(n, AVAHI_DNS_CLASS_IN, AVAHI_DNS_TYPE_AAAA); + hexstring(n+3, sizeof(n)-3, &address->data, 6); + r = avahi_record_new_full(n, AVAHI_DNS_CLASS_IN, AVAHI_DNS_TYPE_AAAA, AVAHI_DEFAULT_TTL_HOST_NAME); r->data.aaaa.address = address->data.ipv6; } @@ -1875,7 +1882,7 @@ gint avahi_server_add_dns_server_name( snprintf(t, sizeof(t), "%s.%s", type == AVAHI_DNS_SERVER_RESOLVE ? "_domain._udp" : "_dns-update._udp", domain); - r = avahi_record_new_full(t, AVAHI_DNS_CLASS_IN, AVAHI_DNS_TYPE_SRV); + r = avahi_record_new_full(t, AVAHI_DNS_CLASS_IN, AVAHI_DNS_TYPE_SRV, AVAHI_DEFAULT_TTL_HOST_NAME); r->data.srv.priority = 0; r->data.srv.weight = 0; r->data.srv.port = port; -- 2.39.5