]> git.meshlink.io Git - catta/blob - avahi-core/rrlist.c
9d766844c022e4286c6f659eb7c9883bcfdf62f4
[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 #ifdef HAVE_CONFIG_H
23 #include <config.h>
24 #endif
25
26 #include <stdlib.h>
27 #include <assert.h>
28
29 #include <avahi-common/llist.h>
30 #include <avahi-common/malloc.h>
31
32 #include "rrlist.h"
33 #include "log.h"
34
35 typedef struct AvahiRecordListItem AvahiRecordListItem;
36
37 struct AvahiRecordListItem {
38     int read;
39     AvahiRecord *record;
40     int unicast_response;
41     int flush_cache;
42     int auxiliary;
43     AVAHI_LLIST_FIELDS(AvahiRecordListItem, items);
44 };
45
46
47 struct AvahiRecordList {
48     AVAHI_LLIST_HEAD(AvahiRecordListItem, read);
49     AVAHI_LLIST_HEAD(AvahiRecordListItem, unread);
50 };
51
52 AvahiRecordList *avahi_record_list_new(void) {
53     AvahiRecordList *l;
54
55     if (!(l = avahi_new(AvahiRecordList, 1))) {
56         avahi_log_error("avahi_new() failed.");
57         return NULL;
58     }
59     
60     AVAHI_LLIST_HEAD_INIT(AvahiRecordListItem, l->read);
61     AVAHI_LLIST_HEAD_INIT(AvahiRecordListItem, l->unread);
62     return l;
63 }
64
65 void avahi_record_list_free(AvahiRecordList *l) {
66     assert(l);
67
68     avahi_record_list_flush(l);
69     avahi_free(l);
70 }
71
72 static void item_free(AvahiRecordList *l, AvahiRecordListItem *i) {
73     assert(l);
74     assert(i);
75
76     if (i->read) 
77         AVAHI_LLIST_REMOVE(AvahiRecordListItem, items, l->read, i);
78     else
79         AVAHI_LLIST_REMOVE(AvahiRecordListItem, items, l->unread, i);
80     
81     avahi_record_unref(i->record);
82     avahi_free(i);
83 }
84
85 void avahi_record_list_flush(AvahiRecordList *l) {
86     assert(l);
87     
88     while (l->read)
89         item_free(l, l->read);
90     while (l->unread)
91         item_free(l, l->unread);
92 }
93
94 AvahiRecord* avahi_record_list_next(AvahiRecordList *l, int *flush_cache, int *unicast_response, int *auxiliary) {
95     AvahiRecord *r;
96     AvahiRecordListItem *i;
97
98     if (!(i = l->unread))
99         return NULL;
100
101     assert(!i->read);
102     
103     r = avahi_record_ref(i->record);
104     if (unicast_response)
105         *unicast_response = i->unicast_response;
106     if (flush_cache)
107         *flush_cache = i->flush_cache;
108     if (auxiliary)
109         *auxiliary = i->auxiliary;
110
111     AVAHI_LLIST_REMOVE(AvahiRecordListItem, items, l->unread, i);
112     AVAHI_LLIST_PREPEND(AvahiRecordListItem, items, l->read, i);
113
114     i->read = 1;
115     
116     return r;
117 }
118
119 static AvahiRecordListItem *get(AvahiRecordList *l, AvahiRecord *r) {
120     AvahiRecordListItem *i;
121
122     assert(l);
123     assert(r);
124     
125     for (i = l->read; i; i = i->items_next)
126         if (avahi_record_equal_no_ttl(i->record, r))
127             return i;
128
129     for (i = l->unread; i; i = i->items_next)
130         if (avahi_record_equal_no_ttl(i->record, r))
131             return i;
132
133     return NULL;
134 }
135
136 void avahi_record_list_push(AvahiRecordList *l, AvahiRecord *r, int flush_cache, int unicast_response, int auxiliary) {
137     AvahiRecordListItem *i;
138         
139     assert(l);
140     assert(r);
141
142     if (get(l, r))
143         return;
144
145     if (!(i = avahi_new(AvahiRecordListItem, 1))) {
146         avahi_log_error("avahi_new() failed.");
147         return;
148     }
149     
150     i->unicast_response = unicast_response;
151     i->flush_cache = flush_cache;
152     i->auxiliary = auxiliary;
153     i->record = avahi_record_ref(r);
154     i->read = 0;
155
156     AVAHI_LLIST_PREPEND(AvahiRecordListItem, items, l->unread, i);
157 }
158
159 void avahi_record_list_drop(AvahiRecordList *l, AvahiRecord *r) {
160     AvahiRecordListItem *i;
161
162     assert(l);
163     assert(r);
164
165     if (!(i = get(l, r)))
166         return;
167
168     item_free(l, i);
169 }
170
171 int avahi_record_list_is_empty(AvahiRecordList *l) {
172     assert(l);
173     
174     return !l->unread && !l->read;
175 }