D-Bus  1.6.18
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 
45 
50 {
52  int fd;
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)
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)
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_get_is_authenticated (transport))
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 = %d outgoing messages exist %d\n",
163  needed, transport->connection, socket_transport->write_watch,
164  socket_transport->fd,
166 
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 = %d\n",socket_transport->fd);
181 
182  if (transport->connection == NULL)
183  return;
184 
185  if (transport->disconnected)
186  {
187  _dbus_assert (socket_transport->read_watch == NULL);
188  return;
189  }
190 
191  _dbus_transport_ref (transport);
192 
193  if (_dbus_transport_get_is_authenticated (transport))
194  need_read_watch =
197  else
198  {
199  if (transport->receive_credentials_pending)
200  need_read_watch = TRUE;
201  else
202  {
203  /* The reason to disable need_read_watch when not WAITING_FOR_INPUT
204  * is to avoid spinning on the file descriptor when we're waiting
205  * to write or for some other part of the auth process
206  */
207  DBusAuthState auth_state;
208 
209  auth_state = _dbus_auth_do_work (transport->auth);
210 
211  /* If we need memory we install the read watch just in case,
212  * if there's no need for it, it will get de-installed
213  * next time we try reading. If we're authenticated we
214  * install it since we normally have it installed while
215  * authenticated.
216  */
217  if (auth_state == DBUS_AUTH_STATE_WAITING_FOR_INPUT ||
218  auth_state == DBUS_AUTH_STATE_WAITING_FOR_MEMORY ||
219  auth_state == DBUS_AUTH_STATE_AUTHENTICATED)
220  need_read_watch = TRUE;
221  else
222  need_read_watch = FALSE;
223  }
224  }
225 
226  _dbus_verbose (" setting read watch enabled = %d\n", need_read_watch);
228  socket_transport->read_watch,
229  need_read_watch);
230 
231  _dbus_transport_unref (transport);
232 }
233 
234 static void
235 do_io_error (DBusTransport *transport)
236 {
237  _dbus_transport_ref (transport);
238  _dbus_transport_disconnect (transport);
239  _dbus_transport_unref (transport);
240 }
241 
242 /* return value is whether we successfully read any new data. */
243 static dbus_bool_t
244 read_data_into_auth (DBusTransport *transport,
245  dbus_bool_t *oom)
246 {
247  DBusTransportSocket *socket_transport = (DBusTransportSocket*) transport;
248  DBusString *buffer;
249  int bytes_read;
250 
251  *oom = FALSE;
252 
253  _dbus_auth_get_buffer (transport->auth, &buffer);
254 
255  bytes_read = _dbus_read_socket (socket_transport->fd,
256  buffer, socket_transport->max_bytes_read_per_iteration);
257 
258  _dbus_auth_return_buffer (transport->auth, buffer,
259  bytes_read > 0 ? bytes_read : 0);
260 
261  if (bytes_read > 0)
262  {
263  _dbus_verbose (" read %d bytes in auth phase\n", bytes_read);
264 
265  return TRUE;
266  }
267  else if (bytes_read < 0)
268  {
269  /* EINTR already handled for us */
270 
272  {
273  *oom = TRUE;
274  }
276  ; /* do nothing, just return FALSE below */
277  else
278  {
279  _dbus_verbose ("Error reading from remote app: %s\n",
281  do_io_error (transport);
282  }
283 
284  return FALSE;
285  }
286  else
287  {
288  _dbus_assert (bytes_read == 0);
289 
290  _dbus_verbose ("Disconnected from remote app\n");
291  do_io_error (transport);
292 
293  return FALSE;
294  }
295 }
296 
297 /* Return value is whether we successfully wrote any bytes */
298 static dbus_bool_t
299 write_data_from_auth (DBusTransport *transport)
300 {
301  DBusTransportSocket *socket_transport = (DBusTransportSocket*) transport;
302  int bytes_written;
303  const DBusString *buffer;
304 
305  if (!_dbus_auth_get_bytes_to_send (transport->auth,
306  &buffer))
307  return FALSE;
308 
309  bytes_written = _dbus_write_socket (socket_transport->fd,
310  buffer,
311  0, _dbus_string_get_length (buffer));
312 
313  if (bytes_written > 0)
314  {
315  _dbus_auth_bytes_sent (transport->auth, bytes_written);
316  return TRUE;
317  }
318  else if (bytes_written < 0)
319  {
320  /* EINTR already handled for us */
321 
323  ;
324  else
325  {
326  _dbus_verbose ("Error writing to remote app: %s\n",
328  do_io_error (transport);
329  }
330  }
331 
332  return FALSE;
333 }
334 
335 /* FALSE on OOM */
336 static dbus_bool_t
337 exchange_credentials (DBusTransport *transport,
338  dbus_bool_t do_reading,
339  dbus_bool_t do_writing)
340 {
341  DBusTransportSocket *socket_transport = (DBusTransportSocket*) transport;
342  DBusError error = DBUS_ERROR_INIT;
343 
344  _dbus_verbose ("exchange_credentials: do_reading = %d, do_writing = %d\n",
345  do_reading, do_writing);
346 
347  if (do_writing && transport->send_credentials_pending)
348  {
349  if (_dbus_send_credentials_socket (socket_transport->fd,
350  &error))
351  {
352  transport->send_credentials_pending = FALSE;
353  }
354  else
355  {
356  _dbus_verbose ("Failed to write credentials: %s\n", error.message);
357  dbus_error_free (&error);
358  do_io_error (transport);
359  }
360  }
361 
362  if (do_reading && transport->receive_credentials_pending)
363  {
364  /* FIXME this can fail due to IO error _or_ OOM, broken
365  * (somewhat tricky to fix since the OOM error can be set after
366  * we already read the credentials byte, so basically we need to
367  * separate reading the byte and storing it in the
368  * transport->credentials). Does not really matter for now
369  * because storing in credentials never actually fails on unix.
370  */
371  if (_dbus_read_credentials_socket (socket_transport->fd,
372  transport->credentials,
373  &error))
374  {
375  transport->receive_credentials_pending = FALSE;
376  }
377  else
378  {
379  _dbus_verbose ("Failed to read credentials %s\n", error.message);
380  dbus_error_free (&error);
381  do_io_error (transport);
382  }
383  }
384 
385  if (!(transport->send_credentials_pending ||
386  transport->receive_credentials_pending))
387  {
388  if (!_dbus_auth_set_credentials (transport->auth,
389  transport->credentials))
390  return FALSE;
391  }
392 
393  return TRUE;
394 }
395 
396 static dbus_bool_t
397 do_authentication (DBusTransport *transport,
398  dbus_bool_t do_reading,
399  dbus_bool_t do_writing,
400  dbus_bool_t *auth_completed)
401 {
402  dbus_bool_t oom;
403  dbus_bool_t orig_auth_state;
404 
405  oom = FALSE;
406 
407  orig_auth_state = _dbus_transport_get_is_authenticated (transport);
408 
409  /* This is essential to avoid the check_write_watch() at the end,
410  * we don't want to add a write watch in do_iteration before
411  * we try writing and get EAGAIN
412  */
413  if (orig_auth_state)
414  {
415  if (auth_completed)
416  *auth_completed = FALSE;
417  return TRUE;
418  }
419 
420  _dbus_transport_ref (transport);
421 
422  while (!_dbus_transport_get_is_authenticated (transport) &&
424  {
425  if (!exchange_credentials (transport, do_reading, do_writing))
426  {
427  /* OOM */
428  oom = TRUE;
429  goto out;
430  }
431 
432  if (transport->send_credentials_pending ||
433  transport->receive_credentials_pending)
434  {
435  _dbus_verbose ("send_credentials_pending = %d receive_credentials_pending = %d\n",
436  transport->send_credentials_pending,
437  transport->receive_credentials_pending);
438  goto out;
439  }
440 
441 #define TRANSPORT_SIDE(t) ((t)->is_server ? "server" : "client")
442  switch (_dbus_auth_do_work (transport->auth))
443  {
444  case DBUS_AUTH_STATE_WAITING_FOR_INPUT:
445  _dbus_verbose (" %s auth state: waiting for input\n",
446  TRANSPORT_SIDE (transport));
447  if (!do_reading || !read_data_into_auth (transport, &oom))
448  goto out;
449  break;
450 
451  case DBUS_AUTH_STATE_WAITING_FOR_MEMORY:
452  _dbus_verbose (" %s auth state: waiting for memory\n",
453  TRANSPORT_SIDE (transport));
454  oom = TRUE;
455  goto out;
456  break;
457 
458  case DBUS_AUTH_STATE_HAVE_BYTES_TO_SEND:
459  _dbus_verbose (" %s auth state: bytes to send\n",
460  TRANSPORT_SIDE (transport));
461  if (!do_writing || !write_data_from_auth (transport))
462  goto out;
463  break;
464 
465  case DBUS_AUTH_STATE_NEED_DISCONNECT:
466  _dbus_verbose (" %s auth state: need to disconnect\n",
467  TRANSPORT_SIDE (transport));
468  do_io_error (transport);
469  break;
470 
471  case DBUS_AUTH_STATE_AUTHENTICATED:
472  _dbus_verbose (" %s auth state: authenticated\n",
473  TRANSPORT_SIDE (transport));
474  break;
475  }
476  }
477 
478  out:
479  if (auth_completed)
480  *auth_completed = (orig_auth_state != _dbus_transport_get_is_authenticated (transport));
481 
482  check_read_watch (transport);
483  check_write_watch (transport);
484  _dbus_transport_unref (transport);
485 
486  if (oom)
487  return FALSE;
488  else
489  return TRUE;
490 }
491 
492 /* returns false on oom */
493 static dbus_bool_t
494 do_writing (DBusTransport *transport)
495 {
496  int total;
497  DBusTransportSocket *socket_transport = (DBusTransportSocket*) transport;
498  dbus_bool_t oom;
499 
500  /* No messages without authentication! */
501  if (!_dbus_transport_get_is_authenticated (transport))
502  {
503  _dbus_verbose ("Not authenticated, not writing anything\n");
504  return TRUE;
505  }
506 
507  if (transport->disconnected)
508  {
509  _dbus_verbose ("Not connected, not writing anything\n");
510  return TRUE;
511  }
512 
513 #if 1
514  _dbus_verbose ("do_writing(), have_messages = %d, fd = %d\n",
516  socket_transport->fd);
517 #endif
518 
519  oom = FALSE;
520  total = 0;
521 
522  while (!transport->disconnected &&
524  {
525  int bytes_written;
526  DBusMessage *message;
527  const DBusString *header;
528  const DBusString *body;
529  int header_len, body_len;
530  int total_bytes_to_write;
531 
532  if (total > socket_transport->max_bytes_written_per_iteration)
533  {
534  _dbus_verbose ("%d bytes exceeds %d bytes written per iteration, returning\n",
535  total, socket_transport->max_bytes_written_per_iteration);
536  goto out;
537  }
538 
539  message = _dbus_connection_get_message_to_send (transport->connection);
540  _dbus_assert (message != NULL);
541  dbus_message_lock (message);
542 
543 #if 0
544  _dbus_verbose ("writing message %p\n", message);
545 #endif
546 
548  &header, &body);
549 
550  header_len = _dbus_string_get_length (header);
551  body_len = _dbus_string_get_length (body);
552 
553  if (_dbus_auth_needs_encoding (transport->auth))
554  {
555  /* Does fd passing even make sense with encoded data? */
556  _dbus_assert(!DBUS_TRANSPORT_CAN_SEND_UNIX_FD(transport));
557 
558  if (_dbus_string_get_length (&socket_transport->encoded_outgoing) == 0)
559  {
560  if (!_dbus_auth_encode_data (transport->auth,
561  header, &socket_transport->encoded_outgoing))
562  {
563  oom = TRUE;
564  goto out;
565  }
566 
567  if (!_dbus_auth_encode_data (transport->auth,
568  body, &socket_transport->encoded_outgoing))
569  {
570  _dbus_string_set_length (&socket_transport->encoded_outgoing, 0);
571  oom = TRUE;
572  goto out;
573  }
574  }
575 
576  total_bytes_to_write = _dbus_string_get_length (&socket_transport->encoded_outgoing);
577 
578 #if 0
579  _dbus_verbose ("encoded message is %d bytes\n",
580  total_bytes_to_write);
581 #endif
582 
583  bytes_written =
584  _dbus_write_socket (socket_transport->fd,
585  &socket_transport->encoded_outgoing,
586  socket_transport->message_bytes_written,
587  total_bytes_to_write - socket_transport->message_bytes_written);
588  }
589  else
590  {
591  total_bytes_to_write = header_len + body_len;
592 
593 #if 0
594  _dbus_verbose ("message is %d bytes\n",
595  total_bytes_to_write);
596 #endif
597 
598 #ifdef HAVE_UNIX_FD_PASSING
599  if (socket_transport->message_bytes_written <= 0 && DBUS_TRANSPORT_CAN_SEND_UNIX_FD(transport))
600  {
601  /* Send the fds along with the first byte of the message */
602  const int *unix_fds;
603  unsigned n;
604 
605  _dbus_message_get_unix_fds(message, &unix_fds, &n);
606 
607  bytes_written =
608  _dbus_write_socket_with_unix_fds_two (socket_transport->fd,
609  header,
610  socket_transport->message_bytes_written,
611  header_len - socket_transport->message_bytes_written,
612  body,
613  0, body_len,
614  unix_fds,
615  n);
616 
617  if (bytes_written > 0 && n > 0)
618  _dbus_verbose("Wrote %i unix fds\n", n);
619  }
620  else
621 #endif
622  {
623  if (socket_transport->message_bytes_written < header_len)
624  {
625  bytes_written =
626  _dbus_write_socket_two (socket_transport->fd,
627  header,
628  socket_transport->message_bytes_written,
629  header_len - socket_transport->message_bytes_written,
630  body,
631  0, body_len);
632  }
633  else
634  {
635  bytes_written =
636  _dbus_write_socket (socket_transport->fd,
637  body,
638  (socket_transport->message_bytes_written - header_len),
639  body_len -
640  (socket_transport->message_bytes_written - header_len));
641  }
642  }
643  }
644 
645  if (bytes_written < 0)
646  {
647  /* EINTR already handled for us */
648 
649  /* If the other end closed the socket with close() or shutdown(), we
650  * receive EPIPE here but we must not close the socket yet: there
651  * might still be some data to read. See:
652  * http://lists.freedesktop.org/archives/dbus/2008-March/009526.html
653  */
654 
656  goto out;
657 
658  /* Since Linux commit 25888e (from 2.6.37-rc4, Nov 2010), sendmsg()
659  * on Unix sockets returns -1 errno=ETOOMANYREFS when the passfd
660  * mechanism (SCM_RIGHTS) is used recursively with a recursion level
661  * of maximum 4. The kernel does not have an API to check whether
662  * the passed fds can be forwarded and it can change asynchronously.
663  * See:
664  * https://bugs.freedesktop.org/show_bug.cgi?id=80163
665  */
666 
668  {
669  /* We only send fds in the first byte of the message.
670  * ETOOMANYREFS cannot happen after.
671  */
672  _dbus_assert (socket_transport->message_bytes_written == 0);
673 
674  _dbus_verbose (" discard message of %d bytes due to ETOOMANYREFS\n",
675  total_bytes_to_write);
676 
677  socket_transport->message_bytes_written = 0;
678  _dbus_string_set_length (&socket_transport->encoded_outgoing, 0);
679  _dbus_string_compact (&socket_transport->encoded_outgoing, 2048);
680 
681  /* The message was not actually sent but it needs to be removed
682  * from the outgoing queue
683  */
685  message);
686  }
687  else
688  {
689  _dbus_verbose ("Error writing to remote app: %s\n",
691  do_io_error (transport);
692  goto out;
693  }
694  }
695  else
696  {
697  _dbus_verbose (" wrote %d bytes of %d\n", bytes_written,
698  total_bytes_to_write);
699 
700  total += bytes_written;
701  socket_transport->message_bytes_written += bytes_written;
702 
703  _dbus_assert (socket_transport->message_bytes_written <=
704  total_bytes_to_write);
705 
706  if (socket_transport->message_bytes_written == total_bytes_to_write)
707  {
708  socket_transport->message_bytes_written = 0;
709  _dbus_string_set_length (&socket_transport->encoded_outgoing, 0);
710  _dbus_string_compact (&socket_transport->encoded_outgoing, 2048);
711 
713  message);
714  }
715  }
716  }
717 
718  out:
719  if (oom)
720  return FALSE;
721  else
722  return TRUE;
723 }
724 
725 /* returns false on out-of-memory */
726 static dbus_bool_t
727 do_reading (DBusTransport *transport)
728 {
729  DBusTransportSocket *socket_transport = (DBusTransportSocket*) transport;
730  DBusString *buffer;
731  int bytes_read;
732  int total;
733  dbus_bool_t oom;
734 
735  _dbus_verbose ("fd = %d\n",socket_transport->fd);
736 
737  /* No messages without authentication! */
738  if (!_dbus_transport_get_is_authenticated (transport))
739  return TRUE;
740 
741  oom = FALSE;
742 
743  total = 0;
744 
745  again:
746 
747  /* See if we've exceeded max messages and need to disable reading */
748  check_read_watch (transport);
749 
750  if (total > socket_transport->max_bytes_read_per_iteration)
751  {
752  _dbus_verbose ("%d bytes exceeds %d bytes read per iteration, returning\n",
753  total, socket_transport->max_bytes_read_per_iteration);
754  goto out;
755  }
756 
757  _dbus_assert (socket_transport->read_watch != NULL ||
758  transport->disconnected);
759 
760  if (transport->disconnected)
761  goto out;
762 
763  if (!dbus_watch_get_enabled (socket_transport->read_watch))
764  return TRUE;
765 
766  if (_dbus_auth_needs_decoding (transport->auth))
767  {
768  /* Does fd passing even make sense with encoded data? */
769  _dbus_assert(!DBUS_TRANSPORT_CAN_SEND_UNIX_FD(transport));
770 
771  if (_dbus_string_get_length (&socket_transport->encoded_incoming) > 0)
772  bytes_read = _dbus_string_get_length (&socket_transport->encoded_incoming);
773  else
774  bytes_read = _dbus_read_socket (socket_transport->fd,
775  &socket_transport->encoded_incoming,
776  socket_transport->max_bytes_read_per_iteration);
777 
778  _dbus_assert (_dbus_string_get_length (&socket_transport->encoded_incoming) ==
779  bytes_read);
780 
781  if (bytes_read > 0)
782  {
783  int orig_len;
784 
786  &buffer);
787 
788  orig_len = _dbus_string_get_length (buffer);
789 
790  if (!_dbus_auth_decode_data (transport->auth,
791  &socket_transport->encoded_incoming,
792  buffer))
793  {
794  _dbus_verbose ("Out of memory decoding incoming data\n");
796  buffer,
797  _dbus_string_get_length (buffer) - orig_len);
798 
799  oom = TRUE;
800  goto out;
801  }
802 
804  buffer,
805  _dbus_string_get_length (buffer) - orig_len);
806 
807  _dbus_string_set_length (&socket_transport->encoded_incoming, 0);
808  _dbus_string_compact (&socket_transport->encoded_incoming, 2048);
809  }
810  }
811  else
812  {
814  &buffer);
815 
816 #ifdef HAVE_UNIX_FD_PASSING
817  if (DBUS_TRANSPORT_CAN_SEND_UNIX_FD(transport))
818  {
819  int *fds, n_fds;
820 
821  if (!_dbus_message_loader_get_unix_fds(transport->loader, &fds, &n_fds))
822  {
823  _dbus_verbose ("Out of memory reading file descriptors\n");
824  _dbus_message_loader_return_buffer (transport->loader, buffer, 0);
825  oom = TRUE;
826  goto out;
827  }
828 
829  bytes_read = _dbus_read_socket_with_unix_fds(socket_transport->fd,
830  buffer,
831  socket_transport->max_bytes_read_per_iteration,
832  fds, &n_fds);
833 
834  if (bytes_read >= 0 && n_fds > 0)
835  _dbus_verbose("Read %i unix fds\n", n_fds);
836 
837  _dbus_message_loader_return_unix_fds(transport->loader, fds, bytes_read < 0 ? 0 : n_fds);
838  }
839  else
840 #endif
841  {
842  bytes_read = _dbus_read_socket (socket_transport->fd,
843  buffer, socket_transport->max_bytes_read_per_iteration);
844  }
845 
847  buffer,
848  bytes_read < 0 ? 0 : bytes_read);
849  }
850 
851  if (bytes_read < 0)
852  {
853  /* EINTR already handled for us */
854 
856  {
857  _dbus_verbose ("Out of memory in read()/do_reading()\n");
858  oom = TRUE;
859  goto out;
860  }
862  goto out;
863  else
864  {
865  _dbus_verbose ("Error reading from remote app: %s\n",
867  do_io_error (transport);
868  goto out;
869  }
870  }
871  else if (bytes_read == 0)
872  {
873  _dbus_verbose ("Disconnected from remote app\n");
874  do_io_error (transport);
875  goto out;
876  }
877  else
878  {
879  _dbus_verbose (" read %d bytes\n", bytes_read);
880 
881  total += bytes_read;
882 
883  if (!_dbus_transport_queue_messages (transport))
884  {
885  oom = TRUE;
886  _dbus_verbose (" out of memory when queueing messages we just read in the transport\n");
887  goto out;
888  }
889 
890  /* Try reading more data until we get EAGAIN and return, or
891  * exceed max bytes per iteration. If in blocking mode of
892  * course we'll block instead of returning.
893  */
894  goto again;
895  }
896 
897  out:
898  if (oom)
899  return FALSE;
900  else
901  return TRUE;
902 }
903 
904 static dbus_bool_t
905 unix_error_with_read_to_come (DBusTransport *itransport,
906  DBusWatch *watch,
907  unsigned int flags)
908 {
909  DBusTransportSocket *transport = (DBusTransportSocket *) itransport;
910 
911  if (!(flags & DBUS_WATCH_HANGUP || flags & DBUS_WATCH_ERROR))
912  return FALSE;
913 
914  /* If we have a read watch enabled ...
915  we -might have data incoming ... => handle the HANGUP there */
916  if (watch != transport->read_watch &&
917  _dbus_watch_get_enabled (transport->read_watch))
918  return FALSE;
919 
920  return TRUE;
921 }
922 
923 static dbus_bool_t
924 socket_handle_watch (DBusTransport *transport,
925  DBusWatch *watch,
926  unsigned int flags)
927 {
928  DBusTransportSocket *socket_transport = (DBusTransportSocket*) transport;
929 
930  _dbus_assert (watch == socket_transport->read_watch ||
931  watch == socket_transport->write_watch);
932  _dbus_assert (watch != NULL);
933 
934  /* If we hit an error here on a write watch, don't disconnect the transport yet because data can
935  * still be in the buffer and do_reading may need several iteration to read
936  * it all (because of its max_bytes_read_per_iteration limit).
937  */
938  if (!(flags & DBUS_WATCH_READABLE) && unix_error_with_read_to_come (transport, watch, flags))
939  {
940  _dbus_verbose ("Hang up or error on watch\n");
941  _dbus_transport_disconnect (transport);
942  return TRUE;
943  }
944 
945  if (watch == socket_transport->read_watch &&
946  (flags & DBUS_WATCH_READABLE))
947  {
948  dbus_bool_t auth_finished;
949 #if 1
950  _dbus_verbose ("handling read watch %p flags = %x\n",
951  watch, flags);
952 #endif
953  if (!do_authentication (transport, TRUE, FALSE, &auth_finished))
954  return FALSE;
955 
956  /* We don't want to do a read immediately following
957  * a successful authentication. This is so we
958  * have a chance to propagate the authentication
959  * state further up. Specifically, we need to
960  * process any pending data from the auth object.
961  */
962  if (!auth_finished)
963  {
964  if (!do_reading (transport))
965  {
966  _dbus_verbose ("no memory to read\n");
967  return FALSE;
968  }
969  }
970  else
971  {
972  _dbus_verbose ("Not reading anything since we just completed the authentication\n");
973  }
974  }
975  else if (watch == socket_transport->write_watch &&
976  (flags & DBUS_WATCH_WRITABLE))
977  {
978 #if 1
979  _dbus_verbose ("handling write watch, have_outgoing_messages = %d\n",
981 #endif
982  if (!do_authentication (transport, FALSE, TRUE, NULL))
983  return FALSE;
984 
985  if (!do_writing (transport))
986  {
987  _dbus_verbose ("no memory to write\n");
988  return FALSE;
989  }
990 
991  /* See if we still need the write watch */
992  check_write_watch (transport);
993  }
994 #ifdef DBUS_ENABLE_VERBOSE_MODE
995  else
996  {
997  if (watch == socket_transport->read_watch)
998  _dbus_verbose ("asked to handle read watch with non-read condition 0x%x\n",
999  flags);
1000  else if (watch == socket_transport->write_watch)
1001  _dbus_verbose ("asked to handle write watch with non-write condition 0x%x\n",
1002  flags);
1003  else
1004  _dbus_verbose ("asked to handle watch %p on fd %d that we don't recognize\n",
1005  watch, dbus_watch_get_socket (watch));
1006  }
1007 #endif /* DBUS_ENABLE_VERBOSE_MODE */
1008 
1009  return TRUE;
1010 }
1011 
1012 static void
1013 socket_disconnect (DBusTransport *transport)
1014 {
1015  DBusTransportSocket *socket_transport = (DBusTransportSocket*) transport;
1016 
1017  _dbus_verbose ("\n");
1018 
1019  free_watches (transport);
1020 
1021  _dbus_close_socket (socket_transport->fd, NULL);
1022  socket_transport->fd = -1;
1023 }
1024 
1025 static dbus_bool_t
1026 socket_connection_set (DBusTransport *transport)
1027 {
1028  DBusTransportSocket *socket_transport = (DBusTransportSocket*) transport;
1029 
1030  _dbus_watch_set_handler (socket_transport->write_watch,
1032  transport->connection, NULL);
1033 
1034  _dbus_watch_set_handler (socket_transport->read_watch,
1036  transport->connection, NULL);
1037 
1039  socket_transport->write_watch))
1040  return FALSE;
1041 
1043  socket_transport->read_watch))
1044  {
1046  socket_transport->write_watch);
1047  return FALSE;
1048  }
1049 
1050  check_read_watch (transport);
1051  check_write_watch (transport);
1052 
1053  return TRUE;
1054 }
1055 
1063 static void
1064 socket_do_iteration (DBusTransport *transport,
1065  unsigned int flags,
1066  int timeout_milliseconds)
1067 {
1068  DBusTransportSocket *socket_transport = (DBusTransportSocket*) transport;
1069  DBusPollFD poll_fd;
1070  int poll_res;
1071  int poll_timeout;
1072 
1073  _dbus_verbose (" iteration flags = %s%s timeout = %d read_watch = %p write_watch = %p fd = %d\n",
1074  flags & DBUS_ITERATION_DO_READING ? "read" : "",
1075  flags & DBUS_ITERATION_DO_WRITING ? "write" : "",
1076  timeout_milliseconds,
1077  socket_transport->read_watch,
1078  socket_transport->write_watch,
1079  socket_transport->fd);
1080 
1081  /* the passed in DO_READING/DO_WRITING flags indicate whether to
1082  * read/write messages, but regardless of those we may need to block
1083  * for reading/writing to do auth. But if we do reading for auth,
1084  * we don't want to read any messages yet if not given DO_READING.
1085  */
1086 
1087  poll_fd.fd = socket_transport->fd;
1088  poll_fd.events = 0;
1089 
1090  if (_dbus_transport_get_is_authenticated (transport))
1091  {
1092  /* This is kind of a hack; if we have stuff to write, then try
1093  * to avoid the poll. This is probably about a 5% speedup on an
1094  * echo client/server.
1095  *
1096  * If both reading and writing were requested, we want to avoid this
1097  * since it could have funky effects:
1098  * - both ends spinning waiting for the other one to read
1099  * data so they can finish writing
1100  * - prioritizing all writing ahead of reading
1101  */
1102  if ((flags & DBUS_ITERATION_DO_WRITING) &&
1103  !(flags & (DBUS_ITERATION_DO_READING | DBUS_ITERATION_BLOCK)) &&
1104  !transport->disconnected &&
1106  {
1107  do_writing (transport);
1108 
1109  if (transport->disconnected ||
1111  goto out;
1112  }
1113 
1114  /* If we get here, we decided to do the poll() after all */
1115  _dbus_assert (socket_transport->read_watch);
1116  if (flags & DBUS_ITERATION_DO_READING)
1117  poll_fd.events |= _DBUS_POLLIN;
1118 
1119  _dbus_assert (socket_transport->write_watch);
1120  if (flags & DBUS_ITERATION_DO_WRITING)
1121  poll_fd.events |= _DBUS_POLLOUT;
1122  }
1123  else
1124  {
1125  DBusAuthState auth_state;
1126 
1127  auth_state = _dbus_auth_do_work (transport->auth);
1128 
1129  if (transport->receive_credentials_pending ||
1130  auth_state == DBUS_AUTH_STATE_WAITING_FOR_INPUT)
1131  poll_fd.events |= _DBUS_POLLIN;
1132 
1133  if (transport->send_credentials_pending ||
1134  auth_state == DBUS_AUTH_STATE_HAVE_BYTES_TO_SEND)
1135  poll_fd.events |= _DBUS_POLLOUT;
1136  }
1137 
1138  if (poll_fd.events)
1139  {
1140  if (flags & DBUS_ITERATION_BLOCK)
1141  poll_timeout = timeout_milliseconds;
1142  else
1143  poll_timeout = 0;
1144 
1145  /* For blocking selects we drop the connection lock here
1146  * to avoid blocking out connection access during a potentially
1147  * indefinite blocking call. The io path is still protected
1148  * by the io_path_cond condvar, so we won't reenter this.
1149  */
1150  if (flags & DBUS_ITERATION_BLOCK)
1151  {
1152  _dbus_verbose ("unlock pre poll\n");
1153  _dbus_connection_unlock (transport->connection);
1154  }
1155 
1156  again:
1157  poll_res = _dbus_poll (&poll_fd, 1, poll_timeout);
1158 
1159  if (poll_res < 0 && _dbus_get_is_errno_eintr ())
1160  goto again;
1161 
1162  if (flags & DBUS_ITERATION_BLOCK)
1163  {
1164  _dbus_verbose ("lock post poll\n");
1165  _dbus_connection_lock (transport->connection);
1166  }
1167 
1168  if (poll_res >= 0)
1169  {
1170  if (poll_res == 0)
1171  poll_fd.revents = 0; /* some concern that posix does not guarantee this;
1172  * valgrind flags it as an error. though it probably
1173  * is guaranteed on linux at least.
1174  */
1175 
1176  if (poll_fd.revents & _DBUS_POLLERR)
1177  do_io_error (transport);
1178  else
1179  {
1180  dbus_bool_t need_read = (poll_fd.revents & _DBUS_POLLIN) > 0;
1181  dbus_bool_t need_write = (poll_fd.revents & _DBUS_POLLOUT) > 0;
1182  dbus_bool_t authentication_completed;
1183 
1184  _dbus_verbose ("in iteration, need_read=%d need_write=%d\n",
1185  need_read, need_write);
1186  do_authentication (transport, need_read, need_write,
1187  &authentication_completed);
1188 
1189  /* See comment in socket_handle_watch. */
1190  if (authentication_completed)
1191  goto out;
1192 
1193  if (need_read && (flags & DBUS_ITERATION_DO_READING))
1194  do_reading (transport);
1195  if (need_write && (flags & DBUS_ITERATION_DO_WRITING))
1196  do_writing (transport);
1197  }
1198  }
1199  else
1200  {
1201  _dbus_verbose ("Error from _dbus_poll(): %s\n",
1203  }
1204  }
1205 
1206 
1207  out:
1208  /* We need to install the write watch only if we did not
1209  * successfully write everything. Note we need to be careful that we
1210  * don't call check_write_watch *before* do_writing, since it's
1211  * inefficient to add the write watch, and we can avoid it most of
1212  * the time since we can write immediately.
1213  *
1214  * However, we MUST always call check_write_watch(); DBusConnection code
1215  * relies on the fact that running an iteration will notice that
1216  * messages are pending.
1217  */
1218  check_write_watch (transport);
1219 
1220  _dbus_verbose (" ... leaving do_iteration()\n");
1221 }
1222 
1223 static void
1224 socket_live_messages_changed (DBusTransport *transport)
1225 {
1226  /* See if we should look for incoming messages again */
1227  check_read_watch (transport);
1228 }
1229 
1230 
1231 static dbus_bool_t
1232 socket_get_socket_fd (DBusTransport *transport,
1233  int *fd_p)
1234 {
1235  DBusTransportSocket *socket_transport = (DBusTransportSocket*) transport;
1236 
1237  *fd_p = socket_transport->fd;
1238 
1239  return TRUE;
1240 }
1241 
1242 static const DBusTransportVTable socket_vtable = {
1243  socket_finalize,
1244  socket_handle_watch,
1245  socket_disconnect,
1246  socket_connection_set,
1247  socket_do_iteration,
1248  socket_live_messages_changed,
1249  socket_get_socket_fd
1250 };
1251 
1265  const DBusString *server_guid,
1266  const DBusString *address)
1267 {
1268  DBusTransportSocket *socket_transport;
1269 
1270  socket_transport = dbus_new0 (DBusTransportSocket, 1);
1271  if (socket_transport == NULL)
1272  return NULL;
1273 
1274  if (!_dbus_string_init (&socket_transport->encoded_outgoing))
1275  goto failed_0;
1276 
1277  if (!_dbus_string_init (&socket_transport->encoded_incoming))
1278  goto failed_1;
1279 
1280  socket_transport->write_watch = _dbus_watch_new (fd,
1281  DBUS_WATCH_WRITABLE,
1282  FALSE,
1283  NULL, NULL, NULL);
1284  if (socket_transport->write_watch == NULL)
1285  goto failed_2;
1286 
1287  socket_transport->read_watch = _dbus_watch_new (fd,
1288  DBUS_WATCH_READABLE,
1289  FALSE,
1290  NULL, NULL, NULL);
1291  if (socket_transport->read_watch == NULL)
1292  goto failed_3;
1293 
1294  if (!_dbus_transport_init_base (&socket_transport->base,
1295  &socket_vtable,
1296  server_guid, address))
1297  goto failed_4;
1298 
1299 #ifdef HAVE_UNIX_FD_PASSING
1301 #endif
1302 
1303  socket_transport->fd = fd;
1304  socket_transport->message_bytes_written = 0;
1305 
1306  /* These values should probably be tunable or something. */
1307  socket_transport->max_bytes_read_per_iteration = 2048;
1308  socket_transport->max_bytes_written_per_iteration = 2048;
1309 
1310  return (DBusTransport*) socket_transport;
1311 
1312  failed_4:
1313  _dbus_watch_invalidate (socket_transport->read_watch);
1314  _dbus_watch_unref (socket_transport->read_watch);
1315  failed_3:
1316  _dbus_watch_invalidate (socket_transport->write_watch);
1317  _dbus_watch_unref (socket_transport->write_watch);
1318  failed_2:
1319  _dbus_string_free (&socket_transport->encoded_incoming);
1320  failed_1:
1321  _dbus_string_free (&socket_transport->encoded_outgoing);
1322  failed_0:
1323  dbus_free (socket_transport);
1324  return NULL;
1325 }
1326 
1340  const char *port,
1341  const char *family,
1342  const char *noncefile,
1343  DBusError *error)
1344 {
1345  int fd;
1346  DBusTransport *transport;
1347  DBusString address;
1348 
1349  _DBUS_ASSERT_ERROR_IS_CLEAR (error);
1350 
1351  if (!_dbus_string_init (&address))
1352  {
1354  return NULL;
1355  }
1356 
1357  if (host == NULL)
1358  host = "localhost";
1359 
1360  if (!_dbus_string_append (&address, noncefile ? "nonce-tcp:" : "tcp:"))
1361  goto error;
1362 
1363  if (!_dbus_string_append (&address, "host=") ||
1364  !_dbus_string_append (&address, host))
1365  goto error;
1366 
1367  if (!_dbus_string_append (&address, ",port=") ||
1368  !_dbus_string_append (&address, port))
1369  goto error;
1370 
1371  if (family != NULL &&
1372  (!_dbus_string_append (&address, ",family=") ||
1373  !_dbus_string_append (&address, family)))
1374  goto error;
1375 
1376  if (noncefile != NULL &&
1377  (!_dbus_string_append (&address, ",noncefile=") ||
1378  !_dbus_string_append (&address, noncefile)))
1379  goto error;
1380 
1381  fd = _dbus_connect_tcp_socket_with_nonce (host, port, family, noncefile, error);
1382  if (fd < 0)
1383  {
1384  _DBUS_ASSERT_ERROR_IS_SET (error);
1385  _dbus_string_free (&address);
1386  return NULL;
1387  }
1388 
1389  _dbus_verbose ("Successfully connected to tcp socket %s:%s\n",
1390  host, port);
1391 
1392  transport = _dbus_transport_new_for_socket (fd, NULL, &address);
1393  _dbus_string_free (&address);
1394  if (transport == NULL)
1395  {
1397  _dbus_close_socket (fd, NULL);
1398  fd = -1;
1399  }
1400 
1401  return transport;
1402 
1403 error:
1404  _dbus_string_free (&address);
1406  return NULL;
1407 }
1408 
1417 DBusTransportOpenResult
1419  DBusTransport **transport_p,
1420  DBusError *error)
1421 {
1422  const char *method;
1423  dbus_bool_t isTcp;
1424  dbus_bool_t isNonceTcp;
1425 
1426  method = dbus_address_entry_get_method (entry);
1427  _dbus_assert (method != NULL);
1428 
1429  isTcp = strcmp (method, "tcp") == 0;
1430  isNonceTcp = strcmp (method, "nonce-tcp") == 0;
1431 
1432  if (isTcp || isNonceTcp)
1433  {
1434  const char *host = dbus_address_entry_get_value (entry, "host");
1435  const char *port = dbus_address_entry_get_value (entry, "port");
1436  const char *family = dbus_address_entry_get_value (entry, "family");
1437  const char *noncefile = dbus_address_entry_get_value (entry, "noncefile");
1438 
1439  if ((isNonceTcp == TRUE) != (noncefile != NULL)) {
1440  _dbus_set_bad_address (error, method, "noncefile", NULL);
1441  return DBUS_TRANSPORT_OPEN_BAD_ADDRESS;
1442  }
1443 
1444  if (port == NULL)
1445  {
1446  _dbus_set_bad_address (error, method, "port", NULL);
1447  return DBUS_TRANSPORT_OPEN_BAD_ADDRESS;
1448  }
1449 
1450  *transport_p = _dbus_transport_new_for_tcp_socket (host, port, family, noncefile, error);
1451  if (*transport_p == NULL)
1452  {
1453  _DBUS_ASSERT_ERROR_IS_SET (error);
1454  return DBUS_TRANSPORT_OPEN_DID_NOT_CONNECT;
1455  }
1456  else
1457  {
1458  _DBUS_ASSERT_ERROR_IS_CLEAR (error);
1459  return DBUS_TRANSPORT_OPEN_OK;
1460  }
1461  }
1462  else
1463  {
1464  _DBUS_ASSERT_ERROR_IS_CLEAR (error);
1465  return DBUS_TRANSPORT_OPEN_NOT_HANDLED;
1466  }
1467 }
1468 
Implementation details of DBusTransportSocket.
dbus_bool_t _dbus_string_append(DBusString *str, const char *buffer)
Appends a nul-terminated C-style string to a DBusString.
Definition: dbus-string.c:921
void dbus_message_lock(DBusMessage *message)
Locks a message.
Definition: dbus-message.c:383
DBusTransport base
Parent instance.
void _dbus_connection_toggle_watch_unlocked(DBusConnection *connection, DBusWatch *watch, dbus_bool_t enabled)
Toggles a watch and notifies app via connection&#39;s DBusWatchToggledFunction if available.
long max_live_messages_unix_fds
Max total unix fds of received messages.
const char * message
public error message field
Definition: dbus-errors.h:51
Implementation of DBusWatch.
Definition: dbus-watch.c:40
DBusTransport * _dbus_transport_new_for_tcp_socket(const char *host, const char *port, const char *family, const char *noncefile, DBusError *error)
Creates a new transport for the given hostname and port.
#define NULL
A null pointer, defined appropriately for C or C++.
int fd
File descriptor.
DBusAuth * auth
Authentication conversation.
unsigned int disconnected
TRUE if we are disconnected.
void dbus_free(void *memory)
Frees a block of memory previously allocated by dbus_malloc() or dbus_malloc0().
Definition: dbus-memory.c:700
void _dbus_message_loader_return_unix_fds(DBusMessageLoader *loader, int *fds, unsigned n_fds)
Returns a buffer obtained from _dbus_message_loader_get_unix_fds().
dbus_bool_t _dbus_socket_can_pass_unix_fd(int fd)
Checks whether file descriptors may be passed via the socket.
dbus_bool_t _dbus_auth_needs_decoding(DBusAuth *auth)
Called post-authentication, indicates whether we need to decode the message stream with _dbus_auth_de...
Definition: dbus-auth.c:2652
void _dbus_watch_invalidate(DBusWatch *watch)
Clears the file descriptor from a now-invalid watch object so that no one tries to use it...
Definition: dbus-watch.c:169
long max_live_messages_size
Max total size of received messages.
void _dbus_connection_lock(DBusConnection *connection)
Acquires the connection lock.
dbus_bool_t _dbus_auth_encode_data(DBusAuth *auth, const DBusString *plaintext, DBusString *encoded)
Called post-authentication, encodes a block of bytes for sending to the peer.
Definition: dbus-auth.c:2620
DBUS_EXPORT dbus_bool_t dbus_watch_get_enabled(DBusWatch *watch)
Returns whether a watch is enabled or not.
Definition: dbus-watch.c:645
#define _dbus_assert(condition)
Aborts with an error message if the condition is false.
int _dbus_write_socket_two(int fd, const DBusString *buffer1, int start1, int len1, const DBusString *buffer2, int start2, int len2)
Like _dbus_write_two() but only works on sockets and is thus available on Windows.
#define DBUS_ERROR_INIT
Expands to a suitable initializer for a DBusError on the stack.
Definition: dbus-errors.h:62
const char * dbus_address_entry_get_method(DBusAddressEntry *entry)
Returns the method string of an address entry.
Definition: dbus-address.c:227
DBusTransportOpenResult _dbus_transport_open_socket(DBusAddressEntry *entry, DBusTransport **transport_p, DBusError *error)
Opens a TCP socket transport.
dbus_bool_t _dbus_transport_queue_messages(DBusTransport *transport)
Processes data we&#39;ve read while handling a watch, potentially converting some of it to messages and q...
dbus_bool_t _dbus_get_is_errno_etoomanyrefs(void)
See if errno is ETOOMANYREFS.
Definition: dbus-sysdeps.c:767
DBusAuthState _dbus_auth_do_work(DBusAuth *auth)
Analyzes buffered input and moves the auth conversation forward, returning the new state of the auth ...
Definition: dbus-auth.c:2431
void dbus_error_free(DBusError *error)
Frees an error that&#39;s been set (or just initialized), then reinitializes the error as in dbus_error_i...
Definition: dbus-errors.c:211
int _dbus_read_socket_with_unix_fds(int fd, DBusString *buffer, int count, int *fds, int *n_fds)
Like _dbus_read_socket() but also tries to read unix fds from the socket.
A portable struct pollfd wrapper.
Definition: dbus-sysdeps.h:299
int max_bytes_written_per_iteration
To avoid blocking too long.
DBusConnection * connection
Connection owning this transport.
#define _DBUS_POLLIN
There is data to read.
Definition: dbus-sysdeps.h:283
unsigned int send_credentials_pending
TRUE if we need to send credentials
dbus_bool_t _dbus_string_init(DBusString *str)
Initializes a string.
Definition: dbus-string.c:175
DBusTransport * _dbus_transport_new_for_socket(int fd, const DBusString *server_guid, const DBusString *address)
Creates a new transport for the given socket file descriptor.
DBusMessage * _dbus_connection_get_message_to_send(DBusConnection *connection)
Gets the next outgoing message.
short events
Events to poll for.
Definition: dbus-sysdeps.h:302
dbus_bool_t _dbus_read_credentials_socket(int client_fd, DBusCredentials *credentials, DBusError *error)
Reads a single byte which must be nul (an error occurs otherwise), and reads unix credentials if avai...
DBusString encoded_incoming
Encoded version of current incoming data.
dbus_bool_t _dbus_transport_get_is_connected(DBusTransport *transport)
Returns TRUE if the transport has not been disconnected.
dbus_bool_t _dbus_transport_init_base(DBusTransport *transport, const DBusTransportVTable *vtable, const DBusString *server_guid, const DBusString *address)
Initializes the base class members of DBusTransport.
void _dbus_auth_get_buffer(DBusAuth *auth, DBusString **buffer)
Get a buffer to be used for reading bytes from the peer we&#39;re conversing with.
Definition: dbus-auth.c:2520
Internals of DBusMessage.
dbus_bool_t _dbus_auth_decode_data(DBusAuth *auth, const DBusString *encoded, DBusString *plaintext)
Called post-authentication, decodes a block of bytes received from the peer.
Definition: dbus-auth.c:2683
const char * dbus_address_entry_get_value(DBusAddressEntry *entry, const char *key)
Returns a value from a key of an entry.
Definition: dbus-address.c:244
#define dbus_new0(type, count)
Safe macro for using dbus_malloc0().
Definition: dbus-memory.h:59
void _dbus_auth_bytes_sent(DBusAuth *auth, int bytes_sent)
Notifies the auth conversation object that the given number of bytes of the outgoing buffer have been...
Definition: dbus-auth.c:2500
void _dbus_connection_remove_watch_unlocked(DBusConnection *connection, DBusWatch *watch)
Removes a watch using the connection&#39;s DBusRemoveWatchFunction if available.
dbus_bool_t _dbus_string_compact(DBusString *str, int max_waste)
Compacts the string to avoid wasted memory.
Definition: dbus-string.c:375
unsigned int receive_credentials_pending
TRUE if we need to receive credentials
As in POLLOUT.
DBusCounter * live_messages
Counter for size/unix fds of all live messages.
dbus_uint32_t dbus_bool_t
A boolean, valid values are TRUE and FALSE.
Definition: dbus-types.h:35
dbus_bool_t _dbus_connection_has_messages_to_send_unlocked(DBusConnection *connection)
Checks whether there are messages in the outgoing message queue.
DBUS_EXPORT int dbus_watch_get_socket(DBusWatch *watch)
Returns a socket to be watched, on UNIX this will return -1 if our transport is not socket-based so d...
Definition: dbus-watch.c:564
int _dbus_poll(DBusPollFD *fds, int n_fds, int timeout_milliseconds)
Wrapper for poll().
#define _DBUS_POLLOUT
Writing now will not block.
Definition: dbus-sysdeps.h:287
dbus_bool_t _dbus_transport_get_is_authenticated(DBusTransport *transport)
Returns TRUE if we have been authenticated.
Internals of DBusAddressEntry.
Definition: dbus-address.c:43
int _dbus_string_get_length(const DBusString *str)
Gets the length of a string (not including nul termination).
Definition: dbus-string.c:725
int _dbus_read_socket(int fd, DBusString *buffer, int count)
Like _dbus_read(), but only works on sockets so is available on Windows.
DBusWatch * write_watch
Watch for writability.
void _dbus_set_bad_address(DBusError *error, const char *address_problem_type, const char *address_problem_field, const char *address_problem_other)
Sets DBUS_ERROR_BAD_ADDRESS.
Definition: dbus-address.c:65
dbus_bool_t _dbus_connection_handle_watch(DBusWatch *watch, unsigned int condition, void *data)
A callback for use with dbus_watch_new() to create a DBusWatch.
dbus_bool_t _dbus_get_is_errno_enomem(void)
See if errno is ENOMEM.
Definition: dbus-sysdeps.c:737
Object representing an exception.
Definition: dbus-errors.h:48
dbus_bool_t _dbus_send_credentials_socket(int server_fd, DBusError *error)
Sends a single nul byte with our UNIX credentials as ancillary data.
dbus_bool_t _dbus_connection_add_watch_unlocked(DBusConnection *connection, DBusWatch *watch)
Adds a watch using the connection&#39;s DBusAddWatchFunction if available.
void dbus_set_error(DBusError *error, const char *name, const char *format,...)
Assigns an error name and message to a DBusError.
Definition: dbus-errors.c:354
The virtual table that must be implemented to create a new kind of transport.
int message_bytes_written
Number of bytes of current outgoing message that have been written.
void _dbus_connection_message_sent_unlocked(DBusConnection *connection, DBusMessage *message)
Notifies the connection that a message has been sent, so the message can be removed from the outgoing...
int fd
File descriptor.
Definition: dbus-sysdeps.h:301
As in POLLERR (can&#39;t watch for this, but can be present in current state passed to dbus_watch_handle(...
void _dbus_string_free(DBusString *str)
Frees a string created by _dbus_string_init().
Definition: dbus-string.c:242
#define TRUE
Expands to &quot;1&quot;.
DBusMessageLoader * loader
Message-loading buffer.
As in POLLHUP (can&#39;t watch for it, but can be present in current state passed to dbus_watch_handle())...
void _dbus_message_get_network_data(DBusMessage *message, const DBusString **header, const DBusString **body)
Gets the data to be sent over the network for this message.
Definition: dbus-message.c:207
void _dbus_message_loader_return_buffer(DBusMessageLoader *loader, DBusString *buffer, int bytes_read)
Returns a buffer obtained from _dbus_message_loader_get_buffer(), indicating to the loader how many b...
Object representing a transport such as a socket.
dbus_bool_t _dbus_auth_set_credentials(DBusAuth *auth, DBusCredentials *credentials)
Sets credentials received via reliable means from the operating system.
Definition: dbus-auth.c:2715
void _dbus_auth_set_unix_fd_possible(DBusAuth *auth, dbus_bool_t b)
Sets whether unix fd passing is potentially on the transport and hence shall be negotiated.
Definition: dbus-auth.c:2791
const char * _dbus_strerror_from_errno(void)
Get error message from errno.
Definition: dbus-sysdeps.c:781
int _dbus_write_socket(int fd, const DBusString *buffer, int start, int len)
Like _dbus_write(), but only supports sockets and is thus available on Windows.
dbus_bool_t _dbus_get_is_errno_eintr(void)
See if errno is EINTR.
Definition: dbus-sysdeps.c:747
dbus_bool_t _dbus_message_loader_get_unix_fds(DBusMessageLoader *loader, int **fds, unsigned *max_n_fds)
Gets the buffer to use for reading unix fds from the network.
DBusWatch * _dbus_watch_new(int fd, unsigned int flags, dbus_bool_t enabled, DBusWatchHandler handler, void *data, DBusFreeFunction free_data_function)
Creates a new DBusWatch.
Definition: dbus-watch.c:88
DBusTransport * _dbus_transport_ref(DBusTransport *transport)
Increments the reference count for the transport.
void _dbus_message_loader_get_buffer(DBusMessageLoader *loader, DBusString **buffer)
Gets the buffer to use for reading data from the network.
dbus_bool_t _dbus_get_is_errno_eagain_or_ewouldblock(void)
See if errno is EAGAIN or EWOULDBLOCK (this has to be done differently for Winsock so is abstracted) ...
dbus_bool_t _dbus_auth_get_bytes_to_send(DBusAuth *auth, const DBusString **str)
Gets bytes that need to be sent to the peer we&#39;re conversing with.
Definition: dbus-auth.c:2475
DBusWatch * read_watch
Watch for readability.
#define DBUS_ERROR_NO_MEMORY
There was not enough memory to complete an operation.
#define FALSE
Expands to &quot;0&quot;.
DBusCredentials * credentials
Credentials of other end read from the socket.
dbus_bool_t _dbus_string_set_length(DBusString *str, int length)
Sets the length of a string.
Definition: dbus-string.c:788
int max_bytes_read_per_iteration
To avoid blocking too long.
void _dbus_watch_unref(DBusWatch *watch)
Decrements the reference count of a DBusWatch object and finalizes the object if the count reaches ze...
Definition: dbus-watch.c:138
dbus_bool_t _dbus_auth_needs_encoding(DBusAuth *auth)
Called post-authentication, indicates whether we need to encode the message stream with _dbus_auth_en...
Definition: dbus-auth.c:2593
As in POLLIN.
void _dbus_message_get_unix_fds(DBusMessage *message, const int **fds, unsigned *n_fds)
Gets the unix fds to be sent over the network for this message.
Definition: dbus-message.c:226
dbus_bool_t _dbus_close_socket(int fd, DBusError *error)
Closes a socket.
void _dbus_watch_set_handler(DBusWatch *watch, DBusWatchHandler handler, void *data, DBusFreeFunction free_data_function)
Sets the handler for the watch.
Definition: dbus-watch.c:469
void _dbus_transport_disconnect(DBusTransport *transport)
Closes our end of the connection to a remote application.
void _dbus_transport_finalize_base(DBusTransport *transport)
Finalizes base class members of DBusTransport.
void _dbus_connection_unlock(DBusConnection *connection)
Releases the connection lock.
short revents
Events that occurred.
Definition: dbus-sysdeps.h:303
dbus_bool_t _dbus_get_is_errno_epipe(void)
See if errno is EPIPE.
Definition: dbus-sysdeps.c:757
void _dbus_auth_return_buffer(DBusAuth *auth, DBusString *buffer, int bytes_read)
Returns a buffer with new data read into it.
Definition: dbus-auth.c:2539
void _dbus_transport_unref(DBusTransport *transport)
Decrements the reference count for the transport.
long _dbus_counter_get_unix_fd_value(DBusCounter *counter)
Gets the current value of the unix fd counter.
#define _DBUS_POLLERR
Error condition.
Definition: dbus-sysdeps.h:289
DBusString encoded_outgoing
Encoded version of current outgoing message.
long _dbus_counter_get_size_value(DBusCounter *counter)
Gets the current value of the size counter.