]> git.meshlink.io Git - catta/blobdiff - avahi-core/probe-sched.c
forgot to pull the publish_no_reverse change to the example.
[catta] / avahi-core / probe-sched.c
index a375c7beeb217c5a3052ca30958bf1e7513f3adf..1e63411ce167fb585ee5f9a8bf2de35260e5c666 100644 (file)
@@ -1,18 +1,16 @@
-/* $Id$ */
-
 /***
   This file is part of avahi.
+
   avahi is free software; you can redistribute it and/or modify it
   under the terms of the GNU Lesser General Public License as
   published by the Free Software Foundation; either version 2.1 of the
   License, or (at your option) any later version.
+
   avahi is distributed in the hope that it will be useful, but WITHOUT
   ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
   or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General
   Public License for more details.
+
   You should have received a copy of the GNU Lesser General Public
   License along with avahi; if not, write to the Free Software
   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
 #include <config.h>
 #endif
 
+#include <stdlib.h>
+
 #include <avahi-common/domain.h>
 #include <avahi-common/timeval.h>
+#include <avahi-common/malloc.h>
 
 #include "probe-sched.h"
 #include "log.h"
+#include "rr-util.h"
 
 #define AVAHI_PROBE_HISTORY_MSEC 150
 #define AVAHI_PROBE_DEFER_MSEC 50
@@ -37,13 +39,13 @@ typedef struct AvahiProbeJob AvahiProbeJob;
 struct AvahiProbeJob {
     AvahiProbeScheduler *scheduler;
     AvahiTimeEvent *time_event;
-    
-    gboolean chosen; /* Use for packet assembling */
-    gboolean done;
+
+    int chosen; /* Use for packet assembling */
+    int done;
     struct timeval delivery;
 
     AvahiRecord *record;
-    
+
     AVAHI_LLIST_FIELDS(AvahiProbeJob, jobs);
 };
 
@@ -55,17 +57,21 @@ struct AvahiProbeScheduler {
     AVAHI_LLIST_HEAD(AvahiProbeJob, history);
 };
 
-static AvahiProbeJob* job_new(AvahiProbeScheduler *s, AvahiRecord *record, gboolean done) {
+static AvahiProbeJob* job_new(AvahiProbeScheduler *s, AvahiRecord *record, int done) {
     AvahiProbeJob *pj;
-    
-    g_assert(s);
-    g_assert(record);
 
-    pj = g_new(AvahiProbeJob, 1);
+    assert(s);
+    assert(record);
+
+    if (!(pj = avahi_new(AvahiProbeJob, 1))) {
+        avahi_log_error(__FILE__": Out of memory");
+        return NULL; /* OOM */
+    }
+
     pj->scheduler = s;
     pj->record = avahi_record_ref(record);
     pj->time_event = NULL;
-    pj->chosen = FALSE;
+    pj->chosen = 0;
 
     if ((pj->done = done))
         AVAHI_LLIST_PREPEND(AvahiProbeJob, jobs, s->history, pj);
@@ -76,10 +82,10 @@ static AvahiProbeJob* job_new(AvahiProbeScheduler *s, AvahiRecord *record, gbool
 }
 
 static void job_free(AvahiProbeScheduler *s, AvahiProbeJob *pj) {
-    g_assert(pj);
+    assert(pj);
 
     if (pj->time_event)
-        avahi_time_event_queue_remove(s->time_event_queue, pj->time_event);
+        avahi_time_event_free(pj->time_event);
 
     if (pj->done)
         AVAHI_LLIST_REMOVE(AvahiProbeJob, jobs, s->history, pj);
@@ -87,35 +93,35 @@ static void job_free(AvahiProbeScheduler *s, AvahiProbeJob *pj) {
         AVAHI_LLIST_REMOVE(AvahiProbeJob, jobs, s->jobs, pj);
 
     avahi_record_unref(pj->record);
-    g_free(pj);
+    avahi_free(pj);
 }
 
-static void elapse_callback(AvahiTimeEvent *e, gpointer data);
+static void elapse_callback(AvahiTimeEvent *e, void* data);
 
-static void job_set_elapse_time(AvahiProbeScheduler *s, AvahiProbeJob *pj, guint msec, guint jitter) {
+static void job_set_elapse_time(AvahiProbeScheduler *s, AvahiProbeJob *pj, unsigned msec, unsigned jitter) {
     struct timeval tv;
 
-    g_assert(s);
-    g_assert(pj);
+    assert(s);
+    assert(pj);
 
     avahi_elapse_time(&tv, msec, jitter);
 
     if (pj->time_event)
-        avahi_time_event_queue_update(s->time_event_queue, pj->time_event, &tv);
+        avahi_time_event_update(pj->time_event, &tv);
     else
-        pj->time_event = avahi_time_event_queue_add(s->time_event_queue, &tv, elapse_callback, pj);
+        pj->time_event = avahi_time_event_new(s->time_event_queue, &tv, elapse_callback, pj);
 }
 
 static void job_mark_done(AvahiProbeScheduler *s, AvahiProbeJob *pj) {
-    g_assert(s);
-    g_assert(pj);
+    assert(s);
+    assert(pj);
 
-    g_assert(!pj->done);
+    assert(!pj->done);
 
     AVAHI_LLIST_REMOVE(AvahiProbeJob, jobs, s->jobs, pj);
     AVAHI_LLIST_PREPEND(AvahiProbeJob, jobs, s->history, pj);
 
-    pj->done = TRUE;
+    pj->done = 1;
 
     job_set_elapse_time(s, pj, AVAHI_PROBE_HISTORY_MSEC, 0);
     gettimeofday(&pj->delivery, NULL);
@@ -124,45 +130,49 @@ static void job_mark_done(AvahiProbeScheduler *s, AvahiProbeJob *pj) {
 AvahiProbeScheduler *avahi_probe_scheduler_new(AvahiInterface *i) {
     AvahiProbeScheduler *s;
 
-    g_assert(i);
+    assert(i);
+
+    if (!(s = avahi_new(AvahiProbeScheduler, 1))) {
+        avahi_log_error(__FILE__": Out of memory");
+        return NULL;
+    }
 
-    s = g_new(AvahiProbeScheduler, 1);
     s->interface = i;
     s->time_event_queue = i->monitor->server->time_event_queue;
 
     AVAHI_LLIST_HEAD_INIT(AvahiProbeJob, s->jobs);
     AVAHI_LLIST_HEAD_INIT(AvahiProbeJob, s->history);
-    
+
     return s;
 }
 
 void avahi_probe_scheduler_free(AvahiProbeScheduler *s) {
-    g_assert(s);
+    assert(s);
 
     avahi_probe_scheduler_clear(s);
-    g_free(s);
+    avahi_free(s);
 }
 
 void avahi_probe_scheduler_clear(AvahiProbeScheduler *s) {
-    g_assert(s);
-    
+    assert(s);
+
     while (s->jobs)
         job_free(s, s->jobs);
     while (s->history)
         job_free(s, s->history);
 }
-static gboolean packet_add_probe_query(AvahiProbeScheduler *s, AvahiDnsPacket *p, AvahiProbeJob *pj) {
-    guint size;
+
+static int packet_add_probe_query(AvahiProbeScheduler *s, AvahiDnsPacket *p, AvahiProbeJob *pj) {
+    size_t size;
     AvahiKey *k;
-    gboolean b;
+    int b;
+
+    assert(s);
+    assert(p);
+    assert(pj);
 
-    g_assert(s);
-    g_assert(p);
-    g_assert(pj);
+    assert(!pj->chosen);
 
-    g_assert(!pj->chosen);
-    
     /* Estimate the size for this record */
     size =
         avahi_key_get_estimate_size(pj->record->key) +
@@ -170,15 +180,17 @@ static gboolean packet_add_probe_query(AvahiProbeScheduler *s, AvahiDnsPacket *p
 
     /* Too large */
     if (size > avahi_dns_packet_space(p))
-        return FALSE;
+        return 0;
 
     /* Create the probe query */
-    k = avahi_key_new(pj->record->key->name, pj->record->key->clazz, AVAHI_DNS_TYPE_ANY);
-    b = !!avahi_dns_packet_append_key(p, k, FALSE);
-    g_assert(b);
+    if (!(k = avahi_key_new(pj->record->key->name, pj->record->key->clazz, AVAHI_DNS_TYPE_ANY)))
+        return 0; /* OOM */
+
+    b = !!avahi_dns_packet_append_key(p, k, 0);
+    assert(b);
 
     /* Mark this job for addition to the packet */
-    pj->chosen = TRUE;
+    pj->chosen = 1;
 
     /* Scan for more jobs whith matching key pattern */
     for (pj = s->jobs; pj; pj = pj->jobs_next) {
@@ -188,27 +200,27 @@ static gboolean packet_add_probe_query(AvahiProbeScheduler *s, AvahiDnsPacket *p
         /* Does the record match the probe? */
         if (k->clazz != pj->record->key->clazz || !avahi_domain_equal(k->name, pj->record->key->name))
             continue;
-        
+
         /* This job wouldn't fit in */
         if (avahi_record_get_estimate_size(pj->record) > avahi_dns_packet_space(p))
             break;
 
         /* Mark this job for addition to the packet */
-        pj->chosen = TRUE;
+        pj->chosen = 1;
     }
 
     avahi_key_unref(k);
-            
-    return TRUE;
+
+    return 1;
 }
 
-static void elapse_callback(AvahiTimeEvent *e, gpointer data) {
+static void elapse_callback(AVAHI_GCC_UNUSED AvahiTimeEvent *e, void* data) {
     AvahiProbeJob *pj = data, *next;
     AvahiProbeScheduler *s;
     AvahiDnsPacket *p;
-    guint n;
+    unsigned n;
 
-    g_assert(pj);
+    assert(pj);
     s = pj->scheduler;
 
     if (pj->done) {
@@ -217,14 +229,15 @@ static void elapse_callback(AvahiTimeEvent *e, gpointer data) {
         return;
     }
 
-    p = avahi_dns_packet_new_query(s->interface->hardware->mtu);
+    if (!(p = avahi_dns_packet_new_query(s->interface->hardware->mtu)))
+        return; /* OOM */
     n = 1;
-    
+
     /* Add the import probe */
     if (!packet_add_probe_query(s, p, pj)) {
-        guint size;
+        size_t size;
         AvahiKey *k;
-        gboolean b;
+        int b;
 
         avahi_dns_packet_free(p);
 
@@ -234,14 +247,16 @@ static void elapse_callback(AvahiTimeEvent *e, gpointer data) {
             avahi_key_get_estimate_size(pj->record->key) +
             avahi_record_get_estimate_size(pj->record) +
             AVAHI_DNS_PACKET_HEADER_SIZE;
-        
-        if (size > AVAHI_DNS_PACKET_MAX_SIZE)
-            size = AVAHI_DNS_PACKET_MAX_SIZE;
-        
-        p = avahi_dns_packet_new_query(size);
-
-        k = avahi_key_new(pj->record->key->name, pj->record->key->clazz, AVAHI_DNS_TYPE_ANY);
-        b = avahi_dns_packet_append_key(p, k, FALSE) && avahi_dns_packet_append_record(p, pj->record, FALSE, 0);
+
+        if (!(p = avahi_dns_packet_new_query(size + AVAHI_DNS_PACKET_EXTRA_SIZE)))
+            return; /* OOM */
+
+        if (!(k = avahi_key_new(pj->record->key->name, pj->record->key->clazz, AVAHI_DNS_TYPE_ANY))) {
+            avahi_dns_packet_free(p);
+            return;  /* OOM */
+        }
+
+        b = avahi_dns_packet_append_key(p, k, 0) && avahi_dns_packet_append_record(p, pj->record, 0, 0);
         avahi_key_unref(k);
 
         if (b) {
@@ -249,8 +264,8 @@ static void elapse_callback(AvahiTimeEvent *e, gpointer data) {
             avahi_dns_packet_set_field(p, AVAHI_DNS_FIELD_QDCOUNT, 1);
             avahi_interface_send_packet(s->interface, p);
         } else
-            avahi_log_warn("Probe record too large, cannot send");   
-        
+            avahi_log_warn("Probe record too large, cannot send");
+
         avahi_dns_packet_free(p);
         job_mark_done(s, pj);
 
@@ -262,10 +277,10 @@ static void elapse_callback(AvahiTimeEvent *e, gpointer data) {
 
         if (pj->chosen)
             continue;
-        
+
         if (!packet_add_probe_query(s, p, pj))
             break;
-        
+
         n++;
     }
 
@@ -281,21 +296,21 @@ static void elapse_callback(AvahiTimeEvent *e, gpointer data) {
         if (!pj->chosen)
             continue;
 
-        if (!avahi_dns_packet_append_record(p, pj->record, FALSE, 0)) {
-            avahi_log_warn("Bad probe size estimate!");
+        if (!avahi_dns_packet_append_record(p, pj->record, 0, 0)) {
+/*             avahi_log_warn("Bad probe size estimate!"); */
 
             /* Unmark all following jobs */
             for (; pj; pj = pj->jobs_next)
-                pj->chosen = FALSE;
-            
+                pj->chosen = 0;
+
             break;
         }
 
         job_mark_done(s, pj);
-        
+
         n ++;
     }
-    
+
     avahi_dns_packet_set_field(p, AVAHI_DNS_FIELD_NSCOUNT, n);
 
     /* Send it now */
@@ -306,12 +321,12 @@ static void elapse_callback(AvahiTimeEvent *e, gpointer data) {
 static AvahiProbeJob* find_scheduled_job(AvahiProbeScheduler *s, AvahiRecord *record) {
     AvahiProbeJob *pj;
 
-    g_assert(s);
-    g_assert(record);
+    assert(s);
+    assert(record);
 
     for (pj = s->jobs; pj; pj = pj->jobs_next) {
-        g_assert(!pj->done);
-        
+        assert(!pj->done);
+
         if (avahi_record_equal_no_ttl(pj->record, record))
             return pj;
     }
@@ -321,12 +336,12 @@ static AvahiProbeJob* find_scheduled_job(AvahiProbeScheduler *s, AvahiRecord *re
 
 static AvahiProbeJob* find_history_job(AvahiProbeScheduler *s, AvahiRecord *record) {
     AvahiProbeJob *pj;
-    
-    g_assert(s);
-    g_assert(record);
+
+    assert(s);
+    assert(record);
 
     for (pj = s->history; pj; pj = pj->jobs_next) {
-        g_assert(pj->done);
+        assert(pj->done);
 
         if (avahi_record_equal_no_ttl(pj->record, record)) {
             /* Check whether this entry is outdated */
@@ -336,7 +351,7 @@ static AvahiProbeJob* find_history_job(AvahiProbeScheduler *s, AvahiRecord *reco
                 job_free(s, pj);
                 return NULL;
             }
-                
+
             return pj;
         }
     }
@@ -344,16 +359,16 @@ static AvahiProbeJob* find_history_job(AvahiProbeScheduler *s, AvahiRecord *reco
     return NULL;
 }
 
-gboolean avahi_probe_scheduler_post(AvahiProbeScheduler *s, AvahiRecord *record, gboolean immediately) {
+int avahi_probe_scheduler_post(AvahiProbeScheduler *s, AvahiRecord *record, int immediately) {
     AvahiProbeJob *pj;
     struct timeval tv;
-    
-    g_assert(s);
-    g_assert(record);
-    g_assert(!avahi_key_is_pattern(record->key));
+
+    assert(s);
+    assert(record);
+    assert(!avahi_key_is_pattern(record->key));
 
     if ((pj = find_history_job(s, record)))
-        return FALSE;
+        return 0;
 
     avahi_elapse_time(&tv, immediately ? 0 : AVAHI_PROBE_DEFER_MSEC, 0);
 
@@ -362,19 +377,21 @@ gboolean avahi_probe_scheduler_post(AvahiProbeScheduler *s, AvahiRecord *record,
         if (avahi_timeval_compare(&tv, &pj->delivery) < 0) {
             /* If the new entry should be scheduled earlier, update the old entry */
             pj->delivery = tv;
-            avahi_time_event_queue_update(s->time_event_queue, pj->time_event, &pj->delivery);
+            avahi_time_event_update(pj->time_event, &pj->delivery);
         }
 
-        return TRUE;
+        return 1;
     } else {
         /* Create a new job and schedule it */
-        pj = job_new(s, record, FALSE);
+        if (!(pj = job_new(s, record, 0)))
+            return 0; /* OOM */
+
         pj->delivery = tv;
-        pj->time_event = avahi_time_event_queue_add(s->time_event_queue, &pj->delivery, elapse_callback, pj);
+        pj->time_event = avahi_time_event_new(s->time_event_queue, &pj->delivery, elapse_callback, pj);
+
 
-        
 /*     avahi_log_debug("Accepted new probe job."); */
 
-        return TRUE;
+        return 1;
     }
 }