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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
24 #include "dbus-internals.h"
25 #include "dbus-connection-internal.h"
26 #include "dbus-transport-socket.h"
27 #include "dbus-transport-protected.h"
28 #include "dbus-watch.h"
29 #include "dbus-credentials.h"
33 * @defgroup DBusTransportSocket DBusTransport implementations for sockets
34 * @ingroup DBusInternals
35 * @brief Implementation details of DBusTransport on sockets
41 * Opaque object representing a socket file descriptor transport.
43 typedef struct DBusTransportSocket DBusTransportSocket;
46 * Implementation details of DBusTransportSocket. All members are private.
48 struct DBusTransportSocket
50 DBusTransport base; /**< Parent instance */
51 int fd; /**< File descriptor. */
52 DBusWatch *read_watch; /**< Watch for readability. */
53 DBusWatch *write_watch; /**< Watch for writability. */
55 int max_bytes_read_per_iteration; /**< To avoid blocking too long. */
56 int max_bytes_written_per_iteration; /**< To avoid blocking too long. */
58 int message_bytes_written; /**< Number of bytes of current
59 * outgoing message that have
62 DBusString encoded_outgoing; /**< Encoded version of current
65 DBusString encoded_incoming; /**< Encoded version of current
71 free_watches (DBusTransport *transport)
73 DBusTransportSocket *socket_transport = (DBusTransportSocket*) transport;
75 _dbus_verbose ("%s start\n", _DBUS_FUNCTION_NAME);
77 if (socket_transport->read_watch)
79 if (transport->connection)
80 _dbus_connection_remove_watch_unlocked (transport->connection,
81 socket_transport->read_watch);
82 _dbus_watch_invalidate (socket_transport->read_watch);
83 _dbus_watch_unref (socket_transport->read_watch);
84 socket_transport->read_watch = NULL;
87 if (socket_transport->write_watch)
89 if (transport->connection)
90 _dbus_connection_remove_watch_unlocked (transport->connection,
91 socket_transport->write_watch);
92 _dbus_watch_invalidate (socket_transport->write_watch);
93 _dbus_watch_unref (socket_transport->write_watch);
94 socket_transport->write_watch = NULL;
97 _dbus_verbose ("%s end\n", _DBUS_FUNCTION_NAME);
101 socket_finalize (DBusTransport *transport)
103 DBusTransportSocket *socket_transport = (DBusTransportSocket*) transport;
105 _dbus_verbose ("%s\n", _DBUS_FUNCTION_NAME);
107 free_watches (transport);
109 _dbus_string_free (&socket_transport->encoded_outgoing);
110 _dbus_string_free (&socket_transport->encoded_incoming);
112 _dbus_transport_finalize_base (transport);
114 _dbus_assert (socket_transport->read_watch == NULL);
115 _dbus_assert (socket_transport->write_watch == NULL);
117 dbus_free (transport);
121 check_write_watch (DBusTransport *transport)
123 DBusTransportSocket *socket_transport = (DBusTransportSocket*) transport;
126 if (transport->connection == NULL)
129 if (transport->disconnected)
131 _dbus_assert (socket_transport->write_watch == NULL);
135 _dbus_transport_ref (transport);
137 if (_dbus_transport_get_is_authenticated (transport))
138 needed = _dbus_connection_has_messages_to_send_unlocked (transport->connection);
141 if (transport->send_credentials_pending)
145 DBusAuthState auth_state;
147 auth_state = _dbus_auth_do_work (transport->auth);
149 /* If we need memory we install the write watch just in case,
150 * if there's no need for it, it will get de-installed
151 * next time we try reading.
153 if (auth_state == DBUS_AUTH_STATE_HAVE_BYTES_TO_SEND ||
154 auth_state == DBUS_AUTH_STATE_WAITING_FOR_MEMORY)
161 _dbus_verbose ("check_write_watch(): needed = %d on connection %p watch %p fd = %d outgoing messages exist %d\n",
162 needed, transport->connection, socket_transport->write_watch,
163 socket_transport->fd,
164 _dbus_connection_has_messages_to_send_unlocked (transport->connection));
166 _dbus_connection_toggle_watch_unlocked (transport->connection,
167 socket_transport->write_watch,
170 _dbus_transport_unref (transport);
174 check_read_watch (DBusTransport *transport)
176 DBusTransportSocket *socket_transport = (DBusTransportSocket*) transport;
177 dbus_bool_t need_read_watch;
179 _dbus_verbose ("%s: fd = %d\n",
180 _DBUS_FUNCTION_NAME, 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_get_is_authenticated (transport))
195 _dbus_counter_get_value (transport->live_messages_size) < transport->max_live_messages_size;
198 if (transport->receive_credentials_pending)
199 need_read_watch = TRUE;
202 /* The reason to disable need_read_watch when not WAITING_FOR_INPUT
203 * is to avoid spinning on the file descriptor when we're waiting
204 * to write or for some other part of the auth process
206 DBusAuthState auth_state;
208 auth_state = _dbus_auth_do_work (transport->auth);
210 /* If we need memory we install the read watch just in case,
211 * if there's no need for it, it will get de-installed
212 * next time we try reading. If we're authenticated we
213 * install it since we normally have it installed while
216 if (auth_state == DBUS_AUTH_STATE_WAITING_FOR_INPUT ||
217 auth_state == DBUS_AUTH_STATE_WAITING_FOR_MEMORY ||
218 auth_state == DBUS_AUTH_STATE_AUTHENTICATED)
219 need_read_watch = TRUE;
221 need_read_watch = FALSE;
225 _dbus_verbose (" setting read watch enabled = %d\n", need_read_watch);
226 _dbus_connection_toggle_watch_unlocked (transport->connection,
227 socket_transport->read_watch,
230 _dbus_transport_unref (transport);
234 do_io_error (DBusTransport *transport)
236 _dbus_transport_ref (transport);
237 _dbus_transport_disconnect (transport);
238 _dbus_transport_unref (transport);
241 /* return value is whether we successfully read any new data. */
243 read_data_into_auth (DBusTransport *transport,
246 DBusTransportSocket *socket_transport = (DBusTransportSocket*) transport;
252 _dbus_auth_get_buffer (transport->auth, &buffer);
254 bytes_read = _dbus_read_socket (socket_transport->fd,
255 buffer, socket_transport->max_bytes_read_per_iteration);
257 _dbus_auth_return_buffer (transport->auth, buffer,
258 bytes_read > 0 ? bytes_read : 0);
262 _dbus_verbose (" read %d bytes in auth phase\n", bytes_read);
266 else if (bytes_read < 0)
268 /* EINTR already handled for us */
270 if (_dbus_get_is_errno_enomem ())
274 else if (_dbus_get_is_errno_eagain_or_ewouldblock ())
275 ; /* do nothing, just return FALSE below */
278 _dbus_verbose ("Error reading from remote app: %s\n",
279 _dbus_strerror_from_errno ());
280 do_io_error (transport);
287 _dbus_assert (bytes_read == 0);
289 _dbus_verbose ("Disconnected from remote app\n");
290 do_io_error (transport);
296 /* Return value is whether we successfully wrote any bytes */
298 write_data_from_auth (DBusTransport *transport)
300 DBusTransportSocket *socket_transport = (DBusTransportSocket*) transport;
302 const DBusString *buffer;
304 if (!_dbus_auth_get_bytes_to_send (transport->auth,
308 bytes_written = _dbus_write_socket (socket_transport->fd,
310 0, _dbus_string_get_length (buffer));
312 if (bytes_written > 0)
314 _dbus_auth_bytes_sent (transport->auth, bytes_written);
317 else if (bytes_written < 0)
319 /* EINTR already handled for us */
321 if (_dbus_get_is_errno_eagain_or_ewouldblock ())
325 _dbus_verbose ("Error writing to remote app: %s\n",
326 _dbus_strerror_from_errno ());
327 do_io_error (transport);
336 exchange_credentials (DBusTransport *transport,
337 dbus_bool_t do_reading,
338 dbus_bool_t do_writing)
340 DBusTransportSocket *socket_transport = (DBusTransportSocket*) transport;
341 DBusError error = DBUS_ERROR_INIT;
343 _dbus_verbose ("exchange_credentials: do_reading = %d, do_writing = %d\n",
344 do_reading, do_writing);
346 if (do_writing && transport->send_credentials_pending)
348 if (_dbus_send_credentials_socket (socket_transport->fd,
351 transport->send_credentials_pending = FALSE;
355 _dbus_verbose ("Failed to write credentials: %s\n", error.message);
356 dbus_error_free (&error);
357 do_io_error (transport);
361 if (do_reading && transport->receive_credentials_pending)
363 /* FIXME this can fail due to IO error _or_ OOM, broken
364 * (somewhat tricky to fix since the OOM error can be set after
365 * we already read the credentials byte, so basically we need to
366 * separate reading the byte and storing it in the
367 * transport->credentials). Does not really matter for now
368 * because storing in credentials never actually fails on unix.
370 if (_dbus_read_credentials_socket (socket_transport->fd,
371 transport->credentials,
374 transport->receive_credentials_pending = FALSE;
378 _dbus_verbose ("Failed to read credentials %s\n", error.message);
379 dbus_error_free (&error);
380 do_io_error (transport);
384 if (!(transport->send_credentials_pending ||
385 transport->receive_credentials_pending))
387 if (!_dbus_auth_set_credentials (transport->auth,
388 transport->credentials))
396 do_authentication (DBusTransport *transport,
397 dbus_bool_t do_reading,
398 dbus_bool_t do_writing,
399 dbus_bool_t *auth_completed)
402 dbus_bool_t orig_auth_state;
406 orig_auth_state = _dbus_transport_get_is_authenticated (transport);
408 /* This is essential to avoid the check_write_watch() at the end,
409 * we don't want to add a write watch in do_iteration before
410 * we try writing and get EAGAIN
415 *auth_completed = FALSE;
419 _dbus_transport_ref (transport);
421 while (!_dbus_transport_get_is_authenticated (transport) &&
422 _dbus_transport_get_is_connected (transport))
424 if (!exchange_credentials (transport, do_reading, do_writing))
431 if (transport->send_credentials_pending ||
432 transport->receive_credentials_pending)
434 _dbus_verbose ("send_credentials_pending = %d receive_credentials_pending = %d\n",
435 transport->send_credentials_pending,
436 transport->receive_credentials_pending);
440 #define TRANSPORT_SIDE(t) ((t)->is_server ? "server" : "client")
441 switch (_dbus_auth_do_work (transport->auth))
443 case DBUS_AUTH_STATE_WAITING_FOR_INPUT:
444 _dbus_verbose (" %s auth state: waiting for input\n",
445 TRANSPORT_SIDE (transport));
446 if (!do_reading || !read_data_into_auth (transport, &oom))
450 case DBUS_AUTH_STATE_WAITING_FOR_MEMORY:
451 _dbus_verbose (" %s auth state: waiting for memory\n",
452 TRANSPORT_SIDE (transport));
457 case DBUS_AUTH_STATE_HAVE_BYTES_TO_SEND:
458 _dbus_verbose (" %s auth state: bytes to send\n",
459 TRANSPORT_SIDE (transport));
460 if (!do_writing || !write_data_from_auth (transport))
464 case DBUS_AUTH_STATE_NEED_DISCONNECT:
465 _dbus_verbose (" %s auth state: need to disconnect\n",
466 TRANSPORT_SIDE (transport));
467 do_io_error (transport);
470 case DBUS_AUTH_STATE_AUTHENTICATED:
471 _dbus_verbose (" %s auth state: authenticated\n",
472 TRANSPORT_SIDE (transport));
479 *auth_completed = (orig_auth_state != _dbus_transport_get_is_authenticated (transport));
481 check_read_watch (transport);
482 check_write_watch (transport);
483 _dbus_transport_unref (transport);
491 /* returns false on oom */
493 do_writing (DBusTransport *transport)
496 DBusTransportSocket *socket_transport = (DBusTransportSocket*) transport;
499 /* No messages without authentication! */
500 if (!_dbus_transport_get_is_authenticated (transport))
502 _dbus_verbose ("Not authenticated, not writing anything\n");
506 if (transport->disconnected)
508 _dbus_verbose ("Not connected, not writing anything\n");
513 _dbus_verbose ("do_writing(), have_messages = %d, fd = %d\n",
514 _dbus_connection_has_messages_to_send_unlocked (transport->connection),
515 socket_transport->fd);
521 while (!transport->disconnected &&
522 _dbus_connection_has_messages_to_send_unlocked (transport->connection))
525 DBusMessage *message;
526 const DBusString *header;
527 const DBusString *body;
528 int header_len, body_len;
529 int total_bytes_to_write;
531 if (total > socket_transport->max_bytes_written_per_iteration)
533 _dbus_verbose ("%d bytes exceeds %d bytes written per iteration, returning\n",
534 total, socket_transport->max_bytes_written_per_iteration);
538 message = _dbus_connection_get_message_to_send (transport->connection);
539 _dbus_assert (message != NULL);
540 dbus_message_lock (message);
543 _dbus_verbose ("writing message %p\n", message);
546 _dbus_message_get_network_data (message,
549 header_len = _dbus_string_get_length (header);
550 body_len = _dbus_string_get_length (body);
552 if (_dbus_auth_needs_encoding (transport->auth))
554 if (_dbus_string_get_length (&socket_transport->encoded_outgoing) == 0)
556 if (!_dbus_auth_encode_data (transport->auth,
557 header, &socket_transport->encoded_outgoing))
563 if (!_dbus_auth_encode_data (transport->auth,
564 body, &socket_transport->encoded_outgoing))
566 _dbus_string_set_length (&socket_transport->encoded_outgoing, 0);
572 total_bytes_to_write = _dbus_string_get_length (&socket_transport->encoded_outgoing);
575 _dbus_verbose ("encoded message is %d bytes\n",
576 total_bytes_to_write);
580 _dbus_write_socket (socket_transport->fd,
581 &socket_transport->encoded_outgoing,
582 socket_transport->message_bytes_written,
583 total_bytes_to_write - socket_transport->message_bytes_written);
587 total_bytes_to_write = header_len + body_len;
590 _dbus_verbose ("message is %d bytes\n",
591 total_bytes_to_write);
594 if (socket_transport->message_bytes_written < header_len)
597 _dbus_write_socket_two (socket_transport->fd,
599 socket_transport->message_bytes_written,
600 header_len - socket_transport->message_bytes_written,
607 _dbus_write_socket (socket_transport->fd,
609 (socket_transport->message_bytes_written - header_len),
611 (socket_transport->message_bytes_written - header_len));
615 if (bytes_written < 0)
617 /* EINTR already handled for us */
619 if (_dbus_get_is_errno_eagain_or_ewouldblock ())
623 _dbus_verbose ("Error writing to remote app: %s\n",
624 _dbus_strerror_from_errno ());
625 do_io_error (transport);
631 _dbus_verbose (" wrote %d bytes of %d\n", bytes_written,
632 total_bytes_to_write);
634 total += bytes_written;
635 socket_transport->message_bytes_written += bytes_written;
637 _dbus_assert (socket_transport->message_bytes_written <=
638 total_bytes_to_write);
640 if (socket_transport->message_bytes_written == total_bytes_to_write)
642 socket_transport->message_bytes_written = 0;
643 _dbus_string_set_length (&socket_transport->encoded_outgoing, 0);
644 _dbus_string_compact (&socket_transport->encoded_outgoing, 2048);
646 _dbus_connection_message_sent (transport->connection,
659 /* returns false on out-of-memory */
661 do_reading (DBusTransport *transport)
663 DBusTransportSocket *socket_transport = (DBusTransportSocket*) transport;
669 _dbus_verbose ("%s: fd = %d\n", _DBUS_FUNCTION_NAME,
670 socket_transport->fd);
672 /* No messages without authentication! */
673 if (!_dbus_transport_get_is_authenticated (transport))
682 /* See if we've exceeded max messages and need to disable reading */
683 check_read_watch (transport);
685 if (total > socket_transport->max_bytes_read_per_iteration)
687 _dbus_verbose ("%d bytes exceeds %d bytes read per iteration, returning\n",
688 total, socket_transport->max_bytes_read_per_iteration);
692 _dbus_assert (socket_transport->read_watch != NULL ||
693 transport->disconnected);
695 if (transport->disconnected)
698 if (!dbus_watch_get_enabled (socket_transport->read_watch))
701 if (_dbus_auth_needs_decoding (transport->auth))
703 if (_dbus_string_get_length (&socket_transport->encoded_incoming) > 0)
704 bytes_read = _dbus_string_get_length (&socket_transport->encoded_incoming);
706 bytes_read = _dbus_read_socket (socket_transport->fd,
707 &socket_transport->encoded_incoming,
708 socket_transport->max_bytes_read_per_iteration);
710 _dbus_assert (_dbus_string_get_length (&socket_transport->encoded_incoming) ==
717 _dbus_message_loader_get_buffer (transport->loader,
720 orig_len = _dbus_string_get_length (buffer);
722 if (!_dbus_auth_decode_data (transport->auth,
723 &socket_transport->encoded_incoming,
726 _dbus_verbose ("Out of memory decoding incoming data\n");
727 _dbus_message_loader_return_buffer (transport->loader,
729 _dbus_string_get_length (buffer) - orig_len);
735 _dbus_message_loader_return_buffer (transport->loader,
737 _dbus_string_get_length (buffer) - orig_len);
739 _dbus_string_set_length (&socket_transport->encoded_incoming, 0);
740 _dbus_string_compact (&socket_transport->encoded_incoming, 2048);
745 _dbus_message_loader_get_buffer (transport->loader,
748 bytes_read = _dbus_read_socket (socket_transport->fd,
749 buffer, socket_transport->max_bytes_read_per_iteration);
751 _dbus_message_loader_return_buffer (transport->loader,
753 bytes_read < 0 ? 0 : bytes_read);
758 /* EINTR already handled for us */
760 if (_dbus_get_is_errno_enomem ())
762 _dbus_verbose ("Out of memory in read()/do_reading()\n");
766 else if (_dbus_get_is_errno_eagain_or_ewouldblock ())
770 _dbus_verbose ("Error reading from remote app: %s\n",
771 _dbus_strerror_from_errno ());
772 do_io_error (transport);
776 else if (bytes_read == 0)
778 _dbus_verbose ("Disconnected from remote app\n");
779 do_io_error (transport);
784 _dbus_verbose (" read %d bytes\n", bytes_read);
788 if (!_dbus_transport_queue_messages (transport))
791 _dbus_verbose (" out of memory when queueing messages we just read in the transport\n");
795 /* Try reading more data until we get EAGAIN and return, or
796 * exceed max bytes per iteration. If in blocking mode of
797 * course we'll block instead of returning.
810 socket_handle_watch (DBusTransport *transport,
814 DBusTransportSocket *socket_transport = (DBusTransportSocket*) transport;
816 _dbus_assert (watch == socket_transport->read_watch ||
817 watch == socket_transport->write_watch);
818 _dbus_assert (watch != NULL);
820 /* Disconnect in case of an error. In case of hangup do not
821 * disconnect the transport because data can still be in the buffer
822 * and do_reading may need several iteration to read it all (because
823 * of its max_bytes_read_per_iteration limit). The condition where
824 * flags == HANGUP (without READABLE) probably never happen in fact.
826 if ((flags & DBUS_WATCH_ERROR) ||
827 ((flags & DBUS_WATCH_HANGUP) && !(flags & DBUS_WATCH_READABLE)))
829 _dbus_verbose ("Hang up or error on watch\n");
830 _dbus_transport_disconnect (transport);
834 if (watch == socket_transport->read_watch &&
835 (flags & DBUS_WATCH_READABLE))
837 dbus_bool_t auth_finished;
839 _dbus_verbose ("handling read watch %p flags = %x\n",
842 if (!do_authentication (transport, TRUE, FALSE, &auth_finished))
845 /* We don't want to do a read immediately following
846 * a successful authentication. This is so we
847 * have a chance to propagate the authentication
848 * state further up. Specifically, we need to
849 * process any pending data from the auth object.
853 if (!do_reading (transport))
855 _dbus_verbose ("no memory to read\n");
861 _dbus_verbose ("Not reading anything since we just completed the authentication\n");
864 else if (watch == socket_transport->write_watch &&
865 (flags & DBUS_WATCH_WRITABLE))
868 _dbus_verbose ("handling write watch, have_outgoing_messages = %d\n",
869 _dbus_connection_has_messages_to_send_unlocked (transport->connection));
871 if (!do_authentication (transport, FALSE, TRUE, NULL))
874 if (!do_writing (transport))
876 _dbus_verbose ("no memory to write\n");
880 /* See if we still need the write watch */
881 check_write_watch (transport);
883 #ifdef DBUS_ENABLE_VERBOSE_MODE
886 if (watch == socket_transport->read_watch)
887 _dbus_verbose ("asked to handle read watch with non-read condition 0x%x\n",
889 else if (watch == socket_transport->write_watch)
890 _dbus_verbose ("asked to handle write watch with non-write condition 0x%x\n",
893 _dbus_verbose ("asked to handle watch %p on fd %d that we don't recognize\n",
894 watch, dbus_watch_get_socket (watch));
896 #endif /* DBUS_ENABLE_VERBOSE_MODE */
902 socket_disconnect (DBusTransport *transport)
904 DBusTransportSocket *socket_transport = (DBusTransportSocket*) transport;
906 _dbus_verbose ("%s\n", _DBUS_FUNCTION_NAME);
908 free_watches (transport);
910 _dbus_close_socket (socket_transport->fd, NULL);
911 socket_transport->fd = -1;
915 socket_connection_set (DBusTransport *transport)
917 DBusTransportSocket *socket_transport = (DBusTransportSocket*) transport;
919 _dbus_watch_set_handler (socket_transport->write_watch,
920 _dbus_connection_handle_watch,
921 transport->connection, NULL);
923 _dbus_watch_set_handler (socket_transport->read_watch,
924 _dbus_connection_handle_watch,
925 transport->connection, NULL);
927 if (!_dbus_connection_add_watch_unlocked (transport->connection,
928 socket_transport->write_watch))
931 if (!_dbus_connection_add_watch_unlocked (transport->connection,
932 socket_transport->read_watch))
934 _dbus_connection_remove_watch_unlocked (transport->connection,
935 socket_transport->write_watch);
939 check_read_watch (transport);
940 check_write_watch (transport);
946 * @todo We need to have a way to wake up the select sleep if
947 * a new iteration request comes in with a flag (read/write) that
948 * we're not currently serving. Otherwise a call that just reads
949 * could block a write call forever (if there are no incoming
953 socket_do_iteration (DBusTransport *transport,
955 int timeout_milliseconds)
957 DBusTransportSocket *socket_transport = (DBusTransportSocket*) transport;
962 _dbus_verbose (" iteration flags = %s%s timeout = %d read_watch = %p write_watch = %p fd = %d\n",
963 flags & DBUS_ITERATION_DO_READING ? "read" : "",
964 flags & DBUS_ITERATION_DO_WRITING ? "write" : "",
965 timeout_milliseconds,
966 socket_transport->read_watch,
967 socket_transport->write_watch,
968 socket_transport->fd);
970 /* the passed in DO_READING/DO_WRITING flags indicate whether to
971 * read/write messages, but regardless of those we may need to block
972 * for reading/writing to do auth. But if we do reading for auth,
973 * we don't want to read any messages yet if not given DO_READING.
976 poll_fd.fd = socket_transport->fd;
979 if (_dbus_transport_get_is_authenticated (transport))
981 /* This is kind of a hack; if we have stuff to write, then try
982 * to avoid the poll. This is probably about a 5% speedup on an
983 * echo client/server.
985 * If both reading and writing were requested, we want to avoid this
986 * since it could have funky effects:
987 * - both ends spinning waiting for the other one to read
988 * data so they can finish writing
989 * - prioritizing all writing ahead of reading
991 if ((flags & DBUS_ITERATION_DO_WRITING) &&
992 !(flags & (DBUS_ITERATION_DO_READING | DBUS_ITERATION_BLOCK)) &&
993 !transport->disconnected &&
994 _dbus_connection_has_messages_to_send_unlocked (transport->connection))
996 do_writing (transport);
998 if (transport->disconnected ||
999 !_dbus_connection_has_messages_to_send_unlocked (transport->connection))
1003 /* If we get here, we decided to do the poll() after all */
1004 _dbus_assert (socket_transport->read_watch);
1005 if (flags & DBUS_ITERATION_DO_READING)
1006 poll_fd.events |= _DBUS_POLLIN;
1008 _dbus_assert (socket_transport->write_watch);
1009 if (flags & DBUS_ITERATION_DO_WRITING)
1010 poll_fd.events |= _DBUS_POLLOUT;
1014 DBusAuthState auth_state;
1016 auth_state = _dbus_auth_do_work (transport->auth);
1018 if (transport->receive_credentials_pending ||
1019 auth_state == DBUS_AUTH_STATE_WAITING_FOR_INPUT)
1020 poll_fd.events |= _DBUS_POLLIN;
1022 if (transport->send_credentials_pending ||
1023 auth_state == DBUS_AUTH_STATE_HAVE_BYTES_TO_SEND)
1024 poll_fd.events |= _DBUS_POLLOUT;
1029 if (flags & DBUS_ITERATION_BLOCK)
1030 poll_timeout = timeout_milliseconds;
1034 /* For blocking selects we drop the connection lock here
1035 * to avoid blocking out connection access during a potentially
1036 * indefinite blocking call. The io path is still protected
1037 * by the io_path_cond condvar, so we won't reenter this.
1039 if (flags & DBUS_ITERATION_BLOCK)
1041 _dbus_verbose ("unlock %s pre poll\n", _DBUS_FUNCTION_NAME);
1042 _dbus_connection_unlock (transport->connection);
1046 poll_res = _dbus_poll (&poll_fd, 1, poll_timeout);
1048 if (poll_res < 0 && _dbus_get_is_errno_eintr ())
1051 if (flags & DBUS_ITERATION_BLOCK)
1053 _dbus_verbose ("lock %s post poll\n", _DBUS_FUNCTION_NAME);
1054 _dbus_connection_lock (transport->connection);
1060 poll_fd.revents = 0; /* some concern that posix does not guarantee this;
1061 * valgrind flags it as an error. though it probably
1062 * is guaranteed on linux at least.
1065 if (poll_fd.revents & _DBUS_POLLERR)
1066 do_io_error (transport);
1069 dbus_bool_t need_read = (poll_fd.revents & _DBUS_POLLIN) > 0;
1070 dbus_bool_t need_write = (poll_fd.revents & _DBUS_POLLOUT) > 0;
1071 dbus_bool_t authentication_completed;
1073 _dbus_verbose ("in iteration, need_read=%d need_write=%d\n",
1074 need_read, need_write);
1075 do_authentication (transport, need_read, need_write,
1076 &authentication_completed);
1078 /* See comment in socket_handle_watch. */
1079 if (authentication_completed)
1082 if (need_read && (flags & DBUS_ITERATION_DO_READING))
1083 do_reading (transport);
1084 if (need_write && (flags & DBUS_ITERATION_DO_WRITING))
1085 do_writing (transport);
1090 _dbus_verbose ("Error from _dbus_poll(): %s\n",
1091 _dbus_strerror_from_errno ());
1097 /* We need to install the write watch only if we did not
1098 * successfully write everything. Note we need to be careful that we
1099 * don't call check_write_watch *before* do_writing, since it's
1100 * inefficient to add the write watch, and we can avoid it most of
1101 * the time since we can write immediately.
1103 * However, we MUST always call check_write_watch(); DBusConnection code
1104 * relies on the fact that running an iteration will notice that
1105 * messages are pending.
1107 check_write_watch (transport);
1109 _dbus_verbose (" ... leaving do_iteration()\n");
1113 socket_live_messages_changed (DBusTransport *transport)
1115 /* See if we should look for incoming messages again */
1116 check_read_watch (transport);
1121 socket_get_socket_fd (DBusTransport *transport,
1124 DBusTransportSocket *socket_transport = (DBusTransportSocket*) transport;
1126 *fd_p = socket_transport->fd;
1131 static const DBusTransportVTable socket_vtable = {
1133 socket_handle_watch,
1135 socket_connection_set,
1136 socket_do_iteration,
1137 socket_live_messages_changed,
1138 socket_get_socket_fd
1142 * Creates a new transport for the given socket file descriptor. The file
1143 * descriptor must be nonblocking (use _dbus_set_fd_nonblocking() to
1144 * make it so). This function is shared by various transports that
1145 * boil down to a full duplex file descriptor.
1147 * @param fd the file descriptor.
1148 * @param server_guid non-#NULL if this transport is on the server side of a connection
1149 * @param address the transport's address
1150 * @returns the new transport, or #NULL if no memory.
1153 _dbus_transport_new_for_socket (int fd,
1154 const DBusString *server_guid,
1155 const DBusString *address)
1157 DBusTransportSocket *socket_transport;
1159 socket_transport = dbus_new0 (DBusTransportSocket, 1);
1160 if (socket_transport == NULL)
1163 if (!_dbus_string_init (&socket_transport->encoded_outgoing))
1166 if (!_dbus_string_init (&socket_transport->encoded_incoming))
1169 socket_transport->write_watch = _dbus_watch_new (fd,
1170 DBUS_WATCH_WRITABLE,
1173 if (socket_transport->write_watch == NULL)
1176 socket_transport->read_watch = _dbus_watch_new (fd,
1177 DBUS_WATCH_READABLE,
1180 if (socket_transport->read_watch == NULL)
1183 if (!_dbus_transport_init_base (&socket_transport->base,
1185 server_guid, address))
1188 socket_transport->fd = fd;
1189 socket_transport->message_bytes_written = 0;
1191 /* These values should probably be tunable or something. */
1192 socket_transport->max_bytes_read_per_iteration = 2048;
1193 socket_transport->max_bytes_written_per_iteration = 2048;
1195 return (DBusTransport*) socket_transport;
1198 _dbus_watch_unref (socket_transport->read_watch);
1200 _dbus_watch_unref (socket_transport->write_watch);
1202 _dbus_string_free (&socket_transport->encoded_incoming);
1204 _dbus_string_free (&socket_transport->encoded_outgoing);
1206 dbus_free (socket_transport);
1211 * Creates a new transport for the given hostname and port.
1212 * If host is NULL, it will default to localhost
1214 * @param host the host to connect to
1215 * @param port the port to connect to
1216 * @param family the address family to connect to
1217 * @param error location to store reason for failure.
1218 * @returns a new transport, or #NULL on failure.
1221 _dbus_transport_new_for_tcp_socket (const char *host,
1227 DBusTransport *transport;
1230 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
1232 if (!_dbus_string_init (&address))
1234 dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
1241 if (!_dbus_string_append (&address, "tcp:"))
1244 if (!_dbus_string_append (&address, "host=") ||
1245 !_dbus_string_append (&address, host))
1248 if (!_dbus_string_append (&address, ",port=") ||
1249 !_dbus_string_append (&address, port))
1252 if (family != NULL &&
1253 (!_dbus_string_append (&address, "family=") ||
1254 !_dbus_string_append (&address, family)))
1257 fd = _dbus_connect_tcp_socket (host, port, family, error);
1260 _DBUS_ASSERT_ERROR_IS_SET (error);
1261 _dbus_string_free (&address);
1265 _dbus_fd_set_close_on_exec (fd);
1267 _dbus_verbose ("Successfully connected to tcp socket %s:%s\n",
1270 transport = _dbus_transport_new_for_socket (fd, NULL, &address);
1271 if (transport == NULL)
1273 dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
1274 _dbus_close_socket (fd, NULL);
1275 _dbus_string_free (&address);
1279 _dbus_string_free (&address);
1284 _dbus_string_free (&address);
1285 dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
1290 * Opens a TCP socket transport.
1292 * @param entry the address entry to try opening as a tcp transport.
1293 * @param transport_p return location for the opened transport
1294 * @param error error to be set
1295 * @returns result of the attempt
1297 DBusTransportOpenResult
1298 _dbus_transport_open_socket(DBusAddressEntry *entry,
1299 DBusTransport **transport_p,
1304 method = dbus_address_entry_get_method (entry);
1305 _dbus_assert (method != NULL);
1307 if (strcmp (method, "tcp") == 0)
1309 const char *host = dbus_address_entry_get_value (entry, "host");
1310 const char *port = dbus_address_entry_get_value (entry, "port");
1311 const char *family = dbus_address_entry_get_value (entry, "family");
1315 _dbus_set_bad_address (error, "tcp", "port", NULL);
1316 return DBUS_TRANSPORT_OPEN_BAD_ADDRESS;
1319 *transport_p = _dbus_transport_new_for_tcp_socket (host, port, family, error);
1320 if (*transport_p == NULL)
1322 _DBUS_ASSERT_ERROR_IS_SET (error);
1323 return DBUS_TRANSPORT_OPEN_DID_NOT_CONNECT;
1327 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
1328 return DBUS_TRANSPORT_OPEN_OK;
1333 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
1334 return DBUS_TRANSPORT_OPEN_NOT_HANDLED;