sftp_client_misc.c
Go to the documentation of this file.
1 /**
2  * @file sftp_client_misc.c
3  * @brief Helper functions for SFTP client
4  *
5  * @section License
6  *
7  * SPDX-License-Identifier: GPL-2.0-or-later
8  *
9  * Copyright (C) 2019-2024 Oryx Embedded SARL. All rights reserved.
10  *
11  * This file is part of CycloneSSH Open.
12  *
13  * This program is free software; you can redistribute it and/or
14  * modify it under the terms of the GNU General Public License
15  * as published by the Free Software Foundation; either version 2
16  * of the License, or (at your option) any later version.
17  *
18  * This program is distributed in the hope that it will be useful,
19  * but WITHOUT ANY WARRANTY; without even the implied warranty of
20  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21  * GNU General Public License for more details.
22  *
23  * You should have received a copy of the GNU General Public License
24  * along with this program; if not, write to the Free Software Foundation,
25  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
26  *
27  * @author Oryx Embedded SARL (www.oryx-embedded.com)
28  * @version 2.4.4
29  **/
30 
31 //Switch to the appropriate trace level
32 #define TRACE_LEVEL SFTP_TRACE_LEVEL
33 
34 //Dependencies
35 #include "ssh/ssh.h"
36 #include "ssh/ssh_connection.h"
37 #include "ssh/ssh_request.h"
38 #include "ssh/ssh_misc.h"
39 #include "sftp/sftp_client.h"
41 #include "sftp/sftp_client_misc.h"
42 #include "path.h"
43 #include "debug.h"
44 
45 //Check SSH stack configuration
46 #if (SFTP_CLIENT_SUPPORT == ENABLED)
47 
48 
49 /**
50  * @brief Update SFTP client state
51  * @param[in] context Pointer to the SFTP client context
52  * @param[in] newState New state to switch to
53  **/
54 
56  SftpClientState newState)
57 {
58  //Switch to the new state
59  context->state = newState;
60 
61  //Save current time
62  context->timestamp = osGetSystemTime();
63 }
64 
65 
66 /**
67  * @brief Open SSH connection
68  * @param[in] context Pointer to the SFTP client context
69  * @return Error code
70  **/
71 
73 {
74  error_t error;
75  Socket *socket;
76  SshConnection *connection;
77 
78  //Initialize SSH context
79  error = sshInit(&context->sshContext, &context->sshConnection, 1,
80  &context->sshChannel, 1);
81  //Any error to report?
82  if(error)
83  return error;
84 
85  //Select client operation mode
86  error = sshSetOperationMode(&context->sshContext, SSH_OPERATION_MODE_CLIENT);
87  //Any error to report?
88  if(error)
89  return error;
90 
91  //Invoke user-defined callback, if any
92  if(context->sshInitCallback != NULL)
93  {
94  //Perform SSH related initialization
95  error = context->sshInitCallback(context, &context->sshContext);
96  //Any error to report?
97  if(error)
98  return error;
99  }
100 
101  //Open a TCP socket
103 
104  //Valid socket handle
105  if(socket != NULL)
106  {
107  //Associate the socket with the relevant interface
108  socketBindToInterface(socket, context->interface);
109  //Set timeout
110  socketSetTimeout(socket, context->timeout);
111 
112  //Open a new SSH connection
113  connection = sshOpenConnection(&context->sshContext, socket);
114 
115  //Failed to open connection?
116  if(connection == NULL)
117  {
118  //Clean up side effects
120  //Report an error
121  error = ERROR_OPEN_FAILED;
122  }
123  }
124  else
125  {
126  //Failed to open socket
127  error = ERROR_OPEN_FAILED;
128  }
129 
130  //Return status code
131  return error;
132 }
133 
134 
135 /**
136  * @brief Establish SSH connection
137  * @param[in] context Pointer to the SFTP client context
138  * @return Error code
139  **/
140 
142 {
143  error_t error;
144  SshConnection *connection;
145  SshChannel *channel;
146 
147  //Point to the SSH connection
148  connection = &context->sshConnection;
149  //Point to the SSH channel
150  channel = &context->sshChannel;
151 
152  //Check the state of the SSH connection
153  if(context->sshConnection.state < SSH_CONN_STATE_OPEN)
154  {
155  //Perform SSH key exchange and user authentication
156  error = sftpClientProcessEvents(context);
157  }
158  else if(context->sshConnection.state == SSH_CONN_STATE_OPEN)
159  {
160  //Check the state of the SFTP client
161  if(context->state == SFTP_CLIENT_STATE_CHANNEL_OPEN)
162  {
163  //Allocate a new SSH channel
164  channel = sshCreateChannel(connection);
165 
166  //Valid channel handle?
167  if(channel != NULL)
168  {
169  //Force the channel to operate in non-blocking mode
170  error = sshSetChannelTimeout(channel, 0);
171 
172  //Check status code
173  if(!error)
174  {
175  //The client sends an SSH_MSG_CHANNEL_OPEN message to the server
176  //in order to open a new channel
177  error = sshSendChannelOpen(channel, "session", NULL);
178  }
179 
180  //Check status code
181  if(!error)
182  {
183  //Update SFTP client state
184  sftpClientChangeState(context,
186  }
187  }
188  else
189  {
190  //Report an error
191  error = ERROR_OPEN_FAILED;
192  }
193  }
194  else if(context->state == SFTP_CLIENT_STATE_CHANNEL_OPEN_REPLY)
195  {
196  //Wait for server's response
197  error = sftpClientProcessEvents(context);
198 
199  //Check status code
200  if(!error)
201  {
202  //Check the state of the channel
203  if(channel->state == SSH_CHANNEL_STATE_RESERVED)
204  {
205  //Continue processing
206  }
207  else if(channel->state == SSH_CHANNEL_STATE_OPEN)
208  {
209  //An SSH_MSG_CHANNEL_OPEN_CONFIRMATION message has been received
211  }
212  else if(channel->state == SSH_CHANNEL_STATE_CLOSED)
213  {
214  //An SSH_MSG_CHANNEL_OPEN_FAILURE message has been received
215  error = ERROR_OPEN_FAILED;
216  }
217  else
218  {
219  //Invalid state
220  error = ERROR_WRONG_STATE;
221  }
222  }
223  }
224  else if(context->state == SFTP_CLIENT_STATE_CHANNEL_REQUEST)
225  {
226  SshSubsystemParams requestParams;
227 
228  //Set "subsystem" request parameters
229  requestParams.subsystemName.value = "sftp";
230  requestParams.subsystemName.length = osStrlen("sftp");
231 
232  //Send an SSH_MSG_CHANNEL_REQUEST message to the server
233  error = sshSendChannelRequest(channel, "subsystem", &requestParams,
234  TRUE);
235 
236  //Check status code
237  if(!error)
238  {
239  //Update SFTP client state
241  }
242  }
243  else if(context->state == SFTP_CLIENT_STATE_CHANNEL_REPLY)
244  {
245  //Wait for server's response
246  error = sftpClientProcessEvents(context);
247 
248  //Check status code
249  if(!error)
250  {
251  //Check the state of the channel request
252  if(channel->requestState == SSH_REQUEST_STATE_PENDING)
253  {
254  //Continue processing
255  }
256  else if(channel->requestState == SSH_REQUEST_STATE_SUCCESS)
257  {
258  //An SSH_MSG_CHANNEL_SUCCESS message has been received
260  }
261  else if(channel->requestState == SSH_REQUEST_STATE_FAILURE)
262  {
263  //An SSH_MSG_CHANNEL_FAILURE message has been received
264  error = ERROR_OPEN_FAILED;
265  }
266  else
267  {
268  //Invalid state
269  error = ERROR_WRONG_STATE;
270  }
271  }
272  }
273  else
274  {
275  //Invalid state
276  error = ERROR_WRONG_STATE;
277  }
278  }
279  else
280  {
281  //Invalid state
282  error = ERROR_WRONG_STATE;
283  }
284 
285  //Return status code
286  return error;
287 }
288 
289 
290 /**
291  * @brief Close SSH connection
292  * @param[in] context Pointer to the SFTP client context
293  **/
294 
296 {
297  //Check the state of the SSH connection
298  if(context->sshConnection.state != SSH_CONN_STATE_CLOSED)
299  {
300  //Close SSH connection
301  sshCloseConnection(&context->sshConnection);
302  }
303 
304  //Release SSH context
305  sshDeinit(&context->sshContext);
306 }
307 
308 
309 /**
310  * @brief Send SFTP request and wait for a response
311  * @param[in] context Pointer to the SFTP client context
312  * @return Error code
313  **/
314 
316 {
317  error_t error;
318  size_t n;
319 
320  //Initialize status code
321  error = NO_ERROR;
322 
323  //Send SFTP request and wait for the SFTP response to be received
324  while(!error)
325  {
326  //Send SFTP request
327  if(context->requestPos < context->requestLen)
328  {
329  //Send more data
330  error = sshWriteChannel(&context->sshChannel,
331  context->buffer + context->requestPos,
332  context->requestLen - context->requestPos, &n, 0);
333 
334  //Check status code
335  if(error == NO_ERROR || error == ERROR_TIMEOUT)
336  {
337  //Advance data pointer
338  context->requestPos += n;
339  }
340  }
341  else
342  {
343  //Receive SFTP response
344  if(context->responsePos < sizeof(SftpPacketHeader))
345  {
346  //Receive more data
347  error = sshReadChannel(&context->sshChannel,
348  context->buffer + context->responsePos,
349  sizeof(SftpPacketHeader) - context->responsePos, &n, 0);
350 
351  //Check status code
352  if(!error)
353  {
354  //Advance data pointer
355  context->responsePos += n;
356 
357  //SFTP packet header successfully received?
358  if(context->responsePos >= sizeof(SftpPacketHeader))
359  {
360  //Parse SFTP packet header
361  error = sftpClientParsePacketLength(context, context->buffer);
362  }
363  }
364  }
365  else if(context->responsePos < context->responseLen)
366  {
367  //Receive more data
368  error = sshReadChannel(&context->sshChannel,
369  context->buffer + context->responsePos,
370  context->responseLen - context->responsePos, &n, 0);
371 
372  //Check status code
373  if(!error)
374  {
375  //Advance data pointer
376  context->responsePos += n;
377  }
378  }
379  else
380  {
381  //Process SFTP packet
382  error = sftpClientParsePacket(context, context->buffer,
383  context->responseLen, context->responseTotalLen);
384 
385  //An SFTP response has been received
386  break;
387  }
388  }
389 
390  //Check status code
391  if(error == ERROR_WOULD_BLOCK || error == ERROR_TIMEOUT)
392  {
393  //Process SSH connection events
394  error = sftpClientProcessEvents(context);
395  }
396  }
397 
398  //Return status code
399  return error;
400 }
401 
402 
403 /**
404  * @brief Process SFTP client events
405  * @param[in] context Pointer to the SFTP client context
406  * @return Error code
407  **/
408 
410 {
411  error_t error;
412  uint_t i;
413  SshContext *sshContext;
414  SshConnection *connection;
415 
416  //Point to the SSH context
417  sshContext = &context->sshContext;
418 
419  //Clear event descriptor set
420  osMemset(sshContext->eventDesc, 0, sizeof(sshContext->eventDesc));
421 
422  //Specify the events the application is interested in
423  for(i = 0; i < sshContext->numConnections; i++)
424  {
425  //Point to the structure describing the current connection
426  connection = &sshContext->connections[i];
427 
428  //Loop through active connections only
429  if(connection->state != SSH_CONN_STATE_CLOSED)
430  {
431  //Register the events related to the current SSH connection
432  sshRegisterConnectionEvents(sshContext, connection, &sshContext->eventDesc[i]);
433  }
434  }
435 
436  //Wait for one of the set of sockets to become ready to perform I/O
437  error = socketPoll(sshContext->eventDesc, sshContext->numConnections,
438  &sshContext->event, context->timeout);
439 
440  //Verify status code
441  if(error == NO_ERROR || error == ERROR_WAIT_CANCELED)
442  {
443  //Clear status code
444  error = NO_ERROR;
445 
446  //Event-driven processing
447  for(i = 0; i < sshContext->numConnections && !error; i++)
448  {
449  //Point to the structure describing the current connection
450  connection = &sshContext->connections[i];
451 
452  //Loop through active connections only
453  if(connection->state != SSH_CONN_STATE_CLOSED)
454  {
455  //Check whether the socket is ready to perform I/O
456  if(sshContext->eventDesc[i].eventFlags != 0)
457  {
458  //Connection event handler
459  error = sshProcessConnectionEvents(sshContext, connection);
460  }
461  }
462  }
463  }
464 
465  //Check status code
466  if(error == ERROR_WOULD_BLOCK || error == ERROR_TIMEOUT)
467  {
468  //Check whether the timeout has elapsed
469  error = sftpClientCheckTimeout(context);
470  }
471 
472  //Return status code
473  return error;
474 }
475 
476 
477 /**
478  * @brief Retrieve the length of an incoming SFTP packet
479  * @param[in] context Pointer to the SFTP client context
480  * @param[in] packet Pointer to received SFTP packet
481  * @return Error code
482  **/
483 
485  const uint8_t *packet)
486 {
487  error_t error;
488  const SftpPacketHeader *header;
489 
490  //Initialize status code
491  error = NO_ERROR;
492 
493  //Point to the SSH packet header
494  header = (SftpPacketHeader *) packet;
495 
496  //Convert the packet length to host byte order
497  context->responseTotalLen = ntohl(header->length);
498  //The length of the packet does not include the packet_length field itself
499  context->responseTotalLen += sizeof(uint32_t);
500 
501  //Sanity check
502  if(context->responseTotalLen > ntohl(header->length))
503  {
504  //SSH_FXP_DATA or SSH_FXP_NAME packet received?
505  if(header->type == SSH_FXP_DATA)
506  {
507  //Read only the header of the SSH_FXP_DATA packet
508  context->responseLen = MIN(context->responseTotalLen,
509  sizeof(SftpPacketHeader) + sizeof(SftpFxpDataHeader));
510  }
511  else if(header->type == SSH_FXP_NAME)
512  {
513  //Read as much data as possible
514  context->responseLen = MIN(context->responseTotalLen,
516  }
517  else
518  {
519  //Check the length of the packet
520  if(context->responseTotalLen <= SFTP_CLIENT_BUFFER_SIZE)
521  {
522  //Save the total length of the packet
523  context->responseLen = context->responseTotalLen;
524  }
525  else
526  {
527  //Report an error
528  error = ERROR_INVALID_LENGTH;
529  }
530  }
531  }
532  else
533  {
534  //Report an error
535  error = ERROR_INVALID_LENGTH;
536  }
537 
538  //Return status code
539  return error;
540 }
541 
542 
543 /**
544  * @brief SFTP packet processing
545  * @param[in] context Pointer to the SFTP client context
546  * @param[in] packet Pointer to received SFTP packet
547  * @param[in] fragLen Number of bytes available on hand
548  * @param[in] totalLen Total length of the packet, in bytes
549  * @return Error code
550  **/
551 
552 error_t sftpClientParsePacket(SftpClientContext *context, const uint8_t *packet,
553  size_t fragLen, size_t totalLen)
554 {
555  error_t error;
556  const SftpPacketHeader *header;
557 
558  //Debug message
559  TRACE_DEBUG("SFTP packet received (%" PRIuSIZE " bytes)...\r\n", totalLen);
560  TRACE_VERBOSE_ARRAY(" ", packet, fragLen);
561 
562  //Check the length of the received packet
563  if(fragLen >= sizeof(SftpPacketHeader) && fragLen <= totalLen)
564  {
565  //Point to the SSH packet header
566  header = (SftpPacketHeader *) packet;
567 
568  //Retrieve the length of the payload
569  fragLen -= sizeof(SftpPacketHeader);
570  totalLen -= sizeof(SftpPacketHeader);
571 
572  //Check message type
573  if(header->type == SSH_FXP_VERSION)
574  {
575  //Parse SSH_FXP_VERSION packet
576  error = sftpClientParseFxpVersion(context, header->payload, fragLen);
577  }
578  else if(header->type == SSH_FXP_STATUS)
579  {
580  //Parse SSH_FXP_STATUS packet
581  error = sftpClientParseFxpStatus(context, header->payload, fragLen);
582  }
583  else if(header->type == SSH_FXP_HANDLE)
584  {
585  //Parse SSH_FXP_HANDLE packet
586  error = sftpClientParseFxpHandle(context, header->payload, fragLen);
587  }
588  else if(header->type == SSH_FXP_DATA)
589  {
590  //Parse SSH_FXP_DATA packet
591  error = sftpClientParseFxpData(context, header->payload, fragLen,
592  totalLen);
593  }
594  else if(header->type == SSH_FXP_NAME)
595  {
596  //Parse SSH_FXP_NAME packet
597  error = sftpClientParseFxpName(context, header->payload, fragLen,
598  totalLen);
599  }
600  else if(header->type == SSH_FXP_ATTRS)
601  {
602  //Parse SSH_FXP_ATTRS packet
603  error = sftpClientParseFxpAttrs(context, header->payload, fragLen);
604  }
605  else
606  {
607  //Debug message
608  TRACE_WARNING("Unknown SFTP packet type!\r\n");
609  //Unknown packet type
610  error = ERROR_INVALID_TYPE;
611  }
612  }
613  else
614  {
615  //Malformed SFTP packet
616  error = ERROR_INVALID_LENGTH;
617  }
618 
619  //Return status code
620  return error;
621 }
622 
623 
624 /**
625  * @brief Determine whether a timeout error has occurred
626  * @param[in] context Pointer to the SFTP client context
627  * @return Error code
628  **/
629 
631 {
632  error_t error;
633  systime_t time;
634 
635  //Get current time
636  time = osGetSystemTime();
637 
638  //Check whether the timeout has elapsed
639  if(timeCompare(time, context->timestamp + context->timeout) >= 0)
640  {
641  //Report a timeout error
642  error = ERROR_TIMEOUT;
643  }
644  else
645  {
646 #if (NET_RTOS_SUPPORT == ENABLED)
647  //Successful operation
648  error = NO_ERROR;
649 #else
650  //The operation would block
651  error = ERROR_WOULD_BLOCK;
652 #endif
653  }
654 
655  //Return status code
656  return error;
657 }
658 
659 
660 /**
661  * @brief Format pathname
662  * @param[in] context Pointer to the SFTP client context
663  * @param[in] path NULL-terminated string that contains the pathname
664  * @param[out] p Output stream where to write the string
665  * @param[out] written Total number of bytes that have been written
666  * @return Error code
667  **/
668 
670  uint8_t *p, size_t *written)
671 {
672  size_t n;
673 
674  //Retrieve the full pathname
675  sftpGetAbsolutePath(context, path, (char_t *) p + sizeof(uint32_t));
676 
677  //Get the length of the resulting string
678  n = osStrlen((char_t *) p + sizeof(uint32_t));
679 
680  //A string is stored as a uint32 containing its length and zero or more
681  //bytes that are the value of the string
682  STORE32BE(n, p);
683 
684  //Total number of bytes that have been written
685  *written = sizeof(uint32_t) + n;
686 
687  //Successful processing
688  return NO_ERROR;
689 }
690 
691 
692 /**
693  * @brief Retrieve the full pathname
694  * @param[in] context Pointer to the SFTP client context
695  * @param[in] path Absolute or relative path
696  * @param[in] fullPath Output buffer where to store the resulting full pathname
697  **/
698 
699 void sftpGetAbsolutePath(SftpClientContext *context, const char_t *path,
700  char_t *fullPath)
701 {
702  //Relative or absolute path?
703  if(pathIsRelative(path))
704  {
705  //Copy current working directory
706  pathCopy(fullPath, context->currentDir, SFTP_CLIENT_MAX_PATH_LEN);
707  //Append the relative path
708  pathCombine(fullPath, path, SFTP_CLIENT_MAX_PATH_LEN);
709  }
710  else
711  {
712  //Copy absolute path
713  pathCopy(fullPath, path, SFTP_CLIENT_MAX_PATH_LEN);
714  }
715 
716  //Clean the resulting path
717  pathCanonicalize(fullPath);
718  pathRemoveSlash(fullPath);
719 }
720 
721 #endif
Path manipulation helper functions.
@ SSH_FXP_ATTRS
Definition: sftp_common.h:159
error_t sftpFormatPath(SftpClientContext *context, const char_t *path, uint8_t *p, size_t *written)
Format pathname.
"subsystem" channel request parameters
Definition: ssh_request.h:129
error_t sftpClientParsePacketLength(SftpClientContext *context, const uint8_t *packet)
Retrieve the length of an incoming SFTP packet.
@ SSH_CONN_STATE_OPEN
Definition: ssh.h:1062
#define SFTP_CLIENT_BUFFER_SIZE
Definition: sftp_client.h:75
@ ERROR_WOULD_BLOCK
Definition: error.h:96
SFTP packet parsing and formatting.
uint8_t p
Definition: ndp.h:300
SSH connection protocol.
#define TRUE
Definition: os_port.h:50
SshConnection * sshOpenConnection(SshContext *context, Socket *socket)
Open a new SSH connection.
Definition: ssh_misc.c:66
void socketClose(Socket *socket)
Close an existing socket.
Definition: socket.c:2062
@ SSH_FXP_STATUS
Definition: sftp_common.h:155
bool_t pathIsRelative(const char_t *path)
Test if the path is relative.
Definition: path.c:61
error_t sftpClientOpenConnection(SftpClientContext *context)
Open SSH connection.
size_t length
Definition: ssh_types.h:58
#define osStrlen(s)
Definition: os_port.h:165
error_t sftpClientProcessEvents(SftpClientContext *context)
Process SFTP client events.
@ SSH_FXP_HANDLE
Definition: sftp_common.h:156
@ SOCKET_TYPE_STREAM
Definition: socket.h:92
#define timeCompare(t1, t2)
Definition: os_port.h:40
@ ERROR_WRONG_STATE
Definition: error.h:209
error_t sshInit(SshContext *context, SshConnection *connections, uint_t numConnections, SshChannel *channels, uint_t numChannels)
SSH context initialization.
Definition: ssh.c:58
error_t sshReadChannel(SshChannel *channel, void *data, size_t size, size_t *received, uint_t flags)
Receive data from the specified channel.
Definition: ssh.c:2180
#define SFTP_CLIENT_MAX_PATH_LEN
Definition: sftp_client.h:96
@ SSH_FXP_VERSION
Definition: sftp_common.h:136
@ SSH_REQUEST_STATE_PENDING
Definition: ssh.h:1087
error_t sftpClientParseFxpHandle(SftpClientContext *context, const uint8_t *packet, size_t length)
Parse SSH_FXP_HANDLE packet.
@ ERROR_OPEN_FAILED
Definition: error.h:75
error_t sftpClientCheckTimeout(SftpClientContext *context)
Determine whether a timeout error has occurred.
void pathCanonicalize(char_t *path)
Simplify a path.
Definition: path.c:158
#define SshContext
Definition: ssh.h:870
const char_t * value
Definition: ssh_types.h:57
error_t sftpClientParseFxpName(SftpClientContext *context, const uint8_t *packet, size_t fragLen, size_t totalLen)
Parse SSH_FXP_NAME packet.
error_t
Error codes.
Definition: error.h:43
error_t sftpClientParseFxpAttrs(SftpClientContext *context, const uint8_t *packet, size_t length)
Parse SSH_FXP_ATTRS packet.
void sshDeinit(SshContext *context)
Release SSH context.
Definition: ssh.c:2556
int_t socket(int_t family, int_t type, int_t protocol)
Create a socket that is bound to a specific transport service provider.
Definition: bsd_socket.c:65
SftpPacketHeader
Definition: sftp_common.h:219
SftpFxpDataHeader
Definition: sftp_common.h:231
@ SFTP_CLIENT_STATE_CHANNEL_REQUEST
Definition: sftp_client.h:122
@ SSH_OPERATION_MODE_CLIENT
Definition: ssh.h:892
@ SSH_FXP_DATA
Definition: sftp_common.h:157
error_t sftpClientEstablishConnection(SftpClientContext *context)
Establish SSH connection.
@ ERROR_INVALID_LENGTH
Definition: error.h:111
SshChannel * sshCreateChannel(SshConnection *connection)
Create a new SSH channel.
Definition: ssh.c:1964
error_t sshProcessConnectionEvents(SshContext *context, SshConnection *connection)
Connection event handler.
Definition: ssh_misc.c:372
void sshCloseConnection(SshConnection *connection)
Close SSH connection.
Definition: ssh_misc.c:172
@ SSH_CHANNEL_STATE_OPEN
Definition: ssh.h:1075
error_t sshWriteChannel(SshChannel *channel, const void *data, size_t length, size_t *written, uint_t flags)
Write data to the specified channel.
Definition: ssh.c:2051
SFTP client.
@ ERROR_INVALID_TYPE
Definition: error.h:115
Socket * socketOpen(uint_t type, uint_t protocol)
Create a socket (UDP or TCP)
Definition: socket.c:125
#define MIN(a, b)
Definition: os_port.h:63
@ SSH_FXP_NAME
Definition: sftp_common.h:158
@ SFTP_CLIENT_STATE_CHANNEL_REPLY
Definition: sftp_client.h:123
SftpClientState
SFTP client state.
Definition: sftp_client.h:116
@ SSH_CONN_STATE_CLOSED
Definition: ssh.h:1033
error_t socketPoll(SocketEventDesc *eventDesc, uint_t size, OsEvent *extEvent, systime_t timeout)
Wait for one of a set of sockets to become ready to perform I/O.
Definition: socket.c:2149
#define socketBindToInterface
Definition: net_legacy.h:193
error_t sftpClientParseFxpData(SftpClientContext *context, const uint8_t *packet, size_t fragLen, size_t totalLen)
Parse SSH_FXP_DATA packet.
@ SSH_REQUEST_STATE_FAILURE
Definition: ssh.h:1089
Helper functions for SFTP client.
error_t sftpClientParseFxpStatus(SftpClientContext *context, const uint8_t *packet, size_t length)
Parse SSH_FXP_STATUS packet.
void sftpClientChangeState(SftpClientContext *context, SftpClientState newState)
Update SFTP client state.
uint32_t systime_t
System time.
#define TRACE_WARNING(...)
Definition: debug.h:85
#define TRACE_DEBUG(...)
Definition: debug.h:107
@ ERROR_TIMEOUT
Definition: error.h:95
char char_t
Definition: compiler_port.h:48
uint32_t time
error_t sftpClientParsePacket(SftpClientContext *context, const uint8_t *packet, size_t fragLen, size_t totalLen)
SFTP packet processing.
@ SSH_REQUEST_STATE_SUCCESS
Definition: ssh.h:1088
uint8_t n
void sshRegisterConnectionEvents(SshContext *context, SshConnection *connection, SocketEventDesc *eventDesc)
Register connection events.
Definition: ssh_misc.c:280
#define SftpClientContext
Definition: sftp_client.h:103
SshString subsystemName
Definition: ssh_request.h:130
#define SshConnection
Definition: ssh.h:874
@ SFTP_CLIENT_STATE_CHANNEL_DATA
Definition: sftp_client.h:124
@ SSH_CHANNEL_STATE_RESERVED
Definition: ssh.h:1074
#define Socket
Definition: socket.h:36
void sftpGetAbsolutePath(SftpClientContext *context, const char_t *path, char_t *fullPath)
Retrieve the full pathname.
@ ERROR_WAIT_CANCELED
Definition: error.h:73
SSH helper functions.
void sftpClientCloseConnection(SftpClientContext *context)
Close SSH connection.
void pathRemoveSlash(char_t *path)
Remove the trailing slash from a given path.
Definition: path.c:360
@ SFTP_CLIENT_STATE_CHANNEL_OPEN
Definition: sftp_client.h:120
error_t sshSendChannelRequest(SshChannel *channel, const char_t *requestType, const void *requestParams, bool_t wantReply)
Send SSH_MSG_CHANNEL_REQUEST message.
Definition: ssh_request.c:179
error_t sftpClientParseFxpVersion(SftpClientContext *context, const uint8_t *packet, size_t length)
Parse SSH_FXP_VERSION packet.
#define PRIuSIZE
error_t sshSendChannelOpen(SshChannel *channel, const char_t *channelType, const void *channelParams)
Send SSH_MSG_CHANNEL_OPEN message.
unsigned int uint_t
Definition: compiler_port.h:50
error_t sftpClientSendCommand(SftpClientContext *context)
Send SFTP request and wait for a response.
@ SSH_CHANNEL_STATE_CLOSED
Definition: ssh.h:1076
#define osMemset(p, value, length)
Definition: os_port.h:135
error_t sshSetOperationMode(SshContext *context, SshOperationMode mode)
Set operation mode (client or server)
Definition: ssh.c:167
error_t sshSetChannelTimeout(SshChannel *channel, systime_t timeout)
Set timeout for read/write operations.
Definition: ssh.c:2027
@ SFTP_CLIENT_STATE_CHANNEL_OPEN_REPLY
Definition: sftp_client.h:121
Secure Shell (SSH)
@ SOCKET_IP_PROTO_TCP
Definition: socket.h:107
error_t socketSetTimeout(Socket *socket, systime_t timeout)
Set timeout value for blocking operations.
Definition: socket.c:148
#define STORE32BE(a, p)
Definition: cpu_endian.h:286
Global request and channel request handling.
#define ntohl(value)
Definition: cpu_endian.h:422
@ NO_ERROR
Success.
Definition: error.h:44
Debugging facilities.
#define SshChannel
Definition: ssh.h:878
void pathCopy(char_t *dest, const char_t *src, size_t maxLen)
Copy a path.
Definition: path.c:137
void pathCombine(char_t *path, const char_t *more, size_t maxLen)
Concatenate two paths.
Definition: path.c:394
systime_t osGetSystemTime(void)
Retrieve system time.
#define TRACE_VERBOSE_ARRAY(p, a, n)
Definition: debug.h:125