*
*/
+#include <config.h>
#include "dbus-internals.h"
#include "dbus-connection-internal.h"
+#include "dbus-nonce.h"
#include "dbus-transport-socket.h"
#include "dbus-transport-protected.h"
#include "dbus-watch.h"
#include "dbus-credentials.h"
+#include <stdio.h> //todo to remove
/**
{
DBusTransportSocket *socket_transport = (DBusTransportSocket*) transport;
- _dbus_verbose ("%s start\n", _DBUS_FUNCTION_NAME);
+ _dbus_verbose ("start\n");
if (socket_transport->read_watch)
{
socket_transport->write_watch = NULL;
}
- _dbus_verbose ("%s end\n", _DBUS_FUNCTION_NAME);
+ _dbus_verbose ("end\n");
}
static void
{
DBusTransportSocket *socket_transport = (DBusTransportSocket*) transport;
- _dbus_verbose ("%s\n", _DBUS_FUNCTION_NAME);
+ _dbus_verbose ("\n");
free_watches (transport);
DBusTransportSocket *socket_transport = (DBusTransportSocket*) transport;
dbus_bool_t need_read_watch;
- _dbus_verbose ("%s: fd = %d\n",
- _DBUS_FUNCTION_NAME, socket_transport->fd);
+ _dbus_verbose ("fd = %d\n",socket_transport->fd);
if (transport->connection == NULL)
return;
if (_dbus_transport_get_is_authenticated (transport))
need_read_watch =
- _dbus_counter_get_value (transport->live_messages_size) < transport->max_live_messages_size;
+ (_dbus_counter_get_size_value (transport->live_messages) < transport->max_live_messages_size) &&
+ (_dbus_counter_get_unix_fd_value (transport->live_messages) < transport->max_live_messages_unix_fds);
else
{
if (transport->receive_credentials_pending)
int header_len, body_len;
int total_bytes_to_write;
+// int i;
+
if (total > socket_transport->max_bytes_written_per_iteration)
{
_dbus_verbose ("%d bytes exceeds %d bytes written per iteration, returning\n",
header_len = _dbus_string_get_length (header);
body_len = _dbus_string_get_length (body);
+ /* fprintf (stderr, "\nheader:\n"); //todo for test - to remove together wit int i declaration above
+ for(i=0; i < header_len; i++)
+ {
+ fprintf (stderr, "%02x", _dbus_string_get_byte(header,i));
+ }
+ fprintf (stderr,"\n");
+ for(i=0; i < header_len; i++)
+ {
+ fprintf (stderr, "%c", _dbus_string_get_byte(header,i));
+ }
+ fprintf (stderr, "\nret size: %u, i: %u\n", header_len, i);
+
+
+ fprintf (stderr, "\nbody:\n");
+ for(i=0; i < body_len; i++)
+ {
+ fprintf (stderr, "%02x", _dbus_string_get_byte(body,i));
+ }
+ fprintf (stderr, "\nret size: %u, i: %u\n", body_len, i);
+ fprintf (stderr,"\n");
+ for(i=0; i < body_len; i++)
+ {
+ fprintf (stderr, "%c", _dbus_string_get_byte(body,i));
+ }
+ fprintf (stderr, "\nret size: %u, i: %u\n", body_len, i);*/
+
if (_dbus_auth_needs_encoding (transport->auth))
{
+ /* Does fd passing even make sense with encoded data? */
+ _dbus_assert(!DBUS_TRANSPORT_CAN_SEND_UNIX_FD(transport));
+
if (_dbus_string_get_length (&socket_transport->encoded_outgoing) == 0)
{
if (!_dbus_auth_encode_data (transport->auth,
#if 0
_dbus_verbose ("message is %d bytes\n",
- total_bytes_to_write);
+ total_bytes_to_write);
#endif
-
- if (socket_transport->message_bytes_written < header_len)
+
+#ifdef HAVE_UNIX_FD_PASSING
+ if (socket_transport->message_bytes_written <= 0 && DBUS_TRANSPORT_CAN_SEND_UNIX_FD(transport))
{
+ /* Send the fds along with the first byte of the message */
+ const int *unix_fds;
+ unsigned n;
+
+ _dbus_message_get_unix_fds(message, &unix_fds, &n);
+
bytes_written =
- _dbus_write_socket_two (socket_transport->fd,
- header,
- socket_transport->message_bytes_written,
- header_len - socket_transport->message_bytes_written,
- body,
- 0, body_len);
+ _dbus_write_socket_with_unix_fds_two (socket_transport->fd,
+ header,
+ socket_transport->message_bytes_written,
+ header_len - socket_transport->message_bytes_written,
+ body,
+ 0, body_len,
+ unix_fds,
+ n);
+
+ if (bytes_written > 0 && n > 0)
+ _dbus_verbose("Wrote %i unix fds\n", n);
}
else
+#endif
{
- bytes_written =
- _dbus_write_socket (socket_transport->fd,
- body,
- (socket_transport->message_bytes_written - header_len),
- body_len -
- (socket_transport->message_bytes_written - header_len));
+ if (socket_transport->message_bytes_written < header_len)
+ {
+ bytes_written =
+ _dbus_write_socket_two (socket_transport->fd,
+ header,
+ socket_transport->message_bytes_written,
+ header_len - socket_transport->message_bytes_written,
+ body,
+ 0, body_len);
+ }
+ else
+ {
+ bytes_written =
+ _dbus_write_socket (socket_transport->fd,
+ body,
+ (socket_transport->message_bytes_written - header_len),
+ body_len -
+ (socket_transport->message_bytes_written - header_len));
+ }
}
}
_dbus_string_set_length (&socket_transport->encoded_outgoing, 0);
_dbus_string_compact (&socket_transport->encoded_outgoing, 2048);
- _dbus_connection_message_sent (transport->connection,
- message);
+ _dbus_connection_message_sent_unlocked (transport->connection,
+ message);
}
}
}
int total;
dbus_bool_t oom;
- _dbus_verbose ("%s: fd = %d\n", _DBUS_FUNCTION_NAME,
- socket_transport->fd);
+ _dbus_verbose ("fd = %d\n",socket_transport->fd);
/* No messages without authentication! */
if (!_dbus_transport_get_is_authenticated (transport))
if (_dbus_auth_needs_decoding (transport->auth))
{
+ /* Does fd passing even make sense with encoded data? */
+ _dbus_assert(!DBUS_TRANSPORT_CAN_SEND_UNIX_FD(transport));
+
if (_dbus_string_get_length (&socket_transport->encoded_incoming) > 0)
bytes_read = _dbus_string_get_length (&socket_transport->encoded_incoming);
else
{
_dbus_message_loader_get_buffer (transport->loader,
&buffer);
-
- bytes_read = _dbus_read_socket (socket_transport->fd,
- buffer, socket_transport->max_bytes_read_per_iteration);
-
+
+#ifdef HAVE_UNIX_FD_PASSING
+ if (DBUS_TRANSPORT_CAN_SEND_UNIX_FD(transport))
+ {
+ int *fds, n_fds;
+
+ if (!_dbus_message_loader_get_unix_fds(transport->loader, &fds, &n_fds))
+ {
+ _dbus_verbose ("Out of memory reading file descriptors\n");
+ _dbus_message_loader_return_buffer (transport->loader, buffer, 0);
+ oom = TRUE;
+ goto out;
+ }
+
+ bytes_read = _dbus_read_socket_with_unix_fds(socket_transport->fd,
+ buffer,
+ socket_transport->max_bytes_read_per_iteration,
+ fds, &n_fds);
+
+ if (bytes_read >= 0 && n_fds > 0)
+ _dbus_verbose("Read %i unix fds\n", n_fds);
+
+ _dbus_message_loader_return_unix_fds(transport->loader, fds, bytes_read < 0 ? 0 : n_fds);
+ }
+ else
+#endif
+ {
+ bytes_read = _dbus_read_socket (socket_transport->fd,
+ buffer, socket_transport->max_bytes_read_per_iteration);
+ }
+
_dbus_message_loader_return_buffer (transport->loader,
buffer,
bytes_read < 0 ? 0 : bytes_read);
}
-
+
if (bytes_read < 0)
{
/* EINTR already handled for us */
{
DBusTransportSocket *socket_transport = (DBusTransportSocket*) transport;
- _dbus_verbose ("%s\n", _DBUS_FUNCTION_NAME);
+ _dbus_verbose ("\n");
free_watches (transport);
*/
if (flags & DBUS_ITERATION_BLOCK)
{
- _dbus_verbose ("unlock %s pre poll\n", _DBUS_FUNCTION_NAME);
+ _dbus_verbose ("unlock pre poll\n");
_dbus_connection_unlock (transport->connection);
}
if (flags & DBUS_ITERATION_BLOCK)
{
- _dbus_verbose ("lock %s post poll\n", _DBUS_FUNCTION_NAME);
+ _dbus_verbose ("lock post poll\n");
_dbus_connection_lock (transport->connection);
}
&socket_vtable,
server_guid, address))
goto failed_4;
-
+
+#ifdef HAVE_UNIX_FD_PASSING
+ _dbus_auth_set_unix_fd_possible(socket_transport->base.auth, _dbus_socket_can_pass_unix_fd(fd));
+#endif
+
socket_transport->fd = fd;
socket_transport->message_bytes_written = 0;
return (DBusTransport*) socket_transport;
failed_4:
+ _dbus_watch_invalidate (socket_transport->read_watch);
_dbus_watch_unref (socket_transport->read_watch);
failed_3:
+ _dbus_watch_invalidate (socket_transport->write_watch);
_dbus_watch_unref (socket_transport->write_watch);
failed_2:
_dbus_string_free (&socket_transport->encoded_incoming);
* @param host the host to connect to
* @param port the port to connect to
* @param family the address family to connect to
+ * @param path to nonce file
* @param error location to store reason for failure.
* @returns a new transport, or #NULL on failure.
*/
_dbus_transport_new_for_tcp_socket (const char *host,
const char *port,
const char *family,
+ const char *noncefile,
DBusError *error)
{
int fd;
if (host == NULL)
host = "localhost";
- if (!_dbus_string_append (&address, "tcp:"))
+ if (!_dbus_string_append (&address, noncefile ? "nonce-tcp:" : "tcp:"))
goto error;
if (!_dbus_string_append (&address, "host=") ||
goto error;
if (family != NULL &&
- (!_dbus_string_append (&address, "family=") ||
+ (!_dbus_string_append (&address, ",family=") ||
!_dbus_string_append (&address, family)))
goto error;
- fd = _dbus_connect_tcp_socket (host, port, family, error);
+ if (noncefile != NULL &&
+ (!_dbus_string_append (&address, ",noncefile=") ||
+ !_dbus_string_append (&address, noncefile)))
+ goto error;
+
+ fd = _dbus_connect_tcp_socket_with_nonce (host, port, family, noncefile, error);
if (fd < 0)
{
_DBUS_ASSERT_ERROR_IS_SET (error);
return NULL;
}
- _dbus_fd_set_close_on_exec (fd);
-
_dbus_verbose ("Successfully connected to tcp socket %s:%s\n",
host, port);
transport = _dbus_transport_new_for_socket (fd, NULL, &address);
+ _dbus_string_free (&address);
if (transport == NULL)
{
dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
_dbus_close_socket (fd, NULL);
- _dbus_string_free (&address);
fd = -1;
}
- _dbus_string_free (&address);
-
return transport;
error:
DBusError *error)
{
const char *method;
+ dbus_bool_t isTcp;
+ dbus_bool_t isNonceTcp;
method = dbus_address_entry_get_method (entry);
_dbus_assert (method != NULL);
- if (strcmp (method, "tcp") == 0)
+ isTcp = strcmp (method, "tcp") == 0;
+ isNonceTcp = strcmp (method, "nonce-tcp") == 0;
+
+ if (isTcp || isNonceTcp)
{
const char *host = dbus_address_entry_get_value (entry, "host");
const char *port = dbus_address_entry_get_value (entry, "port");
const char *family = dbus_address_entry_get_value (entry, "family");
+ const char *noncefile = dbus_address_entry_get_value (entry, "noncefile");
+
+ if ((isNonceTcp == TRUE) != (noncefile != NULL)) {
+ _dbus_set_bad_address (error, method, "noncefile", NULL);
+ return DBUS_TRANSPORT_OPEN_BAD_ADDRESS;
+ }
if (port == NULL)
{
- _dbus_set_bad_address (error, "tcp", "port", NULL);
+ _dbus_set_bad_address (error, method, "port", NULL);
return DBUS_TRANSPORT_OPEN_BAD_ADDRESS;
}
- *transport_p = _dbus_transport_new_for_tcp_socket (host, port, family, error);
+ *transport_p = _dbus_transport_new_for_tcp_socket (host, port, family, noncefile, error);
if (*transport_p == NULL)
{
_DBUS_ASSERT_ERROR_IS_SET (error);