24
25
#include "dbus-internals.h"
25
26
#include "dbus-connection-internal.h"
27
#include "dbus-nonce.h"
26
28
#include "dbus-transport-socket.h"
27
29
#include "dbus-transport-protected.h"
28
30
#include "dbus-watch.h"
29
31
#include "dbus-credentials.h"
33
34
* @defgroup DBusTransportSocket DBusTransport implementations for sockets
34
35
* @ingroup DBusInternals
73
74
DBusTransportSocket *socket_transport = (DBusTransportSocket*) transport;
75
_dbus_verbose ("%s start\n", _DBUS_FUNCTION_NAME);
76
_dbus_verbose ("start\n");
77
78
if (socket_transport->read_watch)
176
177
DBusTransportSocket *socket_transport = (DBusTransportSocket*) transport;
177
178
dbus_bool_t need_read_watch;
179
_dbus_verbose ("%s: fd = %d\n",
180
_DBUS_FUNCTION_NAME, socket_transport->fd);
180
_dbus_verbose ("fd = %d\n",socket_transport->fd);
182
182
if (transport->connection == NULL)
193
193
if (_dbus_transport_get_is_authenticated (transport))
194
194
need_read_watch =
195
_dbus_counter_get_value (transport->live_messages_size) < transport->max_live_messages_size;
195
(_dbus_counter_get_size_value (transport->live_messages) < transport->max_live_messages_size) &&
196
(_dbus_counter_get_unix_fd_value (transport->live_messages) < transport->max_live_messages_unix_fds);
198
199
if (transport->receive_credentials_pending)
552
553
if (_dbus_auth_needs_encoding (transport->auth))
555
/* Does fd passing even make sense with encoded data? */
556
_dbus_assert(!DBUS_TRANSPORT_CAN_SEND_UNIX_FD(transport));
554
558
if (_dbus_string_get_length (&socket_transport->encoded_outgoing) == 0)
556
560
if (!_dbus_auth_encode_data (transport->auth,
590
594
_dbus_verbose ("message is %d bytes\n",
591
total_bytes_to_write);
595
total_bytes_to_write);
594
if (socket_transport->message_bytes_written < header_len)
598
#ifdef HAVE_UNIX_FD_PASSING
599
if (socket_transport->message_bytes_written <= 0 && DBUS_TRANSPORT_CAN_SEND_UNIX_FD(transport))
601
/* Send the fds along with the first byte of the message */
605
_dbus_message_get_unix_fds(message, &unix_fds, &n);
597
_dbus_write_socket_two (socket_transport->fd,
599
socket_transport->message_bytes_written,
600
header_len - socket_transport->message_bytes_written,
608
_dbus_write_socket_with_unix_fds_two (socket_transport->fd,
610
socket_transport->message_bytes_written,
611
header_len - socket_transport->message_bytes_written,
617
if (bytes_written > 0 && n > 0)
618
_dbus_verbose("Wrote %i unix fds\n", n);
623
if (socket_transport->message_bytes_written < header_len)
626
_dbus_write_socket_two (socket_transport->fd,
628
socket_transport->message_bytes_written,
629
header_len - socket_transport->message_bytes_written,
636
_dbus_write_socket (socket_transport->fd,
607
_dbus_write_socket (socket_transport->fd,
609
(socket_transport->message_bytes_written - header_len),
611
(socket_transport->message_bytes_written - header_len));
638
(socket_transport->message_bytes_written - header_len),
640
(socket_transport->message_bytes_written - header_len));
673
_dbus_verbose ("%s: fd = %d\n", _DBUS_FUNCTION_NAME,
674
socket_transport->fd);
703
_dbus_verbose ("fd = %d\n",socket_transport->fd);
676
705
/* No messages without authentication! */
677
706
if (!_dbus_transport_get_is_authenticated (transport))
705
734
if (_dbus_auth_needs_decoding (transport->auth))
736
/* Does fd passing even make sense with encoded data? */
737
_dbus_assert(!DBUS_TRANSPORT_CAN_SEND_UNIX_FD(transport));
707
739
if (_dbus_string_get_length (&socket_transport->encoded_incoming) > 0)
708
740
bytes_read = _dbus_string_get_length (&socket_transport->encoded_incoming);
749
781
_dbus_message_loader_get_buffer (transport->loader,
752
bytes_read = _dbus_read_socket (socket_transport->fd,
753
buffer, socket_transport->max_bytes_read_per_iteration);
784
#ifdef HAVE_UNIX_FD_PASSING
785
if (DBUS_TRANSPORT_CAN_SEND_UNIX_FD(transport))
789
if (!_dbus_message_loader_get_unix_fds(transport->loader, &fds, &n_fds))
791
_dbus_verbose ("Out of memory reading file descriptors\n");
792
_dbus_message_loader_return_buffer (transport->loader, buffer, 0);
797
bytes_read = _dbus_read_socket_with_unix_fds(socket_transport->fd,
799
socket_transport->max_bytes_read_per_iteration,
802
if (bytes_read >= 0 && n_fds > 0)
803
_dbus_verbose("Read %i unix fds\n", n_fds);
805
_dbus_message_loader_return_unix_fds(transport->loader, fds, bytes_read < 0 ? 0 : n_fds);
810
bytes_read = _dbus_read_socket (socket_transport->fd,
811
buffer, socket_transport->max_bytes_read_per_iteration);
755
814
_dbus_message_loader_return_buffer (transport->loader,
757
816
bytes_read < 0 ? 0 : bytes_read);
1059
1118
if (flags & DBUS_ITERATION_BLOCK)
1061
_dbus_verbose ("unlock %s pre poll\n", _DBUS_FUNCTION_NAME);
1120
_dbus_verbose ("unlock pre poll\n");
1062
1121
_dbus_connection_unlock (transport->connection);
1071
1130
if (flags & DBUS_ITERATION_BLOCK)
1073
_dbus_verbose ("lock %s post poll\n", _DBUS_FUNCTION_NAME);
1132
_dbus_verbose ("lock post poll\n");
1074
1133
_dbus_connection_lock (transport->connection);
1204
1263
&socket_vtable,
1205
1264
server_guid, address))
1267
#ifdef HAVE_UNIX_FD_PASSING
1268
_dbus_auth_set_unix_fd_possible(socket_transport->base.auth, _dbus_socket_can_pass_unix_fd(fd));
1208
1271
socket_transport->fd = fd;
1209
1272
socket_transport->message_bytes_written = 0;
1234
1297
* @param host the host to connect to
1235
1298
* @param port the port to connect to
1236
1299
* @param family the address family to connect to
1300
* @param path to nonce file
1237
1301
* @param error location to store reason for failure.
1238
1302
* @returns a new transport, or #NULL on failure.
1258
1323
if (host == NULL)
1259
1324
host = "localhost";
1261
if (!_dbus_string_append (&address, "tcp:"))
1326
if (!_dbus_string_append (&address, noncefile ? "nonce-tcp:" : "tcp:"))
1264
1329
if (!_dbus_string_append (&address, "host=") ||
1274
1339
!_dbus_string_append (&address, family)))
1277
fd = _dbus_connect_tcp_socket (host, port, family, error);
1342
if (noncefile != NULL &&
1343
(!_dbus_string_append (&address, "noncefile=") ||
1344
!_dbus_string_append (&address, noncefile)))
1347
fd = _dbus_connect_tcp_socket_with_nonce (host, port, family, noncefile, error);
1280
1350
_DBUS_ASSERT_ERROR_IS_SET (error);
1318
1386
DBusError *error)
1320
1388
const char *method;
1390
dbus_bool_t isNonceTcp;
1322
1392
method = dbus_address_entry_get_method (entry);
1323
1393
_dbus_assert (method != NULL);
1325
if (strcmp (method, "tcp") == 0)
1395
isTcp = strcmp (method, "tcp") == 0;
1396
isNonceTcp = strcmp (method, "nonce-tcp") == 0;
1398
if (isTcp || isNonceTcp)
1327
1400
const char *host = dbus_address_entry_get_value (entry, "host");
1328
1401
const char *port = dbus_address_entry_get_value (entry, "port");
1329
1402
const char *family = dbus_address_entry_get_value (entry, "family");
1403
const char *noncefile = dbus_address_entry_get_value (entry, "noncefile");
1405
if ((isNonceTcp == TRUE) != (noncefile != NULL)) {
1406
_dbus_set_bad_address (error, method, "noncefile", NULL);
1407
return DBUS_TRANSPORT_OPEN_BAD_ADDRESS;
1331
1410
if (port == NULL)
1333
_dbus_set_bad_address (error, "tcp", "port", NULL);
1412
_dbus_set_bad_address (error, method, "port", NULL);
1334
1413
return DBUS_TRANSPORT_OPEN_BAD_ADDRESS;
1337
*transport_p = _dbus_transport_new_for_tcp_socket (host, port, family, error);
1416
*transport_p = _dbus_transport_new_for_tcp_socket (host, port, family, noncefile, error);
1338
1417
if (*transport_p == NULL)
1340
1419
_DBUS_ASSERT_ERROR_IS_SET (error);