updates.
[silc.git] / lib / silcclient / client.c
index 5cbfc58816b6f2ce3391bdb34e40e60affad063b..4875e8e24d09901ece369ff18c6c3231b2e0c047 100644 (file)
@@ -768,9 +768,6 @@ SILC_TASK_CALLBACK_GLOBAL(silc_client_packet_process)
     if (SILC_IS_DISCONNECTED(sock))
       return;
 
-    if (sock->outbuf->data - sock->outbuf->head)
-      silc_buffer_push(sock->outbuf, sock->outbuf->data - sock->outbuf->head);
-
     ret = silc_packet_send(sock, TRUE);
 
     /* If returned -2 could not write to connection now, will do
@@ -809,14 +806,14 @@ SILC_TASK_CALLBACK_GLOBAL(silc_client_packet_process)
       if (SILC_IS_DISCONNECTING(sock)) {
        if (sock == conn->sock && sock->type != SILC_SOCKET_TYPE_CLIENT)
          client->internal->ops->disconnect(client, conn);
-       silc_client_close_connection(client, sock, conn);
+       silc_client_close_connection_real(client, sock, conn);
        return;
       }
       
       SILC_LOG_DEBUG(("EOF from connection %d", sock->sock));
       if (sock == conn->sock && sock->type != SILC_SOCKET_TYPE_CLIENT)
        client->internal->ops->disconnect(client, conn);
-      silc_client_close_connection(client, sock, conn);
+      silc_client_close_connection_real(client, sock, conn);
       return;
     }
 
@@ -1184,6 +1181,7 @@ void silc_client_packet_send(SilcClient client,
                             int force_send)
 {
   SilcPacketContext packetdata;
+  const SilcBufferStruct packet;
   int block_len;
   SilcUInt32 sequence = 0;
 
@@ -1241,32 +1239,20 @@ void silc_client_packet_send(SilcClient client,
     packetdata.src_id_len + packetdata.dst_id_len;
   packetdata.padlen = SILC_PACKET_PADLEN(packetdata.truelen, block_len);
 
-  /* Prepare outgoing data buffer for packet sending */
-  silc_packet_send_prepare(sock, 
-                          SILC_PACKET_HEADER_LEN +
-                          packetdata.src_id_len + 
-                          packetdata.dst_id_len,
-                          packetdata.padlen,
-                          data_len);
-
-  SILC_LOG_DEBUG(("Putting data to outgoing buffer, len %d", data_len));
-
-  packetdata.buffer = sock->outbuf;
-
-  /* Put the data to the buffer */
-  if (data && data_len)
-    silc_buffer_put(sock->outbuf, data, data_len);
-
   /* Create the outgoing packet */
-  silc_packet_assemble(&packetdata, cipher);
+  if (!silc_packet_assemble(&packetdata, client->rng, cipher, hmac, sock, 
+                            data, data_len, (const SilcBuffer)&packet)) {
+    SILC_LOG_ERROR(("Error assembling packet"));
+    return;
+  }
 
   /* Encrypt the packet */
   if (cipher)
-    silc_packet_encrypt(cipher, hmac, sequence, sock->outbuf
-                       sock->outbuf->len);
+    silc_packet_encrypt(cipher, hmac, sequence, (SilcBuffer)&packet
+                        packet.len);
 
-  SILC_LOG_HEXDUMP(("Packet (%d), len %d", sequence, sock->outbuf->len),
-                  sock->outbuf->data, sock->outbuf->len);
+  SILC_LOG_HEXDUMP(("Packet (%d), len %d", sequence, packet.len),
+                  packet.data, packet.len);
 
   /* Now actually send the packet */
   silc_client_packet_send_real(client, sock, force_send);
@@ -1277,11 +1263,7 @@ void silc_client_packet_queue_purge(SilcClient client,
 {
   if (sock && SILC_IS_OUTBUF_PENDING(sock) && 
       (SILC_IS_DISCONNECTED(sock) == FALSE)) {
-    if (sock->outbuf->data - sock->outbuf->head)
-      silc_buffer_push(sock->outbuf, sock->outbuf->data - sock->outbuf->head);
-
     silc_packet_send(sock, TRUE);
-
     SILC_CLIENT_SET_CONNECTION_FOR_INPUT(client->schedule, sock->sock);
     SILC_UNSET_OUTBUF_PENDING(sock);
     silc_buffer_clear(sock->outbuf);
@@ -1296,9 +1278,9 @@ void silc_client_packet_queue_purge(SilcClient client,
    connection but `conn->sock' might be actually a different connection
    than the `sock'). */
 
-void silc_client_close_connection(SilcClient client,
-                                 SilcSocketConnection sock,
-                                 SilcClientConnection conn)
+void silc_client_close_connection_real(SilcClient client,
+                                      SilcSocketConnection sock,
+                                      SilcClientConnection conn)
 {
   int del = FALSE;
 
@@ -1413,6 +1395,14 @@ void silc_client_close_connection(SilcClient client,
   silc_socket_free(sock);
 }
 
+/* Closes the connection to the remote end */
+
+void silc_client_close_connection(SilcClient client,
+                                 SilcClientConnection conn)
+{
+  silc_client_close_connection_real(client, NULL, conn);
+}
+
 /* Called when we receive disconnection packet from server. This 
    closes our end properly and displays the reason of the disconnection
    on the screen. */
@@ -1426,7 +1416,7 @@ SILC_TASK_CALLBACK(silc_client_disconnected_by_server_later)
   if (sock == NULL)
     return;
 
-  silc_client_close_connection(client, sock, sock->user_data);
+  silc_client_close_connection_real(client, sock, sock->user_data);
 }
 
 /* Called when we receive disconnection packet from server. This