mirror of
https://github.com/ipxe/ipxe
synced 2025-12-12 23:15:10 +03:00
Update ftp.c and hello.c to use the generic async_operations API.
This commit is contained in:
@@ -8,8 +8,12 @@
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
#include <stdint.h>
|
#include <stdint.h>
|
||||||
|
#include <gpxe/async.h>
|
||||||
#include <gpxe/tcp.h>
|
#include <gpxe/tcp.h>
|
||||||
|
|
||||||
|
/** FTP default port */
|
||||||
|
#define FTP_PORT 21
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* FTP states
|
* FTP states
|
||||||
*
|
*
|
||||||
@@ -45,12 +49,8 @@ struct ftp_request {
|
|||||||
* remote server.
|
* remote server.
|
||||||
*/
|
*/
|
||||||
void ( *callback ) ( char *data, size_t len );
|
void ( *callback ) ( char *data, size_t len );
|
||||||
/** Completion indicator
|
/** Asynchronous operation for this FTP operation */
|
||||||
*
|
struct async_operation aop;
|
||||||
* This will be set to a non-zero value when the transfer is
|
|
||||||
* complete. A negative value indicates an error.
|
|
||||||
*/
|
|
||||||
int complete;
|
|
||||||
|
|
||||||
/** Current state */
|
/** Current state */
|
||||||
enum ftp_state state;
|
enum ftp_state state;
|
||||||
@@ -69,6 +69,6 @@ struct ftp_request {
|
|||||||
struct tcp_connection tcp_data;
|
struct tcp_connection tcp_data;
|
||||||
};
|
};
|
||||||
|
|
||||||
extern void ftp_connect ( struct ftp_request *ftp );
|
struct async_operation * ftp_get ( struct ftp_request *ftp );
|
||||||
|
|
||||||
#endif
|
#endif /* _GPXE_FTP_H */
|
||||||
|
|||||||
@@ -9,6 +9,7 @@
|
|||||||
|
|
||||||
#include <stdint.h>
|
#include <stdint.h>
|
||||||
#include <gpxe/tcp.h>
|
#include <gpxe/tcp.h>
|
||||||
|
#include <gpxe/async.h>
|
||||||
|
|
||||||
enum hello_state {
|
enum hello_state {
|
||||||
HELLO_SENDING_MESSAGE = 1,
|
HELLO_SENDING_MESSAGE = 1,
|
||||||
@@ -37,10 +38,10 @@ struct hello_request {
|
|||||||
* remote server.
|
* remote server.
|
||||||
*/
|
*/
|
||||||
void ( *callback ) ( char *data, size_t len );
|
void ( *callback ) ( char *data, size_t len );
|
||||||
/** Connection complete indicator */
|
/** Asynchronous operation */
|
||||||
int complete;
|
struct async_operation aop;
|
||||||
};
|
};
|
||||||
|
|
||||||
extern void hello_connect ( struct hello_request *hello );
|
extern struct async_operation * say_hello ( struct hello_request *hello );
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|||||||
@@ -4,6 +4,7 @@
|
|||||||
#include <vsprintf.h>
|
#include <vsprintf.h>
|
||||||
#include <assert.h>
|
#include <assert.h>
|
||||||
#include <errno.h>
|
#include <errno.h>
|
||||||
|
#include <gpxe/async.h>
|
||||||
#include <gpxe/ftp.h>
|
#include <gpxe/ftp.h>
|
||||||
|
|
||||||
/** @file
|
/** @file
|
||||||
@@ -70,13 +71,13 @@ static inline struct ftp_request * tcp_to_ftp ( struct tcp_connection *conn ) {
|
|||||||
* Mark FTP request as complete
|
* Mark FTP request as complete
|
||||||
*
|
*
|
||||||
* @v ftp FTP request
|
* @v ftp FTP request
|
||||||
* @v complete Completion indicator
|
* @v rc Return status code
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
static void ftp_complete ( struct ftp_request *ftp, int complete ) {
|
static void ftp_done ( struct ftp_request *ftp, int rc ) {
|
||||||
ftp->complete = complete;
|
|
||||||
tcp_close ( &ftp->tcp_data );
|
tcp_close ( &ftp->tcp_data );
|
||||||
tcp_close ( &ftp->tcp );
|
tcp_close ( &ftp->tcp );
|
||||||
|
async_done ( &ftp->aop, rc );
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -145,7 +146,7 @@ static void ftp_reply ( struct ftp_request *ftp ) {
|
|||||||
|
|
||||||
err:
|
err:
|
||||||
/* Flag protocol error and close connections */
|
/* Flag protocol error and close connections */
|
||||||
ftp_complete ( ftp, -EPROTO );
|
ftp_done ( ftp, -EPROTO );
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -248,7 +249,7 @@ static void ftp_senddata ( struct tcp_connection *conn,
|
|||||||
static void ftp_closed ( struct tcp_connection *conn, int status ) {
|
static void ftp_closed ( struct tcp_connection *conn, int status ) {
|
||||||
struct ftp_request *ftp = tcp_to_ftp ( conn );
|
struct ftp_request *ftp = tcp_to_ftp ( conn );
|
||||||
|
|
||||||
ftp_complete ( ftp, status ? status : 1 );
|
ftp_done ( ftp, status );
|
||||||
}
|
}
|
||||||
|
|
||||||
/** FTP control channel operations */
|
/** FTP control channel operations */
|
||||||
@@ -291,7 +292,7 @@ static void ftp_data_closed ( struct tcp_connection *conn, int status ) {
|
|||||||
struct ftp_request *ftp = tcp_to_ftp_data ( conn );
|
struct ftp_request *ftp = tcp_to_ftp_data ( conn );
|
||||||
|
|
||||||
if ( status )
|
if ( status )
|
||||||
ftp_complete ( ftp, status );
|
ftp_done ( ftp, status );
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -327,10 +328,11 @@ static struct tcp_operations ftp_data_tcp_operations = {
|
|||||||
*
|
*
|
||||||
* @v ftp FTP request
|
* @v ftp FTP request
|
||||||
*/
|
*/
|
||||||
void ftp_connect ( struct ftp_request *ftp ) {
|
struct async_operation * ftp_get ( struct ftp_request *ftp ) {
|
||||||
ftp->tcp.tcp_op = &ftp_tcp_operations;
|
ftp->tcp.tcp_op = &ftp_tcp_operations;
|
||||||
ftp->tcp_data.tcp_op = &ftp_data_tcp_operations;
|
ftp->tcp_data.tcp_op = &ftp_data_tcp_operations;
|
||||||
ftp->recvbuf = ftp->status_text;
|
ftp->recvbuf = ftp->status_text;
|
||||||
ftp->recvsize = sizeof ( ftp->status_text ) - 1;
|
ftp->recvsize = sizeof ( ftp->status_text ) - 1;
|
||||||
tcp_connect ( &ftp->tcp );
|
tcp_connect ( &ftp->tcp );
|
||||||
|
return &ftp->aop;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -2,6 +2,7 @@
|
|||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include <vsprintf.h>
|
#include <vsprintf.h>
|
||||||
#include <assert.h>
|
#include <assert.h>
|
||||||
|
#include <gpxe/async.h>
|
||||||
#include <gpxe/hello.h>
|
#include <gpxe/hello.h>
|
||||||
|
|
||||||
/** @file
|
/** @file
|
||||||
@@ -13,7 +14,8 @@
|
|||||||
* message (hello_request::message). Any data received from the
|
* message (hello_request::message). Any data received from the
|
||||||
* server will be passed to the callback function,
|
* server will be passed to the callback function,
|
||||||
* hello_request::callback(), and once the connection has been closed,
|
* hello_request::callback(), and once the connection has been closed,
|
||||||
* hello_request::complete will be set to a non-zero value.
|
* the asynchronous operation associated with the request will be
|
||||||
|
* marked as complete.
|
||||||
*
|
*
|
||||||
* To use this code, do something like:
|
* To use this code, do something like:
|
||||||
*
|
*
|
||||||
@@ -31,10 +33,7 @@
|
|||||||
* hello.sin.sin_addr.s_addr = ... server IP address ...
|
* hello.sin.sin_addr.s_addr = ... server IP address ...
|
||||||
* hello.sin.sin_port = ... server port ...
|
* hello.sin.sin_port = ... server port ...
|
||||||
*
|
*
|
||||||
* hello_connect ( &hello );
|
* rc = async_wait ( say_hello ( &hello ) );
|
||||||
* while ( ! hello.completed ) {
|
|
||||||
* run_tcpip();
|
|
||||||
* }
|
|
||||||
*
|
*
|
||||||
* @endcode
|
* @endcode
|
||||||
*
|
*
|
||||||
@@ -52,7 +51,7 @@ tcp_to_hello ( struct tcp_connection *conn ) {
|
|||||||
static void hello_closed ( struct tcp_connection *conn, int status ) {
|
static void hello_closed ( struct tcp_connection *conn, int status ) {
|
||||||
struct hello_request *hello = tcp_to_hello ( conn );
|
struct hello_request *hello = tcp_to_hello ( conn );
|
||||||
|
|
||||||
hello->complete = ( status ? status : 1 );
|
async_done ( &hello->aop, status );
|
||||||
}
|
}
|
||||||
|
|
||||||
static void hello_connected ( struct tcp_connection *conn ) {
|
static void hello_connected ( struct tcp_connection *conn ) {
|
||||||
@@ -92,7 +91,8 @@ static void hello_newdata ( struct tcp_connection *conn, void *data,
|
|||||||
hello->callback ( data, len );
|
hello->callback ( data, len );
|
||||||
}
|
}
|
||||||
|
|
||||||
static void hello_senddata ( struct tcp_connection *conn ) {
|
static void hello_senddata ( struct tcp_connection *conn,
|
||||||
|
void *buf __unused, size_t len __unused ) {
|
||||||
struct hello_request *hello = tcp_to_hello ( conn );
|
struct hello_request *hello = tcp_to_hello ( conn );
|
||||||
|
|
||||||
tcp_send ( conn, hello->message, hello->remaining );
|
tcp_send ( conn, hello->message, hello->remaining );
|
||||||
@@ -111,7 +111,8 @@ static struct tcp_operations hello_tcp_operations = {
|
|||||||
*
|
*
|
||||||
* @v hello "Hello world" request
|
* @v hello "Hello world" request
|
||||||
*/
|
*/
|
||||||
void hello_connect ( struct hello_request *hello ) {
|
struct async_operation * say_hello ( struct hello_request *hello ) {
|
||||||
hello->tcp.tcp_op = &hello_tcp_operations;
|
hello->tcp.tcp_op = &hello_tcp_operations;
|
||||||
tcp_connect ( &hello->tcp );
|
tcp_connect ( &hello->tcp );
|
||||||
|
return &hello->aop;
|
||||||
}
|
}
|
||||||
|
|||||||
41
src/tests/ftptest.c
Normal file
41
src/tests/ftptest.c
Normal file
@@ -0,0 +1,41 @@
|
|||||||
|
#include <stdint.h>
|
||||||
|
#include <string.h>
|
||||||
|
#include <byteswap.h>
|
||||||
|
#include <console.h>
|
||||||
|
#include <vsprintf.h>
|
||||||
|
#include <gpxe/async.h>
|
||||||
|
#include <gpxe/ftp.h>
|
||||||
|
|
||||||
|
static void test_ftp_callback ( char *data, size_t len ) {
|
||||||
|
unsigned int i;
|
||||||
|
char c;
|
||||||
|
|
||||||
|
for ( i = 0 ; i < len ; i++ ) {
|
||||||
|
c = data[i];
|
||||||
|
if ( c == '\r' ) {
|
||||||
|
/* Print nothing */
|
||||||
|
} else if ( ( c == '\n' ) || ( c >= 32 ) || ( c <= 126 ) ) {
|
||||||
|
putchar ( c );
|
||||||
|
} else {
|
||||||
|
putchar ( '.' );
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void test_ftp ( struct in_addr server, const char *filename ) {
|
||||||
|
struct ftp_request ftp;
|
||||||
|
int rc;
|
||||||
|
|
||||||
|
printf ( "FTP fetching %s:%s\n", inet_ntoa ( server ), filename );
|
||||||
|
|
||||||
|
memset ( &ftp, 0, sizeof ( ftp ) );
|
||||||
|
ftp.tcp.sin.sin_addr.s_addr = server.s_addr;
|
||||||
|
ftp.tcp.sin.sin_port = htons ( FTP_PORT );
|
||||||
|
ftp.filename = filename;
|
||||||
|
ftp.callback = test_ftp_callback;
|
||||||
|
|
||||||
|
rc = async_wait ( ftp_get ( &ftp ) );
|
||||||
|
if ( rc ) {
|
||||||
|
printf ( "FTP fetch failed\n" );
|
||||||
|
}
|
||||||
|
}
|
||||||
41
src/tests/hellotest.c
Normal file
41
src/tests/hellotest.c
Normal file
@@ -0,0 +1,41 @@
|
|||||||
|
#include <stdint.h>
|
||||||
|
#include <string.h>
|
||||||
|
#include <byteswap.h>
|
||||||
|
#include <console.h>
|
||||||
|
#include <vsprintf.h>
|
||||||
|
#include <gpxe/async.h>
|
||||||
|
#include <gpxe/hello.h>
|
||||||
|
|
||||||
|
static void test_hello_callback ( char *data, size_t len ) {
|
||||||
|
unsigned int i;
|
||||||
|
char c;
|
||||||
|
|
||||||
|
for ( i = 0 ; i < len ; i++ ) {
|
||||||
|
c = data[i];
|
||||||
|
if ( c == '\r' ) {
|
||||||
|
/* Print nothing */
|
||||||
|
} else if ( ( c == '\n' ) || ( c >= 32 ) || ( c <= 126 ) ) {
|
||||||
|
putchar ( c );
|
||||||
|
} else {
|
||||||
|
putchar ( '.' );
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void test_hello ( struct sockaddr_in *server, const char *message ) {
|
||||||
|
struct hello_request hello;
|
||||||
|
int rc;
|
||||||
|
|
||||||
|
printf ( "Saying \"%s\" to %s:%d\n", message,
|
||||||
|
inet_ntoa ( server->sin_addr ), ntohs ( server->sin_port ) );
|
||||||
|
|
||||||
|
memset ( &hello, 0, sizeof ( hello ) );
|
||||||
|
hello.tcp.sin = *server;
|
||||||
|
hello.message = message;
|
||||||
|
hello.callback = test_hello_callback;
|
||||||
|
|
||||||
|
rc = async_wait ( say_hello ( &hello ) );
|
||||||
|
if ( rc ) {
|
||||||
|
printf ( "HELLO fetch failed\n" );
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user