25 #include "dbus-internals.h"
26 #include "dbus-connection-internal.h"
27 #include "dbus-nonce.h"
28 #include "dbus-transport-socket.h"
29 #include "dbus-transport-protected.h"
30 #include "dbus-watch.h"
31 #include "dbus-credentials.h"
76 _dbus_verbose (
"start\n");
98 _dbus_verbose (
"end\n");
106 _dbus_verbose (
"\n");
108 free_watches (transport);
146 DBusAuthState auth_state;
154 if (auth_state == DBUS_AUTH_STATE_HAVE_BYTES_TO_SEND ||
155 auth_state == DBUS_AUTH_STATE_WAITING_FOR_MEMORY)
162 _dbus_verbose (
"check_write_watch(): needed = %d on connection %p watch %p fd = %d outgoing messages exist %d\n",
164 socket_transport->
fd,
180 _dbus_verbose (
"fd = %d\n",socket_transport->
fd);
200 need_read_watch =
TRUE;
207 DBusAuthState auth_state;
217 if (auth_state == DBUS_AUTH_STATE_WAITING_FOR_INPUT ||
218 auth_state == DBUS_AUTH_STATE_WAITING_FOR_MEMORY ||
219 auth_state == DBUS_AUTH_STATE_AUTHENTICATED)
220 need_read_watch =
TRUE;
222 need_read_watch =
FALSE;
226 _dbus_verbose (
" setting read watch enabled = %d\n", need_read_watch);
259 bytes_read > 0 ? bytes_read : 0);
263 _dbus_verbose (
" read %d bytes in auth phase\n", bytes_read);
267 else if (bytes_read < 0)
279 _dbus_verbose (
"Error reading from remote app: %s\n",
281 do_io_error (transport);
290 _dbus_verbose (
"Disconnected from remote app\n");
291 do_io_error (transport);
313 if (bytes_written > 0)
318 else if (bytes_written < 0)
326 _dbus_verbose (
"Error writing to remote app: %s\n",
328 do_io_error (transport);
344 _dbus_verbose (
"exchange_credentials: do_reading = %d, do_writing = %d\n",
345 do_reading, do_writing);
356 _dbus_verbose (
"Failed to write credentials: %s\n", error.
message);
358 do_io_error (transport);
379 _dbus_verbose (
"Failed to read credentials %s\n", error.
message);
381 do_io_error (transport);
416 *auth_completed =
FALSE;
425 if (!exchange_credentials (transport, do_reading, do_writing))
435 _dbus_verbose (
"send_credentials_pending = %d receive_credentials_pending = %d\n",
441 #define TRANSPORT_SIDE(t) ((t)->is_server ? "server" : "client")
444 case DBUS_AUTH_STATE_WAITING_FOR_INPUT:
445 _dbus_verbose (
" %s auth state: waiting for input\n",
446 TRANSPORT_SIDE (transport));
447 if (!do_reading || !read_data_into_auth (transport, &oom))
451 case DBUS_AUTH_STATE_WAITING_FOR_MEMORY:
452 _dbus_verbose (
" %s auth state: waiting for memory\n",
453 TRANSPORT_SIDE (transport));
458 case DBUS_AUTH_STATE_HAVE_BYTES_TO_SEND:
459 _dbus_verbose (
" %s auth state: bytes to send\n",
460 TRANSPORT_SIDE (transport));
461 if (!do_writing || !write_data_from_auth (transport))
465 case DBUS_AUTH_STATE_NEED_DISCONNECT:
466 _dbus_verbose (
" %s auth state: need to disconnect\n",
467 TRANSPORT_SIDE (transport));
468 do_io_error (transport);
471 case DBUS_AUTH_STATE_AUTHENTICATED:
472 _dbus_verbose (
" %s auth state: authenticated\n",
473 TRANSPORT_SIDE (transport));
482 check_read_watch (transport);
483 check_write_watch (transport);
503 _dbus_verbose (
"Not authenticated, not writing anything\n");
509 _dbus_verbose (
"Not connected, not writing anything\n");
514 _dbus_verbose (
"do_writing(), have_messages = %d, fd = %d\n",
516 socket_transport->
fd);
529 int header_len, body_len;
530 int total_bytes_to_write;
534 _dbus_verbose (
"%d bytes exceeds %d bytes written per iteration, returning\n",
544 _dbus_verbose (
"writing message %p\n", message);
556 _dbus_assert(!DBUS_TRANSPORT_CAN_SEND_UNIX_FD(transport));
579 _dbus_verbose (
"encoded message is %d bytes\n",
580 total_bytes_to_write);
591 total_bytes_to_write = header_len + body_len;
594 _dbus_verbose (
"message is %d bytes\n",
595 total_bytes_to_write);
598 #ifdef HAVE_UNIX_FD_PASSING
608 _dbus_write_socket_with_unix_fds_two (socket_transport->
fd,
617 if (bytes_written > 0 && n > 0)
618 _dbus_verbose(
"Wrote %i unix fds\n", n);
645 if (bytes_written < 0)
657 _dbus_verbose (
"Error writing to remote app: %s\n",
659 do_io_error (transport);
665 _dbus_verbose (
" wrote %d bytes of %d\n", bytes_written,
666 total_bytes_to_write);
668 total += bytes_written;
672 total_bytes_to_write);
703 _dbus_verbose (
"fd = %d\n",socket_transport->
fd);
716 check_read_watch (transport);
720 _dbus_verbose (
"%d bytes exceeds %d bytes read per iteration, returning\n",
737 _dbus_assert(!DBUS_TRANSPORT_CAN_SEND_UNIX_FD(transport));
762 _dbus_verbose (
"Out of memory decoding incoming data\n");
784 #ifdef HAVE_UNIX_FD_PASSING
785 if (DBUS_TRANSPORT_CAN_SEND_UNIX_FD(transport))
791 _dbus_verbose (
"Out of memory reading file descriptors\n");
802 if (bytes_read >= 0 && n_fds > 0)
803 _dbus_verbose(
"Read %i unix fds\n", n_fds);
816 bytes_read < 0 ? 0 : bytes_read);
825 _dbus_verbose (
"Out of memory in read()/do_reading()\n");
833 _dbus_verbose (
"Error reading from remote app: %s\n",
835 do_io_error (transport);
839 else if (bytes_read == 0)
841 _dbus_verbose (
"Disconnected from remote app\n");
842 do_io_error (transport);
847 _dbus_verbose (
" read %d bytes\n", bytes_read);
854 _dbus_verbose (
" out of memory when queueing messages we just read in the transport\n");
885 _dbus_watch_get_enabled (transport->
read_watch))
906 if (!(flags &
DBUS_WATCH_READABLE) && unix_error_with_read_to_come (transport, watch, flags))
908 _dbus_verbose (
"Hang up or error on watch\n");
914 (flags & DBUS_WATCH_READABLE))
918 _dbus_verbose (
"handling read watch %p flags = %x\n",
921 if (!do_authentication (transport,
TRUE,
FALSE, &auth_finished))
932 if (!do_reading (transport))
934 _dbus_verbose (
"no memory to read\n");
940 _dbus_verbose (
"Not reading anything since we just completed the authentication\n");
947 _dbus_verbose (
"handling write watch, have_outgoing_messages = %d\n",
953 if (!do_writing (transport))
955 _dbus_verbose (
"no memory to write\n");
960 check_write_watch (transport);
962 #ifdef DBUS_ENABLE_VERBOSE_MODE
966 _dbus_verbose (
"asked to handle read watch with non-read condition 0x%x\n",
969 _dbus_verbose (
"asked to handle write watch with non-write condition 0x%x\n",
972 _dbus_verbose (
"asked to handle watch %p on fd %d that we don't recognize\n",
985 _dbus_verbose (
"\n");
987 free_watches (transport);
990 socket_transport->
fd = -1;
1018 check_read_watch (transport);
1019 check_write_watch (transport);
1034 int timeout_milliseconds)
1041 _dbus_verbose (
" iteration flags = %s%s timeout = %d read_watch = %p write_watch = %p fd = %d\n",
1042 flags & DBUS_ITERATION_DO_READING ?
"read" :
"",
1043 flags & DBUS_ITERATION_DO_WRITING ?
"write" :
"",
1044 timeout_milliseconds,
1047 socket_transport->
fd);
1055 poll_fd.
fd = socket_transport->
fd;
1070 if ((flags & DBUS_ITERATION_DO_WRITING) &&
1071 !(flags & (DBUS_ITERATION_DO_READING | DBUS_ITERATION_BLOCK)) &&
1075 do_writing (transport);
1084 if (flags & DBUS_ITERATION_DO_READING)
1088 if (flags & DBUS_ITERATION_DO_WRITING)
1093 DBusAuthState auth_state;
1098 auth_state == DBUS_AUTH_STATE_WAITING_FOR_INPUT)
1102 auth_state == DBUS_AUTH_STATE_HAVE_BYTES_TO_SEND)
1108 if (flags & DBUS_ITERATION_BLOCK)
1109 poll_timeout = timeout_milliseconds;
1118 if (flags & DBUS_ITERATION_BLOCK)
1120 _dbus_verbose (
"unlock pre poll\n");
1125 poll_res =
_dbus_poll (&poll_fd, 1, poll_timeout);
1130 if (flags & DBUS_ITERATION_BLOCK)
1132 _dbus_verbose (
"lock post poll\n");
1145 do_io_error (transport);
1152 _dbus_verbose (
"in iteration, need_read=%d need_write=%d\n",
1153 need_read, need_write);
1154 do_authentication (transport, need_read, need_write,
1155 &authentication_completed);
1158 if (authentication_completed)
1161 if (need_read && (flags & DBUS_ITERATION_DO_READING))
1162 do_reading (transport);
1163 if (need_write && (flags & DBUS_ITERATION_DO_WRITING))
1164 do_writing (transport);
1169 _dbus_verbose (
"Error from _dbus_poll(): %s\n",
1186 check_write_watch (transport);
1188 _dbus_verbose (
" ... leaving do_iteration()\n");
1195 check_read_watch (transport);
1205 *fd_p = socket_transport->
fd;
1212 socket_handle_watch,
1214 socket_connection_set,
1215 socket_do_iteration,
1216 socket_live_messages_changed,
1217 socket_get_socket_fd
1239 if (socket_transport ==
NULL)
1249 DBUS_WATCH_WRITABLE,
1256 DBUS_WATCH_READABLE,
1264 server_guid, address))
1267 #ifdef HAVE_UNIX_FD_PASSING
1271 socket_transport->
fd = fd;
1310 const char *noncefile,
1317 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
1339 if (family !=
NULL &&
1344 if (noncefile !=
NULL &&
1349 fd = _dbus_connect_tcp_socket_with_nonce (host, port, family, noncefile, error);
1352 _DBUS_ASSERT_ERROR_IS_SET (error);
1357 _dbus_verbose (
"Successfully connected to tcp socket %s:%s\n",
1362 if (transport ==
NULL)
1385 DBusTransportOpenResult
1397 isTcp = strcmp (method,
"tcp") == 0;
1398 isNonceTcp = strcmp (method,
"nonce-tcp") == 0;
1400 if (isTcp || isNonceTcp)
1407 if ((isNonceTcp ==
TRUE) != (noncefile !=
NULL)) {
1409 return DBUS_TRANSPORT_OPEN_BAD_ADDRESS;
1415 return DBUS_TRANSPORT_OPEN_BAD_ADDRESS;
1419 if (*transport_p ==
NULL)
1421 _DBUS_ASSERT_ERROR_IS_SET (error);
1422 return DBUS_TRANSPORT_OPEN_DID_NOT_CONNECT;
1426 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
1427 return DBUS_TRANSPORT_OPEN_OK;
1432 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
1433 return DBUS_TRANSPORT_OPEN_NOT_HANDLED;
Implementation details of DBusTransportSocket.
dbus_bool_t _dbus_string_append(DBusString *str, const char *buffer)
Appends a nul-terminated C-style string to a DBusString.
void dbus_message_lock(DBusMessage *message)
Locks a message.
DBusTransport base
Parent instance.
void _dbus_connection_toggle_watch_unlocked(DBusConnection *connection, DBusWatch *watch, dbus_bool_t enabled)
Toggles a watch and notifies app via connection's DBusWatchToggledFunction if available.
long max_live_messages_unix_fds
Max total unix fds of received messages.
const char * message
public error message field
Implementation of DBusWatch.
DBusTransport * _dbus_transport_new_for_tcp_socket(const char *host, const char *port, const char *family, const char *noncefile, DBusError *error)
Creates a new transport for the given hostname and port.
#define NULL
A null pointer, defined appropriately for C or C++.
DBusAuth * auth
Authentication conversation.
unsigned int disconnected
TRUE if we are disconnected.
void dbus_free(void *memory)
Frees a block of memory previously allocated by dbus_malloc() or dbus_malloc0().
void _dbus_message_loader_return_unix_fds(DBusMessageLoader *loader, int *fds, unsigned n_fds)
Returns a buffer obtained from _dbus_message_loader_get_unix_fds().
dbus_bool_t _dbus_socket_can_pass_unix_fd(int fd)
Checks whether file descriptors may be passed via the socket.
dbus_bool_t _dbus_auth_needs_decoding(DBusAuth *auth)
Called post-authentication, indicates whether we need to decode the message stream with _dbus_auth_de...
void _dbus_watch_invalidate(DBusWatch *watch)
Clears the file descriptor from a now-invalid watch object so that no one tries to use it...
long max_live_messages_size
Max total size of received messages.
void _dbus_connection_lock(DBusConnection *connection)
Acquires the connection lock.
dbus_bool_t _dbus_auth_encode_data(DBusAuth *auth, const DBusString *plaintext, DBusString *encoded)
Called post-authentication, encodes a block of bytes for sending to the peer.
DBUS_EXPORT dbus_bool_t dbus_watch_get_enabled(DBusWatch *watch)
Returns whether a watch is enabled or not.
#define _dbus_assert(condition)
Aborts with an error message if the condition is false.
int _dbus_write_socket_two(int fd, const DBusString *buffer1, int start1, int len1, const DBusString *buffer2, int start2, int len2)
Like _dbus_write_two() but only works on sockets and is thus available on Windows.
#define DBUS_ERROR_INIT
Expands to a suitable initializer for a DBusError on the stack.
const char * dbus_address_entry_get_method(DBusAddressEntry *entry)
Returns the method string of an address entry.
DBusTransportOpenResult _dbus_transport_open_socket(DBusAddressEntry *entry, DBusTransport **transport_p, DBusError *error)
Opens a TCP socket transport.
dbus_bool_t _dbus_transport_queue_messages(DBusTransport *transport)
Processes data we've read while handling a watch, potentially converting some of it to messages and q...
DBusAuthState _dbus_auth_do_work(DBusAuth *auth)
Analyzes buffered input and moves the auth conversation forward, returning the new state of the auth ...
void dbus_error_free(DBusError *error)
Frees an error that's been set (or just initialized), then reinitializes the error as in dbus_error_i...
int _dbus_read_socket_with_unix_fds(int fd, DBusString *buffer, int count, int *fds, int *n_fds)
Like _dbus_read_socket() but also tries to read unix fds from the socket.
A portable struct pollfd wrapper.
int max_bytes_written_per_iteration
To avoid blocking too long.
DBusConnection * connection
Connection owning this transport.
#define _DBUS_POLLIN
There is data to read.
unsigned int send_credentials_pending
TRUE if we need to send credentials
dbus_bool_t _dbus_string_init(DBusString *str)
Initializes a string.
DBusTransport * _dbus_transport_new_for_socket(int fd, const DBusString *server_guid, const DBusString *address)
Creates a new transport for the given socket file descriptor.
DBusMessage * _dbus_connection_get_message_to_send(DBusConnection *connection)
Gets the next outgoing message.
short events
Events to poll for.
dbus_bool_t _dbus_read_credentials_socket(int client_fd, DBusCredentials *credentials, DBusError *error)
Reads a single byte which must be nul (an error occurs otherwise), and reads unix credentials if avai...
DBusString encoded_incoming
Encoded version of current incoming data.
dbus_bool_t _dbus_transport_get_is_connected(DBusTransport *transport)
Returns TRUE if the transport has not been disconnected.
dbus_bool_t _dbus_transport_init_base(DBusTransport *transport, const DBusTransportVTable *vtable, const DBusString *server_guid, const DBusString *address)
Initializes the base class members of DBusTransport.
void _dbus_auth_get_buffer(DBusAuth *auth, DBusString **buffer)
Get a buffer to be used for reading bytes from the peer we're conversing with.
Internals of DBusMessage.
dbus_bool_t _dbus_auth_decode_data(DBusAuth *auth, const DBusString *encoded, DBusString *plaintext)
Called post-authentication, decodes a block of bytes received from the peer.
const char * dbus_address_entry_get_value(DBusAddressEntry *entry, const char *key)
Returns a value from a key of an entry.
#define dbus_new0(type, count)
Safe macro for using dbus_malloc0().
void _dbus_auth_bytes_sent(DBusAuth *auth, int bytes_sent)
Notifies the auth conversation object that the given number of bytes of the outgoing buffer have been...
void _dbus_connection_remove_watch_unlocked(DBusConnection *connection, DBusWatch *watch)
Removes a watch using the connection's DBusRemoveWatchFunction if available.
dbus_bool_t _dbus_string_compact(DBusString *str, int max_waste)
Compacts the string to avoid wasted memory.
unsigned int receive_credentials_pending
TRUE if we need to receive credentials
DBusCounter * live_messages
Counter for size/unix fds of all live messages.
dbus_uint32_t dbus_bool_t
A boolean, valid values are TRUE and FALSE.
dbus_bool_t _dbus_connection_has_messages_to_send_unlocked(DBusConnection *connection)
Checks whether there are messages in the outgoing message queue.
DBUS_EXPORT int dbus_watch_get_socket(DBusWatch *watch)
Returns a socket to be watched, on UNIX this will return -1 if our transport is not socket-based so d...
int _dbus_poll(DBusPollFD *fds, int n_fds, int timeout_milliseconds)
Wrapper for poll().
#define _DBUS_POLLOUT
Writing now will not block.
dbus_bool_t _dbus_transport_get_is_authenticated(DBusTransport *transport)
Returns TRUE if we have been authenticated.
Internals of DBusAddressEntry.
int _dbus_string_get_length(const DBusString *str)
Gets the length of a string (not including nul termination).
int _dbus_read_socket(int fd, DBusString *buffer, int count)
Like _dbus_read(), but only works on sockets so is available on Windows.
DBusWatch * write_watch
Watch for writability.
void _dbus_set_bad_address(DBusError *error, const char *address_problem_type, const char *address_problem_field, const char *address_problem_other)
Sets DBUS_ERROR_BAD_ADDRESS.
dbus_bool_t _dbus_connection_handle_watch(DBusWatch *watch, unsigned int condition, void *data)
A callback for use with dbus_watch_new() to create a DBusWatch.
dbus_bool_t _dbus_get_is_errno_enomem(void)
See if errno is ENOMEM.
Object representing an exception.
dbus_bool_t _dbus_send_credentials_socket(int server_fd, DBusError *error)
Sends a single nul byte with our UNIX credentials as ancillary data.
dbus_bool_t _dbus_connection_add_watch_unlocked(DBusConnection *connection, DBusWatch *watch)
Adds a watch using the connection's DBusAddWatchFunction if available.
void dbus_set_error(DBusError *error, const char *name, const char *format,...)
Assigns an error name and message to a DBusError.
The virtual table that must be implemented to create a new kind of transport.
int message_bytes_written
Number of bytes of current outgoing message that have been written.
void _dbus_connection_message_sent_unlocked(DBusConnection *connection, DBusMessage *message)
Notifies the connection that a message has been sent, so the message can be removed from the outgoing...
As in POLLERR (can't watch for this, but can be present in current state passed to dbus_watch_handle(...
void _dbus_string_free(DBusString *str)
Frees a string created by _dbus_string_init().
#define TRUE
Expands to "1".
DBusMessageLoader * loader
Message-loading buffer.
As in POLLHUP (can't watch for it, but can be present in current state passed to dbus_watch_handle())...
void _dbus_message_get_network_data(DBusMessage *message, const DBusString **header, const DBusString **body)
Gets the data to be sent over the network for this message.
void _dbus_message_loader_return_buffer(DBusMessageLoader *loader, DBusString *buffer, int bytes_read)
Returns a buffer obtained from _dbus_message_loader_get_buffer(), indicating to the loader how many b...
Object representing a transport such as a socket.
dbus_bool_t _dbus_auth_set_credentials(DBusAuth *auth, DBusCredentials *credentials)
Sets credentials received via reliable means from the operating system.
void _dbus_auth_set_unix_fd_possible(DBusAuth *auth, dbus_bool_t b)
Sets whether unix fd passing is potentially on the transport and hence shall be negotiated.
const char * _dbus_strerror_from_errno(void)
Get error message from errno.
int _dbus_write_socket(int fd, const DBusString *buffer, int start, int len)
Like _dbus_write(), but only supports sockets and is thus available on Windows.
dbus_bool_t _dbus_get_is_errno_eintr(void)
See if errno is EINTR.
dbus_bool_t _dbus_message_loader_get_unix_fds(DBusMessageLoader *loader, int **fds, unsigned *max_n_fds)
Gets the buffer to use for reading unix fds from the network.
DBusWatch * _dbus_watch_new(int fd, unsigned int flags, dbus_bool_t enabled, DBusWatchHandler handler, void *data, DBusFreeFunction free_data_function)
Creates a new DBusWatch.
DBusTransport * _dbus_transport_ref(DBusTransport *transport)
Increments the reference count for the transport.
void _dbus_message_loader_get_buffer(DBusMessageLoader *loader, DBusString **buffer)
Gets the buffer to use for reading data from the network.
dbus_bool_t _dbus_get_is_errno_eagain_or_ewouldblock(void)
See if errno is EAGAIN or EWOULDBLOCK (this has to be done differently for Winsock so is abstracted) ...
dbus_bool_t _dbus_auth_get_bytes_to_send(DBusAuth *auth, const DBusString **str)
Gets bytes that need to be sent to the peer we're conversing with.
DBusWatch * read_watch
Watch for readability.
#define DBUS_ERROR_NO_MEMORY
There was not enough memory to complete an operation.
#define FALSE
Expands to "0".
DBusCredentials * credentials
Credentials of other end read from the socket.
dbus_bool_t _dbus_string_set_length(DBusString *str, int length)
Sets the length of a string.
int max_bytes_read_per_iteration
To avoid blocking too long.
void _dbus_watch_unref(DBusWatch *watch)
Decrements the reference count of a DBusWatch object and finalizes the object if the count reaches ze...
dbus_bool_t _dbus_auth_needs_encoding(DBusAuth *auth)
Called post-authentication, indicates whether we need to encode the message stream with _dbus_auth_en...
void _dbus_message_get_unix_fds(DBusMessage *message, const int **fds, unsigned *n_fds)
Gets the unix fds to be sent over the network for this message.
dbus_bool_t _dbus_close_socket(int fd, DBusError *error)
Closes a socket.
void _dbus_watch_set_handler(DBusWatch *watch, DBusWatchHandler handler, void *data, DBusFreeFunction free_data_function)
Sets the handler for the watch.
void _dbus_transport_disconnect(DBusTransport *transport)
Closes our end of the connection to a remote application.
void _dbus_transport_finalize_base(DBusTransport *transport)
Finalizes base class members of DBusTransport.
void _dbus_connection_unlock(DBusConnection *connection)
Releases the connection lock.
short revents
Events that occurred.
dbus_bool_t _dbus_get_is_errno_epipe(void)
See if errno is EPIPE.
void _dbus_auth_return_buffer(DBusAuth *auth, DBusString *buffer, int bytes_read)
Returns a buffer with new data read into it.
void _dbus_transport_unref(DBusTransport *transport)
Decrements the reference count for the transport.
long _dbus_counter_get_unix_fd_value(DBusCounter *counter)
Gets the current value of the unix fd counter.
#define _DBUS_POLLERR
Error condition.
DBusString encoded_outgoing
Encoded version of current outgoing message.
long _dbus_counter_get_size_value(DBusCounter *counter)
Gets the current value of the size counter.