]> git.meshlink.io Git - catta/blob - avahi-core/rrlist.c
* Move avahi-core/llist.h to avahi-common/llist.h, not installed.
[catta] / avahi-core / rrlist.c
1 /* $Id$ */
2
3 /***
4   This file is part of avahi.
5  
6   avahi is free software; you can redistribute it and/or modify it
7   under the terms of the GNU Lesser General Public License as
8   published by the Free Software Foundation; either version 2.1 of the
9   License, or (at your option) any later version.
10  
11   avahi is distributed in the hope that it will be useful, but WITHOUT
12   ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
13   or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General
14   Public License for more details.
15  
16   You should have received a copy of the GNU Lesser General Public
17   License along with avahi; if not, write to the Free Software
18   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
19   USA.
20 ***/
21
22 #include <avahi-common/llist.h>
23 #include "rrlist.h"
24
25 typedef struct AvahiRecordListItem AvahiRecordListItem;
26
27 struct AvahiRecordListItem {
28     gboolean read;
29     AvahiRecord *record;
30     gboolean unicast_response;
31     gboolean flush_cache;
32     gboolean auxiliary;
33     AVAHI_LLIST_FIELDS(AvahiRecordListItem, items);
34 };
35
36
37 struct AvahiRecordList {
38     AVAHI_LLIST_HEAD(AvahiRecordListItem, read);
39     AVAHI_LLIST_HEAD(AvahiRecordListItem, unread);
40 };
41
42 AvahiRecordList *avahi_record_list_new(void) {
43     AvahiRecordList *l = g_new(AvahiRecordList, 1);
44     AVAHI_LLIST_HEAD_INIT(AvahiRecordListItem, l->read);
45     AVAHI_LLIST_HEAD_INIT(AvahiRecordListItem, l->unread);
46     return l;
47 }
48
49 void avahi_record_list_free(AvahiRecordList *l) {
50     g_assert(l);
51
52     avahi_record_list_flush(l);
53     g_free(l);
54 }
55
56 static void item_free(AvahiRecordList *l, AvahiRecordListItem *i) {
57     g_assert(l);
58     g_assert(i);
59
60     if (i->read) 
61         AVAHI_LLIST_REMOVE(AvahiRecordListItem, items, l->read, i);
62     else
63         AVAHI_LLIST_REMOVE(AvahiRecordListItem, items, l->unread, i);
64     
65     avahi_record_unref(i->record);
66     g_free(i);
67 }
68
69 void avahi_record_list_flush(AvahiRecordList *l) {
70     g_assert(l);
71     
72     while (l->read)
73         item_free(l, l->read);
74     while (l->unread)
75         item_free(l, l->unread);
76 }
77
78 AvahiRecord* avahi_record_list_next(AvahiRecordList *l, gboolean *flush_cache, gboolean *unicast_response, gboolean *auxiliary) {
79     AvahiRecord *r;
80     AvahiRecordListItem *i;
81
82     if (!(i = l->unread))
83         return NULL;
84
85     g_assert(!i->read);
86     
87     r = avahi_record_ref(i->record);
88     if (unicast_response)
89         *unicast_response = i->unicast_response;
90     if (flush_cache)
91         *flush_cache = i->flush_cache;
92     if (auxiliary)
93         *auxiliary = i->auxiliary;
94
95     AVAHI_LLIST_REMOVE(AvahiRecordListItem, items, l->unread, i);
96     AVAHI_LLIST_PREPEND(AvahiRecordListItem, items, l->read, i);
97
98     i->read = TRUE;
99     
100     return r;
101 }
102
103 static AvahiRecordListItem *get(AvahiRecordList *l, AvahiRecord *r) {
104     AvahiRecordListItem *i;
105
106     g_assert(l);
107     g_assert(r);
108     
109     for (i = l->read; i; i = i->items_next)
110         if (avahi_record_equal_no_ttl(i->record, r))
111             return i;
112
113     for (i = l->unread; i; i = i->items_next)
114         if (avahi_record_equal_no_ttl(i->record, r))
115             return i;
116
117     return NULL;
118 }
119
120 void avahi_record_list_push(AvahiRecordList *l, AvahiRecord *r, gboolean flush_cache, gboolean unicast_response, gboolean auxiliary) {
121     AvahiRecordListItem *i;
122         
123     g_assert(l);
124     g_assert(r);
125
126     if (get(l, r))
127         return;
128
129     i = g_new(AvahiRecordListItem, 1);
130     i->unicast_response = unicast_response;
131     i->flush_cache = flush_cache;
132     i->auxiliary = auxiliary;
133     i->record = avahi_record_ref(r);
134     i->read = FALSE;
135
136     AVAHI_LLIST_PREPEND(AvahiRecordListItem, items, l->unread, i);
137 }
138
139 void avahi_record_list_drop(AvahiRecordList *l, AvahiRecord *r) {
140     AvahiRecordListItem *i;
141
142     g_assert(l);
143     g_assert(r);
144
145     if (!(i = get(l, r)))
146         return;
147
148     item_free(l, i);
149 }
150
151 gboolean avahi_record_list_empty(AvahiRecordList *l) {
152     g_assert(l);
153     
154     return !l->unread && !l->read;
155 }