Merge commit 'origin/silc.1.1.branch'
[silc.git] / lib / silcsftp / silcsftp.h
index ef5735a8cc097ac37b5306a0a5e8a25e22555de6..d2364146c79f39c5f0254ce317efb6930748471f 100644 (file)
@@ -1,10 +1,10 @@
 /*
 
-  silcsftp.h 
+  silcsftp.h
 
   Author: Pekka Riikonen <priikone@silcnet.org>
 
-  Copyright (C) 2001 Pekka Riikonen
+  Copyright (C) 2001 - 2007 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
  *
  * DESCRIPTION
  *
- *    SILC SFTP Interface is the implementation of the SSH File Transfer
- *    Protocol.  The interface defines the SFTP client and the SFTP server.
- *    The SFTP is the mandatory file transfer protocol in the SILC protocol.
- *    The SFTP server implementation is filesystem independent and generic
- *    interface is defined to represent filesystem access.
+ * SILC SFTP Interface is the implementation of the Secure File Transfer
+ * Protocol (or SSH File Transfer Protocol).  The interface defines the SFTP
+ * client and the SFTP server.  The SFTP is the mandatory file transfer
+ * protocol in the SILC protocol, and when used in SILC the SFTP packets are
+ * encapsulated into SILC packets.  The SFTP server implementation is
+ * filesystem independent and generic interface is defined to represent
+ * filesystem access.
  *
- *    The SilcSFTP context is the actual SFTP client or SFTP server, and
- *    each SFTP session (associated to a socket connection) must create
- *    own SFTP context.
+ * The SilcSFTP context is the actual SFTP client or SFTP server, and each
+ * SFTP session should create its own SFTP context.
+ *
+ * The SILC SFTP library is a generic SFTP implementation and not directly
+ * related to either SILC or SSH.  It could be used for any general purpose
+ * SFTP application.
  *
  ***/
 
 /****s* silcsftp/SilcSFTPAPI/SilcSFTP
  *
  * NAME
- * 
+ *
  *    typedef struct SilcSFTPStruct *SilcSFTP;
  *
  * DESCRIPTION
@@ -47,7 +52,7 @@
  *    This context is the actual SFTP client and SFTP server, and is
  *    allocated by silc_sftp_client_start or silc_sftp_server_start and
  *    given as argument usually to all silc_sftp_* functions.  It is freed
- *    by the silc_sftp_client_shutdown or silc_sftp_server_shutdown 
+ *    by the silc_sftp_client_shutdown or silc_sftp_server_shutdown
  *    functions.
  *
  ***/
@@ -56,17 +61,15 @@ typedef struct SilcSFTPStruct *SilcSFTP;
 /****d* silcsftp/SilcSFTPAPI/SilcSFTPVersion
  *
  * NAME
- * 
+ *
  *    typedef SilcUInt32 SilcSFTPVersion;
  *
  * DESCRIPTION
  *
  *    SFTP Version type.
  *
- * SOURCE
- */
+ ***/
 typedef SilcUInt32 SilcSFTPVersion;
-/***/
 
 /* SFTP protocol version */
 #define SILC_SFTP_PROTOCOL_VERSION       3
@@ -74,7 +77,7 @@ typedef SilcUInt32 SilcSFTPVersion;
 /****d* silcsftp/SilcSFTPAPI/SilcSFTPStatus
  *
  * NAME
- * 
+ *
  *    typedef enum { ... } SilcSFTPStatus
  *
  * DESCRIPTION
@@ -92,16 +95,36 @@ typedef enum {
   SILC_SFTP_STATUS_PERMISSION_DENIED   = 3,  /* No sufficient permissions */
   SILC_SFTP_STATUS_FAILURE             = 4,  /* Operation failed */
   SILC_SFTP_STATUS_BAD_MESSAGE         = 5,  /* Bad message received */
-  SILC_SFTP_STATUS_NO_CONNECTION       = 6,  /* No connection to server */
+  SILC_SFTP_STATUS_NO_CONNECTION       = 6,  /* No connection to remote */
   SILC_SFTP_STATUS_CONNECTION_LOST     = 7,  /* Connection lost to server */
   SILC_SFTP_STATUS_OP_UNSUPPORTED      = 8,  /* Operation unsupported */
+  SILC_SFTP_STATUS_INVALID_HANDLE      = 9,  /* Invalid file handle */
+  SILC_SFTP_STATUS_NO_SUCH_PATH        = 10, /* Path does not exist */
+  SILC_SFTP_STATUS_FILE_ALREADY_EXIST  = 11, /* File already exists */
+  SILC_SFTP_STATUS_WRITE_PROTECT       = 12, /* Read-only or protected */
+  SILC_SFTP_STATUS_NO_MEDIA            = 13, /* No media available */
+  SILC_SFTP_STATUS_NO_SPACE_ON_DEVICE  = 14, /* No space on device */
+  SILC_SFTP_STATUS_QUOTA_EXCEEDED      = 15, /* Quota limit reached */
+  SILC_SFTP_STATUS_UNKNOWN_PRINCIBLE   = 16, /* Unknown princible */
+  SILC_SFTP_STATUS_LOCK_CONFLICT       = 17, /* File already locked */
+  SILC_SFTP_STATUS_NOT_EMPTY           = 18, /* Directory not empty */
+  SILC_SFTP_STATUS_NOT_A_DIRECTORY     = 19, /* Not a directory */
+  SILC_SFTP_STATUS_INVALID_FILENAME    = 20, /* Invalid filename */
+  SILC_SFTP_STATUS_LINK_LOOP           = 21, /* Too many symlinks */
+  SILC_SFTP_STATUS_CANNOT_DELETE       = 22, /* Could not delete file */
+  SILC_SFTP_STATUS_INVALID_PARAMETER   = 23, /* Invalid parameter */
+  SILC_SFTP_STATUS_FILE_IS_A_DIRECTORY = 24, /* File is a directory file */
+  SILC_SFTP_STATUS_BR_LOCK_CONFLICT    = 25, /* Byte range lock conflict */
+  SILC_SFTP_STATUS_BR_LOCK_REFUSED     = 26, /* Byte range lock refused */
+  SILC_SFTP_STATUS_DELETE_PENDING      = 27, /* File is being deleted */
+  SILC_SFTP_STATUS_FILE_CORRUPT        = 28, /* File is corrupted */
 } SilcSFTPStatus;
 /***/
 
 /****d* silcsftp/SilcSFTPAPI/SilcSFTPFileOperation
  *
  * NAME
- * 
+ *
  *    typedef enum { ... } SilcSFTPFileOperation
  *
  * DESCRIPTION
@@ -125,24 +148,24 @@ typedef enum {
 /****s* silcsftp/SilcSFTPAPI/SilcSFTPAttributes
  *
  * NAME
- * 
+ *
  *    typedef struct { ... } *SilcSFTPAttributes, SilcSFTPAttributesStruct;
  *
  * DESCRIPTION
  *
  *    SFTP File attributes structure represents the attributes for a file.
- *    This structure can be used by the client to send attributes to the 
+ *    This structure can be used by the client to send attributes to the
  *    server, and by server to return file attributes to the client.
  *
  ***/
-typedef struct {
-  SilcUInt32 flags;                    /* Flags to indicate present attributes */
-  SilcUInt64 size;                     /* Sife of the file in bytes */
-  SilcUInt32 uid;                      /* Unix user ID */
-  SilcUInt32 gid;                      /* Unix group ID */
-  SilcUInt32 permissions;              /* POSIX file permission bitmask */
-  SilcUInt32 atime;                    /* Access time of file */
-  SilcUInt32 mtime;                    /* Modification time of file */
+typedef struct SilcSFTPAttributesObject {
+  SilcUInt32 flags;            /* Flags to indicate present attributes */
+  SilcUInt64 size;             /* Sife of the file in bytes */
+  SilcUInt32 uid;                      /* Unix user ID */
+  SilcUInt32 gid;                      /* Unix group ID */
+  SilcUInt32 permissions;              /* POSIX file permission bitmask */
+  SilcUInt32 atime;                    /* Access time of file */
+  SilcUInt32 mtime;            /* Modification time of file */
 
   SilcUInt32 extended_count;   /* Extended type and data count */
   SilcBuffer *extended_type;
@@ -152,7 +175,7 @@ typedef struct {
 /****s* silcsftp/SilcSFTPAPI/SilcSFTPName
  *
  * NAME
- * 
+ *
  *    typedef struct { ... } *SilcSFTPName, SilcSFTPNameStruct
  *
  * DESCRIPTION
@@ -163,7 +186,7 @@ typedef struct {
  *    example when reading the contents of a directory.
  *
  ***/
-typedef struct  {
+typedef struct SilcSFTPNameObject {
   char **filename;
   char **long_filename;
   SilcSFTPAttributes *attrs;
@@ -173,7 +196,7 @@ typedef struct  {
 /****s* silcsftp/SilcSFTPAPI/SilcSFTPHandle
  *
  * NAME
- * 
+ *
  *    typedef struct SilcSFTPHandleStruct *SilcSFTPHandle;
  *
  * DESCRIPTION
@@ -185,25 +208,6 @@ typedef struct  {
  ***/
 typedef struct SilcSFTPHandleStruct *SilcSFTPHandle;
 
-/****f* silcsftp/SilcSFTPAPI/SilcSFTPSendPacketCallback
- *
- * SYNOPSIS
- *
- *    typedef void (*SilcSFTPSendPacketCallback)(SilcSocketConnection sock,
- *                                               SilcBuffer packet, 
- *                                               void *context);
- *
- * DESCRIPTION
- *
- *    Packet sending callback. The caller of this interface will provide this
- *    function for the library. The libary will call this function everytime
- *    it needs to send a packet to the socket connection indicated by the
- *    `sock'. 
- *
- ***/
-typedef void (*SilcSFTPSendPacketCallback)(SilcSocketConnection sock,
-                                          SilcBuffer packet, void *context);
-
 /****f* silcsftp/SilcSFTPAPI/SilcSFTPVersionCallback
  *
  * SYNOPSIS
@@ -225,6 +229,27 @@ typedef void (*SilcSFTPVersionCallback)(SilcSFTP sftp,
                                        SilcSFTPVersion version,
                                        void *context);
 
+/****f* silcsftp/SilcSFTPAPI/SilcSFTPErrorCallback
+ *
+ * SYNOPSIS
+ *
+ *    typedef void (*SilcSFTPErrorCallback)(SilcSFTP sftp,
+ *                                          SilcSFTPStatus status,
+ *                                          void *context);
+ *
+ * DESCRIPTION
+ *
+ *    Error callback is called if a connection error occurs during SFTP
+ *    session.  If the connection or stream is closed this callback is
+ *    called.  Other errors are delivered in other callbacks.  Only the
+ *    SILC_SFTP_STATUS_EOF or SILC_SFTP_STATUS_NO_CONNECTION is delivered
+ *    in this callback.
+ *
+ ***/
+typedef void (*SilcSFTPErrorCallback)(SilcSFTP sftp,
+                                     SilcSFTPStatus status,
+                                     void *context);
+
 /****f* silcsftp/SilcSFTPAPI/SilcSFTPStatusCallback
  *
  * SYNOPSIS
@@ -369,26 +394,30 @@ typedef void (*SilcSFTPExtendedCallback)(SilcSFTP sftp,
  *
  * SYNOPSIS
  *
- *    SilcSFTP silc_sftp_client_start(SilcSocketConnection sock,
- *                                    SilcSFTPSendPacketCallback send_packet,
- *                                    void *send_context,
- *                                    SilcSFTPVersionCallback callback,
+ *    SilcSFTP silc_sftp_client_start(SilcStream stream,
+ *                                    SilcSchedule schedule,
+ *                                    SilcSFTPVersionCallback version_cb,
+ *                                    SilcSFTPErrorCallback error_cb,
  *                                    void *context);
  *
  * DESCRIPTION
  *
- *    Starts SFTP client by associating the socket connection `sock' to the
- *    created SFTP client context.  The version callback indicated by the
- *    `callback' will be called after the SFTP session has been started
- *    and server has returned the version of the protocol.  The SFTP client
- *    context is returned in the callback too.  This returns the allocated
- *    SFTP client context or NULL on error.
+ *    Starts SFTP client and returns context to it.  The version callback
+ *    indicated by the `version_cb' will be called after the SFTP session has
+ *    been started and server has returned the version of the protocol.  The
+ *    SFTP client context is returned in the callback too.  This returns the
+ *    allocated SFTP client context or NULL on error.  The `stream' will be
+ *    used to read and write the SFTP packets.  The `error_cb' will be called
+ *    in case a stream error occurs, such as end of stream.
+ *
+ *    If `schedule' is NULL this will call silc_schedule_get_global to try to
+ *    get global scheduler.
  *
  ***/
-SilcSFTP silc_sftp_client_start(SilcSocketConnection sock,
-                               SilcSFTPSendPacketCallback send_packet,
-                               void *send_context,
-                               SilcSFTPVersionCallback callback,
+SilcSFTP silc_sftp_client_start(SilcStream stream,
+                               SilcSchedule schedule,
+                               SilcSFTPVersionCallback version_cb,
+                               SilcSFTPErrorCallback error_cb,
                                void *context);
 
 /****f* silcsftp/SilcSFTPAPI/silc_sftp_client_shutdown
@@ -400,24 +429,17 @@ SilcSFTP silc_sftp_client_start(SilcSocketConnection sock,
  * DESCRIPTION
  *
  *    Shutdown's the SFTP client.  The caller is responsible of closing
- *    the associated socket connection.  The SFTP context is freed and is
- *    invalid after this function returns.
+ *    the associated stream.  The SFTP context is freed and is invalid after
+ *    this function returns.
  *
  ***/
 void silc_sftp_client_shutdown(SilcSFTP sftp);
 
-/* Function that is called to process the incmoing SFTP packet. */
-/* XXX Some day this will go away and we have automatic receive callbacks
-   for SilcSocketConnection API or SilcPacketContext API. */
-void silc_sftp_client_receive_process(SilcSFTP sftp,
-                                     SilcSocketConnection sock,
-                                     SilcPacketContext *packet);
-
 /****f* silcsftp/SilcSFTPAPI/silc_sftp_open
  *
  * SYNOPSIS
  *
- *    void silc_sftp_open(SilcSFTP sftp, 
+ *    void silc_sftp_open(SilcSFTP sftp,
  *                        const char *filename,
  *                        SilcSFTPFileOperation pflags,
  *                        SilcSFTPAttributes attrs,
@@ -431,7 +453,7 @@ void silc_sftp_client_receive_process(SilcSFTP sftp,
  *    `callback' to return the opened file handle.
  *
  ***/
-void silc_sftp_open(SilcSFTP sftp, 
+void silc_sftp_open(SilcSFTP sftp,
                    const char *filename,
                    SilcSFTPFileOperation pflags,
                    SilcSFTPAttributes attrs,
@@ -464,7 +486,7 @@ void silc_sftp_close(SilcSFTP sftp,
  *
  *    void silc_sftp_read(SilcSFTP sftp,
  *                        SilcSFTPHandle handle,
- *                        SilcUInt64 offset, 
+ *                        SilcUInt64 offset,
  *                        SilcUInt32 len,
  *                        SilcSFTPDataCallback callback,
  *                        void *context);
@@ -478,7 +500,7 @@ void silc_sftp_close(SilcSFTP sftp,
  ***/
 void silc_sftp_read(SilcSFTP sftp,
                    SilcSFTPHandle handle,
-                   SilcUInt64 offset, 
+                   SilcUInt64 offset,
                    SilcUInt32 len,
                    SilcSFTPDataCallback callback,
                    void *context);
@@ -498,7 +520,7 @@ void silc_sftp_read(SilcSFTP sftp,
  * DESCRIPTION
  *
  *    Writes to a file indicated by the file handle `handle' starting from
- *    offset of `offset' at most `data_len' bytes of `data'.  The `callback' 
+ *    offset of `offset' at most `data_len' bytes of `data'.  The `callback'
  *    is called to indicate the status of the writing.
  *
  ***/
@@ -819,7 +841,7 @@ void silc_sftp_realpath(SilcSFTP sftp,
  *
  * DESCRIPTION
  *
- *    Performs an extended operation indicated by the `request' with 
+ *    Performs an extended operation indicated by the `request' with
  *    optional extended operation data indicated by the `data'.  The callback
  *    is called to return any data associated with the extended request.
  *
@@ -840,23 +862,29 @@ void silc_sftp_extended(SilcSFTP sftp,
  *
  * SYNOPSIS
  *
- *    SilcSFTP silc_sftp_server_start(SilcSocketConnection sock,
- *                                    SilcSFTPSendPacketCallback send_packet,
- *                                    void *send_context, 
+ *    SilcSFTP silc_sftp_server_start(SilcStream stream,
+ *                                    SilcSchedule schedule,
+ *                                    SilcSFTPErrorCallback error_cb,
+ *                                    void *context,
  *                                    SilcSFTPFilesystem fs);
  *
  * DESCRIPTION
  *
- *    Starts SFTP server by associating the socket connection `sock' to the
- *    created SFTP server context.  This function returns the allocated
- *    SFTP client context or NULL on error. The `send_packet' is called
- *    by the library when it needs to send a packet. The `fs' is the
+ *    Starts SFTP server and returns a context to it.  This function returns
+ *    the allocated SFTP server context or NULL on error.  The `stream' is
+ *    the stream (connection) to the client.  The `error_cb' will be called
+ *    when the `stream' is ended (SILC_SFTP_STATUS_EOF).  The caller is
+ *    responsible of closing and destroying the `stream'.  The `fs' is the
  *    filesystem context allocated by the application.
  *
+ *    If `schedule' is NULL this will call silc_schedule_get_global to try to
+ *    get global scheduler.
+ *
  ***/
-SilcSFTP silc_sftp_server_start(SilcSocketConnection sock,
-                               SilcSFTPSendPacketCallback send_packet,
-                               void *send_context, 
+SilcSFTP silc_sftp_server_start(SilcStream stream,
+                               SilcSchedule schedule,
+                               SilcSFTPErrorCallback error_cb,
+                               void *context,
                                SilcSFTPFilesystem fs);
 
 /****f* silcsftp/SilcSFTPAPI/silc_sftp_server_shutdown
@@ -867,9 +895,9 @@ SilcSFTP silc_sftp_server_start(SilcSocketConnection sock,
  *
  * DESCRIPTION
  *
- *    Shutdown's the SFTP server.  The caller is responsible of closing
- *    the associated socket connection.  The SFTP context is freed and is
- *    invalid after this function returns.
+ *    Shutdown the SFTP server.  The caller is responsible of closing the
+ *    associated stream.  The SFTP context is freed and is invalid after
+ *    this function returns.
  *
  ***/
 void silc_sftp_server_shutdown(SilcSFTP sftp);
@@ -877,7 +905,7 @@ void silc_sftp_server_shutdown(SilcSFTP sftp);
 /****d* silcsftp/SilcSFTPAPI/SilcSFTPMonitors
  *
  * NAME
- * 
+ *
  *    typedef enum { ... } SilcSFTPMonitors;
  *
  * DESCRIPTION
@@ -914,18 +942,19 @@ typedef enum {
 /****s* silcsftp/SilcSFTPAPI/SilcSFTPMonitorData
  *
  * NAME
- * 
+ *
  *    typedef struct { ... } *SilcSFTPMonitorData, SilcSFTPMonitorDataStruct;
  *
  * DESCRIPTION
  *
  *    This structure includes the monitor type specific data.  The
  *    application can check what the client has requested from this
- *    structure.
+ *    structure.  See the comments below what data is available for what
+ *    monitor type.
  *
  * SOURCE
  */
-typedef struct {
+typedef struct SilcSFTPMonitorDataObject {
   SilcSFTPVersion version;     /* _INIT */
   char *name;                  /* _OPEN, _REMOVE, _RENAME, _MKDIR,
                                   _RMDIR, _OPENDIR, _STAT, _LSTAT,
@@ -965,26 +994,19 @@ typedef void (*SilcSFTPMonitor)(SilcSFTP sftp,
  *
  *    void silc_sftp_server_set_monitor(SilcSFTP sftp,
  *                                      SilcSFTPMonitors monitors,
- *                                      SilcSFTPMonitor monitor, 
+ *                                      SilcSFTPMonitor monitor,
  *                                      void *context);
  *
  * DESCRIPTION
  *
- *    Sets monitor callback to monitor various request sent by an client.
+ *    Sets monitor callback to monitor various request sent by a client.
  *    When request that has been set in the `monitors' is received the
  *    monitor callback will be called to notify the caller.
  *
  ***/
 void silc_sftp_server_set_monitor(SilcSFTP sftp,
                                  SilcSFTPMonitors monitors,
-                                 SilcSFTPMonitor monitor, 
+                                 SilcSFTPMonitor monitor,
                                  void *context);
 
-/* Function that is called to process the incmoing SFTP packet. */
-/* XXX Some day this will go away and we have automatic receive callbacks
-   for SilcSocketConnection API or SilcPacketContext API. */
-void silc_sftp_server_receive_process(SilcSFTP sftp,
-                                     SilcSocketConnection sock,
-                                     SilcPacketContext *packet);
-
 #endif /* SILCSFTP_H */