* @v linebuf Line buffer
* @v data New data to add
* @v len Length of new data to add
- * @ret rc Return status code
- *
- * If line_buffer() returns >0, then an end of line has been reached
- * and the buffered-up line can be obtained from buffered_line().
- * Carriage returns and newlines will have been stripped, and the line
- * will be NUL-terminated. This buffered line is valid only until the
- * next call to line_buffer() (or to empty_line_buffer()).
+ * @ret len Consumed length, or negative error number
*
- * @c data and @c len will be updated to reflect the data consumed by
- * line_buffer().
+ * After calling line_buffer(), use buffered_line() to determine
+ * whether or not a complete line is available. Carriage returns and
+ * newlines will have been stripped, and the line will be
+ * NUL-terminated. This buffered line is valid only until the next
+ * call to line_buffer() (or to empty_line_buffer()).
*
* Note that line buffers use dynamically allocated storage; you
* should call empty_line_buffer() before freeing a @c struct @c
* line_buffer.
*/
-int line_buffer ( struct line_buffer *linebuf,
- const char **data, size_t *len ) {
+ssize_t line_buffer ( struct line_buffer *linebuf,
+ const char *data, size_t len ) {
const char *eol;
size_t consume;
size_t new_len;
empty_line_buffer ( linebuf );
/* Search for line terminator */
- if ( ( eol = memchr ( *data, '\n', *len ) ) ) {
- consume = ( eol - *data + 1 );
+ if ( ( eol = memchr ( data, '\n', len ) ) ) {
+ consume = ( eol - data + 1 );
} else {
- consume = *len;
+ consume = len;
}
/* Reallocate data buffer and copy in new data */
new_data = realloc ( linebuf->data, ( new_len + 1 ) );
if ( ! new_data )
return -ENOMEM;
- memcpy ( ( new_data + linebuf->len ), *data, consume );
+ memcpy ( ( new_data + linebuf->len ), data, consume );
new_data[new_len] = '\0';
linebuf->data = new_data;
linebuf->len = new_len;
- /* Update data and len */
- *data += consume;
- *len -= consume;
-
/* If we have reached end of line, trim the line and mark as ready */
if ( eol ) {
linebuf->data[--linebuf->len] = '\0'; /* trim NL */
linebuf->ready = 1;
}
- return 0;
+ return consume;
}
#include <string.h>
#include <errno.h>
#include <gpxe/in.h>
+#include <gpxe/xfer.h>
+#include <gpxe/open.h>
+#include <gpxe/process.h>
#include <gpxe/resolv.h>
/** @file
*
*/
-static struct async_operations resolv_async_operations;
+/***************************************************************************
+ *
+ * Name resolution interfaces
+ *
+ ***************************************************************************
+ */
+
+/**
+ * Name resolution completed
+ *
+ * @v resolv Name resolution interface
+ * @v sa Completed socket address (if successful)
+ * @v rc Final status code
+ */
+void resolv_done ( struct resolv_interface *resolv, struct sockaddr *sa,
+ int rc ) {
+ struct resolv_interface *dest = resolv_get_dest ( resolv );
+
+ dest->op->done ( dest, sa, rc );
+ resolv_unplug ( resolv );
+ resolv_put ( dest );
+}
+
+/**
+ * Ignore name resolution done() event
+ *
+ * @v resolv Name resolution interface
+ * @v sa Completed socket address (if successful)
+ * @v rc Final status code
+ */
+void ignore_resolv_done ( struct resolv_interface *resolv __unused,
+ struct sockaddr *sa __unused, int rc __unused ) {
+ /* Do nothing */
+}
+
+/** Null name resolution interface operations */
+struct resolv_interface_operations null_resolv_ops = {
+ .done = ignore_resolv_done,
+};
+
+/** Null name resolution interface */
+struct resolv_interface null_resolv = {
+ .intf = {
+ .dest = &null_resolv.intf,
+ .refcnt = NULL,
+ },
+ .op = &null_resolv_ops,
+};
+
+/***************************************************************************
+ *
+ * Numeric name resolver
+ *
+ ***************************************************************************
+ */
+
+/** A numeric name resolver */
+struct numeric_resolv {
+ /** Reference counter */
+ struct refcnt refcnt;
+ /** Name resolution interface */
+ struct resolv_interface resolv;
+ /** Process */
+ struct process process;
+ /** Completed socket address */
+ struct sockaddr sa;
+ /** Overall status code */
+ int rc;
+};
+
+static void numeric_step ( struct process *process ) {
+ struct numeric_resolv *numeric =
+ container_of ( process, struct numeric_resolv, process );
+
+ resolv_done ( &numeric->resolv, &numeric->sa, numeric->rc );
+ process_del ( process );
+}
+
+static int numeric_resolv ( struct resolv_interface *resolv,
+ const char *name, struct sockaddr *sa ) {
+ struct numeric_resolv *numeric;
+ struct sockaddr_in *sin;
+
+ /* Allocate and initialise structure */
+ numeric = malloc ( sizeof ( *numeric ) );
+ if ( ! numeric )
+ return -ENOMEM;
+ memset ( numeric, 0, sizeof ( *numeric ) );
+ resolv_init ( &numeric->resolv, &null_resolv_ops, &numeric->refcnt );
+ process_init ( &numeric->process, numeric_step, &numeric->refcnt );
+ memcpy ( &numeric->sa, sa, sizeof ( numeric->sa ) );
+
+ DBGC ( numeric, "NUMERIC %p attempting to resolve \"%s\"\n",
+ numeric, name );
+
+ /* Attempt to resolve name */
+ sin = ( ( struct sockaddr_in * ) &numeric->sa );
+ sin->sin_family = AF_INET;
+ if ( inet_aton ( name, &sin->sin_addr ) == 0 )
+ numeric->rc = -EINVAL;
+
+ /* Attach to parent interface, mortalise self, and return */
+ resolv_plug_plug ( &numeric->resolv, resolv );
+ ref_put ( &numeric->refcnt );
+ return 0;
+}
+
+struct resolver numeric_resolver __resolver ( RESOLV_NUMERIC ) = {
+ .name = "NUMERIC",
+ .resolv = numeric_resolv,
+};
+
+/***************************************************************************
+ *
+ * Name resolution multiplexer
+ *
+ ***************************************************************************
+ */
/** Registered name resolvers */
static struct resolver resolvers[0]
static struct resolver resolvers_end[0]
__table_end ( struct resolver, resolvers );
+/** A name resolution multiplexer */
+struct resolv_mux {
+ /** Reference counter */
+ struct refcnt refcnt;
+ /** Parent name resolution interface */
+ struct resolv_interface parent;
+
+ /** Child name resolution interface */
+ struct resolv_interface child;
+ /** Current child resolver */
+ struct resolver *resolver;
+
+ /** Socket address to complete */
+ struct sockaddr sa;
+ /** Name to be resolved
+ *
+ * Must be at end of structure
+ */
+ char name[0];
+};
+
/**
- * Start name resolution
+ * Try current child name resolver
*
- * @v name Host name to resolve
- * @v sa Socket address to fill in
- * @v parent Parent asynchronous operation
+ * @v mux Name resolution multiplexer
* @ret rc Return status code
*/
-int resolv ( const char *name, struct sockaddr *sa, struct async *parent ) {
- struct resolution *resolution;
- struct resolver *resolver;
- struct sockaddr_in *sin = ( struct sockaddr_in * ) sa;
- struct in_addr in;
- int rc = -ENXIO;
+static int resolv_mux_try ( struct resolv_mux *mux ) {
+ struct resolver *resolver = mux->resolver;
+ int rc;
- /* Allocate and populate resolution structure */
- resolution = malloc ( sizeof ( *resolution ) );
- if ( ! resolution )
- return -ENOMEM;
- memset ( resolution, 0, sizeof ( *resolution ) );
- async_init ( &resolution->async, &resolv_async_operations, parent );
-
- /* Check for a dotted quad IP address first */
- if ( inet_aton ( name, &in ) != 0 ) {
- DBGC ( resolution, "RESOLV %p saw valid IP address %s\n",
- resolution, name );
- sin->sin_family = AF_INET;
- sin->sin_addr = in;
- async_done ( &resolution->async, 0 );
- return 0;
- }
+ DBGC ( mux, "RESOLV %p trying method %s\n", mux, resolver->name );
- /* Start up all resolvers */
- for ( resolver = resolvers ; resolver < resolvers_end ; resolver++ ) {
- if ( ( rc = resolver->resolv ( name, sa,
- &resolution->async ) ) != 0 ) {
- DBGC ( resolution, "RESOLV %p could not start %s: "
- "%s\n", resolution, resolver->name,
- strerror ( rc ) );
- /* Continue to try other resolvers */
- continue;
- }
- (resolution->pending)++;
+ if ( ( rc = resolver->resolv ( &mux->child, mux->name,
+ &mux->sa ) ) != 0 ) {
+ DBGC ( mux, "RESOLV %p could not use method %s: %s\n",
+ mux, resolver->name, strerror ( rc ) );
+ return rc;
}
- if ( ! resolution->pending )
- goto err;
return 0;
+}
- err:
- async_uninit ( &resolution->async );
- free ( resolution );
- return rc;
+/**
+ * Handle done() event from child name resolver
+ *
+ * @v resolv Child name resolution interface
+ * @v sa Completed socket address (if successful)
+ * @v rc Final status code
+ */
+static void resolv_mux_done ( struct resolv_interface *resolv,
+ struct sockaddr *sa, int rc ) {
+ struct resolv_mux *mux =
+ container_of ( resolv, struct resolv_mux, child );
+
+ /* Unplug child */
+ resolv_unplug ( &mux->child );
+
+ /* If this resolution succeeded, stop now */
+ if ( rc == 0 ) {
+ DBGC ( mux, "RESOLV %p succeeded using method %s\n",
+ mux, mux->resolver->name );
+ goto finished;
+ }
+
+ /* Attempt next child resolver, if possible */
+ mux->resolver++;
+ if ( mux->resolver >= resolvers_end ) {
+ DBGC ( mux, "RESOLV %p failed to resolve name\n", mux );
+ goto finished;
+ }
+ if ( ( rc = resolv_mux_try ( mux ) ) != 0 )
+ goto finished;
+
+ /* Next resolver is now running */
+ return;
+
+ finished:
+ resolv_done ( &mux->parent, sa, rc );
}
+/** Name resolution multiplexer operations */
+static struct resolv_interface_operations resolv_mux_child_ops = {
+ .done = resolv_mux_done,
+};
+
/**
- * Handle child name resolution completion
+ * Start name resolution
*
- * @v async Name resolution asynchronous operation
- * @v signal SIGCHLD
+ * @v resolv Name resolution interface
+ * @v name Name to resolve
+ * @v sa Socket address to complete
+ * @ret rc Return status code
*/
-static void resolv_sigchld ( struct async *async,
- enum signal signal __unused ) {
- struct resolution *resolution =
- container_of ( async, struct resolution, async );
+int resolv ( struct resolv_interface *resolv, const char *name,
+ struct sockaddr *sa ) {
+ struct resolv_mux *mux;
+ size_t name_len = ( strlen ( name ) + 1 );
int rc;
- /* Reap the child */
- async_wait ( async, &rc, 1 );
+ /* Allocate and initialise structure */
+ mux = malloc ( sizeof ( *mux ) + name_len );
+ if ( ! mux )
+ return -ENOMEM;
+ memset ( mux, 0, sizeof ( *mux ) );
+ resolv_init ( &mux->parent, &null_resolv_ops, &mux->refcnt );
+ resolv_init ( &mux->child, &resolv_mux_child_ops, &mux->refcnt );
+ mux->resolver = resolvers;
+ memcpy ( &mux->sa, sa, sizeof ( mux->sa ) );
+ memcpy ( mux->name, name, name_len );
+
+ DBGC ( mux, "RESOLV %p attempting to resolve \"%s\"\n", mux, name );
- /* If this child succeeded, kill all the others. They should
- * immediately die (invoking resolv_sigchld() again, which
- * won't do anything because the exit status is non-zero and
- * the pending count won't reach zero until this instance
- * completes).
+ /* Start first resolver in chain. There will always be at
+ * least one resolver (the numeric resolver), so no need to
+ * check for the zero-resolvers-available case.
*/
- if ( rc == 0 )
- async_signal_children ( async, SIGKILL );
+ if ( ( rc = resolv_mux_try ( mux ) ) != 0 )
+ goto err;
+
+ /* Attach parent interface, mortalise self, and return */
+ resolv_plug_plug ( &mux->parent, resolv );
+ ref_put ( &mux->refcnt );
+ return 0;
- /* When we have no children left, exit */
- if ( --(resolution->pending) == 0 )
- async_done ( async, rc );
+ err:
+ ref_put ( &mux->refcnt );
+ return rc;
}
+/***************************************************************************
+ *
+ * Named socket opening
+ *
+ ***************************************************************************
+ */
+
+/** A named socket */
+struct named_socket {
+ /** Reference counter */
+ struct refcnt refcnt;
+ /** Data transfer interface */
+ struct xfer_interface xfer;
+ /** Name resolution interface */
+ struct resolv_interface resolv;
+ /** Communication semantics (e.g. SOCK_STREAM) */
+ int semantics;
+ /** Stored local socket address, if applicable */
+ struct sockaddr local;
+ /** Stored local socket address exists */
+ int have_local;
+};
+
/**
- * Free name resolution structure
+ * Handle seek() event
*
- * @v async Asynchronous operation
+ * @v xfer Data transfer interface
+ * @v offset Offset to new position
+ * @v whence Basis for new position
+ * @ret rc Return status code
*/
-static void resolv_reap ( struct async *async ) {
- free ( container_of ( async, struct resolution, async ) );
+static int resolv_xfer_seek ( struct xfer_interface *xfer __unused,
+ off_t offset __unused, int whence __unused ) {
+ /* Never ready to accept data */
+ return -EAGAIN;
}
-/** Name resolution asynchronous operations */
-static struct async_operations resolv_async_operations = {
- .reap = resolv_reap,
- .signal = {
- [SIGKILL] = async_signal_children,
- [SIGCHLD] = resolv_sigchld,
- },
+/** Named socket opener data transfer interface operations */
+static struct xfer_interface_operations named_xfer_ops = {
+ .close = ignore_xfer_close,
+ .vredirect = ignore_xfer_vredirect,
+ .request = ignore_xfer_request,
+ .seek = resolv_xfer_seek,
+ .alloc_iob = default_xfer_alloc_iob,
+ .deliver_iob = xfer_deliver_as_raw,
+ .deliver_raw = ignore_xfer_deliver_raw,
};
+
+/**
+ * Handle done() event
+ *
+ * @v resolv Name resolution interface
+ * @v sa Completed socket address (if successful)
+ * @v rc Final status code
+ */
+static void named_resolv_done ( struct resolv_interface *resolv,
+ struct sockaddr *sa, int rc ) {
+ struct named_socket *named =
+ container_of ( resolv, struct named_socket, resolv );
+
+ /* Unplug resolver and nullify data transfer interface */
+ resolv_unplug ( &named->resolv );
+ xfer_nullify ( &named->xfer );
+
+ /* Redirect if name resolution was successful */
+ if ( rc == 0 ) {
+ rc = xfer_redirect ( &named->xfer, LOCATION_SOCKET,
+ named->semantics, sa,
+ ( named->have_local ?
+ &named->local : NULL ) );
+ }
+
+ /* Close data transfer interface if redirection failed */
+ if ( rc != 0 )
+ xfer_close ( &named->xfer, rc );
+
+ /* Unplug data transfer interface */
+ xfer_unplug ( &named->xfer );
+}
+
+/** Named socket opener name resolution interface operations */
+static struct resolv_interface_operations named_resolv_ops = {
+ .done = named_resolv_done,
+};
+
+/**
+ * Open named socket
+ *
+ * @v semantics Communication semantics (e.g. SOCK_STREAM)
+ * @v peer Peer socket address to complete
+ * @v name Name to resolve
+ * @v local Local socket address, or NULL
+ * @ret rc Return status code
+ */
+int xfer_open_named_socket ( struct xfer_interface *xfer, int semantics,
+ struct sockaddr *peer, const char *name,
+ struct sockaddr *local ) {
+ struct named_socket *named;
+ int rc;
+
+ /* Allocate and initialise structure */
+ named = malloc ( sizeof ( *named ) );
+ if ( ! named )
+ return -ENOMEM;
+ memset ( named, 0, sizeof ( *named ) );
+ xfer_init ( &named->xfer, &named_xfer_ops, &named->refcnt );
+ resolv_init ( &named->resolv, &named_resolv_ops, &named->refcnt );
+ named->semantics = semantics;
+ if ( local ) {
+ memcpy ( &named->local, local, sizeof ( named->local ) );
+ named->have_local = 1;
+ }
+
+ DBGC ( named, "RESOLV %p opening named socket \"%s\"\n",
+ named, name );
+
+ /* Start name resolution */
+ if ( ( rc = resolv ( &named->resolv, name, peer ) ) != 0 )
+ goto err;
+
+ /* Attach parent interface, mortalise self, and return */
+ xfer_plug_plug ( &named->xfer, xfer );
+ ref_put ( &named->refcnt );
+ return 0;
+
+ err:
+ ref_put ( &named->refcnt );
+ return rc;
+}
*
*/
-#include <stdint.h>
-#include <gpxe/async.h>
-#include <gpxe/stream.h>
-
-struct buffer;
-
/** FTP default port */
#define FTP_PORT 21
-/**
- * FTP states
- *
- * These @b must be sequential, i.e. a successful FTP session must
- * pass through each of these states in order.
- */
-enum ftp_state {
- FTP_CONNECT = 0,
- FTP_USER,
- FTP_PASS,
- FTP_TYPE,
- FTP_PASV,
- FTP_RETR,
- FTP_QUIT,
- FTP_DONE,
-};
-
-/**
- * An FTP request
- *
- */
-struct ftp_request {
- /** URI being fetched */
- struct uri *uri;
- /** Data buffer to fill */
- struct buffer *buffer;
- /** Asynchronous operation */
- struct async async;
-
- /** Current state */
- enum ftp_state state;
- /** Amount of current message already transmitted */
- size_t already_sent;
- /** Buffer to be filled with data received via the control channel */
- char *recvbuf;
- /** Remaining size of recvbuf */
- size_t recvsize;
- /** FTP status code, as text */
- char status_text[4];
- /** Passive-mode parameters, as text */
- char passive_text[24]; /* "aaa,bbb,ccc,ddd,eee,fff" */
- /** Stream application for the control channel */
- struct stream_application stream;
- /** Stream application for the data channel */
- struct stream_application stream_data;
-};
-
-extern int ftp_get ( struct uri *uri, struct buffer *buffer,
- struct async *parent );
-
#endif /* _GPXE_FTP_H */
*
*/
-#include <stdint.h>
-#include <gpxe/stream.h>
-#include <gpxe/async.h>
-#include <gpxe/linebuf.h>
-#include <gpxe/uri.h>
-
/** HTTP default port */
#define HTTP_PORT 80
/** HTTPS default port */
#define HTTPS_PORT 443
-/** HTTP receive state */
-enum http_rx_state {
- HTTP_RX_RESPONSE = 0,
- HTTP_RX_HEADER,
- HTTP_RX_DATA,
- HTTP_RX_DEAD,
-};
-
-/**
- * An HTTP request
- *
- */
-struct http_request {
- /** URI being fetched */
- struct uri *uri;
- /** Data buffer to fill */
- struct buffer *buffer;
- /** Asynchronous operation */
- struct async async;
-
- /** HTTP response code */
- unsigned int response;
- /** HTTP Content-Length */
- size_t content_length;
-
- /** Server address */
- struct sockaddr server;
- /** Stream application for this request */
- struct stream_application stream;
- /** Number of bytes already sent */
- size_t tx_offset;
- /** RX state */
- enum http_rx_state rx_state;
- /** Line buffer for received header lines */
- struct line_buffer linebuf;
-};
-
-extern int http_get ( struct uri *uri, struct buffer *buffer,
- struct async *parent );
-
#endif /* _GPXE_HTTP_H */
};
extern char * buffered_line ( struct line_buffer *linebuf );
-extern int line_buffer ( struct line_buffer *linebuf,
- const char **data, size_t *len );
+extern ssize_t line_buffer ( struct line_buffer *linebuf,
+ const char *data, size_t len );
extern void empty_line_buffer ( struct line_buffer *linebuf );
#endif /* _GPXE_LINEBUF_H */
*
*/
+#include <gpxe/refcnt.h>
+#include <gpxe/interface.h>
+#include <gpxe/tables.h>
+
struct sockaddr;
+struct resolv_interface;
-#include <gpxe/async.h>
-#include <gpxe/tables.h>
+/** Name resolution interface operations */
+struct resolv_interface_operations {
+ /** Name resolution completed
+ *
+ * @v resolv Name resolution interface
+ * @v sa Completed socket address (if successful)
+ * @v rc Final status code
+ */
+ void ( * done ) ( struct resolv_interface *resolv,
+ struct sockaddr *sa, int rc );
+};
+
+/** A name resolution interface */
+struct resolv_interface {
+ /** Generic object communication interface */
+ struct interface intf;
+ /** Operations for received messages */
+ struct resolv_interface_operations *op;
+};
+
+extern struct resolv_interface null_resolv;
+extern struct resolv_interface_operations null_resolv_ops;
+
+/**
+ * Initialise a name resolution interface
+ *
+ * @v resolv Name resolution interface
+ * @v op Name resolution interface operations
+ * @v refcnt Containing object reference counter, or NULL
+ */
+static inline void resolv_init ( struct resolv_interface *resolv,
+ struct resolv_interface_operations *op,
+ struct refcnt *refcnt ) {
+ resolv->intf.dest = &null_resolv.intf;
+ resolv->intf.refcnt = refcnt;
+ resolv->op = op;
+}
+
+/**
+ * Get name resolution interface from generic object communication interface
+ *
+ * @v intf Generic object communication interface
+ * @ret resolv Name resolution interface
+ */
+static inline __attribute__ (( always_inline )) struct resolv_interface *
+intf_to_resolv ( struct interface *intf ) {
+ return container_of ( intf, struct resolv_interface, intf );
+}
+
+/**
+ * Get reference to destination name resolution interface
+ *
+ * @v resolv Name resolution interface
+ * @ret dest Destination interface
+ */
+static inline __attribute__ (( always_inline )) struct resolv_interface *
+resolv_get_dest ( struct resolv_interface *resolv ) {
+ return intf_to_resolv ( intf_get ( resolv->intf.dest ) );
+}
+
+/**
+ * Drop reference to name resolution interface
+ *
+ * @v resolv name resolution interface
+ */
+static inline __attribute__ (( always_inline )) void
+resolv_put ( struct resolv_interface *resolv ) {
+ intf_put ( &resolv->intf );
+}
+
+/**
+ * Plug a name resolution interface into a new destination interface
+ *
+ * @v resolv Name resolution interface
+ * @v dest New destination interface
+ */
+static inline __attribute__ (( always_inline )) void
+resolv_plug ( struct resolv_interface *resolv, struct resolv_interface *dest ) {
+ plug ( &resolv->intf, &dest->intf );
+}
+
+/**
+ * Plug two name resolution interfaces together
+ *
+ * @v a Name resolution interface A
+ * @v b Name resolution interface B
+ */
+static inline __attribute__ (( always_inline )) void
+resolv_plug_plug ( struct resolv_interface *a, struct resolv_interface *b ) {
+ plug_plug ( &a->intf, &b->intf );
+}
+
+/**
+ * Unplug a name resolution interface
+ *
+ * @v resolv Name resolution interface
+ */
+static inline __attribute__ (( always_inline )) void
+resolv_unplug ( struct resolv_interface *resolv ) {
+ plug ( &resolv->intf, &null_resolv.intf );
+}
+
+/**
+ * Stop using a name resolution interface
+ *
+ * @v resolv Name resolution interface
+ *
+ * After calling this method, no further messages will be received via
+ * the interface.
+ */
+static inline void resolv_nullify ( struct resolv_interface *resolv ) {
+ resolv->op = &null_resolv_ops;
+};
/** A name resolver */
struct resolver {
const char *name;
/** Start name resolution
*
- * @v name Host name to resolve
- * @v sa Socket address to fill in
- * @v parent Parent asynchronous operation
+ * @v resolv Name resolution interface
+ * @v name Name to resolve
+ * @v sa Socket address to complete
* @ret rc Return status code
- *
- * The asynchronous process must be prepared to accept
- * SIGKILL.
*/
- int ( * resolv ) ( const char *name, struct sockaddr *sa,
- struct async *parent );
+ int ( * resolv ) ( struct resolv_interface *resolv, const char *name,
+ struct sockaddr *sa );
};
-/** A name resolution in progress */
-struct resolution {
- /** Asynchronous operation */
- struct async async;
- /** Numner of active child resolvers */
- unsigned int pending;
-};
+/** Numeric resolver priority */
+#define RESOLV_NUMERIC 01
+
+/** Normal resolver priority */
+#define RESOLV_NORMAL 02
/** Register as a name resolver */
-#define __resolver __table ( struct resolver, resolvers, 01 )
+#define __resolver( resolv_order ) \
+ __table ( struct resolver, resolvers, resolv_order )
-extern int resolv ( const char *name, struct sockaddr *sa,
- struct async *parent );
+extern int resolv ( struct resolv_interface *resolv, const char *name,
+ struct sockaddr *sa );
#endif /* _GPXE_RESOLV_H */
*/
#define TCP_MSL ( 2 * 60 * TICKS_PER_SEC )
-extern int tcp_open ( struct stream_application *app );
-
extern struct tcpip_protocol tcp_protocol;
#endif /* _GPXE_TCP_H */
#include <gpxe/iobuf.h>
#include <gpxe/malloc.h>
#include <gpxe/retry.h>
-#include <gpxe/stream.h>
+#include <gpxe/refcnt.h>
+#include <gpxe/xfer.h>
+#include <gpxe/open.h>
+#include <gpxe/uri.h>
#include <gpxe/tcpip.h>
#include <gpxe/tcp.h>
*
*/
-struct tcp_connection;
-static void tcp_expired ( struct retry_timer *timer, int over );
-static int tcp_senddata_conn ( struct tcp_connection *tcp, int force_send );
-static struct stream_connection_operations tcp_op;
-
-/**
- * A TCP connection
- *
- * This data structure represents the internal state of a TCP
- * connection.
- */
+/** A TCP connection */
struct tcp_connection {
- /** The stream connection */
- struct stream_connection stream;
+ /** Reference counter */
+ struct refcnt refcnt;
/** List of TCP connections */
struct list_head list;
+ /** Data transfer interface */
+ struct xfer_interface xfer;
+ /** Data transfer interface closed flag */
+ int xfer_closed;
+
/** Remote socket address */
struct sockaddr_tcpip peer;
/** Local port, in network byte order */
- uint16_t local_port;
+ unsigned int local_port;
/** Current TCP state */
unsigned int tcp_state;
*/
uint32_t rcv_ack;
- /** Transmit I/O buffer
- *
- * This buffer is allocated prior to calling the application's
- * senddata() method, to provide temporary storage space.
- */
- struct io_buffer *tx_iob;
+ /** Transmit queue */
+ struct list_head queue;
/** Retransmission timer */
struct retry_timer timer;
};
*/
static LIST_HEAD ( tcp_conns );
+/* Forward declarations */
+static struct xfer_interface_operations tcp_xfer_operations;
+static void tcp_expired ( struct retry_timer *timer, int over );
+static int tcp_rx_ack ( struct tcp_connection *tcp, uint32_t ack,
+ uint32_t win );
+
/**
* Name TCP state
*
DBGC ( tcp, " ACK" );
}
+/***************************************************************************
+ *
+ * Open and close
+ *
+ ***************************************************************************
+ */
+
/**
- * Allocate TCP connection
+ * Bind TCP connection to local port
*
- * @ret conn TCP connection, or NULL
+ * @v tcp TCP connection
+ * @v port Local port number, in network-endian order
+ * @ret rc Return status code
*
- * Allocates TCP connection and adds it to the TCP connection list.
+ * If the port is 0, the connection is assigned an available port
+ * between 1024 and 65535.
*/
-static struct tcp_connection * alloc_tcp ( void ) {
- struct tcp_connection *tcp;
+static int tcp_bind ( struct tcp_connection *tcp, unsigned int port ) {
+ struct tcp_connection *existing;
+ static uint16_t try_port = 1024;
- tcp = malloc ( sizeof ( *tcp ) );
- if ( tcp ) {
- DBGC ( tcp, "TCP %p allocated\n", tcp );
- memset ( tcp, 0, sizeof ( *tcp ) );
- tcp->tcp_state = tcp->prev_tcp_state = TCP_CLOSED;
- tcp->snd_seq = random();
- tcp->timer.expired = tcp_expired;
- tcp->stream.op = &tcp_op;
- list_add ( &tcp->list, &tcp_conns );
+ /* If no port specified, find the first available port */
+ if ( ! port ) {
+ for ( ; try_port ; try_port++ ) {
+ if ( try_port < 1024 )
+ continue;
+ if ( tcp_bind ( tcp, htons ( try_port ) ) == 0 )
+ return 0;
+ }
+ DBGC ( tcp, "TCP %p could not bind: no free ports\n", tcp );
+ return -EADDRINUSE;
}
- return tcp;
+
+ /* Attempt bind to local port */
+ list_for_each_entry ( existing, &tcp_conns, list ) {
+ if ( existing->local_port == port ) {
+ DBGC ( tcp, "TCP %p could not bind: port %d in use\n",
+ tcp, ntohs ( port ) );
+ return -EADDRINUSE;
+ }
+ }
+ tcp->local_port = port;
+
+ DBGC ( tcp, "TCP %p bound to port %d\n", tcp, ntohs ( port ) );
+ return 0;
}
/**
- * Free TCP connection
- *
- * @v tcp TCP connection
+ * Open a TCP connection
*
- * Removes connection from TCP connection list and frees the data
- * structure.
+ * @v xfer Data transfer interface
+ * @v peer Peer socket address
+ * @v local Local socket address, or NULL
+ * @ret rc Return status code
*/
-static void free_tcp ( struct tcp_connection *tcp ) {
+static int tcp_open ( struct xfer_interface *xfer, struct sockaddr *peer,
+ struct sockaddr *local ) {
+ struct sockaddr_tcpip *st_peer = ( struct sockaddr_tcpip * ) peer;
+ struct sockaddr_tcpip *st_local = ( struct sockaddr_tcpip * ) local;
+ struct tcp_connection *tcp;
+ unsigned int bind_port;
+ int rc;
- assert ( tcp );
- assert ( tcp->tcp_state == TCP_CLOSED );
+ /* Allocate and initialise structure */
+ tcp = malloc ( sizeof ( *tcp ) );
+ if ( ! tcp )
+ return -ENOMEM;
+ DBGC ( tcp, "TCP %p allocated\n", tcp );
+ memset ( tcp, 0, sizeof ( *tcp ) );
+ xfer_init ( &tcp->xfer, &tcp_xfer_operations, &tcp->refcnt );
+ tcp->prev_tcp_state = TCP_CLOSED;
+ tcp->tcp_state = TCP_STATE_SENT ( TCP_SYN );
+ tcp_dump_state ( tcp );
+ tcp->snd_seq = random();
+ INIT_LIST_HEAD ( &tcp->queue );
+ tcp->timer.expired = tcp_expired;
+ memcpy ( &tcp->peer, st_peer, sizeof ( tcp->peer ) );
- stop_timer ( &tcp->timer );
- list_del ( &tcp->list );
- free ( tcp );
- DBGC ( tcp, "TCP %p freed\n", tcp );
+ /* Bind to local port */
+ bind_port = ( st_local ? st_local->st_port : 0 );
+ if ( ( rc = tcp_bind ( tcp, bind_port ) ) != 0 )
+ goto err;
+
+ /* Start timer to initiate SYN */
+ start_timer ( &tcp->timer );
+
+ /* Attach parent interface, transfer reference to connection
+ * list and return
+ */
+ xfer_plug_plug ( &tcp->xfer, xfer );
+ list_add ( &tcp->list, &tcp_conns );
+ return 0;
+
+ err:
+ ref_put ( &tcp->refcnt );
+ return rc;
}
/**
- * Abort TCP connection
+ * Close TCP connection
*
* @v tcp TCP connection
- * @v send_rst Send a RST after closing
- * @v rc Reason code
+ * @v rc Reason for close
+ *
+ * Closes the data transfer interface. If the TCP state machine is in
+ * a suitable state, the connection will be deleted.
*/
-static void tcp_abort ( struct tcp_connection *tcp, int send_rst, int rc ) {
+static void tcp_close ( struct tcp_connection *tcp, int rc ) {
+ struct io_buffer *iobuf;
+ struct io_buffer *tmp;
- /* Transition to CLOSED */
- tcp->tcp_state = TCP_CLOSED;
- tcp_dump_state ( tcp );
+ /* Close data transfer interface */
+ xfer_nullify ( &tcp->xfer );
+ xfer_close ( &tcp->xfer, rc );
+ tcp->xfer_closed = 1;
- /* Send RST if requested to do so */
- if ( send_rst )
- tcp_senddata_conn ( tcp, 1 );
+ /* If we are in CLOSED, or have otherwise not yet received a
+ * SYN (i.e. we are in LISTEN or SYN_SENT), just delete the
+ * connection.
+ */
+ if ( ! ( tcp->tcp_state & TCP_STATE_RCVD ( TCP_SYN ) ) ) {
- /* Close stream */
- stream_closed ( &tcp->stream, rc );
+ /* Transition to CLOSED for the sake of debugging messages */
+ tcp->tcp_state = TCP_CLOSED;
+ tcp_dump_state ( tcp );
- /* Free the connection */
- free_tcp ( tcp );
+ /* Free any unsent I/O buffers */
+ list_for_each_entry_safe ( iobuf, tmp, &tcp->queue, list ) {
+ list_del ( &iobuf->list );
+ free_iob ( iobuf );
+ }
+
+ /* Remove from list and drop reference */
+ stop_timer ( &tcp->timer );
+ list_del ( &tcp->list );
+ ref_put ( &tcp->refcnt );
+ DBGC ( tcp, "TCP %p connection deleted\n", tcp );
+ return;
+ }
+
+ /* If we have not had our SYN acknowledged (i.e. we are in
+ * SYN_RCVD), pretend that it has been acknowledged so that we
+ * can send a FIN without breaking things.
+ */
+ if ( ! ( tcp->tcp_state & TCP_STATE_ACKED ( TCP_SYN ) ) )
+ tcp_rx_ack ( tcp, ( tcp->snd_seq + 1 ), 0 );
+
+ /* If we have no data remaining to send, start sending FIN */
+ if ( list_empty ( &tcp->queue ) ) {
+ tcp->tcp_state |= TCP_STATE_SENT ( TCP_FIN );
+ tcp_dump_state ( tcp );
+ }
+}
+
+/***************************************************************************
+ *
+ * Transmit data path
+ *
+ ***************************************************************************
+ */
+
+/**
+ * Process TCP transmit queue
+ *
+ * @v tcp TCP connection
+ * @v max_len Maximum length to process
+ * @v dest I/O buffer to fill with data, or NULL
+ * @v remove Remove data from queue
+ * @ret len Length of data processed
+ *
+ * This processes at most @c max_len bytes from the TCP connection's
+ * transmit queue. Data will be copied into the @c dest I/O buffer
+ * (if provided) and, if @c remove is true, removed from the transmit
+ * queue.
+ */
+static size_t tcp_process_queue ( struct tcp_connection *tcp, size_t max_len,
+ struct io_buffer *dest, int remove ) {
+ struct io_buffer *iobuf;
+ struct io_buffer *tmp;
+ size_t frag_len;
+ size_t len = 0;
+
+ list_for_each_entry_safe ( iobuf, tmp, &tcp->queue, list ) {
+ frag_len = iob_len ( iobuf );
+ if ( frag_len > max_len )
+ frag_len = max_len;
+ if ( dest ) {
+ memcpy ( iob_put ( dest, frag_len ), iobuf->data,
+ frag_len );
+ }
+ if ( remove ) {
+ iob_pull ( iobuf, frag_len );
+ if ( ! iob_len ( iobuf ) ) {
+ list_del ( &iobuf->list );
+ free_iob ( iobuf );
+ }
+ }
+ len += frag_len;
+ max_len -= frag_len;
+ }
+ return len;
}
/**
* @v tcp TCP connection
* @v force_send Force sending of packet
*
- * Transmits any outstanding data on the connection. If the
- * connection is in a connected state, the application's senddata()
- * method will be called to generate the data payload, if any.
+ * Transmits any outstanding data on the connection.
*
* Note that even if an error is returned, the retransmission timer
* will have been started if necessary, and so the stack will
* eventually attempt to retransmit the failed packet.
*/
-static int tcp_senddata_conn ( struct tcp_connection *tcp, int force_send ) {
+static int tcp_xmit ( struct tcp_connection *tcp, int force_send ) {
struct io_buffer *iobuf;
struct tcp_header *tcphdr;
struct tcp_mss_option *mssopt;
void *payload;
unsigned int flags;
- size_t len;
+ size_t len = 0;
size_t seq_len;
size_t window;
int rc;
- /* Allocate space to the TX buffer */
- iobuf = alloc_iob ( MAX_IOB_LEN );
- if ( ! iobuf ) {
- DBGC ( tcp, "TCP %p could not allocate data buffer\n", tcp );
- /* Start the retry timer so that we attempt to
- * retransmit this packet later. (Start it
- * unconditionally, since without a I/O buffer we
- * can't call the senddata() callback, and so may not
- * be able to tell whether or not we have something
- * that actually needs to be retransmitted).
- */
- start_timer ( &tcp->timer );
- return -ENOMEM;
- }
- iob_reserve ( iobuf, MAX_HDR_LEN );
+ /* If retransmission timer is already running, do nothing */
+ if ( timer_running ( &tcp->timer ) )
+ return 0;
- /* If we are connected, call the senddata() method, which may
- * call tcp_send() to queue up a data payload.
+ /* Calculate both the actual (payload) and sequence space
+ * lengths that we wish to transmit.
*/
if ( TCP_CAN_SEND_DATA ( tcp->tcp_state ) ) {
- tcp->tx_iob = iobuf;
- stream_senddata ( &tcp->stream, iobuf->data,
- iob_tailroom ( iobuf ) );
- tcp->tx_iob = NULL;
+ len = tcp_process_queue ( tcp, tcp->snd_win, NULL, 0 );
}
-
- /* Truncate payload length to fit transmit window */
- len = iob_len ( iobuf );
- if ( len > tcp->snd_win )
- len = tcp->snd_win;
-
- /* Calculate amount of sequence space that this transmission
- * consumes. (SYN or FIN consume one byte, and we can never
- * send both at once).
- */
seq_len = len;
flags = TCP_FLAGS_SENDING ( tcp->tcp_state );
- assert ( ! ( ( flags & TCP_SYN ) && ( flags & TCP_FIN ) ) );
- if ( flags & ( TCP_SYN | TCP_FIN ) )
+ if ( flags & ( TCP_SYN | TCP_FIN ) ) {
+ /* SYN or FIN consume one byte, and we can never send both */
+ assert ( ! ( ( flags & TCP_SYN ) && ( flags & TCP_FIN ) ) );
seq_len++;
+ }
tcp->snd_sent = seq_len;
- /* If we have nothing to transmit, drop the packet */
- if ( ( seq_len == 0 ) && ! force_send ) {
- free_iob ( iobuf );
+ /* If we have nothing to transmit, stop now */
+ if ( ( seq_len == 0 ) && ! force_send )
return 0;
- }
/* If we are transmitting anything that requires
* acknowledgement (i.e. consumes sequence space), start the
- * retransmission timer.
+ * retransmission timer. Do this before attempting to
+ * allocate the I/O buffer, in case allocation itself fails.
*/
if ( seq_len )
start_timer ( &tcp->timer );
+ /* Allocate I/O buffer */
+ iobuf = alloc_iob ( len + MAX_HDR_LEN );
+ if ( ! iobuf ) {
+ DBGC ( tcp, "TCP %p could not allocate data buffer\n", tcp );
+ return -ENOMEM;
+ }
+ iob_reserve ( iobuf, MAX_HDR_LEN );
+
+ /* Fill data payload from transmit queue */
+ tcp_process_queue ( tcp, len, iobuf, 0 );
+
/* Estimate window size */
window = ( ( freemem * 3 ) / 4 );
if ( window > TCP_MAX_WINDOW_SIZE )
( rc == -ENETUNREACH ) ) {
DBGC ( tcp, "TCP %p aborting after TX failed: %s\n",
tcp, strerror ( rc ) );
- tcp_abort ( tcp, 0, rc );
+ tcp->tcp_state = TCP_CLOSED;
+ tcp_dump_state ( tcp );
+ tcp_close ( tcp, rc );
}
return rc;
}
-/**
- * Transmit any outstanding data
- *
- * @v stream TCP stream
- *
- * This function allocates space to the transmit buffer and invokes
- * the senddata() callback function, to allow the application to
- * transmit new data.
- */
-static int tcp_kick ( struct stream_connection *stream ) {
- struct tcp_connection *tcp =
- container_of ( stream, struct tcp_connection, stream );
-
- return tcp_senddata_conn ( tcp, 0 );
-}
-
-/**
- * Transmit data
- *
- * @v stream TCP stream
- * @v data Data to be sent
- * @v len Length of the data
- * @ret rc Return status code
- *
- * This function queues data to be sent via the TCP connection. It
- * can be called only in the context of an application's senddata()
- * method.
- */
-static int tcp_send ( struct stream_connection *stream,
- const void *data, size_t len ) {
- struct tcp_connection *tcp =
- container_of ( stream, struct tcp_connection, stream );
- struct io_buffer *iobuf;
-
- /* Check that we have a I/O buffer to fill */
- iobuf = tcp->tx_iob;
- if ( ! iobuf ) {
- DBGC ( tcp, "TCP %p tried to send data outside of the "
- "senddata() method\n", tcp );
- return -EINVAL;
- }
-
- /* Truncate length to fit I/O buffer */
- if ( len > iob_tailroom ( iobuf ) )
- len = iob_tailroom ( iobuf );
-
- /* Copy payload */
- memmove ( iob_put ( iobuf, len ), data, len );
-
- return 0;
-}
-
/**
* Retransmission timer expired
*
* this is the result of a graceful close, terminate
* the connection
*/
- tcp_abort ( tcp, 1, -ETIMEDOUT );
+ tcp->tcp_state = TCP_CLOSED;
+ tcp_dump_state ( tcp );
+ tcp_close ( tcp, -ETIMEDOUT );
} else {
/* Otherwise, retransmit the packet */
- tcp_senddata_conn ( tcp, 0 );
+ tcp_xmit ( tcp, 0 );
}
}
* @v in_tcphdr TCP header of incoming packet
* @ret rc Return status code
*/
-static int tcp_send_reset ( struct tcp_connection *tcp,
+static int tcp_xmit_reset ( struct tcp_connection *tcp,
+ struct sockaddr_tcpip *st_dest,
struct tcp_header *in_tcphdr ) {
struct io_buffer *iobuf;
struct tcp_header *tcphdr;
DBGC ( tcp, "\n" );
/* Transmit packet */
- return tcpip_tx ( iobuf, &tcp_protocol, &tcp->peer,
+ return tcpip_tx ( iobuf, &tcp_protocol, st_dest,
NULL, &tcphdr->csum );
}
+/***************************************************************************
+ *
+ * Receive data path
+ *
+ ***************************************************************************
+ */
+
/**
* Identify TCP connection by local port number
*
* @v local_port Local port (in network-endian order)
* @ret tcp TCP connection, or NULL
*/
-static struct tcp_connection * tcp_demux ( uint16_t local_port ) {
+static struct tcp_connection * tcp_demux ( unsigned int local_port ) {
struct tcp_connection *tcp;
list_for_each_entry ( tcp, &tcp_conns, list ) {
/* Mark SYN as received and start sending ACKs with each packet */
tcp->tcp_state |= ( TCP_STATE_SENT ( TCP_ACK ) |
- TCP_STATE_RCVD ( TCP_SYN ) );
+ TCP_STATE_RCVD ( TCP_SYN ) );
/* Acknowledge SYN */
tcp->rcv_ack++;
- /* Notify application of established connection, if applicable */
- if ( ( tcp->tcp_state & TCP_STATE_ACKED ( TCP_SYN ) ) )
- stream_connected ( &tcp->stream );
-
return 0;
}
uint32_t win ) {
size_t ack_len = ( ack - tcp->snd_seq );
size_t len;
- unsigned int acked_flags = 0;
+ unsigned int acked_flags;
/* Ignore duplicate or out-of-range ACK */
if ( ack_len > tcp->snd_sent ) {
return -EINVAL;
}
- /* If we are sending flags and this ACK acknowledges all
- * outstanding sequence points, then it acknowledges the
- * flags. (This works since both SYN and FIN will always be
- * the last outstanding sequence point.)
- */
+ /* Acknowledge any flags being sent */
len = ack_len;
- if ( ack_len == tcp->snd_sent ) {
- acked_flags = ( TCP_FLAGS_SENDING ( tcp->tcp_state ) &
- ( TCP_SYN | TCP_FIN ) );
- if ( acked_flags )
- len--;
- }
+ acked_flags = ( TCP_FLAGS_SENDING ( tcp->tcp_state ) &
+ ( TCP_SYN | TCP_FIN ) );
+ if ( acked_flags )
+ len--;
/* Update SEQ and sent counters, and window size */
tcp->snd_seq = ack;
/* Stop the retransmission timer */
stop_timer ( &tcp->timer );
- /* Notify application of acknowledged data, if any */
- if ( len )
- stream_acked ( &tcp->stream, len );
-
+ /* Remove any acknowledged data from transmit queue */
+ tcp_process_queue ( tcp, len, NULL, 1 );
+
/* Mark SYN/FIN as acknowledged if applicable. */
if ( acked_flags )
tcp->tcp_state |= TCP_STATE_ACKED ( acked_flags );
- /* Notify application of established connection, if applicable */
- if ( ( acked_flags & TCP_SYN ) &&
- ( tcp->tcp_state & TCP_STATE_RCVD ( TCP_SYN ) ) ) {
- stream_connected ( &tcp->stream );
- }
+ /* Start sending FIN if we've had all possible data ACKed */
+ if ( list_empty ( &tcp->queue ) && tcp->xfer_closed )
+ tcp->tcp_state |= TCP_STATE_SENT ( TCP_FIN );
return 0;
}
*
* @v tcp TCP connection
* @v seq SEQ value (in host-endian order)
- * @v data Data buffer
- * @v len Length of data buffer
+ * @v iobuf I/O buffer
* @ret rc Return status code
+ *
+ * This function takes ownership of the I/O buffer.
*/
static int tcp_rx_data ( struct tcp_connection *tcp, uint32_t seq,
- void *data, size_t len ) {
+ struct io_buffer *iobuf ) {
size_t already_rcvd;
+ size_t len;
+ int rc;
/* Ignore duplicate data */
already_rcvd = ( tcp->rcv_ack - seq );
- if ( already_rcvd >= len )
+ len = iob_len ( iobuf );
+ if ( already_rcvd >= len ) {
+ free_iob ( iobuf );
return 0;
- data += already_rcvd;
- len -= already_rcvd;
+ }
+ iob_pull ( iobuf, already_rcvd );
+
+ /* Deliver data to application */
+ if ( ( rc = xfer_deliver_iob ( &tcp->xfer, iobuf ) ) != 0 )
+ return rc;
/* Acknowledge new data */
tcp->rcv_ack += len;
-
- /* Notify application */
- stream_newdata ( &tcp->stream, data, len );
-
return 0;
}
if ( ( tcp->rcv_ack - seq ) > 0 )
return 0;
- /* Mark FIN as received, acknowledge it, and send our own FIN */
- tcp->tcp_state |= ( TCP_STATE_RCVD ( TCP_FIN ) |
- TCP_STATE_SENT ( TCP_FIN ) );
+ /* Mark FIN as received and acknowledge it */
+ tcp->tcp_state |= TCP_STATE_RCVD ( TCP_FIN );
tcp->rcv_ack++;
- /* Close stream */
- stream_closed ( &tcp->stream, 0 );
+ /* Close connection */
+ tcp_close ( tcp, 0 );
return 0;
}
return 0;
}
- /* Abort connection without sending a RST */
- tcp_abort ( tcp, 0, -ECONNRESET );
+ /* Abort connection */
+ tcp->tcp_state = TCP_CLOSED;
+ tcp_dump_state ( tcp );
+ tcp_close ( tcp, -ECONNRESET );
return -ECONNRESET;
}
* @ret rc Return status code
*/
static int tcp_rx ( struct io_buffer *iobuf,
- struct sockaddr_tcpip *st_src __unused,
+ struct sockaddr_tcpip *st_src,
struct sockaddr_tcpip *st_dest __unused,
uint16_t pshdr_csum ) {
struct tcp_header *tcphdr = iobuf->data;
uint32_t ack;
uint32_t win;
unsigned int flags;
- void *data;
size_t len;
int rc;
DBG ( "TCP packet too short at %d bytes (min %d bytes)\n",
iob_len ( iobuf ), sizeof ( *tcphdr ) );
rc = -EINVAL;
- goto done;
+ goto discard;
}
hlen = ( ( tcphdr->hlen & TCP_MASK_HLEN ) / 16 ) * 4;
if ( hlen < sizeof ( *tcphdr ) ) {
DBG ( "TCP header too short at %d bytes (min %d bytes)\n",
hlen, sizeof ( *tcphdr ) );
rc = -EINVAL;
- goto done;
+ goto discard;
}
if ( hlen > iob_len ( iobuf ) ) {
DBG ( "TCP header too long at %d bytes (max %d bytes)\n",
hlen, iob_len ( iobuf ) );
rc = -EINVAL;
- goto done;
+ goto discard;
}
csum = tcpip_continue_chksum ( pshdr_csum, iobuf->data, iob_len ( iobuf ));
if ( csum != 0 ) {
DBG ( "TCP checksum incorrect (is %04x including checksum "
"field, should be 0000)\n", csum );
rc = -EINVAL;
- goto done;
+ goto discard;
}
/* Parse parameters from header and strip header */
ack = ntohl ( tcphdr->ack );
win = ntohs ( tcphdr->win );
flags = tcphdr->flags;
- data = iob_pull ( iobuf, hlen );
+ iob_pull ( iobuf, hlen );
len = iob_len ( iobuf );
/* Dump header */
/* If no connection was found, send RST */
if ( ! tcp ) {
- tcp_send_reset ( tcp, tcphdr );
+ tcp_xmit_reset ( tcp, st_src, tcphdr );
rc = -ENOTCONN;
- goto done;
+ goto discard;
}
/* Handle ACK, if present */
if ( flags & TCP_ACK ) {
if ( ( rc = tcp_rx_ack ( tcp, ack, win ) ) != 0 ) {
- tcp_send_reset ( tcp, tcphdr );
- goto done;
+ tcp_xmit_reset ( tcp, st_src, tcphdr );
+ goto discard;
}
}
/* Handle RST, if present */
if ( flags & TCP_RST ) {
if ( ( rc = tcp_rx_rst ( tcp, seq ) ) != 0 )
- goto done;
+ goto discard;
}
/* Handle new data, if any */
- tcp_rx_data ( tcp, seq, data, len );
+ tcp_rx_data ( tcp, seq, iobuf );
seq += len;
/* Handle FIN, if present */
/* Send out any pending data. If peer is expecting an ACK for
* this packet then force sending a reply.
*/
- tcp_senddata_conn ( tcp, ( start_seq != seq ) );
+ tcp_xmit ( tcp, ( start_seq != seq ) );
/* If this packet was the last we expect to receive, set up
* timer to expire and cause the connection to be freed.
start_timer ( &tcp->timer );
}
- rc = 0;
- done:
+ return 0;
+
+ discard:
/* Free received packet */
free_iob ( iobuf );
return rc;
}
-/**
- * Bind TCP connection to local port
+/** TCP protocol */
+struct tcpip_protocol tcp_protocol __tcpip_protocol = {
+ .name = "TCP",
+ .rx = tcp_rx,
+ .tcpip_proto = IP_TCP,
+};
+
+/***************************************************************************
*
- * @v stream TCP stream
- * @v local Local address
- * @ret rc Return status code
+ * Data transfer interface
*
- * Only the port portion of the local address is used. If the local
- * port is 0, the connection is assigned an available port between
- * 1024 and 65535.
+ ***************************************************************************
*/
-static int tcp_bind ( struct stream_connection *stream,
- struct sockaddr *local ) {
- struct tcp_connection *tcp =
- container_of ( stream, struct tcp_connection, stream );
- struct sockaddr_tcpip *st = ( ( struct sockaddr_tcpip * ) local );
- struct tcp_connection *existing;
- struct sockaddr_tcpip try;
- static uint16_t try_port = 1024;
- uint16_t local_port = st->st_port;
- /* If no port specified, find the first available port */
- if ( ! local_port ) {
- for ( ; try_port ; try_port++ ) {
- if ( try_port < 1024 )
- continue;
- try.st_port = htons ( try_port );
- if ( tcp_bind ( stream,
- ( struct sockaddr * ) &try ) == 0 ) {
- return 0;
- }
- }
- DBGC ( tcp, "TCP %p could not bind: no free ports\n", tcp );
- return -EADDRINUSE;
- }
+/**
+ * Close interface
+ *
+ * @v xfer Data transfer interface
+ * @v rc Reason for close
+ */
+static void tcp_xfer_close ( struct xfer_interface *xfer, int rc ) {
+ struct tcp_connection *tcp =
+ container_of ( xfer, struct tcp_connection, xfer );
- /* Attempt bind to local port */
- list_for_each_entry ( existing, &tcp_conns, list ) {
- if ( existing->local_port == local_port ) {
- DBGC ( tcp, "TCP %p could not bind: port %d in use\n",
- tcp, ntohs ( local_port ) );
- return -EADDRINUSE;
- }
- }
- tcp->local_port = local_port;
+ /* Close data transfer interface */
+ tcp_close ( tcp, rc );
- DBGC ( tcp, "TCP %p bound to port %d\n", tcp, ntohs ( local_port ) );
- return 0;
+ /* Transmit FIN, if possible */
+ tcp_xmit ( tcp, 0 );
}
/**
- * Connect to a remote server
+ * Seek to position
*
- * @v stream TCP stream
- * @v peer Remote socket address
+ * @v xfer Data transfer interface
+ * @v offset Offset to new position
+ * @v whence Basis for new position
* @ret rc Return status code
- *
- * This function initiates a TCP connection to the socket address specified in
- * peer. It sends a SYN packet to peer. When the connection is established, the
- * TCP stack calls the connected() callback function.
*/
-static int tcp_connect ( struct stream_connection *stream,
- struct sockaddr *peer ) {
+static int tcp_xfer_seek ( struct xfer_interface *xfer, off_t offset,
+ int whence ) {
struct tcp_connection *tcp =
- container_of ( stream, struct tcp_connection, stream );
- struct sockaddr_tcpip *st = ( ( struct sockaddr_tcpip * ) peer );
- struct sockaddr_tcpip local;
- int rc;
+ container_of ( xfer, struct tcp_connection, xfer );
- /* Bind to local port if not already bound */
- if ( ! tcp->local_port ) {
- local.st_port = 0;
- if ( ( rc = tcp_bind ( stream,
- ( struct sockaddr * ) &local ) ) != 0 ){
- return rc;
- }
- }
+ /* TCP doesn't support seeking to arbitrary positions */
+ if ( ( whence != SEEK_CUR ) || ( offset != 0 ) )
+ return -EINVAL;
- /* Bind to peer */
- memcpy ( &tcp->peer, st, sizeof ( tcp->peer ) );
+ /* Not ready if we're not in a suitable connection state */
+ if ( ! TCP_CAN_SEND_DATA ( tcp->tcp_state ) )
+ return -EAGAIN;
- /* Transition to TCP_SYN_SENT and send the SYN */
- tcp->tcp_state = TCP_SYN_SENT;
- tcp_dump_state ( tcp );
- tcp_senddata_conn ( tcp, 0 );
+ /* Not ready if data queue is non-empty */
+ if ( ! list_empty ( &tcp->queue ) )
+ return -EAGAIN;
return 0;
}
/**
- * Close the connection
+ * Deliver datagram as I/O buffer
*
- * @v stream TCP stream
- *
- * The TCP connection will persist until the state machine has
- * returned to the TCP_CLOSED state.
+ * @v xfer Data transfer interface
+ * @v iobuf Datagram I/O buffer
+ * @ret rc Return status code
*/
-static void tcp_close ( struct stream_connection *stream ) {
+static int tcp_xfer_deliver_iob ( struct xfer_interface *xfer,
+ struct io_buffer *iobuf ) {
struct tcp_connection *tcp =
- container_of ( stream, struct tcp_connection, stream );
+ container_of ( xfer, struct tcp_connection, xfer );
- /* If we have not yet received a SYN (i.e. we are in CLOSED,
- * LISTEN or SYN_SENT), just delete the connection
- */
- if ( ! ( tcp->tcp_state & TCP_STATE_RCVD ( TCP_SYN ) ) ) {
- tcp->tcp_state = TCP_CLOSED;
- tcp_dump_state ( tcp );
- free_tcp ( tcp );
- return;
- }
+ /* Enqueue packet */
+ list_add_tail ( &iobuf->list, &tcp->queue );
- /* If we have not had our SYN acknowledged (i.e. we are in
- * SYN_RCVD), pretend that it has been acknowledged so that we
- * can send a FIN without breaking things.
- */
- if ( ! ( tcp->tcp_state & TCP_STATE_ACKED ( TCP_SYN ) ) )
- tcp_rx_ack ( tcp, ( tcp->snd_seq + 1 ), 0 );
+ /* Transmit data, if possible */
+ tcp_xmit ( tcp, 0 );
- /* Send a FIN to initiate the close */
- tcp->tcp_state |= TCP_STATE_SENT ( TCP_FIN );
- tcp_dump_state ( tcp );
- tcp_senddata_conn ( tcp, 0 );
+ return 0;
}
-/**
- * Open TCP connection
+/** TCP data transfer interface operations */
+static struct xfer_interface_operations tcp_xfer_operations = {
+ .close = tcp_xfer_close,
+ .vredirect = ignore_xfer_vredirect,
+ .request = ignore_xfer_request,
+ .seek = tcp_xfer_seek,
+ .alloc_iob = default_xfer_alloc_iob,
+ .deliver_iob = tcp_xfer_deliver_iob,
+ .deliver_raw = xfer_deliver_as_iob,
+};
+
+/***************************************************************************
*
- * @v app Stream application
- * @ret rc Return status code
+ * Openers
+ *
+ ***************************************************************************
*/
-int tcp_open ( struct stream_application *app ) {
- struct tcp_connection *tcp;
- /* Application must not already have an open connection */
- if ( app->conn ) {
- DBG ( "TCP app %p already open on %p\n", app, app->conn );
- return -EISCONN;
- }
+/** TCP socket opener */
+struct socket_opener tcp_socket_opener __socket_opener = {
+ .semantics = SOCK_STREAM,
+ .family = AF_INET,
+ .open = tcp_open,
+};
- /* Allocate connection state storage and add to connection list */
- tcp = alloc_tcp();
- if ( ! tcp ) {
- DBG ( "TCP app %p could not allocate connection\n", app );
- return -ENOMEM;
- }
+/**
+ * Open TCP URI
+ *
+ * @v xfer Data transfer interface
+ * @v uri URI
+ * @ret rc Return status code
+ */
+static int tcp_open_uri ( struct xfer_interface *xfer, struct uri *uri ) {
+ struct sockaddr_tcpip peer;
- /* Associate application with connection */
- stream_associate ( app, &tcp->stream );
+ /* Sanity check */
+ if ( ! uri->host )
+ return -EINVAL;
- return 0;
+ memset ( &peer, 0, sizeof ( peer ) );
+ peer.st_port = htons ( uri_port ( uri, 0 ) );
+ return xfer_open_named_socket ( xfer, SOCK_STREAM,
+ ( struct sockaddr * ) &peer,
+ uri->host, NULL );
}
-/** TCP stream operations */
-static struct stream_connection_operations tcp_op = {
- .bind = tcp_bind,
- .connect = tcp_connect,
- .close = tcp_close,
- .send = tcp_send,
- .kick = tcp_kick,
+/** TCP URI opener */
+struct uri_opener tcp_uri_opener __uri_opener = {
+ .scheme = "tcp",
+ .open = tcp_open_uri,
};
-/** TCP protocol */
-struct tcpip_protocol tcp_protocol __tcpip_protocol = {
- .name = "TCP",
- .rx = tcp_rx,
- .tcpip_proto = IP_TCP,
-};
-#include <stddef.h>
+#include <stdint.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <assert.h>
#include <errno.h>
#include <byteswap.h>
-#include <gpxe/async.h>
-#include <gpxe/buffer.h>
+#include <gpxe/socket.h>
+#include <gpxe/tcpip.h>
+#include <gpxe/in.h>
+#include <gpxe/xfer.h>
+#include <gpxe/open.h>
#include <gpxe/uri.h>
-#include <gpxe/download.h>
-#include <gpxe/tcp.h>
#include <gpxe/ftp.h>
/** @file
*
*/
+/**
+ * FTP states
+ *
+ * These @b must be sequential, i.e. a successful FTP session must
+ * pass through each of these states in order.
+ */
+enum ftp_state {
+ FTP_CONNECT = 0,
+ FTP_USER,
+ FTP_PASS,
+ FTP_TYPE,
+ FTP_PASV,
+ FTP_RETR,
+ FTP_QUIT,
+ FTP_DONE,
+};
+
+/**
+ * An FTP request
+ *
+ */
+struct ftp_request {
+ /** Reference counter */
+ struct refcnt refcnt;
+ /** Data transfer interface */
+ struct xfer_interface xfer;
+
+ /** URI being fetched */
+ struct uri *uri;
+ /** FTP control channel interface */
+ struct xfer_interface control;
+ /** FTP data channel interface */
+ struct xfer_interface data;
+
+ /** Current state */
+ enum ftp_state state;
+ /** Buffer to be filled with data received via the control channel */
+ char *recvbuf;
+ /** Remaining size of recvbuf */
+ size_t recvsize;
+ /** FTP status code, as text */
+ char status_text[5];
+ /** Passive-mode parameters, as text */
+ char passive_text[24]; /* "aaa,bbb,ccc,ddd,eee,fff" */
+};
+
+/**
+ * Free FTP request
+ *
+ * @v refcnt Reference counter
+ */
+static void ftp_free ( struct refcnt *refcnt ) {
+ struct ftp_request *ftp =
+ container_of ( refcnt, struct ftp_request, refcnt );
+
+ DBGC ( ftp, "FTP %p freed\n", ftp );
+
+ uri_put ( ftp->uri );
+ free ( ftp );
+}
+
+/**
+ * Mark FTP operation as complete
+ *
+ * @v ftp FTP request
+ * @v rc Return status code
+ */
+static void ftp_done ( struct ftp_request *ftp, int rc ) {
+
+ DBGC ( ftp, "FTP %p completed (%s)\n", ftp, strerror ( rc ) );
+
+ /* Close all data transfer interfaces */
+ xfer_nullify ( &ftp->xfer );
+ xfer_close ( &ftp->xfer, rc );
+ xfer_nullify ( &ftp->control );
+ xfer_close ( &ftp->control, rc );
+ xfer_nullify ( &ftp->data );
+ xfer_close ( &ftp->data, rc );
+}
+
/*****************************************************************************
*
* FTP control channel
*
*/
-/** FTP control channel strings
+/**
+ * FTP control channel strings
*
* These are used as printf() format strings. Since only one of them
* (RETR) takes an argument, we always supply that argument to the
};
/**
- * Get FTP request from control stream application
+ * Handle control channel being closed
*
- * @v app Stream application
- * @ret ftp FTP request
- */
-static inline struct ftp_request *
-stream_to_ftp ( struct stream_application *app ) {
- return container_of ( app, struct ftp_request, stream );
-}
-
-/**
- * Mark FTP operation as complete
+ * @v control FTP control channel interface
+ * @v rc Reason for close
*
- * @v ftp FTP request
- * @v rc Return status code
+ * When the control channel is closed, the data channel must also be
+ * closed, if it is currently open.
*/
-static void ftp_done ( struct ftp_request *ftp, int rc ) {
-
- DBGC ( ftp, "FTP %p completed with status %d\n", ftp, rc );
+static void ftp_control_close ( struct xfer_interface *control, int rc ) {
+ struct ftp_request *ftp =
+ container_of ( control, struct ftp_request, control );
- /* Close both stream connections */
- stream_close ( &ftp->stream );
- stream_close ( &ftp->stream_data );
+ DBGC ( ftp, "FTP %p control connection closed: %s\n",
+ ftp, strerror ( rc ) );
- /* Mark asynchronous operation as complete */
- async_done ( &ftp->async, rc );
+ /* Complete FTP operation */
+ ftp_done ( ftp, rc );
}
/**
*/
static void ftp_reply ( struct ftp_request *ftp ) {
char status_major = ftp->status_text[0];
+ char separator = ftp->status_text[3];
DBGC ( ftp, "FTP %p received status %s\n", ftp, ftp->status_text );
+ /* Ignore malformed lines */
+ if ( separator != ' ' )
+ return;
+
/* Ignore "intermediate" responses (1xx codes) */
if ( status_major == '1' )
return;
sizeof ( sa.sin.sin_addr ) );
ftp_parse_value ( &ptr, ( uint8_t * ) &sa.sin.sin_port,
sizeof ( sa.sin.sin_port ) );
- if ( ( rc = tcp_open ( &ftp->stream_data ) ) != 0 ) {
+ if ( ( rc = xfer_open_socket ( &ftp->data, SOCK_STREAM,
+ &sa.sa, NULL ) ) != 0 ) {
DBGC ( ftp, "FTP %p could not open data connection\n",
ftp );
ftp_done ( ftp, rc );
return;
}
- if ( ( rc = stream_connect ( &ftp->stream_data,
- &sa.sa ) ) != 0 ){
- DBGC ( ftp, "FTP %p could not make data connection\n",
- ftp );
- ftp_done ( ftp, rc );
- return;
- }
}
/* Move to next state */
if ( ftp->state < FTP_DONE )
ftp->state++;
- ftp->already_sent = 0;
+ /* Send control string */
if ( ftp->state < FTP_DONE ) {
DBGC ( ftp, "FTP %p sending ", ftp );
DBGC ( ftp, ftp_strings[ftp->state], ftp->uri->path );
+ xfer_printf ( &ftp->control, ftp_strings[ftp->state],
+ ftp->uri->path );
}
-
- return;
}
/**
* Handle new data arriving on FTP control channel
*
- * @v app Stream application
+ * @v control FTP control channel interface
* @v data New data
* @v len Length of new data
*
* Data is collected until a complete line is received, at which point
* its information is passed to ftp_reply().
*/
-static void ftp_newdata ( struct stream_application *app,
- void *data, size_t len ) {
- struct ftp_request *ftp = stream_to_ftp ( app );
+static int ftp_control_deliver_raw ( struct xfer_interface *control,
+ const void *data, size_t len ) {
+ struct ftp_request *ftp =
+ container_of ( control, struct ftp_request, control );
char *recvbuf = ftp->recvbuf;
size_t recvsize = ftp->recvsize;
char c;
/* Store for next invocation */
ftp->recvbuf = recvbuf;
ftp->recvsize = recvsize;
-}
-
-/**
- * Handle acknowledgement of data sent on FTP control channel
- *
- * @v app Stream application
- */
-static void ftp_acked ( struct stream_application *app, size_t len ) {
- struct ftp_request *ftp = stream_to_ftp ( app );
-
- /* Mark off ACKed portion of the currently-transmitted data */
- ftp->already_sent += len;
-}
-/**
- * Construct data to send on FTP control channel
- *
- * @v app Stream application
- * @v buf Temporary data buffer
- * @v len Length of temporary data buffer
- */
-static void ftp_senddata ( struct stream_application *app,
- void *buf, size_t len ) {
- struct ftp_request *ftp = stream_to_ftp ( app );
-
- /* Send the as-yet-unACKed portion of the string for the
- * current state.
- */
- len = snprintf ( buf, len, ftp_strings[ftp->state], ftp->uri->path );
- stream_send ( app, buf + ftp->already_sent, len - ftp->already_sent );
-}
-
-/**
- * Handle control channel being closed
- *
- * @v app Stream application
- *
- * When the control channel is closed, the data channel must also be
- * closed, if it is currently open.
- */
-static void ftp_closed ( struct stream_application *app, int rc ) {
- struct ftp_request *ftp = stream_to_ftp ( app );
-
- DBGC ( ftp, "FTP %p control connection closed: %s\n",
- ftp, strerror ( rc ) );
-
- /* Complete FTP operation */
- ftp_done ( ftp, rc );
+ return 0;
}
/** FTP control channel operations */
-static struct stream_application_operations ftp_stream_operations = {
- .closed = ftp_closed,
- .acked = ftp_acked,
- .newdata = ftp_newdata,
- .senddata = ftp_senddata,
+static struct xfer_interface_operations ftp_control_operations = {
+ .close = ftp_control_close,
+ .vredirect = xfer_vopen,
+ .request = ignore_xfer_request,
+ .seek = ignore_xfer_seek,
+ .alloc_iob = default_xfer_alloc_iob,
+ .deliver_iob = xfer_deliver_as_raw,
+ .deliver_raw = ftp_control_deliver_raw,
};
/*****************************************************************************
*/
/**
- * Get FTP request from data stream application
- *
- * @v app Stream application
- * @ret ftp FTP request
- */
-static inline struct ftp_request *
-stream_to_ftp_data ( struct stream_application *app ) {
- return container_of ( app, struct ftp_request, stream_data );
-}
-
-/**
- * Handle data channel being closed
+ * Handle FTP data channel being closed
*
- * @v app Stream application
+ * @v data FTP data channel interface
+ * @v rc Reason for closure
*
* When the data channel is closed, the control channel should be left
* alone; the server will send a completion message via the control
*
* If the data channel is closed due to an error, we abort the request.
*/
-static void ftp_data_closed ( struct stream_application *app, int rc ) {
- struct ftp_request *ftp = stream_to_ftp_data ( app );
+static void ftp_data_closed ( struct xfer_interface *data, int rc ) {
+ struct ftp_request *ftp =
+ container_of ( data, struct ftp_request, data );
DBGC ( ftp, "FTP %p data connection closed: %s\n",
ftp, strerror ( rc ) );
}
/**
- * Handle new data arriving on the FTP data channel
+ * Handle data delivery via FTP data channel
*
- * @v app Stream application
- * @v data New data
- * @v len Length of new data
+ * @v xfer FTP data channel interface
+ * @v iobuf I/O buffer
+ * @ret rc Return status code
*/
-static void ftp_data_newdata ( struct stream_application *app,
- void *data, size_t len ) {
- struct ftp_request *ftp = stream_to_ftp_data ( app );
+static int ftp_data_deliver_iob ( struct xfer_interface *data,
+ struct io_buffer *iobuf ) {
+ struct ftp_request *ftp =
+ container_of ( data, struct ftp_request, data );
int rc;
- /* Fill data buffer */
- if ( ( rc = fill_buffer ( ftp->buffer, data,
- ftp->buffer->fill, len ) ) != 0 ){
- DBGC ( ftp, "FTP %p failed to fill data buffer: %s\n",
+ if ( ( rc = xfer_deliver_iob ( &ftp->xfer, iobuf ) ) != 0 ) {
+ DBGC ( ftp, "FTP %p failed to deliver data: %s\n",
ftp, strerror ( rc ) );
- ftp_done ( ftp, rc );
- return;
+ return rc;
}
+
+ return 0;
}
/** FTP data channel operations */
-static struct stream_application_operations ftp_data_stream_operations = {
- .closed = ftp_data_closed,
- .newdata = ftp_data_newdata,
+static struct xfer_interface_operations ftp_data_operations = {
+ .close = ftp_data_closed,
+ .vredirect = xfer_vopen,
+ .request = ignore_xfer_request,
+ .seek = ignore_xfer_seek,
+ .alloc_iob = default_xfer_alloc_iob,
+ .deliver_iob = ftp_data_deliver_iob,
+ .deliver_raw = xfer_deliver_as_iob,
};
/*****************************************************************************
*
- * API
+ * Data transfer interface
*
*/
/**
- * Reap asynchronous operation
+ * Close FTP data transfer interface
*
- * @v async Asynchronous operation
+ * @v xfer FTP data transfer interface
+ * @v rc Reason for close
*/
-static void ftp_reap ( struct async *async ) {
+static void ftp_xfer_closed ( struct xfer_interface *xfer, int rc ) {
struct ftp_request *ftp =
- container_of ( async, struct ftp_request, async );
+ container_of ( xfer, struct ftp_request, xfer );
- free ( ftp );
+ DBGC ( ftp, "FTP %p data transfer interface closed: %s\n",
+ ftp, strerror ( rc ) );
+
+ ftp_done ( ftp, rc );
}
-/** FTP asynchronous operations */
-static struct async_operations ftp_async_operations = {
- .reap = ftp_reap,
+/** FTP data transfer interface operations */
+static struct xfer_interface_operations ftp_xfer_operations = {
+ .close = ftp_xfer_closed,
+ .vredirect = ignore_xfer_vredirect,
+ .request = ignore_xfer_request,
+ .seek = ignore_xfer_seek,
+ .alloc_iob = default_xfer_alloc_iob,
+ .deliver_iob = xfer_deliver_as_raw,
+ .deliver_raw = ignore_xfer_deliver_raw,
};
+/*****************************************************************************
+ *
+ * URI opener
+ *
+ */
+
/**
* Initiate an FTP connection
*
+ * @v xfer Data transfer interface
* @v uri Uniform Resource Identifier
- * @v buffer Buffer into which to download file
- * @v parent Parent asynchronous operation
* @ret rc Return status code
*/
-int ftp_get ( struct uri *uri, struct buffer *buffer, struct async *parent ) {
- struct ftp_request *ftp = NULL;
+static int ftp_open ( struct xfer_interface *xfer, struct uri *uri ) {
+ struct ftp_request *ftp;
+ struct sockaddr_tcpip server;
int rc;
/* Sanity checks */
- if ( ! uri->path ) {
- rc = -EINVAL;
- goto err;
- }
+ if ( ! uri->path )
+ return -EINVAL;
+ if ( ! uri->host )
+ return -EINVAL;
- /* Allocate and populate FTP structure */
+ /* Allocate and populate structure */
ftp = malloc ( sizeof ( *ftp ) );
- if ( ! ftp ) {
- rc = -ENOMEM;
- goto err;
- }
+ if ( ! ftp )
+ return -ENOMEM;
memset ( ftp, 0, sizeof ( *ftp ) );
- ftp->uri = uri;
- ftp->buffer = buffer;
- ftp->state = FTP_CONNECT;
- ftp->already_sent = 0;
+ ftp->refcnt.free = ftp_free;
+ xfer_init ( &ftp->xfer, &ftp_xfer_operations, &ftp->refcnt );
+ ftp->uri = uri_get ( uri );
+ xfer_init ( &ftp->control, &ftp_control_operations, &ftp->refcnt );
+ xfer_init ( &ftp->data, &ftp_data_operations, &ftp->refcnt );
ftp->recvbuf = ftp->status_text;
ftp->recvsize = sizeof ( ftp->status_text ) - 1;
- ftp->stream.op = &ftp_stream_operations;
- ftp->stream_data.op = &ftp_data_stream_operations;
-
-#warning "Quick name resolution hack"
- union {
- struct sockaddr sa;
- struct sockaddr_in sin;
- } server;
- server.sin.sin_port = htons ( FTP_PORT );
- server.sin.sin_family = AF_INET;
- if ( inet_aton ( uri->host, &server.sin.sin_addr ) == 0 ) {
- rc = -EINVAL;
- goto err;
- }
DBGC ( ftp, "FTP %p fetching %s\n", ftp, ftp->uri->path );
- if ( ( rc = tcp_open ( &ftp->stream ) ) != 0 )
- goto err;
- if ( ( rc = stream_connect ( &ftp->stream, &server.sa ) ) != 0 )
+ /* Open control connection */
+ memset ( &server, 0, sizeof ( server ) );
+ server.st_port = htons ( uri_port ( uri, FTP_PORT ) );
+ if ( ( rc = xfer_open_named_socket ( &ftp->control, SOCK_STREAM,
+ ( struct sockaddr * ) &server,
+ uri->host, NULL ) ) != 0 )
goto err;
- async_init ( &ftp->async, &ftp_async_operations, parent );
+ /* Attach to parent interface, mortalise self, and return */
+ xfer_plug_plug ( &ftp->xfer, xfer );
+ ref_put ( &ftp->refcnt );
return 0;
err:
DBGC ( ftp, "FTP %p could not create request: %s\n",
ftp, strerror ( rc ) );
- free ( ftp );
+ ftp_done ( ftp, rc );
+ ref_put ( &ftp->refcnt );
return rc;
}
-/** HTTP download protocol */
-struct download_protocol ftp_download_protocol __download_protocol = {
- .name = "ftp",
- .start_download = ftp_get,
+/** FTP URI opener */
+struct uri_opener ftp_uri_opener __uri_opener = {
+ .scheme = "ftp",
+ .open = ftp_open,
};
*
*/
-#include <stddef.h>
+#include <stdint.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <byteswap.h>
#include <errno.h>
#include <assert.h>
-#include <gpxe/async.h>
#include <gpxe/uri.h>
-#include <gpxe/buffer.h>
-#include <gpxe/download.h>
-#include <gpxe/resolv.h>
-#include <gpxe/tcp.h>
+#include <gpxe/refcnt.h>
+#include <gpxe/iobuf.h>
+#include <gpxe/xfer.h>
+#include <gpxe/open.h>
+#include <gpxe/socket.h>
+#include <gpxe/tcpip.h>
+#include <gpxe/process.h>
+#include <gpxe/linebuf.h>
#include <gpxe/tls.h>
#include <gpxe/http.h>
-static struct async_operations http_async_operations;
+/** HTTP receive state */
+enum http_rx_state {
+ HTTP_RX_RESPONSE = 0,
+ HTTP_RX_HEADER,
+ HTTP_RX_DATA,
+ HTTP_RX_DEAD,
+};
-static inline struct http_request *
-stream_to_http ( struct stream_application *app ) {
- return container_of ( app, struct http_request, stream );
-}
+/**
+ * An HTTP request
+ *
+ */
+struct http_request {
+ /** Reference count */
+ struct refcnt refcnt;
+ /** Data transfer interface */
+ struct xfer_interface xfer;
+
+ /** URI being fetched */
+ struct uri *uri;
+ /** Transport layer interface */
+ struct xfer_interface socket;
+
+ /** TX process */
+ struct process process;
+
+ /** HTTP response code */
+ unsigned int response;
+ /** HTTP Content-Length */
+ size_t content_length;
+ /** Received length */
+ size_t rx_len;
+ /** RX state */
+ enum http_rx_state rx_state;
+ /** Line buffer for received header lines */
+ struct line_buffer linebuf;
+};
+
+/**
+ * Free HTTP request
+ *
+ * @v refcnt Reference counter
+ */
+static void http_free ( struct refcnt *refcnt ) {
+ struct http_request *http =
+ container_of ( refcnt, struct http_request, refcnt );
+
+ uri_put ( http->uri );
+ empty_line_buffer ( &http->linebuf );
+ free ( http );
+};
/**
* Mark HTTP request as complete
*
* @v http HTTP request
* @v rc Return status code
- *
*/
static void http_done ( struct http_request *http, int rc ) {
- /* Close stream connection */
- stream_close ( &http->stream );
-
/* Prevent further processing of any current packet */
http->rx_state = HTTP_RX_DEAD;
- /* Free up any dynamically allocated storage */
- empty_line_buffer ( &http->linebuf );
-
/* If we had a Content-Length, and the received content length
* isn't correct, flag an error
*/
if ( http->content_length &&
- ( http->content_length != http->buffer->fill ) ) {
+ ( http->content_length != http->rx_len ) ) {
DBGC ( http, "HTTP %p incorrect length %zd, should be %zd\n",
- http, http->buffer->fill, http->content_length );
+ http, http->rx_len, http->content_length );
rc = -EIO;
}
- /* Mark async operation as complete */
- async_done ( &http->async, rc );
+ /* Remove process */
+ process_del ( &http->process );
+
+ /* Close all data transfer interfaces */
+ xfer_nullify ( &http->socket );
+ xfer_close ( &http->socket, rc );
+ xfer_nullify ( &http->xfer );
+ xfer_close ( &http->xfer, rc );
}
/**
*
* @v http HTTP request
* @v response HTTP response
+ * @ret rc Return status code
*/
-static void http_rx_response ( struct http_request *http, char *response ) {
+static int http_rx_response ( struct http_request *http, char *response ) {
char *spc;
- int rc = -EIO;
+ int rc;
DBGC ( http, "HTTP %p response \"%s\"\n", http, response );
/* Check response starts with "HTTP/" */
if ( strncmp ( response, "HTTP/", 5 ) != 0 )
- goto err;
+ return -EIO;
/* Locate and check response code */
spc = strchr ( response, ' ' );
if ( ! spc )
- goto err;
+ return -EIO;
http->response = strtoul ( spc, NULL, 10 );
if ( ( rc = http_response_to_rc ( http->response ) ) != 0 )
- goto err;
+ return rc;
/* Move to received headers */
http->rx_state = HTTP_RX_HEADER;
- return;
-
- err:
- DBGC ( http, "HTTP %p bad response\n", http );
- http_done ( http, rc );
- return;
+ return 0;
}
/**
static int http_rx_content_length ( struct http_request *http,
const char *value ) {
char *endp;
- int rc;
http->content_length = strtoul ( value, &endp, 10 );
if ( *endp != '\0' ) {
return -EIO;
}
- /* Try to presize the receive buffer */
- if ( ( rc = expand_buffer ( http->buffer,
- http->content_length ) ) != 0 ) {
- /* May as well abandon the download now; it will fail */
- DBGC ( http, "HTTP %p could not presize buffer: %s\n",
- http, strerror ( rc ) );
- return rc;
- }
+ /* Use seek() to notify recipient of filesize */
+ xfer_seek ( &http->xfer, http->content_length, SEEK_SET );
+ xfer_seek ( &http->xfer, 0, SEEK_SET );
return 0;
}
-/**
- * An HTTP header handler
- *
- */
+/** An HTTP header handler */
struct http_header_handler {
/** Name (e.g. "Content-Length") */
const char *header;
*
* @v http HTTP request
* @v header HTTP header
+ * @ret rc Return status code
*/
-static void http_rx_header ( struct http_request *http, char *header ) {
+static int http_rx_header ( struct http_request *http, char *header ) {
struct http_header_handler *handler;
char *separator;
char *value;
- int rc = -EIO;
+ int rc;
/* An empty header line marks the transition to the data phase */
if ( ! header[0] ) {
DBGC ( http, "HTTP %p start of data\n", http );
empty_line_buffer ( &http->linebuf );
http->rx_state = HTTP_RX_DATA;
- return;
+ return 0;
}
DBGC ( http, "HTTP %p header \"%s\"\n", http, header );
/* Split header at the ": " */
separator = strstr ( header, ": " );
- if ( ! separator )
- goto err;
+ if ( ! separator ) {
+ DBGC ( http, "HTTP %p malformed header\n", http );
+ return -EIO;
+ }
*separator = '\0';
value = ( separator + 2 );
for ( handler = http_header_handlers ; handler->header ; handler++ ) {
if ( strcasecmp ( header, handler->header ) == 0 ) {
if ( ( rc = handler->rx ( http, value ) ) != 0 )
- goto err;
+ return rc;
break;
}
}
- return;
-
- err:
- DBGC ( http, "HTTP %p bad header\n", http );
- http_done ( http, rc );
- return;
+ return 0;
}
+/** An HTTP line-based data handler */
+struct http_line_handler {
+ /** Handle line
+ *
+ * @v http HTTP request
+ * @v line Line to handle
+ * @ret rc Return status code
+ */
+ int ( * rx ) ( struct http_request *http, char *line );
+};
+
+/** List of HTTP line-based data handlers */
+struct http_line_handler http_line_handlers[] = {
+ [HTTP_RX_RESPONSE] = { .rx = http_rx_response },
+ [HTTP_RX_HEADER] = { .rx = http_rx_header },
+};
+
/**
* Handle new data arriving via HTTP connection in the data phase
*
* @v http HTTP request
- * @v data New data
- * @v len Length of new data
+ * @v iobuf I/O buffer
+ * @ret rc Return status code
*/
-static void http_rx_data ( struct http_request *http,
- const char *data, size_t len ) {
+static int http_rx_data ( struct http_request *http,
+ struct io_buffer *iobuf ) {
int rc;
- /* Fill data buffer */
- if ( ( rc = fill_buffer ( http->buffer, data,
- http->buffer->fill, len ) ) != 0 ) {
- DBGC ( http, "HTTP %p failed to fill data buffer: %s\n",
- http, strerror ( rc ) );
- http_done ( http, rc );
- return;
- }
+ /* Update received length */
+ http->rx_len += iob_len ( iobuf );
- /* Update progress */
- http->async.completed = http->buffer->fill;
- http->async.total = http->content_length;
+ /* Hand off data buffer */
+ if ( ( rc = xfer_deliver_iob ( &http->xfer, iobuf ) ) != 0 )
+ return rc;
/* If we have reached the content-length, stop now */
if ( http->content_length &&
- ( http->buffer->fill >= http->content_length ) ) {
+ ( http->rx_len >= http->content_length ) ) {
http_done ( http, 0 );
}
+
+ return 0;
}
/**
* Handle new data arriving via HTTP connection
*
- * @v http HTTP request
- * @v data New data
- * @v len Length of new data
+ * @v socket Transport layer interface
+ * @v iobuf I/O buffer
+ * @ret rc Return status code
*/
-static void http_newdata ( struct stream_application *app,
- void *data, size_t len ) {
- struct http_request *http = stream_to_http ( app );
- const char *buf = data;
+static int http_socket_deliver_iob ( struct xfer_interface *socket,
+ struct io_buffer *iobuf ) {
+ struct http_request *http =
+ container_of ( socket, struct http_request, socket );
+ struct http_line_handler *lh;
char *line;
- int rc;
+ ssize_t len;
+ int rc = 0;
- while ( len ) {
- if ( http->rx_state == HTTP_RX_DEAD ) {
+ while ( iob_len ( iobuf ) ) {
+ switch ( http->rx_state ) {
+ case HTTP_RX_DEAD:
/* Do no further processing */
- return;
- } else if ( http->rx_state == HTTP_RX_DATA ) {
+ goto done;
+ case HTTP_RX_DATA:
/* Once we're into the data phase, just fill
* the data buffer
*/
- http_rx_data ( http, buf, len );
- return;
- } else {
+ rc = http_rx_data ( http, iobuf );
+ iobuf = NULL;
+ goto done;
+ case HTTP_RX_RESPONSE:
+ case HTTP_RX_HEADER:
/* In the other phases, buffer and process a
* line at a time
*/
- if ( ( rc = line_buffer ( &http->linebuf, &buf,
- &len ) ) != 0 ) {
+ len = line_buffer ( &http->linebuf, iobuf->data,
+ iob_len ( iobuf ) );
+ if ( len < 0 ) {
DBGC ( http, "HTTP %p could not buffer line: "
"%s\n", http, strerror ( rc ) );
- http_done ( http, rc );
- return;
+ goto done;
}
- if ( ( line = buffered_line ( &http->linebuf ) ) ) {
- switch ( http->rx_state ) {
- case HTTP_RX_RESPONSE:
- http_rx_response ( http, line );
- break;
- case HTTP_RX_HEADER:
- http_rx_header ( http, line );
- break;
- default:
- assert ( 0 );
- break;
- }
+ iob_pull ( iobuf, len );
+ line = buffered_line ( &http->linebuf );
+ if ( line ) {
+ lh = &http_line_handlers[http->rx_state];
+ if ( ( rc = lh->rx ( http, line ) ) != 0 )
+ goto done;
}
+ break;
+ default:
+ assert ( 0 );
+ break;
}
}
+
+ done:
+ if ( rc )
+ http_done ( http, rc );
+ free_iob ( iobuf );
+ return rc;
}
/**
- * Send HTTP data
+ * HTTP process
*
- * @v app Stream application
- * @v buf Temporary data buffer
- * @v len Length of temporary data buffer
+ * @v process Process
*/
-static void http_senddata ( struct stream_application *app,
- void *buf, size_t len ) {
- struct http_request *http = stream_to_http ( app );
+static void http_step ( struct process *process ) {
+ struct http_request *http =
+ container_of ( process, struct http_request, process );
const char *path = http->uri->path;
const char *host = http->uri->host;
const char *query = http->uri->query;
+ int rc;
- len = snprintf ( buf, len,
- "GET %s%s%s HTTP/1.1\r\n"
- "User-Agent: gPXE/" VERSION "\r\n"
- "Host: %s\r\n"
- "\r\n",
- ( path ? path : "/" ),
- ( query ? "?" : "" ),
- ( query ? query : "" ),
- host );
-
- stream_send ( app, ( buf + http->tx_offset ),
- ( len - http->tx_offset ) );
+ if ( xfer_ready ( &http->socket ) == 0 ) {
+ process_del ( &http->process );
+ if ( ( rc = xfer_printf ( &http->socket,
+ "GET %s%s%s HTTP/1.1\r\n"
+ "User-Agent: gPXE/" VERSION "\r\n"
+ "Host: %s\r\n"
+ "\r\n",
+ ( path ? path : "/" ),
+ ( query ? "?" : "" ),
+ ( query ? query : "" ),
+ host ) ) != 0 ) {
+ http_done ( http, rc );
+ }
+ }
}
/**
- * HTTP data acknowledged
+ * HTTP connection closed by network stack
*
- * @v app Stream application
- * @v len Length of acknowledged data
+ * @v socket Transport layer interface
+ * @v rc Reason for close
*/
-static void http_acked ( struct stream_application *app, size_t len ) {
- struct http_request *http = stream_to_http ( app );
+static void http_socket_close ( struct xfer_interface *socket, int rc ) {
+ struct http_request *http =
+ container_of ( socket, struct http_request, socket );
- http->tx_offset += len;
+ DBGC ( http, "HTTP %p socket closed: %s\n",
+ http, strerror ( rc ) );
+
+ http_done ( http, rc );
}
+/** HTTP socket operations */
+static struct xfer_interface_operations http_socket_operations = {
+ .close = http_socket_close,
+ .vredirect = xfer_vopen,
+ .request = ignore_xfer_request,
+ .seek = ignore_xfer_seek,
+ .alloc_iob = default_xfer_alloc_iob,
+ .deliver_iob = http_socket_deliver_iob,
+ .deliver_raw = xfer_deliver_as_iob,
+};
+
/**
- * HTTP connection closed by network stack
+ * Close HTTP data transfer interface
*
- * @v app Stream application
+ * @v xfer Data transfer interface
+ * @v rc Reason for close
*/
-static void http_closed ( struct stream_application *app, int rc ) {
- struct http_request *http = stream_to_http ( app );
+static void http_xfer_close ( struct xfer_interface *xfer, int rc ) {
+ struct http_request *http =
+ container_of ( xfer, struct http_request, xfer );
- DBGC ( http, "HTTP %p connection closed: %s\n",
+ DBGC ( http, "HTTP %p interface closed: %s\n",
http, strerror ( rc ) );
-
+
http_done ( http, rc );
}
-/** HTTP stream operations */
-static struct stream_application_operations http_stream_operations = {
- .closed = http_closed,
- .acked = http_acked,
- .newdata = http_newdata,
- .senddata = http_senddata,
+/** HTTP data transfer interface operations */
+static struct xfer_interface_operations http_xfer_operations = {
+ .close = http_xfer_close,
+ .vredirect = ignore_xfer_vredirect,
+ .request = ignore_xfer_request,
+ .seek = ignore_xfer_seek,
+ .alloc_iob = default_xfer_alloc_iob,
+ .deliver_iob = xfer_deliver_as_raw,
+ .deliver_raw = ignore_xfer_deliver_raw,
};
/**
- * Initiate a HTTP connection
+ * Initiate an HTTP connection
*
+ * @v xfer Data transfer interface
* @v uri Uniform Resource Identifier
- * @v buffer Buffer into which to download file
- * @v parent Parent asynchronous operation
* @ret rc Return status code
*/
-int http_get ( struct uri *uri, struct buffer *buffer, struct async *parent ) {
- struct http_request *http = NULL;
- struct sockaddr_tcpip *st;
+int http_open ( struct xfer_interface *xfer, struct uri *uri ) {
+ struct http_request *http;
+ struct sockaddr_tcpip server;
int rc;
+ /* Sanity checks */
+ if ( ! uri->host )
+ return -EINVAL;
+
/* Allocate and populate HTTP structure */
http = malloc ( sizeof ( *http ) );
if ( ! http )
return -ENOMEM;
memset ( http, 0, sizeof ( *http ) );
- http->uri = uri;
- http->buffer = buffer;
- async_init ( &http->async, &http_async_operations, parent );
- http->stream.op = &http_stream_operations;
- st = ( struct sockaddr_tcpip * ) &http->server;
- st->st_port = htons ( uri_port ( http->uri, HTTP_PORT ) );
-
- /* Open TCP connection */
- if ( ( rc = tcp_open ( &http->stream ) ) != 0 )
+ http->refcnt.free = http_free;
+ xfer_init ( &http->xfer, &http_xfer_operations, &http->refcnt );
+ http->uri = uri_get ( uri );
+ xfer_init ( &http->socket, &http_socket_operations, &http->refcnt );
+ process_init ( &http->process, http_step, &http->refcnt );
+
+ /* Open socket */
+ memset ( &server, 0, sizeof ( server ) );
+ server.st_port = htons ( uri_port ( http->uri, HTTP_PORT ) );
+ if ( ( rc = xfer_open_named_socket ( &http->socket, SOCK_STREAM,
+ ( struct sockaddr * ) &server,
+ uri->host, NULL ) ) != 0 )
goto err;
+
+#if 0
if ( strcmp ( http->uri->scheme, "https" ) == 0 ) {
st->st_port = htons ( uri_port ( http->uri, HTTPS_PORT ) );
if ( ( rc = add_tls ( &http->stream ) ) != 0 )
goto err;
}
+#endif
- /* Start name resolution. The download proper will start when
- * name resolution completes.
- */
- if ( ( rc = resolv ( uri->host, &http->server, &http->async ) ) != 0 )
- goto err;
-
+ /* Attach to parent interface, mortalise self, and return */
+ xfer_plug_plug ( &http->xfer, xfer );
+ ref_put ( &http->refcnt );
return 0;
err:
DBGC ( http, "HTTP %p could not create request: %s\n",
http, strerror ( rc ) );
- async_uninit ( &http->async );
- free ( http );
+ http_done ( http, rc );
+ ref_put ( &http->refcnt );
return rc;
}
-/**
- * Handle name resolution completion
- *
- * @v async HTTP asynchronous operation
- * @v signal SIGCHLD
- */
-static void http_sigchld ( struct async *async, enum signal signal __unused ) {
- struct http_request *http =
- container_of ( async, struct http_request, async );
- int rc;
-
- /* If name resolution failed, abort now */
- async_wait ( async, &rc, 1 );
- if ( rc != 0 ) {
- http_done ( http, rc );
- return;
- }
-
- /* Otherwise, start the HTTP connection */
- if ( ( rc = stream_connect ( &http->stream, &http->server ) ) != 0 ) {
- DBGC ( http, "HTTP %p could not connect stream: %s\n",
- http, strerror ( rc ) );
- http_done ( http, rc );
- return;
- }
-}
-
-/**
- * Free HTTP connection
- *
- * @v async Asynchronous operation
- */
-static void http_reap ( struct async *async ) {
- free ( container_of ( async, struct http_request, async ) );
-}
-
-/** HTTP asynchronous operations */
-static struct async_operations http_async_operations = {
- .reap = http_reap,
- .signal = {
- [SIGCHLD] = http_sigchld,
- },
-};
-
-/** HTTP download protocol */
-struct download_protocol http_download_protocol __download_protocol = {
- .name = "http",
- .start_download = http_get,
+/** HTTP URI opener */
+struct uri_opener http_uri_opener __uri_opener = {
+ .scheme = "http",
+ .open = http_open,
};
-/** HTTPS download protocol */
-struct download_protocol https_download_protocol __download_protocol = {
- .name = "https",
- .start_download = http_get,
+/** HTTPS URI opener */
+struct uri_opener https_uri_opener __uri_opener = {
+ .scheme = "https",
+ .open = http_open,
};
}
/** DNS name resolver */
-struct resolver dns_resolver __resolver = {
+struct resolver dns_resolver __resolver ( RESOLV_NORMAL ) = {
.name = "DNS",
.resolv = dns_resolv,
};