SocketAddress.st
author Stefan Vogel <sv@exept.de>
Thu, 27 Mar 2003 14:30:01 +0100
changeset 1164 41a2750af1fa
parent 818 2f8331ad12d4
child 1167 56c172bc5cfe
permissions -rw-r--r--
New socket support

"
 COPYRIGHT (c) 1995 by Claus Gittinger
	      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' }"

UninterpretedBytes variableByteSubclass:#SocketAddress
	instanceVariableNames:''
	classVariableNames:''
	poolDictionaries:''
	category:'OS-Sockets'
!

!SocketAddress class methodsFor:'documentation'!

copyright
"
 COPYRIGHT (c) 1995 by Claus Gittinger
	      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
"
    Abstract superclass for subclasses implementing various IPC addressing schemes.
    See concrete examples IPSocketAddress and UDSocketAddress.

    ST-80 compatibility class.
    This may be required when existing code has to be ported to ST/X;
    however, it may not be complete and more protocol may be added in the future.
    The code here was created when public domain code (Manchester) had to
    be ported to ST/X and missing classes/methods were encountered, and code added
    by reasoning 'what the original class could probably do there'.

    This is an additional goody class; therefore:

    THIS SOFTWARE IS PROVIDED BY THE CONTRIBUTOR ``AS IS'' AND
    ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
    IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
    ARE DISCLAIMED.  IN NO EVENT SHALL THE CONTRIBUTOR BE LIABLE
    FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
    DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
    OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
    HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
    LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
    OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
    SUCH DAMAGE.
"
! !

!SocketAddress class methodsFor:'instance creation'!

hostAddress:addr port:portNr
    ^ self new hostAddress:addr port:portNr
!

hostName:name
    ^ self hostName:name serviceName:nil type:nil
!

hostName:name port:portNr
    ^ self hostName:name serviceName:portNr type:nil
!

hostName:name serviceName:portNrOrName type:socketTypeSymbol

    |addressInfo serviceName port sa|

    portNrOrName isString ifTrue:[
        serviceName := portNrOrName
    ] ifFalse:[
        port := portNrOrName.
    ].

    addressInfo := self getAddressInfo:name serviceName:serviceName 
                 domain:self domainSymbol type:socketTypeSymbol
                 protocol:nil flags:0.

    sa := addressInfo first socketAddress.
    addressInfo size > 1 ifTrue:[
        (addressInfo contains:[:entry| entry socketAddress ~= sa]) ifTrue:[
            self error:'too many results'
        ].
    ].
    port notNil ifTrue:[
        sa port:port.
    ].
    ^ sa

    "
     SocketAddress hostName:'localhost' serviceName:10 type:#stream
     IPSocketAddress hostName:'localhost' serviceName:'echo' type:#datagram 
     IPSocketAddress hostName:'localhost' serviceName:'echo' type:nil
    "
!

new
    ^ (self new:self socketAddressSize) domainCode:self domainCode

    "
     IPSocketAddress new
     IPv6SocketAddress new
     UDSocketAddress new
     AppletalkSocketAddress new
   "
!

newDomain:domain
   "answer an new socket address for a given domain"

   ^ (self knownClassFromCode:domain) new

   "
    self newDomain:#unix
    self newDomain:#inet
   "
! !

!SocketAddress class methodsFor:'queries'!

domainCode
    "answer the numerical domain code used in socket addresses"

    ^ OperatingSystem socketAccessor domainCodeOf:self domainSymbol
!

domainCodeFromName:aNameSymbol
    "this is a compatibility method;
     VW returns the internal unix codes here - however, in ST/X,
     symbols are returned, which are translated later (in Socket)"

    aNameSymbol == #afUnix      ifTrue:[^ #unix].
    aNameSymbol == #afInet      ifTrue:[^ #inet].    
    aNameSymbol == #afIpV6      ifTrue:[^ #inet6].    
    aNameSymbol == #afAppletalk ifTrue:[^ #appletalk].    
    aNameSymbol == #afDecnet    ifTrue:[^ #DECnet].    
    aNameSymbol == #afSna       ifTrue:[^ #sna].    
    aNameSymbol == #afNs        ifTrue:[^ #xns].    
    aNameSymbol == #afCcitt     ifTrue:[^ #ccitt].    

    "/
    "/ could someone tell me which symbols are used in ST-80's SocketAddress class ?
    "/
    self error:'no more mimicri implemented yet ...'

    "Modified: / 9.1.1998 / 10:03:56 / stefan"
!

domainNameFromCode:code
    "this is a compatibility method;
     VW expects the internal unix codes here - however, in ST/X,
     symbols are expected - keeping the numeric values secret (in Socket)"

    code == #unix      ifTrue:[^ #afUnix].
    code == #inet      ifTrue:[^ #afInet].
    code == #inet6     ifTrue:[^ #afIpV6].
    code == #appletalk ifTrue:[^ #afAppletalk].
    code == #DECnet    ifTrue:[^ #afDecnet].
    code == #sna       ifTrue:[^ #afSna].
    code == #xns       ifTrue:[^ #afNs].
    code == #ccitt     ifTrue:[^ #afCcitt].

    "/
    "/ could someone tell me which symbols are used in ST-80's SocketAddress class ?
    "/
    self error:'no more mimicri implemented yet ...'

!

domainSymbol
    "answer the domain symbol. Subclasses redefine this"

    self == SocketAddress ifTrue:[
        ^ nil
    ] ifFalse:[
        self subclassResponsibility
    ]
!

getAddressInfo:hostName serviceName:serviceName domain:domainArg type:typeArg protocol:protoArg flags:flags 
    "answer an Array of socket addresses for serviceName on hostName
     Domain, type, protocol may be nil or specify a hint for the socket 
     addresses to be returned."

    ^ OperatingSystem socketAccessor 
            getAddressInfo:hostName serviceName:serviceName 
            domain:domainArg type:typeArg protocol:protoArg flags:flags 

    "
     self getAddressInfo:'localhost' serviceName:nil 
            domain:nil type:nil protocol:nil flags:nil
     self getAddressInfo:'localhost' serviceName:nil 
            domain:#inet type:#stream protocol:nil flags:nil
     self getAddressInfo:'localhost' serviceName:nil 
            domain:#inet type:#stream protocol:#tcp flags:nil
     self getAddressInfo:'blurb.exept.de' serviceName:nil 
            domain:#inet type:nil protocol:nil flags:nil
     self getAddressInfo:'1.2.3.4' serviceName:'6' 
            domain:#inet type:nil protocol:nil flags:nil
     self getAddressInfo:'localhost' serviceName:'echo' 
            domain:#inet6 type:nil protocol:nil flags:nil
    "
!

getNameInfo:socketAddress wantHostName:wantHostName wantServiceName:wantServiceName datagram:useDatagram flags:flags 
    "answer an Array containing the hostName and serviceName
     in socketAddress. SocketType may be one "

    ^ OperatingSystem socketAccessor
            getNameInfo:socketAddress wantHostName:wantHostName 
            wantServiceName:wantServiceName datagram:useDatagram flags:flags 
    
    "
     self getNameInfo:
         (self getAddressInfo:'localhost' serviceName:'echo' 
                domain:#inet type:#stream protocol:nil flags:nil) first socketAddress
         wantHostName:true wantServiceName:true datagram:false flags:0
    "
!

knownClassFromCode:code
    "this is a compatibility method;
     VW expects the internal unix codes here - however, in ST/X,
     symbols are expected - keeping the numeric values secret (in Socket)"

    code isInteger ifTrue:[
        self allSubclassesDo:[:cls|
            cls domainCode == code ifTrue:[
                ^ cls
            ].
        ].
    ] ifFalse:[
        self allSubclassesDo:[:cls|
            cls domainSymbol == code ifTrue:[
                ^ cls
            ].
        ].
    ].
    ^ SocketAddress

    "
     self knownClassFromCode:#unix
     self knownClassFromCode:#inet
     self knownClassFromCode:2
    "
!

socketAddressSize
    "answer the OS specific size of a socket address"

    ^ self subclassResponsibility
! !

!SocketAddress methodsFor:'accessing'!

domainCode

    ^ self unsignedShortAt:1
!

domainCode:anInteger

    self unsignedShortAt:1 put:anInteger
!

hostAddress:anAddress
    "generic method, subclasses usually redefine this"

    self replaceBytesFrom:3 to:anAddress size+2 with:anAddress startingAt:1
!

hostAddress:addr port:portNr
    self hostAddress:addr.
    self port:portNr

    "
     IPSocketAddress hostAddress:#[193 141 12 193] port:10
    "

! !

!SocketAddress methodsFor:'printing & storing'!

displayString
    "redefine form Collection"

    |s|

    s := WriteStream on:''.
    self printOn:s.
    ^ s contents.
! !

!SocketAddress methodsFor:'private'!

fromBytes:aByteArray
    "Copy the internal representation of a SocketAddress to myself

     This is an internal interface!!"

    self replaceBytesFrom:1 to:aByteArray size 
         with:aByteArray startingAt:1.
! !

!SocketAddress methodsFor:'queries'!

address
    ^ self subclassResponsibility
!

hostName
    ^ (self class getNameInfo:self wantHostName:true 
                  wantServiceName:false datagram:false flags:0) first

    "
     (IPSocketAddress hostAddress:#[127 0 0 1] port:7) hostName
     (IPSocketAddress hostAddress:#[193 141 12 193] port:10) hostName
    "
!

portOrName
    ^ self subclassResponsibility
!

serviceName
    ^ (self class getNameInfo:self wantHostName:false 
                  wantServiceName:true datagram:false flags:0) second

    "
     (IPSocketAddress hostAddress:#[127 0 0 1] port:7) serviceName
     (IPSocketAddress hostAddress:#[193 141 12 193] port:10) serviceName
    "
! !

!SocketAddress class methodsFor:'documentation'!

version
    ^ '$Header: /cvs/stx/stx/libbasic2/SocketAddress.st,v 1.10 2003-03-27 13:30:01 stefan Exp $'
! !