dbus-marshal-byteswap: Byte-swap Unix fd indexes if needed
[platform/upstream/dbus.git] / dbus / dbus-transport-protected.h
1 /* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*- */
2 /* dbus-transport-protected.h Used by subclasses of DBusTransport object (internal to D-Bus implementation)
3  *
4  * Copyright (C) 2002, 2004  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 #ifndef DBUS_TRANSPORT_PROTECTED_H
24 #define DBUS_TRANSPORT_PROTECTED_H
25
26 #include <dbus/dbus-internals.h>
27 #include <dbus/dbus-errors.h>
28 #include <dbus/dbus-transport.h>
29 #include <dbus/dbus-message-internal.h>
30 #include <dbus/dbus-auth.h>
31 #include <dbus/dbus-resources.h>
32
33 DBUS_BEGIN_DECLS
34
35 typedef struct DBusTransportVTable DBusTransportVTable;
36
37 /**
38  * The virtual table that must be implemented to
39  * create a new kind of transport.
40  */
41 struct DBusTransportVTable
42 {
43   void        (* finalize)              (DBusTransport *transport);
44   /**< The finalize method must free the transport. */
45
46   dbus_bool_t (* handle_watch)          (DBusTransport *transport,
47                                          DBusWatch     *watch,
48                                          unsigned int   flags);
49   /**< The handle_watch method handles reading/writing
50    * data as indicated by the flags.
51    */
52
53   void        (* disconnect)            (DBusTransport *transport);
54   /**< Disconnect this transport. */
55
56   dbus_bool_t (* connection_set)        (DBusTransport *transport);
57   /**< Called when transport->connection has been filled in */
58
59   void        (* do_iteration)          (DBusTransport *transport,
60                                          unsigned int   flags,
61                                          int            timeout_milliseconds);
62   /**< Called to do a single "iteration" (block on select/poll
63    * followed by reading or writing data).
64    */
65
66   void        (* live_messages_changed) (DBusTransport *transport);
67   /**< Outstanding messages counter changed */
68
69   dbus_bool_t (* get_socket_fd) (DBusTransport *transport,
70                                  DBusSocket    *fd_p);
71   /**< Get socket file descriptor */
72 };
73
74 typedef dbus_bool_t (*DBusTransportGetUnixUserFunction) (DBusTransport *transport,
75                                                          unsigned long *uid);
76 typedef dbus_bool_t (*DBusTransportGetUnixPIDFunction)  (DBusTransport *transport,
77                                                          unsigned long *uid);
78 typedef DBusMessage * (*DBusTransportSendSyncCallFunction) (DBusTransport *transport,
79                                                             DBusMessage   *message);
80 typedef dbus_bool_t (*DBusTransportAssureProtocolFunction) (DBusMessage **message);
81 /**
82  * Object representing a transport such as a socket.
83  * A transport can shuttle messages from point A to point B,
84  * and is the backend for a #DBusConnection.
85  *
86  */
87 struct DBusTransport
88 {
89   int refcount;                               /**< Reference count. */
90
91   const DBusTransportVTable *vtable;          /**< Virtual methods for this instance. */
92
93   DBusConnection *connection;                 /**< Connection owning this transport. */
94
95   DBusMessageLoader *loader;                  /**< Message-loading buffer. */
96
97   DBusAuth *auth;                             /**< Authentication conversation */
98
99   DBusCredentials *credentials;               /**< Credentials of other end read from the socket */  
100
101   long max_live_messages_size;                /**< Max total size of received messages. */
102   long max_live_messages_unix_fds;            /**< Max total unix fds of received messages. */
103
104   DBusCounter *live_messages;                 /**< Counter for size/unix fds of all live messages. */
105
106   char *address;                              /**< Address of the server we are connecting to (#NULL for the server side of a transport) */
107
108   char *expected_guid;                        /**< GUID we expect the server to have, #NULL on server side or if we don't have an expectation */
109   
110   DBusAllowUnixUserFunction unix_user_function; /**< Function for checking whether a user is authorized. */
111   void *unix_user_data;                         /**< Data for unix_user_function */
112   
113   DBusFreeFunction free_unix_user_data;         /**< Function to free unix_user_data */
114
115   DBusAllowWindowsUserFunction windows_user_function; /**< Function for checking whether a user is authorized. */
116   void *windows_user_data;                            /**< Data for windows_user_function */
117   
118   DBusFreeFunction free_windows_user_data;            /**< Function to free windows_user_data */
119
120   DBusTransportGetUnixUserFunction get_unix_user_function;      /**< Function for getting Unix user ID */
121   DBusTransportGetUnixPIDFunction get_unix_process_id_function; /**< Function for getting Unix process ID */
122   DBusTransportAssureProtocolFunction assure_protocol_function; /**< Function for converting messages, if needed */
123   DBusTransportSendSyncCallFunction send_sync_call_function;    /**< Function for sending synchronous calls */
124   int protocol;                               /**< type of protocol for this transport */
125   
126   unsigned int disconnected : 1;              /**< #TRUE if we are disconnected. */
127   unsigned int authenticated : 1;             /**< Cache of auth state; use _dbus_transport_peek_is_authenticated() to query value */
128   unsigned int send_credentials_pending : 1;  /**< #TRUE if we need to send credentials */
129   unsigned int receive_credentials_pending : 1; /**< #TRUE if we need to receive credentials */
130   unsigned int is_server : 1;                 /**< #TRUE if on the server side */
131   unsigned int unused_bytes_recovered : 1;    /**< #TRUE if we've recovered unused bytes from auth */
132   unsigned int allow_anonymous : 1;           /**< #TRUE if an anonymous client can connect */
133 };
134
135 dbus_bool_t _dbus_transport_init_base     (DBusTransport             *transport,
136                                            const DBusTransportVTable *vtable,
137                                            const DBusString          *server_guid,
138                                            const DBusString          *address);
139 dbus_bool_t _dbus_transport_init_base_authenticated     (DBusTransport             *transport,
140                                                          const DBusTransportVTable *vtable,
141                                                          const DBusString          *server_guid,
142                                                          const DBusString          *address);
143 void        _dbus_transport_finalize_base (DBusTransport             *transport);
144
145 void        _dbus_transport_set_get_unix_user_function       (DBusTransport                    *transport,
146                                                               DBusTransportGetUnixUserFunction  function);
147 void        _dbus_transport_set_get_unix_process_id_function (DBusTransport                    *transport,
148                                                               DBusTransportGetUnixPIDFunction   function);
149 void        _dbus_transport_set_send_sync_call_function      (DBusTransport                     *transport,
150                                                               DBusTransportSendSyncCallFunction  function);
151 void        _dbus_transport_set_assure_protocol_function     (DBusTransport                    *transport,
152                                                               DBusTransportAssureProtocolFunction function,
153                                                               int                               protocol);
154 int         _dbus_transport_get_protocol                     (DBusTransport                    *transport);
155
156 typedef enum
157 {
158   DBUS_TRANSPORT_OPEN_NOT_HANDLED,    /**< we aren't in charge of this address type */
159   DBUS_TRANSPORT_OPEN_OK,             /**< we set up the listen */
160   DBUS_TRANSPORT_OPEN_BAD_ADDRESS,    /**< malformed address */
161   DBUS_TRANSPORT_OPEN_DID_NOT_CONNECT /**< well-formed address but failed to set it up */
162 } DBusTransportOpenResult;
163
164 DBusTransportOpenResult _dbus_transport_open_platform_specific (DBusAddressEntry  *entry,
165                                                                 DBusTransport    **transport_p,
166                                                                 DBusError         *error);
167
168 #define DBUS_TRANSPORT_CAN_SEND_UNIX_FD(x)      \
169   _dbus_auth_get_unix_fd_negotiated((x)->auth)
170
171 DBUS_END_DECLS
172
173 #endif /* DBUS_TRANSPORT_PROTECTED_H */