IPv6SocketAddress.st
author Claus Gittinger <cg@exept.de>
Tue, 26 Feb 2008 11:29:14 +0100
changeset 1930 935b2870be2e
parent 1559 609a0707ab22
child 2568 4b0180395472
permissions -rw-r--r--
arrow points reusable (class protocol)

"
 COPYRIGHT (c) 1999 by eXept Software AG
              All Rights Reserved

 This software is furnished under a license and may be used
 only in accordance with the terms of that license and with the
 inclusion of the above copyright notice.   This software may not
 be provided or otherwise made available to, or used by, any
 other person.  No title to or ownership of the software is
 hereby transferred.
"



"{ Package: 'stx:libbasic2' }"

IPSocketAddress variableByteSubclass:#IPv6SocketAddress
	instanceVariableNames:''
	classVariableNames:''
	poolDictionaries:''
	category:'OS-Sockets'
!

!IPv6SocketAddress class methodsFor:'documentation'!

copyright
"
 COPYRIGHT (c) 1999 by eXept Software AG
              All Rights Reserved

 This software is furnished under a license and may be used
 only in accordance with the terms of that license and with the
 inclusion of the above copyright notice.   This software may not
 be provided or otherwise made available to, or used by, any
 other person.  No title to or ownership of the software is
 hereby transferred.
"


!

documentation
"
    Instances of IPv6SocketAddress represent v6 IP socket addresses.
    These consist of a 16byte hostId and a port number.

    Contains
        2 byte domain AF_INET6  
        2 byte port
        4 byte flowInfo
        16 byte address
        4 byte scope

    [author:]
        Claus Gittinger (cg@exept)

    [see also:]

    [instance variables:]

    [class variables:]
"
! !

!IPv6SocketAddress class methodsFor:'addressing'!

anyAddress
    "return the anonymous addresses bytes"

    ^ #[0 0 0 0  0 0 0 0  0 0 0 0  0 0 0 0]
!

local
    "return the addresses bytes addressing the local host"

    ^ #[0 0 0 0  0 0 0 0  0 0 0 0  0 0 0 1]
! !

!IPv6SocketAddress class methodsFor:'conversion'!

hostAddressFromString:aString 
    "convert an address given in a dot notation like 1:2:3:4:5:6:7:8 or ::1 or even ::"
    
    |components words bytes prevWord bytesToGenerate i|

    components := aString asCollectionOfSubstringsSeparatedBy:$:.
    components size > 8 ifTrue:[
        ^ NameLookupError raiseRequestWith:aString
            errorString:' - bad address string'.
    ].

    (components last occurrencesOf:$.) == 3 ifTrue:[
        "IPV4 address embedded"
        bytes := IPSocketAddress hostAddressFromString:components removeLast.
        components add:(((bytes at:1) bitShift:8) bitOr:(bytes at:2)).
        components add:(((bytes at:3) bitShift:8) bitOr:(bytes at:4)) .
    ].
    words := components 
                collect:[:eachComponent | 
                    eachComponent size == 0 ifTrue:[
                        eachComponent isInteger ifTrue:[eachComponent] ifFalse:[nil].
                    ] ifFalse:[
                        Integer 
                            readFromString:eachComponent
                            radix:16
                            onError:[
                                ^ NameLookupError raiseRequestWith:aString
                                    errorString:' - bad address string'
                            ].
                    ].
                ].

    bytes := ByteArray new:16.
    bytesToGenerate := 2 * (8 - words size).
    i := 1.
    words do:[:eachWord|
        eachWord == nil ifTrue:[
            i ~~ 1 ifTrue:[
                i := i + bytesToGenerate.
                bytesToGenerate := 0.
            ].
        ] ifFalse:[
            bytes at:i put:(eachWord digitByteAt:2).
            bytes at:i+1 put:(eachWord digitByteAt:1).
        ].
        i := i + 2.
        prevWord := eachWord.
    ].

    ^ bytes.

    "
        IPv6SocketAddress hostAddressFromString:'::'
        IPv6SocketAddress hostAddressFromString:'::1'
        IPv6SocketAddress hostAddressFromString:':1:2'
        IPv6SocketAddress hostAddressFromString:'1::2'
        IPv6SocketAddress hostAddressFromString:'1:2:3:4:5:6:7:8'
        IPv6SocketAddress hostAddressFromString:'1234:5678:9abc:def1:2345:6789:abcd:ef12'
        IPv6SocketAddress hostAddressFromString:'a:b:c:d:e:f:7:8'
        IPv6SocketAddress hostAddressFromString:'1::2:3:4'
        IPv6SocketAddress hostAddressFromString:'1:2:3::4'
        IPv6SocketAddress hostAddressFromString:'::1.2.3.4'
    "
! !

!IPv6SocketAddress class methodsFor:'queries'!

domainSymbol

    ^ #'AF_INET6'
!

obsoleteDomainSymbol
    ^ #inet6
! !

!IPv6SocketAddress methodsFor:'accessing'!

hostAddress
"/    struct sockaddr_in6 {
"/            unsigned short int      sin6_family;    /* AF_INET6 */
"/            __u16                   sin6_port;      /* Transport layer port # */
"/            __u32                   sin6_flowinfo;  /* IPv6 flow information */
"/            struct in6_addr         sin6_addr;      /* IPv6 address */
"/            __u32                   sin6_scope_id;  /* scope id (new in RFC2553) */
"/    };
    ^ (ByteArray new:16) replaceFrom:1 to:16 with:self startingAt:9
!

hostAddress:aByteArray
    ^ self replaceFrom:9 to:9+16-1 with:aByteArray startingAt:1
! !

!IPv6SocketAddress methodsFor:'printing & storing'!

printAddressOn:aStream
    |i1 i2 colons|

    colons := 0.
    i1 := self adrBytesStart.
    i2 := i1 + self numAdrBytes - 1.
    i1 to:i2 by:2 do:[:i | 
        |word|

        word := self wordAt:i MSB:true.
        word == 0 ifTrue:[
            (colons <= 1 and:[i ~~ (i2-1)]) ifTrue:[
                colons := colons + 1.
                aStream nextPut:$:
            ]
        ] ifFalse:[
            word printOn:aStream base:16.
            i ~~ (i2-1) ifTrue:[
                aStream nextPut:$:.
                colons >= 2 ifTrue:[
                    colons := -20.       "no more $: may be omitted"
                ] ifFalse:[
                    colons positive ifTrue:[colons := 1].
                ]
            ].
        ].
    ].

    "
       String streamContents:[:s | self localHost printAddressOn:s]
       String streamContents:[:s |(self hostAddress:#[0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0]) printAddressOn:s]
       String streamContents:[:s |(self hostAddress:#[1 2 3 4 5 6 7 8 0 0 0 0 0 0 0 0]) printAddressOn:s]
       String streamContents:[:s |(self hostAddress:#[0 0 0 0 0 0 0 0 1 2 3 4 5 6 7 8]) printAddressOn:s]
       String streamContents:[:s |(self hostAddress:#[0 0 0 0 1 2 3 4 5 6 7 8 0 0 0 0]) printAddressOn:s]
       String streamContents:[:s |(self hostAddress:#[16r1a 16r1b 0 0 0 0 0 0  0 0 0 0 0 0 0 0]) printAddressOn:s]
    "
! !

!IPv6SocketAddress methodsFor:'private'!

adrBytesStart
    ^ 9
!

numAdrBytes
    ^ 16
! !

!IPv6SocketAddress methodsFor:'testing'!

isLocal
    "answer true, if this address adresses a peer on the same host"

    ^ self hostAddress = self class local
! !

!IPv6SocketAddress class methodsFor:'documentation'!

version
    ^ '$Header: /cvs/stx/stx/libbasic2/IPv6SocketAddress.st,v 1.13 2005-06-28 13:10:47 stefan Exp $'
! !