Working - simple message passing with serverClient signal send-receive
[platform/upstream/dbus.git] / dbus / dbus-transport-socket.c
index 4b88af7..5da3e98 100644 (file)
  *
  */
 
+#include <config.h>
 #include "dbus-internals.h"
 #include "dbus-connection-internal.h"
+#include "dbus-nonce.h"
 #include "dbus-transport-socket.h"
 #include "dbus-transport-protected.h"
 #include "dbus-watch.h"
 #include "dbus-credentials.h"
+#include <stdio.h>  //todo to remove
 
 /**
  * @defgroup DBusTransportSocket DBusTransport implementations for sockets
@@ -71,7 +74,7 @@ free_watches (DBusTransport *transport)
 {
   DBusTransportSocket *socket_transport = (DBusTransportSocket*) transport;
 
-  _dbus_verbose ("%s start\n", _DBUS_FUNCTION_NAME);
+  _dbus_verbose ("start\n");
   
   if (socket_transport->read_watch)
     {
@@ -93,7 +96,7 @@ free_watches (DBusTransport *transport)
       socket_transport->write_watch = NULL;
     }
 
-  _dbus_verbose ("%s end\n", _DBUS_FUNCTION_NAME);
+  _dbus_verbose ("end\n");
 }
 
 static void
@@ -101,7 +104,7 @@ socket_finalize (DBusTransport *transport)
 {
   DBusTransportSocket *socket_transport = (DBusTransportSocket*) transport;
 
-  _dbus_verbose ("%s\n", _DBUS_FUNCTION_NAME);
+  _dbus_verbose ("\n");
   
   free_watches (transport);
 
@@ -175,8 +178,7 @@ check_read_watch (DBusTransport *transport)
   DBusTransportSocket *socket_transport = (DBusTransportSocket*) transport;
   dbus_bool_t need_read_watch;
 
-  _dbus_verbose ("%s: fd = %d\n",
-                 _DBUS_FUNCTION_NAME, socket_transport->fd);
+  _dbus_verbose ("fd = %d\n",socket_transport->fd);
   
   if (transport->connection == NULL)
     return;
@@ -528,6 +530,8 @@ do_writing (DBusTransport *transport)
       int header_len, body_len;
       int total_bytes_to_write;
       
+//      int i;
+
       if (total > socket_transport->max_bytes_written_per_iteration)
         {
           _dbus_verbose ("%d bytes exceeds %d bytes written per iteration, returning\n",
@@ -549,6 +553,32 @@ do_writing (DBusTransport *transport)
       header_len = _dbus_string_get_length (header);
       body_len = _dbus_string_get_length (body);
 
+  /*    fprintf (stderr, "\nheader:\n");  //todo for test - to remove
+      for(i=0; i < header_len; i++)
+      {
+       fprintf (stderr, "%02x", _dbus_string_get_byte(header,i));
+      }
+      fprintf (stderr,"\n");
+      for(i=0; i < header_len; i++)
+      {
+       fprintf (stderr, "%c", _dbus_string_get_byte(header,i));
+      }
+      fprintf (stderr, "\nret size: %u, i: %u\n", header_len, i);
+
+
+      fprintf (stderr, "\nbody:\n");
+      for(i=0; i < body_len; i++)
+      {
+       fprintf (stderr, "%02x", _dbus_string_get_byte(body,i));
+      }
+      fprintf (stderr, "\nret size: %u, i: %u\n", body_len, i);
+      fprintf (stderr,"\n");
+      for(i=0; i < body_len; i++)
+      {
+       fprintf (stderr, "%c", _dbus_string_get_byte(body,i));
+      }
+      fprintf (stderr, "\nret size: %u, i: %u\n", body_len, i);*/
+
       if (_dbus_auth_needs_encoding (transport->auth))
         {
           /* Does fd passing even make sense with encoded data? */
@@ -676,8 +706,8 @@ do_writing (DBusTransport *transport)
               _dbus_string_set_length (&socket_transport->encoded_outgoing, 0);
               _dbus_string_compact (&socket_transport->encoded_outgoing, 2048);
 
-              _dbus_connection_message_sent (transport->connection,
-                                             message);
+              _dbus_connection_message_sent_unlocked (transport->connection,
+                                                      message);
             }
         }
     }
@@ -699,8 +729,7 @@ do_reading (DBusTransport *transport)
   int total;
   dbus_bool_t oom;
 
-  _dbus_verbose ("%s: fd = %d\n", _DBUS_FUNCTION_NAME,
-                 socket_transport->fd);
+  _dbus_verbose ("fd = %d\n",socket_transport->fd);
   
   /* No messages without authentication! */
   if (!_dbus_transport_get_is_authenticated (transport))
@@ -982,7 +1011,7 @@ socket_disconnect (DBusTransport *transport)
 {
   DBusTransportSocket *socket_transport = (DBusTransportSocket*) transport;
 
-  _dbus_verbose ("%s\n", _DBUS_FUNCTION_NAME);
+  _dbus_verbose ("\n");
   
   free_watches (transport);
   
@@ -1117,7 +1146,7 @@ socket_do_iteration (DBusTransport *transport,
        */
       if (flags & DBUS_ITERATION_BLOCK)
         {
-          _dbus_verbose ("unlock %s pre poll\n", _DBUS_FUNCTION_NAME);
+          _dbus_verbose ("unlock pre poll\n");
           _dbus_connection_unlock (transport->connection);
         }
       
@@ -1129,7 +1158,7 @@ socket_do_iteration (DBusTransport *transport,
 
       if (flags & DBUS_ITERATION_BLOCK)
         {
-          _dbus_verbose ("lock %s post poll\n", _DBUS_FUNCTION_NAME);
+          _dbus_verbose ("lock post poll\n");
           _dbus_connection_lock (transport->connection);
         }
       
@@ -1278,8 +1307,10 @@ _dbus_transport_new_for_socket (int               fd,
   return (DBusTransport*) socket_transport;
 
  failed_4:
+  _dbus_watch_invalidate (socket_transport->read_watch);
   _dbus_watch_unref (socket_transport->read_watch);
  failed_3:
+  _dbus_watch_invalidate (socket_transport->write_watch);
   _dbus_watch_unref (socket_transport->write_watch);
  failed_2:
   _dbus_string_free (&socket_transport->encoded_incoming);
@@ -1297,6 +1328,7 @@ _dbus_transport_new_for_socket (int               fd,
  * @param host the host to connect to
  * @param port the port to connect to
  * @param family the address family to connect to
+ * @param path to nonce file
  * @param error location to store reason for failure.
  * @returns a new transport, or #NULL on failure.
  */
@@ -1304,6 +1336,7 @@ DBusTransport*
 _dbus_transport_new_for_tcp_socket (const char     *host,
                                     const char     *port,
                                     const char     *family,
+                                    const char     *noncefile,
                                     DBusError      *error)
 {
   int fd;
@@ -1321,7 +1354,7 @@ _dbus_transport_new_for_tcp_socket (const char     *host,
   if (host == NULL)
     host = "localhost";
 
-  if (!_dbus_string_append (&address, "tcp:"))
+  if (!_dbus_string_append (&address, noncefile ? "nonce-tcp:" : "tcp:"))
     goto error;
 
   if (!_dbus_string_append (&address, "host=") ||
@@ -1333,11 +1366,16 @@ _dbus_transport_new_for_tcp_socket (const char     *host,
     goto error;
 
   if (family != NULL &&
-      (!_dbus_string_append (&address, "family=") ||
+      (!_dbus_string_append (&address, ",family=") ||
        !_dbus_string_append (&address, family)))
     goto error;
 
-  fd = _dbus_connect_tcp_socket (host, port, family, error);
+  if (noncefile != NULL &&
+      (!_dbus_string_append (&address, ",noncefile=") ||
+       !_dbus_string_append (&address, noncefile)))
+    goto error;
+
+  fd = _dbus_connect_tcp_socket_with_nonce (host, port, family, noncefile, error);
   if (fd < 0)
     {
       _DBUS_ASSERT_ERROR_IS_SET (error);
@@ -1349,16 +1387,14 @@ _dbus_transport_new_for_tcp_socket (const char     *host,
                  host, port);
   
   transport = _dbus_transport_new_for_socket (fd, NULL, &address);
+  _dbus_string_free (&address);
   if (transport == NULL)
     {
       dbus_set_error (error, DBUS_ERROR_NO_MEMORY, NULL);
       _dbus_close_socket (fd, NULL);
-      _dbus_string_free (&address);
       fd = -1;
     }
 
-  _dbus_string_free (&address);
-  
   return transport;
 
 error:
@@ -1381,23 +1417,34 @@ _dbus_transport_open_socket(DBusAddressEntry  *entry,
                             DBusError         *error)
 {
   const char *method;
+  dbus_bool_t isTcp;
+  dbus_bool_t isNonceTcp;
   
   method = dbus_address_entry_get_method (entry);
   _dbus_assert (method != NULL);
 
-  if (strcmp (method, "tcp") == 0)
+  isTcp = strcmp (method, "tcp") == 0;
+  isNonceTcp = strcmp (method, "nonce-tcp") == 0;
+
+  if (isTcp || isNonceTcp)
     {
       const char *host = dbus_address_entry_get_value (entry, "host");
       const char *port = dbus_address_entry_get_value (entry, "port");
       const char *family = dbus_address_entry_get_value (entry, "family");
+      const char *noncefile = dbus_address_entry_get_value (entry, "noncefile");
+
+      if ((isNonceTcp == TRUE) != (noncefile != NULL)) {
+          _dbus_set_bad_address (error, method, "noncefile", NULL);
+          return DBUS_TRANSPORT_OPEN_BAD_ADDRESS;
+      }
 
       if (port == NULL)
         {
-          _dbus_set_bad_address (error, "tcp", "port", NULL);
+          _dbus_set_bad_address (error, method, "port", NULL);
           return DBUS_TRANSPORT_OPEN_BAD_ADDRESS;
         }
 
-      *transport_p = _dbus_transport_new_for_tcp_socket (host, port, family, error);
+      *transport_p = _dbus_transport_new_for_tcp_socket (host, port, family, noncefile, error);
       if (*transport_p == NULL)
         {
           _DBUS_ASSERT_ERROR_IS_SET (error);