1 /* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*- */
2 /* dbus-transport-socket.c Socket subclasses of DBusTransport
4 * Copyright (C) 2002, 2003, 2004, 2006 Red Hat Inc.
6 * Licensed under the Academic Free License version 2.1
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.
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.
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
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"
34 * @defgroup DBusTransportSocket DBusTransport implementations for sockets
35 * @ingroup DBusInternals
36 * @brief Implementation details of DBusTransport on sockets
42 * Opaque object representing a socket file descriptor transport.
44 typedef struct DBusTransportSocket DBusTransportSocket;
47 * Implementation details of DBusTransportSocket. All members are private.
49 struct DBusTransportSocket
51 DBusTransport base; /**< Parent instance */
52 int fd; /**< File descriptor. */
53 DBusWatch *read_watch; /**< Watch for readability. */
54 DBusWatch *write_watch; /**< Watch for writability. */
56 int max_bytes_read_per_iteration; /**< To avoid blocking too long. */
57 int max_bytes_written_per_iteration; /**< To avoid blocking too long. */
59 int message_bytes_written; /**< Number of bytes of current
60 * outgoing message that have
63 DBusString encoded_outgoing; /**< Encoded version of current
66 DBusString encoded_incoming; /**< Encoded version of current
72 free_watches (DBusTransport *transport)
74 DBusTransportSocket *socket_transport = (DBusTransportSocket*) transport;
76 _dbus_verbose ("start\n");
78 if (socket_transport->read_watch)
80 if (transport->connection)
81 _dbus_connection_remove_watch_unlocked (transport->connection,
82 socket_transport->read_watch);
83 _dbus_watch_invalidate (socket_transport->read_watch);
84 _dbus_watch_unref (socket_transport->read_watch);
85 socket_transport->read_watch = NULL;
88 if (socket_transport->write_watch)
90 if (transport->connection)
91 _dbus_connection_remove_watch_unlocked (transport->connection,
92 socket_transport->write_watch);
93 _dbus_watch_invalidate (socket_transport->write_watch);
94 _dbus_watch_unref (socket_transport->write_watch);
95 socket_transport->write_watch = NULL;
98 _dbus_verbose ("end\n");
102 socket_finalize (DBusTransport *transport)
104 DBusTransportSocket *socket_transport = (DBusTransportSocket*) transport;
106 _dbus_verbose ("\n");
108 free_watches (transport);
110 _dbus_string_free (&socket_transport->encoded_outgoing);
111 _dbus_string_free (&socket_transport->encoded_incoming);
113 _dbus_transport_finalize_base (transport);
115 _dbus_assert (socket_transport->read_watch == NULL);
116 _dbus_assert (socket_transport->write_watch == NULL);
118 dbus_free (transport);
122 check_write_watch (DBusTransport *transport)
124 DBusTransportSocket *socket_transport = (DBusTransportSocket*) transport;
127 if (transport->connection == NULL)
130 if (transport->disconnected)
132 _dbus_assert (socket_transport->write_watch == NULL);
136 _dbus_transport_ref (transport);
138 if (_dbus_transport_try_to_authenticate (transport))
139 needed = _dbus_connection_has_messages_to_send_unlocked (transport->connection);
142 if (transport->send_credentials_pending)
146 DBusAuthState auth_state;
148 auth_state = _dbus_auth_do_work (transport->auth);
150 /* If we need memory we install the write watch just in case,
151 * if there's no need for it, it will get de-installed
152 * next time we try reading.
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",
163 needed, transport->connection, socket_transport->write_watch,
164 socket_transport->fd,
165 _dbus_connection_has_messages_to_send_unlocked (transport->connection));
167 _dbus_connection_toggle_watch_unlocked (transport->connection,
168 socket_transport->write_watch,
171 _dbus_transport_unref (transport);
175 check_read_watch (DBusTransport *transport)
177 DBusTransportSocket *socket_transport = (DBusTransportSocket*) transport;
178 dbus_bool_t need_read_watch;
180 _dbus_verbose ("fd = %d\n",socket_transport->fd);
182 if (transport->connection == NULL)
185 if (transport->disconnected)
187 _dbus_assert (socket_transport->read_watch == NULL);
191 _dbus_transport_ref (transport);
193 if (_dbus_transport_try_to_authenticate (transport))
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);
199 if (transport->receive_credentials_pending)
200 need_read_watch = TRUE;
203 /* The reason to disable need_read_watch when not WAITING_FOR_INPUT
204 * is to avoid spinning on the file descriptor when we're waiting
205 * to write or for some other part of the auth process
207 DBusAuthState auth_state;
209 auth_state = _dbus_auth_do_work (transport->auth);
211 /* If we need memory we install the read watch just in case,
212 * if there's no need for it, it will get de-installed
213 * next time we try reading. If we're authenticated we
214 * install it since we normally have it installed while
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);
227 _dbus_connection_toggle_watch_unlocked (transport->connection,
228 socket_transport->read_watch,
231 _dbus_transport_unref (transport);
235 do_io_error (DBusTransport *transport)
237 _dbus_transport_ref (transport);
238 _dbus_transport_disconnect (transport);
239 _dbus_transport_unref (transport);
242 /* return value is whether we successfully read any new data. */
244 read_data_into_auth (DBusTransport *transport,
247 DBusTransportSocket *socket_transport = (DBusTransportSocket*) transport;
253 _dbus_auth_get_buffer (transport->auth, &buffer);
255 bytes_read = _dbus_read_socket (socket_transport->fd,
256 buffer, socket_transport->max_bytes_read_per_iteration);
258 _dbus_auth_return_buffer (transport->auth, buffer,
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)
269 /* EINTR already handled for us */
271 if (_dbus_get_is_errno_enomem ())
275 else if (_dbus_get_is_errno_eagain_or_ewouldblock ())
276 ; /* do nothing, just return FALSE below */
279 _dbus_verbose ("Error reading from remote app: %s\n",
280 _dbus_strerror_from_errno ());
281 do_io_error (transport);
288 _dbus_assert (bytes_read == 0);
290 _dbus_verbose ("Disconnected from remote app\n");
291 do_io_error (transport);
297 /* Return value is whether we successfully wrote any bytes */
299 write_data_from_auth (DBusTransport *transport)
301 DBusTransportSocket *socket_transport = (DBusTransportSocket*) transport;
303 const DBusString *buffer;
305 if (!_dbus_auth_get_bytes_to_send (transport->auth,
309 bytes_written = _dbus_write_socket (socket_transport->fd,
311 0, _dbus_string_get_length (buffer));
313 if (bytes_written > 0)
315 _dbus_auth_bytes_sent (transport->auth, bytes_written);
318 else if (bytes_written < 0)
320 /* EINTR already handled for us */
322 if (_dbus_get_is_errno_eagain_or_ewouldblock ())
326 _dbus_verbose ("Error writing to remote app: %s\n",
327 _dbus_strerror_from_errno ());
328 do_io_error (transport);
337 exchange_credentials (DBusTransport *transport,
338 dbus_bool_t do_reading,
339 dbus_bool_t do_writing)
341 DBusTransportSocket *socket_transport = (DBusTransportSocket*) transport;
342 DBusError error = DBUS_ERROR_INIT;
344 _dbus_verbose ("exchange_credentials: do_reading = %d, do_writing = %d\n",
345 do_reading, do_writing);
347 if (do_writing && transport->send_credentials_pending)
349 if (_dbus_send_credentials_socket (socket_transport->fd,
352 transport->send_credentials_pending = FALSE;
356 _dbus_verbose ("Failed to write credentials: %s\n", error.message);
357 dbus_error_free (&error);
358 do_io_error (transport);
362 if (do_reading && transport->receive_credentials_pending)
364 /* FIXME this can fail due to IO error _or_ OOM, broken
365 * (somewhat tricky to fix since the OOM error can be set after
366 * we already read the credentials byte, so basically we need to
367 * separate reading the byte and storing it in the
368 * transport->credentials). Does not really matter for now
369 * because storing in credentials never actually fails on unix.
371 if (_dbus_read_credentials_socket (socket_transport->fd,
372 transport->credentials,
375 transport->receive_credentials_pending = FALSE;
379 _dbus_verbose ("Failed to read credentials %s\n", error.message);
380 dbus_error_free (&error);
381 do_io_error (transport);
385 if (!(transport->send_credentials_pending ||
386 transport->receive_credentials_pending))
388 if (!_dbus_auth_set_credentials (transport->auth,
389 transport->credentials))
397 do_authentication (DBusTransport *transport,
398 dbus_bool_t do_reading,
399 dbus_bool_t do_writing,
400 dbus_bool_t *auth_completed)
403 dbus_bool_t orig_auth_state;
407 orig_auth_state = _dbus_transport_try_to_authenticate (transport);
409 /* This is essential to avoid the check_write_watch() at the end,
410 * we don't want to add a write watch in do_iteration before
411 * we try writing and get EAGAIN
416 *auth_completed = FALSE;
420 _dbus_transport_ref (transport);
422 while (!_dbus_transport_try_to_authenticate (transport) &&
423 _dbus_transport_get_is_connected (transport))
425 if (!exchange_credentials (transport, do_reading, do_writing))
432 if (transport->send_credentials_pending ||
433 transport->receive_credentials_pending)
435 _dbus_verbose ("send_credentials_pending = %d receive_credentials_pending = %d\n",
436 transport->send_credentials_pending,
437 transport->receive_credentials_pending);
441 #define TRANSPORT_SIDE(t) ((t)->is_server ? "server" : "client")
442 switch (_dbus_auth_do_work (transport->auth))
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));
480 *auth_completed = (orig_auth_state != _dbus_transport_try_to_authenticate (transport));
482 check_read_watch (transport);
483 check_write_watch (transport);
484 _dbus_transport_unref (transport);
492 /* returns false on oom */
494 do_writing (DBusTransport *transport)
497 DBusTransportSocket *socket_transport = (DBusTransportSocket*) transport;
500 /* No messages without authentication! */
501 if (!_dbus_transport_try_to_authenticate (transport))
503 _dbus_verbose ("Not authenticated, not writing anything\n");
507 if (transport->disconnected)
509 _dbus_verbose ("Not connected, not writing anything\n");
514 _dbus_verbose ("do_writing(), have_messages = %d, fd = %d\n",
515 _dbus_connection_has_messages_to_send_unlocked (transport->connection),
516 socket_transport->fd);
522 while (!transport->disconnected &&
523 _dbus_connection_has_messages_to_send_unlocked (transport->connection))
526 DBusMessage *message;
527 const DBusString *header;
528 const DBusString *body;
529 int header_len, body_len;
530 int total_bytes_to_write;
532 if (total > socket_transport->max_bytes_written_per_iteration)
534 _dbus_verbose ("%d bytes exceeds %d bytes written per iteration, returning\n",
535 total, socket_transport->max_bytes_written_per_iteration);
539 message = _dbus_connection_get_message_to_send (transport->connection);
540 _dbus_assert (message != NULL);
541 dbus_message_lock (message);
544 _dbus_verbose ("writing message %p\n", message);
547 _dbus_message_get_network_data (message,
550 header_len = _dbus_string_get_length (header);
551 body_len = _dbus_string_get_length (body);
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));
558 if (_dbus_string_get_length (&socket_transport->encoded_outgoing) == 0)
560 if (!_dbus_auth_encode_data (transport->auth,
561 header, &socket_transport->encoded_outgoing))
567 if (!_dbus_auth_encode_data (transport->auth,
568 body, &socket_transport->encoded_outgoing))
570 _dbus_string_set_length (&socket_transport->encoded_outgoing, 0);
576 total_bytes_to_write = _dbus_string_get_length (&socket_transport->encoded_outgoing);
579 _dbus_verbose ("encoded message is %d bytes\n",
580 total_bytes_to_write);
584 _dbus_write_socket (socket_transport->fd,
585 &socket_transport->encoded_outgoing,
586 socket_transport->message_bytes_written,
587 total_bytes_to_write - socket_transport->message_bytes_written);
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
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);
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,
638 (socket_transport->message_bytes_written - header_len),
640 (socket_transport->message_bytes_written - header_len));
645 if (bytes_written < 0)
647 /* EINTR already handled for us */
649 /* For some discussion of why we also ignore EPIPE here, see
650 * http://lists.freedesktop.org/archives/dbus/2008-March/009526.html
653 if (_dbus_get_is_errno_eagain_or_ewouldblock () || _dbus_get_is_errno_epipe ())
657 _dbus_verbose ("Error writing to remote app: %s\n",
658 _dbus_strerror_from_errno ());
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;
669 socket_transport->message_bytes_written += bytes_written;
671 _dbus_assert (socket_transport->message_bytes_written <=
672 total_bytes_to_write);
674 if (socket_transport->message_bytes_written == total_bytes_to_write)
676 socket_transport->message_bytes_written = 0;
677 _dbus_string_set_length (&socket_transport->encoded_outgoing, 0);
678 _dbus_string_compact (&socket_transport->encoded_outgoing, 2048);
680 _dbus_connection_message_sent_unlocked (transport->connection,
693 /* returns false on out-of-memory */
695 do_reading (DBusTransport *transport)
697 DBusTransportSocket *socket_transport = (DBusTransportSocket*) transport;
703 _dbus_verbose ("fd = %d\n",socket_transport->fd);
705 /* No messages without authentication! */
706 if (!_dbus_transport_try_to_authenticate (transport))
715 /* See if we've exceeded max messages and need to disable reading */
716 check_read_watch (transport);
718 if (total > socket_transport->max_bytes_read_per_iteration)
720 _dbus_verbose ("%d bytes exceeds %d bytes read per iteration, returning\n",
721 total, socket_transport->max_bytes_read_per_iteration);
725 _dbus_assert (socket_transport->read_watch != NULL ||
726 transport->disconnected);
728 if (transport->disconnected)
731 if (!dbus_watch_get_enabled (socket_transport->read_watch))
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));
739 if (_dbus_string_get_length (&socket_transport->encoded_incoming) > 0)
740 bytes_read = _dbus_string_get_length (&socket_transport->encoded_incoming);
742 bytes_read = _dbus_read_socket (socket_transport->fd,
743 &socket_transport->encoded_incoming,
744 socket_transport->max_bytes_read_per_iteration);
746 _dbus_assert (_dbus_string_get_length (&socket_transport->encoded_incoming) ==
753 _dbus_message_loader_get_buffer (transport->loader,
756 orig_len = _dbus_string_get_length (buffer);
758 if (!_dbus_auth_decode_data (transport->auth,
759 &socket_transport->encoded_incoming,
762 _dbus_verbose ("Out of memory decoding incoming data\n");
763 _dbus_message_loader_return_buffer (transport->loader,
765 _dbus_string_get_length (buffer) - orig_len);
771 _dbus_message_loader_return_buffer (transport->loader,
773 _dbus_string_get_length (buffer) - orig_len);
775 _dbus_string_set_length (&socket_transport->encoded_incoming, 0);
776 _dbus_string_compact (&socket_transport->encoded_incoming, 2048);
781 _dbus_message_loader_get_buffer (transport->loader,
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);
814 _dbus_message_loader_return_buffer (transport->loader,
816 bytes_read < 0 ? 0 : bytes_read);
821 /* EINTR already handled for us */
823 if (_dbus_get_is_errno_enomem ())
825 _dbus_verbose ("Out of memory in read()/do_reading()\n");
829 else if (_dbus_get_is_errno_eagain_or_ewouldblock ())
833 _dbus_verbose ("Error reading from remote app: %s\n",
834 _dbus_strerror_from_errno ());
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);
851 if (!_dbus_transport_queue_messages (transport))
854 _dbus_verbose (" out of memory when queueing messages we just read in the transport\n");
858 /* Try reading more data until we get EAGAIN and return, or
859 * exceed max bytes per iteration. If in blocking mode of
860 * course we'll block instead of returning.
873 unix_error_with_read_to_come (DBusTransport *itransport,
877 DBusTransportSocket *transport = (DBusTransportSocket *) itransport;
879 if (!(flags & DBUS_WATCH_HANGUP || flags & DBUS_WATCH_ERROR))
882 /* If we have a read watch enabled ...
883 we -might have data incoming ... => handle the HANGUP there */
884 if (watch != transport->read_watch &&
885 _dbus_watch_get_enabled (transport->read_watch))
892 socket_handle_watch (DBusTransport *transport,
896 DBusTransportSocket *socket_transport = (DBusTransportSocket*) transport;
898 _dbus_assert (watch == socket_transport->read_watch ||
899 watch == socket_transport->write_watch);
900 _dbus_assert (watch != NULL);
902 /* If we hit an error here on a write watch, don't disconnect the transport yet because data can
903 * still be in the buffer and do_reading may need several iteration to read
904 * it all (because of its max_bytes_read_per_iteration limit).
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");
909 _dbus_transport_disconnect (transport);
913 if (watch == socket_transport->read_watch &&
914 (flags & DBUS_WATCH_READABLE))
916 dbus_bool_t auth_finished;
918 _dbus_verbose ("handling read watch %p flags = %x\n",
921 if (!do_authentication (transport, TRUE, FALSE, &auth_finished))
924 /* We don't want to do a read immediately following
925 * a successful authentication. This is so we
926 * have a chance to propagate the authentication
927 * state further up. Specifically, we need to
928 * process any pending data from the auth object.
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");
943 else if (watch == socket_transport->write_watch &&
944 (flags & DBUS_WATCH_WRITABLE))
947 _dbus_verbose ("handling write watch, have_outgoing_messages = %d\n",
948 _dbus_connection_has_messages_to_send_unlocked (transport->connection));
950 if (!do_authentication (transport, FALSE, TRUE, NULL))
953 if (!do_writing (transport))
955 _dbus_verbose ("no memory to write\n");
959 /* See if we still need the write watch */
960 check_write_watch (transport);
962 #ifdef DBUS_ENABLE_VERBOSE_MODE
965 if (watch == socket_transport->read_watch)
966 _dbus_verbose ("asked to handle read watch with non-read condition 0x%x\n",
968 else if (watch == socket_transport->write_watch)
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",
973 watch, dbus_watch_get_socket (watch));
975 #endif /* DBUS_ENABLE_VERBOSE_MODE */
981 socket_disconnect (DBusTransport *transport)
983 DBusTransportSocket *socket_transport = (DBusTransportSocket*) transport;
985 _dbus_verbose ("\n");
987 free_watches (transport);
989 _dbus_close_socket (socket_transport->fd, NULL);
990 socket_transport->fd = -1;
994 socket_connection_set (DBusTransport *transport)
996 DBusTransportSocket *socket_transport = (DBusTransportSocket*) transport;
998 _dbus_watch_set_handler (socket_transport->write_watch,
999 _dbus_connection_handle_watch,
1000 transport->connection, NULL);
1002 _dbus_watch_set_handler (socket_transport->read_watch,
1003 _dbus_connection_handle_watch,
1004 transport->connection, NULL);
1006 if (!_dbus_connection_add_watch_unlocked (transport->connection,
1007 socket_transport->write_watch))
1010 if (!_dbus_connection_add_watch_unlocked (transport->connection,
1011 socket_transport->read_watch))
1013 _dbus_connection_remove_watch_unlocked (transport->connection,
1014 socket_transport->write_watch);
1018 check_read_watch (transport);
1019 check_write_watch (transport);
1025 * @todo We need to have a way to wake up the select sleep if
1026 * a new iteration request comes in with a flag (read/write) that
1027 * we're not currently serving. Otherwise a call that just reads
1028 * could block a write call forever (if there are no incoming
1032 socket_do_iteration (DBusTransport *transport,
1034 int timeout_milliseconds)
1036 DBusTransportSocket *socket_transport = (DBusTransportSocket*) transport;
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,
1045 socket_transport->read_watch,
1046 socket_transport->write_watch,
1047 socket_transport->fd);
1049 /* the passed in DO_READING/DO_WRITING flags indicate whether to
1050 * read/write messages, but regardless of those we may need to block
1051 * for reading/writing to do auth. But if we do reading for auth,
1052 * we don't want to read any messages yet if not given DO_READING.
1055 poll_fd.fd = socket_transport->fd;
1058 if (_dbus_transport_try_to_authenticate (transport))
1060 /* This is kind of a hack; if we have stuff to write, then try
1061 * to avoid the poll. This is probably about a 5% speedup on an
1062 * echo client/server.
1064 * If both reading and writing were requested, we want to avoid this
1065 * since it could have funky effects:
1066 * - both ends spinning waiting for the other one to read
1067 * data so they can finish writing
1068 * - prioritizing all writing ahead of reading
1070 if ((flags & DBUS_ITERATION_DO_WRITING) &&
1071 !(flags & (DBUS_ITERATION_DO_READING | DBUS_ITERATION_BLOCK)) &&
1072 !transport->disconnected &&
1073 _dbus_connection_has_messages_to_send_unlocked (transport->connection))
1075 do_writing (transport);
1077 if (transport->disconnected ||
1078 !_dbus_connection_has_messages_to_send_unlocked (transport->connection))
1082 /* If we get here, we decided to do the poll() after all */
1083 _dbus_assert (socket_transport->read_watch);
1084 if (flags & DBUS_ITERATION_DO_READING)
1085 poll_fd.events |= _DBUS_POLLIN;
1087 _dbus_assert (socket_transport->write_watch);
1088 if (flags & DBUS_ITERATION_DO_WRITING)
1089 poll_fd.events |= _DBUS_POLLOUT;
1093 DBusAuthState auth_state;
1095 auth_state = _dbus_auth_do_work (transport->auth);
1097 if (transport->receive_credentials_pending ||
1098 auth_state == DBUS_AUTH_STATE_WAITING_FOR_INPUT)
1099 poll_fd.events |= _DBUS_POLLIN;
1101 if (transport->send_credentials_pending ||
1102 auth_state == DBUS_AUTH_STATE_HAVE_BYTES_TO_SEND)
1103 poll_fd.events |= _DBUS_POLLOUT;
1108 if (flags & DBUS_ITERATION_BLOCK)
1109 poll_timeout = timeout_milliseconds;
1113 /* For blocking selects we drop the connection lock here
1114 * to avoid blocking out connection access during a potentially
1115 * indefinite blocking call. The io path is still protected
1116 * by the io_path_cond condvar, so we won't reenter this.
1118 if (flags & DBUS_ITERATION_BLOCK)
1120 _dbus_verbose ("unlock pre poll\n");
1121 _dbus_connection_unlock (transport->connection);
1125 poll_res = _dbus_poll (&poll_fd, 1, poll_timeout);
1127 if (poll_res < 0 && _dbus_get_is_errno_eintr ())
1130 if (flags & DBUS_ITERATION_BLOCK)
1132 _dbus_verbose ("lock post poll\n");
1133 _dbus_connection_lock (transport->connection);
1139 poll_fd.revents = 0; /* some concern that posix does not guarantee this;
1140 * valgrind flags it as an error. though it probably
1141 * is guaranteed on linux at least.
1144 if (poll_fd.revents & _DBUS_POLLERR)
1145 do_io_error (transport);
1148 dbus_bool_t need_read = (poll_fd.revents & _DBUS_POLLIN) > 0;
1149 dbus_bool_t need_write = (poll_fd.revents & _DBUS_POLLOUT) > 0;
1150 dbus_bool_t authentication_completed;
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);
1157 /* See comment in socket_handle_watch. */
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",
1170 _dbus_strerror_from_errno ());
1176 /* We need to install the write watch only if we did not
1177 * successfully write everything. Note we need to be careful that we
1178 * don't call check_write_watch *before* do_writing, since it's
1179 * inefficient to add the write watch, and we can avoid it most of
1180 * the time since we can write immediately.
1182 * However, we MUST always call check_write_watch(); DBusConnection code
1183 * relies on the fact that running an iteration will notice that
1184 * messages are pending.
1186 check_write_watch (transport);
1188 _dbus_verbose (" ... leaving do_iteration()\n");
1192 socket_live_messages_changed (DBusTransport *transport)
1194 /* See if we should look for incoming messages again */
1195 check_read_watch (transport);
1200 socket_get_socket_fd (DBusTransport *transport,
1203 DBusTransportSocket *socket_transport = (DBusTransportSocket*) transport;
1205 *fd_p = socket_transport->fd;
1210 static const DBusTransportVTable socket_vtable = {
1212 socket_handle_watch,
1214 socket_connection_set,
1215 socket_do_iteration,
1216 socket_live_messages_changed,
1217 socket_get_socket_fd
1221 * Creates a new transport for the given socket file descriptor. The file
1222 * descriptor must be nonblocking (use _dbus_set_fd_nonblocking() to
1223 * make it so). This function is shared by various transports that
1224 * boil down to a full duplex file descriptor.
1226 * @param fd the file descriptor.
1227 * @param server_guid non-#NULL if this transport is on the server side of a connection
1228 * @param address the transport's address
1229 * @returns the new transport, or #NULL if no memory.
1232 _dbus_transport_new_for_socket (int fd,
1233 const DBusString *server_guid,
1234 const DBusString *address)
1236 DBusTransportSocket *socket_transport;
1238 socket_transport = dbus_new0 (DBusTransportSocket, 1);
1239 if (socket_transport == NULL)
1242 if (!_dbus_string_init (&socket_transport->encoded_outgoing))
1245 if (!_dbus_string_init (&socket_transport->encoded_incoming))
1248 socket_transport->write_watch = _dbus_watch_new (fd,
1249 DBUS_WATCH_WRITABLE,
1252 if (socket_transport->write_watch == NULL)
1255 socket_transport->read_watch = _dbus_watch_new (fd,
1256 DBUS_WATCH_READABLE,
1259 if (socket_transport->read_watch == NULL)
1262 if (!_dbus_transport_init_base (&socket_transport->base,
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));
1271 socket_transport->fd = fd;
1272 socket_transport->message_bytes_written = 0;
1274 /* These values should probably be tunable or something. */
1275 socket_transport->max_bytes_read_per_iteration = 2048;
1276 socket_transport->max_bytes_written_per_iteration = 2048;
1278 return (DBusTransport*) socket_transport;
1281 _dbus_watch_invalidate (socket_transport->read_watch);
1282 _dbus_watch_unref (socket_transport->read_watch);
1284 _dbus_watch_invalidate (socket_transport->write_watch);
1285 _dbus_watch_unref (socket_transport->write_watch);
1287 _dbus_string_free (&socket_transport->encoded_incoming);
1289 _dbus_string_free (&socket_transport->encoded_outgoing);
1291 dbus_free (socket_transport);
1296 * Creates a new transport for the given hostname and port.
1297 * If host is NULL, it will default to localhost
1299 * @param host the host to connect to
1300 * @param port the port to connect to
1301 * @param family the address family to connect to
1302 * @param noncefile path to nonce file
1303 * @param error location to store reason for failure.
1304 * @returns a new transport, or #NULL on failure.
1307 _dbus_transport_new_for_tcp_socket (const char *host,
1310 const char *noncefile,
1314 DBusTransport *transport;
1317 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
1319 if (!_dbus_string_init (&address))
1321 dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
1328 if (!_dbus_string_append (&address, noncefile ? "nonce-tcp:" : "tcp:"))
1331 if (!_dbus_string_append (&address, "host=") ||
1332 !_dbus_string_append (&address, host))
1335 if (!_dbus_string_append (&address, ",port=") ||
1336 !_dbus_string_append (&address, port))
1339 if (family != NULL &&
1340 (!_dbus_string_append (&address, ",family=") ||
1341 !_dbus_string_append (&address, family)))
1344 if (noncefile != NULL &&
1345 (!_dbus_string_append (&address, ",noncefile=") ||
1346 !_dbus_string_append (&address, noncefile)))
1349 fd = _dbus_connect_tcp_socket_with_nonce (host, port, family, noncefile, error);
1352 _DBUS_ASSERT_ERROR_IS_SET (error);
1353 _dbus_string_free (&address);
1357 _dbus_verbose ("Successfully connected to tcp socket %s:%s\n",
1360 transport = _dbus_transport_new_for_socket (fd, NULL, &address);
1361 _dbus_string_free (&address);
1362 if (transport == NULL)
1364 dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
1365 _dbus_close_socket (fd, NULL);
1372 _dbus_string_free (&address);
1373 dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
1378 * Opens a TCP socket transport.
1380 * @param entry the address entry to try opening as a tcp transport.
1381 * @param transport_p return location for the opened transport
1382 * @param error error to be set
1383 * @returns result of the attempt
1385 DBusTransportOpenResult
1386 _dbus_transport_open_socket(DBusAddressEntry *entry,
1387 DBusTransport **transport_p,
1392 dbus_bool_t isNonceTcp;
1394 method = dbus_address_entry_get_method (entry);
1395 _dbus_assert (method != NULL);
1397 isTcp = strcmp (method, "tcp") == 0;
1398 isNonceTcp = strcmp (method, "nonce-tcp") == 0;
1400 if (isTcp || isNonceTcp)
1402 const char *host = dbus_address_entry_get_value (entry, "host");
1403 const char *port = dbus_address_entry_get_value (entry, "port");
1404 const char *family = dbus_address_entry_get_value (entry, "family");
1405 const char *noncefile = dbus_address_entry_get_value (entry, "noncefile");
1407 if ((isNonceTcp == TRUE) != (noncefile != NULL)) {
1408 _dbus_set_bad_address (error, method, "noncefile", NULL);
1409 return DBUS_TRANSPORT_OPEN_BAD_ADDRESS;
1414 _dbus_set_bad_address (error, method, "port", NULL);
1415 return DBUS_TRANSPORT_OPEN_BAD_ADDRESS;
1418 *transport_p = _dbus_transport_new_for_tcp_socket (host, port, family, noncefile, error);
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;