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
223 * @v buf Temporary data buffer
224 * @v len Length of temporary data buffer
226 static void ftp_senddata ( struct tcp_connection *conn,
227 void *buf, size_t len ) {
228 struct ftp_request *ftp = tcp_to_ftp ( conn );
229 const struct ftp_string *string;
231 /* Send the as-yet-unACKed portion of the string for the
234 string = &ftp_strings[ftp->state];
235 len = snprintf ( buf, len, string->format,
236 ftp_string_data ( ftp, string->data_offset ) );
237 tcp_send ( conn, buf + ftp->already_sent, len - ftp->already_sent );
241 * Handle control channel being closed
243 * @v conn TCP connection
245 * When the control channel is closed, the data channel must also be
246 * closed, if it is currently open.
248 static void ftp_closed ( struct tcp_connection *conn, int status ) {
249 struct ftp_request *ftp = tcp_to_ftp ( conn );
251 ftp_complete ( ftp, status ? status : 1 );
254 /** FTP control channel operations */
255 static struct tcp_operations ftp_tcp_operations = {
256 .closed = ftp_closed,
258 .newdata = ftp_newdata,
259 .senddata = ftp_senddata,
262 /*****************************************************************************
269 * Get FTP request from data TCP connection
271 * @v conn TCP connection
272 * @ret ftp FTP request
274 static inline struct ftp_request *
275 tcp_to_ftp_data ( struct tcp_connection *conn ) {
276 return container_of ( conn, struct ftp_request, tcp_data );
280 * Handle data channel being closed
282 * @v conn TCP connection
284 * When the data channel is closed, the control channel should be left
285 * alone; the server will send a completion message via the control
286 * channel which we'll pick up.
288 * If the data channel is closed due to an error, we abort the request.
290 static void ftp_data_closed ( struct tcp_connection *conn, int status ) {
291 struct ftp_request *ftp = tcp_to_ftp_data ( conn );
294 ftp_complete ( ftp, status );
298 * Handle new data arriving on the FTP data channel
300 * @v conn TCP connection
302 * @v len Length of new data
304 * Data is handed off to the callback registered in the FTP request.
306 static void ftp_data_newdata ( struct tcp_connection *conn,
307 void *data, size_t len ) {
308 struct ftp_request *ftp = tcp_to_ftp_data ( conn );
310 ftp->callback ( data, len );
313 /** FTP data channel operations */
314 static struct tcp_operations ftp_data_tcp_operations = {
315 .closed = ftp_data_closed,
316 .newdata = ftp_data_newdata,
319 /*****************************************************************************
326 * Initiate an FTP connection
330 void ftp_connect ( struct ftp_request *ftp ) {
331 ftp->tcp.tcp_op = &ftp_tcp_operations;
332 ftp->tcp_data.tcp_op = &ftp_data_tcp_operations;
333 ftp->recvbuf = ftp->status_text;
334 ftp->recvsize = sizeof ( ftp->status_text ) - 1;
335 tcp_connect ( &ftp->tcp );