dbus_new_introspect.c 7.7 KB

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