100
|
1 |
"
|
|
2 |
COPYRIGHT (c) 1995 by Claus Gittinger
|
|
3 |
All Rights Reserved
|
|
4 |
|
|
5 |
This software is furnished under a license and may be used
|
|
6 |
only in accordance with the terms of that license and with the
|
|
7 |
inclusion of the above copyright notice. This software may not
|
|
8 |
be provided or otherwise made available to, or used by, any
|
|
9 |
other person. No title to or ownership of the software is
|
|
10 |
hereby transferred.
|
|
11 |
"
|
|
12 |
|
1164
|
13 |
"{ Package: 'stx:libbasic2' }"
|
|
14 |
|
818
|
15 |
UninterpretedBytes variableByteSubclass:#SocketAddress
|
488
|
16 |
instanceVariableNames:''
|
|
17 |
classVariableNames:''
|
|
18 |
poolDictionaries:''
|
816
|
19 |
category:'OS-Sockets'
|
100
|
20 |
!
|
|
21 |
|
|
22 |
!SocketAddress class methodsFor:'documentation'!
|
|
23 |
|
|
24 |
copyright
|
|
25 |
"
|
|
26 |
COPYRIGHT (c) 1995 by Claus Gittinger
|
|
27 |
All Rights Reserved
|
|
28 |
|
|
29 |
This software is furnished under a license and may be used
|
|
30 |
only in accordance with the terms of that license and with the
|
|
31 |
inclusion of the above copyright notice. This software may not
|
|
32 |
be provided or otherwise made available to, or used by, any
|
|
33 |
other person. No title to or ownership of the software is
|
|
34 |
hereby transferred.
|
|
35 |
"
|
|
36 |
!
|
|
37 |
|
|
38 |
documentation
|
|
39 |
"
|
111
|
40 |
Abstract superclass for subclasses implementing various IPC addressing schemes.
|
|
41 |
See concrete examples IPSocketAddress and UDSocketAddress.
|
|
42 |
|
100
|
43 |
ST-80 compatibility class.
|
|
44 |
This may be required when existing code has to be ported to ST/X;
|
|
45 |
however, it may not be complete and more protocol may be added in the future.
|
|
46 |
The code here was created when public domain code (Manchester) had to
|
|
47 |
be ported to ST/X and missing classes/methods were encountered, and code added
|
|
48 |
by reasoning 'what the original class could probably do there'.
|
|
49 |
|
|
50 |
This is an additional goody class; therefore:
|
|
51 |
|
|
52 |
THIS SOFTWARE IS PROVIDED BY THE CONTRIBUTOR ``AS IS'' AND
|
|
53 |
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
54 |
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
55 |
ARE DISCLAIMED. IN NO EVENT SHALL THE CONTRIBUTOR BE LIABLE
|
|
56 |
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
57 |
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
|
58 |
OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
|
59 |
HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
|
60 |
LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
|
61 |
OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
|
62 |
SUCH DAMAGE.
|
|
63 |
"
|
|
64 |
! !
|
|
65 |
|
816
|
66 |
!SocketAddress class methodsFor:'instance creation'!
|
|
67 |
|
|
68 |
hostAddress:addr port:portNr
|
818
|
69 |
^ self new hostAddress:addr port:portNr
|
816
|
70 |
!
|
|
71 |
|
1164
|
72 |
hostName:name
|
|
73 |
^ self hostName:name serviceName:nil type:nil
|
|
74 |
!
|
|
75 |
|
816
|
76 |
hostName:name port:portNr
|
1164
|
77 |
^ self hostName:name serviceName:portNr type:nil
|
|
78 |
!
|
|
79 |
|
|
80 |
hostName:name serviceName:portNrOrName type:socketTypeSymbol
|
|
81 |
|
|
82 |
|addressInfo serviceName port sa|
|
|
83 |
|
|
84 |
portNrOrName isString ifTrue:[
|
|
85 |
serviceName := portNrOrName
|
|
86 |
] ifFalse:[
|
|
87 |
port := portNrOrName.
|
|
88 |
].
|
|
89 |
|
|
90 |
addressInfo := self getAddressInfo:name serviceName:serviceName
|
|
91 |
domain:self domainSymbol type:socketTypeSymbol
|
|
92 |
protocol:nil flags:0.
|
|
93 |
|
|
94 |
sa := addressInfo first socketAddress.
|
|
95 |
addressInfo size > 1 ifTrue:[
|
|
96 |
(addressInfo contains:[:entry| entry socketAddress ~= sa]) ifTrue:[
|
|
97 |
self error:'too many results'
|
|
98 |
].
|
|
99 |
].
|
|
100 |
port notNil ifTrue:[
|
|
101 |
sa port:port.
|
|
102 |
].
|
|
103 |
^ sa
|
|
104 |
|
|
105 |
"
|
|
106 |
SocketAddress hostName:'localhost' serviceName:10 type:#stream
|
|
107 |
IPSocketAddress hostName:'localhost' serviceName:'echo' type:#datagram
|
|
108 |
IPSocketAddress hostName:'localhost' serviceName:'echo' type:nil
|
|
109 |
"
|
818
|
110 |
!
|
|
111 |
|
|
112 |
new
|
1164
|
113 |
^ (self new:self socketAddressSize) domainCode:self domainCode
|
|
114 |
|
|
115 |
"
|
|
116 |
IPSocketAddress new
|
|
117 |
IPv6SocketAddress new
|
|
118 |
UDSocketAddress new
|
|
119 |
AppletalkSocketAddress new
|
|
120 |
"
|
|
121 |
!
|
818
|
122 |
|
1164
|
123 |
newDomain:domain
|
|
124 |
"answer an new socket address for a given domain"
|
|
125 |
|
|
126 |
^ (self knownClassFromCode:domain) new
|
|
127 |
|
|
128 |
"
|
|
129 |
self newDomain:#unix
|
|
130 |
self newDomain:#inet
|
|
131 |
"
|
816
|
132 |
! !
|
|
133 |
|
100
|
134 |
!SocketAddress class methodsFor:'queries'!
|
|
135 |
|
1164
|
136 |
domainCode
|
|
137 |
"answer the numerical domain code used in socket addresses"
|
|
138 |
|
|
139 |
^ OperatingSystem socketAccessor domainCodeOf:self domainSymbol
|
|
140 |
!
|
|
141 |
|
100
|
142 |
domainCodeFromName:aNameSymbol
|
816
|
143 |
"this is a compatibility method;
|
|
144 |
VW returns the internal unix codes here - however, in ST/X,
|
|
145 |
symbols are returned, which are translated later (in Socket)"
|
|
146 |
|
|
147 |
aNameSymbol == #afUnix ifTrue:[^ #unix].
|
|
148 |
aNameSymbol == #afInet ifTrue:[^ #inet].
|
|
149 |
aNameSymbol == #afIpV6 ifTrue:[^ #inet6].
|
|
150 |
aNameSymbol == #afAppletalk ifTrue:[^ #appletalk].
|
|
151 |
aNameSymbol == #afDecnet ifTrue:[^ #DECnet].
|
|
152 |
aNameSymbol == #afSna ifTrue:[^ #sna].
|
|
153 |
aNameSymbol == #afNs ifTrue:[^ #xns].
|
|
154 |
aNameSymbol == #afCcitt ifTrue:[^ #ccitt].
|
|
155 |
|
100
|
156 |
"/
|
|
157 |
"/ could someone tell me which symbols are used in ST-80's SocketAddress class ?
|
|
158 |
"/
|
|
159 |
self error:'no more mimicri implemented yet ...'
|
602
|
160 |
|
|
161 |
"Modified: / 9.1.1998 / 10:03:56 / stefan"
|
816
|
162 |
!
|
|
163 |
|
|
164 |
domainNameFromCode:code
|
|
165 |
"this is a compatibility method;
|
|
166 |
VW expects the internal unix codes here - however, in ST/X,
|
|
167 |
symbols are expected - keeping the numeric values secret (in Socket)"
|
|
168 |
|
|
169 |
code == #unix ifTrue:[^ #afUnix].
|
|
170 |
code == #inet ifTrue:[^ #afInet].
|
|
171 |
code == #inet6 ifTrue:[^ #afIpV6].
|
|
172 |
code == #appletalk ifTrue:[^ #afAppletalk].
|
|
173 |
code == #DECnet ifTrue:[^ #afDecnet].
|
|
174 |
code == #sna ifTrue:[^ #afSna].
|
|
175 |
code == #xns ifTrue:[^ #afNs].
|
|
176 |
code == #ccitt ifTrue:[^ #afCcitt].
|
|
177 |
|
|
178 |
"/
|
|
179 |
"/ could someone tell me which symbols are used in ST-80's SocketAddress class ?
|
|
180 |
"/
|
|
181 |
self error:'no more mimicri implemented yet ...'
|
|
182 |
|
|
183 |
!
|
|
184 |
|
1164
|
185 |
domainSymbol
|
|
186 |
"answer the domain symbol. Subclasses redefine this"
|
|
187 |
|
|
188 |
self == SocketAddress ifTrue:[
|
|
189 |
^ nil
|
|
190 |
] ifFalse:[
|
|
191 |
self subclassResponsibility
|
|
192 |
]
|
|
193 |
!
|
|
194 |
|
|
195 |
getAddressInfo:hostName serviceName:serviceName domain:domainArg type:typeArg protocol:protoArg flags:flags
|
|
196 |
"answer an Array of socket addresses for serviceName on hostName
|
|
197 |
Domain, type, protocol may be nil or specify a hint for the socket
|
|
198 |
addresses to be returned."
|
|
199 |
|
|
200 |
^ OperatingSystem socketAccessor
|
|
201 |
getAddressInfo:hostName serviceName:serviceName
|
|
202 |
domain:domainArg type:typeArg protocol:protoArg flags:flags
|
|
203 |
|
|
204 |
"
|
|
205 |
self getAddressInfo:'localhost' serviceName:nil
|
|
206 |
domain:nil type:nil protocol:nil flags:nil
|
|
207 |
self getAddressInfo:'localhost' serviceName:nil
|
|
208 |
domain:#inet type:#stream protocol:nil flags:nil
|
|
209 |
self getAddressInfo:'localhost' serviceName:nil
|
|
210 |
domain:#inet type:#stream protocol:#tcp flags:nil
|
|
211 |
self getAddressInfo:'blurb.exept.de' serviceName:nil
|
|
212 |
domain:#inet type:nil protocol:nil flags:nil
|
|
213 |
self getAddressInfo:'1.2.3.4' serviceName:'6'
|
|
214 |
domain:#inet type:nil protocol:nil flags:nil
|
|
215 |
self getAddressInfo:'localhost' serviceName:'echo'
|
|
216 |
domain:#inet6 type:nil protocol:nil flags:nil
|
|
217 |
"
|
|
218 |
!
|
|
219 |
|
1167
|
220 |
getAddressInfo:hostName serviceName:serviceName type:typeArg protocol:protoArg flags:flags
|
|
221 |
"answer an Array of socket addresses for serviceName on hostName
|
|
222 |
Domain, type, protocol may be nil or specify a hint for the socket
|
|
223 |
addresses to be returned."
|
|
224 |
|
|
225 |
^ OperatingSystem socketAccessor
|
|
226 |
getAddressInfo:hostName serviceName:serviceName
|
|
227 |
domain:self domainCode type:typeArg protocol:protoArg flags:flags
|
|
228 |
|
|
229 |
"
|
|
230 |
IPSocketAddress getAddressInfo:'localhost' serviceName:nil
|
|
231 |
type:nil protocol:nil flags:nil
|
|
232 |
IPSocketAddress getAddressInfo:'localhost' serviceName:nil
|
|
233 |
type:#stream protocol:nil flags:nil
|
|
234 |
IPSocketAddress getAddressInfo:'localhost' serviceName:nil
|
|
235 |
type:#stream protocol:#tcp flags:nil
|
|
236 |
IPSocketAddress getAddressInfo:'blurb.exept.de' serviceName:nil
|
|
237 |
type:nil protocol:nil flags:nil
|
|
238 |
IPSocketAddress getAddressInfo:'1.2.3.4' serviceName:'6'
|
|
239 |
type:nil protocol:nil flags:nil
|
|
240 |
IPSocketAddress getAddressInfo:'localhost' serviceName:'echo'
|
|
241 |
type:nil protocol:nil flags:nil
|
|
242 |
"
|
|
243 |
!
|
|
244 |
|
1164
|
245 |
getNameInfo:socketAddress wantHostName:wantHostName wantServiceName:wantServiceName datagram:useDatagram flags:flags
|
|
246 |
"answer an Array containing the hostName and serviceName
|
|
247 |
in socketAddress. SocketType may be one "
|
|
248 |
|
|
249 |
^ OperatingSystem socketAccessor
|
|
250 |
getNameInfo:socketAddress wantHostName:wantHostName
|
|
251 |
wantServiceName:wantServiceName datagram:useDatagram flags:flags
|
|
252 |
|
|
253 |
"
|
|
254 |
self getNameInfo:
|
|
255 |
(self getAddressInfo:'localhost' serviceName:'echo'
|
|
256 |
domain:#inet type:#stream protocol:nil flags:nil) first socketAddress
|
|
257 |
wantHostName:true wantServiceName:true datagram:false flags:0
|
|
258 |
"
|
|
259 |
!
|
|
260 |
|
816
|
261 |
knownClassFromCode:code
|
|
262 |
"this is a compatibility method;
|
|
263 |
VW expects the internal unix codes here - however, in ST/X,
|
|
264 |
symbols are expected - keeping the numeric values secret (in Socket)"
|
|
265 |
|
1164
|
266 |
code isInteger ifTrue:[
|
|
267 |
self allSubclassesDo:[:cls|
|
|
268 |
cls domainCode == code ifTrue:[
|
|
269 |
^ cls
|
|
270 |
].
|
|
271 |
].
|
|
272 |
] ifFalse:[
|
|
273 |
self allSubclassesDo:[:cls|
|
|
274 |
cls domainSymbol == code ifTrue:[
|
|
275 |
^ cls
|
|
276 |
].
|
|
277 |
].
|
|
278 |
].
|
|
279 |
^ SocketAddress
|
816
|
280 |
|
1164
|
281 |
"
|
|
282 |
self knownClassFromCode:#unix
|
|
283 |
self knownClassFromCode:#inet
|
|
284 |
self knownClassFromCode:2
|
|
285 |
"
|
|
286 |
!
|
816
|
287 |
|
1164
|
288 |
socketAddressSize
|
|
289 |
"answer the OS specific size of a socket address"
|
|
290 |
|
|
291 |
^ self subclassResponsibility
|
100
|
292 |
! !
|
|
293 |
|
818
|
294 |
!SocketAddress methodsFor:'accessing'!
|
|
295 |
|
1164
|
296 |
domainCode
|
|
297 |
|
|
298 |
^ self unsignedShortAt:1
|
|
299 |
!
|
|
300 |
|
|
301 |
domainCode:anInteger
|
|
302 |
|
|
303 |
self unsignedShortAt:1 put:anInteger
|
|
304 |
!
|
|
305 |
|
1223
|
306 |
hostAddress:addressBytes
|
1164
|
307 |
"generic method, subclasses usually redefine this"
|
818
|
308 |
|
1223
|
309 |
"/ the first 2-bytes (a short) is the domainCode
|
|
310 |
self replaceBytesFrom:1+2 to:addressBytes size+2 with:addressBytes startingAt:1
|
|
311 |
|
|
312 |
"
|
|
313 |
IPSocketAddress hostAddress:#[193 141 12 193] port:80
|
|
314 |
"
|
818
|
315 |
!
|
|
316 |
|
1223
|
317 |
hostAddress:addressBytes port:portNr
|
|
318 |
self hostAddress:addressBytes.
|
818
|
319 |
self port:portNr
|
|
320 |
|
|
321 |
"
|
|
322 |
IPSocketAddress hostAddress:#[193 141 12 193] port:10
|
|
323 |
"
|
1223
|
324 |
!
|
818
|
325 |
|
1223
|
326 |
port
|
|
327 |
self subclassResponsibility
|
|
328 |
!
|
|
329 |
|
|
330 |
port:portNr
|
|
331 |
self subclassResponsibility
|
818
|
332 |
! !
|
|
333 |
|
1164
|
334 |
!SocketAddress methodsFor:'printing & storing'!
|
|
335 |
|
|
336 |
displayString
|
|
337 |
"redefine form Collection"
|
|
338 |
|
|
339 |
|s|
|
|
340 |
|
|
341 |
s := WriteStream on:''.
|
|
342 |
self printOn:s.
|
|
343 |
^ s contents.
|
|
344 |
! !
|
|
345 |
|
|
346 |
!SocketAddress methodsFor:'private'!
|
|
347 |
|
|
348 |
fromBytes:aByteArray
|
|
349 |
"Copy the internal representation of a SocketAddress to myself
|
|
350 |
|
|
351 |
This is an internal interface!!"
|
|
352 |
|
1225
|
353 |
self replaceBytesFrom:1 to:(self size) with:aByteArray startingAt:1.
|
|
354 |
|
|
355 |
|
1164
|
356 |
! !
|
|
357 |
|
100
|
358 |
!SocketAddress methodsFor:'queries'!
|
|
359 |
|
488
|
360 |
address
|
|
361 |
^ self subclassResponsibility
|
|
362 |
!
|
|
363 |
|
111
|
364 |
hostName
|
1164
|
365 |
^ (self class getNameInfo:self wantHostName:true
|
|
366 |
wantServiceName:false datagram:false flags:0) first
|
111
|
367 |
|
1164
|
368 |
"
|
|
369 |
(IPSocketAddress hostAddress:#[127 0 0 1] port:7) hostName
|
|
370 |
(IPSocketAddress hostAddress:#[193 141 12 193] port:10) hostName
|
|
371 |
"
|
111
|
372 |
!
|
|
373 |
|
100
|
374 |
portOrName
|
210
|
375 |
^ self subclassResponsibility
|
1164
|
376 |
!
|
|
377 |
|
|
378 |
serviceName
|
|
379 |
^ (self class getNameInfo:self wantHostName:false
|
|
380 |
wantServiceName:true datagram:false flags:0) second
|
|
381 |
|
|
382 |
"
|
|
383 |
(IPSocketAddress hostAddress:#[127 0 0 1] port:7) serviceName
|
|
384 |
(IPSocketAddress hostAddress:#[193 141 12 193] port:10) serviceName
|
|
385 |
"
|
100
|
386 |
! !
|
|
387 |
|
488
|
388 |
!SocketAddress class methodsFor:'documentation'!
|
|
389 |
|
|
390 |
version
|
1225
|
391 |
^ '$Header: /cvs/stx/stx/libbasic2/SocketAddress.st,v 1.13 2003-05-21 13:45:45 cg Exp $'
|
488
|
392 |
! !
|