11 * File transfer protocol
15 /*****************************************************************************
21 /** An FTP control channel string */
25 /** Offset to string data
27 * This is the offset within the struct ftp_request to the
28 * pointer to the string data. Use ftp_string_data() to get a
29 * pointer to the actual data.
34 /** FTP control channel strings */
35 static const struct ftp_string ftp_strings[] = {
36 [FTP_CONNECT] = { "", 0 },
37 [FTP_USER] = { "USER anonymous\r\n", 0 },
38 [FTP_PASS] = { "PASS etherboot@etherboot.org\r\n", 0 },
39 [FTP_TYPE] = { "TYPE I\r\n", 0 },
40 [FTP_PASV] = { "PASV\r\n", 0 },
41 [FTP_RETR] = { "RETR %s\r\n",
42 offsetof ( struct ftp_request, filename ) },
43 [FTP_QUIT] = { "QUIT\r\n", 0 },
44 [FTP_DONE] = { "", 0 },
48 * Get data associated with an FTP control channel string
51 * @v data_offset Data offset field from ftp_string structure
52 * @ret data Pointer to data
54 static inline const void * ftp_string_data ( struct ftp_request *ftp,
56 return * ( ( void ** ) ( ( ( void * ) ftp ) + data_offset ) );
60 * Get FTP request from control TCP connection
62 * @v conn TCP connection
63 * @ret ftp FTP request
65 static inline struct ftp_request * tcp_to_ftp ( struct tcp_connection *conn ) {
66 return container_of ( conn, struct ftp_request, tcp );
70 * Mark FTP request as complete
73 * @v complete Completion indicator
76 static void ftp_complete ( struct ftp_request *ftp, int complete ) {
77 ftp->complete = complete;
78 tcp_close ( &ftp->tcp_data );
79 tcp_close ( &ftp->tcp );
83 * Parse FTP byte sequence value
86 * @v value Value buffer
87 * @v len Length of value buffer
89 * This parses an FTP byte sequence value (e.g. the "aaa,bbb,ccc,ddd"
90 * form for IP addresses in PORT commands) into a byte sequence. @c
91 * *text will be updated to point beyond the end of the parsed byte
94 * This function is safe in the presence of malformed data, though the
95 * output is undefined.
97 static void ftp_parse_value ( char **text, uint8_t *value, size_t len ) {
99 *(value++) = strtoul ( *text, text, 10 );
106 * Handle an FTP control channel response
110 * This is called once we have received a complete repsonse line.
112 static void ftp_reply ( struct ftp_request *ftp ) {
113 char status_major = ftp->status_text[0];
115 /* Ignore "intermediate" responses (1xx codes) */
116 if ( status_major == '1' )
119 /* Anything other than success (2xx) or, in the case of a
120 * repsonse to a "USER" command, a password prompt (3xx), is a
123 if ( ! ( ( status_major == '2' ) ||
124 ( ( status_major == '3' ) && ( ftp->state == FTP_USER ) ) ) )
127 /* Open passive connection when we get "PASV" response */
128 if ( ftp->state == FTP_PASV ) {
129 char *ptr = ftp->passive_text;
131 ftp_parse_value ( &ptr,
132 ( uint8_t * ) &ftp->tcp_data.sin.sin_addr,
133 sizeof ( ftp->tcp_data.sin.sin_addr ) );
134 ftp_parse_value ( &ptr,
135 ( uint8_t * ) &ftp->tcp_data.sin.sin_port,
136 sizeof ( ftp->tcp_data.sin.sin_port ) );
137 tcp_connect ( &ftp->tcp_data );
140 /* Move to next state */
141 if ( ftp->state < FTP_DONE )
143 ftp->already_sent = 0;
147 /* Flag protocol error and close connections */
148 ftp_complete ( ftp, -EPROTO );
152 * Handle new data arriving on FTP control channel
154 * @v conn TCP connection
156 * @v len Length of new data
158 * Data is collected until a complete line is received, at which point
159 * its information is passed to ftp_reply().
161 static void ftp_newdata ( struct tcp_connection *conn,
162 void *data, size_t len ) {
163 struct ftp_request *ftp = tcp_to_ftp ( conn );
164 char *recvbuf = ftp->recvbuf;
165 size_t recvsize = ftp->recvsize;
169 c = * ( ( char * ) data++ );
173 /* End of line: call ftp_reply() to handle
174 * completed reply. Avoid calling ftp_reply()
175 * twice if we receive both \r and \n.
179 /* Start filling up the status code buffer */
180 recvbuf = ftp->status_text;
181 recvsize = sizeof ( ftp->status_text ) - 1;
184 /* Start filling up the passive parameter buffer */
185 recvbuf = ftp->passive_text;
186 recvsize = sizeof ( ftp->passive_text ) - 1;
189 /* Stop filling the passive parameter buffer */
193 /* Fill up buffer if applicable */
194 if ( recvsize > 0 ) {
202 /* Store for next invocation */
203 ftp->recvbuf = recvbuf;
204 ftp->recvsize = recvsize;
208 * Handle acknowledgement of data sent on FTP control channel
210 * @v conn TCP connection
212 static void ftp_acked ( struct tcp_connection *conn, size_t len ) {
213 struct ftp_request *ftp = tcp_to_ftp ( conn );
215 /* Mark off ACKed portion of the currently-transmitted data */
216 ftp->already_sent += len;
220 * Construct data to send on FTP control channel
222 * @v conn TCP connection
224 static void ftp_senddata ( struct tcp_connection *conn ) {
225 struct ftp_request *ftp = tcp_to_ftp ( conn );
226 const struct ftp_string *string;
229 /* Send the as-yet-unACKed portion of the string for the
232 string = &ftp_strings[ftp->state];
233 len = snprintf ( tcp_buffer, tcp_buflen, string->format,
234 ftp_string_data ( ftp, string->data_offset ) );
235 tcp_send ( conn, tcp_buffer + ftp->already_sent,
236 len - ftp->already_sent );
240 * Handle control channel being closed
242 * @v conn TCP connection
244 * When the control channel is closed, the data channel must also be
245 * closed, if it is currently open.
247 static void ftp_closed ( struct tcp_connection *conn, int status ) {
248 struct ftp_request *ftp = tcp_to_ftp ( conn );
250 ftp_complete ( ftp, status ? status : 1 );
253 /** FTP control channel operations */
254 static struct tcp_operations ftp_tcp_operations = {
255 .closed = ftp_closed,
257 .newdata = ftp_newdata,
258 .senddata = ftp_senddata,
261 /*****************************************************************************
268 * Get FTP request from data TCP connection
270 * @v conn TCP connection
271 * @ret ftp FTP request
273 static inline struct ftp_request *
274 tcp_to_ftp_data ( struct tcp_connection *conn ) {
275 return container_of ( conn, struct ftp_request, tcp_data );
279 * Handle data channel being closed
281 * @v conn TCP connection
283 * When the data channel is closed, the control channel should be left
284 * alone; the server will send a completion message via the control
285 * channel which we'll pick up.
287 * If the data channel is closed due to an error, we abort the request.
289 static void ftp_data_closed ( struct tcp_connection *conn, int status ) {
290 struct ftp_request *ftp = tcp_to_ftp_data ( conn );
293 ftp_complete ( ftp, status );
297 * Handle new data arriving on the FTP data channel
299 * @v conn TCP connection
301 * @v len Length of new data
303 * Data is handed off to the callback registered in the FTP request.
305 static void ftp_data_newdata ( struct tcp_connection *conn,
306 void *data, size_t len ) {
307 struct ftp_request *ftp = tcp_to_ftp_data ( conn );
309 ftp->callback ( data, len );
312 /** FTP data channel operations */
313 static struct tcp_operations ftp_data_tcp_operations = {
314 .closed = ftp_data_closed,
315 .newdata = ftp_data_newdata,
318 /*****************************************************************************
325 * Initiate an FTP connection
329 void ftp_connect ( struct ftp_request *ftp ) {
330 ftp->tcp.tcp_op = &ftp_tcp_operations;
331 ftp->tcp_data.tcp_op = &ftp_data_tcp_operations;
332 ftp->recvbuf = ftp->status_text;
333 ftp->recvsize = sizeof ( ftp->status_text ) - 1;
334 tcp_connect ( &ftp->tcp );