1 /* -*- mode: C; c-file-style: "gnu" -*- */
2 /* dbus-address.c Server address parser.
4 * Copyright (C) 2003 CodeFactory AB
5 * Copyright (C) 2004,2005 Red Hat, Inc.
7 * Licensed under the Academic Free License version 2.1
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.
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.
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
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"
33 * @defgroup DBusAddressInternals Address parsing
34 * @ingroup DBusInternals
35 * @brief Implementation of parsing addresses of D-Bus servers.
41 * Internals of DBusAddressEntry
43 struct DBusAddressEntry
45 DBusString method; /**< The address type (unix, tcp, etc.) */
47 DBusList *keys; /**< List of keys */
48 DBusList *values; /**< List of values */
54 * Sets #DBUS_ERROR_BAD_ADDRESS.
55 * If address_problem_type and address_problem_field are not #NULL,
56 * sets an error message about how the field is no good. Otherwise, sets
57 * address_problem_other as the error message.
59 * @param error the error to set
60 * @param address_problem_type the address type of the bad address or #NULL
61 * @param address_problem_field the missing field of the bad address or #NULL
62 * @param address_problem_other any other error message or #NULL
65 _dbus_set_bad_address (DBusError *error,
66 const char *address_problem_type,
67 const char *address_problem_field,
68 const char *address_problem_other)
70 if (address_problem_type != NULL)
71 dbus_set_error (error, DBUS_ERROR_BAD_ADDRESS,
72 "Server address of type %s was missing argument %s",
73 address_problem_type, address_problem_field);
75 dbus_set_error (error, DBUS_ERROR_BAD_ADDRESS,
76 "Could not parse server address: %s",
77 address_problem_other);
81 * #TRUE if the byte need not be escaped when found in a dbus address.
82 * All other bytes are required to be escaped in a valid address.
84 #define _DBUS_ADDRESS_OPTIONALLY_ESCAPED_BYTE(b) \
85 (((b) >= 'a' && (b) <= 'z') || \
86 ((b) >= 'A' && (b) <= 'Z') || \
87 ((b) >= '0' && (b) <= '9') || \
95 * Appends an escaped version of one string to another string,
96 * using the D-Bus address escaping mechanism
98 * @param escaped the string to append to
99 * @param unescaped the string to escape
100 * @returns #FALSE if no memory
103 _dbus_address_append_escaped (DBusString *escaped,
104 const DBusString *unescaped)
113 orig_len = _dbus_string_get_length (escaped);
114 p = _dbus_string_get_const_data (unescaped);
115 end = p + _dbus_string_get_length (unescaped);
118 if (_DBUS_ADDRESS_OPTIONALLY_ESCAPED_BYTE (*p))
120 if (!_dbus_string_append_byte (escaped, *p))
125 if (!_dbus_string_append_byte (escaped, '%'))
127 if (!_dbus_string_append_byte_as_hex (escaped, *p))
138 _dbus_string_set_length (escaped, orig_len);
142 /** @} */ /* End of internals */
145 dbus_address_entry_free (DBusAddressEntry *entry)
149 _dbus_string_free (&entry->method);
151 link = _dbus_list_get_first_link (&entry->keys);
154 _dbus_string_free (link->data);
155 dbus_free (link->data);
157 link = _dbus_list_get_next_link (&entry->keys, link);
159 _dbus_list_clear (&entry->keys);
161 link = _dbus_list_get_first_link (&entry->values);
164 _dbus_string_free (link->data);
165 dbus_free (link->data);
167 link = _dbus_list_get_next_link (&entry->values, link);
169 _dbus_list_clear (&entry->values);
175 * @defgroup DBusAddress Address parsing
177 * @brief Parsing addresses of D-Bus servers.
183 * Frees a #NULL-terminated array of address entries.
185 * @param entries the array.
188 dbus_address_entries_free (DBusAddressEntry **entries)
192 for (i = 0; entries[i] != NULL; i++)
193 dbus_address_entry_free (entries[i]);
197 static DBusAddressEntry *
200 DBusAddressEntry *entry;
202 entry = dbus_new0 (DBusAddressEntry, 1);
207 if (!_dbus_string_init (&entry->method))
217 * Returns the method string of an address entry. For example, given
218 * the address entry "tcp:host=example.com" it would return the string
221 * @param entry the entry.
222 * @returns a string describing the method. This string
226 dbus_address_entry_get_method (DBusAddressEntry *entry)
228 return _dbus_string_get_const_data (&entry->method);
232 * Returns a value from a key of an entry. For example,
233 * given the address "tcp:host=example.com,port=8073" if you asked
234 * for the key "host" you would get the value "example.com"
236 * The returned value is already unescaped.
238 * @param entry the entry.
239 * @param key the key.
240 * @returns the key value. This string must not be freed.
243 dbus_address_entry_get_value (DBusAddressEntry *entry,
246 DBusList *values, *keys;
248 keys = _dbus_list_get_first_link (&entry->keys);
249 values = _dbus_list_get_first_link (&entry->values);
253 _dbus_assert (values != NULL);
255 if (_dbus_string_equal_c_str (keys->data, key))
256 return _dbus_string_get_const_data (values->data);
258 keys = _dbus_list_get_next_link (&entry->keys, keys);
259 values = _dbus_list_get_next_link (&entry->values, values);
266 append_unescaped_value (DBusString *unescaped,
267 const DBusString *escaped,
278 p = _dbus_string_get_const_data (escaped) + escaped_start;
279 end = p + escaped_len;
282 if (_DBUS_ADDRESS_OPTIONALLY_ESCAPED_BYTE (*p))
284 if (!_dbus_string_append_byte (unescaped, *p))
289 /* Efficiency is king */
298 dbus_set_error (error, DBUS_ERROR_BAD_ADDRESS,
299 "In D-Bus address, percent character was not followed by two hex digits");
308 _dbus_string_init_const (&hex, buf);
310 if (!_dbus_string_hex_decode (&hex, 0, &hex_end,
312 _dbus_string_get_length (unescaped)))
317 dbus_set_error (error, DBUS_ERROR_BAD_ADDRESS,
318 "In D-Bus address, percent character was followed by characters other than hex digits");
324 /* Error, should have been escaped */
325 dbus_set_error (error, DBUS_ERROR_BAD_ADDRESS,
326 "In D-Bus address, character '%c' should have been escaped\n",
337 if (!ret && error && !dbus_error_is_set (error))
338 _DBUS_SET_OOM (error);
340 _dbus_assert (ret || error == NULL || dbus_error_is_set (error));
346 * Parses an address string of the form:
348 * method:key=value,key=value;method:key=value
350 * See the D-Bus specification for complete docs on the format.
352 * When connecting to an address, the first address entries
353 * in the semicolon-separated list should be tried first.
355 * @param address the address.
356 * @param entry return location to an array of entries.
357 * @param array_len return location for array length.
358 * @param error address where an error can be returned.
359 * @returns #TRUE on success, #FALSE otherwise.
362 dbus_parse_address (const char *address,
363 DBusAddressEntry ***entry,
368 int pos, end_pos, len, i;
369 DBusList *entries, *link;
370 DBusAddressEntry **entry_array;
372 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
374 _dbus_string_init_const (&str, address);
378 len = _dbus_string_get_length (&str);
382 dbus_set_error (error, DBUS_ERROR_BAD_ADDRESS,
383 "Empty address '%s'", address);
389 DBusAddressEntry *entry;
393 entry = create_entry ();
396 dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
401 /* Append the entry */
402 if (!_dbus_list_append (&entries, entry))
404 dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
405 dbus_address_entry_free (entry);
409 /* Look for a semi-colon */
410 if (!_dbus_string_find (&str, pos, ";", &end_pos))
413 /* Look for the colon : */
414 if (!_dbus_string_find_to (&str, pos, end_pos, ":", &found_pos))
416 dbus_set_error (error, DBUS_ERROR_BAD_ADDRESS, "Address does not contain a colon");
420 if (!_dbus_string_copy_len (&str, pos, found_pos - pos, &entry->method, 0))
422 dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
428 while (pos < end_pos)
430 int comma_pos, equals_pos;
432 if (!_dbus_string_find_to (&str, pos, end_pos, ",", &comma_pos))
435 if (!_dbus_string_find_to (&str, pos, comma_pos, "=", &equals_pos) ||
436 equals_pos == pos || equals_pos + 1 == comma_pos)
438 dbus_set_error (error, DBUS_ERROR_BAD_ADDRESS,
439 "'=' character not found or has no value following it");
447 key = dbus_new0 (DBusString, 1);
451 dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
455 value = dbus_new0 (DBusString, 1);
458 dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
463 if (!_dbus_string_init (key))
465 dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
472 if (!_dbus_string_init (value))
474 dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
475 _dbus_string_free (key);
482 if (!_dbus_string_copy_len (&str, pos, equals_pos - pos, key, 0))
484 dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
485 _dbus_string_free (key);
486 _dbus_string_free (value);
493 if (!append_unescaped_value (value, &str, equals_pos + 1,
494 comma_pos - equals_pos - 1, error))
496 _dbus_assert (error == NULL || dbus_error_is_set (error));
497 _dbus_string_free (key);
498 _dbus_string_free (value);
505 if (!_dbus_list_append (&entry->keys, key))
507 dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
508 _dbus_string_free (key);
509 _dbus_string_free (value);
516 if (!_dbus_list_append (&entry->values, value))
518 dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
519 _dbus_string_free (value);
532 *array_len = _dbus_list_get_length (&entries);
534 entry_array = dbus_new (DBusAddressEntry *, *array_len + 1);
538 dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
543 entry_array [*array_len] = NULL;
545 link = _dbus_list_get_first_link (&entries);
549 entry_array[i] = link->data;
551 link = _dbus_list_get_next_link (&entries, link);
554 _dbus_list_clear (&entries);
555 *entry = entry_array;
561 link = _dbus_list_get_first_link (&entries);
564 dbus_address_entry_free (link->data);
565 link = _dbus_list_get_next_link (&entries, link);
568 _dbus_list_clear (&entries);
575 * Escapes the given string as a value in a key=value pair
576 * for a D-Bus address.
578 * @param value the unescaped value
579 * @returns newly-allocated escaped value or #NULL if no memory
582 dbus_address_escape_value (const char *value)
585 DBusString unescaped;
590 _dbus_string_init_const (&unescaped, value);
592 if (!_dbus_string_init (&escaped))
595 if (!_dbus_address_append_escaped (&escaped, &unescaped))
598 if (!_dbus_string_steal_data (&escaped, &ret))
602 _dbus_string_free (&escaped);
607 * Unescapes the given string as a value in a key=value pair
608 * for a D-Bus address. Note that dbus_address_entry_get_value()
609 * returns an already-unescaped value.
611 * @param value the escaped value
612 * @param error error to set if the unescaping fails
613 * @returns newly-allocated unescaped value or #NULL if no memory
616 dbus_address_unescape_value (const char *value,
619 DBusString unescaped;
625 _dbus_string_init_const (&escaped, value);
627 if (!_dbus_string_init (&unescaped))
630 if (!append_unescaped_value (&unescaped, &escaped,
631 0, _dbus_string_get_length (&escaped),
635 if (!_dbus_string_steal_data (&unescaped, &ret))
639 if (ret == NULL && error && !dbus_error_is_set (error))
640 _DBUS_SET_OOM (error);
642 _dbus_assert (ret != NULL || error == NULL || dbus_error_is_set (error));
644 _dbus_string_free (&unescaped);
648 /** @} */ /* End of public API */
650 #ifdef DBUS_BUILD_TESTS
652 #ifndef DOXYGEN_SHOULD_SKIP_THIS
654 #include "dbus-test.h"
660 const char *unescaped;
663 static const EscapeTest escape_tests[] = {
664 { "abcde", "abcde" },
669 { "abc%24", "abc$" },
670 { "%24abc", "$abc" },
671 { "abc%24abc", "abc$abc" },
683 static const char* invalid_escaped_values[] = {
698 _dbus_address_test (void)
700 DBusAddressEntry **entries;
705 dbus_error_init (&error);
708 while (i < _DBUS_N_ELEMENTS (escape_tests))
710 const EscapeTest *test = &escape_tests[i];
714 escaped = dbus_address_escape_value (test->unescaped);
716 _dbus_assert_not_reached ("oom");
718 if (strcmp (escaped, test->escaped) != 0)
720 _dbus_warn ("Escaped '%s' as '%s' should have been '%s'\n",
721 test->unescaped, escaped, test->escaped);
726 unescaped = dbus_address_unescape_value (test->escaped, &error);
727 if (unescaped == NULL)
729 _dbus_warn ("Failed to unescape '%s': %s\n",
730 test->escaped, error.message);
731 dbus_error_free (&error);
735 if (strcmp (unescaped, test->unescaped) != 0)
737 _dbus_warn ("Unescaped '%s' as '%s' should have been '%s'\n",
738 test->escaped, unescaped, test->unescaped);
741 dbus_free (unescaped);
747 while (i < _DBUS_N_ELEMENTS (invalid_escaped_values))
751 unescaped = dbus_address_unescape_value (invalid_escaped_values[i],
753 if (unescaped != NULL)
755 _dbus_warn ("Should not have successfully unescaped '%s' to '%s'\n",
756 invalid_escaped_values[i], unescaped);
757 dbus_free (unescaped);
761 _dbus_assert (dbus_error_is_set (&error));
762 dbus_error_free (&error);
767 if (!dbus_parse_address ("unix:path=/tmp/foo;debug:name=test,sliff=sloff;",
768 &entries, &len, &error))
769 _dbus_assert_not_reached ("could not parse address");
770 _dbus_assert (len == 2);
771 _dbus_assert (strcmp (dbus_address_entry_get_value (entries[0], "path"), "/tmp/foo") == 0);
772 _dbus_assert (strcmp (dbus_address_entry_get_value (entries[1], "name"), "test") == 0);
773 _dbus_assert (strcmp (dbus_address_entry_get_value (entries[1], "sliff"), "sloff") == 0);
775 dbus_address_entries_free (entries);
777 /* Different possible errors */
778 if (dbus_parse_address ("", &entries, &len, &error))
779 _dbus_assert_not_reached ("Parsed incorrect address.");
781 dbus_error_free (&error);
783 if (dbus_parse_address ("foo", &entries, &len, &error))
784 _dbus_assert_not_reached ("Parsed incorrect address.");
786 dbus_error_free (&error);
788 if (dbus_parse_address ("foo:bar", &entries, &len, &error))
789 _dbus_assert_not_reached ("Parsed incorrect address.");
791 dbus_error_free (&error);
793 if (dbus_parse_address ("foo:bar,baz", &entries, &len, &error))
794 _dbus_assert_not_reached ("Parsed incorrect address.");
796 dbus_error_free (&error);
798 if (dbus_parse_address ("foo:bar=foo,baz", &entries, &len, &error))
799 _dbus_assert_not_reached ("Parsed incorrect address.");
801 dbus_error_free (&error);
803 if (dbus_parse_address ("foo:bar=foo;baz", &entries, &len, &error))
804 _dbus_assert_not_reached ("Parsed incorrect address.");
806 dbus_error_free (&error);
808 if (dbus_parse_address ("foo:=foo", &entries, &len, &error))
809 _dbus_assert_not_reached ("Parsed incorrect address.");
811 dbus_error_free (&error);
813 if (dbus_parse_address ("foo:foo=", &entries, &len, &error))
814 _dbus_assert_not_reached ("Parsed incorrect address.");
816 dbus_error_free (&error);
818 if (dbus_parse_address ("foo:foo,bar=baz", &entries, &len, &error))
819 _dbus_assert_not_reached ("Parsed incorrect address.");
821 dbus_error_free (&error);
826 #endif /* !DOXYGEN_SHOULD_SKIP_THIS */