[ds6-devel] nc6/src connection.c,1.17,1.18 connection.h,1.10,1.11 filter.c,1.15,1.16 main.c,1.15,1.16 network.c,1.27,1.28

chris at deepspace6.net chris at deepspace6.net
Sun Jan 5 14:40:03 CET 2003


Update of /cvs/nc6/src

Modified Files:
	connection.c connection.h filter.c main.c network.c 
Log Message:
Cleaned up main.
Normalized ca interface - providing additional accessors.

Yes - I've slightly ignored demeter's law for ca wrt the ca_remote_stream and
ca_local_stream accessors (they return 'objects').  But trying to do it any
other way results in all the code that was in main ending up in a function of
the ca and being just as messy as before.  I've taken the view that the ca
is basically just a storage for all our 'attributes', not a representation of
the actual connection, so returning these objects is necessary.


Index: connection.c
===================================================================
RCS file: /cvs/nc6/src/connection.c,v
retrieving revision 1.17
retrieving revision 1.18
diff -u -d -r1.17 -r1.18
--- connection.c	3 Jan 2003 09:30:20 -0000	1.17
+++ connection.c	5 Jan 2003 13:40:01 -0000	1.18
@@ -34,7 +34,7 @@
 static const size_t DEFAULT_BUFFER_SIZE = 8192;
 
 
-void connection_attributes_init(connection_attributes *attrs)
+void ca_init(connection_attributes *attrs)
 {
 	assert(attrs != NULL);
 
@@ -71,7 +71,7 @@
 
 
 
-void connection_attributes_destroy(connection_attributes *attrs)
+void ca_destroy(connection_attributes *attrs)
 {
 	assert(attrs != NULL);
 
@@ -81,8 +81,8 @@
 
 
 
-void connection_attributes_to_addrinfo(struct addrinfo *ainfo,
-                                       const connection_attributes *attrs)
+void ca_to_addrinfo(struct addrinfo *ainfo,
+                    const connection_attributes *attrs)
 {
 	assert(ainfo != NULL);
 	assert(attrs != NULL);
@@ -125,4 +125,31 @@
 		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.10
retrieving revision 1.11
diff -u -d -r1.10 -r1.11
--- connection.h	3 Jan 2003 23:25:19 -0000	1.10
+++ connection.h	5 Jan 2003 13:40:01 -0000	1.11
@@ -45,6 +45,7 @@
 
 #define address_init(AD)	((AD)->address = (AD)->service = NULL)
 
+
 typedef struct connection_attributes_t
 {
 	sock_family   family;
@@ -58,18 +59,26 @@
 	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))
+
+#define ca_remote_address(CA)	((const address*)&((CA)->remote_address))
+#define ca_local_address(CA)	((const address*)&((CA)->local_address))
 #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_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_set_connection_timeout(CA, CT)	\
-	((CA)->connect_timeout = (CT))
+#define ca_connect_timeout(CA)			((CA)->connect_timeout)
+#define ca_set_connection_timeout(CA, CT)	((CA)->connect_timeout = (CT))
+
+#define ca_remote_stream(CA)	(&((CA)->remote_stream))
+#define ca_local_stream(CA)	(&((CA)->remote_stream))
 	
 #define ca_suppress_half_close_remote(CA)	\
 	ios_suppress_half_close(&((CA)->remote_stream), FALSE)
@@ -84,10 +93,10 @@
 #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))
 
-void connection_attributes_init(connection_attributes *attrs);
-void connection_attributes_destroy(connection_attributes *attrs);
+/* fill out an addrinfo structure with parameters from the ca */
+void ca_to_addrinfo(struct addrinfo *ainfo, const connection_attributes *attrs);
 
-void connection_attributes_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: filter.c
===================================================================
RCS file: /cvs/nc6/src/filter.c,v
retrieving revision 1.15
retrieving revision 1.16
diff -u -d -r1.15 -r1.16
--- filter.c	3 Jan 2003 00:14:39 -0000	1.15
+++ filter.c	5 Jan 2003 13:40:01 -0000	1.16
@@ -185,7 +185,7 @@
 	ret = FALSE;
 	
 	memset(&hints, 0, sizeof(hints));
-	connection_attributes_to_addrinfo(&hints, attrs);
+	ca_to_addrinfo(&hints, attrs);
 
 	if (is_flag_set(NUMERIC_MODE) == TRUE)
 		hints.ai_flags |= AI_NUMERICHOST;
Index: main.c
===================================================================
RCS file: /cvs/nc6/src/main.c,v
retrieving revision 1.15
retrieving revision 1.16
diff -u -d -r1.15 -r1.16
--- main.c	1 Jan 2003 10:05:32 -0000	1.15
+++ main.c	5 Jan 2003 13:40:01 -0000	1.16
@@ -34,6 +34,10 @@
 static char *program_name  = NULL;
 
 
+static void establish_connection(int mode, connection_attributes *attrs);
+static int do_transfer(connection_attributes *attrs);
+
+
 int main(int argc, char **argv)
 {
 	connection_attributes connection_attrs;
@@ -41,7 +45,7 @@
 	char *ptr;
 	int retval;
 
-	connection_attributes_init(&connection_attrs);
+	ca_init(&connection_attrs);
 
 	/* save the program name in a static variable */
 	if ((ptr = strrchr(argv[0], '/')) != NULL) {
@@ -58,46 +62,45 @@
 	mode = parse_arguments(argc, argv, &connection_attrs);
 
 	/* setup local stream */
-	ios_assign_stdio(&(connection_attrs.local_stream));
+	ios_assign_stdio(ca_local_stream(&connection_attrs));
 	
 	/* establish remote connection */
+	establish_connection(mode, &connection_attrs);
+
+	/* give information about the connection in very verbose mode */
+	if (is_flag_set(VERY_VERBOSE_MODE) == TRUE)
+		ca_warn_details(&connection_attrs);
+
+	retval = do_transfer(&connection_attrs);
+
+	/* cleanup */
+	ca_destroy(&connection_attrs);
+
+	return (retval)? EXIT_FAILURE : EXIT_SUCCESS;
+}
+
+
+static void establish_connection(int mode, connection_attributes *attrs)
+{
+	/* establish remote connection */
 	switch (mode) {
 	case LISTEN_MODE:
-		do_listen(&connection_attrs);
+		do_listen(attrs);
 		break;
 	case CONNECT_MODE:
-		do_connect(&connection_attrs);
+		do_connect(attrs);
 		break;
 	default:
 		fatal("internal error: unknown connection mode");
 	}
+}
 
-	/* give information about the connection in very verbose mode */
-	if (is_flag_set(VERY_VERBOSE_MODE) == TRUE) {
-
-		switch (connection_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",
-			      connection_attrs.remote_stream.socktype);
-		}
-
-		warn("using remote receive buffer size of %d",
-		     connection_attrs.remote_buffer.buf_size);
-
-		if (connection_attrs.remote_stream.nru)
-			warn("using remote receive nru of %d",
-			     connection_attrs.remote_stream.nru);
 
-		if (connection_attrs.remote_stream.mtu)
-			warn("using remote send mtu of %d",
-			     connection_attrs.remote_stream.mtu);
-	}
+static int do_transfer(connection_attributes *attrs)
+{
+	io_stream *remote_stream = ca_remote_stream(attrs);
+	io_stream *local_stream = ca_local_stream(attrs);
+	int retval;
 
[...61 lines suppressed...]
Index: network.c
===================================================================
RCS file: /cvs/nc6/src/network.c,v
retrieving revision 1.27
retrieving revision 1.28
diff -u -d -r1.27 -r1.28
--- network.c	3 Jan 2003 00:14:39 -0000	1.27
+++ network.c	5 Jan 2003 13:40:01 -0000	1.28
@@ -39,7 +39,7 @@
 
 void do_connect(connection_attributes *attrs)
 {
-	address *remote, *local;
+	const address *remote, *local;
 	int err, fd = -1;
 	struct addrinfo hints, *res = NULL, *ptr;
 	bool connect_attempted = FALSE;
@@ -55,8 +55,8 @@
 	assert(attrs != NULL);
 	
 	/* setup the addresses of the two connection endpoints */
-	remote = &(attrs->remote_address);
-	local  = &(attrs->local_address);
+	remote = ca_remote_address(attrs);
+	local  = ca_local_address(attrs);
 
 	/* make sure all the preconditions are respected */
 	assert(remote->address != NULL && strlen(remote->address) > 0);
@@ -71,7 +71,7 @@
 	
 	/* setup hints structure to be passed to getaddrinfo */
 	memset(&hints, 0, sizeof(hints));
-	connection_attributes_to_addrinfo(&hints, attrs);
+	ca_to_addrinfo(&hints, attrs);
 
 #ifdef HAVE_GETADDRINFO_AI_ADDRCONFIG
 	hints.ai_flags |= AI_ADDRCONFIG;
@@ -233,8 +233,8 @@
 		}
 
 		/* set connect timeout */
-		if (attrs->connect_timeout > 0) {
-			tv.tv_sec = (time_t)attrs->connect_timeout;
+		if (ca_connect_timeout(attrs) > 0) {
+			tv.tv_sec = (time_t)ca_connect_timeout(attrs);
 			tv.tv_usec = 0;
 			tvp = &tv;
 		}
@@ -321,7 +321,7 @@
 	}
 
 	/* fill out the remote io_stream */
-	ios_assign_socket(&(attrs->remote_stream), fd, ptr->ai_socktype);
+	ios_assign_socket(ca_remote_stream(attrs), fd, ptr->ai_socktype);
 
 	/* cleanup addrinfo structure */
 	freeaddrinfo(res);
@@ -331,7 +331,7 @@
 
 void do_listen(connection_attributes *attrs)
 {
-	address *remote, *local;
+	const address *remote, *local;
 	int nfd, i, fd, err, ns = -1, socktype = -1, maxfd = -1;
 	struct addrinfo hints, *res = NULL, *ptr;
 	char hbuf_num[NI_MAXHOST + 1];
@@ -354,8 +354,8 @@
 	assert(attrs != NULL);
 	
 	/* setup the addresses of the two connection endpoints */
-	remote = &(attrs->remote_address);
-	local  = &(attrs->local_address);
+	remote = ca_remote_address(attrs);
+	local  = ca_local_address(attrs);
 
 	/* make sure all the preconditions are respected */
 	assert(local->address == NULL || strlen(local->address) > 0);
@@ -374,7 +374,7 @@
 	
 	/* setup hints structure to be passed to getaddrinfo */
 	memset(&hints, 0, sizeof(hints));
-	connection_attributes_to_addrinfo(&hints, attrs);
+	ca_to_addrinfo(&hints, attrs);
 
 	hints.ai_flags = AI_PASSIVE;
 	if (numeric_mode == TRUE)
@@ -566,8 +566,8 @@
 		memcpy(&tmp_ap_fdset, &accept_fdset, sizeof(fd_set));
 
 		/* setup timeout */
-		if (attrs->connect_timeout > 0) {
-			tv.tv_sec = (time_t)attrs->connect_timeout;
+		if (ca_connect_timeout(attrs) > 0) {
+			tv.tv_sec = (time_t)ca_connect_timeout(attrs);
 			tv.tv_usec = 0;
 			tvp = &tv;
 		}
@@ -720,5 +720,5 @@
 	destroy_bound_sockets(bound_sockets);
 
[...4 lines suppressed...]



More information about the ds6-devel mailing list