class: IPv6SocketAddress
authorStefan Vogel <sv@exept.de>
Fri, 27 Feb 2015 22:09:28 +0100
changeset 3511 f23e77f37681
parent 3510 51b43eca349f
child 3512 b6feb770d2b8
class: IPv6SocketAddress added: #asIPv4SocketAddressIfPossible #hasSameHostAddress:
IPv6SocketAddress.st
--- a/IPv6SocketAddress.st	Fri Feb 27 22:09:14 2015 +0100
+++ b/IPv6SocketAddress.st	Fri Feb 27 22:09:28 2015 +0100
@@ -190,6 +190,22 @@
     ^ self unsignedLongAt:5 bigEndian:false.
 !
 
+hasSameHostAddress:aSocketAddress
+    "answer true, if myself and aSocketAddress have the same host address
+     (but possibly different ports)."
+
+"/    struct sockaddr_in6 {
+"/  0           unsigned short int      sin6_family;    /* AF_INET6 */
+"/  2           __u16                   sin6_port;      /* Transport layer port # */
+"/  4           __u32                   sin6_flowinfo;  /* IPv6 flow information */
+"/  8           struct in6_addr         sin6_addr;      /* IPv6 address */
+"/ 24            __u32                   sin6_scope_id;  /* scope id (new in RFC2553) */
+"/ 28   };
+
+    ^ aSocketAddress class == self class 
+        and:[self sameContentsFrom:9 to:25 as:aSocketAddress startingAt:9]
+!
+
 hostAddress
 "/    struct sockaddr_in6 {
 "/  0           unsigned short int      sin6_family;    /* AF_INET6 */
@@ -233,6 +249,22 @@
     "
 !
 
+asIPv4SocketAddressIfPossible
+    "try to convert to an IPv4 socket address (works when a IPv6 adreess is a mapped IPv4 address). 
+     Answer myself, if the conversion is not possible."
+
+    self isMappedIPv4 ifFalse:[
+        ^ self.
+    ].
+
+    ^ IPSocketAddress hostAddress:(self copyFrom:21 to:24) port:self port.
+
+    "
+        (self addressString:'0:0:0:0:0:ffff::') asIPv4SocketAddressIfPossible
+        (IPSocketAddress localHost port:80) asIPv6SocketAddress asIPv4SocketAddressIfPossible
+    "
+!
+
 asIPv6SocketAddress
     ^ self
 ! !
@@ -373,10 +405,10 @@
 !IPv6SocketAddress class methodsFor:'documentation'!
 
 version
-    ^ '$Header: /cvs/stx/stx/libbasic2/IPv6SocketAddress.st,v 1.19 2015-02-26 10:01:22 stefan Exp $'
+    ^ '$Header: /cvs/stx/stx/libbasic2/IPv6SocketAddress.st,v 1.20 2015-02-27 21:09:28 stefan Exp $'
 !
 
 version_CVS
-    ^ '$Header: /cvs/stx/stx/libbasic2/IPv6SocketAddress.st,v 1.19 2015-02-26 10:01:22 stefan Exp $'
+    ^ '$Header: /cvs/stx/stx/libbasic2/IPv6SocketAddress.st,v 1.20 2015-02-27 21:09:28 stefan Exp $'
 ! !