[PATCH] Socket: Store address family in file structure

Carl Worth cworth at cworth.org
Sat Feb 2 02:31:26 PST 2008


This is more straightforward than using getsockname to read the
address family back out from the kernel.
---
 builtin-sockets.c |   19 +++++++++----------
 file.c            |    1 +
 test/README       |    2 +-
 value.h           |    1 +
 4 files changed, 12 insertions(+), 11 deletions(-)

diff --git a/builtin-sockets.c b/builtin-sockets.c
index 8ff1879..3ad61e4 100644
--- a/builtin-sockets.c
+++ b/builtin-sockets.c
@@ -163,6 +163,7 @@ do_Socket_create (int num, Value *args)
 {
     ENTER ();
     int ifamily, itype, type_index, s;
+    Value ret;

     if (num == 0 || num > 2) {
 	RaiseStandardException (exception_invalid_argument,
@@ -184,7 +185,9 @@ do_Socket_create (int num, Value *args)
     s = socket (ifamily, itype, 0);
     if (s == -1)
 	RETURN (Void);
-    RETURN (FileCreate (s, FileReadable|FileWritable));
+    ret = FileCreate (s, FileReadable|FileWritable);
+    ret->file.sock_family = ifamily;
+    RETURN (ret);
 }

 typedef union {
@@ -197,7 +200,7 @@ typedef union {
     } align;
 } sockaddr_all_t;

-static Bool address_lookup (int s, Value hostname, Value portname,
+static Bool address_lookup (Value s, Value hostname, Value portname,
 			    sockaddr_all_t *addr, socklen_t *len)
 {
     struct hostent *host;
@@ -217,12 +220,7 @@ static Bool address_lookup (int s, Value hostname, Value portname,
     if (*hostchars == '\0' || *portchars == '\0')
 	return False; /* FIXME: more here? */

-    /* Read the address family back from the kernel. */
-    *len = sizeof (*addr);
-    if (getsockname (s, &addr->addr, len) < 0)
-	return False;
-
-    switch (addr->addr.sa_family) {
+    switch (s->file.sock_family) {
     case AF_UNIX:
 	if (strlen (portchars) > PATH_MAX)
 	    return False;
@@ -237,6 +235,7 @@ static Bool address_lookup (int s, Value hostname, Value portname,
 	    herror ("address_lookup");
 	    return False; /* FIXME: more here? */
 	}
+	*len = sizeof (addr->in);
 	memcpy (&addr->in.sin_addr.s_addr, host->h_addr_list[0], sizeof addr->in.sin_addr.s_addr);

 	/* port lookup */
@@ -275,7 +274,7 @@ do_Socket_connect (Value s, Value host, Value port)
     sockaddr_all_t addr;
     socklen_t len;

-    if (!address_lookup (s->file.fd, host, port, &addr, &len))
+    if (!address_lookup (s, host, port, &addr, &len))
 	RETURN (Void);

     if (!running->thread.partial)
@@ -330,7 +329,7 @@ do_Socket_bind (Value s, Value host, Value port)
     sockaddr_all_t addr;
     socklen_t len;

-    if (!address_lookup (s->file.fd, host, port, &addr, &len))
+    if (!address_lookup (s, host, port, &addr, &len))
 	RETURN (Void);

 #ifdef SO_REUSEADDR
diff --git a/file.c b/file.c
index 0628cfa..64b7c41 100644
--- a/file.c
+++ b/file.c
@@ -653,6 +653,7 @@ FileCreate (int fd, int flags)
 	file->file.flags |= FileIsPipe;
     if (fd >= 3)
 	FileSetFd (fd);
+    file->file.sock_family = 0;
     RETURN (file);
 }

diff --git a/test/README b/test/README
index 214487d..44644c9 100644
--- a/test/README
+++ b/test/README
@@ -1,4 +1,4 @@
 These files are used to make sure that everything in Nickle is working as it should be, or at least how we hope it should work.  Each file tests specific items within Nickle such as order of operations.

 optest.5c - tests the operators to make sure they are calculating properly
-orderofoptest.5c - tests to make sure that Nickle evaluates in the correct order
+orderofoptest.5c - tests to make sure that Nickle evaluates in the correct order
\ No newline at end of file
diff --git a/value.h b/value.h
index fbebfe8..c55d89e 100644
--- a/value.h
+++ b/value.h
@@ -645,6 +645,7 @@ typedef struct _file {
     int		    error;
     FileChainPtr    input;
     FileChainPtr    output;
+    int		    sock_family;
 } File;

 #define FileBufferSize	4096
--
1.5.3.2




More information about the Nickle mailing list