|
@@ -12,6 +12,12 @@
|
12
|
12
|
*
|
13
|
13
|
*/
|
14
|
14
|
|
|
15
|
+/*****************************************************************************
|
|
16
|
+ *
|
|
17
|
+ * FTP control channel
|
|
18
|
+ *
|
|
19
|
+ */
|
|
20
|
+
|
15
|
21
|
/** An FTP control channel string */
|
16
|
22
|
struct ftp_string {
|
17
|
23
|
/** String format */
|
|
@@ -25,8 +31,18 @@ struct ftp_string {
|
25
|
31
|
off_t data_offset;
|
26
|
32
|
};
|
27
|
33
|
|
28
|
|
-#define ftp_string_offset( fieldname ) \
|
29
|
|
- offsetof ( struct ftp_request, fieldname )
|
|
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 },
|
|
45
|
+};
|
30
|
46
|
|
31
|
47
|
/**
|
32
|
48
|
* Get data associated with an FTP control channel string
|
|
@@ -40,23 +56,23 @@ static inline const void * ftp_string_data ( struct ftp_request *ftp,
|
40
|
56
|
return * ( ( void ** ) ( ( ( void * ) ftp ) + data_offset ) );
|
41
|
57
|
}
|
42
|
58
|
|
43
|
|
-/** FTP control channel strings */
|
44
|
|
-const struct ftp_string ftp_strings[] = {
|
45
|
|
- [FTP_CONNECT] = { "", 0 },
|
46
|
|
- [FTP_USER] = { "USER anonymous\r\n", 0 },
|
47
|
|
- [FTP_PASS] = { "PASS etherboot@etherboot.org\r\n", 0 },
|
48
|
|
- [FTP_TYPE] = { "TYPE I\r\n", 0 },
|
49
|
|
- [FTP_PASV] = { "PASV\r\n", 0 },
|
50
|
|
- [FTP_RETR] = { "RETR %s\r\n", ftp_string_offset ( filename ) },
|
51
|
|
- [FTP_QUIT] = { "QUIT\r\n", 0 },
|
52
|
|
- [FTP_DONE] = { "", 0 },
|
53
|
|
-};
|
54
|
|
-
|
55
|
|
-static inline struct ftp_request *
|
56
|
|
-tcp_to_ftp ( struct tcp_connection *conn ) {
|
|
59
|
+/**
|
|
60
|
+ * Get FTP request from control TCP connection
|
|
61
|
+ *
|
|
62
|
+ * @v conn TCP connection
|
|
63
|
+ * @ret ftp FTP request
|
|
64
|
+ */
|
|
65
|
+static inline struct ftp_request * tcp_to_ftp ( struct tcp_connection *conn ) {
|
57
|
66
|
return container_of ( conn, struct ftp_request, tcp );
|
58
|
67
|
}
|
59
|
68
|
|
|
69
|
+/**
|
|
70
|
+ * Mark FTP request as complete
|
|
71
|
+ *
|
|
72
|
+ * @v ftp FTP request
|
|
73
|
+ * @v complete Completion indicator
|
|
74
|
+ *
|
|
75
|
+ */
|
60
|
76
|
static void ftp_complete ( struct ftp_request *ftp, int complete ) {
|
61
|
77
|
ftp->complete = complete;
|
62
|
78
|
tcp_close ( &ftp->tcp_data );
|
|
@@ -87,7 +103,7 @@ static void ftp_parse_value ( char **text, uint8_t *value, size_t len ) {
|
87
|
103
|
}
|
88
|
104
|
|
89
|
105
|
/**
|
90
|
|
- * Handle a response from an FTP server
|
|
106
|
+ * Handle an FTP control channel response
|
91
|
107
|
*
|
92
|
108
|
* @v ftp FTP request
|
93
|
109
|
*
|
|
@@ -132,6 +148,16 @@ static void ftp_reply ( struct ftp_request *ftp ) {
|
132
|
148
|
ftp_complete ( ftp, -EPROTO );
|
133
|
149
|
}
|
134
|
150
|
|
|
151
|
+/**
|
|
152
|
+ * Handle new data arriving on FTP control channel
|
|
153
|
+ *
|
|
154
|
+ * @v conn TCP connection
|
|
155
|
+ * @v data New data
|
|
156
|
+ * @v len Length of new data
|
|
157
|
+ *
|
|
158
|
+ * Data is collected until a complete line is received, at which point
|
|
159
|
+ * its information is passed to ftp_reply().
|
|
160
|
+ */
|
135
|
161
|
static void ftp_newdata ( struct tcp_connection *conn,
|
136
|
162
|
void *data, size_t len ) {
|
137
|
163
|
struct ftp_request *ftp = tcp_to_ftp ( conn );
|
|
@@ -178,6 +204,11 @@ static void ftp_newdata ( struct tcp_connection *conn,
|
178
|
204
|
ftp->recvsize = recvsize;
|
179
|
205
|
}
|
180
|
206
|
|
|
207
|
+/**
|
|
208
|
+ * Handle acknowledgement of data sent on FTP control channel
|
|
209
|
+ *
|
|
210
|
+ * @v conn TCP connection
|
|
211
|
+ */
|
181
|
212
|
static void ftp_acked ( struct tcp_connection *conn, size_t len ) {
|
182
|
213
|
struct ftp_request *ftp = tcp_to_ftp ( conn );
|
183
|
214
|
|
|
@@ -185,6 +216,11 @@ static void ftp_acked ( struct tcp_connection *conn, size_t len ) {
|
185
|
216
|
ftp->already_sent += len;
|
186
|
217
|
}
|
187
|
218
|
|
|
219
|
+/**
|
|
220
|
+ * Construct data to send on FTP control channel
|
|
221
|
+ *
|
|
222
|
+ * @v conn TCP connection
|
|
223
|
+ */
|
188
|
224
|
static void ftp_senddata ( struct tcp_connection *conn ) {
|
189
|
225
|
struct ftp_request *ftp = tcp_to_ftp ( conn );
|
190
|
226
|
const struct ftp_string *string;
|
|
@@ -200,50 +236,72 @@ static void ftp_senddata ( struct tcp_connection *conn ) {
|
200
|
236
|
len - ftp->already_sent );
|
201
|
237
|
}
|
202
|
238
|
|
203
|
|
-static void ftp_aborted ( struct tcp_connection *conn ) {
|
204
|
|
- struct ftp_request *ftp = tcp_to_ftp ( conn );
|
205
|
|
-
|
206
|
|
- ftp_complete ( ftp, -ECONNABORTED );
|
207
|
|
-}
|
208
|
|
-
|
209
|
|
-static void ftp_timedout ( struct tcp_connection *conn ) {
|
210
|
|
- struct ftp_request *ftp = tcp_to_ftp ( conn );
|
211
|
|
-
|
212
|
|
- ftp_complete ( ftp, -ETIMEDOUT );
|
213
|
|
-}
|
214
|
|
-
|
215
|
|
-static void ftp_closed ( struct tcp_connection *conn ) {
|
|
239
|
+/**
|
|
240
|
+ * Handle control channel being closed
|
|
241
|
+ *
|
|
242
|
+ * @v conn TCP connection
|
|
243
|
+ *
|
|
244
|
+ * When the control channel is closed, the data channel must also be
|
|
245
|
+ * closed, if it is currently open.
|
|
246
|
+ */
|
|
247
|
+static void ftp_closed ( struct tcp_connection *conn, int status ) {
|
216
|
248
|
struct ftp_request *ftp = tcp_to_ftp ( conn );
|
217
|
249
|
|
218
|
|
- ftp_complete ( ftp, 1 );
|
|
250
|
+ ftp_complete ( ftp, status ? status : 1 );
|
219
|
251
|
}
|
220
|
252
|
|
|
253
|
+/** FTP control channel operations */
|
221
|
254
|
static struct tcp_operations ftp_tcp_operations = {
|
222
|
|
- .aborted = ftp_aborted,
|
223
|
|
- .timedout = ftp_timedout,
|
224
|
255
|
.closed = ftp_closed,
|
225
|
256
|
.acked = ftp_acked,
|
226
|
257
|
.newdata = ftp_newdata,
|
227
|
258
|
.senddata = ftp_senddata,
|
228
|
259
|
};
|
229
|
260
|
|
|
261
|
+/*****************************************************************************
|
|
262
|
+ *
|
|
263
|
+ * FTP control channel
|
|
264
|
+ *
|
|
265
|
+ */
|
|
266
|
+
|
|
267
|
+/**
|
|
268
|
+ * Get FTP request from data TCP connection
|
|
269
|
+ *
|
|
270
|
+ * @v conn TCP connection
|
|
271
|
+ * @ret ftp FTP request
|
|
272
|
+ */
|
230
|
273
|
static inline struct ftp_request *
|
231
|
274
|
tcp_to_ftp_data ( struct tcp_connection *conn ) {
|
232
|
275
|
return container_of ( conn, struct ftp_request, tcp_data );
|
233
|
276
|
}
|
234
|
277
|
|
235
|
|
-static void ftp_data_aborted ( struct tcp_connection *conn ) {
|
236
|
|
- struct ftp_request *ftp = tcp_to_ftp_data ( conn );
|
237
|
|
-
|
238
|
|
- ftp_complete ( ftp, -ECONNABORTED );
|
239
|
|
-}
|
240
|
|
-
|
241
|
|
-static void ftp_data_timedout ( struct tcp_connection *conn ) {
|
|
278
|
+/**
|
|
279
|
+ * Handle data channel being closed
|
|
280
|
+ *
|
|
281
|
+ * @v conn TCP connection
|
|
282
|
+ *
|
|
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.
|
|
286
|
+ *
|
|
287
|
+ * If the data channel is closed due to an error, we abort the request.
|
|
288
|
+ */
|
|
289
|
+static void ftp_data_closed ( struct tcp_connection *conn, int status ) {
|
242
|
290
|
struct ftp_request *ftp = tcp_to_ftp_data ( conn );
|
243
|
291
|
|
244
|
|
- ftp_complete ( ftp, -ETIMEDOUT );
|
|
292
|
+ if ( status )
|
|
293
|
+ ftp_complete ( ftp, status );
|
245
|
294
|
}
|
246
|
295
|
|
|
296
|
+/**
|
|
297
|
+ * Handle new data arriving on the FTP data channel
|
|
298
|
+ *
|
|
299
|
+ * @v conn TCP connection
|
|
300
|
+ * @v data New data
|
|
301
|
+ * @v len Length of new data
|
|
302
|
+ *
|
|
303
|
+ * Data is handed off to the callback registered in the FTP request.
|
|
304
|
+ */
|
247
|
305
|
static void ftp_data_newdata ( struct tcp_connection *conn,
|
248
|
306
|
void *data, size_t len ) {
|
249
|
307
|
struct ftp_request *ftp = tcp_to_ftp_data ( conn );
|
|
@@ -251,9 +309,9 @@ static void ftp_data_newdata ( struct tcp_connection *conn,
|
251
|
309
|
ftp->callback ( data, len );
|
252
|
310
|
}
|
253
|
311
|
|
|
312
|
+/** FTP data channel operations */
|
254
|
313
|
static struct tcp_operations ftp_data_tcp_operations = {
|
255
|
|
- .aborted = ftp_data_aborted,
|
256
|
|
- .timedout = ftp_data_timedout,
|
|
314
|
+ .closed = ftp_data_closed,
|
257
|
315
|
.newdata = ftp_data_newdata,
|
258
|
316
|
};
|
259
|
317
|
|