develooper Front page | perl.perl5.porters | Postings from August 2009

[PATCH] Add support for Abstract namespace sockets

Thread Next
From:
Lubomir Rintel
Date:
August 26, 2009 01:32
Subject:
[PATCH] Add support for Abstract namespace sockets
Message ID:
1251212993-2784-1-git-send-email-lkundrak@v3.sk
Abstract namespace sockets are Linux-specific socket type that live in
AF_UNIX family, slightly abusing it to be able to use arbitrary
character arrays as addresses: They start with nul byte and are not
terminated by nul byte, but with the length passed to the socket()
system call.

Added regression test for the correct address length computation.

Signed-off-by: Lubomir Rintel <lkundrak@fedoraproject.org>
---
 ext/Socket/Socket.xs  |   13 ++++++++++++-
 ext/Socket/t/Socket.t |   12 +++++++++++-
 2 files changed, 23 insertions(+), 2 deletions(-)

diff --git a/ext/Socket/Socket.xs b/ext/Socket/Socket.xs
index 076297f..5d36bbd 100644
--- a/ext/Socket/Socket.xs
+++ b/ext/Socket/Socket.xs
@@ -303,6 +303,7 @@ pack_sockaddr_un(pathname)
 	struct sockaddr_un sun_ad; /* fear using sun */
 	STRLEN len;
 	char * pathname_pv;
+	int addr_len;
 
 	Zero( &sun_ad, sizeof sun_ad, char );
 	sun_ad.sun_family = AF_UNIX;
@@ -336,7 +337,17 @@ pack_sockaddr_un(pathname)
 	Copy( pathname_pv, sun_ad.sun_path, len, char );
 #  endif
 	if (0) not_here("dummy");
-	ST(0) = sv_2mortal(newSVpvn((char *)&sun_ad, sizeof sun_ad));
+	if (len > 1 && sun_ad.sun_path[0] == '\0') {
+		/* Linux-style abstract-namespace socket.
+		 * The name is not a file name, but an array of arbitrary
+		 * character, starting with \0 and possibly including \0s,
+		 * therefore the length of the structure must denote the
+		 * end of that character array */
+		addr_len = (void *)&sun_ad.sun_path - (void *)&sun_ad + len;
+	} else {
+		addr_len = sizeof sun_ad;
+	}
+	ST(0) = sv_2mortal(newSVpvn((char *)&sun_ad, addr_len));
 #else
 	ST(0) = (SV *) not_here("pack_sockaddr_un");
 #endif
diff --git a/ext/Socket/t/Socket.t b/ext/Socket/t/Socket.t
index f707999..b8f014b 100755
--- a/ext/Socket/t/Socket.t
+++ b/ext/Socket/t/Socket.t
@@ -14,7 +14,7 @@ BEGIN {
 	
 use Socket qw(:all);
 
-print "1..17\n";
+print "1..18\n";
 
 $has_echo = $^O ne 'MSWin32';
 $alarmed = 0;
@@ -163,7 +163,17 @@ if ($^O eq 'linux') {
 	print "# got <$path>\n";
         print "not ok 17\n";
     }
+
+    # see if we calculate the address structure length correctly
+    if (length ($test_abstract_socket) + 2 == length $addr) {
+        print "ok 18\n";
+    } else {
+	print "# got ".(length $addr)."\n";
+        print "not ok 18\n";
+    }
+
 } else {
     # doesn't have abstract socket support
     print "ok 17 - skipped on this platform\n";
+    print "ok 18 - skipped on this platform\n";
 }
-- 
1.6.2.5


Thread Next


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About