Do not mention disallowed auth mechanisms in REJECTED message
[platform/upstream/dbus.git] / dbus / dbus-transport-socket.c
1 /* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*- */
2 /* dbus-transport-socket.c  Socket subclasses of DBusTransport
3  *
4  * Copyright (C) 2002, 2003, 2004, 2006  Red Hat Inc.
5  *
6  * Licensed under the Academic Free License version 2.1
7  * 
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.
12  *
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.
17  * 
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
21  *
22  */
23
24 #include <config.h>
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"
32
33 /**
34  * @defgroup DBusTransportSocket DBusTransport implementations for sockets
35  * @ingroup  DBusInternals
36  * @brief Implementation details of DBusTransport on sockets
37  *
38  * @{
39  */
40
41 /**
42  * Opaque object representing a socket file descriptor transport.
43  */
44 typedef struct DBusTransportSocket DBusTransportSocket;
45
46 /**
47  * Implementation details of DBusTransportSocket. All members are private.
48  */
49 struct DBusTransportSocket
50 {
51   DBusTransport base;                   /**< Parent instance */
52   DBusSocket fd;                        /**< File descriptor. */
53   DBusWatch *read_watch;                /**< Watch for readability. */
54   DBusWatch *write_watch;               /**< Watch for writability. */
55
56   int max_bytes_read_per_iteration;     /**< To avoid blocking too long. */
57   int max_bytes_written_per_iteration;  /**< To avoid blocking too long. */
58
59   int message_bytes_written;            /**< Number of bytes of current
60                                          *   outgoing message that have
61                                          *   been written.
62                                          */
63   DBusString encoded_outgoing;          /**< Encoded version of current
64                                          *   outgoing message.
65                                          */
66   DBusString encoded_incoming;          /**< Encoded version of current
67                                          *   incoming data.
68                                          */
69 };
70
71 static void
72 free_watches (DBusTransport *transport)
73 {
74   DBusTransportSocket *socket_transport = (DBusTransportSocket*) transport;
75
76   _dbus_verbose ("start\n");
77   
78   if (socket_transport->read_watch)
79     {
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;
86     }
87
88   if (socket_transport->write_watch)
89     {
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;
96     }
97
98   _dbus_verbose ("end\n");
99 }
100
101 static void
102 socket_finalize (DBusTransport *transport)
103 {
104   DBusTransportSocket *socket_transport = (DBusTransportSocket*) transport;
105
106   _dbus_verbose ("\n");
107   
108   free_watches (transport);
109
110   _dbus_string_free (&socket_transport->encoded_outgoing);
111   _dbus_string_free (&socket_transport->encoded_incoming);
112   
113   _dbus_transport_finalize_base (transport);
114
115   _dbus_assert (socket_transport->read_watch == NULL);
116   _dbus_assert (socket_transport->write_watch == NULL);
117   
118   dbus_free (transport);
119 }
120
121 static void
122 check_write_watch (DBusTransport *transport)
123 {
124   DBusTransportSocket *socket_transport = (DBusTransportSocket*) transport;
125   dbus_bool_t needed;
126
127   if (transport->connection == NULL)
128     return;
129
130   if (transport->disconnected)
131     {
132       _dbus_assert (socket_transport->write_watch == NULL);
133       return;
134     }
135   
136   _dbus_transport_ref (transport);
137
138   if (_dbus_transport_try_to_authenticate (transport))
139     needed = _dbus_connection_has_messages_to_send_unlocked (transport->connection);
140   else
141     {
142       if (transport->send_credentials_pending)
143         needed = TRUE;
144       else
145         {
146           DBusAuthState auth_state;
147           
148           auth_state = _dbus_auth_do_work (transport->auth);
149           
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.
153            */
154           if (auth_state == DBUS_AUTH_STATE_HAVE_BYTES_TO_SEND ||
155               auth_state == DBUS_AUTH_STATE_WAITING_FOR_MEMORY)
156             needed = TRUE;
157           else
158             needed = FALSE;
159         }
160     }
161
162   _dbus_verbose ("check_write_watch(): needed = %d on connection %p watch %p fd = %" DBUS_SOCKET_FORMAT " outgoing messages exist %d\n",
163                  needed, transport->connection, socket_transport->write_watch,
164                  _dbus_socket_printable (socket_transport->fd),
165                  _dbus_connection_has_messages_to_send_unlocked (transport->connection));
166
167   _dbus_connection_toggle_watch_unlocked (transport->connection,
168                                           socket_transport->write_watch,
169                                           needed);
170
171   _dbus_transport_unref (transport);
172 }
173
174 static void
175 check_read_watch (DBusTransport *transport)
176 {
177   DBusTransportSocket *socket_transport = (DBusTransportSocket*) transport;
178   dbus_bool_t need_read_watch;
179
180   _dbus_verbose ("fd = %" DBUS_SOCKET_FORMAT "\n",
181                  _dbus_socket_printable (socket_transport->fd));
182   
183   if (transport->connection == NULL)
184     return;
185
186   if (transport->disconnected)
187     {
188       _dbus_assert (socket_transport->read_watch == NULL);
189       return;
190     }
191   
192   _dbus_transport_ref (transport);
193
194   if (_dbus_transport_try_to_authenticate (transport))
195     need_read_watch =
196       (_dbus_counter_get_size_value (transport->live_messages) < transport->max_live_messages_size) &&
197       (_dbus_counter_get_unix_fd_value (transport->live_messages) < transport->max_live_messages_unix_fds);
198   else
199     {
200       if (transport->receive_credentials_pending)
201         need_read_watch = TRUE;
202       else
203         {
204           /* The reason to disable need_read_watch when not WAITING_FOR_INPUT
205            * is to avoid spinning on the file descriptor when we're waiting
206            * to write or for some other part of the auth process
207            */
208           DBusAuthState auth_state;
209           
210           auth_state = _dbus_auth_do_work (transport->auth);
211
212           /* If we need memory we install the read watch just in case,
213            * if there's no need for it, it will get de-installed
214            * next time we try reading. If we're authenticated we
215            * install it since we normally have it installed while
216            * authenticated.
217            */
218           if (auth_state == DBUS_AUTH_STATE_WAITING_FOR_INPUT ||
219               auth_state == DBUS_AUTH_STATE_WAITING_FOR_MEMORY ||
220               auth_state == DBUS_AUTH_STATE_AUTHENTICATED)
221             need_read_watch = TRUE;
222           else
223             need_read_watch = FALSE;
224         }
225     }
226
227   _dbus_verbose ("  setting read watch enabled = %d\n", need_read_watch);
228   _dbus_connection_toggle_watch_unlocked (transport->connection,
229                                           socket_transport->read_watch,
230                                           need_read_watch);
231
232   _dbus_transport_unref (transport);
233 }
234
235 static void
236 do_io_error (DBusTransport *transport)
237 {
238   _dbus_transport_ref (transport);
239   _dbus_transport_disconnect (transport);
240   _dbus_transport_unref (transport);
241 }
242
243 /* return value is whether we successfully read any new data. */
244 static dbus_bool_t
245 read_data_into_auth (DBusTransport *transport,
246                      dbus_bool_t   *oom)
247 {
248   DBusTransportSocket *socket_transport = (DBusTransportSocket*) transport;
249   DBusString *buffer;
250   int bytes_read;
251   int saved_errno;
252
253   *oom = FALSE;
254
255   _dbus_auth_get_buffer (transport->auth, &buffer);
256   
257   bytes_read = _dbus_read_socket (socket_transport->fd,
258                                   buffer, socket_transport->max_bytes_read_per_iteration);
259   saved_errno = _dbus_save_socket_errno ();
260
261   _dbus_auth_return_buffer (transport->auth, buffer);
262
263   if (bytes_read > 0)
264     {
265       _dbus_verbose (" read %d bytes in auth phase\n", bytes_read);
266
267       return TRUE;
268     }
269   else if (bytes_read < 0)
270     {
271       /* EINTR already handled for us */
272
273       if (_dbus_get_is_errno_enomem (saved_errno))
274         {
275           *oom = TRUE;
276         }
277       else if (_dbus_get_is_errno_eagain_or_ewouldblock (saved_errno))
278         ; /* do nothing, just return FALSE below */
279       else
280         {
281           _dbus_verbose ("Error reading from remote app: %s\n",
282                          _dbus_strerror (saved_errno));
283           do_io_error (transport);
284         }
285
286       return FALSE;
287     }
288   else
289     {
290       _dbus_assert (bytes_read == 0);
291       
292       _dbus_verbose ("Disconnected from remote app\n");
293       do_io_error (transport);
294
295       return FALSE;
296     }
297 }
298
299 /* Return value is whether we successfully wrote any bytes */
300 static dbus_bool_t
301 write_data_from_auth (DBusTransport *transport)
302 {
303   DBusTransportSocket *socket_transport = (DBusTransportSocket*) transport;
304   int bytes_written;
305   int saved_errno;
306   const DBusString *buffer;
307
308   if (!_dbus_auth_get_bytes_to_send (transport->auth,
309                                      &buffer))
310     return FALSE;
311   
312   bytes_written = _dbus_write_socket (socket_transport->fd,
313                                       buffer,
314                                       0, _dbus_string_get_length (buffer));
315   saved_errno = _dbus_save_socket_errno ();
316
317   if (bytes_written > 0)
318     {
319       _dbus_auth_bytes_sent (transport->auth, bytes_written);
320       return TRUE;
321     }
322   else if (bytes_written < 0)
323     {
324       /* EINTR already handled for us */
325       
326       if (_dbus_get_is_errno_eagain_or_ewouldblock (saved_errno))
327         ;
328       else
329         {
330           _dbus_verbose ("Error writing to remote app: %s\n",
331                          _dbus_strerror (saved_errno));
332           do_io_error (transport);
333         }
334     }
335
336   return FALSE;
337 }
338
339 /* FALSE on OOM */
340 static dbus_bool_t
341 exchange_credentials (DBusTransport *transport,
342                       dbus_bool_t    do_reading,
343                       dbus_bool_t    do_writing)
344 {
345   DBusTransportSocket *socket_transport = (DBusTransportSocket*) transport;
346   DBusError error = DBUS_ERROR_INIT;
347
348   _dbus_verbose ("exchange_credentials: do_reading = %d, do_writing = %d\n",
349                   do_reading, do_writing);
350
351   if (do_writing && transport->send_credentials_pending)
352     {
353       if (_dbus_send_credentials_socket (socket_transport->fd,
354                                          &error))
355         {
356           transport->send_credentials_pending = FALSE;
357         }
358       else
359         {
360           _dbus_verbose ("Failed to write credentials: %s\n", error.message);
361           dbus_error_free (&error);
362           do_io_error (transport);
363         }
364     }
365   
366   if (do_reading && transport->receive_credentials_pending)
367     {
368       /* FIXME this can fail due to IO error _or_ OOM, broken
369        * (somewhat tricky to fix since the OOM error can be set after
370        * we already read the credentials byte, so basically we need to
371        * separate reading the byte and storing it in the
372        * transport->credentials). Does not really matter for now
373        * because storing in credentials never actually fails on unix.
374        */      
375       if (_dbus_read_credentials_socket (socket_transport->fd,
376                                          transport->credentials,
377                                          &error))
378         {
379           transport->receive_credentials_pending = FALSE;
380         }
381       else
382         {
383           _dbus_verbose ("Failed to read credentials %s\n", error.message);
384           dbus_error_free (&error);
385           do_io_error (transport);
386         }
387     }
388
389   if (!(transport->send_credentials_pending ||
390         transport->receive_credentials_pending))
391     {
392       if (!_dbus_auth_set_credentials (transport->auth,
393                                        transport->credentials))
394         return FALSE;
395     }
396
397   return TRUE;
398 }
399
400 static dbus_bool_t
401 do_authentication (DBusTransport *transport,
402                    dbus_bool_t    do_reading,
403                    dbus_bool_t    do_writing,
404                    dbus_bool_t   *auth_completed)
405 {
406   dbus_bool_t oom;
407   dbus_bool_t orig_auth_state;
408
409   oom = FALSE;
410   
411   orig_auth_state = _dbus_transport_try_to_authenticate (transport);
412
413   /* This is essential to avoid the check_write_watch() at the end,
414    * we don't want to add a write watch in do_iteration before
415    * we try writing and get EAGAIN
416    */
417   if (orig_auth_state)
418     {
419       if (auth_completed)
420         *auth_completed = FALSE;
421       return TRUE;
422     }
423   
424   _dbus_transport_ref (transport);
425   
426   while (!_dbus_transport_try_to_authenticate (transport) &&
427          _dbus_transport_get_is_connected (transport))
428     {      
429       if (!exchange_credentials (transport, do_reading, do_writing))
430         {
431           /* OOM */
432           oom = TRUE;
433           goto out;
434         }
435       
436       if (transport->send_credentials_pending ||
437           transport->receive_credentials_pending)
438         {
439           _dbus_verbose ("send_credentials_pending = %d receive_credentials_pending = %d\n",
440                          transport->send_credentials_pending,
441                          transport->receive_credentials_pending);
442           goto out;
443         }
444
445 #define TRANSPORT_SIDE(t) ((t)->is_server ? "server" : "client")
446       switch (_dbus_auth_do_work (transport->auth))
447         {
448         case DBUS_AUTH_STATE_WAITING_FOR_INPUT:
449           _dbus_verbose (" %s auth state: waiting for input\n",
450                          TRANSPORT_SIDE (transport));
451           if (!do_reading || !read_data_into_auth (transport, &oom))
452             goto out;
453           break;
454       
455         case DBUS_AUTH_STATE_WAITING_FOR_MEMORY:
456           _dbus_verbose (" %s auth state: waiting for memory\n",
457                          TRANSPORT_SIDE (transport));
458           oom = TRUE;
459           goto out;
460           break;
461       
462         case DBUS_AUTH_STATE_HAVE_BYTES_TO_SEND:
463           _dbus_verbose (" %s auth state: bytes to send\n",
464                          TRANSPORT_SIDE (transport));
465           if (!do_writing || !write_data_from_auth (transport))
466             goto out;
467           break;
468       
469         case DBUS_AUTH_STATE_NEED_DISCONNECT:
470           _dbus_verbose (" %s auth state: need to disconnect\n",
471                          TRANSPORT_SIDE (transport));
472           do_io_error (transport);
473           break;
474       
475         case DBUS_AUTH_STATE_AUTHENTICATED:
476           _dbus_verbose (" %s auth state: authenticated\n",
477                          TRANSPORT_SIDE (transport));
478           break;
479         default:
480           _dbus_assert_not_reached ("invalid auth state");
481         }
482     }
483
484  out:
485   if (auth_completed)
486     *auth_completed = (orig_auth_state != _dbus_transport_try_to_authenticate (transport));
487   
488   check_read_watch (transport);
489   check_write_watch (transport);
490   _dbus_transport_unref (transport);
491
492   if (oom)
493     return FALSE;
494   else
495     return TRUE;
496 }
497
498 /* returns false on oom */
499 static dbus_bool_t
500 do_writing (DBusTransport *transport)
501 {
502   int total;
503   DBusTransportSocket *socket_transport = (DBusTransportSocket*) transport;
504   dbus_bool_t oom;
505   
506   /* No messages without authentication! */
507   if (!_dbus_transport_try_to_authenticate (transport))
508     {
509       _dbus_verbose ("Not authenticated, not writing anything\n");
510       return TRUE;
511     }
512
513   if (transport->disconnected)
514     {
515       _dbus_verbose ("Not connected, not writing anything\n");
516       return TRUE;
517     }
518
519 #if 1
520   _dbus_verbose ("do_writing(), have_messages = %d, fd = %" DBUS_SOCKET_FORMAT "\n",
521                  _dbus_connection_has_messages_to_send_unlocked (transport->connection),
522                  _dbus_socket_printable (socket_transport->fd));
523 #endif
524   
525   oom = FALSE;
526   total = 0;
527
528   while (!transport->disconnected &&
529          _dbus_connection_has_messages_to_send_unlocked (transport->connection))
530     {
531       int bytes_written;
532       DBusMessage *message;
533       const DBusString *header;
534       const DBusString *body;
535       int header_len, body_len;
536       int total_bytes_to_write;
537       int saved_errno;
538       
539       if (total > socket_transport->max_bytes_written_per_iteration)
540         {
541           _dbus_verbose ("%d bytes exceeds %d bytes written per iteration, returning\n",
542                          total, socket_transport->max_bytes_written_per_iteration);
543           goto out;
544         }
545       
546       message = _dbus_connection_get_message_to_send (transport->connection);
547       _dbus_assert (message != NULL);
548       dbus_message_lock (message);
549
550 #if 0
551       _dbus_verbose ("writing message %p\n", message);
552 #endif
553       
554       _dbus_message_get_network_data (message,
555                                       &header, &body);
556
557       header_len = _dbus_string_get_length (header);
558       body_len = _dbus_string_get_length (body);
559
560       if (_dbus_auth_needs_encoding (transport->auth))
561         {
562           /* Does fd passing even make sense with encoded data? */
563           _dbus_assert(!DBUS_TRANSPORT_CAN_SEND_UNIX_FD(transport));
564
565           if (_dbus_string_get_length (&socket_transport->encoded_outgoing) == 0)
566             {
567               if (!_dbus_auth_encode_data (transport->auth,
568                                            header, &socket_transport->encoded_outgoing))
569                 {
570                   oom = TRUE;
571                   goto out;
572                 }
573               
574               if (!_dbus_auth_encode_data (transport->auth,
575                                            body, &socket_transport->encoded_outgoing))
576                 {
577                   _dbus_string_set_length (&socket_transport->encoded_outgoing, 0);
578                   oom = TRUE;
579                   goto out;
580                 }
581             }
582           
583           total_bytes_to_write = _dbus_string_get_length (&socket_transport->encoded_outgoing);
584
585 #if 0
586           _dbus_verbose ("encoded message is %d bytes\n",
587                          total_bytes_to_write);
588 #endif
589           
590           bytes_written =
591             _dbus_write_socket (socket_transport->fd,
592                                 &socket_transport->encoded_outgoing,
593                                 socket_transport->message_bytes_written,
594                                 total_bytes_to_write - socket_transport->message_bytes_written);
595           saved_errno = _dbus_save_socket_errno ();
596         }
597       else
598         {
599           total_bytes_to_write = header_len + body_len;
600
601 #if 0
602           _dbus_verbose ("message is %d bytes\n",
603                          total_bytes_to_write);
604 #endif
605
606 #ifdef HAVE_UNIX_FD_PASSING
607           if (socket_transport->message_bytes_written <= 0 && DBUS_TRANSPORT_CAN_SEND_UNIX_FD(transport))
608             {
609               /* Send the fds along with the first byte of the message */
610               const int *unix_fds;
611               unsigned n;
612
613               _dbus_message_get_unix_fds(message, &unix_fds, &n);
614
615               bytes_written =
616                 _dbus_write_socket_with_unix_fds_two (socket_transport->fd,
617                                                       header,
618                                                       socket_transport->message_bytes_written,
619                                                       header_len - socket_transport->message_bytes_written,
620                                                       body,
621                                                       0, body_len,
622                                                       unix_fds,
623                                                       n);
624               saved_errno = _dbus_save_socket_errno ();
625
626               if (bytes_written > 0 && n > 0)
627                 _dbus_verbose("Wrote %i unix fds\n", n);
628             }
629           else
630 #endif
631             {
632               if (socket_transport->message_bytes_written < header_len)
633                 {
634                   bytes_written =
635                     _dbus_write_socket_two (socket_transport->fd,
636                                             header,
637                                             socket_transport->message_bytes_written,
638                                             header_len - socket_transport->message_bytes_written,
639                                             body,
640                                             0, body_len);
641                 }
642               else
643                 {
644                   bytes_written =
645                     _dbus_write_socket (socket_transport->fd,
646                                         body,
647                                         (socket_transport->message_bytes_written - header_len),
648                                         body_len -
649                                         (socket_transport->message_bytes_written - header_len));
650                 }
651
652               saved_errno = _dbus_save_socket_errno ();
653             }
654         }
655
656       if (bytes_written < 0)
657         {
658           /* EINTR already handled for us */
659           
660           /* If the other end closed the socket with close() or shutdown(), we
661            * receive EPIPE here but we must not close the socket yet: there
662            * might still be some data to read. See:
663            * http://lists.freedesktop.org/archives/dbus/2008-March/009526.html
664            */
665           
666           if (_dbus_get_is_errno_eagain_or_ewouldblock (saved_errno) || _dbus_get_is_errno_epipe (saved_errno))
667             goto out;
668
669           /* Since Linux commit 25888e (from 2.6.37-rc4, Nov 2010), sendmsg()
670            * on Unix sockets returns -1 errno=ETOOMANYREFS when the passfd
671            * mechanism (SCM_RIGHTS) is used recursively with a recursion level
672            * of maximum 4. The kernel does not have an API to check whether
673            * the passed fds can be forwarded and it can change asynchronously.
674            * See:
675            * https://bugs.freedesktop.org/show_bug.cgi?id=80163
676            */
677
678           else if (_dbus_get_is_errno_etoomanyrefs (saved_errno))
679             {
680               /* We only send fds in the first byte of the message.
681                * ETOOMANYREFS cannot happen after.
682                */
683               _dbus_assert (socket_transport->message_bytes_written == 0);
684
685               _dbus_verbose (" discard message of %d bytes due to ETOOMANYREFS\n",
686                              total_bytes_to_write);
687
688               socket_transport->message_bytes_written = 0;
689               _dbus_string_set_length (&socket_transport->encoded_outgoing, 0);
690               _dbus_string_compact (&socket_transport->encoded_outgoing, 2048);
691
692               /* The message was not actually sent but it needs to be removed
693                * from the outgoing queue
694                */
695               _dbus_connection_message_sent_unlocked (transport->connection,
696                                                       message);
697             }
698           else
699             {
700               _dbus_verbose ("Error writing to remote app: %s\n",
701                              _dbus_strerror (saved_errno));
702               do_io_error (transport);
703               goto out;
704             }
705         }
706       else
707         {
708           _dbus_verbose (" wrote %d bytes of %d\n", bytes_written,
709                          total_bytes_to_write);
710           
711           total += bytes_written;
712           socket_transport->message_bytes_written += bytes_written;
713
714           _dbus_assert (socket_transport->message_bytes_written <=
715                         total_bytes_to_write);
716           
717           if (socket_transport->message_bytes_written == total_bytes_to_write)
718             {
719               socket_transport->message_bytes_written = 0;
720               _dbus_string_set_length (&socket_transport->encoded_outgoing, 0);
721               _dbus_string_compact (&socket_transport->encoded_outgoing, 2048);
722
723               _dbus_connection_message_sent_unlocked (transport->connection,
724                                                       message);
725             }
726         }
727     }
728
729  out:
730   if (oom)
731     return FALSE;
732   else
733     return TRUE;
734 }
735
736 /* returns false on out-of-memory */
737 static dbus_bool_t
738 do_reading (DBusTransport *transport)
739 {
740   DBusTransportSocket *socket_transport = (DBusTransportSocket*) transport;
741   DBusString *buffer;
742   int bytes_read;
743   int total;
744   dbus_bool_t oom;
745   int saved_errno;
746
747   _dbus_verbose ("fd = %" DBUS_SOCKET_FORMAT "\n",
748                  _dbus_socket_printable (socket_transport->fd));
749   
750   /* No messages without authentication! */
751   if (!_dbus_transport_try_to_authenticate (transport))
752     return TRUE;
753
754   oom = FALSE;
755   
756   total = 0;
757
758  again:
759   
760   /* See if we've exceeded max messages and need to disable reading */
761   check_read_watch (transport);
762   
763   if (total > socket_transport->max_bytes_read_per_iteration)
764     {
765       _dbus_verbose ("%d bytes exceeds %d bytes read per iteration, returning\n",
766                      total, socket_transport->max_bytes_read_per_iteration);
767       goto out;
768     }
769
770   _dbus_assert (socket_transport->read_watch != NULL ||
771                 transport->disconnected);
772   
773   if (transport->disconnected)
774     goto out;
775
776   if (!dbus_watch_get_enabled (socket_transport->read_watch))
777     return TRUE;
778   
779   if (_dbus_auth_needs_decoding (transport->auth))
780     {
781       /* Does fd passing even make sense with encoded data? */
782       _dbus_assert(!DBUS_TRANSPORT_CAN_SEND_UNIX_FD(transport));
783
784       if (_dbus_string_get_length (&socket_transport->encoded_incoming) > 0)
785         bytes_read = _dbus_string_get_length (&socket_transport->encoded_incoming);
786       else
787         bytes_read = _dbus_read_socket (socket_transport->fd,
788                                         &socket_transport->encoded_incoming,
789                                         socket_transport->max_bytes_read_per_iteration);
790
791       saved_errno = _dbus_save_socket_errno ();
792
793       _dbus_assert (_dbus_string_get_length (&socket_transport->encoded_incoming) ==
794                     bytes_read);
795       
796       if (bytes_read > 0)
797         {
798           _dbus_message_loader_get_buffer (transport->loader,
799                                            &buffer,
800                                            NULL,
801                                            NULL);
802
803           if (!_dbus_auth_decode_data (transport->auth,
804                                        &socket_transport->encoded_incoming,
805                                        buffer))
806             {
807               _dbus_verbose ("Out of memory decoding incoming data\n");
808               _dbus_message_loader_return_buffer (transport->loader,
809                                               buffer);
810
811               oom = TRUE;
812               goto out;
813             }
814
815           _dbus_message_loader_return_buffer (transport->loader,
816                                               buffer);
817
818           _dbus_string_set_length (&socket_transport->encoded_incoming, 0);
819           _dbus_string_compact (&socket_transport->encoded_incoming, 2048);
820         }
821     }
822   else
823     {
824       int max_to_read = DBUS_MAXIMUM_MESSAGE_LENGTH;
825       dbus_bool_t may_read_unix_fds = TRUE;
826
827       _dbus_message_loader_get_buffer (transport->loader,
828                                        &buffer,
829                                        &max_to_read,
830                                        &may_read_unix_fds);
831
832       if (max_to_read > socket_transport->max_bytes_read_per_iteration)
833         max_to_read = socket_transport->max_bytes_read_per_iteration;
834
835 #ifdef HAVE_UNIX_FD_PASSING
836       if (DBUS_TRANSPORT_CAN_SEND_UNIX_FD(transport) && may_read_unix_fds)
837         {
838           int *fds;
839           unsigned int n_fds;
840
841           if (!_dbus_message_loader_get_unix_fds(transport->loader, &fds, &n_fds))
842             {
843               _dbus_verbose ("Out of memory reading file descriptors\n");
844               _dbus_message_loader_return_buffer (transport->loader, buffer);
845               oom = TRUE;
846               goto out;
847             }
848
849           bytes_read = _dbus_read_socket_with_unix_fds(socket_transport->fd,
850                                                        buffer,
851                                                        max_to_read,
852                                                        fds, &n_fds);
853           saved_errno = _dbus_save_socket_errno ();
854
855           if (bytes_read >= 0 && n_fds > 0)
856             _dbus_verbose("Read %i unix fds\n", n_fds);
857
858           _dbus_message_loader_return_unix_fds(transport->loader, fds, bytes_read < 0 ? 0 : n_fds);
859         }
860       else
861 #endif
862         {
863           bytes_read = _dbus_read_socket (socket_transport->fd,
864                                           buffer, max_to_read);
865           saved_errno = _dbus_save_socket_errno ();
866         }
867
868       _dbus_message_loader_return_buffer (transport->loader,
869                                           buffer);
870     }
871
872   if (bytes_read < 0)
873     {
874       /* EINTR already handled for us */
875
876       if (_dbus_get_is_errno_enomem (saved_errno))
877         {
878           _dbus_verbose ("Out of memory in read()/do_reading()\n");
879           oom = TRUE;
880           goto out;
881         }
882       else if (_dbus_get_is_errno_eagain_or_ewouldblock (saved_errno))
883         goto out;
884       else
885         {
886           _dbus_verbose ("Error reading from remote app: %s\n",
887                          _dbus_strerror (saved_errno));
888           do_io_error (transport);
889           goto out;
890         }
891     }
892   else if (bytes_read == 0)
893     {
894       _dbus_verbose ("Disconnected from remote app\n");
895       do_io_error (transport);
896       goto out;
897     }
898   else
899     {
900       _dbus_verbose (" read %d bytes\n", bytes_read);
901       
902       total += bytes_read;      
903
904       if (!_dbus_transport_queue_messages (transport))
905         {
906           oom = TRUE;
907           _dbus_verbose (" out of memory when queueing messages we just read in the transport\n");
908           goto out;
909         }
910       
911       /* Try reading more data until we get EAGAIN and return, or
912        * exceed max bytes per iteration.  If in blocking mode of
913        * course we'll block instead of returning.
914        */
915       goto again;
916     }
917
918  out:
919   if (oom)
920     return FALSE;
921   else
922     return TRUE;
923 }
924
925 static dbus_bool_t
926 unix_error_with_read_to_come (DBusTransport *itransport,
927                               DBusWatch     *watch,
928                               unsigned int   flags)
929 {
930   DBusTransportSocket *transport = (DBusTransportSocket *) itransport;
931
932   if (!(flags & DBUS_WATCH_HANGUP || flags & DBUS_WATCH_ERROR))
933     return FALSE;
934    
935   /* If we have a read watch enabled ...
936      we -might have data incoming ... => handle the HANGUP there */
937   if (watch != transport->read_watch &&
938       _dbus_watch_get_enabled (transport->read_watch))
939     return FALSE;
940       
941   return TRUE; 
942 }
943
944 static dbus_bool_t
945 socket_handle_watch (DBusTransport *transport,
946                    DBusWatch     *watch,
947                    unsigned int   flags)
948 {
949   DBusTransportSocket *socket_transport = (DBusTransportSocket*) transport;
950
951   _dbus_assert (watch == socket_transport->read_watch ||
952                 watch == socket_transport->write_watch);
953   _dbus_assert (watch != NULL);
954   
955   /* If we hit an error here on a write watch, don't disconnect the transport yet because data can
956    * still be in the buffer and do_reading may need several iteration to read
957    * it all (because of its max_bytes_read_per_iteration limit). 
958    */
959   if (!(flags & DBUS_WATCH_READABLE) && unix_error_with_read_to_come (transport, watch, flags))
960     {
961       _dbus_verbose ("Hang up or error on watch\n");
962       _dbus_transport_disconnect (transport);
963       return TRUE;
964     }
965   
966   if (watch == socket_transport->read_watch &&
967       (flags & DBUS_WATCH_READABLE))
968     {
969       dbus_bool_t auth_finished;
970 #if 1
971       _dbus_verbose ("handling read watch %p flags = %x\n",
972                      watch, flags);
973 #endif
974       if (!do_authentication (transport, TRUE, FALSE, &auth_finished))
975         return FALSE;
976
977       /* We don't want to do a read immediately following
978        * a successful authentication.  This is so we
979        * have a chance to propagate the authentication
980        * state further up.  Specifically, we need to
981        * process any pending data from the auth object.
982        */
983       if (!auth_finished)
984         {
985           if (!do_reading (transport))
986             {
987               _dbus_verbose ("no memory to read\n");
988               return FALSE;
989             }
990         }
991       else
992         {
993           _dbus_verbose ("Not reading anything since we just completed the authentication\n");
994         }
995     }
996   else if (watch == socket_transport->write_watch &&
997            (flags & DBUS_WATCH_WRITABLE))
998     {
999 #if 1
1000       _dbus_verbose ("handling write watch, have_outgoing_messages = %d\n",
1001                      _dbus_connection_has_messages_to_send_unlocked (transport->connection));
1002 #endif
1003       if (!do_authentication (transport, FALSE, TRUE, NULL))
1004         return FALSE;
1005       
1006       if (!do_writing (transport))
1007         {
1008           _dbus_verbose ("no memory to write\n");
1009           return FALSE;
1010         }
1011
1012       /* See if we still need the write watch */
1013       check_write_watch (transport);
1014     }
1015 #ifdef DBUS_ENABLE_VERBOSE_MODE
1016   else
1017     {
1018       if (watch == socket_transport->read_watch)
1019         _dbus_verbose ("asked to handle read watch with non-read condition 0x%x\n",
1020                        flags);
1021       else if (watch == socket_transport->write_watch)
1022         _dbus_verbose ("asked to handle write watch with non-write condition 0x%x\n",
1023                        flags);
1024       else
1025         _dbus_verbose ("asked to handle watch %p on fd %" DBUS_SOCKET_FORMAT " that we don't recognize\n",
1026                        watch, dbus_watch_get_socket (watch));
1027     }
1028 #endif /* DBUS_ENABLE_VERBOSE_MODE */
1029
1030   return TRUE;
1031 }
1032
1033 static void
1034 socket_disconnect (DBusTransport *transport)
1035 {
1036   DBusTransportSocket *socket_transport = (DBusTransportSocket*) transport;
1037
1038   _dbus_verbose ("\n");
1039   
1040   free_watches (transport);
1041   
1042   _dbus_close_socket (socket_transport->fd, NULL);
1043   _dbus_socket_invalidate (&socket_transport->fd);
1044 }
1045
1046 static dbus_bool_t
1047 socket_connection_set (DBusTransport *transport)
1048 {
1049   DBusTransportSocket *socket_transport = (DBusTransportSocket*) transport;
1050
1051   _dbus_watch_set_handler (socket_transport->write_watch,
1052                            _dbus_connection_handle_watch,
1053                            transport->connection, NULL);
1054
1055   _dbus_watch_set_handler (socket_transport->read_watch,
1056                            _dbus_connection_handle_watch,
1057                            transport->connection, NULL);
1058   
1059   if (!_dbus_connection_add_watch_unlocked (transport->connection,
1060                                             socket_transport->write_watch))
1061     return FALSE;
1062
1063   if (!_dbus_connection_add_watch_unlocked (transport->connection,
1064                                             socket_transport->read_watch))
1065     {
1066       _dbus_connection_remove_watch_unlocked (transport->connection,
1067                                               socket_transport->write_watch);
1068       return FALSE;
1069     }
1070
1071   check_read_watch (transport);
1072   check_write_watch (transport);
1073
1074   return TRUE;
1075 }
1076
1077 /**
1078  * @todo We need to have a way to wake up the select sleep if
1079  * a new iteration request comes in with a flag (read/write) that
1080  * we're not currently serving. Otherwise a call that just reads
1081  * could block a write call forever (if there are no incoming
1082  * messages).
1083  */
1084 static  void
1085 socket_do_iteration (DBusTransport *transport,
1086                    unsigned int   flags,
1087                    int            timeout_milliseconds)
1088 {
1089   DBusTransportSocket *socket_transport = (DBusTransportSocket*) transport;
1090   DBusPollFD poll_fd;
1091   int poll_res;
1092   int poll_timeout;
1093
1094   _dbus_verbose (" iteration flags = %s%s timeout = %d read_watch = %p write_watch = %p fd = %" DBUS_SOCKET_FORMAT "\n",
1095                  flags & DBUS_ITERATION_DO_READING ? "read" : "",
1096                  flags & DBUS_ITERATION_DO_WRITING ? "write" : "",
1097                  timeout_milliseconds,
1098                  socket_transport->read_watch,
1099                  socket_transport->write_watch,
1100                  _dbus_socket_printable (socket_transport->fd));
1101   
1102   /* the passed in DO_READING/DO_WRITING flags indicate whether to
1103    * read/write messages, but regardless of those we may need to block
1104    * for reading/writing to do auth.  But if we do reading for auth,
1105    * we don't want to read any messages yet if not given DO_READING.
1106    */
1107
1108   poll_fd.fd = _dbus_socket_get_pollable (socket_transport->fd);
1109   poll_fd.events = 0;
1110   
1111   if (_dbus_transport_try_to_authenticate (transport))
1112     {
1113       /* This is kind of a hack; if we have stuff to write, then try
1114        * to avoid the poll. This is probably about a 5% speedup on an
1115        * echo client/server.
1116        *
1117        * If both reading and writing were requested, we want to avoid this
1118        * since it could have funky effects:
1119        *   - both ends spinning waiting for the other one to read
1120        *     data so they can finish writing
1121        *   - prioritizing all writing ahead of reading
1122        */
1123       if ((flags & DBUS_ITERATION_DO_WRITING) &&
1124           !(flags & (DBUS_ITERATION_DO_READING | DBUS_ITERATION_BLOCK)) &&
1125           !transport->disconnected &&
1126           _dbus_connection_has_messages_to_send_unlocked (transport->connection))
1127         {
1128           do_writing (transport);
1129
1130           if (transport->disconnected ||
1131               !_dbus_connection_has_messages_to_send_unlocked (transport->connection))
1132             goto out;
1133         }
1134
1135       /* If we get here, we decided to do the poll() after all */
1136       _dbus_assert (socket_transport->read_watch);
1137       if (flags & DBUS_ITERATION_DO_READING)
1138         poll_fd.events |= _DBUS_POLLIN;
1139
1140       _dbus_assert (socket_transport->write_watch);
1141       if (flags & DBUS_ITERATION_DO_WRITING)
1142         poll_fd.events |= _DBUS_POLLOUT;
1143     }
1144   else
1145     {
1146       DBusAuthState auth_state;
1147       
1148       auth_state = _dbus_auth_do_work (transport->auth);
1149
1150       if (transport->receive_credentials_pending ||
1151           auth_state == DBUS_AUTH_STATE_WAITING_FOR_INPUT)
1152         poll_fd.events |= _DBUS_POLLIN;
1153
1154       if (transport->send_credentials_pending ||
1155           auth_state == DBUS_AUTH_STATE_HAVE_BYTES_TO_SEND)
1156         poll_fd.events |= _DBUS_POLLOUT;
1157     }
1158
1159   if (poll_fd.events)
1160     {
1161       int saved_errno;
1162
1163       if (flags & DBUS_ITERATION_BLOCK)
1164         poll_timeout = timeout_milliseconds;
1165       else
1166         poll_timeout = 0;
1167
1168       /* For blocking selects we drop the connection lock here
1169        * to avoid blocking out connection access during a potentially
1170        * indefinite blocking call. The io path is still protected
1171        * by the io_path_cond condvar, so we won't reenter this.
1172        */
1173       if (flags & DBUS_ITERATION_BLOCK)
1174         {
1175           _dbus_verbose ("unlock pre poll\n");
1176           _dbus_connection_unlock (transport->connection);
1177         }
1178       
1179     again:
1180       poll_res = _dbus_poll (&poll_fd, 1, poll_timeout);
1181       saved_errno = _dbus_save_socket_errno ();
1182
1183       if (poll_res < 0 && _dbus_get_is_errno_eintr (saved_errno))
1184         goto again;
1185
1186       if (flags & DBUS_ITERATION_BLOCK)
1187         {
1188           _dbus_verbose ("lock post poll\n");
1189           _dbus_connection_lock (transport->connection);
1190         }
1191       
1192       if (poll_res >= 0)
1193         {
1194           if (poll_res == 0)
1195             poll_fd.revents = 0; /* some concern that posix does not guarantee this;
1196                                   * valgrind flags it as an error. though it probably
1197                                   * is guaranteed on linux at least.
1198                                   */
1199           
1200           if (poll_fd.revents & _DBUS_POLLERR)
1201             do_io_error (transport);
1202           else
1203             {
1204               dbus_bool_t need_read = (poll_fd.revents & _DBUS_POLLIN) > 0;
1205               dbus_bool_t need_write = (poll_fd.revents & _DBUS_POLLOUT) > 0;
1206               dbus_bool_t authentication_completed;
1207
1208               _dbus_verbose ("in iteration, need_read=%d need_write=%d\n",
1209                              need_read, need_write);
1210               do_authentication (transport, need_read, need_write,
1211                                  &authentication_completed);
1212
1213               /* See comment in socket_handle_watch. */
1214               if (authentication_completed)
1215                 goto out;
1216                                  
1217               if (need_read && (flags & DBUS_ITERATION_DO_READING))
1218                 do_reading (transport);
1219               if (need_write && (flags & DBUS_ITERATION_DO_WRITING))
1220                 do_writing (transport);
1221             }
1222         }
1223       else
1224         {
1225           _dbus_verbose ("Error from _dbus_poll(): %s\n",
1226                          _dbus_strerror (saved_errno));
1227         }
1228     }
1229
1230
1231  out:
1232   /* We need to install the write watch only if we did not
1233    * successfully write everything. Note we need to be careful that we
1234    * don't call check_write_watch *before* do_writing, since it's
1235    * inefficient to add the write watch, and we can avoid it most of
1236    * the time since we can write immediately.
1237    * 
1238    * However, we MUST always call check_write_watch(); DBusConnection code
1239    * relies on the fact that running an iteration will notice that
1240    * messages are pending.
1241    */
1242   check_write_watch (transport);
1243
1244   _dbus_verbose (" ... leaving do_iteration()\n");
1245 }
1246
1247 static void
1248 socket_live_messages_changed (DBusTransport *transport)
1249 {
1250   /* See if we should look for incoming messages again */
1251   check_read_watch (transport);
1252 }
1253
1254
1255 static dbus_bool_t
1256 socket_get_socket_fd (DBusTransport *transport,
1257                       DBusSocket    *fd_p)
1258 {
1259   DBusTransportSocket *socket_transport = (DBusTransportSocket*) transport;
1260   
1261   *fd_p = socket_transport->fd;
1262   
1263   return TRUE;
1264 }
1265
1266 static const DBusTransportVTable socket_vtable = {
1267   socket_finalize,
1268   socket_handle_watch,
1269   socket_disconnect,
1270   socket_connection_set,
1271   socket_do_iteration,
1272   socket_live_messages_changed,
1273   socket_get_socket_fd
1274 };
1275
1276 /**
1277  * Creates a new transport for the given socket file descriptor.  The file
1278  * descriptor must be nonblocking (use _dbus_set_fd_nonblocking() to
1279  * make it so). This function is shared by various transports that
1280  * boil down to a full duplex file descriptor.
1281  *
1282  * @param fd the file descriptor.
1283  * @param server_guid non-#NULL if this transport is on the server side of a connection
1284  * @param address the transport's address
1285  * @returns the new transport, or #NULL if no memory.
1286  */
1287 DBusTransport*
1288 _dbus_transport_new_for_socket (DBusSocket        fd,
1289                                 const DBusString *server_guid,
1290                                 const DBusString *address)
1291 {
1292   DBusTransportSocket *socket_transport;
1293   
1294   socket_transport = dbus_new0 (DBusTransportSocket, 1);
1295   if (socket_transport == NULL)
1296     return NULL;
1297
1298   if (!_dbus_string_init (&socket_transport->encoded_outgoing))
1299     goto failed_0;
1300
1301   if (!_dbus_string_init (&socket_transport->encoded_incoming))
1302     goto failed_1;
1303   
1304   socket_transport->write_watch = _dbus_watch_new (_dbus_socket_get_pollable (fd),
1305                                                  DBUS_WATCH_WRITABLE,
1306                                                  FALSE,
1307                                                  NULL, NULL, NULL);
1308   if (socket_transport->write_watch == NULL)
1309     goto failed_2;
1310   
1311   socket_transport->read_watch = _dbus_watch_new (_dbus_socket_get_pollable (fd),
1312                                                 DBUS_WATCH_READABLE,
1313                                                 FALSE,
1314                                                 NULL, NULL, NULL);
1315   if (socket_transport->read_watch == NULL)
1316     goto failed_3;
1317
1318   if (!_dbus_transport_init_base (&socket_transport->base,
1319                                   &socket_vtable,
1320                                   server_guid, address))
1321     goto failed_4;
1322
1323 #ifdef HAVE_UNIX_FD_PASSING
1324   _dbus_auth_set_unix_fd_possible(socket_transport->base.auth, _dbus_socket_can_pass_unix_fd(fd));
1325 #endif
1326
1327   socket_transport->fd = fd;
1328   socket_transport->message_bytes_written = 0;
1329   
1330   /* These values should probably be tunable or something. */     
1331   socket_transport->max_bytes_read_per_iteration = 2048;
1332   socket_transport->max_bytes_written_per_iteration = 2048;
1333   
1334   return (DBusTransport*) socket_transport;
1335
1336  failed_4:
1337   _dbus_watch_invalidate (socket_transport->read_watch);
1338   _dbus_watch_unref (socket_transport->read_watch);
1339  failed_3:
1340   _dbus_watch_invalidate (socket_transport->write_watch);
1341   _dbus_watch_unref (socket_transport->write_watch);
1342  failed_2:
1343   _dbus_string_free (&socket_transport->encoded_incoming);
1344  failed_1:
1345   _dbus_string_free (&socket_transport->encoded_outgoing);
1346  failed_0:
1347   dbus_free (socket_transport);
1348   return NULL;
1349 }
1350
1351 /**
1352  * Creates a new transport for the given hostname and port.
1353  * If host is NULL, it will default to localhost
1354  *
1355  * @param host the host to connect to
1356  * @param port the port to connect to
1357  * @param family the address family to connect to
1358  * @param noncefile path to nonce file
1359  * @param error location to store reason for failure.
1360  * @returns a new transport, or #NULL on failure.
1361  */
1362 DBusTransport*
1363 _dbus_transport_new_for_tcp_socket (const char     *host,
1364                                     const char     *port,
1365                                     const char     *family,
1366                                     const char     *noncefile,
1367                                     DBusError      *error)
1368 {
1369   DBusSocket fd;
1370   DBusTransport *transport;
1371   DBusString address;
1372   
1373   _DBUS_ASSERT_ERROR_IS_CLEAR (error);
1374
1375   if (!_dbus_string_init (&address))
1376     {
1377       dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
1378       return NULL;
1379     }
1380
1381   if (host == NULL)
1382     host = "localhost";
1383
1384   if (!_dbus_string_append (&address, noncefile ? "nonce-tcp:" : "tcp:"))
1385     goto error;
1386
1387   if (!_dbus_string_append (&address, "host=") ||
1388       !_dbus_string_append (&address, host))
1389     goto error;
1390
1391   if (!_dbus_string_append (&address, ",port=") ||
1392       !_dbus_string_append (&address, port))
1393     goto error;
1394
1395   if (family != NULL &&
1396       (!_dbus_string_append (&address, ",family=") ||
1397        !_dbus_string_append (&address, family)))
1398     goto error;
1399
1400   if (noncefile != NULL &&
1401       (!_dbus_string_append (&address, ",noncefile=") ||
1402        !_dbus_string_append (&address, noncefile)))
1403     goto error;
1404
1405   fd = _dbus_connect_tcp_socket_with_nonce (host, port, family, noncefile, error);
1406   if (!_dbus_socket_is_valid (fd))
1407     {
1408       _DBUS_ASSERT_ERROR_IS_SET (error);
1409       _dbus_string_free (&address);
1410       return NULL;
1411     }
1412
1413   _dbus_verbose ("Successfully connected to tcp socket %s:%s\n",
1414                  host, port);
1415   
1416   transport = _dbus_transport_new_for_socket (fd, NULL, &address);
1417   _dbus_string_free (&address);
1418   if (transport == NULL)
1419     {
1420       dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
1421       _dbus_close_socket (fd, NULL);
1422       _dbus_socket_invalidate (&fd);
1423     }
1424
1425   return transport;
1426
1427 error:
1428   _dbus_string_free (&address);
1429   dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
1430   return NULL;
1431 }
1432
1433 /**
1434  * Opens a TCP socket transport.
1435  * 
1436  * @param entry the address entry to try opening as a tcp transport.
1437  * @param transport_p return location for the opened transport
1438  * @param error error to be set
1439  * @returns result of the attempt
1440  */
1441 DBusTransportOpenResult
1442 _dbus_transport_open_socket(DBusAddressEntry  *entry,
1443                             DBusTransport    **transport_p,                            
1444                             DBusError         *error)
1445 {
1446   const char *method;
1447   dbus_bool_t isTcp;
1448   dbus_bool_t isNonceTcp;
1449   
1450   method = dbus_address_entry_get_method (entry);
1451   _dbus_assert (method != NULL);
1452
1453   isTcp = strcmp (method, "tcp") == 0;
1454   isNonceTcp = strcmp (method, "nonce-tcp") == 0;
1455
1456   if (isTcp || isNonceTcp)
1457     {
1458       const char *host = dbus_address_entry_get_value (entry, "host");
1459       const char *port = dbus_address_entry_get_value (entry, "port");
1460       const char *family = dbus_address_entry_get_value (entry, "family");
1461       const char *noncefile = dbus_address_entry_get_value (entry, "noncefile");
1462
1463       if ((isNonceTcp == TRUE) != (noncefile != NULL)) {
1464           _dbus_set_bad_address (error, method, "noncefile", NULL);
1465           return DBUS_TRANSPORT_OPEN_BAD_ADDRESS;
1466       }
1467
1468       if (port == NULL)
1469         {
1470           _dbus_set_bad_address (error, method, "port", NULL);
1471           return DBUS_TRANSPORT_OPEN_BAD_ADDRESS;
1472         }
1473
1474       *transport_p = _dbus_transport_new_for_tcp_socket (host, port, family, noncefile, error);
1475       if (*transport_p == NULL)
1476         {
1477           _DBUS_ASSERT_ERROR_IS_SET (error);
1478           return DBUS_TRANSPORT_OPEN_DID_NOT_CONNECT;
1479         }
1480       else
1481         {
1482           _DBUS_ASSERT_ERROR_IS_CLEAR (error);
1483           return DBUS_TRANSPORT_OPEN_OK;
1484         }
1485     }
1486   else
1487     {
1488       _DBUS_ASSERT_ERROR_IS_CLEAR (error);
1489       return DBUS_TRANSPORT_OPEN_NOT_HANDLED;
1490     }
1491 }
1492
1493 /** @} */
1494