TrustCore SDK NanoSSH API reference  version 7.0
SFTP Server Messaging Functions

Functions

MOC_EXTERN void SSH_sftpNumBytesRead (void *sftpInternelDescr, sbyte4 numBytesRead)
 Set an sftp file descriptor's $numBytesRead value to the number of bytes read from the incoming socket. More...
 
MOC_EXTERN sbyte * SSH_sftpReadBuffer (void *sftpInternelDescr)
 Get a pointer to a buffer containing a file's read data. More...
 
MOC_EXTERN sbyte4 SSH_sftpReadBufferSize (void *sftpInternelDescr)
 Get the number of read bytes a client is requesting. More...
 
MOC_EXTERN sbyte4 SSH_sftpReadLocation (void *sftpInternelDescr)
 Get a file's read byte position. More...
 
MOC_EXTERN sbyte * SSH_sftpWriteBuffer (void *sftpInternelDescr)
 Get a pointer to a buffer (received from an SFTP client) containing data to write to a file. More...
 
MOC_EXTERN sbyte4 SSH_sftpWriteBufferSize (void *sftpInternelDescr)
 Get the number of bytes written to a file by an SFTP write upcall handler. More...
 
MOC_EXTERN sbyte4 SSH_sftpWriteLocation (void *sftpInternelDescr)
 Get a file's write byte position. More...
 

Detailed Description

Function Documentation

◆ SSH_sftpNumBytesRead()

MOC_EXTERN void SSH_sftpNumBytesRead ( void *  sftpInternelDescr,
sbyte4  numBytesRead 
)

This function sets an sftp file descriptor's numBytesRead value to the number of bytes read from the incoming socket.

Since
1.41
Version
1.41 and later

To enable this function, the following flags must be defined in moptions.h:

  • __ENABLE_MOCANA_SSH_SERVER__
  • __ENABLE_MOCANA_SSH_FTP_SERVER__

sftp.h

Parameters
sftpInternelDescrState information for a specific open file connection (similar to a connection instance).
numBytesReadNumber of bytes read.
Returns
None.
Remarks
This function is applicable to synchronous SFTP servers.
static sbyte4
SFTP_EXAMPLE_readUpcall(sbyte4 connectionInstance, sbyte4 sftpInternelDescr)
{
FILE* fd = (FILE *)SSH_sftpGetCookie(sftpInternelDescr);
ubyte* pBuffer;
sbyte4 bufferSize;
sbyte4 numBytesRead;
sbyte4 fileSize;
sbyte4 status = SSH_FTP_OK;
fseek(fd, 0, SEEK_END); // determine size
fileSize = ftell(fd);
if ((0 == fileSize) ||
(SSH_sftpReadLocation(sftpInternelDescr) >= fileSize))
{ // eof reached
status = SSH_FTP_EOF;
goto exit;
}
// set the position to read from
fseek(fd, SSH_sftpReadLocation(sftpInternelDescr), SEEK_SET);
// fetch max bytes that can be sent at the moment
pBuffer = SSH_sftpReadBuffer(sftpInternelDescr);
bufferSize = SSH_sftpReadBufferSize(sftpInternelDescr);
// check if enough bytes have been read
numBytesRead = fread(pBuffer, 1, bufferSize, fd);
if (0 == numBytesRead) // if eof reached
status = SSH_FTP_EOF;
// set the number of bytes waiting to be sent
SSH_sftpNumBytesRead(sftpInternelDescr, numBytesRead);
exit:
return status;
}

ssh_ftp.c

◆ SSH_sftpReadBuffer()

MOC_EXTERN sbyte* SSH_sftpReadBuffer ( void *  sftpInternelDescr)

This function returns a pointer to a buffer in which data read from a file is stored.

Since
1.41
Version
1.41 and later

To enable this function, the following flags must be defined in moptions.h:

  • __ENABLE_MOCANA_SSH_SERVER__
  • __ENABLE_MOCANA_SSH_FTP_SERVER__

sftp.h

Parameters
sftpInternelDescrState information for a specific open file connection (similar to a connection instance).
Returns
Pointer to a buffer containing a file's read data.
Remarks
This function is applicable to synchronous SFTP servers.
static sbyte4
SFTP_EXAMPLE_readUpcall(sbyte4 connectionInstance, sbyte4 sftpInternelDescr)
{
FILE* fd = (FILE *)SSH_sftpGetCookie(sftpInternelDescr);
ubyte* pBuffer;
sbyte4 bufferSize;
sbyte4 numBytesRead;
sbyte4 fileSize;
sbyte4 status = SSH_FTP_OK;
fseek(fd, 0, SEEK_END); // determine size
fileSize = ftell(fd);
if ((0 == fileSize) ||
(SSH_sftpReadLocation(sftpInternelDescr) >= fileSize))
{ // eof reached
status = SSH_FTP_EOF;
goto exit;
}
// set the position to read from
fseek(fd, SSH_sftpReadLocation(sftpInternelDescr), SEEK_SET);
// fetch max bytes that can be sent at the moment
pBuffer = SSH_sftpReadBuffer(sftpInternelDescr);
bufferSize = SSH_sftpReadBufferSize(sftpInternelDescr);
// check if enough bytes have been read
numBytesRead = fread(pBuffer, 1, bufferSize, fd);
if (0 == numBytesRead) // eof reached
status = SSH_FTP_EOF;
// set the number of bytes waiting to be sent
SSH_sftpNumBytesRead(sftpInternelDescr, numBytesRead);
exit:
return status;
}

ssh_ftp.c

◆ SSH_sftpReadBufferSize()

MOC_EXTERN sbyte4 SSH_sftpReadBufferSize ( void *  sftpInternelDescr)

This function returns the number of read bytes a client is requesting (or the maximum size of the read buffer if the buffer is too small for the number of bytes requested).

Since
1.41
Version
1.41 and later

To enable this function, the following flags must be defined in moptions.h:

  • __ENABLE_MOCANA_SSH_SERVER__
  • __ENABLE_MOCANA_SSH_FTP_SERVER__

sftp.h

Parameters
sftpInternelDescrState information for a specific open file connection (similar to a connection instance).
Returns
Number of read bytes a client is requesting (or the maximum size of the read buffer if the buffer is too small for the number of bytes requested).
Remarks
This function is applicable to synchronous SFTP servers.
static sbyte4
SFTP_EXAMPLE_readUpcall(sbyte4 connectionInstance, sbyte4 sftpInternelDescr)
{
FILE* fd = (FILE *)SSH_sftpGetCookie(sftpInternelDescr);
ubyte* pBuffer;
sbyte4 bufferSize;
sbyte4 numBytesRead;
sbyte4 fileSize;
sbyte4 status = SSH_FTP_OK;
fseek(fd, 0, SEEK_END); // determine size
fileSize = ftell(fd);
if ((0 == fileSize) ||
(SSH_sftpReadLocation(sftpInternelDescr) >= fileSize)) { // eof reached
status = SSH_FTP_EOF;
goto exit;
}
// set the position to read from
fseek(fd, SSH_sftpReadLocation(sftpInternelDescr), SEEK_SET);
// fetch max bytes that can be sent at the moment
pBuffer = SSH_sftpReadBuffer(sftpInternelDescr);
bufferSize = SSH_sftpReadBufferSize(sftpInternelDescr);
// check if enough bytes have been read
numBytesRead = fread(pBuffer, 1, bufferSize, fd);
// end of file reached
if (0 == numBytesRead)
status = SSH_FTP_EOF;
// set the number of bytes waiting to be sent
SSH_sftpNumBytesRead(sftpInternelDescr, numBytesRead);
exit:
return status;
}

ssh_ftp.c

◆ SSH_sftpReadLocation()

MOC_EXTERN sbyte4 SSH_sftpReadLocation ( void *  sftpInternelDescr)

This function (typically used within an SFTP read file upcall) returns a file's read byte position.

Since
1.41
Version
1.41 and later

To enable this function, the following flags must be defined in moptions.h:

  • __ENABLE_MOCANA_SSH_SERVER__
  • __ENABLE_MOCANA_SSH_FTP_SERVER__

sftp.h

Parameters
sftpInternelDescrState information for a specific open file connection (similar to a connection instance).
Returns
0-based byte index of file's current read location.
Remarks
This function is applicable to synchronous SFTP servers.
static sbyte4
SFTP_EXAMPLE_readUpcall(sbyte4 connectionInstance, sbyte4 sftpInternelDescr)
{
FILE* fd = (FILE *)SSH_sftpGetCookie(sftpInternelDescr);
ubyte* pBuffer;
sbyte4 bufferSize;
sbyte4 numBytesRead;
sbyte4 fileSize;
sbyte4 status = SSH_FTP_OK;
fseek(fd, 0, SEEK_END); // determine size
fileSize = ftell(fd);
if ((0 == fileSize) ||
(SSH_sftpReadLocation(sftpInternelDescr) >= fileSize))
{ // eof reached
status = SSH_FTP_EOF;
goto exit;
}
// set the position to read from
fseek(fd, SSH_sftpReadLocation(sftpInternelDescr), SEEK_SET);
// fetch max bytes that can be sent at the moment
pBuffer = SSH_sftpReadBuffer(sftpInternelDescr);
bufferSize = SSH_sftpReadBufferSize(sftpInternelDescr);
// check if enough bytes have been read
numBytesRead = fread(pBuffer, 1, bufferSize, fd);
// end of file reached
if (0 == numBytesRead)
status = SSH_FTP_EOF;
// set the number of bytes waiting to be sent
SSH_sftpNumBytesRead(sftpInternelDescr, numBytesRead);
exit:
return status;
}

ssh_ftp.c

◆ SSH_sftpWriteBuffer()

MOC_EXTERN sbyte* SSH_sftpWriteBuffer ( void *  sftpInternelDescr)

This function returns a pointer to a buffer (received from an SFTP client) containing data to write to a file.

Since
1.41
Version
1.41 and later

To enable this function, the following flags must be defined in moptions.h:

  • __ENABLE_MOCANA_SSH_SERVER__
  • __ENABLE_MOCANA_SSH_FTP_SERVER__

sftp.h

Parameters
sftpInternelDescrState information for a specific open file connection (similar to a connection instance).
Returns
Pointer to the buffer containing data to write.
Remarks
This function is applicable to synchronous SFTP servers.
static sbyte4
SFTP_EXAMPLE_writeUpcall(sbyte4 connectionInstance, sbyte4 sftpInternelDescr)
{
FILE* fd = (FILE *)SSH_sftpGetCookie(sftpInternelDescr);
ubyte* pBuffer;
sbyte4 bufferSize;
sbyte4 numBytesWritten;
sbyte4 status = SSH_FTP_OK;
// set the position to write from
fseek(fd, SSH_sftpWriteLocation(sftpInternelDescr), SEEK_SET);
// fetch buffer and max bytes that can be sent at the moment
pBuffer = SSH_sftpWriteBuffer(sftpInternelDescr);
bufferSize = SSH_sftpWriteBufferSize(sftpInternelDescr);
// write the bytes
numBytesWritten = fwrite(pBuffer, 1, bufferSize, fd);
if (bufferSize < numBytesWritten)
{
// write failed
status = SSH_FTP_FAILURE;
}
exit:
return status;
}

ssh_ftp.c

◆ SSH_sftpWriteBufferSize()

MOC_EXTERN sbyte4 SSH_sftpWriteBufferSize ( void *  sftpInternelDescr)

This function returns the number of bytes written to a file by an SFTP write upcall handler.

Since
1.41
Version
1.41 and later

To enable this function, the following flags must be defined in moptions.h:

  • __ENABLE_MOCANA_SSH_SERVER__
  • __ENABLE_MOCANA_SSH_FTP_SERVER__

sftp.h

Parameters
sftpInternelDescrState information for a specific open file connection (similar to a connection instance).
Returns
Number of bytes written by the SFTP write upcall handler.
Remarks
This function is applicable to synchronous SFTP servers.
static sbyte4
SFTP_EXAMPLE_writeUpcall(sbyte4 connectionInstance, sbyte4 sftpInternelDescr)
{
FILE* fd = (FILE *)SSH_sftpGetCookie(sftpInternelDescr);
ubyte* pBuffer;
sbyte4 bufferSize;
sbyte4 numBytesWritten;
sbyte4 status = SSH_FTP_OK;
// set the position to write from
fseek(fd, SSH_sftpWriteLocation(sftpInternelDescr), SEEK_SET);
// fetch buffer and max bytes that can be sent at the moment
pBuffer = SSH_sftpWriteBuffer(sftpInternelDescr);
bufferSize = SSH_sftpWriteBufferSize(sftpInternelDescr);
// write the bytes
numBytesWritten = fwrite(pBuffer, 1, bufferSize, fd);
if (bufferSize < numBytesWritten)
{
// write failed
status = SSH_FTP_FAILURE;
}
exit:
return status;
}

ssh_ftp.c

◆ SSH_sftpWriteLocation()

MOC_EXTERN sbyte4 SSH_sftpWriteLocation ( void *  sftpInternelDescr)

This function (typically used within an SFTP write file upcall) returns a file's write byte position.

Since
1.41
Version
1.41 and later

To enable this function, the following flags must be defined in moptions.h:

  • __ENABLE_MOCANA_SSH_SERVER__
  • __ENABLE_MOCANA_SSH_FTP_SERVER__

sftp.h

Parameters
sftpInternelDescrState information for a specific open file connection (similar to a connection instance).
Returns
0-based byte index of file's current write location.
Remarks
This function is applicable to synchronous SFTP servers.
static sbyte4
SFTP_EXAMPLE_writeUpcall(sbyte4 connectionInstance, sbyte4 sftpInternelDescr)
{
FILE* fd = (FILE *)SSH_sftpGetCookie(sftpInternelDescr);
ubyte* pBuffer;
sbyte4 bufferSize;
sbyte4 numBytesWritten;
sbyte4 status = SSH_FTP_OK;
// set the position to write from
fseek(fd, SSH_sftpWriteLocation(sftpInternelDescr), SEEK_SET);
// fetch buffer and max bytes that can be sent at the moment
pBuffer = SSH_sftpWriteBuffer(sftpInternelDescr);
bufferSize = SSH_sftpWriteBufferSize(sftpInternelDescr);
// write the bytes
numBytesWritten = fwrite(pBuffer, 1, bufferSize, fd);
if (bufferSize < numBytesWritten)
{
// write failed
status = SSH_FTP_FAILURE;
}
exit:
return status;
}

ssh_ftp.c