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-2026 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.6.0
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
102  socket = socketOpenEx(context->netContext, SOCKET_TYPE_STREAM,
104 
105  //Valid socket handle
106  if(socket != NULL)
107  {
108  //Associate the socket with the relevant interface
109  socketBindToInterface(socket, context->interface);
110  //Set timeout
111  socketSetTimeout(socket, context->timeout);
112 
113  //Open a new SSH connection
114  connection = sshOpenConnection(&context->sshContext, socket);
115 
116  //Failed to open connection?
117  if(connection == NULL)
118  {
119  //Clean up side effects
121  //Report an error
122  error = ERROR_OPEN_FAILED;
123  }
124  }
125  else
126  {
127  //Failed to open socket
128  error = ERROR_OPEN_FAILED;
129  }
130 
131  //Return status code
132  return error;
133 }
134 
135 
136 /**
137  * @brief Establish SSH connection
138  * @param[in] context Pointer to the SFTP client context
139  * @return Error code
140  **/
141 
143 {
144  error_t error;
145  SshConnection *connection;
146  SshChannel *channel;
147 
148  //Point to the SSH connection
149  connection = &context->sshConnection;
150  //Point to the SSH channel
151  channel = &context->sshChannel;
152 
153  //Check the state of the SSH connection
154  if(context->sshConnection.state < SSH_CONN_STATE_OPEN)
155  {
156  //Perform SSH key exchange and user authentication
157  error = sftpClientProcessEvents(context);
158  }
159  else if(context->sshConnection.state == SSH_CONN_STATE_OPEN)
160  {
161  //Check the state of the SFTP client
162  if(context->state == SFTP_CLIENT_STATE_CHANNEL_OPEN)
163  {
164  //Allocate a new SSH channel
165  channel = sshCreateChannel(connection);
166 
167  //Valid channel handle?
168  if(channel != NULL)
169  {
170  //Force the channel to operate in non-blocking mode
171  error = sshSetChannelTimeout(channel, 0);
172 
173  //Check status code
174  if(!error)
175  {
176  //The client sends an SSH_MSG_CHANNEL_OPEN message to the server
177  //in order to open a new channel
178  error = sshSendChannelOpen(channel, "session", NULL);
179  }
180 
181  //Check status code
182  if(!error)
183  {
184  //Update SFTP client state
185  sftpClientChangeState(context,
187  }
188  }
189  else
190  {
191  //Report an error
192  error = ERROR_OPEN_FAILED;
193  }
194  }
195  else if(context->state == SFTP_CLIENT_STATE_CHANNEL_OPEN_REPLY)
196  {
197  //Wait for server's response
198  error = sftpClientProcessEvents(context);
199 
200  //Check status code
201  if(!error)
202  {
203  //Check the state of the channel
204  if(channel->state == SSH_CHANNEL_STATE_RESERVED)
205  {
206  //Continue processing
207  }
208  else if(channel->state == SSH_CHANNEL_STATE_OPEN)
209  {
210  //An SSH_MSG_CHANNEL_OPEN_CONFIRMATION message has been received
212  }
213  else if(channel->state == SSH_CHANNEL_STATE_CLOSED)
214  {
215  //An SSH_MSG_CHANNEL_OPEN_FAILURE message has been received
216  error = ERROR_OPEN_FAILED;
217  }
218  else
219  {
220  //Invalid state
221  error = ERROR_WRONG_STATE;
222  }
223  }
224  }
225  else if(context->state == SFTP_CLIENT_STATE_CHANNEL_REQUEST)
226  {
227  SshSubsystemParams requestParams;
228 
229  //Set "subsystem" request parameters
230  requestParams.subsystemName.value = "sftp";
231  requestParams.subsystemName.length = osStrlen("sftp");
232 
233  //Send an SSH_MSG_CHANNEL_REQUEST message to the server
234  error = sshSendChannelRequest(channel, "subsystem", &requestParams,
235  TRUE);
236 
237  //Check status code
238  if(!error)
239  {
240  //Update SFTP client state
242  }
243  }
244  else if(context->state == SFTP_CLIENT_STATE_CHANNEL_REPLY)
245  {
246  //Wait for server's response
247  error = sftpClientProcessEvents(context);
248 
249  //Check status code
250  if(!error)
251  {
252  //Check the state of the channel request
253  if(channel->requestState == SSH_REQUEST_STATE_PENDING)
254  {
255  //Continue processing
256  }
257  else if(channel->requestState == SSH_REQUEST_STATE_SUCCESS)
258  {
259  //An SSH_MSG_CHANNEL_SUCCESS message has been received
261  }
262  else if(channel->requestState == SSH_REQUEST_STATE_FAILURE)
263  {
264  //An SSH_MSG_CHANNEL_FAILURE message has been received
265  error = ERROR_OPEN_FAILED;
266  }
267  else
268  {
269  //Invalid state
270  error = ERROR_WRONG_STATE;
271  }
272  }
273  }
274  else
275  {
276  //Invalid state
277  error = ERROR_WRONG_STATE;
278  }
279  }
280  else
281  {
282  //Invalid state
283  error = ERROR_WRONG_STATE;
284  }
285 
286  //Return status code
287  return error;
288 }
289 
290 
291 /**
292  * @brief Close SSH connection
293  * @param[in] context Pointer to the SFTP client context
294  **/
295 
297 {
298  //Check the state of the SSH connection
299  if(context->sshConnection.state != SSH_CONN_STATE_CLOSED)
300  {
301  //Close SSH connection
302  sshCloseConnection(&context->sshConnection);
303  }
304 
305  //Release SSH context
306  sshDeinit(&context->sshContext);
307 }
308 
309 
310 /**
311  * @brief Send SFTP request and wait for a response
312  * @param[in] context Pointer to the SFTP client context
313  * @return Error code
314  **/
315 
317 {
318  error_t error;
319  size_t n;
320 
321  //Initialize status code
322  error = NO_ERROR;
323 
324  //Send SFTP request and wait for the SFTP response to be received
325  while(!error)
326  {
327  //Send SFTP request
328  if(context->requestPos < context->requestLen)
329  {
330  //Send more data
331  error = sshWriteChannel(&context->sshChannel,
332  context->buffer + context->requestPos,
333  context->requestLen - context->requestPos, &n, 0);
334 
335  //Check status code
336  if(error == NO_ERROR || error == ERROR_TIMEOUT)
337  {
338  //Advance data pointer
339  context->requestPos += n;
340  }
341  }
342  else
343  {
344  //Receive SFTP response
345  if(context->responsePos < sizeof(SftpPacketHeader))
346  {
347  //Receive more data
348  error = sshReadChannel(&context->sshChannel,
349  context->buffer + context->responsePos,
350  sizeof(SftpPacketHeader) - context->responsePos, &n, 0);
351 
352  //Check status code
353  if(!error)
354  {
355  //Advance data pointer
356  context->responsePos += n;
357 
358  //SFTP packet header successfully received?
359  if(context->responsePos >= sizeof(SftpPacketHeader))
360  {
361  //Parse SFTP packet header
362  error = sftpClientParsePacketLength(context, context->buffer);
363  }
364  }
365  }
366  else if(context->responsePos < context->responseLen)
367  {
368  //Receive more data
369  error = sshReadChannel(&context->sshChannel,
370  context->buffer + context->responsePos,
371  context->responseLen - context->responsePos, &n, 0);
372 
373  //Check status code
374  if(!error)
375  {
376  //Advance data pointer
377  context->responsePos += n;
378  }
379  }
380  else
381  {
382  //Process SFTP packet
383  error = sftpClientParsePacket(context, context->buffer,
384  context->responseLen, context->responseTotalLen);
385 
386  //An SFTP response has been received
387  break;
388  }
389  }
390 
391  //Check status code
392  if(error == ERROR_WOULD_BLOCK || error == ERROR_TIMEOUT)
393  {
394  //Process SSH connection events
395  error = sftpClientProcessEvents(context);
396  }
397  }
398 
399  //Return status code
400  return error;
401 }
402 
403 
404 /**
405  * @brief Process SFTP client events
406  * @param[in] context Pointer to the SFTP client context
407  * @return Error code
408  **/
409 
411 {
412  error_t error;
413  uint_t i;
414  SshContext *sshContext;
415  SshConnection *connection;
416 
417  //Point to the SSH context
418  sshContext = &context->sshContext;
419 
420  //Clear event descriptor set
421  osMemset(sshContext->eventDesc, 0, sizeof(sshContext->eventDesc));
422 
423  //Specify the events the application is interested in
424  for(i = 0; i < sshContext->numConnections; i++)
425  {
426  //Point to the structure describing the current connection
427  connection = &sshContext->connections[i];
428 
429  //Loop through active connections only
430  if(connection->state != SSH_CONN_STATE_CLOSED)
431  {
432  //Register the events related to the current SSH connection
433  sshRegisterConnectionEvents(sshContext, connection, &sshContext->eventDesc[i]);
434  }
435  }
436 
437  //Wait for one of the set of sockets to become ready to perform I/O
438  error = socketPoll(sshContext->eventDesc, sshContext->numConnections,
439  &sshContext->event, context->timeout);
440 
441  //Verify status code
442  if(error == NO_ERROR || error == ERROR_WAIT_CANCELED)
443  {
444  //Clear status code
445  error = NO_ERROR;
446 
447  //Event-driven processing
448  for(i = 0; i < sshContext->numConnections && !error; i++)
449  {
450  //Point to the structure describing the current connection
451  connection = &sshContext->connections[i];
452 
453  //Loop through active connections only
454  if(connection->state != SSH_CONN_STATE_CLOSED)
455  {
456  //Check whether the socket is ready to perform I/O
457  if(sshContext->eventDesc[i].eventFlags != 0)
458  {
459  //Connection event handler
460  error = sshProcessConnectionEvents(sshContext, connection);
461  }
462  }
463  }
464  }
465 
466  //Check status code
467  if(error == ERROR_WOULD_BLOCK || error == ERROR_TIMEOUT)
468  {
469  //Check whether the timeout has elapsed
470  error = sftpClientCheckTimeout(context);
471  }
472 
473  //Return status code
474  return error;
475 }
476 
477 
478 /**
479  * @brief Retrieve the length of an incoming SFTP packet
480  * @param[in] context Pointer to the SFTP client context
481  * @param[in] packet Pointer to received SFTP packet
482  * @return Error code
483  **/
484 
486  const uint8_t *packet)
487 {
488  error_t error;
489  const SftpPacketHeader *header;
490 
491  //Initialize status code
492  error = NO_ERROR;
493 
494  //Point to the SSH packet header
495  header = (SftpPacketHeader *) packet;
496 
497  //Convert the packet length to host byte order
498  context->responseTotalLen = ntohl(header->length);
499  //The length of the packet does not include the packet_length field itself
500  context->responseTotalLen += sizeof(uint32_t);
501 
502  //Sanity check
503  if(context->responseTotalLen > ntohl(header->length))
504  {
505  //SSH_FXP_DATA or SSH_FXP_NAME packet received?
506  if(header->type == SSH_FXP_DATA)
507  {
508  //Read only the header of the SSH_FXP_DATA packet
509  context->responseLen = MIN(context->responseTotalLen,
510  sizeof(SftpPacketHeader) + sizeof(SftpFxpDataHeader));
511  }
512  else if(header->type == SSH_FXP_NAME)
513  {
514  //Read as much data as possible
515  context->responseLen = MIN(context->responseTotalLen,
517  }
518  else
519  {
520  //Check the length of the packet
521  if(context->responseTotalLen <= SFTP_CLIENT_BUFFER_SIZE)
522  {
523  //Save the total length of the packet
524  context->responseLen = context->responseTotalLen;
525  }
526  else
527  {
528  //Report an error
529  error = ERROR_INVALID_LENGTH;
530  }
531  }
532  }
533  else
534  {
535  //Report an error
536  error = ERROR_INVALID_LENGTH;
537  }
538 
539  //Return status code
540  return error;
541 }
542 
543 
544 /**
545  * @brief SFTP packet processing
546  * @param[in] context Pointer to the SFTP client context
547  * @param[in] packet Pointer to received SFTP packet
548  * @param[in] fragLen Number of bytes available on hand
549  * @param[in] totalLen Total length of the packet, in bytes
550  * @return Error code
551  **/
552 
553 error_t sftpClientParsePacket(SftpClientContext *context, const uint8_t *packet,
554  size_t fragLen, size_t totalLen)
555 {
556  error_t error;
557  const SftpPacketHeader *header;
558 
559  //Debug message
560  TRACE_DEBUG("SFTP packet received (%" PRIuSIZE " bytes)...\r\n", totalLen);
561  TRACE_VERBOSE_ARRAY(" ", packet, fragLen);
562 
563  //Check the length of the received packet
564  if(fragLen >= sizeof(SftpPacketHeader) && fragLen <= totalLen)
565  {
566  //Point to the SSH packet header
567  header = (SftpPacketHeader *) packet;
568 
569  //Retrieve the length of the payload
570  fragLen -= sizeof(SftpPacketHeader);
571  totalLen -= sizeof(SftpPacketHeader);
572 
573  //Check message type
574  if(header->type == SSH_FXP_VERSION)
575  {
576  //Parse SSH_FXP_VERSION packet
577  error = sftpClientParseFxpVersion(context, header->payload, fragLen);
578  }
579  else if(header->type == SSH_FXP_STATUS)
580  {
581  //Parse SSH_FXP_STATUS packet
582  error = sftpClientParseFxpStatus(context, header->payload, fragLen);
583  }
584  else if(header->type == SSH_FXP_HANDLE)
585  {
586  //Parse SSH_FXP_HANDLE packet
587  error = sftpClientParseFxpHandle(context, header->payload, fragLen);
588  }
589  else if(header->type == SSH_FXP_DATA)
590  {
591  //Parse SSH_FXP_DATA packet
592  error = sftpClientParseFxpData(context, header->payload, fragLen,
593  totalLen);
594  }
595  else if(header->type == SSH_FXP_NAME)
596  {
597  //Parse SSH_FXP_NAME packet
598  error = sftpClientParseFxpName(context, header->payload, fragLen,
599  totalLen);
600  }
601  else if(header->type == SSH_FXP_ATTRS)
602  {
603  //Parse SSH_FXP_ATTRS packet
604  error = sftpClientParseFxpAttrs(context, header->payload, fragLen);
605  }
606  else
607  {
608  //Debug message
609  TRACE_WARNING("Unknown SFTP packet type!\r\n");
610  //Unknown packet type
611  error = ERROR_INVALID_TYPE;
612  }
613  }
614  else
615  {
616  //Malformed SFTP packet
617  error = ERROR_INVALID_LENGTH;
618  }
619 
620  //Return status code
621  return error;
622 }
623 
624 
625 /**
626  * @brief Determine whether a timeout error has occurred
627  * @param[in] context Pointer to the SFTP client context
628  * @return Error code
629  **/
630 
632 {
633  error_t error;
634  systime_t time;
635 
636  //Get current time
637  time = osGetSystemTime();
638 
639  //Check whether the timeout has elapsed
640  if(timeCompare(time, context->timestamp + context->timeout) >= 0)
641  {
642  //Report a timeout error
643  error = ERROR_TIMEOUT;
644  }
645  else
646  {
647 #if (NET_RTOS_SUPPORT == ENABLED)
648  //Successful operation
649  error = NO_ERROR;
650 #else
651  //The operation would block
652  error = ERROR_WOULD_BLOCK;
653 #endif
654  }
655 
656  //Return status code
657  return error;
658 }
659 
660 
661 /**
662  * @brief Format pathname
663  * @param[in] context Pointer to the SFTP client context
664  * @param[in] path NULL-terminated string that contains the pathname
665  * @param[out] p Output stream where to write the string
666  * @param[out] written Total number of bytes that have been written
667  * @return Error code
668  **/
669 
671  uint8_t *p, size_t *written)
672 {
673  size_t n;
674 
675  //Retrieve the full pathname
676  sftpGetAbsolutePath(context, path, (char_t *) p + sizeof(uint32_t));
677 
678  //Get the length of the resulting string
679  n = osStrlen((char_t *) p + sizeof(uint32_t));
680 
681  //A string is stored as a uint32 containing its length and zero or more
682  //bytes that are the value of the string
683  STORE32BE(n, p);
684 
685  //Total number of bytes that have been written
686  *written = sizeof(uint32_t) + n;
687 
688  //Successful processing
689  return NO_ERROR;
690 }
691 
692 
693 /**
694  * @brief Retrieve the full pathname
695  * @param[in] context Pointer to the SFTP client context
696  * @param[in] path Absolute or relative path
697  * @param[in] fullPath Output buffer where to store the resulting full pathname
698  **/
699 
700 void sftpGetAbsolutePath(SftpClientContext *context, const char_t *path,
701  char_t *fullPath)
702 {
703  //Relative or absolute path?
704  if(pathIsRelative(path))
705  {
706  //Copy current working directory
707  pathCopy(fullPath, context->currentDir, SFTP_CLIENT_MAX_PATH_LEN);
708  //Append the relative path
709  pathCombine(fullPath, path, SFTP_CLIENT_MAX_PATH_LEN);
710  }
711  else
712  {
713  //Copy absolute path
714  pathCopy(fullPath, path, SFTP_CLIENT_MAX_PATH_LEN);
715  }
716 
717  //Clean the resulting path
718  pathCanonicalize(fullPath);
719  pathRemoveSlash(fullPath);
720 }
721 
722 #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:1088
#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:68
void socketClose(Socket *socket)
Close an existing socket.
Definition: socket.c:2094
@ 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:168
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:210
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:2205
#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:1113
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:162
#define SshContext
Definition: ssh.h:892
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:2581
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:914
@ 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:1989
error_t sshProcessConnectionEvents(SshContext *context, SshConnection *connection)
Connection event handler.
Definition: ssh_misc.c:375
void sshCloseConnection(SshConnection *connection)
Close SSH connection.
Definition: ssh_misc.c:174
@ SSH_CHANNEL_STATE_OPEN
Definition: ssh.h:1101
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:2076
SFTP client.
@ ERROR_INVALID_TYPE
Definition: error.h:115
#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:1057
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:2182
#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:1115
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:93
#define TRACE_DEBUG(...)
Definition: debug.h:119
@ ERROR_TIMEOUT
Definition: error.h:95
char char_t
Definition: compiler_port.h:55
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:1114
uint8_t n
void sshRegisterConnectionEvents(SshContext *context, SshConnection *connection, SocketEventDesc *eventDesc)
Register connection events.
Definition: ssh_misc.c:282
#define SftpClientContext
Definition: sftp_client.h:103
SshString subsystemName
Definition: ssh_request.h:130
#define SshConnection
Definition: ssh.h:896
@ SFTP_CLIENT_STATE_CHANNEL_DATA
Definition: sftp_client.h:124
Socket * socketOpenEx(NetContext *context, uint_t type, uint_t protocol)
Create a socket.
Definition: socket.c:146
@ SSH_CHANNEL_STATE_RESERVED
Definition: ssh.h:1100
#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:376
@ 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:57
error_t sftpClientSendCommand(SftpClientContext *context)
Send SFTP request and wait for a response.
@ SSH_CHANNEL_STATE_CLOSED
Definition: ssh.h:1102
#define osMemset(p, value, length)
Definition: os_port.h:138
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:2052
@ 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:169
#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:900
void pathCopy(char_t *dest, const char_t *src, size_t maxLen)
Copy a path.
Definition: path.c:141
void pathCombine(char_t *path, const char_t *more, size_t maxLen)
Concatenate two paths.
Definition: path.c:414
systime_t osGetSystemTime(void)
Retrieve system time.
#define TRACE_VERBOSE_ARRAY(p, a, n)
Definition: debug.h:140