8 #include <gpxe/socket.h>
9 #include <gpxe/tcpip.h>
11 #include <gpxe/xfer.h>
12 #include <gpxe/open.h>
18 * File transfer protocol
25 * These @b must be sequential, i.e. a successful FTP session must
26 * pass through each of these states in order.
44 /** Reference counter */
46 /** Data transfer interface */
47 struct xfer_interface xfer;
49 /** URI being fetched */
51 /** FTP control channel interface */
52 struct xfer_interface control;
53 /** FTP data channel interface */
54 struct xfer_interface data;
58 /** Buffer to be filled with data received via the control channel */
60 /** Remaining size of recvbuf */
62 /** FTP status code, as text */
64 /** Passive-mode parameters, as text */
65 char passive_text[24]; /* "aaa,bbb,ccc,ddd,eee,fff" */
71 * @v refcnt Reference counter
73 static void ftp_free ( struct refcnt *refcnt ) {
74 struct ftp_request *ftp =
75 container_of ( refcnt, struct ftp_request, refcnt );
77 DBGC ( ftp, "FTP %p freed\n", ftp );
84 * Mark FTP operation as complete
87 * @v rc Return status code
89 static void ftp_done ( struct ftp_request *ftp, int rc ) {
91 DBGC ( ftp, "FTP %p completed (%s)\n", ftp, strerror ( rc ) );
93 /* Close all data transfer interfaces */
94 xfer_nullify ( &ftp->xfer );
95 xfer_close ( &ftp->xfer, rc );
96 xfer_nullify ( &ftp->control );
97 xfer_close ( &ftp->control, rc );
98 xfer_nullify ( &ftp->data );
99 xfer_close ( &ftp->data, rc );
102 /*****************************************************************************
104 * FTP control channel
109 * FTP control channel strings
111 * These are used as printf() format strings. Since only one of them
112 * (RETR) takes an argument, we always supply that argument to the
115 static const char * ftp_strings[] = {
117 [FTP_USER] = "USER anonymous\r\n",
118 [FTP_PASS] = "PASS etherboot@etherboot.org\r\n",
119 [FTP_TYPE] = "TYPE I\r\n",
120 [FTP_PASV] = "PASV\r\n",
121 [FTP_RETR] = "RETR %s\r\n",
122 [FTP_QUIT] = "QUIT\r\n",
127 * Handle control channel being closed
129 * @v control FTP control channel interface
130 * @v rc Reason for close
132 * When the control channel is closed, the data channel must also be
133 * closed, if it is currently open.
135 static void ftp_control_close ( struct xfer_interface *control, int rc ) {
136 struct ftp_request *ftp =
137 container_of ( control, struct ftp_request, control );
139 DBGC ( ftp, "FTP %p control connection closed: %s\n",
140 ftp, strerror ( rc ) );
142 /* Complete FTP operation */
143 ftp_done ( ftp, rc );
147 * Parse FTP byte sequence value
149 * @v text Text string
150 * @v value Value buffer
151 * @v len Length of value buffer
153 * This parses an FTP byte sequence value (e.g. the "aaa,bbb,ccc,ddd"
154 * form for IP addresses in PORT commands) into a byte sequence. @c
155 * *text will be updated to point beyond the end of the parsed byte
158 * This function is safe in the presence of malformed data, though the
159 * output is undefined.
161 static void ftp_parse_value ( char **text, uint8_t *value, size_t len ) {
163 *(value++) = strtoul ( *text, text, 10 );
170 * Handle an FTP control channel response
174 * This is called once we have received a complete response line.
176 static void ftp_reply ( struct ftp_request *ftp ) {
177 char status_major = ftp->status_text[0];
178 char separator = ftp->status_text[3];
180 DBGC ( ftp, "FTP %p received status %s\n", ftp, ftp->status_text );
182 /* Ignore malformed lines */
183 if ( separator != ' ' )
186 /* Ignore "intermediate" responses (1xx codes) */
187 if ( status_major == '1' )
190 /* Anything other than success (2xx) or, in the case of a
191 * repsonse to a "USER" command, a password prompt (3xx), is a
194 if ( ! ( ( status_major == '2' ) ||
195 ( ( status_major == '3' ) && ( ftp->state == FTP_USER ) ) ) ){
196 /* Flag protocol error and close connections */
197 ftp_done ( ftp, -EPROTO );
200 /* Open passive connection when we get "PASV" response */
201 if ( ftp->state == FTP_PASV ) {
202 char *ptr = ftp->passive_text;
204 struct sockaddr_in sin;
209 sa.sin.sin_family = AF_INET;
210 ftp_parse_value ( &ptr, ( uint8_t * ) &sa.sin.sin_addr,
211 sizeof ( sa.sin.sin_addr ) );
212 ftp_parse_value ( &ptr, ( uint8_t * ) &sa.sin.sin_port,
213 sizeof ( sa.sin.sin_port ) );
214 if ( ( rc = xfer_open_socket ( &ftp->data, SOCK_STREAM,
215 &sa.sa, NULL ) ) != 0 ) {
216 DBGC ( ftp, "FTP %p could not open data connection\n",
218 ftp_done ( ftp, rc );
223 /* Move to next state */
224 if ( ftp->state < FTP_DONE )
227 /* Send control string */
228 if ( ftp->state < FTP_DONE ) {
229 DBGC ( ftp, "FTP %p sending ", ftp );
230 DBGC ( ftp, ftp_strings[ftp->state], ftp->uri->path );
231 xfer_printf ( &ftp->control, ftp_strings[ftp->state],
237 * Handle new data arriving on FTP control channel
239 * @v control FTP control channel interface
241 * @v len Length of new data
243 * Data is collected until a complete line is received, at which point
244 * its information is passed to ftp_reply().
246 static int ftp_control_deliver_raw ( struct xfer_interface *control,
247 const void *data, size_t len ) {
248 struct ftp_request *ftp =
249 container_of ( control, struct ftp_request, control );
250 char *recvbuf = ftp->recvbuf;
251 size_t recvsize = ftp->recvsize;
255 c = * ( ( char * ) data++ );
259 /* End of line: call ftp_reply() to handle
260 * completed reply. Avoid calling ftp_reply()
261 * twice if we receive both \r and \n.
265 /* Start filling up the status code buffer */
266 recvbuf = ftp->status_text;
267 recvsize = sizeof ( ftp->status_text ) - 1;
270 /* Start filling up the passive parameter buffer */
271 recvbuf = ftp->passive_text;
272 recvsize = sizeof ( ftp->passive_text ) - 1;
275 /* Stop filling the passive parameter buffer */
279 /* Fill up buffer if applicable */
280 if ( recvsize > 0 ) {
288 /* Store for next invocation */
289 ftp->recvbuf = recvbuf;
290 ftp->recvsize = recvsize;
295 /** FTP control channel operations */
296 static struct xfer_interface_operations ftp_control_operations = {
297 .close = ftp_control_close,
298 .vredirect = xfer_vopen,
299 .request = ignore_xfer_request,
300 .seek = ignore_xfer_seek,
301 .alloc_iob = default_xfer_alloc_iob,
302 .deliver_iob = xfer_deliver_as_raw,
303 .deliver_raw = ftp_control_deliver_raw,
306 /*****************************************************************************
313 * Handle FTP data channel being closed
315 * @v data FTP data channel interface
316 * @v rc Reason for closure
318 * When the data channel is closed, the control channel should be left
319 * alone; the server will send a completion message via the control
320 * channel which we'll pick up.
322 * If the data channel is closed due to an error, we abort the request.
324 static void ftp_data_closed ( struct xfer_interface *data, int rc ) {
325 struct ftp_request *ftp =
326 container_of ( data, struct ftp_request, data );
328 DBGC ( ftp, "FTP %p data connection closed: %s\n",
329 ftp, strerror ( rc ) );
331 /* If there was an error, close control channel and record status */
333 ftp_done ( ftp, rc );
337 * Handle data delivery via FTP data channel
339 * @v xfer FTP data channel interface
340 * @v iobuf I/O buffer
341 * @ret rc Return status code
343 static int ftp_data_deliver_iob ( struct xfer_interface *data,
344 struct io_buffer *iobuf ) {
345 struct ftp_request *ftp =
346 container_of ( data, struct ftp_request, data );
349 if ( ( rc = xfer_deliver_iob ( &ftp->xfer, iobuf ) ) != 0 ) {
350 DBGC ( ftp, "FTP %p failed to deliver data: %s\n",
351 ftp, strerror ( rc ) );
358 /** FTP data channel operations */
359 static struct xfer_interface_operations ftp_data_operations = {
360 .close = ftp_data_closed,
361 .vredirect = xfer_vopen,
362 .request = ignore_xfer_request,
363 .seek = ignore_xfer_seek,
364 .alloc_iob = default_xfer_alloc_iob,
365 .deliver_iob = ftp_data_deliver_iob,
366 .deliver_raw = xfer_deliver_as_iob,
369 /*****************************************************************************
371 * Data transfer interface
376 * Close FTP data transfer interface
378 * @v xfer FTP data transfer interface
379 * @v rc Reason for close
381 static void ftp_xfer_closed ( struct xfer_interface *xfer, int rc ) {
382 struct ftp_request *ftp =
383 container_of ( xfer, struct ftp_request, xfer );
385 DBGC ( ftp, "FTP %p data transfer interface closed: %s\n",
386 ftp, strerror ( rc ) );
388 ftp_done ( ftp, rc );
391 /** FTP data transfer interface operations */
392 static struct xfer_interface_operations ftp_xfer_operations = {
393 .close = ftp_xfer_closed,
394 .vredirect = ignore_xfer_vredirect,
395 .request = ignore_xfer_request,
396 .seek = ignore_xfer_seek,
397 .alloc_iob = default_xfer_alloc_iob,
398 .deliver_iob = xfer_deliver_as_raw,
399 .deliver_raw = ignore_xfer_deliver_raw,
402 /*****************************************************************************
409 * Initiate an FTP connection
411 * @v xfer Data transfer interface
412 * @v uri Uniform Resource Identifier
413 * @ret rc Return status code
415 static int ftp_open ( struct xfer_interface *xfer, struct uri *uri ) {
416 struct ftp_request *ftp;
417 struct sockaddr_tcpip server;
426 /* Allocate and populate structure */
427 ftp = malloc ( sizeof ( *ftp ) );
430 memset ( ftp, 0, sizeof ( *ftp ) );
431 ftp->refcnt.free = ftp_free;
432 xfer_init ( &ftp->xfer, &ftp_xfer_operations, &ftp->refcnt );
433 ftp->uri = uri_get ( uri );
434 xfer_init ( &ftp->control, &ftp_control_operations, &ftp->refcnt );
435 xfer_init ( &ftp->data, &ftp_data_operations, &ftp->refcnt );
436 ftp->recvbuf = ftp->status_text;
437 ftp->recvsize = sizeof ( ftp->status_text ) - 1;
439 DBGC ( ftp, "FTP %p fetching %s\n", ftp, ftp->uri->path );
441 /* Open control connection */
442 memset ( &server, 0, sizeof ( server ) );
443 server.st_port = htons ( uri_port ( uri, FTP_PORT ) );
444 if ( ( rc = xfer_open_named_socket ( &ftp->control, SOCK_STREAM,
445 ( struct sockaddr * ) &server,
446 uri->host, NULL ) ) != 0 )
449 /* Attach to parent interface, mortalise self, and return */
450 xfer_plug_plug ( &ftp->xfer, xfer );
451 ref_put ( &ftp->refcnt );
455 DBGC ( ftp, "FTP %p could not create request: %s\n",
456 ftp, strerror ( rc ) );
457 ftp_done ( ftp, rc );
458 ref_put ( &ftp->refcnt );
462 /** FTP URI opener */
463 struct uri_opener ftp_uri_opener __uri_opener = {