Added asynchronous event tasks to SILC Scheduler. Added
[crypto.git] / lib / silcclient / client.c
index af9954973c2fe549e5d468e73e561bd7f58aba03..c27d055e1e3e230ac2a65ae7bd54d5094761fd15 100644 (file)
@@ -149,7 +149,15 @@ static void silc_client_packet_error(SilcPacketEngine engine,
                                     void *callback_context,
                                     void *stream_context)
 {
-  /* Nothing */
+  SilcClient client = callback_context;
+  SilcClientConnection conn = stream_context;
+
+  /* Read and write errors are silent */
+  if (error == SILC_PACKET_ERR_READ || error == SILC_PACKET_ERR_WRITE)
+    return;
+
+  client->internal->ops->say(client, conn, SILC_CLIENT_MESSAGE_ERROR,
+                            (char *)silc_packet_error_string(error));
 }
 
 /* Packet stream callbacks */
@@ -515,11 +523,13 @@ SILC_FSM_STATE(silc_client_st_run)
 
   /* Process events */
 
-  if (client->internal->run_callback && client->internal->running) {
+  if (client->internal->run_callback) {
     /* Call running callbcak back to application */
-    SILC_LOG_DEBUG(("We are up, call running callback"));
     client->internal->run_callback = FALSE;
-    client->internal->running(client, client->internal->running_context);
+    if (client->internal->running) {
+      SILC_LOG_DEBUG(("We are up, call running callback"));
+      client->internal->running(client, client->internal->running_context);
+    }
     return SILC_FSM_CONTINUE;
   }
 
@@ -770,6 +780,12 @@ silc_client_connect_to_server(SilcClient client,
   if (!client || !remote_host)
     return NULL;
 
+  if (client->internal->run_callback) {
+    SILC_LOG_ERROR(("Client library is not started yet. SilcClientRunning "
+                   "callback has not been called yet."));
+    return NULL;
+  }
+
   /* Add new connection */
   conn = silc_client_add_connection(client, SILC_CONN_SERVER, TRUE, params,
                                    public_key, private_key, remote_host,
@@ -807,6 +823,12 @@ silc_client_connect_to_client(SilcClient client,
   if (!client || !remote_host)
     return NULL;
 
+  if (client->internal->run_callback) {
+    SILC_LOG_ERROR(("Client library is not started yet. SilcClientRunning "
+                   "callback has not been called yet."));
+    return NULL;
+  }
+
   if (params)
     params->no_authentication = TRUE;
 
@@ -846,6 +868,12 @@ silc_client_key_exchange(SilcClient client,
   if (!client || !stream)
     return NULL;
 
+  if (client->internal->run_callback) {
+    SILC_LOG_ERROR(("Client library is not started yet. SilcClientRunning "
+                   "callback has not been called yet."));
+    return NULL;
+  }
+
   if (!silc_socket_stream_get_info(stream, NULL, &host, NULL, &port)) {
     SILC_LOG_ERROR(("Socket stream does not have remote host name set"));
     callback(client, NULL, SILC_CLIENT_CONN_ERROR, 0, NULL, context);
@@ -932,12 +960,8 @@ void silc_client_free(SilcClient client)
   if (client->rng)
     silc_rng_free(client->rng);
 
-  if (!client->internal->params->dont_register_crypto_library) {
-    silc_cipher_unregister_all();
-    silc_pkcs_unregister_all();
-    silc_hash_unregister_all();
-    silc_hmac_unregister_all();
-  }
+  if (!client->internal->params->dont_register_crypto_library)
+    silc_crypto_uninit();
 
   silc_packet_engine_stop(client->internal->packet_engine);
   silc_dlist_uninit(client->internal->ftp_sessions);
@@ -1003,15 +1027,10 @@ SilcBool silc_client_init(SilcClient client, const char *username,
   if (!client->internal->ftp_sessions)
     return FALSE;
 
-  if (!client->internal->params->dont_register_crypto_library) {
+  if (!client->internal->params->dont_register_crypto_library)
     /* Initialize the crypto library.  If application has done this already
-       this has no effect.  Also, we will not be overriding something
-       application might have registered earlier. */
-    silc_cipher_register_default();
-    silc_pkcs_register_default();
-    silc_hash_register_default();
-    silc_hmac_register_default();
-  }
+       this has no effect. */
+    silc_crypto_init(NULL);
 
   /* Initialize random number generator */
   client->rng = silc_rng_alloc();
@@ -1021,7 +1040,7 @@ SilcBool silc_client_init(SilcClient client, const char *username,
   silc_rng_global_init(client->rng);
 
   /* Initialize the scheduler */
-  client->schedule = silc_schedule_init(0, client);
+  client->schedule = silc_schedule_init(0, client, NULL, NULL);
   if (!client->schedule)
     return FALSE;
 
@@ -1079,6 +1098,12 @@ void silc_client_stop(SilcClient client, SilcClientStopped stopped,
 {
   SILC_LOG_DEBUG(("Stopping client"));
 
+  if (!silc_fsm_is_started(&client->internal->fsm)) {
+    SILC_LOG_WARNING(("Attempting to stop client library before it has been "
+                     "started (silc_client_init not called)"));
+    return;
+  }
+
   client->internal->running = (SilcClientRunning)stopped;
   client->internal->running_context = context;