dbus_new_introspect.c 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  1. /*
  2. * wpa_supplicant - D-Bus introspection
  3. * Copyright (c) 2006, Dan Williams <dcbw@redhat.com> and Red Hat, Inc.
  4. * Copyright (c) 2009, Witold Sowa <witold.sowa@gmail.com>
  5. * Copyright (c) 2010, Jouni Malinen <j@w1.fi>
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License version 2 as
  9. * published by the Free Software Foundation.
  10. *
  11. * Alternatively, this software may be distributed under the terms of BSD
  12. * license.
  13. *
  14. * See README and COPYING for more details.
  15. */
  16. #include "utils/includes.h"
  17. #include "utils/common.h"
  18. #include "utils/list.h"
  19. #include "utils/wpabuf.h"
  20. #include "dbus_common_i.h"
  21. #include "dbus_new_helpers.h"
  22. struct interfaces {
  23. struct dl_list list;
  24. char *dbus_interface;
  25. struct wpabuf *xml;
  26. };
  27. static struct interfaces * add_interface(struct dl_list *list,
  28. const char *dbus_interface)
  29. {
  30. struct interfaces *iface;
  31. dl_list_for_each(iface, list, struct interfaces, list) {
  32. if (os_strcmp(iface->dbus_interface, dbus_interface) == 0)
  33. return iface; /* already in the list */
  34. }
  35. iface = os_zalloc(sizeof(struct interfaces));
  36. if (!iface)
  37. return NULL;
  38. iface->xml = wpabuf_alloc(6000);
  39. if (iface->xml == NULL) {
  40. os_free(iface);
  41. return NULL;
  42. }
  43. wpabuf_printf(iface->xml, "<interface name=\"%s\">", dbus_interface);
  44. dl_list_add_tail(list, &iface->list);
  45. iface->dbus_interface = os_strdup(dbus_interface);
  46. return iface;
  47. }
  48. static void add_arg(struct wpabuf *xml, const char *name, const char *type,
  49. const char *direction)
  50. {
  51. wpabuf_printf(xml, "<arg name=\"%s\"", name);
  52. if (type)
  53. wpabuf_printf(xml, " type=\"%s\"", type);
  54. if (direction)
  55. wpabuf_printf(xml, " direction=\"%s\"", direction);
  56. wpabuf_put_str(xml, "/>");
  57. }
  58. static void add_entry(struct wpabuf *xml, const char *type, const char *name,
  59. const struct wpa_dbus_argument *args, int include_dir)
  60. {
  61. const struct wpa_dbus_argument *arg;
  62. if (args == NULL || args->name == NULL) {
  63. wpabuf_printf(xml, "<%s name=\"%s\"/>", type, name);
  64. return;
  65. }
  66. wpabuf_printf(xml, "<%s name=\"%s\">", type, name);
  67. for (arg = args; arg && arg->name; arg++) {
  68. add_arg(xml, arg->name, arg->type,
  69. include_dir ? (arg->dir == ARG_IN ? "in" : "out") :
  70. NULL);
  71. }
  72. wpabuf_printf(xml, "</%s>", type);
  73. }
  74. static void add_property(struct wpabuf *xml,
  75. const struct wpa_dbus_property_desc *dsc)
  76. {
  77. wpabuf_printf(xml, "<property name=\"%s\" type=\"%s\" "
  78. "access=\"%s%s\"/>",
  79. dsc->dbus_property, dsc->type,
  80. dsc->getter ? "read" : "",
  81. dsc->setter ? "write" : "");
  82. }
  83. static void extract_interfaces_methods(
  84. struct dl_list *list, const struct wpa_dbus_method_desc *methods)
  85. {
  86. const struct wpa_dbus_method_desc *dsc;
  87. struct interfaces *iface;
  88. for (dsc = methods; dsc && dsc->dbus_method; dsc++) {
  89. iface = add_interface(list, dsc->dbus_interface);
  90. if (iface)
  91. add_entry(iface->xml, "method", dsc->dbus_method,
  92. dsc->args, 1);
  93. }
  94. }
  95. static void extract_interfaces_signals(
  96. struct dl_list *list, const struct wpa_dbus_signal_desc *signals)
  97. {
  98. const struct wpa_dbus_signal_desc *dsc;
  99. struct interfaces *iface;
  100. for (dsc = signals; dsc && dsc->dbus_signal; dsc++) {
  101. iface = add_interface(list, dsc->dbus_interface);
  102. if (iface)
  103. add_entry(iface->xml, "signal", dsc->dbus_signal,
  104. dsc->args, 0);
  105. }
  106. }
  107. static void extract_interfaces_properties(
  108. struct dl_list *list, const struct wpa_dbus_property_desc *properties)
  109. {
  110. const struct wpa_dbus_property_desc *dsc;
  111. struct interfaces *iface;
  112. for (dsc = properties; dsc && dsc->dbus_property; dsc++) {
  113. iface = add_interface(list, dsc->dbus_interface);
  114. if (iface)
  115. add_property(iface->xml, dsc);
  116. }
  117. }
  118. /**
  119. * extract_interfaces - Extract interfaces from methods, signals and props
  120. * @list: Interface list to be filled
  121. * @obj_dsc: Description of object from which interfaces will be extracted
  122. *
  123. * Iterates over all methods, signals, and properties registered with an
  124. * object and collects all declared DBus interfaces and create interfaces'
  125. * node in XML root node for each. Returned list elements contain interface
  126. * name and XML node of corresponding interface.
  127. */
  128. static void extract_interfaces(struct dl_list *list,
  129. struct wpa_dbus_object_desc *obj_dsc)
  130. {
  131. extract_interfaces_methods(list, obj_dsc->methods);
  132. extract_interfaces_signals(list, obj_dsc->signals);
  133. extract_interfaces_properties(list, obj_dsc->properties);
  134. }
  135. static void add_interfaces(struct dl_list *list, struct wpabuf *xml)
  136. {
  137. struct interfaces *iface, *n;
  138. dl_list_for_each_safe(iface, n, list, struct interfaces, list) {
  139. if (wpabuf_len(iface->xml) + 20 < wpabuf_tailroom(xml)) {
  140. wpabuf_put_buf(xml, iface->xml);
  141. wpabuf_put_str(xml, "</interface>");
  142. }
  143. dl_list_del(&iface->list);
  144. wpabuf_free(iface->xml);
  145. os_free(iface->dbus_interface);
  146. os_free(iface);
  147. }
  148. }
  149. static void add_child_nodes(struct wpabuf *xml, DBusConnection *con,
  150. const char *path)
  151. {
  152. char **children;
  153. int i;
  154. /* add child nodes to introspection tree */
  155. dbus_connection_list_registered(con, path, &children);
  156. for (i = 0; children[i]; i++)
  157. wpabuf_printf(xml, "<node name=\"%s\"/>", children[i]);
  158. dbus_free_string_array(children);
  159. }
  160. static void add_introspectable_interface(struct wpabuf *xml)
  161. {
  162. wpabuf_printf(xml, "<interface name=\"%s\">"
  163. "<method name=\"%s\">"
  164. "<arg name=\"data\" type=\"s\" direction=\"out\"/>"
  165. "</method>"
  166. "</interface>",
  167. WPA_DBUS_INTROSPECTION_INTERFACE,
  168. WPA_DBUS_INTROSPECTION_METHOD);
  169. }
  170. static void add_properties_interface(struct wpabuf *xml)
  171. {
  172. wpabuf_printf(xml, "<interface name=\"%s\">",
  173. WPA_DBUS_PROPERTIES_INTERFACE);
  174. wpabuf_printf(xml, "<method name=\"%s\">", WPA_DBUS_PROPERTIES_GET);
  175. add_arg(xml, "interface", "s", "in");
  176. add_arg(xml, "propname", "s", "in");
  177. add_arg(xml, "value", "v", "out");
  178. wpabuf_put_str(xml, "</method>");
  179. wpabuf_printf(xml, "<method name=\"%s\">", WPA_DBUS_PROPERTIES_GETALL);
  180. add_arg(xml, "interface", "s", "in");
  181. add_arg(xml, "props", "a{sv}", "out");
  182. wpabuf_put_str(xml, "</method>");
  183. wpabuf_printf(xml, "<method name=\"%s\">", WPA_DBUS_PROPERTIES_SET);
  184. add_arg(xml, "interface", "s", "in");
  185. add_arg(xml, "propname", "s", "in");
  186. add_arg(xml, "value", "v", "in");
  187. wpabuf_put_str(xml, "</method>");
  188. wpabuf_put_str(xml, "</interface>");
  189. }
  190. static void add_wpas_interfaces(struct wpabuf *xml,
  191. struct wpa_dbus_object_desc *obj_dsc)
  192. {
  193. struct dl_list ifaces;
  194. dl_list_init(&ifaces);
  195. extract_interfaces(&ifaces, obj_dsc);
  196. add_interfaces(&ifaces, xml);
  197. }
  198. /**
  199. * wpa_dbus_introspect - Responds for Introspect calls on object
  200. * @message: Message with Introspect call
  201. * @obj_dsc: Object description on which Introspect was called
  202. * Returns: Message with introspection result XML string as only argument
  203. *
  204. * Iterates over all methods, signals and properties registered with
  205. * object and generates introspection data for the object as XML string.
  206. */
  207. DBusMessage * wpa_dbus_introspect(DBusMessage *message,
  208. struct wpa_dbus_object_desc *obj_dsc)
  209. {
  210. DBusMessage *reply;
  211. struct wpabuf *xml;
  212. xml = wpabuf_alloc(8000);
  213. if (xml == NULL)
  214. return NULL;
  215. wpabuf_put_str(xml, "<?xml version=\"1.0\"?>\n");
  216. wpabuf_put_str(xml, DBUS_INTROSPECT_1_0_XML_DOCTYPE_DECL_NODE);
  217. wpabuf_put_str(xml, "<node>");
  218. add_introspectable_interface(xml);
  219. add_properties_interface(xml);
  220. add_wpas_interfaces(xml, obj_dsc);
  221. add_child_nodes(xml, obj_dsc->connection,
  222. dbus_message_get_path(message));
  223. wpabuf_put_str(xml, "</node>\n");
  224. reply = dbus_message_new_method_return(message);
  225. if (reply) {
  226. const char *intro_str = wpabuf_head(xml);
  227. dbus_message_append_args(reply, DBUS_TYPE_STRING, &intro_str,
  228. DBUS_TYPE_INVALID);
  229. }
  230. wpabuf_free(xml);
  231. return reply;
  232. }