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 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->xml = wpabuf_alloc(6000);
  33. if (iface->xml == NULL) {
  34. os_free(iface);
  35. return NULL;
  36. }
  37. wpabuf_printf(iface->xml, "<interface name=\"%s\">", dbus_interface);
  38. dl_list_add_tail(list, &iface->list);
  39. iface->dbus_interface = os_strdup(dbus_interface);
  40. return iface;
  41. }
  42. static void add_arg(struct wpabuf *xml, const char *name, const char *type,
  43. const char *direction)
  44. {
  45. wpabuf_printf(xml, "<arg name=\"%s\"", name);
  46. if (type)
  47. wpabuf_printf(xml, " type=\"%s\"", type);
  48. if (direction)
  49. wpabuf_printf(xml, " direction=\"%s\"", direction);
  50. wpabuf_put_str(xml, "/>");
  51. }
  52. static void add_entry(struct wpabuf *xml, const char *type, const char *name,
  53. const struct wpa_dbus_argument *args, int include_dir)
  54. {
  55. const struct wpa_dbus_argument *arg;
  56. if (args == NULL || args->name == NULL) {
  57. wpabuf_printf(xml, "<%s name=\"%s\"/>", type, name);
  58. return;
  59. }
  60. wpabuf_printf(xml, "<%s name=\"%s\">", type, name);
  61. for (arg = args; arg && arg->name; arg++) {
  62. add_arg(xml, arg->name, arg->type,
  63. include_dir ? (arg->dir == ARG_IN ? "in" : "out") :
  64. NULL);
  65. }
  66. wpabuf_printf(xml, "</%s>", type);
  67. }
  68. static void add_property(struct wpabuf *xml,
  69. const struct wpa_dbus_property_desc *dsc)
  70. {
  71. wpabuf_printf(xml, "<property name=\"%s\" type=\"%s\" "
  72. "access=\"%s%s\"/>",
  73. dsc->dbus_property, dsc->type,
  74. dsc->getter ? "read" : "",
  75. dsc->setter ? "write" : "");
  76. }
  77. static void extract_interfaces_methods(
  78. struct dl_list *list, const struct wpa_dbus_method_desc *methods)
  79. {
  80. const struct wpa_dbus_method_desc *dsc;
  81. struct interfaces *iface;
  82. for (dsc = methods; dsc && dsc->dbus_method; dsc++) {
  83. iface = add_interface(list, dsc->dbus_interface);
  84. if (iface)
  85. add_entry(iface->xml, "method", dsc->dbus_method,
  86. dsc->args, 1);
  87. }
  88. }
  89. static void extract_interfaces_signals(
  90. struct dl_list *list, const struct wpa_dbus_signal_desc *signals)
  91. {
  92. const struct wpa_dbus_signal_desc *dsc;
  93. struct interfaces *iface;
  94. for (dsc = signals; dsc && dsc->dbus_signal; dsc++) {
  95. iface = add_interface(list, dsc->dbus_interface);
  96. if (iface)
  97. add_entry(iface->xml, "signal", dsc->dbus_signal,
  98. dsc->args, 0);
  99. }
  100. }
  101. static void extract_interfaces_properties(
  102. struct dl_list *list, const struct wpa_dbus_property_desc *properties)
  103. {
  104. const struct wpa_dbus_property_desc *dsc;
  105. struct interfaces *iface;
  106. for (dsc = properties; dsc && dsc->dbus_property; dsc++) {
  107. iface = add_interface(list, dsc->dbus_interface);
  108. if (iface)
  109. add_property(iface->xml, dsc);
  110. }
  111. }
  112. /**
  113. * extract_interfaces - Extract interfaces from methods, signals and props
  114. * @list: Interface list to be filled
  115. * @obj_dsc: Description of object from which interfaces will be extracted
  116. *
  117. * Iterates over all methods, signals, and properties registered with an
  118. * object and collects all declared DBus interfaces and create interfaces'
  119. * node in XML root node for each. Returned list elements contain interface
  120. * name and XML node of corresponding interface.
  121. */
  122. static void extract_interfaces(struct dl_list *list,
  123. struct wpa_dbus_object_desc *obj_dsc)
  124. {
  125. extract_interfaces_methods(list, obj_dsc->methods);
  126. extract_interfaces_signals(list, obj_dsc->signals);
  127. extract_interfaces_properties(list, obj_dsc->properties);
  128. }
  129. static void add_interfaces(struct dl_list *list, struct wpabuf *xml)
  130. {
  131. struct interfaces *iface, *n;
  132. dl_list_for_each_safe(iface, n, list, struct interfaces, list) {
  133. if (wpabuf_len(iface->xml) + 20 < wpabuf_tailroom(xml)) {
  134. wpabuf_put_buf(xml, iface->xml);
  135. wpabuf_put_str(xml, "</interface>");
  136. } else {
  137. wpa_printf(MSG_DEBUG, "dbus: Not enough room for "
  138. "add_interfaces inspect data: tailroom %u, "
  139. "add %u",
  140. (unsigned int) wpabuf_tailroom(xml),
  141. (unsigned int) wpabuf_len(iface->xml));
  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(10000);
  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. }