1 /* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*- */
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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 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') || \
96 * Appends an escaped version of one string to another string,
97 * using the D-Bus address escaping mechanism
99 * @param escaped the string to append to
100 * @param unescaped the string to escape
101 * @returns #FALSE if no memory
104 _dbus_address_append_escaped (DBusString *escaped,
105 const DBusString *unescaped)
114 orig_len = _dbus_string_get_length (escaped);
115 p = _dbus_string_get_const_data (unescaped);
116 end = p + _dbus_string_get_length (unescaped);
119 if (_DBUS_ADDRESS_OPTIONALLY_ESCAPED_BYTE (*p))
121 if (!_dbus_string_append_byte (escaped, *p))
126 if (!_dbus_string_append_byte (escaped, '%'))
128 if (!_dbus_string_append_byte_as_hex (escaped, *p))
139 _dbus_string_set_length (escaped, orig_len);
143 /** @} */ /* End of internals */
146 dbus_address_entry_free (DBusAddressEntry *entry)
150 _dbus_string_free (&entry->method);
152 link = _dbus_list_get_first_link (&entry->keys);
155 _dbus_string_free (link->data);
156 dbus_free (link->data);
158 link = _dbus_list_get_next_link (&entry->keys, link);
160 _dbus_list_clear (&entry->keys);
162 link = _dbus_list_get_first_link (&entry->values);
165 _dbus_string_free (link->data);
166 dbus_free (link->data);
168 link = _dbus_list_get_next_link (&entry->values, link);
170 _dbus_list_clear (&entry->values);
176 * @defgroup DBusAddress Address parsing
178 * @brief Parsing addresses of D-Bus servers.
184 * Frees a #NULL-terminated array of address entries.
186 * @param entries the array.
189 dbus_address_entries_free (DBusAddressEntry **entries)
193 for (i = 0; entries[i] != NULL; i++)
194 dbus_address_entry_free (entries[i]);
198 static DBusAddressEntry *
201 DBusAddressEntry *entry;
203 entry = dbus_new0 (DBusAddressEntry, 1);
208 if (!_dbus_string_init (&entry->method))
218 * Returns the method string of an address entry. For example, given
219 * the address entry "tcp:host=example.com" it would return the string
222 * @param entry the entry.
223 * @returns a string describing the method. This string
227 dbus_address_entry_get_method (DBusAddressEntry *entry)
229 return _dbus_string_get_const_data (&entry->method);
233 * Returns a value from a key of an entry. For example,
234 * given the address "tcp:host=example.com,port=8073" if you asked
235 * for the key "host" you would get the value "example.com"
237 * The returned value is already unescaped.
239 * @param entry the entry.
240 * @param key the key.
241 * @returns the key value. This string must not be freed.
244 dbus_address_entry_get_value (DBusAddressEntry *entry,
247 DBusList *values, *keys;
249 keys = _dbus_list_get_first_link (&entry->keys);
250 values = _dbus_list_get_first_link (&entry->values);
254 _dbus_assert (values != NULL);
256 if (_dbus_string_equal_c_str (keys->data, key))
257 return _dbus_string_get_const_data (values->data);
259 keys = _dbus_list_get_next_link (&entry->keys, keys);
260 values = _dbus_list_get_next_link (&entry->values, values);
267 append_unescaped_value (DBusString *unescaped,
268 const DBusString *escaped,
279 p = _dbus_string_get_const_data (escaped) + escaped_start;
280 end = p + escaped_len;
283 if (_DBUS_ADDRESS_OPTIONALLY_ESCAPED_BYTE (*p))
285 if (!_dbus_string_append_byte (unescaped, *p))
290 /* Efficiency is king */
299 dbus_set_error (error, DBUS_ERROR_BAD_ADDRESS,
300 "In D-Bus address, percent character was not followed by two hex digits");
309 _dbus_string_init_const (&hex, buf);
311 if (!_dbus_string_hex_decode (&hex, 0, &hex_end,
313 _dbus_string_get_length (unescaped)))
318 dbus_set_error (error, DBUS_ERROR_BAD_ADDRESS,
319 "In D-Bus address, percent character was followed by characters other than hex digits");
325 /* Error, should have been escaped */
326 dbus_set_error (error, DBUS_ERROR_BAD_ADDRESS,
327 "In D-Bus address, character '%c' should have been escaped\n",
338 if (!ret && error && !dbus_error_is_set (error))
339 _DBUS_SET_OOM (error);
341 _dbus_assert (ret || error == NULL || dbus_error_is_set (error));
347 * Parses an address string of the form:
349 * method:key=value,key=value;method:key=value
351 * See the D-Bus specification for complete docs on the format.
353 * When connecting to an address, the first address entries
354 * in the semicolon-separated list should be tried first.
356 * @param address the address.
357 * @param entry return location to an array of entries.
358 * @param array_len return location for array length.
359 * @param error address where an error can be returned.
360 * @returns #TRUE on success, #FALSE otherwise.
363 dbus_parse_address (const char *address,
364 DBusAddressEntry ***entry,
369 int pos, end_pos, len, i;
370 DBusList *entries, *link;
371 DBusAddressEntry **entry_array;
373 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
375 _dbus_string_init_const (&str, address);
379 len = _dbus_string_get_length (&str);
383 dbus_set_error (error, DBUS_ERROR_BAD_ADDRESS,
384 "Empty address '%s'", address);
390 DBusAddressEntry *entry;
394 entry = create_entry ();
397 dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
402 /* Append the entry */
403 if (!_dbus_list_append (&entries, entry))
405 dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
406 dbus_address_entry_free (entry);
410 /* Look for a semi-colon */
411 if (!_dbus_string_find (&str, pos, ";", &end_pos))
414 /* Look for the colon : */
415 if (!_dbus_string_find_to (&str, pos, end_pos, ":", &found_pos))
417 dbus_set_error (error, DBUS_ERROR_BAD_ADDRESS, "Address does not contain a colon");
421 if (!_dbus_string_copy_len (&str, pos, found_pos - pos, &entry->method, 0))
423 dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
429 while (pos < end_pos)
431 int comma_pos, equals_pos;
433 if (!_dbus_string_find_to (&str, pos, end_pos, ",", &comma_pos))
436 if (!_dbus_string_find_to (&str, pos, comma_pos, "=", &equals_pos) ||
437 equals_pos == pos || equals_pos + 1 == comma_pos)
439 dbus_set_error (error, DBUS_ERROR_BAD_ADDRESS,
440 "'=' character not found or has no value following it");
448 key = dbus_new0 (DBusString, 1);
452 dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
456 value = dbus_new0 (DBusString, 1);
459 dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
464 if (!_dbus_string_init (key))
466 dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
473 if (!_dbus_string_init (value))
475 dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
476 _dbus_string_free (key);
483 if (!_dbus_string_copy_len (&str, pos, equals_pos - pos, key, 0))
485 dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
486 _dbus_string_free (key);
487 _dbus_string_free (value);
494 if (!append_unescaped_value (value, &str, equals_pos + 1,
495 comma_pos - equals_pos - 1, error))
497 _dbus_assert (error == NULL || dbus_error_is_set (error));
498 _dbus_string_free (key);
499 _dbus_string_free (value);
506 if (!_dbus_list_append (&entry->keys, key))
508 dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
509 _dbus_string_free (key);
510 _dbus_string_free (value);
517 if (!_dbus_list_append (&entry->values, value))
519 dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
520 _dbus_string_free (value);
533 *array_len = _dbus_list_get_length (&entries);
535 entry_array = dbus_new (DBusAddressEntry *, *array_len + 1);
539 dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
544 entry_array [*array_len] = NULL;
546 link = _dbus_list_get_first_link (&entries);
550 entry_array[i] = link->data;
552 link = _dbus_list_get_next_link (&entries, link);
555 _dbus_list_clear (&entries);
556 *entry = entry_array;
562 link = _dbus_list_get_first_link (&entries);
565 dbus_address_entry_free (link->data);
566 link = _dbus_list_get_next_link (&entries, link);
569 _dbus_list_clear (&entries);
576 * Escapes the given string as a value in a key=value pair
577 * for a D-Bus address.
579 * @param value the unescaped value
580 * @returns newly-allocated escaped value or #NULL if no memory
583 dbus_address_escape_value (const char *value)
586 DBusString unescaped;
591 _dbus_string_init_const (&unescaped, value);
593 if (!_dbus_string_init (&escaped))
596 if (!_dbus_address_append_escaped (&escaped, &unescaped))
599 if (!_dbus_string_steal_data (&escaped, &ret))
603 _dbus_string_free (&escaped);
608 * Unescapes the given string as a value in a key=value pair
609 * for a D-Bus address. Note that dbus_address_entry_get_value()
610 * returns an already-unescaped value.
612 * @param value the escaped value
613 * @param error error to set if the unescaping fails
614 * @returns newly-allocated unescaped value or #NULL if no memory
617 dbus_address_unescape_value (const char *value,
620 DBusString unescaped;
626 _dbus_string_init_const (&escaped, value);
628 if (!_dbus_string_init (&unescaped))
631 if (!append_unescaped_value (&unescaped, &escaped,
632 0, _dbus_string_get_length (&escaped),
636 if (!_dbus_string_steal_data (&unescaped, &ret))
640 if (ret == NULL && error && !dbus_error_is_set (error))
641 _DBUS_SET_OOM (error);
643 _dbus_assert (ret != NULL || error == NULL || dbus_error_is_set (error));
645 _dbus_string_free (&unescaped);
649 /** @} */ /* End of public API */
651 #ifdef DBUS_BUILD_TESTS
653 #ifndef DOXYGEN_SHOULD_SKIP_THIS
655 #include "dbus-test.h"
661 const char *unescaped;
664 static const EscapeTest escape_tests[] = {
665 { "abcde", "abcde" },
670 { "abc%24", "abc$" },
671 { "%24abc", "$abc" },
672 { "abc%24abc", "abc$abc" },
684 static const char* invalid_escaped_values[] = {
698 _dbus_address_test (void)
700 DBusAddressEntry **entries;
702 DBusError error = DBUS_ERROR_INIT;
706 while (i < _DBUS_N_ELEMENTS (escape_tests))
708 const EscapeTest *test = &escape_tests[i];
712 escaped = dbus_address_escape_value (test->unescaped);
714 _dbus_assert_not_reached ("oom");
716 if (strcmp (escaped, test->escaped) != 0)
718 _dbus_warn ("Escaped '%s' as '%s' should have been '%s'\n",
719 test->unescaped, escaped, test->escaped);
724 unescaped = dbus_address_unescape_value (test->escaped, &error);
725 if (unescaped == NULL)
727 _dbus_warn ("Failed to unescape '%s': %s\n",
728 test->escaped, error.message);
729 dbus_error_free (&error);
733 if (strcmp (unescaped, test->unescaped) != 0)
735 _dbus_warn ("Unescaped '%s' as '%s' should have been '%s'\n",
736 test->escaped, unescaped, test->unescaped);
739 dbus_free (unescaped);
745 while (i < _DBUS_N_ELEMENTS (invalid_escaped_values))
749 unescaped = dbus_address_unescape_value (invalid_escaped_values[i],
751 if (unescaped != NULL)
753 _dbus_warn ("Should not have successfully unescaped '%s' to '%s'\n",
754 invalid_escaped_values[i], unescaped);
755 dbus_free (unescaped);
759 _dbus_assert (dbus_error_is_set (&error));
760 dbus_error_free (&error);
765 if (!dbus_parse_address ("unix:path=/tmp/foo;debug:name=test,sliff=sloff;",
766 &entries, &len, &error))
767 _dbus_assert_not_reached ("could not parse address");
768 _dbus_assert (len == 2);
769 _dbus_assert (strcmp (dbus_address_entry_get_value (entries[0], "path"), "/tmp/foo") == 0);
770 _dbus_assert (strcmp (dbus_address_entry_get_value (entries[1], "name"), "test") == 0);
771 _dbus_assert (strcmp (dbus_address_entry_get_value (entries[1], "sliff"), "sloff") == 0);
773 dbus_address_entries_free (entries);
775 /* Different possible errors */
776 if (dbus_parse_address ("", &entries, &len, &error))
777 _dbus_assert_not_reached ("Parsed incorrect address.");
779 dbus_error_free (&error);
781 if (dbus_parse_address ("foo", &entries, &len, &error))
782 _dbus_assert_not_reached ("Parsed incorrect address.");
784 dbus_error_free (&error);
786 if (dbus_parse_address ("foo:bar", &entries, &len, &error))
787 _dbus_assert_not_reached ("Parsed incorrect address.");
789 dbus_error_free (&error);
791 if (dbus_parse_address ("foo:bar,baz", &entries, &len, &error))
792 _dbus_assert_not_reached ("Parsed incorrect address.");
794 dbus_error_free (&error);
796 if (dbus_parse_address ("foo:bar=foo,baz", &entries, &len, &error))
797 _dbus_assert_not_reached ("Parsed incorrect address.");
799 dbus_error_free (&error);
801 if (dbus_parse_address ("foo:bar=foo;baz", &entries, &len, &error))
802 _dbus_assert_not_reached ("Parsed incorrect address.");
804 dbus_error_free (&error);
806 if (dbus_parse_address ("foo:=foo", &entries, &len, &error))
807 _dbus_assert_not_reached ("Parsed incorrect address.");
809 dbus_error_free (&error);
811 if (dbus_parse_address ("foo:foo=", &entries, &len, &error))
812 _dbus_assert_not_reached ("Parsed incorrect address.");
814 dbus_error_free (&error);
816 if (dbus_parse_address ("foo:foo,bar=baz", &entries, &len, &error))
817 _dbus_assert_not_reached ("Parsed incorrect address.");
819 dbus_error_free (&error);
824 #endif /* !DOXYGEN_SHOULD_SKIP_THIS */