iPXE
Data Structures | Enumerations | Functions | Variables
ftp.c File Reference

File transfer protocol. More...

#include <stdint.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <assert.h>
#include <errno.h>
#include <ctype.h>
#include <byteswap.h>
#include <ipxe/socket.h>
#include <ipxe/tcpip.h>
#include <ipxe/in.h>
#include <ipxe/iobuf.h>
#include <ipxe/xfer.h>
#include <ipxe/open.h>
#include <ipxe/uri.h>
#include <ipxe/features.h>
#include <ipxe/ftp.h>

Go to the source code of this file.

Data Structures

struct  ftp_request
 An FTP request. More...
 
struct  ftp_control_string
 An FTP control channel string. More...
 

Enumerations

enum  ftp_state {
  FTP_CONNECT = 0, FTP_USER, FTP_PASS, FTP_TYPE,
  FTP_SIZE, FTP_PASV, FTP_RETR, FTP_WAIT,
  FTP_QUIT, FTP_DONE
}
 FTP states. More...
 

Functions

 FEATURE (FEATURE_PROTOCOL, "FTP", DHCP_EB_FEATURE_FTP, 1)
 
static void ftp_free (struct refcnt *refcnt)
 Free FTP request. More...
 
static void ftp_done (struct ftp_request *ftp, int rc)
 Mark FTP operation as complete. More...
 
static const char * ftp_uri_path (struct ftp_request *ftp)
 Retrieve FTP pathname. More...
 
static const char * ftp_user (struct ftp_request *ftp)
 Retrieve FTP user. More...
 
static const char * ftp_password (struct ftp_request *ftp)
 Retrieve FTP password. More...
 
static void ftp_parse_value (char **text, uint8_t *value, size_t len)
 Parse FTP byte sequence value. More...
 
static void ftp_next_state (struct ftp_request *ftp)
 Move to next state and send the appropriate FTP control string. More...
 
static void ftp_reply (struct ftp_request *ftp)
 Handle an FTP control channel response. More...
 
static int ftp_control_deliver (struct ftp_request *ftp, struct io_buffer *iobuf, struct xfer_metadata *meta __unused)
 Handle new data arriving on FTP control channel. More...
 
static void ftp_data_closed (struct ftp_request *ftp, int rc)
 Handle FTP data channel being closed. More...
 
static int ftp_check_string (const char *string)
 Check validity of FTP control channel string. More...
 
static int ftp_open (struct interface *xfer, struct uri *uri)
 Initiate an FTP connection. More...
 

Variables

static struct ftp_control_string ftp_strings []
 FTP control channel strings. More...
 
static struct interface_operation ftp_control_operations []
 FTP control channel interface operations. More...
 
static struct interface_descriptor ftp_control_desc
 FTP control channel interface descriptor. More...
 
static struct interface_operation ftp_data_operations []
 FTP data channel interface operations. More...
 
static struct interface_descriptor ftp_data_desc
 FTP data channel interface descriptor. More...
 
static struct interface_operation ftp_xfer_operations []
 FTP data transfer interface operations. More...
 
static struct interface_descriptor ftp_xfer_desc
 FTP data transfer interface descriptor. More...
 
struct uri_opener ftp_uri_opener __uri_opener
 FTP URI opener. More...
 

Detailed Description

File transfer protocol.

Definition in file ftp.c.

Enumeration Type Documentation

◆ ftp_state

enum ftp_state

FTP states.

These must be sequential, i.e. a successful FTP session must pass through each of these states in order.

Enumerator
FTP_CONNECT 
FTP_USER 
FTP_PASS 
FTP_TYPE 
FTP_SIZE 
FTP_PASV 
FTP_RETR 
FTP_WAIT 
FTP_QUIT 
FTP_DONE 

Definition at line 52 of file ftp.c.

52  {
53  FTP_CONNECT = 0,
54  FTP_USER,
55  FTP_PASS,
56  FTP_TYPE,
57  FTP_SIZE,
58  FTP_PASV,
59  FTP_RETR,
60  FTP_WAIT,
61  FTP_QUIT,
62  FTP_DONE,
63 };
Definition: ftp.c:57
Definition: ftp.c:56
Definition: ftp.c:62
Definition: ftp.c:59
Definition: ftp.c:58
Definition: ftp.c:60
Definition: ftp.c:61
Definition: ftp.c:55
Definition: ftp.c:54

Function Documentation

◆ FEATURE()

FEATURE ( FEATURE_PROTOCOL  ,
"FTP"  ,
DHCP_EB_FEATURE_FTP  ,
 
)

◆ ftp_free()

static void ftp_free ( struct refcnt refcnt)
static

Free FTP request.

Parameters
refcntReference counter

Definition at line 101 of file ftp.c.

101  {
102  struct ftp_request *ftp =
103  container_of ( refcnt, struct ftp_request, refcnt );
104 
105  DBGC ( ftp, "FTP %p freed\n", ftp );
106 
107  uri_put ( ftp->uri );
108  free ( ftp );
109 }
static void uri_put(struct uri *uri)
Decrement URI reference count.
Definition: uri.h:188
struct uri * uri
URI being fetched.
Definition: ftp.c:76
#define DBGC(...)
Definition: compiler.h:505
A reference counter.
Definition: refcnt.h:26
#define container_of(ptr, type, field)
Get containing structure.
Definition: stddef.h:35
An FTP request.
Definition: ftp.c:69
static void(* free)(struct refcnt *refcnt))
Definition: refcnt.h:54

References container_of, DBGC, free, ftp_request::uri, and uri_put().

Referenced by ftp_open().

◆ ftp_done()

static void ftp_done ( struct ftp_request ftp,
int  rc 
)
static

Mark FTP operation as complete.

Parameters
ftpFTP request
rcReturn status code

Definition at line 117 of file ftp.c.

117  {
118 
119  DBGC ( ftp, "FTP %p completed (%s)\n", ftp, strerror ( rc ) );
120 
121  /* Close all data transfer interfaces */
122  intf_shutdown ( &ftp->data, rc );
123  intf_shutdown ( &ftp->control, rc );
124  intf_shutdown ( &ftp->xfer, rc );
125 }
struct arbelprm_rc_send_wqe rc
Definition: arbel.h:14
void intf_shutdown(struct interface *intf, int rc)
Shut down an object interface.
Definition: interface.c:273
#define DBGC(...)
Definition: compiler.h:505
char * strerror(int errno)
Retrieve string representation of error number.
Definition: strerror.c:78
struct interface xfer
Data transfer interface.
Definition: ftp.c:73
struct interface data
FTP data channel interface.
Definition: ftp.c:80
struct interface control
FTP control channel interface.
Definition: ftp.c:78

References ftp_request::control, ftp_request::data, DBGC, intf_shutdown(), rc, strerror(), and ftp_request::xfer.

Referenced by ftp_data_closed(), ftp_open(), and ftp_reply().

◆ ftp_uri_path()

static const char* ftp_uri_path ( struct ftp_request ftp)
static

Retrieve FTP pathname.

Parameters
ftpFTP request
Return values
pathFTP pathname

Definition at line 151 of file ftp.c.

151  {
152  return ftp->uri->path;
153 }
struct uri * uri
URI being fetched.
Definition: ftp.c:76
const char * path
Path.
Definition: uri.h:66

References uri::path, and ftp_request::uri.

◆ ftp_user()

static const char* ftp_user ( struct ftp_request ftp)
static

Retrieve FTP user.

Parameters
ftpFTP request
Return values
userFTP user

Definition at line 161 of file ftp.c.

161  {
162  static char *ftp_default_user = "anonymous";
163  return ftp->uri->user ? ftp->uri->user : ftp_default_user;
164 }
struct uri * uri
URI being fetched.
Definition: ftp.c:76
const char * user
User name.
Definition: uri.h:58

References ftp_request::uri, and uri::user.

◆ ftp_password()

static const char* ftp_password ( struct ftp_request ftp)
static

Retrieve FTP password.

Parameters
ftpFTP request
Return values
passwordFTP password

Definition at line 172 of file ftp.c.

172  {
173  static char *ftp_default_password = "ipxe@ipxe.org";
174  return ftp->uri->password ? ftp->uri->password : ftp_default_password;
175 }
struct uri * uri
URI being fetched.
Definition: ftp.c:76
const char * password
Password.
Definition: uri.h:60

References uri::password, and ftp_request::uri.

◆ ftp_parse_value()

static void ftp_parse_value ( char **  text,
uint8_t value,
size_t  len 
)
static

Parse FTP byte sequence value.

Parameters
textText string
valueValue buffer
lenLength of value buffer

This parses an FTP byte sequence value (e.g. the "aaa,bbb,ccc,ddd" form for IP addresses in PORT commands) into a byte sequence. *text will be updated to point beyond the end of the parsed byte sequence.

This function is safe in the presence of malformed data, though the output is undefined.

Definition at line 206 of file ftp.c.

206  {
207  do {
208  *(value++) = strtoul ( *text, text, 10 );
209  if ( **text )
210  (*text)++;
211  } while ( --len );
212 }
unsigned long strtoul(const char *string, char **endp, int base)
Convert string to numeric value.
Definition: string.c:456
pseudo_bit_t value[0x00020]
Definition: arbel.h:13
uint32_t len
Length.
Definition: ena.h:14

References len, strtoul(), and value.

Referenced by ftp_reply().

◆ ftp_next_state()

static void ftp_next_state ( struct ftp_request ftp)
static

Move to next state and send the appropriate FTP control string.

Parameters
ftpFTP request

Definition at line 220 of file ftp.c.

220  {
221  struct ftp_control_string *ftp_string;
222  const char *literal;
223  const char *variable;
224 
225  /* Move to next state */
226  if ( ftp->state < FTP_DONE )
227  ftp->state++;
228 
229  /* Send control string if needed */
230  ftp_string = &ftp_strings[ftp->state];
231  literal = ftp_string->literal;
232  variable = ( ftp_string->variable ?
233  ftp_string->variable ( ftp ) : "" );
234  if ( literal ) {
235  DBGC ( ftp, "FTP %p sending %s%s\n", ftp, literal, variable );
236  xfer_printf ( &ftp->control, "%s%s\r\n", literal, variable );
237  }
238 }
const char *(* variable)(struct ftp_request *ftp)
Variable portion.
Definition: ftp.c:142
#define DBGC(...)
Definition: compiler.h:505
An FTP control channel string.
Definition: ftp.c:134
Definition: ftp.c:62
int xfer_printf(struct interface *intf, const char *format,...)
Deliver formatted string.
Definition: xfer.c:333
enum ftp_state state
Current state.
Definition: ftp.c:83
const char * literal
Literal portion.
Definition: ftp.c:136
static struct ftp_control_string ftp_strings[]
FTP control channel strings.
Definition: ftp.c:178
struct interface control
FTP control channel interface.
Definition: ftp.c:78

References ftp_request::control, DBGC, FTP_DONE, ftp_strings, ftp_control_string::literal, ftp_request::state, ftp_control_string::variable, and xfer_printf().

Referenced by ftp_data_closed(), and ftp_reply().

◆ ftp_reply()

static void ftp_reply ( struct ftp_request ftp)
static

Handle an FTP control channel response.

Parameters
ftpFTP request

This is called once we have received a complete response line.

Definition at line 247 of file ftp.c.

247  {
248  char status_major = ftp->status_text[0];
249  char separator = ftp->status_text[3];
250 
251  DBGC ( ftp, "FTP %p received status %s\n", ftp, ftp->status_text );
252 
253  /* Ignore malformed lines */
254  if ( separator != ' ' )
255  return;
256 
257  /* Ignore "intermediate" responses (1xx codes) */
258  if ( status_major == '1' )
259  return;
260 
261  /* If the SIZE command is not supported by the server, we go to
262  * the next step.
263  */
264  if ( ( status_major == '5' ) && ( ftp->state == FTP_SIZE ) ) {
265  ftp_next_state ( ftp );
266  return;
267  }
268 
269  /* Anything other than success (2xx) or, in the case of a
270  * repsonse to a "USER" command, a password prompt (3xx), is a
271  * fatal error.
272  */
273  if ( ! ( ( status_major == '2' ) ||
274  ( ( status_major == '3' ) && ( ftp->state == FTP_USER ) ) ) ){
275  /* Flag protocol error and close connections */
276  ftp_done ( ftp, -EPROTO );
277  return;
278  }
279 
280  /* Parse file size */
281  if ( ftp->state == FTP_SIZE ) {
282  size_t filesize;
283  char *endptr;
284 
285  /* Parse size */
286  filesize = strtoul ( ftp->filesize, &endptr, 10 );
287  if ( *endptr != '\0' ) {
288  DBGC ( ftp, "FTP %p invalid SIZE \"%s\"\n",
289  ftp, ftp->filesize );
290  ftp_done ( ftp, -EPROTO );
291  return;
292  }
293 
294  /* Use seek() to notify recipient of filesize */
295  DBGC ( ftp, "FTP %p file size is %zd bytes\n", ftp, filesize );
296  xfer_seek ( &ftp->xfer, filesize );
297  xfer_seek ( &ftp->xfer, 0 );
298  }
299 
300  /* Open passive connection when we get "PASV" response */
301  if ( ftp->state == FTP_PASV ) {
302  char *ptr = ftp->passive_text;
303  union {
304  struct sockaddr_in sin;
305  struct sockaddr sa;
306  } sa;
307  int rc;
308 
309  sa.sin.sin_family = AF_INET;
310  ftp_parse_value ( &ptr, ( uint8_t * ) &sa.sin.sin_addr,
311  sizeof ( sa.sin.sin_addr ) );
312  ftp_parse_value ( &ptr, ( uint8_t * ) &sa.sin.sin_port,
313  sizeof ( sa.sin.sin_port ) );
314  if ( ( rc = xfer_open_socket ( &ftp->data, SOCK_STREAM,
315  &sa.sa, NULL ) ) != 0 ) {
316  DBGC ( ftp, "FTP %p could not open data connection\n",
317  ftp );
318  ftp_done ( ftp, rc );
319  return;
320  }
321  }
322 
323  /* Move to next state and send control string */
324  ftp_next_state ( ftp );
325 
326 }
static void ftp_done(struct ftp_request *ftp, int rc)
Mark FTP operation as complete.
Definition: ftp.c:117
struct arbelprm_rc_send_wqe rc
Definition: arbel.h:14
Definition: ftp.c:57
unsigned long strtoul(const char *string, char **endp, int base)
Convert string to numeric value.
Definition: string.c:456
int xfer_open_socket(struct interface *intf, int semantics, struct sockaddr *peer, struct sockaddr *local)
Open socket.
Definition: open.c:141
#define DBGC(...)
Definition: compiler.h:505
IPv4 socket address.
Definition: in.h:82
static void ftp_parse_value(char **text, uint8_t *value, size_t len)
Parse FTP byte sequence value.
Definition: ftp.c:206
char status_text[5]
FTP status code, as text.
Definition: ftp.c:89
struct sockaddr sa
Definition: syslog.c:55
#define EPROTO
Protocol error.
Definition: errno.h:624
int xfer_seek(struct interface *intf, off_t offset)
Seek to position.
Definition: xfer.c:350
Generalized socket address structure.
Definition: socket.h:96
char filesize[20]
File size, as text.
Definition: ftp.c:93
Definition: ftp.c:58
#define SOCK_STREAM
Definition: socket.h:24
unsigned char uint8_t
Definition: stdint.h:10
static void ftp_next_state(struct ftp_request *ftp)
Move to next state and send the appropriate FTP control string.
Definition: ftp.c:220
enum ftp_state state
Current state.
Definition: ftp.c:83
Definition: ftp.c:54
struct interface xfer
Data transfer interface.
Definition: ftp.c:73
struct sockaddr_in sin
Definition: syslog.c:57
struct interface data
FTP data channel interface.
Definition: ftp.c:80
#define NULL
NULL pointer (VOID *)
Definition: Base.h:362
#define AF_INET
IPv4 Internet addresses.
Definition: socket.h:63
char passive_text[24]
Passive-mode parameters, as text.
Definition: ftp.c:91

References AF_INET, ftp_request::data, DBGC, EPROTO, ftp_request::filesize, ftp_done(), ftp_next_state(), ftp_parse_value(), FTP_PASV, FTP_SIZE, FTP_USER, NULL, ftp_request::passive_text, rc, sa, sin, SOCK_STREAM, ftp_request::state, ftp_request::status_text, strtoul(), ftp_request::xfer, xfer_open_socket(), and xfer_seek().

Referenced by ftp_control_deliver().

◆ ftp_control_deliver()

static int ftp_control_deliver ( struct ftp_request ftp,
struct io_buffer iobuf,
struct xfer_metadata *meta  __unused 
)
static

Handle new data arriving on FTP control channel.

Parameters
ftpFTP request
iobI/O buffer
metaData transfer metadata
Return values
rcReturn status code

Data is collected until a complete line is received, at which point its information is passed to ftp_reply().

Definition at line 339 of file ftp.c.

341  {
342  char *data = iobuf->data;
343  size_t len = iob_len ( iobuf );
344  char *recvbuf = ftp->recvbuf;
345  size_t recvsize = ftp->recvsize;
346  char c;
347 
348  while ( len-- ) {
349  c = *(data++);
350  if ( ( c == '\r' ) || ( c == '\n' ) ) {
351  /* End of line: call ftp_reply() to handle
352  * completed reply. Avoid calling ftp_reply()
353  * twice if we receive both \r and \n.
354  */
355  if ( recvbuf != ftp->status_text )
356  ftp_reply ( ftp );
357  /* Start filling up the status code buffer */
358  recvbuf = ftp->status_text;
359  recvsize = sizeof ( ftp->status_text ) - 1;
360  } else if ( ( ftp->state == FTP_PASV ) && ( c == '(' ) ) {
361  /* Start filling up the passive parameter buffer */
362  recvbuf = ftp->passive_text;
363  recvsize = sizeof ( ftp->passive_text ) - 1;
364  } else if ( ( ftp->state == FTP_PASV ) && ( c == ')' ) ) {
365  /* Stop filling the passive parameter buffer */
366  recvsize = 0;
367  } else if ( ( ftp->state == FTP_SIZE ) && ( c == ' ' ) ) {
368  /* Start filling up the file size buffer */
369  recvbuf = ftp->filesize;
370  recvsize = sizeof ( ftp->filesize ) - 1;
371  } else {
372  /* Fill up buffer if applicable */
373  if ( recvsize > 0 ) {
374  *(recvbuf++) = c;
375  recvsize--;
376  }
377  }
378  }
379 
380  /* Store for next invocation */
381  ftp->recvbuf = recvbuf;
382  ftp->recvsize = recvsize;
383 
384  /* Free I/O buffer */
385  free_iob ( iobuf );
386 
387  return 0;
388 }
uint32_t c
Definition: md4.c:30
Definition: ftp.c:57
void free_iob(struct io_buffer *iobuf)
Free I/O buffer.
Definition: iobuf.c:145
char * recvbuf
Buffer to be filled with data received via the control channel.
Definition: ftp.c:85
char status_text[5]
FTP status code, as text.
Definition: ftp.c:89
size_t recvsize
Remaining size of recvbuf.
Definition: ftp.c:87
char filesize[20]
File size, as text.
Definition: ftp.c:93
static size_t iob_len(struct io_buffer *iobuf)
Calculate length of data in an I/O buffer.
Definition: iobuf.h:151
Definition: ftp.c:58
static void ftp_reply(struct ftp_request *ftp)
Handle an FTP control channel response.
Definition: ftp.c:247
uint32_t len
Length.
Definition: ena.h:14
void * data
Start of data.
Definition: iobuf.h:44
enum ftp_state state
Current state.
Definition: ftp.c:83
struct arbelprm_port_state_change_st data
Message.
Definition: arbel.h:12
char passive_text[24]
Passive-mode parameters, as text.
Definition: ftp.c:91

References c, data, io_buffer::data, ftp_request::filesize, free_iob(), FTP_PASV, ftp_reply(), FTP_SIZE, iob_len(), len, ftp_request::passive_text, ftp_request::recvbuf, ftp_request::recvsize, ftp_request::state, and ftp_request::status_text.

◆ ftp_data_closed()

static void ftp_data_closed ( struct ftp_request ftp,
int  rc 
)
static

Handle FTP data channel being closed.

Parameters
ftpFTP request
rcReason for closure

When the data channel is closed, the control channel should be left alone; the server will send a completion message via the control channel which we'll pick up.

If the data channel is closed due to an error, we abort the request.

Definition at line 418 of file ftp.c.

418  {
419 
420  DBGC ( ftp, "FTP %p data connection closed: %s\n",
421  ftp, strerror ( rc ) );
422 
423  /* If there was an error, close control channel and record status */
424  if ( rc ) {
425  ftp_done ( ftp, rc );
426  } else {
427  ftp_next_state ( ftp );
428  }
429 }
static void ftp_done(struct ftp_request *ftp, int rc)
Mark FTP operation as complete.
Definition: ftp.c:117
struct arbelprm_rc_send_wqe rc
Definition: arbel.h:14
#define DBGC(...)
Definition: compiler.h:505
char * strerror(int errno)
Retrieve string representation of error number.
Definition: strerror.c:78
static void ftp_next_state(struct ftp_request *ftp)
Move to next state and send the appropriate FTP control string.
Definition: ftp.c:220

References DBGC, ftp_done(), ftp_next_state(), rc, and strerror().

◆ ftp_check_string()

static int ftp_check_string ( const char *  string)
static

Check validity of FTP control channel string.

Parameters
stringString
Return values
rcReturn status code

Definition at line 469 of file ftp.c.

469  {
470  char c;
471 
472  /* The FTP control channel is line-based. Check for invalid
473  * non-printable characters (e.g. newlines).
474  */
475  while ( ( c = *(string++) ) ) {
476  if ( ! isprint ( c ) )
477  return -EINVAL;
478  }
479  return 0;
480 }
uint32_t c
Definition: md4.c:30
#define EINVAL
Invalid argument.
Definition: errno.h:428
static int isprint(int character)
Check if character is printable.
Definition: ctype.h:86

References c, EINVAL, and isprint().

Referenced by ftp_open().

◆ ftp_open()

static int ftp_open ( struct interface xfer,
struct uri uri 
)
static

Initiate an FTP connection.

Parameters
xferData transfer interface
uriUniform Resource Identifier
Return values
rcReturn status code

Definition at line 489 of file ftp.c.

489  {
490  struct ftp_request *ftp;
491  struct sockaddr_tcpip server;
492  int rc;
493 
494  /* Sanity checks */
495  if ( ! uri->host )
496  return -EINVAL;
497  if ( ! uri->path )
498  return -EINVAL;
499  if ( ( rc = ftp_check_string ( uri->path ) ) != 0 )
500  return rc;
501  if ( uri->user && ( ( rc = ftp_check_string ( uri->user ) ) != 0 ) )
502  return rc;
503  if ( uri->password &&
504  ( ( rc = ftp_check_string ( uri->password ) ) != 0 ) )
505  return rc;
506 
507  /* Allocate and populate structure */
508  ftp = zalloc ( sizeof ( *ftp ) );
509  if ( ! ftp )
510  return -ENOMEM;
511  ref_init ( &ftp->refcnt, ftp_free );
512  intf_init ( &ftp->xfer, &ftp_xfer_desc, &ftp->refcnt );
513  intf_init ( &ftp->control, &ftp_control_desc, &ftp->refcnt );
514  intf_init ( &ftp->data, &ftp_data_desc, &ftp->refcnt );
515  ftp->uri = uri_get ( uri );
516  ftp->recvbuf = ftp->status_text;
517  ftp->recvsize = sizeof ( ftp->status_text ) - 1;
518 
519  DBGC ( ftp, "FTP %p fetching %s\n", ftp, ftp->uri->path );
520 
521  /* Open control connection */
522  memset ( &server, 0, sizeof ( server ) );
523  server.st_port = htons ( uri_port ( uri, FTP_PORT ) );
524  if ( ( rc = xfer_open_named_socket ( &ftp->control, SOCK_STREAM,
525  ( struct sockaddr * ) &server,
526  uri->host, NULL ) ) != 0 )
527  goto err;
528 
529  /* Attach to parent interface, mortalise self, and return */
530  intf_plug_plug ( &ftp->xfer, xfer );
531  ref_put ( &ftp->refcnt );
532  return 0;
533 
534  err:
535  DBGC ( ftp, "FTP %p could not create request: %s\n",
536  ftp, strerror ( rc ) );
537  ftp_done ( ftp, rc );
538  ref_put ( &ftp->refcnt );
539  return rc;
540 }
static void ftp_done(struct ftp_request *ftp, int rc)
Mark FTP operation as complete.
Definition: ftp.c:117
#define EINVAL
Invalid argument.
Definition: errno.h:428
TCP/IP socket address.
Definition: tcpip.h:75
struct arbelprm_rc_send_wqe rc
Definition: arbel.h:14
static struct uri * uri_get(struct uri *uri)
Increment URI reference count.
Definition: uri.h:177
#define ref_init(refcnt, free)
Initialise a reference counter.
Definition: refcnt.h:64
struct uri * uri
URI being fetched.
Definition: ftp.c:76
#define DBGC(...)
Definition: compiler.h:505
void intf_plug_plug(struct interface *a, struct interface *b)
Plug two object interfaces together.
Definition: interface.c:102
char * recvbuf
Buffer to be filled with data received via the control channel.
Definition: ftp.c:85
static int ftp_check_string(const char *string)
Check validity of FTP control channel string.
Definition: ftp.c:469
char status_text[5]
FTP status code, as text.
Definition: ftp.c:89
#define ENOMEM
Not enough space.
Definition: errno.h:534
static struct interface_descriptor ftp_control_desc
FTP control channel interface descriptor.
Definition: ftp.c:397
size_t recvsize
Remaining size of recvbuf.
Definition: ftp.c:87
static void ftp_free(struct refcnt *refcnt)
Free FTP request.
Definition: ftp.c:101
const char * path
Path.
Definition: uri.h:66
An FTP request.
Definition: ftp.c:69
Generalized socket address structure.
Definition: socket.h:96
static struct interface_descriptor ftp_data_desc
FTP data channel interface descriptor.
Definition: ftp.c:437
char * strerror(int errno)
Retrieve string representation of error number.
Definition: strerror.c:78
void * zalloc(size_t size)
Allocate cleared memory.
Definition: malloc.c:624
#define SOCK_STREAM
Definition: socket.h:24
const char * host
Host name.
Definition: uri.h:62
unsigned int uri_port(const struct uri *uri, unsigned int default_port)
Get port from URI.
Definition: uri.c:441
const char * password
Password.
Definition: uri.h:60
struct interface xfer
Data transfer interface.
Definition: ftp.c:73
const char * user
User name.
Definition: uri.h:58
A Uniform Resource Identifier.
Definition: uri.h:50
static struct interface_descriptor ftp_xfer_desc
FTP data transfer interface descriptor.
Definition: ftp.c:453
static void intf_init(struct interface *intf, struct interface_descriptor *desc, struct refcnt *refcnt)
Initialise an object interface.
Definition: interface.h:173
struct interface data
FTP data channel interface.
Definition: ftp.c:80
#define NULL
NULL pointer (VOID *)
Definition: Base.h:362
struct interface control
FTP control channel interface.
Definition: ftp.c:78
#define htons(value)
Definition: byteswap.h:135
int xfer_open_named_socket(struct interface *xfer, int semantics, struct sockaddr *peer, const char *name, struct sockaddr *local)
Open named socket.
Definition: resolv.c:402
#define ref_put(refcnt)
Drop reference to object.
Definition: refcnt.h:106
void * memset(void *dest, int character, size_t len) __nonnull
struct refcnt refcnt
Reference counter.
Definition: ftp.c:71
#define FTP_PORT
FTP default port.
Definition: ftp.h:13

References ftp_request::control, ftp_request::data, DBGC, EINVAL, ENOMEM, ftp_check_string(), ftp_control_desc, ftp_data_desc, ftp_done(), ftp_free(), FTP_PORT, ftp_xfer_desc, uri::host, htons, intf_init(), intf_plug_plug(), memset(), NULL, uri::password, uri::path, rc, ftp_request::recvbuf, ftp_request::recvsize, ref_init, ref_put, ftp_request::refcnt, SOCK_STREAM, sockaddr_tcpip::st_port, ftp_request::status_text, strerror(), ftp_request::uri, uri_get(), uri_port(), uri::user, ftp_request::xfer, xfer_open_named_socket(), and zalloc().

Variable Documentation

◆ ftp_strings

struct ftp_control_string ftp_strings[]
static
Initial value:
= {
[FTP_CONNECT] = { NULL, NULL },
[FTP_USER] = { "USER ", ftp_user },
[FTP_PASS] = { "PASS ", ftp_password },
[FTP_TYPE] = { "TYPE I", NULL },
[FTP_SIZE] = { "SIZE ", ftp_uri_path },
[FTP_PASV] = { "PASV", NULL },
[FTP_RETR] = { "RETR ", ftp_uri_path },
[FTP_WAIT] = { NULL, NULL },
[FTP_QUIT] = { "QUIT", NULL },
[FTP_DONE] = { NULL, NULL },
}
Definition: ftp.c:57
static const char * ftp_user(struct ftp_request *ftp)
Retrieve FTP user.
Definition: ftp.c:161
Definition: ftp.c:56
Definition: ftp.c:62
Definition: ftp.c:59
static const char * ftp_uri_path(struct ftp_request *ftp)
Retrieve FTP pathname.
Definition: ftp.c:151
Definition: ftp.c:58
Definition: ftp.c:60
Definition: ftp.c:61
static const char * ftp_password(struct ftp_request *ftp)
Retrieve FTP password.
Definition: ftp.c:172
Definition: ftp.c:55
Definition: ftp.c:54
#define NULL
NULL pointer (VOID *)
Definition: Base.h:362

FTP control channel strings.

Definition at line 178 of file ftp.c.

Referenced by ftp_next_state().

◆ ftp_control_operations

struct interface_operation ftp_control_operations[]
static
Initial value:
= {
}
static void ftp_done(struct ftp_request *ftp, int rc)
Mark FTP operation as complete.
Definition: ftp.c:117
void intf_close(struct interface *intf, int rc)
Close an object interface.
Definition: interface.c:244
An FTP request.
Definition: ftp.c:69
static int ftp_control_deliver(struct ftp_request *ftp, struct io_buffer *iobuf, struct xfer_metadata *meta __unused)
Handle new data arriving on FTP control channel.
Definition: ftp.c:339
#define INTF_OP(op_type, object_type, op_func)
Define an object interface operation.
Definition: interface.h:32
int xfer_deliver(struct interface *intf, struct io_buffer *iobuf, struct xfer_metadata *meta)
Deliver datagram.
Definition: xfer.c:193

FTP control channel interface operations.

Definition at line 391 of file ftp.c.

◆ ftp_control_desc

struct interface_descriptor ftp_control_desc
static
Initial value:
=
static struct interface_operation ftp_control_operations[]
FTP control channel interface operations.
Definition: ftp.c:391
An FTP request.
Definition: ftp.c:69
uint32_t control
Control.
Definition: myson.h:14
#define INTF_DESC(object_type, intf, operations)
Define an object interface descriptor.
Definition: interface.h:65

FTP control channel interface descriptor.

Definition at line 397 of file ftp.c.

Referenced by ftp_open().

◆ ftp_data_operations

struct interface_operation ftp_data_operations[]
static
Initial value:
= {
}
void intf_close(struct interface *intf, int rc)
Close an object interface.
Definition: interface.c:244
static void ftp_data_closed(struct ftp_request *ftp, int rc)
Handle FTP data channel being closed.
Definition: ftp.c:418
An FTP request.
Definition: ftp.c:69
#define INTF_OP(op_type, object_type, op_func)
Define an object interface operation.
Definition: interface.h:32

FTP data channel interface operations.

Definition at line 432 of file ftp.c.

◆ ftp_data_desc

struct interface_descriptor ftp_data_desc
static
Initial value:
=
xfer )
static struct interface_operation ftp_data_operations[]
FTP data channel interface operations.
Definition: ftp.c:432
An FTP request.
Definition: ftp.c:69
#define INTF_DESC_PASSTHRU(object_type, intf, operations, passthru)
Define an object interface descriptor with pass-through interface.
Definition: interface.h:82
struct arbelprm_port_state_change_st data
Message.
Definition: arbel.h:12

FTP data channel interface descriptor.

Definition at line 437 of file ftp.c.

Referenced by ftp_open().

◆ ftp_xfer_operations

struct interface_operation ftp_xfer_operations[]
static
Initial value:
= {
}
static void ftp_done(struct ftp_request *ftp, int rc)
Mark FTP operation as complete.
Definition: ftp.c:117
void intf_close(struct interface *intf, int rc)
Close an object interface.
Definition: interface.c:244
An FTP request.
Definition: ftp.c:69
#define INTF_OP(op_type, object_type, op_func)
Define an object interface operation.
Definition: interface.h:32

FTP data transfer interface operations.

Definition at line 448 of file ftp.c.

◆ ftp_xfer_desc

struct interface_descriptor ftp_xfer_desc
static
Initial value:
=
data )
An FTP request.
Definition: ftp.c:69
static struct interface_operation ftp_xfer_operations[]
FTP data transfer interface operations.
Definition: ftp.c:448
#define INTF_DESC_PASSTHRU(object_type, intf, operations, passthru)
Define an object interface descriptor with pass-through interface.
Definition: interface.h:82
struct arbelprm_port_state_change_st data
Message.
Definition: arbel.h:12

FTP data transfer interface descriptor.

Definition at line 453 of file ftp.c.

Referenced by ftp_open().

◆ __uri_opener

struct uri_opener ftp_uri_opener __uri_opener
Initial value:
= {
.scheme = "ftp",
.open = ftp_open,
}
static int ftp_open(struct interface *xfer, struct uri *uri)
Initiate an FTP connection.
Definition: ftp.c:489

FTP URI opener.

Definition at line 543 of file ftp.c.