1 /* -*- mode: C; c-file-style: "gnu" -*- */
2 /* dbus-transport-unix.c UNIX socket subclasses of DBusTransport
4 * Copyright (C) 2002, 2003 Red Hat Inc.
6 * Licensed under the Academic Free License version 1.2
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-unix.h"
27 #include "dbus-transport-protected.h"
28 #include "dbus-watch.h"
29 #include <sys/types.h>
35 * @defgroup DBusTransportUnix DBusTransport implementations for UNIX
36 * @ingroup DBusInternals
37 * @brief Implementation details of DBusTransport on UNIX
43 * Opaque object representing a Unix file descriptor transport.
45 typedef struct DBusTransportUnix DBusTransportUnix;
48 * Implementation details of DBusTransportUnix. All members are private.
50 struct DBusTransportUnix
52 DBusTransport base; /**< Parent instance */
53 int fd; /**< File descriptor. */
54 DBusWatch *read_watch; /**< Watch for readability. */
55 DBusWatch *write_watch; /**< Watch for writability. */
57 int max_bytes_read_per_iteration; /**< To avoid blocking too long. */
58 int max_bytes_written_per_iteration; /**< To avoid blocking too long. */
60 int message_bytes_written; /**< Number of bytes of current
61 * outgoing message that have
64 DBusString encoded_outgoing; /**< Encoded version of current
67 DBusString encoded_incoming; /**< Encoded version of current
73 free_watches (DBusTransport *transport)
75 DBusTransportUnix *unix_transport = (DBusTransportUnix*) transport;
77 if (unix_transport->read_watch)
79 if (transport->connection)
80 _dbus_connection_remove_watch (transport->connection,
81 unix_transport->read_watch);
82 _dbus_watch_invalidate (unix_transport->read_watch);
83 _dbus_watch_unref (unix_transport->read_watch);
84 unix_transport->read_watch = NULL;
87 if (unix_transport->write_watch)
89 if (transport->connection)
90 _dbus_connection_remove_watch (transport->connection,
91 unix_transport->write_watch);
92 _dbus_watch_invalidate (unix_transport->write_watch);
93 _dbus_watch_unref (unix_transport->write_watch);
94 unix_transport->write_watch = NULL;
99 unix_finalize (DBusTransport *transport)
101 DBusTransportUnix *unix_transport = (DBusTransportUnix*) transport;
103 free_watches (transport);
105 _dbus_string_free (&unix_transport->encoded_outgoing);
106 _dbus_string_free (&unix_transport->encoded_incoming);
108 _dbus_transport_finalize_base (transport);
110 _dbus_assert (unix_transport->read_watch == NULL);
111 _dbus_assert (unix_transport->write_watch == NULL);
113 dbus_free (transport);
117 check_write_watch (DBusTransport *transport)
119 DBusTransportUnix *unix_transport = (DBusTransportUnix*) transport;
120 dbus_bool_t need_write_watch;
122 if (transport->connection == NULL)
125 if (transport->disconnected)
127 _dbus_assert (unix_transport->write_watch == NULL);
131 _dbus_transport_ref (transport);
133 if (_dbus_transport_get_is_authenticated (transport))
134 need_write_watch = transport->messages_need_sending;
136 need_write_watch = transport->send_credentials_pending ||
137 _dbus_auth_do_work (transport->auth) == DBUS_AUTH_STATE_HAVE_BYTES_TO_SEND;
139 _dbus_connection_toggle_watch (transport->connection,
140 unix_transport->write_watch,
143 _dbus_transport_unref (transport);
147 check_read_watch (DBusTransport *transport)
149 DBusTransportUnix *unix_transport = (DBusTransportUnix*) transport;
150 dbus_bool_t need_read_watch;
152 if (transport->connection == NULL)
155 if (transport->disconnected)
157 _dbus_assert (unix_transport->read_watch == NULL);
161 _dbus_transport_ref (transport);
163 if (_dbus_transport_get_is_authenticated (transport))
165 _dbus_counter_get_value (transport->live_messages_size) < transport->max_live_messages_size;
167 need_read_watch = transport->receive_credentials_pending ||
168 _dbus_auth_do_work (transport->auth) == DBUS_AUTH_STATE_WAITING_FOR_INPUT;
170 _dbus_connection_toggle_watch (transport->connection,
171 unix_transport->read_watch,
174 _dbus_transport_unref (transport);
178 do_io_error (DBusTransport *transport)
180 _dbus_transport_ref (transport);
181 _dbus_transport_disconnect (transport);
182 _dbus_transport_unref (transport);
185 /* return value is whether we successfully read any new data. */
187 read_data_into_auth (DBusTransport *transport,
190 DBusTransportUnix *unix_transport = (DBusTransportUnix*) transport;
196 _dbus_auth_get_buffer (transport->auth, &buffer);
198 bytes_read = _dbus_read (unix_transport->fd,
199 buffer, unix_transport->max_bytes_read_per_iteration);
201 _dbus_auth_return_buffer (transport->auth, buffer,
202 bytes_read > 0 ? bytes_read : 0);
206 _dbus_verbose (" read %d bytes in auth phase\n", bytes_read);
210 else if (bytes_read < 0)
212 /* EINTR already handled for us */
218 else if (errno == EAGAIN ||
219 errno == EWOULDBLOCK)
220 ; /* do nothing, just return FALSE below */
223 _dbus_verbose ("Error reading from remote app: %s\n",
224 _dbus_strerror (errno));
225 do_io_error (transport);
232 _dbus_assert (bytes_read == 0);
234 _dbus_verbose ("Disconnected from remote app\n");
235 do_io_error (transport);
241 /* Return value is whether we successfully wrote any bytes */
243 write_data_from_auth (DBusTransport *transport)
245 DBusTransportUnix *unix_transport = (DBusTransportUnix*) transport;
247 const DBusString *buffer;
249 if (!_dbus_auth_get_bytes_to_send (transport->auth,
253 bytes_written = _dbus_write (unix_transport->fd,
255 0, _dbus_string_get_length (buffer));
257 if (bytes_written > 0)
259 _dbus_auth_bytes_sent (transport->auth, bytes_written);
262 else if (bytes_written < 0)
264 /* EINTR already handled for us */
266 if (errno == EAGAIN ||
267 errno == EWOULDBLOCK)
271 _dbus_verbose ("Error writing to remote app: %s\n",
272 _dbus_strerror (errno));
273 do_io_error (transport);
281 exchange_credentials (DBusTransport *transport,
282 dbus_bool_t do_reading,
283 dbus_bool_t do_writing)
285 DBusTransportUnix *unix_transport = (DBusTransportUnix*) transport;
287 if (do_writing && transport->send_credentials_pending)
289 if (_dbus_send_credentials_unix_socket (unix_transport->fd,
292 transport->send_credentials_pending = FALSE;
296 _dbus_verbose ("Failed to write credentials\n");
297 do_io_error (transport);
301 if (do_reading && transport->receive_credentials_pending)
303 if (_dbus_read_credentials_unix_socket (unix_transport->fd,
304 &transport->credentials,
307 transport->receive_credentials_pending = FALSE;
311 _dbus_verbose ("Failed to read credentials\n");
312 do_io_error (transport);
316 if (!(transport->send_credentials_pending ||
317 transport->receive_credentials_pending))
319 _dbus_auth_set_credentials (transport->auth,
320 &transport->credentials);
325 do_authentication (DBusTransport *transport,
326 dbus_bool_t do_reading,
327 dbus_bool_t do_writing)
331 _dbus_transport_ref (transport);
335 while (!_dbus_transport_get_is_authenticated (transport) &&
336 _dbus_transport_get_is_connected (transport))
338 exchange_credentials (transport, do_reading, do_writing);
340 if (transport->send_credentials_pending ||
341 transport->receive_credentials_pending)
343 _dbus_verbose ("send_credentials_pending = %d receive_credentials_pending = %d\n",
344 transport->send_credentials_pending,
345 transport->receive_credentials_pending);
349 switch (_dbus_auth_do_work (transport->auth))
351 case DBUS_AUTH_STATE_WAITING_FOR_INPUT:
352 _dbus_verbose (" auth state: waiting for input\n");
353 if (!do_reading || !read_data_into_auth (transport, &oom))
357 case DBUS_AUTH_STATE_WAITING_FOR_MEMORY:
358 _dbus_verbose (" auth state: waiting for memory\n");
363 case DBUS_AUTH_STATE_HAVE_BYTES_TO_SEND:
364 _dbus_verbose (" auth state: bytes to send\n");
365 if (!do_writing || !write_data_from_auth (transport))
369 case DBUS_AUTH_STATE_NEED_DISCONNECT:
370 _dbus_verbose (" auth state: need to disconnect\n");
371 do_io_error (transport);
374 case DBUS_AUTH_STATE_AUTHENTICATED_WITH_UNUSED_BYTES:
375 _dbus_verbose (" auth state: auth with unused bytes\n");
376 /* We'll recover the unused bytes in dbus-transport.c */
380 case DBUS_AUTH_STATE_AUTHENTICATED:
381 _dbus_verbose (" auth state: authenticated\n");
387 check_read_watch (transport);
388 check_write_watch (transport);
389 _dbus_transport_unref (transport);
397 /* returns false on oom */
399 do_writing (DBusTransport *transport)
402 DBusTransportUnix *unix_transport = (DBusTransportUnix*) transport;
405 /* No messages without authentication! */
406 if (!_dbus_transport_get_is_authenticated (transport))
408 _dbus_verbose ("Not authenticated, not writing anything\n");
412 if (transport->disconnected)
414 _dbus_verbose ("Not connected, not writing anything\n");
419 _dbus_verbose ("do_writing(), have_messages = %d\n",
420 _dbus_connection_have_messages_to_send (transport->connection));
426 while (!transport->disconnected &&
427 _dbus_connection_have_messages_to_send (transport->connection))
430 DBusMessage *message;
431 const DBusString *header;
432 const DBusString *body;
433 int header_len, body_len;
434 int total_bytes_to_write;
436 if (total > unix_transport->max_bytes_written_per_iteration)
438 _dbus_verbose ("%d bytes exceeds %d bytes written per iteration, returning\n",
439 total, unix_transport->max_bytes_written_per_iteration);
443 if (!dbus_watch_get_enabled (unix_transport->write_watch))
445 _dbus_verbose ("write watch disabled, not writing more stuff\n");
449 message = _dbus_connection_get_message_to_send (transport->connection);
450 _dbus_assert (message != NULL);
451 _dbus_message_lock (message);
454 _dbus_verbose ("writing message %p\n", message);
457 _dbus_message_get_network_data (message,
460 header_len = _dbus_string_get_length (header);
461 body_len = _dbus_string_get_length (body);
463 if (_dbus_auth_needs_encoding (transport->auth))
465 if (_dbus_string_get_length (&unix_transport->encoded_outgoing) == 0)
467 if (!_dbus_auth_encode_data (transport->auth,
468 header, &unix_transport->encoded_outgoing))
474 if (!_dbus_auth_encode_data (transport->auth,
475 body, &unix_transport->encoded_outgoing))
477 _dbus_string_set_length (&unix_transport->encoded_outgoing, 0);
483 total_bytes_to_write = _dbus_string_get_length (&unix_transport->encoded_outgoing);
486 _dbus_verbose ("encoded message is %d bytes\n",
487 total_bytes_to_write);
491 _dbus_write (unix_transport->fd,
492 &unix_transport->encoded_outgoing,
493 unix_transport->message_bytes_written,
494 total_bytes_to_write - unix_transport->message_bytes_written);
498 total_bytes_to_write = header_len + body_len;
501 _dbus_verbose ("message is %d bytes\n",
502 total_bytes_to_write);
505 if (unix_transport->message_bytes_written < header_len)
508 _dbus_write_two (unix_transport->fd,
510 unix_transport->message_bytes_written,
511 header_len - unix_transport->message_bytes_written,
518 _dbus_write (unix_transport->fd,
520 (unix_transport->message_bytes_written - header_len),
522 (unix_transport->message_bytes_written - header_len));
526 if (bytes_written < 0)
528 /* EINTR already handled for us */
530 if (errno == EAGAIN ||
531 errno == EWOULDBLOCK)
535 _dbus_verbose ("Error writing to remote app: %s\n",
536 _dbus_strerror (errno));
537 do_io_error (transport);
543 _dbus_verbose (" wrote %d bytes of %d\n", bytes_written,
544 total_bytes_to_write);
546 total += bytes_written;
547 unix_transport->message_bytes_written += bytes_written;
549 _dbus_assert (unix_transport->message_bytes_written <=
550 total_bytes_to_write);
552 if (unix_transport->message_bytes_written == total_bytes_to_write)
554 unix_transport->message_bytes_written = 0;
555 _dbus_string_set_length (&unix_transport->encoded_outgoing, 0);
557 _dbus_connection_message_sent (transport->connection,
570 /* returns false on out-of-memory */
572 do_reading (DBusTransport *transport)
574 DBusTransportUnix *unix_transport = (DBusTransportUnix*) transport;
580 /* No messages without authentication! */
581 if (!_dbus_transport_get_is_authenticated (transport))
590 /* See if we've exceeded max messages and need to disable reading */
591 check_read_watch (transport);
592 if (!dbus_watch_get_enabled (unix_transport->read_watch))
595 if (total > unix_transport->max_bytes_read_per_iteration)
597 _dbus_verbose ("%d bytes exceeds %d bytes read per iteration, returning\n",
598 total, unix_transport->max_bytes_read_per_iteration);
602 if (transport->disconnected)
605 if (_dbus_auth_needs_decoding (transport->auth))
607 if (_dbus_string_get_length (&unix_transport->encoded_incoming) > 0)
608 bytes_read = _dbus_string_get_length (&unix_transport->encoded_incoming);
610 bytes_read = _dbus_read (unix_transport->fd,
611 &unix_transport->encoded_incoming,
612 unix_transport->max_bytes_read_per_iteration);
614 _dbus_assert (_dbus_string_get_length (&unix_transport->encoded_incoming) ==
621 _dbus_message_loader_get_buffer (transport->loader,
624 orig_len = _dbus_string_get_length (buffer);
626 if (!_dbus_auth_decode_data (transport->auth,
627 &unix_transport->encoded_incoming,
630 _dbus_verbose ("Out of memory decoding incoming data\n");
635 _dbus_message_loader_return_buffer (transport->loader,
637 _dbus_string_get_length (buffer) - orig_len);
639 _dbus_string_set_length (&unix_transport->encoded_incoming, 0);
644 _dbus_message_loader_get_buffer (transport->loader,
647 bytes_read = _dbus_read (unix_transport->fd,
648 buffer, unix_transport->max_bytes_read_per_iteration);
650 _dbus_message_loader_return_buffer (transport->loader,
652 bytes_read < 0 ? 0 : bytes_read);
657 /* EINTR already handled for us */
661 _dbus_verbose ("Out of memory in read()/do_reading()\n");
665 else if (errno == EAGAIN ||
666 errno == EWOULDBLOCK)
670 _dbus_verbose ("Error reading from remote app: %s\n",
671 _dbus_strerror (errno));
672 do_io_error (transport);
676 else if (bytes_read == 0)
678 _dbus_verbose ("Disconnected from remote app\n");
679 do_io_error (transport);
684 _dbus_verbose (" read %d bytes\n", bytes_read);
688 if (_dbus_transport_queue_messages (transport) == DBUS_DISPATCH_NEED_MEMORY)
691 _dbus_verbose (" out of memory when queueing messages we just read in the transport\n");
695 /* Try reading more data until we get EAGAIN and return, or
696 * exceed max bytes per iteration. If in blocking mode of
697 * course we'll block instead of returning.
710 unix_handle_watch (DBusTransport *transport,
714 DBusTransportUnix *unix_transport = (DBusTransportUnix*) transport;
716 _dbus_assert (watch == unix_transport->read_watch ||
717 watch == unix_transport->write_watch);
719 if (flags & (DBUS_WATCH_HANGUP | DBUS_WATCH_ERROR))
721 _dbus_transport_disconnect (transport);
725 if (watch == unix_transport->read_watch &&
726 (flags & DBUS_WATCH_READABLE))
729 _dbus_verbose ("handling read watch\n");
731 if (!do_authentication (transport, TRUE, FALSE))
734 if (!do_reading (transport))
736 _dbus_verbose ("no memory to read\n");
740 else if (watch == unix_transport->write_watch &&
741 (flags & DBUS_WATCH_WRITABLE))
744 _dbus_verbose ("handling write watch, messages_need_sending = %d\n",
745 transport->messages_need_sending);
747 if (!do_authentication (transport, FALSE, TRUE))
750 if (!do_writing (transport))
752 _dbus_verbose ("no memory to write\n");
756 #ifdef DBUS_ENABLE_VERBOSE_MODE
759 if (watch == unix_transport->read_watch)
760 _dbus_verbose ("asked to handle read watch with non-read condition 0x%x\n",
762 else if (watch == unix_transport->write_watch)
763 _dbus_verbose ("asked to handle write watch with non-write condition 0x%x\n",
766 _dbus_verbose ("asked to handle watch %p on fd %d that we don't recognize\n",
767 watch, dbus_watch_get_fd (watch));
769 #endif /* DBUS_ENABLE_VERBOSE_MODE */
775 unix_disconnect (DBusTransport *transport)
777 DBusTransportUnix *unix_transport = (DBusTransportUnix*) transport;
779 free_watches (transport);
781 _dbus_close (unix_transport->fd, NULL);
782 unix_transport->fd = -1;
786 unix_connection_set (DBusTransport *transport)
788 DBusTransportUnix *unix_transport = (DBusTransportUnix*) transport;
790 if (!_dbus_connection_add_watch (transport->connection,
791 unix_transport->write_watch))
794 if (!_dbus_connection_add_watch (transport->connection,
795 unix_transport->read_watch))
797 _dbus_connection_remove_watch (transport->connection,
798 unix_transport->write_watch);
802 check_read_watch (transport);
803 check_write_watch (transport);
809 unix_messages_pending (DBusTransport *transport,
810 int messages_pending)
812 check_write_watch (transport);
815 /* FIXME use _dbus_poll(), not select() */
817 * @todo We need to have a way to wake up the select sleep if
818 * a new iteration request comes in with a flag (read/write) that
819 * we're not currently serving. Otherwise a call that just reads
820 * could block a write call forever (if there are no incoming
824 unix_do_iteration (DBusTransport *transport,
826 int timeout_milliseconds)
828 DBusTransportUnix *unix_transport = (DBusTransportUnix*) transport;
831 dbus_bool_t do_select;
834 _dbus_verbose (" iteration flags = %s%s timeout = %d read_watch = %p write_watch = %p\n",
835 flags & DBUS_ITERATION_DO_READING ? "read" : "",
836 flags & DBUS_ITERATION_DO_WRITING ? "write" : "",
837 timeout_milliseconds,
838 unix_transport->read_watch,
839 unix_transport->write_watch);
841 /* "again" has to be up here because on EINTR the fd sets become
848 /* the passed in DO_READING/DO_WRITING flags indicate whether to
849 * read/write messages, but regardless of those we may need to block
850 * for reading/writing to do auth. But if we do reading for auth,
851 * we don't want to read any messages yet if not given DO_READING.
853 * Also, if read_watch == NULL or write_watch == NULL, we don't
854 * want to read/write so don't.
858 FD_ZERO (&write_set);
860 if (_dbus_transport_get_is_authenticated (transport))
862 if (unix_transport->read_watch &&
863 (flags & DBUS_ITERATION_DO_READING))
865 FD_SET (unix_transport->fd, &read_set);
869 if (unix_transport->write_watch &&
870 (flags & DBUS_ITERATION_DO_WRITING))
872 FD_SET (unix_transport->fd, &write_set);
878 DBusAuthState auth_state;
880 auth_state = _dbus_auth_do_work (transport->auth);
882 if (transport->receive_credentials_pending ||
883 auth_state == DBUS_AUTH_STATE_WAITING_FOR_INPUT)
885 FD_SET (unix_transport->fd, &read_set);
889 if (transport->send_credentials_pending ||
890 auth_state == DBUS_AUTH_STATE_HAVE_BYTES_TO_SEND)
892 FD_SET (unix_transport->fd, &write_set);
900 struct timeval timeout;
901 dbus_bool_t use_timeout;
904 FD_SET (unix_transport->fd, &err_set);
906 if (flags & DBUS_ITERATION_BLOCK)
908 if (timeout_milliseconds >= 0)
910 timeout.tv_sec = timeout_milliseconds / 1000;
911 timeout.tv_usec = (timeout_milliseconds % 1000) * 1000;
913 /* Always use timeout if one is passed in. */
918 use_timeout = FALSE; /* NULL timeout to block forever */
923 /* 0 timeout to not block */
929 /* For blocking selects we drop the connection lock here
930 * to avoid blocking out connection access during a potentially
931 * indefinite blocking call. The io path is still protected
932 * by the io_path_cond condvar, so we won't reenter this.
934 if (flags & DBUS_ITERATION_BLOCK)
935 _dbus_connection_unlock (transport->connection);
937 select_res = select (unix_transport->fd + 1,
938 &read_set, &write_set, &err_set,
939 use_timeout ? &timeout : NULL);
941 if (flags & DBUS_ITERATION_BLOCK)
942 _dbus_connection_lock (transport->connection);
947 if (FD_ISSET (unix_transport->fd, &err_set))
948 do_io_error (transport);
951 dbus_bool_t need_read = FD_ISSET (unix_transport->fd, &read_set);
952 dbus_bool_t need_write = FD_ISSET (unix_transport->fd, &write_set);
954 _dbus_verbose ("in iteration, need_read=%d need_write=%d\n",
955 need_read, need_write);
956 do_authentication (transport, need_read, need_write);
958 if (need_read && (flags & DBUS_ITERATION_DO_READING))
959 do_reading (transport);
960 if (need_write && (flags & DBUS_ITERATION_DO_WRITING))
961 do_writing (transport);
964 else if (errno == EINTR)
968 _dbus_verbose ("Error from select(): %s\n",
969 _dbus_strerror (errno));
975 unix_live_messages_changed (DBusTransport *transport)
977 /* See if we should look for incoming messages again */
978 check_read_watch (transport);
981 static DBusTransportVTable unix_vtable = {
986 unix_messages_pending,
988 unix_live_messages_changed
992 * Creates a new transport for the given file descriptor. The file
993 * descriptor must be nonblocking (use _dbus_set_fd_nonblocking() to
994 * make it so). This function is shared by various transports that
995 * boil down to a full duplex file descriptor.
997 * @param fd the file descriptor.
998 * @param server #TRUE if this transport is on the server side of a connection
999 * @param address the transport's address
1000 * @returns the new transport, or #NULL if no memory.
1003 _dbus_transport_new_for_fd (int fd,
1005 const DBusString *address)
1007 DBusTransportUnix *unix_transport;
1009 unix_transport = dbus_new0 (DBusTransportUnix, 1);
1010 if (unix_transport == NULL)
1013 if (!_dbus_string_init (&unix_transport->encoded_outgoing))
1016 if (!_dbus_string_init (&unix_transport->encoded_incoming))
1019 unix_transport->write_watch = _dbus_watch_new (fd,
1020 DBUS_WATCH_WRITABLE,
1022 if (unix_transport->write_watch == NULL)
1025 unix_transport->read_watch = _dbus_watch_new (fd,
1026 DBUS_WATCH_READABLE,
1028 if (unix_transport->read_watch == NULL)
1031 if (!_dbus_transport_init_base (&unix_transport->base,
1036 unix_transport->fd = fd;
1037 unix_transport->message_bytes_written = 0;
1039 /* These values should probably be tunable or something. */
1040 unix_transport->max_bytes_read_per_iteration = 2048;
1041 unix_transport->max_bytes_written_per_iteration = 2048;
1043 return (DBusTransport*) unix_transport;
1046 _dbus_watch_unref (unix_transport->read_watch);
1048 _dbus_watch_unref (unix_transport->write_watch);
1050 _dbus_string_free (&unix_transport->encoded_incoming);
1052 _dbus_string_free (&unix_transport->encoded_outgoing);
1054 dbus_free (unix_transport);
1059 * Creates a new transport for the given Unix domain socket
1060 * path. This creates a client-side of a transport.
1062 * @param path the path to the domain socket.
1063 * @param error address where an error can be returned.
1064 * @returns a new transport, or #NULL on failure.
1067 _dbus_transport_new_for_domain_socket (const char *path,
1071 DBusTransport *transport;
1074 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
1076 if (!_dbus_string_init (&address))
1078 dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
1084 if (!_dbus_string_append (&address, "unix:path=") ||
1085 !_dbus_string_append (&address, path))
1087 dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
1091 fd = _dbus_connect_unix_socket (path, error);
1094 _DBUS_ASSERT_ERROR_IS_SET (error);
1098 _dbus_fd_set_close_on_exec (fd);
1100 _dbus_verbose ("Successfully connected to unix socket %s\n",
1103 transport = _dbus_transport_new_for_fd (fd, FALSE, &address);
1104 if (transport == NULL)
1106 dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
1110 _dbus_string_free (&address);
1115 _dbus_close (fd, NULL);
1117 _dbus_string_free (&address);
1122 * Creates a new transport for the given hostname and port.
1124 * @param host the host to connect to
1125 * @param port the port to connect to
1126 * @param error location to store reason for failure.
1127 * @returns a new transport, or #NULL on failure.
1130 _dbus_transport_new_for_tcp_socket (const char *host,
1135 DBusTransport *transport;
1138 _DBUS_ASSERT_ERROR_IS_CLEAR (error);
1140 if (!_dbus_string_init (&address))
1142 dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
1146 if (!_dbus_string_append (&address, "tcp:host=") ||
1147 !_dbus_string_append (&address, host) ||
1148 !_dbus_string_append (&address, ",port=") ||
1149 !_dbus_string_append_int (&address, port))
1151 _dbus_string_free (&address);
1152 dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
1156 fd = _dbus_connect_tcp_socket (host, port, error);
1159 _DBUS_ASSERT_ERROR_IS_SET (error);
1160 _dbus_string_free (&address);
1164 _dbus_fd_set_close_on_exec (fd);
1166 _dbus_verbose ("Successfully connected to tcp socket %s:%d\n",
1169 transport = _dbus_transport_new_for_fd (fd, FALSE, &address);
1170 if (transport == NULL)
1172 dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
1173 _dbus_close (fd, NULL);
1174 _dbus_string_free (&address);
1178 _dbus_string_free (&address);