updates.
[silc.git] / lib / silcclient / client.c
index 92fac57bc5e0d83bbb78cfefe473bbf9429fb942..8403f44a30f8dabef3c9e37b1707f633a008bfdc 100644 (file)
@@ -141,6 +141,7 @@ SilcClientConnection silc_client_add_connection(SilcClient client,
   conn->remote_host = strdup(hostname);
   conn->remote_port = port;
   conn->context = context;
+  conn->pending_commands = silc_dlist_init();
 
   /* Add the connection to connections table */
   for (i = 0; i < client->conns_count; i++)
@@ -165,6 +166,8 @@ void silc_client_del_connection(SilcClient client, SilcClientConnection conn)
 
   for (i = 0; i < client->conns_count; i++)
     if (client->conns[i] == conn) {
+      if (conn->pending_commands)
+       silc_dlist_uninit(conn->pending_commands);
       silc_free(conn);
       client->conns[i] = NULL;
     }
@@ -414,7 +417,7 @@ SILC_TASK_CALLBACK(silc_client_connect_to_server_second)
   /* Free old protocol as it is finished now */
   silc_protocol_free(protocol);
   if (ctx->packet)
-    silc_buffer_free(ctx->packet);
+    silc_packet_context_free(ctx->packet);
   silc_free(ctx);
   /* silc_free(ctx->keymat....); */
   sock->protocol = NULL;
@@ -648,12 +651,8 @@ SILC_TASK_CALLBACK(silc_client_packet_parse_real)
   silc_client_packet_parse_type(client, sock, packet);
 
  out:
-  silc_buffer_clear(buffer);
-  if (packet->src_id)
-    silc_free(packet->src_id);
-  if (packet->dst_id)
-    silc_free(packet->dst_id);
-  silc_free(packet);
+  silc_buffer_clear(sock->inbuf);
+  silc_packet_context_free(packet);
   silc_free(parse_ctx);
 }
 
@@ -780,7 +779,7 @@ void silc_client_packet_parse_type(SilcClient client,
       SilcClientKEInternalContext *proto_ctx = 
        (SilcClientKEInternalContext *)sock->protocol->context;
 
-      proto_ctx->packet = buffer;
+      proto_ctx->packet = silc_packet_context_dup(packet);
       proto_ctx->dest_id_type = packet->src_id_type;
       proto_ctx->dest_id = silc_id_str2id(packet->src_id, packet->src_id_type);
 
@@ -809,9 +808,9 @@ void silc_client_packet_parse_type(SilcClient client,
        (SilcClientKEInternalContext *)sock->protocol->context;
 
       if (proto_ctx->packet)
-       silc_buffer_free(proto_ctx->packet);
+       silc_packet_context_free(proto_ctx->packet);
 
-      proto_ctx->packet = buffer;
+      proto_ctx->packet = silc_packet_context_dup(packet);
       proto_ctx->dest_id_type = packet->src_id_type;
       proto_ctx->dest_id = silc_id_str2id(packet->src_id, packet->src_id_type);
 
@@ -1199,6 +1198,8 @@ void silc_client_close_connection(SilcClient client,
       memset(conn->hmac_key, 0, conn->hmac_key_len);
       silc_free(conn->hmac_key);
     }
+    if (conn->pending_commands)
+      silc_dlist_uninit(conn->pending_commands);
 
     conn->sock = NULL;
     conn->remote_port = 0;
@@ -1212,6 +1213,7 @@ void silc_client_close_connection(SilcClient client,
     conn->local_id_data = NULL;
     conn->remote_host = NULL;
     conn->current_channel = NULL;
+    conn->pending_commands = NULL;
 
     silc_client_del_connection(client, conn);
   }
@@ -1267,12 +1269,7 @@ static void silc_client_notify_by_server_pending(void *context)
 {
   SilcPacketContext *p = (SilcPacketContext *)context;
   silc_client_notify_by_server(p->context, p->sock, p);
-  if (p->src_id)
-    silc_free(p->src_id);
-  if (p->dst_id)
-    silc_free(p->dst_id);
-  silc_buffer_free(p->buffer);
-  silc_free(p);
+  silc_packet_context_free(p);
 }
 
 /* Received notify message from server */
@@ -1286,7 +1283,6 @@ void silc_client_notify_by_server(SilcClient client,
   SilcNotifyPayload payload;
   SilcNotifyType type;
   SilcArgumentPayload args;
-  int i;
 
   SilcClientID *client_id = NULL;
   SilcChannelID *channel_id = NULL;
@@ -1330,7 +1326,7 @@ void silc_client_notify_by_server(SilcClient client,
       SilcPacketContext *p = silc_packet_context_dup(packet);
       p->context = (void *)client;
       p->sock = sock;
-      silc_client_command_pending(SILC_COMMAND_WHOIS
+      silc_client_command_pending(conn,SILC_COMMAND_WHOIS, 0
                                  silc_client_notify_by_server_pending, p);
       goto out;
     }
@@ -1376,7 +1372,7 @@ void silc_client_notify_by_server(SilcClient client,
       SilcPacketContext *p = silc_packet_context_dup(packet);
       p->context = (void *)client;
       p->sock = sock;
-      silc_client_command_pending(SILC_COMMAND_WHOIS
+      silc_client_command_pending(conn, SILC_COMMAND_WHOIS, 0
                                  silc_client_notify_by_server_pending, p);
       goto out;
     }
@@ -1550,7 +1546,7 @@ void silc_client_notify_by_server(SilcClient client,
       SilcPacketContext *p = silc_packet_context_dup(packet);
       p->context = (void *)client;
       p->sock = sock;
-      silc_client_command_pending(SILC_COMMAND_WHOIS
+      silc_client_command_pending(conn, SILC_COMMAND_WHOIS, 0
                                  silc_client_notify_by_server_pending, p);
       goto out;
     }
@@ -1790,27 +1786,21 @@ void silc_client_new_channel_id(SilcClient client,
                   (void *)channel->id, (void *)channel, TRUE);
 }
 
-/* Processes received key for channel. The received key will be used
-   to protect the traffic on the channel for now on. Client must receive
-   the key to the channel before talking on the channel is possible. 
-   This is the key that server has generated, this is not the channel
-   private key, it is entirely local setting. */
+/* Saves channel key from encoded `key_payload'. This is used when we
+   receive Channel Key Payload and when we are processing JOIN command 
+   reply. */
 
-void silc_client_receive_channel_key(SilcClient client,
-                                    SilcSocketConnection sock,
-                                    SilcBuffer packet)
+void silc_client_save_channel_key(SilcClientConnection conn,
+                                 SilcBuffer key_payload, 
+                                 SilcChannelEntry channel)
 {
   unsigned char *id_string, *key, *cipher;
   unsigned int tmp_len;
-  SilcClientConnection conn = (SilcClientConnection)sock->user_data;
   SilcChannelID *id;
   SilcIDCacheEntry id_cache = NULL;
-  SilcChannelEntry channel;
   SilcChannelKeyPayload payload;
 
-  SILC_LOG_DEBUG(("Received key for channel"));
-  
-  payload = silc_channel_key_payload_parse(packet);
+  payload = silc_channel_key_payload_parse(key_payload);
   if (!payload)
     return;
 
@@ -1819,24 +1809,28 @@ void silc_client_receive_channel_key(SilcClient client,
     silc_channel_key_payload_free(payload);
     return;
   }
-  id = silc_id_payload_parse_id(id_string, tmp_len);
+
+  id = silc_id_str2id(id_string, SILC_ID_CHANNEL);
 
   /* Find channel. */
-  if (!silc_idcache_find_by_id_one(conn->channel_cache, (void *)id,
-                                  SILC_ID_CHANNEL, &id_cache))
-    goto out;
+  if (!channel) {
+    if (!silc_idcache_find_by_id_one(conn->channel_cache, (void *)id,
+                                    SILC_ID_CHANNEL, &id_cache))
+      goto out;
     
+    /* Get channel entry */
+    channel = (SilcChannelEntry)id_cache->context;
+  }
+
   /* Save the key */
   key = silc_channel_key_get_key(payload, &tmp_len);
   cipher = silc_channel_key_get_cipher(payload, NULL);
-
-  channel = (SilcChannelEntry)id_cache->context;
   channel->key_len = tmp_len;
   channel->key = silc_calloc(tmp_len, sizeof(*channel->key));
   memcpy(channel->key, key, tmp_len);
 
   if (!silc_cipher_alloc(cipher, &channel->channel_key)) {
-    client->ops->say(client, conn,
+    conn->client->ops->say(conn->client, conn,
                     "Cannot talk to channel: unsupported cipher %s", cipher);
     goto out;
   }
@@ -1851,6 +1845,22 @@ void silc_client_receive_channel_key(SilcClient client,
   silc_channel_key_payload_free(payload);
 }
 
+/* Processes received key for channel. The received key will be used
+   to protect the traffic on the channel for now on. Client must receive
+   the key to the channel before talking on the channel is possible. 
+   This is the key that server has generated, this is not the channel
+   private key, it is entirely local setting. */
+
+void silc_client_receive_channel_key(SilcClient client,
+                                    SilcSocketConnection sock,
+                                    SilcBuffer packet)
+{
+  SILC_LOG_DEBUG(("Received key for channel"));
+
+  /* Save the key */
+  silc_client_save_channel_key(sock->user_data, packet, NULL);
+}
+
 /* Process received message to a channel (or from a channel, really). This
    decrypts the channel message with channel specific key and parses the
    channel payload. Finally it displays the message on the screen. */
@@ -1867,7 +1877,6 @@ void silc_client_channel_message(SilcClient client,
   SilcChannelUser chu;
   SilcIDCacheEntry id_cache = NULL;
   SilcClientID *client_id = NULL;
-  int i;
   char *nickname;
 
   /* Sanity checks */
@@ -2006,7 +2015,6 @@ void silc_client_remove_from_channels(SilcClient client,
   SilcIDCacheList list;
   SilcChannelEntry channel;
   SilcChannelUser chu;
-  int i;
 
   if (!silc_idcache_find_by_id(conn->channel_cache, SILC_ID_CACHE_ANY,
                               SILC_ID_CHANNEL, &list))
@@ -2050,7 +2058,6 @@ void silc_client_replace_from_channels(SilcClient client,
   SilcIDCacheList list;
   SilcChannelEntry channel;
   SilcChannelUser chu;
-  int i;
 
   if (!silc_idcache_find_by_id(conn->channel_cache, SILC_ID_CACHE_ANY,
                               SILC_ID_CHANNEL, &list))