]> git.meshlink.io Git - catta/blob - avahi-compat-howl/text.c
* implement HOWL TXT functions
[catta] / avahi-compat-howl / text.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 <assert.h>
27
28 #include <howl.h>
29
30 #include <avahi-common/strlst.h>
31 #include <avahi-common/malloc.h>
32 #include <avahi-common/domain.h>
33
34 #include "warn.h"
35
36 struct _sw_text_record {
37     AvahiStringList *strlst;
38     uint8_t *buffer;
39     size_t buffer_size;
40     int buffer_valid;
41 };
42
43 sw_result sw_text_record_init(sw_text_record *self) {
44     assert(self);
45
46     AVAHI_WARN_LINKAGE;
47     
48     if (!(*self = avahi_new(struct _sw_text_record, 1))) {
49         *self = NULL;
50         return SW_E_UNKNOWN;
51     }
52     
53     (*self)->strlst = NULL;
54     (*self)->buffer = NULL;
55     (*self)->buffer_size = 0;
56     (*self)->buffer_valid = 0;
57     
58     return SW_OKAY;
59 }
60
61 sw_result sw_text_record_fina(sw_text_record self) {
62     assert(self);
63
64     AVAHI_WARN_LINKAGE;
65
66     avahi_string_list_free(self->strlst);
67     avahi_free(self->buffer);
68     avahi_free(self);
69     return SW_OKAY;
70 }
71
72 sw_result sw_text_record_add_string(
73     sw_text_record self,
74     sw_const_string string) {
75
76     AvahiStringList *n;
77     
78     assert(self);
79     assert(string);
80
81     AVAHI_WARN_LINKAGE;
82     
83     if (!(n = avahi_string_list_add(self->strlst, string)))
84         return SW_E_UNKNOWN;
85
86     self->strlst = n;
87     self->buffer_valid = 0;
88     return SW_OKAY;
89 }
90
91 sw_result sw_text_record_add_key_and_string_value(
92     sw_text_record self,
93     sw_const_string key,
94     sw_const_string val) {
95
96     AvahiStringList *n;
97
98     assert(self);
99     assert(key);
100     
101     AVAHI_WARN_LINKAGE;
102
103     if (!(n = avahi_string_list_add_pair(self->strlst, key, val)))
104         return SW_E_UNKNOWN;
105
106     self->strlst = n;
107     self->buffer_valid = 0;
108     return SW_OKAY;
109 }
110
111 sw_result sw_text_record_add_key_and_binary_value(
112     sw_text_record self,
113     sw_const_string key,
114     sw_octets val,
115     sw_uint32 len) {
116
117     AvahiStringList *n;
118
119     assert(self);
120     assert(key);
121     assert(len || !val);
122     
123     AVAHI_WARN_LINKAGE;
124
125     if (!(n = avahi_string_list_add_pair_arbitrary(self->strlst, key, val, len)))
126         return SW_E_UNKNOWN;
127
128     self->strlst = n;
129     self->buffer_valid = 0;
130     return SW_OKAY;
131 }
132
133 static int rebuild(sw_text_record self) {
134     assert(self);
135
136     if (self->buffer_valid)
137         return 0;
138
139     self->buffer_size = avahi_string_list_serialize(self->strlst, NULL, 0);
140     
141     if (!(self->buffer = avahi_realloc(self->buffer, self->buffer_size + 1)))
142         return -1;
143         
144     avahi_string_list_serialize(self->strlst, self->buffer, self->buffer_size);
145     self->buffer_valid = 1;
146
147     return 0;
148 }
149
150 sw_octets sw_text_record_bytes(sw_text_record self) {
151     assert(self);
152
153     AVAHI_WARN_LINKAGE;
154
155     if (rebuild(self) < 0)
156         return NULL;
157
158     return self->buffer;
159 }
160
161 sw_uint32 sw_text_record_len(sw_text_record self) {
162     assert(self);
163
164     AVAHI_WARN_LINKAGE;
165     
166     if (rebuild(self) < 0)
167         return (uint32_t) -1;
168
169     return self->buffer_size;
170 }
171
172 struct _sw_text_record_iterator {
173     AvahiStringList *strlst, *index;
174     
175 };
176
177 sw_result sw_text_record_iterator_init(
178     sw_text_record_iterator * self,
179     sw_octets text_record,
180     sw_uint32 text_record_len) {
181
182     assert(self);
183
184     AVAHI_WARN_LINKAGE;
185     
186     if (!(*self = avahi_new(struct _sw_text_record_iterator, 1))) {
187         *self = NULL;
188         return SW_E_UNKNOWN;
189     }
190
191     (*self)->index = (*self)->strlst = avahi_string_list_reverse(avahi_string_list_parse(text_record, text_record_len));
192     
193     return SW_OKAY;
194 }
195
196 sw_result sw_text_record_iterator_fina(sw_text_record_iterator self) {
197     assert(self);
198
199     AVAHI_WARN_LINKAGE;
200
201     avahi_string_list_free(self->strlst);
202     avahi_free(self);
203     
204     return SW_OKAY;
205 }
206
207 sw_result sw_text_record_iterator_next(
208     sw_text_record_iterator self,
209     char key[255],
210     sw_uint8 val[255],
211     sw_uint32 * val_len) {
212
213     char *mkey = NULL, *mvalue = NULL;
214     size_t msize = 0;
215     
216     assert(self);
217     assert(key);
218     
219     AVAHI_WARN_LINKAGE;
220
221     if (!self->index)
222         return SW_E_UNKNOWN;
223
224     if (avahi_string_list_get_pair(self->index, &mkey, &mvalue, &msize) < 0)
225         return SW_E_UNKNOWN;
226
227     avahi_strlcpy(key, mkey, 255);
228     memcpy(val, mvalue, msize);
229     *val_len = msize;
230     
231     avahi_free(mkey);
232     avahi_free(mvalue);
233
234     self->index = self->index->next;
235
236     return SW_OKAY;
237 }
238