silcclient: fix packet stream coder function
[silc.git] / lib / silcclient / client_ftp.c
index 42e22d15189e40564bc3126c5ebdb23d280c719c..fc1aef48d8bdb37610dfef994b608fef81768a1e 100644 (file)
@@ -4,7 +4,7 @@
 
   Author: Pekka Riikonen <priikone@silcnet.org>
 
-  Copyright (C) 2001 - 2007 Pekka Riikonen
+  Copyright (C) 2001 - 2014 Pekka Riikonen
 
   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
@@ -108,7 +108,7 @@ static void silc_client_ftp_data(SilcSFTP sftp,
     silc_sftp_close(sftp, session->read_handle, NULL, NULL);
     session->read_handle = NULL;
 
-    /* Close the read file descriptor */
+    /* Close the real file descriptor */
     silc_file_close(session->fd);
     return;
   }
@@ -130,7 +130,7 @@ static void silc_client_ftp_data(SilcSFTP sftp,
     silc_sftp_close(sftp, session->read_handle, NULL, NULL);
     session->read_handle = NULL;
 
-    /* Close the read file descriptor */
+    /* Close the real file descriptor */
     silc_file_close(session->fd);
     return;
   }
@@ -189,11 +189,14 @@ static void silc_client_ftp_open_handle(SilcSFTP sftp,
   session->fd = silc_file_open(path, O_RDWR | O_CREAT | O_EXCL);
   if (session->fd < 0) {
     /* Call monitor callback */
+    session->conn->context_type = SILC_ID_CLIENT;
+    session->conn->client_entry = session->client_entry;
     session->client->internal->ops->say(session->client, session->conn,
                                        SILC_CLIENT_MESSAGE_ERROR,
                                        "File `%s' open failed: %s",
                                        session->filepath,
                                        strerror(errno));
+    session->conn->context_type = SILC_ID_NONE;
 
     if (session->monitor)
       (*session->monitor)(session->client, session->conn,
@@ -380,6 +383,8 @@ static void silc_client_ftp_session_free(SilcClientFtpSession session)
 {
   SILC_LOG_DEBUG(("Free session %d", session->session_id));
 
+  silc_schedule_task_del_by_context(session->client->schedule, session);
+
   silc_dlist_del(session->client->internal->ftp_sessions, session);
 
   /* Abort connecting  */
@@ -493,6 +498,7 @@ silc_client_ftp_coder(SilcStream stream, SilcStreamStatus status,
 
   /* Add FTP type before SFTP data */
   if (status == SILC_STREAM_CAN_WRITE) {
+    silc_buffer_push(buffer, 1);
     if (silc_buffer_format(buffer,
                           SILC_STR_UI_CHAR(1),
                           SILC_STR_END) < 0)
@@ -526,7 +532,7 @@ silc_client_ftp_connect_completion(SilcClient client,
 
     /* Wrap the connection packet stream */
     session->stream = silc_packet_stream_wrap(conn->stream, SILC_PACKET_FTP,
-                                             0, FALSE,
+                                             0, FALSE, 0, NULL, 0, NULL,
                                              silc_client_ftp_coder, session);
     if (!session->stream) {
       /* Call monitor callback */
@@ -734,9 +740,12 @@ silc_client_file_send(SilcClient client,
                               silc_client_ftp_connect_completion,
                               session);
     if (!session->listener) {
+      conn->context_type = SILC_ID_CLIENT;
+      conn->client_entry = session->client_entry;
       client->internal->ops->say(client, conn, SILC_CLIENT_MESSAGE_ERROR,
                                 "Cannot create listener for file transfer: "
                                 "%s", strerror(errno));
+      conn->context_type = SILC_ID_NONE;
       silc_free(session);
       return SILC_CLIENT_FILE_NO_MEMORY;
     }
@@ -853,6 +862,14 @@ silc_client_file_receive(SilcClient client,
     /* Add the listener for the key agreement */
     SILC_LOG_DEBUG(("Creating listener for file transfer"));
     if (!params || (!params->local_ip && !params->bind_ip)) {
+      session->conn->context_type = SILC_ID_CLIENT;
+      session->conn->client_entry = session->client_entry;
+      session->client->internal->ops->say(session->client, session->conn,
+                                         SILC_CLIENT_MESSAGE_ERROR,
+                                         "Cannot create listener for file "
+                                         "transfer; IP address and/or port "
+                                         "not provided");
+      session->conn->context_type = SILC_ID_NONE;
       silc_free(session);
       return SILC_CLIENT_FILE_ERROR;
     }
@@ -862,9 +879,12 @@ silc_client_file_receive(SilcClient client,
                               silc_client_ftp_connect_completion,
                               session);
     if (!session->listener) {
+      session->conn->context_type = SILC_ID_CLIENT;
+      session->conn->client_entry = session->client_entry;
       client->internal->ops->say(client, conn, SILC_CLIENT_MESSAGE_ERROR,
                                 "Cannot create listener for file transfer: "
                                 "%s", strerror(errno));
+      session->conn->context_type = SILC_ID_NONE;
       silc_free(session);
       return SILC_CLIENT_FILE_NO_MEMORY;
     }
@@ -967,7 +987,7 @@ SILC_FSM_STATE(silc_client_ftp)
   SilcClientFtpSession session;
   SilcClientID remote_id;
   SilcClientEntry remote_client;
-  SilcKeyAgreementPayload payload;
+  SilcKeyAgreementPayload payload = NULL;
   char *hostname;
   SilcUInt16 port;
 
@@ -1079,6 +1099,8 @@ SILC_FSM_STATE(silc_client_ftp)
   }
 
  out:
+  if (payload)
+    silc_key_agreement_payload_free(payload);
   silc_packet_free(packet);
   return SILC_FSM_FINISH;
 }