Added some asserts and error checks.
[silc.git] / lib / silcclient / client_prvmsg.c
index 2d065abe6406e005ff4f58c70cb8cd0c665cdffc..dc7b14d467760966b4bdc2220005d4869970ec13 100644 (file)
@@ -41,7 +41,7 @@ void silc_client_send_private_message(SilcClient client,
                                      SilcUInt32 data_len, 
                                      int force_send)
 {
-  SilcSocketConnection sock = conn->sock;
+  SilcSocketConnection sock;
   SilcBuffer buffer;
   SilcPacketContext packetdata;
   const SilcBufferStruct packet;
@@ -49,6 +49,8 @@ void silc_client_send_private_message(SilcClient client,
   SilcHmac hmac;
   int block_len;
 
+  assert(client && conn && client_entry);
+  sock = conn->sock;
   SILC_LOG_DEBUG(("Sending private message"));
 
   /* Encode private message payload */
@@ -71,8 +73,8 @@ void silc_client_send_private_message(SilcClient client,
   /* We have private message specific key */
 
   /* Get data used in the encryption */
-  cipher = conn->send_key;
-  hmac = conn->hmac_send;
+  cipher = conn->internal->send_key;
+  hmac = conn->internal->hmac_send;
   block_len = silc_cipher_get_block_len(cipher);
 
   /* Set the packet context pointers. */
@@ -103,7 +105,7 @@ void silc_client_send_private_message(SilcClient client,
   }
 
   /* Encrypt the header and padding of the packet. */
-  silc_packet_encrypt(cipher, hmac, conn->psn_send++,
+  silc_packet_encrypt(cipher, hmac, conn->internal->psn_send++,
                      (SilcBuffer)&packet, SILC_PACKET_HEADER_LEN + 
                      packetdata.src_id_len + packetdata.dst_id_len +
                      packetdata.padlen);
@@ -202,7 +204,8 @@ void silc_client_private_message(SilcClient client,
 
   /* See if we are away (gone). If we are away we will reply to the
      sender with the set away message. */
-  if (conn->away && conn->away->away && !(flags & SILC_MESSAGE_FLAG_NOREPLY)) {
+  if (conn->internal->away && conn->internal->away->away &&
+      !(flags & SILC_MESSAGE_FLAG_NOREPLY)) {
     /* If it's me, ignore */
     if (SILC_ID_CLIENT_COMPARE(remote_id, conn->local_id))
       goto out;
@@ -211,8 +214,8 @@ void silc_client_private_message(SilcClient client,
     silc_client_send_private_message(client, conn, remote_client,
                                     SILC_MESSAGE_FLAG_AUTOREPLY |
                                     SILC_MESSAGE_FLAG_NOREPLY,
-                                    conn->away->away,
-                                    strlen(conn->away->away), TRUE);
+                                    conn->internal->away->away,
+                                    strlen(conn->internal->away->away), TRUE);
   }
 
  out:
@@ -328,7 +331,7 @@ int silc_client_add_private_message_key(SilcClient client,
   int i;
   SilcSKEKeyMaterial *keymat;
 
-  assert(client_entry);
+  assert(client && client_entry);
 
   /* Return FALSE if key already set */
   if (client_entry->send_key && client_entry->receive_key)
@@ -401,7 +404,7 @@ int silc_client_add_private_message_key_ske(SilcClient client,
                                            SilcSKEKeyMaterial *key,
                                            bool responder)
 {
-  assert(client_entry);
+  assert(client && client_entry);
 
   /* Return FALSE if key already set */
   if (client_entry->send_key && client_entry->receive_key)
@@ -453,16 +456,21 @@ int silc_client_send_private_message_key(SilcClient client,
                                         SilcClientEntry client_entry,
                                         int force_send)
 {
-  SilcSocketConnection sock = conn->sock;
+  SilcSocketConnection sock;
   SilcBuffer buffer;
   int cipher_len;
+  const char *cipher;
 
+  assert(client && conn && client_entry);
+
+  sock = conn->sock;
   if (!client_entry->send_key || !client_entry->key)
     return FALSE;
 
   SILC_LOG_DEBUG(("Sending private message key"));
 
-  cipher_len = strlen(client_entry->send_key->cipher->name);
+  cipher = silc_cipher_get_name(client_entry->send_key);
+  cipher_len = strlen(cipher);
 
   /* Create private message key payload */
   buffer = silc_buffer_alloc(2 + client_entry->key_len);
@@ -472,7 +480,7 @@ int silc_client_send_private_message_key(SilcClient client,
                     SILC_STR_UI_XNSTRING(client_entry->key, 
                                          client_entry->key_len),
                     SILC_STR_UI_SHORT(cipher_len),
-                    SILC_STR_UI_XNSTRING(client_entry->send_key->cipher->name,
+                    SILC_STR_UI_XNSTRING(cipher,
                                          cipher_len),
                     SILC_STR_END);
 
@@ -493,7 +501,7 @@ int silc_client_del_private_message_key(SilcClient client,
                                        SilcClientConnection conn,
                                        SilcClientEntry client_entry)
 {
-  assert(client_entry);
+  assert(client && client_entry);
 
   if (!client_entry->send_key && !client_entry->receive_key)
     return FALSE;
@@ -532,7 +540,9 @@ silc_client_list_private_message_keys(SilcClient client,
   SilcIDCacheList list;
   SilcClientEntry entry;
 
-  if (!silc_idcache_get_all(conn->client_cache, &list))
+  assert(client && conn);
+
+  if (!silc_idcache_get_all(conn->internal->client_cache, &list))
     return NULL;
 
   if (!silc_idcache_list_count(list)) {
@@ -548,7 +558,7 @@ silc_client_list_private_message_keys(SilcClient client,
 
     if (entry->send_key) {
       keys[count].client_entry = entry;
-      keys[count].cipher = entry->send_key->cipher->name;
+      keys[count].cipher = (char *)silc_cipher_get_name(entry->send_key);
       keys[count].key = entry->generated == FALSE ? entry->key : NULL;
       keys[count].key_len = entry->generated == FALSE ? entry->key_len : 0;
       count++;
@@ -585,17 +595,19 @@ void silc_client_set_away_message(SilcClient client,
                                  SilcClientConnection conn,
                                  char *message)
 {
-  if (!message && conn->away) {
-    silc_free(conn->away->away);
-    silc_free(conn->away);
-    conn->away = NULL;
+  assert(client && conn);
+
+  if (!message && conn->internal->away) {
+    silc_free(conn->internal->away->away);
+    silc_free(conn->internal->away);
+    conn->internal->away = NULL;
   }
 
   if (message) {
-    if (!conn->away)
-      conn->away = silc_calloc(1, sizeof(*conn->away));
-    if (conn->away->away)
-      silc_free(conn->away->away);
-    conn->away->away = strdup(message);
+    if (!conn->internal->away)
+      conn->internal->away = silc_calloc(1, sizeof(*conn->internal->away));
+    if (conn->internal->away->away)
+      silc_free(conn->internal->away->away);
+    conn->internal->away->away = strdup(message);
   }
 }