dbus-connection.h 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527
  1. /* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*- */
  2. /* dbus-connection.h DBusConnection object
  3. *
  4. * Copyright (C) 2002, 2003 Red Hat Inc.
  5. *
  6. * Licensed under the Academic Free License version 2.1
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation; either version 2 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, write to the Free Software
  20. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  21. *
  22. */
  23. #if !defined (DBUS_INSIDE_DBUS_H) && !defined (DBUS_COMPILATION)
  24. #error "Only <dbus/dbus.h> can be included directly, this file may disappear or change contents."
  25. #endif
  26. #ifndef DBUS_CONNECTION_H
  27. #define DBUS_CONNECTION_H
  28. #include <dbus/dbus-errors.h>
  29. #include <dbus/dbus-macros.h>
  30. #include <dbus/dbus-memory.h>
  31. #include <dbus/dbus-message.h>
  32. #include <dbus/dbus-shared.h>
  33. DBUS_BEGIN_DECLS
  34. /**
  35. * @addtogroup DBusConnection
  36. * @{
  37. */
  38. /* documented in dbus-watch.c */
  39. typedef struct DBusWatch DBusWatch;
  40. /* documented in dbus-timeout.c */
  41. typedef struct DBusTimeout DBusTimeout;
  42. /** Opaque type representing preallocated resources so a message can be sent without further memory allocation. */
  43. typedef struct DBusPreallocatedSend DBusPreallocatedSend;
  44. /** Opaque type representing a method call that has not yet received a reply. */
  45. typedef struct DBusPendingCall DBusPendingCall;
  46. /** Opaque type representing a connection to a remote application and associated incoming/outgoing message queues. */
  47. typedef struct DBusConnection DBusConnection;
  48. /** Set of functions that must be implemented to handle messages sent to a particular object path. */
  49. typedef struct DBusObjectPathVTable DBusObjectPathVTable;
  50. /**
  51. * Indicates the status of a #DBusWatch.
  52. */
  53. typedef enum
  54. {
  55. DBUS_WATCH_READABLE = 1 << 0, /**< As in POLLIN */
  56. DBUS_WATCH_WRITABLE = 1 << 1, /**< As in POLLOUT */
  57. DBUS_WATCH_ERROR = 1 << 2, /**< As in POLLERR (can't watch for
  58. * this, but can be present in
  59. * current state passed to
  60. * dbus_watch_handle()).
  61. */
  62. DBUS_WATCH_HANGUP = 1 << 3 /**< As in POLLHUP (can't watch for
  63. * it, but can be present in current
  64. * state passed to
  65. * dbus_watch_handle()).
  66. */
  67. /* Internal to libdbus, there is also _DBUS_WATCH_NVAL in dbus-watch.h */
  68. } DBusWatchFlags;
  69. /**
  70. * Indicates the status of incoming data on a #DBusConnection. This determines whether
  71. * dbus_connection_dispatch() needs to be called.
  72. */
  73. typedef enum
  74. {
  75. DBUS_DISPATCH_DATA_REMAINS, /**< There is more data to potentially convert to messages. */
  76. DBUS_DISPATCH_COMPLETE, /**< All currently available data has been processed. */
  77. DBUS_DISPATCH_NEED_MEMORY /**< More memory is needed to continue. */
  78. } DBusDispatchStatus;
  79. /** Called when libdbus needs a new watch to be monitored by the main
  80. * loop. Returns #FALSE if it lacks enough memory to add the
  81. * watch. Set by dbus_connection_set_watch_functions() or
  82. * dbus_server_set_watch_functions().
  83. */
  84. typedef dbus_bool_t (* DBusAddWatchFunction) (DBusWatch *watch,
  85. void *data);
  86. /** Called when dbus_watch_get_enabled() may return a different value
  87. * than it did before. Set by dbus_connection_set_watch_functions()
  88. * or dbus_server_set_watch_functions().
  89. */
  90. typedef void (* DBusWatchToggledFunction) (DBusWatch *watch,
  91. void *data);
  92. /** Called when libdbus no longer needs a watch to be monitored by the
  93. * main loop. Set by dbus_connection_set_watch_functions() or
  94. * dbus_server_set_watch_functions().
  95. */
  96. typedef void (* DBusRemoveWatchFunction) (DBusWatch *watch,
  97. void *data);
  98. /** Called when libdbus needs a new timeout to be monitored by the main
  99. * loop. Returns #FALSE if it lacks enough memory to add the
  100. * watch. Set by dbus_connection_set_timeout_functions() or
  101. * dbus_server_set_timeout_functions().
  102. */
  103. typedef dbus_bool_t (* DBusAddTimeoutFunction) (DBusTimeout *timeout,
  104. void *data);
  105. /** Called when dbus_timeout_get_enabled() may return a different
  106. * value than it did before.
  107. * Set by dbus_connection_set_timeout_functions() or
  108. * dbus_server_set_timeout_functions().
  109. */
  110. typedef void (* DBusTimeoutToggledFunction) (DBusTimeout *timeout,
  111. void *data);
  112. /** Called when libdbus no longer needs a timeout to be monitored by the
  113. * main loop. Set by dbus_connection_set_timeout_functions() or
  114. * dbus_server_set_timeout_functions().
  115. */
  116. typedef void (* DBusRemoveTimeoutFunction) (DBusTimeout *timeout,
  117. void *data);
  118. /** Called when the return value of dbus_connection_get_dispatch_status()
  119. * may have changed. Set with dbus_connection_set_dispatch_status_function().
  120. */
  121. typedef void (* DBusDispatchStatusFunction) (DBusConnection *connection,
  122. DBusDispatchStatus new_status,
  123. void *data);
  124. /**
  125. * Called when the main loop's thread should be notified that there's now work
  126. * to do. Set with dbus_connection_set_wakeup_main_function().
  127. */
  128. typedef void (* DBusWakeupMainFunction) (void *data);
  129. /**
  130. * Called during authentication to check whether the given UNIX user
  131. * ID is allowed to connect, if the client tried to auth as a UNIX
  132. * user ID. Normally on Windows this would never happen. Set with
  133. * dbus_connection_set_unix_user_function().
  134. */
  135. typedef dbus_bool_t (* DBusAllowUnixUserFunction) (DBusConnection *connection,
  136. unsigned long uid,
  137. void *data);
  138. /**
  139. * Called during authentication to check whether the given Windows user
  140. * ID is allowed to connect, if the client tried to auth as a Windows
  141. * user ID. Normally on UNIX this would never happen. Set with
  142. * dbus_connection_set_windows_user_function().
  143. */
  144. typedef dbus_bool_t (* DBusAllowWindowsUserFunction) (DBusConnection *connection,
  145. const char *user_sid,
  146. void *data);
  147. /**
  148. * Called when a pending call now has a reply available. Set with
  149. * dbus_pending_call_set_notify().
  150. */
  151. typedef void (* DBusPendingCallNotifyFunction) (DBusPendingCall *pending,
  152. void *user_data);
  153. /**
  154. * Called when a message needs to be handled. The result indicates whether or
  155. * not more handlers should be run. Set with dbus_connection_add_filter().
  156. */
  157. typedef DBusHandlerResult (* DBusHandleMessageFunction) (DBusConnection *connection,
  158. DBusMessage *message,
  159. void *user_data);
  160. DBUS_EXPORT
  161. DBusConnection* dbus_connection_open (const char *address,
  162. DBusError *error);
  163. DBUS_EXPORT
  164. DBusConnection* dbus_connection_open_private (const char *address,
  165. DBusError *error);
  166. DBUS_EXPORT
  167. DBusConnection* dbus_connection_ref (DBusConnection *connection);
  168. DBUS_EXPORT
  169. void dbus_connection_unref (DBusConnection *connection);
  170. DBUS_EXPORT
  171. void dbus_connection_close (DBusConnection *connection);
  172. DBUS_EXPORT
  173. dbus_bool_t dbus_connection_get_is_connected (DBusConnection *connection);
  174. DBUS_EXPORT
  175. dbus_bool_t dbus_connection_get_is_authenticated (DBusConnection *connection);
  176. DBUS_EXPORT
  177. dbus_bool_t dbus_connection_get_is_anonymous (DBusConnection *connection);
  178. DBUS_EXPORT
  179. char* dbus_connection_get_server_id (DBusConnection *connection);
  180. DBUS_EXPORT
  181. dbus_bool_t dbus_connection_can_send_type (DBusConnection *connection,
  182. int type);
  183. DBUS_EXPORT
  184. void dbus_connection_set_exit_on_disconnect (DBusConnection *connection,
  185. dbus_bool_t exit_on_disconnect);
  186. DBUS_EXPORT
  187. void dbus_connection_flush (DBusConnection *connection);
  188. DBUS_EXPORT
  189. dbus_bool_t dbus_connection_read_write_dispatch (DBusConnection *connection,
  190. int timeout_milliseconds);
  191. DBUS_EXPORT
  192. dbus_bool_t dbus_connection_read_write (DBusConnection *connection,
  193. int timeout_milliseconds);
  194. DBUS_EXPORT
  195. DBusMessage* dbus_connection_borrow_message (DBusConnection *connection);
  196. DBUS_EXPORT
  197. void dbus_connection_return_message (DBusConnection *connection,
  198. DBusMessage *message);
  199. DBUS_EXPORT
  200. void dbus_connection_steal_borrowed_message (DBusConnection *connection,
  201. DBusMessage *message);
  202. DBUS_EXPORT
  203. DBusMessage* dbus_connection_pop_message (DBusConnection *connection);
  204. DBUS_EXPORT
  205. DBusDispatchStatus dbus_connection_get_dispatch_status (DBusConnection *connection);
  206. DBUS_EXPORT
  207. DBusDispatchStatus dbus_connection_dispatch (DBusConnection *connection);
  208. DBUS_EXPORT
  209. dbus_bool_t dbus_connection_has_messages_to_send (DBusConnection *connection);
  210. DBUS_EXPORT
  211. dbus_bool_t dbus_connection_send (DBusConnection *connection,
  212. DBusMessage *message,
  213. dbus_uint32_t *client_serial);
  214. DBUS_EXPORT
  215. dbus_bool_t dbus_connection_send_with_reply (DBusConnection *connection,
  216. DBusMessage *message,
  217. DBusPendingCall **pending_return,
  218. int timeout_milliseconds);
  219. DBUS_EXPORT
  220. DBusMessage * dbus_connection_send_with_reply_and_block (DBusConnection *connection,
  221. DBusMessage *message,
  222. int timeout_milliseconds,
  223. DBusError *error);
  224. DBUS_EXPORT
  225. dbus_bool_t dbus_connection_set_watch_functions (DBusConnection *connection,
  226. DBusAddWatchFunction add_function,
  227. DBusRemoveWatchFunction remove_function,
  228. DBusWatchToggledFunction toggled_function,
  229. void *data,
  230. DBusFreeFunction free_data_function);
  231. DBUS_EXPORT
  232. dbus_bool_t dbus_connection_set_timeout_functions (DBusConnection *connection,
  233. DBusAddTimeoutFunction add_function,
  234. DBusRemoveTimeoutFunction remove_function,
  235. DBusTimeoutToggledFunction toggled_function,
  236. void *data,
  237. DBusFreeFunction free_data_function);
  238. DBUS_EXPORT
  239. void dbus_connection_set_wakeup_main_function (DBusConnection *connection,
  240. DBusWakeupMainFunction wakeup_main_function,
  241. void *data,
  242. DBusFreeFunction free_data_function);
  243. DBUS_EXPORT
  244. void dbus_connection_set_dispatch_status_function (DBusConnection *connection,
  245. DBusDispatchStatusFunction function,
  246. void *data,
  247. DBusFreeFunction free_data_function);
  248. DBUS_EXPORT
  249. dbus_bool_t dbus_connection_get_unix_user (DBusConnection *connection,
  250. unsigned long *uid);
  251. DBUS_EXPORT
  252. dbus_bool_t dbus_connection_get_unix_process_id (DBusConnection *connection,
  253. unsigned long *pid);
  254. DBUS_EXPORT
  255. dbus_bool_t dbus_connection_get_adt_audit_session_data (DBusConnection *connection,
  256. void **data,
  257. dbus_int32_t *data_size);
  258. DBUS_EXPORT
  259. void dbus_connection_set_unix_user_function (DBusConnection *connection,
  260. DBusAllowUnixUserFunction function,
  261. void *data,
  262. DBusFreeFunction free_data_function);
  263. DBUS_EXPORT
  264. dbus_bool_t dbus_connection_get_windows_user (DBusConnection *connection,
  265. char **windows_sid_p);
  266. DBUS_EXPORT
  267. void dbus_connection_set_windows_user_function (DBusConnection *connection,
  268. DBusAllowWindowsUserFunction function,
  269. void *data,
  270. DBusFreeFunction free_data_function);
  271. DBUS_EXPORT
  272. void dbus_connection_set_allow_anonymous (DBusConnection *connection,
  273. dbus_bool_t value);
  274. DBUS_EXPORT
  275. void dbus_connection_set_route_peer_messages (DBusConnection *connection,
  276. dbus_bool_t value);
  277. /* Filters */
  278. DBUS_EXPORT
  279. dbus_bool_t dbus_connection_add_filter (DBusConnection *connection,
  280. DBusHandleMessageFunction function,
  281. void *user_data,
  282. DBusFreeFunction free_data_function);
  283. DBUS_EXPORT
  284. void dbus_connection_remove_filter (DBusConnection *connection,
  285. DBusHandleMessageFunction function,
  286. void *user_data);
  287. /* Other */
  288. DBUS_EXPORT
  289. dbus_bool_t dbus_connection_allocate_data_slot (dbus_int32_t *slot_p);
  290. DBUS_EXPORT
  291. void dbus_connection_free_data_slot (dbus_int32_t *slot_p);
  292. DBUS_EXPORT
  293. dbus_bool_t dbus_connection_set_data (DBusConnection *connection,
  294. dbus_int32_t slot,
  295. void *data,
  296. DBusFreeFunction free_data_func);
  297. DBUS_EXPORT
  298. void* dbus_connection_get_data (DBusConnection *connection,
  299. dbus_int32_t slot);
  300. DBUS_EXPORT
  301. void dbus_connection_set_change_sigpipe (dbus_bool_t will_modify_sigpipe);
  302. DBUS_EXPORT
  303. void dbus_connection_set_max_message_size (DBusConnection *connection,
  304. long size);
  305. DBUS_EXPORT
  306. long dbus_connection_get_max_message_size (DBusConnection *connection);
  307. DBUS_EXPORT
  308. void dbus_connection_set_max_received_size (DBusConnection *connection,
  309. long size);
  310. DBUS_EXPORT
  311. long dbus_connection_get_max_received_size (DBusConnection *connection);
  312. DBUS_EXPORT
  313. void dbus_connection_set_max_message_unix_fds (DBusConnection *connection,
  314. long n);
  315. DBUS_EXPORT
  316. long dbus_connection_get_max_message_unix_fds (DBusConnection *connection);
  317. DBUS_EXPORT
  318. void dbus_connection_set_max_received_unix_fds(DBusConnection *connection,
  319. long n);
  320. DBUS_EXPORT
  321. long dbus_connection_get_max_received_unix_fds(DBusConnection *connection);
  322. DBUS_EXPORT
  323. long dbus_connection_get_outgoing_size (DBusConnection *connection);
  324. DBUS_EXPORT
  325. long dbus_connection_get_outgoing_unix_fds (DBusConnection *connection);
  326. DBUS_EXPORT
  327. DBusPreallocatedSend* dbus_connection_preallocate_send (DBusConnection *connection);
  328. DBUS_EXPORT
  329. void dbus_connection_free_preallocated_send (DBusConnection *connection,
  330. DBusPreallocatedSend *preallocated);
  331. DBUS_EXPORT
  332. void dbus_connection_send_preallocated (DBusConnection *connection,
  333. DBusPreallocatedSend *preallocated,
  334. DBusMessage *message,
  335. dbus_uint32_t *client_serial);
  336. /* Object tree functionality */
  337. /**
  338. * Called when a #DBusObjectPathVTable is unregistered (or its connection is freed).
  339. * Found in #DBusObjectPathVTable.
  340. */
  341. typedef void (* DBusObjectPathUnregisterFunction) (DBusConnection *connection,
  342. void *user_data);
  343. /**
  344. * Called when a message is sent to a registered object path. Found in
  345. * #DBusObjectPathVTable which is registered with dbus_connection_register_object_path()
  346. * or dbus_connection_register_fallback().
  347. */
  348. typedef DBusHandlerResult (* DBusObjectPathMessageFunction) (DBusConnection *connection,
  349. DBusMessage *message,
  350. void *user_data);
  351. /**
  352. * Virtual table that must be implemented to handle a portion of the
  353. * object path hierarchy. Attach the vtable to a particular path using
  354. * dbus_connection_register_object_path() or
  355. * dbus_connection_register_fallback().
  356. */
  357. struct DBusObjectPathVTable
  358. {
  359. DBusObjectPathUnregisterFunction unregister_function; /**< Function to unregister this handler */
  360. DBusObjectPathMessageFunction message_function; /**< Function to handle messages */
  361. void (* dbus_internal_pad1) (void *); /**< Reserved for future expansion */
  362. void (* dbus_internal_pad2) (void *); /**< Reserved for future expansion */
  363. void (* dbus_internal_pad3) (void *); /**< Reserved for future expansion */
  364. void (* dbus_internal_pad4) (void *); /**< Reserved for future expansion */
  365. };
  366. DBUS_EXPORT
  367. dbus_bool_t dbus_connection_try_register_object_path (DBusConnection *connection,
  368. const char *path,
  369. const DBusObjectPathVTable *vtable,
  370. void *user_data,
  371. DBusError *error);
  372. DBUS_EXPORT
  373. dbus_bool_t dbus_connection_register_object_path (DBusConnection *connection,
  374. const char *path,
  375. const DBusObjectPathVTable *vtable,
  376. void *user_data);
  377. DBUS_EXPORT
  378. dbus_bool_t dbus_connection_try_register_fallback (DBusConnection *connection,
  379. const char *path,
  380. const DBusObjectPathVTable *vtable,
  381. void *user_data,
  382. DBusError *error);
  383. DBUS_EXPORT
  384. dbus_bool_t dbus_connection_register_fallback (DBusConnection *connection,
  385. const char *path,
  386. const DBusObjectPathVTable *vtable,
  387. void *user_data);
  388. DBUS_EXPORT
  389. dbus_bool_t dbus_connection_unregister_object_path (DBusConnection *connection,
  390. const char *path);
  391. DBUS_EXPORT
  392. dbus_bool_t dbus_connection_get_object_path_data (DBusConnection *connection,
  393. const char *path,
  394. void **data_p);
  395. DBUS_EXPORT
  396. dbus_bool_t dbus_connection_list_registered (DBusConnection *connection,
  397. const char *parent_path,
  398. char ***child_entries);
  399. DBUS_EXPORT
  400. dbus_bool_t dbus_connection_get_unix_fd (DBusConnection *connection,
  401. int *fd);
  402. DBUS_EXPORT
  403. dbus_bool_t dbus_connection_get_socket (DBusConnection *connection,
  404. int *fd);
  405. /**
  406. * Clear a variable or struct member that contains a #DBusConnection.
  407. * If it does not contain #NULL, the connection that was previously
  408. * there is unreferenced with dbus_connection_unref().
  409. *
  410. * For example, this function and the similar functions for
  411. * other reference-counted types can be used in code like this:
  412. *
  413. * @code
  414. * DBusConnection *conn = NULL;
  415. * struct { ...; DBusMessage *m; ... } *larger_structure = ...;
  416. *
  417. * ... code that might set conn or m to be non-NULL ...
  418. *
  419. * dbus_clear_connection (&conn);
  420. * dbus_clear_message (&larger_structure->m);
  421. * @endcode
  422. *
  423. * @param pointer_to_connection A pointer to a variable or struct member.
  424. * pointer_to_connection must not be #NULL, but *pointer_to_connection
  425. * may be #NULL.
  426. */
  427. static inline void
  428. dbus_clear_connection (DBusConnection **pointer_to_connection)
  429. {
  430. _dbus_clear_pointer_impl (DBusConnection, pointer_to_connection,
  431. dbus_connection_unref);
  432. }
  433. /** @} */
  434. /**
  435. * @addtogroup DBusWatch
  436. * @{
  437. */
  438. #ifndef DBUS_DISABLE_DEPRECATED
  439. DBUS_EXPORT
  440. DBUS_DEPRECATED int dbus_watch_get_fd (DBusWatch *watch);
  441. #endif
  442. DBUS_EXPORT
  443. int dbus_watch_get_unix_fd (DBusWatch *watch);
  444. DBUS_EXPORT
  445. int dbus_watch_get_socket (DBusWatch *watch);
  446. DBUS_EXPORT
  447. unsigned int dbus_watch_get_flags (DBusWatch *watch);
  448. DBUS_EXPORT
  449. void* dbus_watch_get_data (DBusWatch *watch);
  450. DBUS_EXPORT
  451. void dbus_watch_set_data (DBusWatch *watch,
  452. void *data,
  453. DBusFreeFunction free_data_function);
  454. DBUS_EXPORT
  455. dbus_bool_t dbus_watch_handle (DBusWatch *watch,
  456. unsigned int flags);
  457. DBUS_EXPORT
  458. dbus_bool_t dbus_watch_get_enabled (DBusWatch *watch);
  459. /** @} */
  460. /**
  461. * @addtogroup DBusTimeout
  462. * @{
  463. */
  464. DBUS_EXPORT
  465. int dbus_timeout_get_interval (DBusTimeout *timeout);
  466. DBUS_EXPORT
  467. void* dbus_timeout_get_data (DBusTimeout *timeout);
  468. DBUS_EXPORT
  469. void dbus_timeout_set_data (DBusTimeout *timeout,
  470. void *data,
  471. DBusFreeFunction free_data_function);
  472. DBUS_EXPORT
  473. dbus_bool_t dbus_timeout_handle (DBusTimeout *timeout);
  474. DBUS_EXPORT
  475. dbus_bool_t dbus_timeout_get_enabled (DBusTimeout *timeout);
  476. /** @} */
  477. DBUS_END_DECLS
  478. #endif /* DBUS_CONNECTION_H */