7 #include <gpxe/async.h>
12 * File transfer protocol
16 /*****************************************************************************
22 /** An FTP control channel string */
26 /** Offset to string data
28 * This is the offset within the struct ftp_request to the
29 * pointer to the string data. Use ftp_string_data() to get a
30 * pointer to the actual data.
35 /** FTP control channel strings */
36 static const struct ftp_string ftp_strings[] = {
37 [FTP_CONNECT] = { "", 0 },
38 [FTP_USER] = { "USER anonymous\r\n", 0 },
39 [FTP_PASS] = { "PASS etherboot@etherboot.org\r\n", 0 },
40 [FTP_TYPE] = { "TYPE I\r\n", 0 },
41 [FTP_PASV] = { "PASV\r\n", 0 },
42 [FTP_RETR] = { "RETR %s\r\n",
43 offsetof ( struct ftp_request, filename ) },
44 [FTP_QUIT] = { "QUIT\r\n", 0 },
45 [FTP_DONE] = { "", 0 },
49 * Get data associated with an FTP control channel string
52 * @v data_offset Data offset field from ftp_string structure
53 * @ret data Pointer to data
55 static inline const void * ftp_string_data ( struct ftp_request *ftp,
57 return * ( ( void ** ) ( ( ( void * ) ftp ) + data_offset ) );
61 * Get FTP request from control TCP application
63 * @v app TCP application
64 * @ret ftp FTP request
66 static inline struct ftp_request * tcp_to_ftp ( struct tcp_application *app ) {
67 return container_of ( app, struct ftp_request, tcp );
71 * Mark FTP operation as complete
74 * @v rc Return status code
76 static void ftp_done ( struct ftp_request *ftp, int rc ) {
78 DBGC ( ftp, "FTP %p completed with status %d\n", ftp, rc );
80 /* Close both TCP connections */
81 tcp_close ( &ftp->tcp );
82 tcp_close ( &ftp->tcp_data );
84 /* Mark asynchronous operation as complete */
85 async_done ( &ftp->aop, rc );
89 * Parse FTP byte sequence value
92 * @v value Value buffer
93 * @v len Length of value buffer
95 * This parses an FTP byte sequence value (e.g. the "aaa,bbb,ccc,ddd"
96 * form for IP addresses in PORT commands) into a byte sequence. @c
97 * *text will be updated to point beyond the end of the parsed byte
100 * This function is safe in the presence of malformed data, though the
101 * output is undefined.
103 static void ftp_parse_value ( char **text, uint8_t *value, size_t len ) {
105 *(value++) = strtoul ( *text, text, 10 );
112 * Handle an FTP control channel response
116 * This is called once we have received a complete response line.
118 static void ftp_reply ( struct ftp_request *ftp ) {
119 char status_major = ftp->status_text[0];
121 DBGC ( ftp, "FTP %p received status %s\n", ftp, ftp->status_text );
123 /* Ignore "intermediate" responses (1xx codes) */
124 if ( status_major == '1' )
127 /* Anything other than success (2xx) or, in the case of a
128 * repsonse to a "USER" command, a password prompt (3xx), is a
131 if ( ! ( ( status_major == '2' ) ||
132 ( ( status_major == '3' ) && ( ftp->state == FTP_USER ) ) ) ){
133 /* Flag protocol error and close connections */
134 ftp_done ( ftp, -EPROTO );
137 /* Open passive connection when we get "PASV" response */
138 if ( ftp->state == FTP_PASV ) {
139 char *ptr = ftp->passive_text;
141 struct sockaddr_in sin;
142 struct sockaddr_tcpip st;
146 sa.sin.sin_family = AF_INET;
147 ftp_parse_value ( &ptr, ( uint8_t * ) &sa.sin.sin_addr,
148 sizeof ( sa.sin.sin_addr ) );
149 ftp_parse_value ( &ptr, ( uint8_t * ) &sa.sin.sin_port,
150 sizeof ( sa.sin.sin_port ) );
151 if ( ( rc = tcp_connect ( &ftp->tcp_data, &sa.st, 0 ) ) != 0 ){
152 DBGC ( ftp, "FTP %p could not make data connection\n",
154 ftp_done ( ftp, rc );
159 /* Move to next state */
160 if ( ftp->state < FTP_DONE )
162 ftp->already_sent = 0;
164 if ( ftp->state < FTP_DONE ) {
165 const struct ftp_string *string = &ftp_strings[ftp->state];
166 DBGC ( ftp, "FTP %p sending ", ftp );
167 DBGC ( ftp, string->format,
168 ftp_string_data ( ftp, string->data_offset ) );
175 * Handle new data arriving on FTP control channel
177 * @v app TCP application
179 * @v len Length of new data
181 * Data is collected until a complete line is received, at which point
182 * its information is passed to ftp_reply().
184 static void ftp_newdata ( struct tcp_application *app,
185 void *data, size_t len ) {
186 struct ftp_request *ftp = tcp_to_ftp ( app );
187 char *recvbuf = ftp->recvbuf;
188 size_t recvsize = ftp->recvsize;
192 c = * ( ( char * ) data++ );
196 /* End of line: call ftp_reply() to handle
197 * completed reply. Avoid calling ftp_reply()
198 * twice if we receive both \r and \n.
202 /* Start filling up the status code buffer */
203 recvbuf = ftp->status_text;
204 recvsize = sizeof ( ftp->status_text ) - 1;
207 /* Start filling up the passive parameter buffer */
208 recvbuf = ftp->passive_text;
209 recvsize = sizeof ( ftp->passive_text ) - 1;
212 /* Stop filling the passive parameter buffer */
216 /* Fill up buffer if applicable */
217 if ( recvsize > 0 ) {
225 /* Store for next invocation */
226 ftp->recvbuf = recvbuf;
227 ftp->recvsize = recvsize;
231 * Handle acknowledgement of data sent on FTP control channel
233 * @v app TCP application
235 static void ftp_acked ( struct tcp_application *app, size_t len ) {
236 struct ftp_request *ftp = tcp_to_ftp ( app );
238 /* Mark off ACKed portion of the currently-transmitted data */
239 ftp->already_sent += len;
243 * Construct data to send on FTP control channel
245 * @v app TCP application
246 * @v buf Temporary data buffer
247 * @v len Length of temporary data buffer
249 static void ftp_senddata ( struct tcp_application *app,
250 void *buf, size_t len ) {
251 struct ftp_request *ftp = tcp_to_ftp ( app );
252 const struct ftp_string *string;
254 /* Send the as-yet-unACKed portion of the string for the
257 string = &ftp_strings[ftp->state];
258 len = snprintf ( buf, len, string->format,
259 ftp_string_data ( ftp, string->data_offset ) );
260 tcp_send ( app, buf + ftp->already_sent, len - ftp->already_sent );
264 * Handle control channel being closed
266 * @v app TCP application
268 * When the control channel is closed, the data channel must also be
269 * closed, if it is currently open.
271 static void ftp_closed ( struct tcp_application *app, int status ) {
272 struct ftp_request *ftp = tcp_to_ftp ( app );
274 DBGC ( ftp, "FTP %p control connection closed (status %d)\n",
277 /* Complete FTP operation */
278 ftp_done ( ftp, status );
281 /** FTP control channel operations */
282 static struct tcp_operations ftp_tcp_operations = {
283 .closed = ftp_closed,
285 .newdata = ftp_newdata,
286 .senddata = ftp_senddata,
289 /*****************************************************************************
296 * Get FTP request from data TCP application
298 * @v app TCP application
299 * @ret ftp FTP request
301 static inline struct ftp_request *
302 tcp_to_ftp_data ( struct tcp_application *app ) {
303 return container_of ( app, struct ftp_request, tcp_data );
307 * Handle data channel being closed
309 * @v app TCP application
311 * When the data channel is closed, the control channel should be left
312 * alone; the server will send a completion message via the control
313 * channel which we'll pick up.
315 * If the data channel is closed due to an error, we abort the request.
317 static void ftp_data_closed ( struct tcp_application *app, int status ) {
318 struct ftp_request *ftp = tcp_to_ftp_data ( app );
320 DBGC ( ftp, "FTP %p data connection closed (status %d)\n",
323 /* If there was an error, close control channel and record status */
325 ftp_done ( ftp, status );
329 * Handle new data arriving on the FTP data channel
331 * @v app TCP application
333 * @v len Length of new data
335 * Data is handed off to the callback registered in the FTP request.
337 static void ftp_data_newdata ( struct tcp_application *app,
338 void *data, size_t len ) {
339 struct ftp_request *ftp = tcp_to_ftp_data ( app );
341 ftp->callback ( data, len );
344 /** FTP data channel operations */
345 static struct tcp_operations ftp_data_tcp_operations = {
346 .closed = ftp_data_closed,
347 .newdata = ftp_data_newdata,
350 /*****************************************************************************
357 * Initiate an FTP connection
361 struct async_operation * ftp_get ( struct ftp_request *ftp ) {
364 DBGC ( ftp, "FTP %p fetching %s\n", ftp, ftp->filename );
366 ftp->tcp.tcp_op = &ftp_tcp_operations;
367 ftp->tcp_data.tcp_op = &ftp_data_tcp_operations;
368 ftp->recvbuf = ftp->status_text;
369 ftp->recvsize = sizeof ( ftp->status_text ) - 1;
370 if ( ( rc = tcp_connect ( &ftp->tcp, &ftp->server, 0 ) ) != 0 )
371 ftp_done ( ftp, rc );