Fixed connection authentication protocol aborting.
[silc.git] / lib / silcske / silcconnauth.c
index 6947095b880f899a48bcb62a70a26918ea55b8f4..ecd515f084363d3f097dcfa442e60fba27a19132 100644 (file)
@@ -32,6 +32,7 @@ static SilcBool silc_connauth_packet_receive(SilcPacketEngine engine,
 struct SilcConnAuthStruct {
   SilcSKE ske;
   SilcFSM fsm;
+  SilcAsyncOperationStruct op;
   SilcConnectionType conn_type;
   SilcAuthMethod auth_method;
   void *auth_data;
@@ -226,6 +227,7 @@ SilcConnAuth silc_connauth_alloc(SilcSchedule schedule,
 
   connauth->timeout_secs = timeout_secs;
   connauth->ske = ske;
+  ske->refcnt++;
 
   return connauth;
 }
@@ -236,6 +238,10 @@ void silc_connauth_free(SilcConnAuth connauth)
 {
   if (connauth->public_keys)
     silc_dlist_uninit(connauth->public_keys);
+
+  /* Free reference */
+  silc_ske_free(connauth->ske);
+
   silc_free(connauth);
 }
 
@@ -373,12 +379,14 @@ SILC_FSM_STATE(silc_connauth_st_initiator_failure)
 
   SILC_LOG_DEBUG(("Start"));
 
-  /* Send FAILURE packet */
-  SILC_PUT32_MSB(SILC_AUTH_FAILED, error);
-  silc_packet_send(connauth->ske->stream, SILC_PACKET_FAILURE, 0, error, 4);
+  if (!connauth->aborted) {
+    /* Send FAILURE packet */
+    SILC_PUT32_MSB(SILC_AUTH_FAILED, error);
+    silc_packet_send(connauth->ske->stream, SILC_PACKET_FAILURE, 0, error, 4);
 
-  /* Call completion callback */
-  connauth->completion(connauth, FALSE, connauth->context);
+    /* Call completion callback */
+    connauth->completion(connauth, FALSE, connauth->context);
+  }
 
   silc_packet_stream_unlink(connauth->ske->stream,
                            &silc_connauth_stream_cbs, connauth);
@@ -395,8 +403,6 @@ silc_connauth_initiator(SilcConnAuth connauth,
                        SilcConnAuthCompletion completion,
                        void *context)
 {
-  SilcAsyncOperation op;
-
   SILC_LOG_DEBUG(("Connection authentication as initiator"));
 
   if (auth_method == SILC_AUTH_PASSWORD && !auth_data) {
@@ -423,10 +429,10 @@ silc_connauth_initiator(SilcConnAuth connauth,
                          SILC_PACKET_FAILURE, -1);
 
   /* Start the protocol */
-  op = silc_async_alloc(silc_connauth_abort, NULL, connauth);
+  silc_async_init(&connauth->op, silc_connauth_abort, NULL, connauth);
   silc_fsm_start(connauth->fsm, silc_connauth_st_initiator_start);
 
-  return op;
+  return &connauth->op;
 }
 
 
@@ -664,12 +670,14 @@ SILC_FSM_STATE(silc_connauth_st_responder_failure)
 
   SILC_LOG_ERROR(("Authentication failed"));
 
-  /* Send FAILURE packet */
-  SILC_PUT32_MSB(SILC_AUTH_FAILED, error);
-  silc_packet_send(connauth->ske->stream, SILC_PACKET_FAILURE, 0, error, 4);
+  if (!connauth->aborted) {
+    /* Send FAILURE packet */
+    SILC_PUT32_MSB(SILC_AUTH_FAILED, error);
+    silc_packet_send(connauth->ske->stream, SILC_PACKET_FAILURE, 0, error, 4);
 
-  /* Call completion callback */
-  connauth->completion(connauth, FALSE, connauth->context);
+    /* Call completion callback */
+    connauth->completion(connauth, FALSE, connauth->context);
+  }
 
   silc_packet_stream_unlink(connauth->ske->stream,
                            &silc_connauth_stream_cbs, connauth);
@@ -684,8 +692,6 @@ silc_connauth_responder(SilcConnAuth connauth,
                        SilcConnAuthCompletion completion,
                        void *context)
 {
-  SilcAsyncOperation op;
-
   SILC_LOG_DEBUG(("Connection authentication as responder"));
 
   connauth->get_auth_data = get_auth_data;
@@ -699,8 +705,8 @@ silc_connauth_responder(SilcConnAuth connauth,
                          SILC_PACKET_FAILURE, -1);
 
   /* Start the protocol */
-  op = silc_async_alloc(silc_connauth_abort, NULL, connauth);
+  silc_async_init(&connauth->op, silc_connauth_abort, NULL, connauth);
   silc_fsm_start(connauth->fsm, silc_connauth_st_responder_start);
 
-  return op;
+  return &connauth->op;
 }