2005-01-27 Havoc Pennington <hp@redhat.com>
[platform/upstream/dbus.git] / dbus / dbus-address.c
1 /* -*- mode: C; c-file-style: "gnu" -*- */
2 /* dbus-address.c  Server address parser.
3  *
4  * Copyright (C) 2003  CodeFactory AB
5  * Copyright (C) 2004  Red Hat, Inc.
6  *
7  * Licensed under the Academic Free License version 2.1
8  * 
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  * 
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software
21  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
22  *
23  */
24
25 #include <config.h>
26 #include "dbus-address.h"
27 #include "dbus-internals.h"
28 #include "dbus-list.h"
29 #include "dbus-string.h"
30 #include "dbus-protocol.h"
31
32 /**
33  * @defgroup DBusAddressInternals Address parsing
34  * @ingroup  DBusInternals
35  * @brief Implementation of parsing addresses of D-BUS servers.
36  *
37  * @{
38  */
39
40 /**
41  * Internals of DBusAddressEntry 
42  */
43 struct DBusAddressEntry
44 {
45   DBusString method; /**< The address type (unix, tcp, etc.) */
46
47   DBusList *keys;    /**< List of keys */
48   DBusList *values;  /**< List of values */
49 };
50
51 /** @} */ /* End of internals */
52
53 static void
54 dbus_address_entry_free (DBusAddressEntry *entry)
55 {
56   DBusList *link;
57   
58   _dbus_string_free (&entry->method);
59
60   link = _dbus_list_get_first_link (&entry->keys);
61   while (link != NULL)
62     {
63       _dbus_string_free (link->data);
64       dbus_free (link->data);
65       
66       link = _dbus_list_get_next_link (&entry->keys, link);
67     }
68   _dbus_list_clear (&entry->keys);
69   
70   link = _dbus_list_get_first_link (&entry->values);
71   while (link != NULL)
72     {
73       _dbus_string_free (link->data);
74       dbus_free (link->data);
75       
76       link = _dbus_list_get_next_link (&entry->values, link);
77     }
78   _dbus_list_clear (&entry->values);
79   
80   dbus_free (entry);
81 }
82
83 /**
84  * @defgroup DBusAddress Address parsing
85  * @ingroup  DBus
86  * @brief Parsing addresses of D-BUS servers.
87  *
88  * @{
89  */
90
91 /**
92  * Frees a #NULL-terminated array of address entries.
93  *
94  * @param entries the array.
95  */
96 void
97 dbus_address_entries_free (DBusAddressEntry **entries)
98 {
99   int i;
100   
101   for (i = 0; entries[i] != NULL; i++)
102     dbus_address_entry_free (entries[i]);
103   dbus_free (entries);
104 }
105
106 static DBusAddressEntry *
107 create_entry (void)
108 {
109   DBusAddressEntry *entry;
110
111   entry = dbus_new0 (DBusAddressEntry, 1);
112
113   if (entry == NULL)
114     return NULL;
115
116   if (!_dbus_string_init (&entry->method))
117     {
118       dbus_free (entry);
119       return NULL;
120     }
121
122   return entry;
123 }
124
125 /**
126  * Returns the method string of an address entry.
127  *
128  * @param entry the entry.
129  * @returns a string describing the method. This string
130  * must not be freed.
131  */
132 const char *
133 dbus_address_entry_get_method (DBusAddressEntry *entry)
134 {
135   return _dbus_string_get_const_data (&entry->method);
136 }
137
138 /**
139  * Returns a value from a key of an entry.
140  *
141  * @param entry the entry.
142  * @param key the key.
143  * @returns the key value. This string must not be fred.
144  */
145 const char *
146 dbus_address_entry_get_value (DBusAddressEntry *entry,
147                               const char       *key)
148 {
149   DBusList *values, *keys;
150
151   keys = _dbus_list_get_first_link (&entry->keys);
152   values = _dbus_list_get_first_link (&entry->values);
153
154   while (keys != NULL)
155     {
156       _dbus_assert (values != NULL);
157
158       if (_dbus_string_equal_c_str (keys->data, key))
159         return _dbus_string_get_const_data (values->data);
160
161       keys = _dbus_list_get_next_link (&entry->keys, keys);
162       values = _dbus_list_get_next_link (&entry->values, values);
163     }
164   
165   return NULL;
166 }
167
168 /**
169  * Parses an address string of the form:
170  *
171  * method:key=value,key=value;method:key=value
172  *
173  * @todo document address format in the specification
174  *
175  * @todo need to be able to escape ';' and ',' in the
176  * key values, and the parsing needs to handle that.
177  * 
178  * @param address the address.
179  * @param entry return location to an array of entries.
180  * @param array_len return location for array length.
181  * @param error address where an error can be returned.
182  * @returns #TRUE on success, #FALSE otherwise.
183  */
184 dbus_bool_t
185 dbus_parse_address (const char         *address,
186                     DBusAddressEntry ***entry,
187                     int                *array_len,
188                     DBusError          *error)
189 {
190   DBusString str;
191   int pos, end_pos, len, i;
192   DBusList *entries, *link;
193   DBusAddressEntry **entry_array;
194
195   _DBUS_ASSERT_ERROR_IS_CLEAR (error);
196   
197   _dbus_string_init_const (&str, address);
198
199   entries = NULL;
200   pos = 0;
201   len = _dbus_string_get_length (&str);
202   
203   while (pos < len)
204     {
205       DBusAddressEntry *entry;
206
207       int found_pos;
208
209       entry = create_entry ();
210       if (!entry)
211         {
212           dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
213
214           goto error;
215         }
216       
217       /* Append the entry */
218       if (!_dbus_list_append (&entries, entry))
219         {
220           dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
221           dbus_address_entry_free (entry);
222           goto error;
223         }
224       
225       /* Look for a semi-colon */
226       if (!_dbus_string_find (&str, pos, ";", &end_pos))
227         end_pos = len;
228       
229       /* Look for the colon : */
230       if (!_dbus_string_find_to (&str, pos, end_pos, ":", &found_pos))
231         {
232           dbus_set_error (error, DBUS_ERROR_BAD_ADDRESS, "Address does not contain a colon");
233           goto error;
234         }
235
236       if (!_dbus_string_copy_len (&str, pos, found_pos - pos, &entry->method, 0))
237         {
238           dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
239           goto error;
240         }
241           
242       pos = found_pos + 1;
243
244       while (pos < end_pos)
245         {
246           int comma_pos, equals_pos;
247
248           if (!_dbus_string_find_to (&str, pos, end_pos, ",", &comma_pos))
249             comma_pos = end_pos;
250           
251           if (!_dbus_string_find_to (&str, pos, comma_pos, "=", &equals_pos) ||
252               equals_pos == pos || equals_pos + 1 == comma_pos)
253             {
254               dbus_set_error (error, DBUS_ERROR_BAD_ADDRESS,
255                               "'=' character not found or has no value following it");
256               goto error;
257             }
258           else
259             {
260               DBusString *key;
261               DBusString *value;
262
263               key = dbus_new0 (DBusString, 1);
264
265               if (!key)
266                 {
267                   dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);             
268                   goto error;
269                 }
270
271               value = dbus_new0 (DBusString, 1);
272               if (!value)
273                 {
274                   dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
275                   dbus_free (key);
276                   goto error;
277                 }
278               
279               if (!_dbus_string_init (key))
280                 {
281                   dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
282                   dbus_free (key);
283                   dbus_free (value);
284                   
285                   goto error;
286                 }
287               
288               if (!_dbus_string_init (value))
289                 {
290                   dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
291                   _dbus_string_free (key);
292
293                   dbus_free (key);
294                   dbus_free (value);              
295                   goto error;
296                 }
297
298               if (!_dbus_string_copy_len (&str, pos, equals_pos - pos, key, 0))
299                 {
300                   dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
301                   _dbus_string_free (key);
302                   _dbus_string_free (value);
303
304                   dbus_free (key);
305                   dbus_free (value);              
306                   goto error;
307                 }
308
309               if (!_dbus_string_copy_len (&str, equals_pos + 1, comma_pos - equals_pos - 1, value, 0))
310                 {
311                   dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);             
312                   _dbus_string_free (key);
313                   _dbus_string_free (value);
314
315                   dbus_free (key);
316                   dbus_free (value);              
317                   goto error;
318                 }
319
320               if (!_dbus_list_append (&entry->keys, key))
321                 {
322                   dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);             
323                   _dbus_string_free (key);
324                   _dbus_string_free (value);
325
326                   dbus_free (key);
327                   dbus_free (value);              
328                   goto error;
329                 }
330
331               if (!_dbus_list_append (&entry->values, value))
332                 {
333                   dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);             
334                   _dbus_string_free (value);
335
336                   dbus_free (value);
337                   goto error;             
338                 }
339             }
340
341           pos = comma_pos + 1;
342         }
343
344       pos = end_pos + 1;
345     }
346
347   *array_len = _dbus_list_get_length (&entries);
348   
349   entry_array = dbus_new (DBusAddressEntry *, *array_len + 1);
350
351   if (!entry_array)
352     {
353       dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
354       
355       goto error;
356     }
357   
358   entry_array [*array_len] = NULL;
359
360   link = _dbus_list_get_first_link (&entries);
361   i = 0;
362   while (link != NULL)
363     {
364       entry_array[i] = link->data;
365       i++;
366       link = _dbus_list_get_next_link (&entries, link);
367     }
368
369   _dbus_list_clear (&entries);
370   *entry = entry_array;
371
372   return TRUE;
373
374  error:
375   
376   link = _dbus_list_get_first_link (&entries);
377   while (link != NULL)
378     {
379       dbus_address_entry_free (link->data);
380       link = _dbus_list_get_next_link (&entries, link);
381     }
382
383   _dbus_list_clear (&entries);
384   
385   return FALSE;
386   
387 }
388
389
390 /** @} */ /* End of public API */
391
392 #ifdef DBUS_BUILD_TESTS
393 #include "dbus-test.h"
394
395 dbus_bool_t
396 _dbus_address_test (void)
397 {
398   DBusAddressEntry **entries;
399   int len;  
400   DBusError error;
401
402   dbus_error_init (&error);
403   
404   if (!dbus_parse_address ("unix:path=/tmp/foo;debug:name=test,sliff=sloff;",
405                            &entries, &len, &error))
406     _dbus_assert_not_reached ("could not parse address");
407   _dbus_assert (len == 2);
408   _dbus_assert (strcmp (dbus_address_entry_get_value (entries[0], "path"), "/tmp/foo") == 0);
409   _dbus_assert (strcmp (dbus_address_entry_get_value (entries[1], "name"), "test") == 0);
410   _dbus_assert (strcmp (dbus_address_entry_get_value (entries[1], "sliff"), "sloff") == 0);
411   
412   dbus_address_entries_free (entries);
413
414   /* Different possible errors */
415   if (dbus_parse_address ("foo", &entries, &len, &error))
416     _dbus_assert_not_reached ("Parsed incorrect address.");
417   else
418     dbus_error_free (&error);
419   
420   if (dbus_parse_address ("foo:bar", &entries, &len, &error))
421     _dbus_assert_not_reached ("Parsed incorrect address.");
422   else
423     dbus_error_free (&error);
424   
425   if (dbus_parse_address ("foo:bar,baz", &entries, &len, &error))
426     _dbus_assert_not_reached ("Parsed incorrect address.");
427   else
428     dbus_error_free (&error);
429   
430   if (dbus_parse_address ("foo:bar=foo,baz", &entries, &len, &error))
431     _dbus_assert_not_reached ("Parsed incorrect address.");
432   else
433     dbus_error_free (&error);
434   
435   if (dbus_parse_address ("foo:bar=foo;baz", &entries, &len, &error))
436     _dbus_assert_not_reached ("Parsed incorrect address.");
437   else
438     dbus_error_free (&error);
439   
440   if (dbus_parse_address ("foo:=foo", &entries, &len, &error))
441     _dbus_assert_not_reached ("Parsed incorrect address.");
442   else
443     dbus_error_free (&error);
444   
445   if (dbus_parse_address ("foo:foo=", &entries, &len, &error))
446     _dbus_assert_not_reached ("Parsed incorrect address.");
447   else
448     dbus_error_free (&error);
449
450   if (dbus_parse_address ("foo:foo,bar=baz", &entries, &len, &error))
451     _dbus_assert_not_reached ("Parsed incorrect address.");
452   else
453     dbus_error_free (&error);
454
455   return TRUE;
456 }
457
458 #endif