[ds6-devel] nc6/src connection.c,1.18,1.18.2.1
connection.h,1.12,1.12.2.1
main.c,1.16,1.16.2.1 network.c,1.28,1.28.2.1 network.h,1.5,1.5.2.1
parser.c,1.31,1.31.2.1
chris at deepspace6.net
chris at deepspace6.net
Mon Jan 6 01:50:13 CET 2003
Update of /cvs/nc6/src
Modified Files:
Tag: chris_refactor_060103
connection.c connection.h main.c network.c network.h parser.c
Log Message:
Reduced connection_attributes to purely an attribute store. It just stores
the values to use for configuring everything (the buffer size, etc) rather
than configuring things directly.
Reorganised main such that the buffers and io_streams and initialized there,
using values pulled from the ca for buffer size, mtu, nru, etc.
Changed do_connect / do_listen so that instead of modifing the remote_stream
inside the ca, they just return the fd & socktype. main (or actually a static
establish_connection function) then sticks these into the remote io_stream.
Broke main up a bit more (main, connection_main, establish_connection &
run_transfer).
Index: connection.c
===================================================================
RCS file: /cvs/nc6/src/connection.c,v
retrieving revision 1.18
retrieving revision 1.18.2.1
diff -u -d -r1.18 -r1.18.2.1
--- connection.c 5 Jan 2003 13:40:01 -0000 1.18
+++ connection.c 6 Jan 2003 00:50:11 -0000 1.18.2.1
@@ -44,28 +44,14 @@
address_init(&(attrs->remote_address));
address_init(&(attrs->local_address));
- cb_init(&(attrs->remote_buffer), DEFAULT_BUFFER_SIZE);
- cb_init(&(attrs->local_buffer), DEFAULT_BUFFER_SIZE);
-
- /* setup the remote stream to read into the remote buffer and write
- * from the local buffer */
- io_stream_init(&(attrs->remote_stream), "remote",
- &(attrs->remote_buffer), &(attrs->local_buffer));
-
- /* setup the local stream to read into the local buffer and write
- * from the remote buffer */
- io_stream_init(&(attrs->local_stream), "local",
- &(attrs->local_buffer), &(attrs->remote_buffer));
-
- /* the remote stream has an instant hold timeout by default,
- * which means that as soon as the remote read stream closes, the
- * entire connection will be torn down */
- ios_set_hold_timeout(&(attrs->remote_stream), 0);
-
- /* by default we don't send TCP half closes to the remote system */
- ios_suppress_half_close(&(attrs->remote_stream), TRUE);
-
- /* no connect timeout */
+ /* set default values for everything */
+ attrs->buffer_size = DEFAULT_BUFFER_SIZE;
+ attrs->remote_mtu = 0;
+ attrs->remote_nru = 0;
+ attrs->local_hold_timeout = -1;
+ attrs->remote_hold_timeout = 0;
+ attrs->remote_half_close_suppress = TRUE;
+ attrs->local_half_close_suppress = FALSE;
attrs->connect_timeout = -1;
}
@@ -74,9 +60,6 @@
void ca_destroy(connection_attributes *attrs)
{
assert(attrs != NULL);
-
- io_stream_destroy(&(attrs->remote_stream));
- io_stream_destroy(&(attrs->local_stream));
}
@@ -125,31 +108,4 @@
default:
fatal("internal error: unknown socket type");
}
-}
-
-
-void ca_warn_details(const connection_attributes *attrs)
-{
- switch (attrs->remote_stream.socktype) {
- case SOCK_STREAM:
- warn("using stream socket");
- break;
- case SOCK_DGRAM:
- warn("using datagram socket");
- break;
- default:
- fatal("internal error: unsupported socktype %d",
- attrs->remote_stream.socktype);
- }
-
- warn("using remote receive buffer size of %d",
- attrs->remote_buffer.buf_size);
-
- if (attrs->remote_stream.nru)
- warn("using remote receive nru of %d",
- attrs->remote_stream.nru);
-
- if (attrs->remote_stream.mtu)
- warn("using remote send mtu of %d",
- attrs->remote_stream.mtu);
}
Index: connection.h
===================================================================
RCS file: /cvs/nc6/src/connection.h,v
retrieving revision 1.12
retrieving revision 1.12.2.1
diff -u -d -r1.12 -r1.12.2.1
--- connection.h 5 Jan 2003 13:44:31 -0000 1.12
+++ connection.h 6 Jan 2003 00:50:11 -0000 1.12.2.1
@@ -52,17 +52,20 @@
sock_protocol protocol;
address remote_address;
address local_address;
- circ_buf remote_buffer;
- circ_buf local_buffer;
- io_stream remote_stream;
- io_stream local_stream;
+ size_t buffer_size;
+ size_t remote_mtu;
+ size_t remote_nru;
+ int local_hold_timeout;
+ int remote_hold_timeout;
+ bool remote_half_close_suppress;
+ bool local_half_close_suppress;
int connect_timeout;
} connection_attributes;
+
void ca_init(connection_attributes *attrs);
void ca_destroy(connection_attributes *attrs);
-
#define ca_set_family(CA, FAMILY) ((CA)->family = (FAMILY))
#define ca_set_protocol(CA, PROTO) ((CA)->protocol = (PROTO))
@@ -71,32 +74,35 @@
#define ca_set_remote_addr(CA, ADDR) ((CA)->remote_address = (ADDR))
#define ca_set_local_addr(CA, ADDR) ((CA)->local_address = (ADDR))
-#define ca_set_MTU(CA, MTU) ios_set_mtu(&((CA)->remote_stream),(MTU))
-#define ca_set_NRU(CA, NRU) ios_set_nru(&((CA)->remote_stream),(NRU))
-
-#define ca_connect_timeout(CA) ((CA)->connect_timeout)
-#define ca_set_connection_timeout(CA, CT) ((CA)->connect_timeout = (CT))
+#define ca_buffer_size(CA) ((CA)->buffer_size)
+#define ca_set_buffer_size(CA, SZ) ((CA)->buffer_size = (SZ))
-#define ca_remote_stream(CA) (&((CA)->remote_stream))
-#define ca_local_stream(CA) (&((CA)->local_stream))
-
-#define ca_suppress_half_close_remote(CA) \
- ios_suppress_half_close(&((CA)->remote_stream), FALSE)
-#define ca_suppress_half_close_local(CA) \
- ios_suppress_half_close(&((CA)->local_stream), FALSE)
+#define ca_remote_MTU(CA) ((CA)->remote_mtu)
+#define ca_set_remote_MTU(CA, MTU) ((CA)->remote_mtu = (MTU))
+
+#define ca_remote_NRU(CA) ((CA)->remote_nru)
+#define ca_set_remote_NRU(CA, NRU) ((CA)->remote_nru = (NRU))
-#define ca_set_hold_timeout_remote(CA, T) \
- ios_set_hold_timeout(&((CA)->remote_stream), (T))
-#define ca_set_hold_timeout_local(CA, T) \
- ios_set_hold_timeout(&((CA)->local_stream), (T))
+#define ca_connect_timeout(CA) ((CA)->connect_timeout)
+#define ca_set_connect_timeout(CA, CT) ((CA)->connect_timeout = (CT))
-#define ca_resize_local_buf(CA, SIZE) cb_resize(&((CA)->local_buffer),(SIZE))
-#define ca_resize_remote_buf(CA, SIZE) cb_resize(&((CA)->remote_buffer),(SIZE))
+#define ca_remote_hold_timeout(CA) ((CA)->remote_hold_timeout)
+#define ca_set_remote_hold_timeout(CA, T) \
+ ((CA)->remote_hold_timeout = (T))
+#define ca_local_hold_timeout(CA) ((CA)->local_hold_timeout)
+#define ca_set_local_hold_timeout(CA, T) \
+ ((CA)->local_hold_timeout = (T))
+#define ca_remote_half_close_suppress(CA) \
+ ((CA)->remote_half_close_suppress)
+#define ca_set_remote_half_close_suppress(CA, B) \
+ ((CA)->remote_half_close_suppress = (B))
+#define ca_local_half_close_suppress(CA) \
+ ((CA)->local_half_close_suppress)
+#define ca_set_local_half_close_suppress(CA, B) \
+ ((CA)->local_half_close_suppress = (B))
+
/* fill out an addrinfo structure with parameters from the ca */
void ca_to_addrinfo(struct addrinfo *ainfo, const connection_attributes *attrs);
-
-/* display the details of the connection attributes to stderr */
-void ca_warn_details(const connection_attributes *attrs);
#endif /* CONNECTION_H */
Index: main.c
===================================================================
RCS file: /cvs/nc6/src/main.c,v
retrieving revision 1.16
retrieving revision 1.16.2.1
diff -u -d -r1.16 -r1.16.2.1
--- main.c 5 Jan 2003 13:40:01 -0000 1.16
+++ main.c 6 Jan 2003 00:50:11 -0000 1.16.2.1
@@ -20,6 +20,8 @@
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
#include "config.h"
+#include "connection.h"
+#include "io_stream.h"
#include "parser.h"
#include "network.h"
#include "readwrite.h"
@@ -34,17 +36,19 @@
static char *program_name = NULL;
-static void establish_connection(int mode, connection_attributes *attrs);
-static int do_transfer(connection_attributes *attrs);
+static int connection_main(int mode, connection_attributes *attrs);
+static void establish_connection(int mode, connection_attributes *attrs,
+ io_stream *remote_stream);
+static int run_transfer(io_stream *remote_stream, io_stream *local_stream);
int main(int argc, char **argv)
{
connection_attributes connection_attrs;
- int mode;
char *ptr;
- int retval;
+ int mode, retval;
+ /* initialise connection attributes */
ca_init(&connection_attrs);
/* save the program name in a static variable */
@@ -61,45 +65,114 @@
/* set flags and fill out the addresses and connection attributes */
mode = parse_arguments(argc, argv, &connection_attrs);
+ /* handle the connection */
+ retval = connection_main(mode, &connection_attrs);
+
+ /* cleanup */
+ ca_destroy(&connection_attrs);
+
+ return (retval)? EXIT_FAILURE : EXIT_SUCCESS;
+}
+
+
+int connection_main(int mode, connection_attributes *attrs)
+{
+ circ_buf remote_buffer, local_buffer;
+ io_stream remote_stream, local_stream;
+ int retval;
+
+ /* initialise buffers */
+ cb_init(&remote_buffer, ca_buffer_size(attrs));
+ cb_init(&local_buffer, ca_buffer_size(attrs));
+
+ /* initialise io streams */
+ io_stream_init(&remote_stream, "remote", &remote_buffer, &local_buffer);
+ io_stream_init(&local_stream, "local", &local_buffer, &remote_buffer);
+
+ /* establish remote connection */
+ establish_connection(mode, attrs, &remote_stream);
+
/* setup local stream */
- ios_assign_stdio(ca_local_stream(&connection_attrs));
+ ios_assign_stdio(&local_stream);
- /* establish remote connection */
- establish_connection(mode, &connection_attrs);
+ /* set remote mtu & nru */
+ ios_set_mtu(&remote_stream, ca_remote_MTU(attrs));
+ ios_set_nru(&remote_stream, ca_remote_NRU(attrs));
+
+ /* set stream hold timeouts */
+ ios_set_hold_timeout(&remote_stream, ca_remote_hold_timeout(attrs));
+ ios_set_hold_timeout(&local_stream, ca_local_hold_timeout(attrs));
+
+ /* set stream half close suppression */
+ ios_suppress_half_close(&remote_stream,
+ ca_remote_half_close_suppress(attrs));
+ ios_suppress_half_close(&local_stream,
+ ca_local_half_close_suppress(attrs));
/* give information about the connection in very verbose mode */
- if (is_flag_set(VERY_VERBOSE_MODE) == TRUE)
- ca_warn_details(&connection_attrs);
+ if (is_flag_set(VERY_VERBOSE_MODE) == TRUE) {
+ warn("using buffer size of %d", remote_buffer.buf_size);
+ if (remote_stream.nru > 0)
+ warn("using remote receive nru of %d",
+ remote_stream.nru);
[...81 lines suppressed...]
Index: network.c
===================================================================
RCS file: /cvs/nc6/src/network.c,v
retrieving revision 1.28
retrieving revision 1.28.2.1
diff -u -d -r1.28 -r1.28.2.1
--- network.c 5 Jan 2003 13:40:01 -0000 1.28
+++ network.c 6 Jan 2003 00:50:11 -0000 1.28.2.1
@@ -37,7 +37,7 @@
RCSID("@(#) $Header$");
-void do_connect(connection_attributes *attrs)
+int do_connect(connection_attributes *attrs, int *rt_socktype)
{
const address *remote, *local;
int err, fd = -1;
@@ -320,16 +320,19 @@
sbuf_num, sbuf_rev);
}
- /* fill out the remote io_stream */
- ios_assign_socket(ca_remote_stream(attrs), fd, ptr->ai_socktype);
+ /* return the socktype */
+ if (rt_socktype != NULL)
+ *rt_socktype = ptr->ai_socktype;
/* cleanup addrinfo structure */
freeaddrinfo(res);
+
+ return fd;
}
-void do_listen(connection_attributes *attrs)
+int do_listen(connection_attributes *attrs, int *rt_socktype)
{
const address *remote, *local;
int nfd, i, fd, err, ns = -1, socktype = -1, maxfd = -1;
@@ -719,6 +722,9 @@
/* free the bound_socket list */
destroy_bound_sockets(bound_sockets);
- /* fill out the remote io_stream */
- ios_assign_socket(ca_remote_stream(attrs), ns, socktype);
+ /* return the socktype */
+ if (rt_socktype != NULL)
+ *rt_socktype = socktype;
+
+ return ns;
}
Index: network.h
===================================================================
RCS file: /cvs/nc6/src/network.h,v
retrieving revision 1.5
retrieving revision 1.5.2.1
diff -u -d -r1.5 -r1.5.2.1
--- network.h 30 Dec 2002 22:35:47 -0000 1.5
+++ network.h 6 Jan 2003 00:50:11 -0000 1.5.2.1
@@ -24,8 +24,8 @@
#include "connection.h"
-/* establish a connection and fill out the local & remote streams in attrs */
-void do_connect(connection_attributes *attrs);
-void do_listen(connection_attributes *attrs);
+/* establish a connection and return a new fd and socktype */
+int do_connect(connection_attributes *attrs, int *socktype);
+int do_listen(connection_attributes *attrs, int *socktype);
#endif /* NETWORK_H */
Index: parser.c
===================================================================
RCS file: /cvs/nc6/src/parser.c,v
retrieving revision 1.31
retrieving revision 1.31.2.1
diff -u -d -r1.31 -r1.31.2.1
--- parser.c 4 Jan 2003 14:20:24 -0000 1.31
+++ parser.c 6 Jan 2003 00:50:11 -0000 1.31.2.1
@@ -324,30 +324,30 @@
/* setup connection timeout */
if (connect_timeout != -1) {
- ca_set_connection_timeout(attrs, connect_timeout);
+ ca_set_connect_timeout(attrs, connect_timeout);
}
/* setup half close mode */
if (half_close == TRUE) {
/* keep remote open after half close */
- ca_suppress_half_close_remote(attrs);
- ca_set_hold_timeout_remote(attrs, -1);
+ ca_set_remote_half_close_suppress(attrs, TRUE);
+ ca_set_remote_hold_timeout(attrs, -1);
}
/* setup hold timeout */
- if (set_local_hold_timeout == TRUE)
- ca_set_hold_timeout_local(attrs, local_hold_timeout);
if (set_remote_hold_timeout == TRUE)
- ca_set_hold_timeout_remote(attrs, remote_hold_timeout);
+ ca_set_remote_hold_timeout(attrs, remote_hold_timeout);
+ if (set_local_hold_timeout == TRUE)
+ ca_set_local_hold_timeout(attrs, local_hold_timeout);
/* setup mtu, nru and buffer size if they were specified */
if (remote_mtu > 0)
- ca_set_MTU(attrs, remote_mtu);
+ ca_set_remote_MTU(attrs, remote_mtu);
if (remote_nru > 0)
- ca_set_NRU(attrs, remote_nru);
+ ca_set_remote_NRU(attrs, remote_nru);
if (buffer_size > 0) {
- ca_resize_remote_buf(attrs, buffer_size);
- ca_resize_local_buf(attrs, buffer_size);
+ ca_set_buffer_size(attrs, buffer_size);
+ ca_set_buffer_size(attrs, buffer_size);
}
assert(ret != -1);
More information about the ds6-devel
mailing list