diff --git a/src/include/gpxe/ftp.h b/src/include/gpxe/ftp.h index 6fad1527..1a0861e9 100644 --- a/src/include/gpxe/ftp.h +++ b/src/include/gpxe/ftp.h @@ -49,6 +49,8 @@ struct ftp_request { * remote server. */ void ( *callback ) ( char *data, size_t len ); + /** Eventual return status */ + int rc; /** Asynchronous operation for this FTP operation */ struct async_operation aop; diff --git a/src/include/gpxe/tcp.h b/src/include/gpxe/tcp.h index b7f0acca..9afb061b 100644 --- a/src/include/gpxe/tcp.h +++ b/src/include/gpxe/tcp.h @@ -144,10 +144,6 @@ extern void tcp_close ( struct tcp_connection *conn ); */ struct tcp_connection { struct sockaddr_tcpip peer; /* Remote socket address */ - - /* FIXME: this field should no longer be present */ - struct sockaddr_in sin; - uint16_t local_port; /* Local port, in network byte order */ int tcp_state; /* TCP state */ int tcp_lstate; /* Last TCP state */ @@ -206,6 +202,10 @@ struct tcp_header { extern struct tcpip_protocol tcp_protocol; +static inline int tcp_closed ( struct tcp_connection *conn ) { + return ( conn->tcp_state == TCP_CLOSED ); +} + extern void tcp_init_conn ( struct tcp_connection *conn ); extern int tcp_connect ( struct tcp_connection *conn ); extern int tcp_connectto ( struct tcp_connection *conn, diff --git a/src/net/tcp/ftp.c b/src/net/tcp/ftp.c index 71a53ccf..3ccae05e 100644 --- a/src/net/tcp/ftp.c +++ b/src/net/tcp/ftp.c @@ -67,17 +67,22 @@ static inline struct ftp_request * tcp_to_ftp ( struct tcp_connection *conn ) { return container_of ( conn, struct ftp_request, tcp ); } +static void ftp_set_status ( struct ftp_request *ftp, int rc ) { + if ( ! ftp->rc ) + ftp->rc = rc; +} + +static void ftp_clear_status ( struct ftp_request *ftp ) { + ftp->rc = 0; +} + /** - * Mark FTP request as complete + * Mark FTP operation as complete * * @v ftp FTP request - * @v rc Return status code - * */ -static void ftp_done ( struct ftp_request *ftp, int rc ) { - tcp_close ( &ftp->tcp_data ); - tcp_close ( &ftp->tcp ); - async_done ( &ftp->aop, rc ); +static void ftp_done ( struct ftp_request *ftp ) { + async_done ( &ftp->aop, ftp->rc ); } /** @@ -128,13 +133,14 @@ static void ftp_reply ( struct ftp_request *ftp ) { /* Open passive connection when we get "PASV" response */ if ( ftp->state == FTP_PASV ) { char *ptr = ftp->passive_text; + struct sockaddr_in *sin = + ( struct sockaddr_in * ) &ftp->tcp_data.peer; - ftp_parse_value ( &ptr, - ( uint8_t * ) &ftp->tcp_data.sin.sin_addr, - sizeof ( ftp->tcp_data.sin.sin_addr ) ); - ftp_parse_value ( &ptr, - ( uint8_t * ) &ftp->tcp_data.sin.sin_port, - sizeof ( ftp->tcp_data.sin.sin_port ) ); + sin->sin_family = AF_INET; + ftp_parse_value ( &ptr, ( uint8_t * ) &sin->sin_addr, + sizeof ( sin->sin_addr ) ); + ftp_parse_value ( &ptr, ( uint8_t * ) &sin->sin_port, + sizeof ( sin->sin_port ) ); tcp_connect ( &ftp->tcp_data ); } @@ -146,7 +152,8 @@ static void ftp_reply ( struct ftp_request *ftp ) { err: /* Flag protocol error and close connections */ - ftp_done ( ftp, -EPROTO ); + ftp_set_status ( ftp, -EPROTO ); + tcp_close ( &ftp->tcp ); } /** @@ -249,7 +256,15 @@ static void ftp_senddata ( struct tcp_connection *conn, static void ftp_closed ( struct tcp_connection *conn, int status ) { struct ftp_request *ftp = tcp_to_ftp ( conn ); - ftp_done ( ftp, status ); + /* Close data channel and record status */ + ftp_set_status ( ftp, status ); + tcp_close ( &ftp->tcp_data ); + + /* Mark FTP operation as complete if we are the last + * connection to close + */ + if ( tcp_closed ( &ftp->tcp_data ) ) + ftp_done ( ftp ); } /** FTP control channel operations */ @@ -290,9 +305,18 @@ tcp_to_ftp_data ( struct tcp_connection *conn ) { */ static void ftp_data_closed ( struct tcp_connection *conn, int status ) { struct ftp_request *ftp = tcp_to_ftp_data ( conn ); + + /* If there was an error, close control channel and record status */ + if ( status ) { + ftp_set_status ( ftp, status ); + tcp_close ( &ftp->tcp ); + } - if ( status ) - ftp_done ( ftp, status ); + /* Mark FTP operation as complete if we are the last + * connection to close + */ + if ( tcp_closed ( &ftp->tcp ) ) + ftp_done ( ftp ); } /** @@ -333,6 +357,7 @@ struct async_operation * ftp_get ( struct ftp_request *ftp ) { ftp->tcp_data.tcp_op = &ftp_data_tcp_operations; ftp->recvbuf = ftp->status_text; ftp->recvsize = sizeof ( ftp->status_text ) - 1; + ftp_clear_status ( ftp ); tcp_connect ( &ftp->tcp ); return &ftp->aop; } diff --git a/src/tests/dhcptest.c b/src/tests/dhcptest.c index d1e7a6cb..a315de09 100644 --- a/src/tests/dhcptest.c +++ b/src/tests/dhcptest.c @@ -14,8 +14,10 @@ static int test_dhcp_aoe_boot ( struct net_device *netdev, return test_aoeboot ( netdev, aoename, drivenum ); } -static int test_dhcp_iscsi_boot ( struct net_device *netdev, char *iscsiname ) { - char *initiator_iqn = "iqn.1900-01.localdomain.localhost:initiator"; +static int test_dhcp_iscsi_boot ( struct net_device *netdev, + char *iscsiname ) { + char initiator_iqn_buf[32]; + char *initiator_iqn = initiator_iqn_buf; char username[32]; char password[32]; char *target_iqn; @@ -24,6 +26,7 @@ static int test_dhcp_iscsi_boot ( struct net_device *netdev, char *iscsiname ) { struct sockaddr_tcpip st; } target; unsigned int drivenum; + struct dhcp_option *option; memset ( &target, 0, sizeof ( target ) ); target.sin.sin_family = AF_INET; @@ -36,6 +39,10 @@ static int test_dhcp_iscsi_boot ( struct net_device *netdev, char *iscsiname ) { } inet_aton ( iscsiname, &target.sin.sin_addr ); + dhcp_snprintf ( initiator_iqn, sizeof ( initiator_iqn ), + find_global_dhcp_option ( DHCP_ISCSI_INITIATOR_IQN ) ); + if ( ! *initiator_iqn ) + initiator_iqn = "iqn.1900-01.localdomain.localhost:initiator"; dhcp_snprintf ( username, sizeof ( username ), find_global_dhcp_option ( DHCP_EB_USERNAME ) ); dhcp_snprintf ( password, sizeof ( password ), @@ -89,6 +96,29 @@ static int test_dhcp_http ( struct net_device *netdev, char *url ) { return 0; } +static int test_dhcp_ftp ( struct net_device *netdev, char *ftpname ) { + union { + struct sockaddr_in sin; + struct sockaddr_tcpip st; + } target; + char *filename; + + filename = strchr ( ftpname, ':' ); + if ( ! filename ) { + printf ( "Invalid FTP path \"%s\"\n", ftpname ); + return -EINVAL; + } + *filename++ = '\0'; + + memset ( &target, 0, sizeof ( target ) ); + target.sin.sin_family = AF_INET; + target.sin.sin_port = htons ( 21 ); + inet_aton ( ftpname, &target.sin.sin_addr ); + + test_ftp ( &target, filename ); + return 0; +} + static int test_dhcp_tftp ( struct net_device *netdev, char *tftpname ) { union { struct sockaddr_in sin; @@ -105,17 +135,27 @@ static int test_dhcp_tftp ( struct net_device *netdev, char *tftpname ) { } static int test_dhcp_boot ( struct net_device *netdev, char *filename ) { + /* if ( strncmp ( filename, "aoe:", 4 ) == 0 ) { return test_dhcp_aoe_boot ( netdev, &filename[4] ); - } else if ( strncmp ( filename, "iscsi:", 6 ) == 0 ) { + } + */ + if ( strncmp ( filename, "iscsi:", 6 ) == 0 ) { return test_dhcp_iscsi_boot ( netdev, &filename[6] ); - } else if ( strncmp ( filename, "hello:", 6 ) == 0 ) { - return test_dhcp_hello ( &filename[6] ); - } else if ( strncmp ( filename, "http:", 5 ) == 0 ) { - return test_dhcp_http ( netdev, filename ); - } else { - return test_dhcp_tftp ( netdev, filename ); } + if ( strncmp ( filename, "ftp:", 4 ) == 0 ) { + return test_dhcp_ftp ( netdev, &filename[4] ); + } + /* + if ( strncmp ( filename, "hello:", 6 ) == 0 ) { + return test_dhcp_hello ( &filename[6] ); + } + if ( strncmp ( filename, "http:", 5 ) == 0 ) { + return test_dhcp_http ( netdev, filename ); + } + return test_dhcp_tftp ( netdev, filename ); + */ + return -EPROTONOSUPPORT; } int test_dhcp ( struct net_device *netdev ) { diff --git a/src/tests/ftptest.c b/src/tests/ftptest.c index f8208de7..6b0002fb 100644 --- a/src/tests/ftptest.c +++ b/src/tests/ftptest.c @@ -22,15 +22,14 @@ static void test_ftp_callback ( char *data, size_t len ) { } } -void test_ftp ( struct in_addr server, const char *filename ) { +void test_ftp ( struct sockaddr_tcpip *server, const char *filename ) { struct ftp_request ftp; int rc; - printf ( "FTP fetching %s:%s\n", inet_ntoa ( server ), filename ); + printf ( "FTP fetching %s\n", filename ); memset ( &ftp, 0, sizeof ( ftp ) ); - ftp.tcp.sin.sin_addr.s_addr = server.s_addr; - ftp.tcp.sin.sin_port = htons ( FTP_PORT ); + memcpy ( &ftp.tcp.peer, server, sizeof ( ftp.tcp.peer ) ); ftp.filename = filename; ftp.callback = test_ftp_callback;