author | Claus Gittinger <cg@exept.de> |
Wed, 21 May 2003 19:03:42 +0200 | |
changeset 1226 | 0114bd044aa9 |
parent 1225 | 8839f52dde96 |
child 1228 | b766b9873ba6 |
permissions | -rw-r--r-- |
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 |
||
1226
0114bd044aa9
vw compatible domainSymbols (afXXX)
Claus Gittinger <cg@exept.de>
parents:
1225
diff
changeset
|
90 |
addressInfo := self |
0114bd044aa9
vw compatible domainSymbols (afXXX)
Claus Gittinger <cg@exept.de>
parents:
1225
diff
changeset
|
91 |
getAddressInfo:name |
0114bd044aa9
vw compatible domainSymbols (afXXX)
Claus Gittinger <cg@exept.de>
parents:
1225
diff
changeset
|
92 |
serviceName:serviceName |
0114bd044aa9
vw compatible domainSymbols (afXXX)
Claus Gittinger <cg@exept.de>
parents:
1225
diff
changeset
|
93 |
domain:(self domainSymbol) |
0114bd044aa9
vw compatible domainSymbols (afXXX)
Claus Gittinger <cg@exept.de>
parents:
1225
diff
changeset
|
94 |
type:socketTypeSymbol |
0114bd044aa9
vw compatible domainSymbols (afXXX)
Claus Gittinger <cg@exept.de>
parents:
1225
diff
changeset
|
95 |
protocol:nil |
0114bd044aa9
vw compatible domainSymbols (afXXX)
Claus Gittinger <cg@exept.de>
parents:
1225
diff
changeset
|
96 |
flags:0. |
1164 | 97 |
|
98 |
sa := addressInfo first socketAddress. |
|
99 |
addressInfo size > 1 ifTrue:[ |
|
100 |
(addressInfo contains:[:entry| entry socketAddress ~= sa]) ifTrue:[ |
|
101 |
self error:'too many results' |
|
102 |
]. |
|
103 |
]. |
|
104 |
port notNil ifTrue:[ |
|
105 |
sa port:port. |
|
106 |
]. |
|
107 |
^ sa |
|
108 |
||
109 |
" |
|
110 |
SocketAddress hostName:'localhost' serviceName:10 type:#stream |
|
111 |
IPSocketAddress hostName:'localhost' serviceName:'echo' type:#datagram |
|
112 |
IPSocketAddress hostName:'localhost' serviceName:'echo' type:nil |
|
113 |
" |
|
818 | 114 |
! |
115 |
||
116 |
new |
|
1164 | 117 |
^ (self new:self socketAddressSize) domainCode:self domainCode |
118 |
||
119 |
" |
|
120 |
IPSocketAddress new |
|
121 |
IPv6SocketAddress new |
|
122 |
UDSocketAddress new |
|
123 |
AppletalkSocketAddress new |
|
124 |
" |
|
125 |
! |
|
818 | 126 |
|
1164 | 127 |
newDomain:domain |
128 |
"answer an new socket address for a given domain" |
|
129 |
||
130 |
^ (self knownClassFromCode:domain) new |
|
131 |
||
132 |
" |
|
133 |
self newDomain:#unix |
|
134 |
self newDomain:#inet |
|
135 |
" |
|
816 | 136 |
! ! |
137 |
||
100 | 138 |
!SocketAddress class methodsFor:'queries'! |
139 |
||
1164 | 140 |
domainCode |
141 |
"answer the numerical domain code used in socket addresses" |
|
142 |
||
143 |
^ OperatingSystem socketAccessor domainCodeOf:self domainSymbol |
|
144 |
! |
|
145 |
||
100 | 146 |
domainCodeFromName:aNameSymbol |
816 | 147 |
"this is a compatibility method; |
148 |
VW returns the internal unix codes here - however, in ST/X, |
|
149 |
symbols are returned, which are translated later (in Socket)" |
|
150 |
||
151 |
aNameSymbol == #afUnix ifTrue:[^ #unix]. |
|
152 |
aNameSymbol == #afInet ifTrue:[^ #inet]. |
|
153 |
aNameSymbol == #afIpV6 ifTrue:[^ #inet6]. |
|
154 |
aNameSymbol == #afAppletalk ifTrue:[^ #appletalk]. |
|
155 |
aNameSymbol == #afDecnet ifTrue:[^ #DECnet]. |
|
156 |
aNameSymbol == #afSna ifTrue:[^ #sna]. |
|
157 |
aNameSymbol == #afNs ifTrue:[^ #xns]. |
|
158 |
aNameSymbol == #afCcitt ifTrue:[^ #ccitt]. |
|
159 |
||
100 | 160 |
"/ |
161 |
"/ could someone tell me which symbols are used in ST-80's SocketAddress class ? |
|
162 |
"/ |
|
163 |
self error:'no more mimicri implemented yet ...' |
|
602 | 164 |
|
165 |
"Modified: / 9.1.1998 / 10:03:56 / stefan" |
|
816 | 166 |
! |
167 |
||
168 |
domainNameFromCode:code |
|
169 |
"this is a compatibility method; |
|
170 |
VW expects the internal unix codes here - however, in ST/X, |
|
171 |
symbols are expected - keeping the numeric values secret (in Socket)" |
|
172 |
||
173 |
code == #unix ifTrue:[^ #afUnix]. |
|
174 |
code == #inet ifTrue:[^ #afInet]. |
|
175 |
code == #inet6 ifTrue:[^ #afIpV6]. |
|
176 |
code == #appletalk ifTrue:[^ #afAppletalk]. |
|
177 |
code == #DECnet ifTrue:[^ #afDecnet]. |
|
178 |
code == #sna ifTrue:[^ #afSna]. |
|
179 |
code == #xns ifTrue:[^ #afNs]. |
|
180 |
code == #ccitt ifTrue:[^ #afCcitt]. |
|
181 |
||
182 |
"/ |
|
183 |
"/ could someone tell me which symbols are used in ST-80's SocketAddress class ? |
|
184 |
"/ |
|
185 |
self error:'no more mimicri implemented yet ...' |
|
186 |
||
187 |
! |
|
188 |
||
1164 | 189 |
domainSymbol |
190 |
"answer the domain symbol. Subclasses redefine this" |
|
191 |
||
192 |
self == SocketAddress ifTrue:[ |
|
193 |
^ nil |
|
194 |
] ifFalse:[ |
|
195 |
self subclassResponsibility |
|
196 |
] |
|
197 |
! |
|
198 |
||
199 |
getAddressInfo:hostName serviceName:serviceName domain:domainArg type:typeArg protocol:protoArg flags:flags |
|
200 |
"answer an Array of socket addresses for serviceName on hostName |
|
201 |
Domain, type, protocol may be nil or specify a hint for the socket |
|
202 |
addresses to be returned." |
|
203 |
||
204 |
^ OperatingSystem socketAccessor |
|
205 |
getAddressInfo:hostName serviceName:serviceName |
|
206 |
domain:domainArg type:typeArg protocol:protoArg flags:flags |
|
207 |
||
208 |
" |
|
209 |
self getAddressInfo:'localhost' serviceName:nil |
|
210 |
domain:nil type:nil protocol:nil flags:nil |
|
211 |
self getAddressInfo:'localhost' serviceName:nil |
|
212 |
domain:#inet type:#stream protocol:nil flags:nil |
|
213 |
self getAddressInfo:'localhost' serviceName:nil |
|
214 |
domain:#inet type:#stream protocol:#tcp flags:nil |
|
215 |
self getAddressInfo:'blurb.exept.de' serviceName:nil |
|
216 |
domain:#inet type:nil protocol:nil flags:nil |
|
217 |
self getAddressInfo:'1.2.3.4' serviceName:'6' |
|
218 |
domain:#inet type:nil protocol:nil flags:nil |
|
219 |
self getAddressInfo:'localhost' serviceName:'echo' |
|
220 |
domain:#inet6 type:nil protocol:nil flags:nil |
|
221 |
" |
|
222 |
! |
|
223 |
||
1167 | 224 |
getAddressInfo:hostName serviceName:serviceName type:typeArg protocol:protoArg flags:flags |
225 |
"answer an Array of socket addresses for serviceName on hostName |
|
226 |
Domain, type, protocol may be nil or specify a hint for the socket |
|
227 |
addresses to be returned." |
|
228 |
||
229 |
^ OperatingSystem socketAccessor |
|
230 |
getAddressInfo:hostName serviceName:serviceName |
|
231 |
domain:self domainCode type:typeArg protocol:protoArg flags:flags |
|
232 |
||
233 |
" |
|
234 |
IPSocketAddress getAddressInfo:'localhost' serviceName:nil |
|
235 |
type:nil protocol:nil flags:nil |
|
236 |
IPSocketAddress getAddressInfo:'localhost' serviceName:nil |
|
237 |
type:#stream protocol:nil flags:nil |
|
238 |
IPSocketAddress getAddressInfo:'localhost' serviceName:nil |
|
239 |
type:#stream protocol:#tcp flags:nil |
|
240 |
IPSocketAddress getAddressInfo:'blurb.exept.de' serviceName:nil |
|
241 |
type:nil protocol:nil flags:nil |
|
242 |
IPSocketAddress getAddressInfo:'1.2.3.4' serviceName:'6' |
|
243 |
type:nil protocol:nil flags:nil |
|
244 |
IPSocketAddress getAddressInfo:'localhost' serviceName:'echo' |
|
245 |
type:nil protocol:nil flags:nil |
|
246 |
" |
|
247 |
! |
|
248 |
||
1164 | 249 |
getNameInfo:socketAddress wantHostName:wantHostName wantServiceName:wantServiceName datagram:useDatagram flags:flags |
250 |
"answer an Array containing the hostName and serviceName |
|
251 |
in socketAddress. SocketType may be one " |
|
252 |
||
253 |
^ OperatingSystem socketAccessor |
|
254 |
getNameInfo:socketAddress wantHostName:wantHostName |
|
255 |
wantServiceName:wantServiceName datagram:useDatagram flags:flags |
|
256 |
||
257 |
" |
|
258 |
self getNameInfo: |
|
259 |
(self getAddressInfo:'localhost' serviceName:'echo' |
|
260 |
domain:#inet type:#stream protocol:nil flags:nil) first socketAddress |
|
261 |
wantHostName:true wantServiceName:true datagram:false flags:0 |
|
262 |
" |
|
263 |
! |
|
264 |
||
816 | 265 |
knownClassFromCode:code |
266 |
"this is a compatibility method; |
|
267 |
VW expects the internal unix codes here - however, in ST/X, |
|
268 |
symbols are expected - keeping the numeric values secret (in Socket)" |
|
269 |
||
1226
0114bd044aa9
vw compatible domainSymbols (afXXX)
Claus Gittinger <cg@exept.de>
parents:
1225
diff
changeset
|
270 |
code notNil ifTrue:[ |
1164 | 271 |
self allSubclassesDo:[:cls| |
272 |
cls domainCode == code ifTrue:[ |
|
273 |
^ cls |
|
274 |
]. |
|
275 |
cls domainSymbol == code ifTrue:[ |
|
276 |
^ cls |
|
277 |
]. |
|
1226
0114bd044aa9
vw compatible domainSymbols (afXXX)
Claus Gittinger <cg@exept.de>
parents:
1225
diff
changeset
|
278 |
cls obsoleteDomainSymbol == code ifTrue:[ |
0114bd044aa9
vw compatible domainSymbols (afXXX)
Claus Gittinger <cg@exept.de>
parents:
1225
diff
changeset
|
279 |
^ cls |
0114bd044aa9
vw compatible domainSymbols (afXXX)
Claus Gittinger <cg@exept.de>
parents:
1225
diff
changeset
|
280 |
]. |
0114bd044aa9
vw compatible domainSymbols (afXXX)
Claus Gittinger <cg@exept.de>
parents:
1225
diff
changeset
|
281 |
cls vwDomainSymbol == code ifTrue:[ |
0114bd044aa9
vw compatible domainSymbols (afXXX)
Claus Gittinger <cg@exept.de>
parents:
1225
diff
changeset
|
282 |
^ cls |
0114bd044aa9
vw compatible domainSymbols (afXXX)
Claus Gittinger <cg@exept.de>
parents:
1225
diff
changeset
|
283 |
]. |
1164 | 284 |
]. |
285 |
]. |
|
286 |
^ SocketAddress |
|
816 | 287 |
|
1164 | 288 |
" |
1226
0114bd044aa9
vw compatible domainSymbols (afXXX)
Claus Gittinger <cg@exept.de>
parents:
1225
diff
changeset
|
289 |
self knownClassFromCode:#'AF_UNIX' |
0114bd044aa9
vw compatible domainSymbols (afXXX)
Claus Gittinger <cg@exept.de>
parents:
1225
diff
changeset
|
290 |
self knownClassFromCode:#'AF_INET' |
0114bd044aa9
vw compatible domainSymbols (afXXX)
Claus Gittinger <cg@exept.de>
parents:
1225
diff
changeset
|
291 |
|
0114bd044aa9
vw compatible domainSymbols (afXXX)
Claus Gittinger <cg@exept.de>
parents:
1225
diff
changeset
|
292 |
self knownClassFromCode:1 |
0114bd044aa9
vw compatible domainSymbols (afXXX)
Claus Gittinger <cg@exept.de>
parents:
1225
diff
changeset
|
293 |
self knownClassFromCode:2 |
0114bd044aa9
vw compatible domainSymbols (afXXX)
Claus Gittinger <cg@exept.de>
parents:
1225
diff
changeset
|
294 |
|
0114bd044aa9
vw compatible domainSymbols (afXXX)
Claus Gittinger <cg@exept.de>
parents:
1225
diff
changeset
|
295 |
self knownClassFromCode:#unix obsolete ST/X codes |
1164 | 296 |
self knownClassFromCode:#inet |
1226
0114bd044aa9
vw compatible domainSymbols (afXXX)
Claus Gittinger <cg@exept.de>
parents:
1225
diff
changeset
|
297 |
|
0114bd044aa9
vw compatible domainSymbols (afXXX)
Claus Gittinger <cg@exept.de>
parents:
1225
diff
changeset
|
298 |
self knownClassFromCode:#afUnix visualWorks codes |
0114bd044aa9
vw compatible domainSymbols (afXXX)
Claus Gittinger <cg@exept.de>
parents:
1225
diff
changeset
|
299 |
self knownClassFromCode:#afInet |
1164 | 300 |
" |
301 |
! |
|
816 | 302 |
|
1226
0114bd044aa9
vw compatible domainSymbols (afXXX)
Claus Gittinger <cg@exept.de>
parents:
1225
diff
changeset
|
303 |
obsoleteDomainSymbol |
0114bd044aa9
vw compatible domainSymbols (afXXX)
Claus Gittinger <cg@exept.de>
parents:
1225
diff
changeset
|
304 |
^ nil |
0114bd044aa9
vw compatible domainSymbols (afXXX)
Claus Gittinger <cg@exept.de>
parents:
1225
diff
changeset
|
305 |
! |
0114bd044aa9
vw compatible domainSymbols (afXXX)
Claus Gittinger <cg@exept.de>
parents:
1225
diff
changeset
|
306 |
|
1164 | 307 |
socketAddressSize |
308 |
"answer the OS specific size of a socket address" |
|
309 |
||
310 |
^ self subclassResponsibility |
|
1226
0114bd044aa9
vw compatible domainSymbols (afXXX)
Claus Gittinger <cg@exept.de>
parents:
1225
diff
changeset
|
311 |
! |
0114bd044aa9
vw compatible domainSymbols (afXXX)
Claus Gittinger <cg@exept.de>
parents:
1225
diff
changeset
|
312 |
|
0114bd044aa9
vw compatible domainSymbols (afXXX)
Claus Gittinger <cg@exept.de>
parents:
1225
diff
changeset
|
313 |
vwDomainSymbol |
0114bd044aa9
vw compatible domainSymbols (afXXX)
Claus Gittinger <cg@exept.de>
parents:
1225
diff
changeset
|
314 |
^ nil |
100 | 315 |
! ! |
316 |
||
818 | 317 |
!SocketAddress methodsFor:'accessing'! |
318 |
||
1164 | 319 |
domainCode |
320 |
||
321 |
^ self unsignedShortAt:1 |
|
322 |
! |
|
323 |
||
324 |
domainCode:anInteger |
|
325 |
||
326 |
self unsignedShortAt:1 put:anInteger |
|
327 |
! |
|
328 |
||
1223 | 329 |
hostAddress:addressBytes |
1164 | 330 |
"generic method, subclasses usually redefine this" |
818 | 331 |
|
1223 | 332 |
"/ the first 2-bytes (a short) is the domainCode |
333 |
self replaceBytesFrom:1+2 to:addressBytes size+2 with:addressBytes startingAt:1 |
|
334 |
||
335 |
" |
|
336 |
IPSocketAddress hostAddress:#[193 141 12 193] port:80 |
|
337 |
" |
|
818 | 338 |
! |
339 |
||
1223 | 340 |
hostAddress:addressBytes port:portNr |
341 |
self hostAddress:addressBytes. |
|
818 | 342 |
self port:portNr |
343 |
||
344 |
" |
|
345 |
IPSocketAddress hostAddress:#[193 141 12 193] port:10 |
|
346 |
" |
|
1223 | 347 |
! |
818 | 348 |
|
1223 | 349 |
port |
350 |
self subclassResponsibility |
|
351 |
! |
|
352 |
||
353 |
port:portNr |
|
354 |
self subclassResponsibility |
|
818 | 355 |
! ! |
356 |
||
1164 | 357 |
!SocketAddress methodsFor:'printing & storing'! |
358 |
||
359 |
displayString |
|
360 |
"redefine form Collection" |
|
361 |
||
362 |
|s| |
|
363 |
||
364 |
s := WriteStream on:''. |
|
365 |
self printOn:s. |
|
366 |
^ s contents. |
|
367 |
! ! |
|
368 |
||
369 |
!SocketAddress methodsFor:'private'! |
|
370 |
||
371 |
fromBytes:aByteArray |
|
372 |
"Copy the internal representation of a SocketAddress to myself |
|
373 |
||
374 |
This is an internal interface!!" |
|
375 |
||
1225 | 376 |
self replaceBytesFrom:1 to:(self size) with:aByteArray startingAt:1. |
377 |
||
378 |
||
1164 | 379 |
! ! |
380 |
||
100 | 381 |
!SocketAddress methodsFor:'queries'! |
382 |
||
488 | 383 |
address |
384 |
^ self subclassResponsibility |
|
385 |
! |
|
386 |
||
111 | 387 |
hostName |
1164 | 388 |
^ (self class getNameInfo:self wantHostName:true |
389 |
wantServiceName:false datagram:false flags:0) first |
|
111 | 390 |
|
1164 | 391 |
" |
392 |
(IPSocketAddress hostAddress:#[127 0 0 1] port:7) hostName |
|
393 |
(IPSocketAddress hostAddress:#[193 141 12 193] port:10) hostName |
|
394 |
" |
|
111 | 395 |
! |
396 |
||
100 | 397 |
portOrName |
210 | 398 |
^ self subclassResponsibility |
1164 | 399 |
! |
400 |
||
401 |
serviceName |
|
402 |
^ (self class getNameInfo:self wantHostName:false |
|
403 |
wantServiceName:true datagram:false flags:0) second |
|
404 |
||
405 |
" |
|
406 |
(IPSocketAddress hostAddress:#[127 0 0 1] port:7) serviceName |
|
407 |
(IPSocketAddress hostAddress:#[193 141 12 193] port:10) serviceName |
|
408 |
" |
|
100 | 409 |
! ! |
410 |
||
488 | 411 |
!SocketAddress class methodsFor:'documentation'! |
412 |
||
413 |
version |
|
1226
0114bd044aa9
vw compatible domainSymbols (afXXX)
Claus Gittinger <cg@exept.de>
parents:
1225
diff
changeset
|
414 |
^ '$Header: /cvs/stx/stx/libbasic2/SocketAddress.st,v 1.14 2003-05-21 17:03:42 cg Exp $' |
488 | 415 |
! ! |