author | mawalch |
Mon, 12 Mar 2018 15:53:19 +0100 | |
changeset 4618 | c68aee83ba0b |
parent 4339 | 499b62578e95 |
child 4678 | 28f9a9f71751 |
permissions | -rw-r--r-- |
100 | 1 |
" |
2 |
COPYRIGHT (c) 1995 by Claus Gittinger |
|
1164 | 3 |
All Rights Reserved |
100 | 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 |
" |
|
1023 | 12 |
"{ Package: 'stx:libbasic2' }" |
13 |
||
3508 | 14 |
"{ NameSpace: Smalltalk }" |
15 |
||
818 | 16 |
SocketAddress variableByteSubclass:#IPSocketAddress |
1164 | 17 |
instanceVariableNames:'' |
1877
d143433fd007
cache: decay after some time to allow for dhcp-changing addresses
Claus Gittinger <cg@exept.de>
parents:
1585
diff
changeset
|
18 |
classVariableNames:'CacheInvalidationTimeInterval AddressCacheSize NameCacheSize' |
488 | 19 |
poolDictionaries:'' |
816 | 20 |
category:'OS-Sockets' |
100 | 21 |
! |
22 |
||
1273
03afdf3f895e
addrCache and nameCache must be separate for
Claus Gittinger <cg@exept.de>
parents:
1262
diff
changeset
|
23 |
IPSocketAddress class instanceVariableNames:'addrCache nameCache' |
03afdf3f895e
addrCache and nameCache must be separate for
Claus Gittinger <cg@exept.de>
parents:
1262
diff
changeset
|
24 |
|
03afdf3f895e
addrCache and nameCache must be separate for
Claus Gittinger <cg@exept.de>
parents:
1262
diff
changeset
|
25 |
" |
03afdf3f895e
addrCache and nameCache must be separate for
Claus Gittinger <cg@exept.de>
parents:
1262
diff
changeset
|
26 |
No other class instance variables are inherited by this class. |
03afdf3f895e
addrCache and nameCache must be separate for
Claus Gittinger <cg@exept.de>
parents:
1262
diff
changeset
|
27 |
" |
03afdf3f895e
addrCache and nameCache must be separate for
Claus Gittinger <cg@exept.de>
parents:
1262
diff
changeset
|
28 |
! |
03afdf3f895e
addrCache and nameCache must be separate for
Claus Gittinger <cg@exept.de>
parents:
1262
diff
changeset
|
29 |
|
100 | 30 |
!IPSocketAddress class methodsFor:'documentation'! |
31 |
||
32 |
copyright |
|
33 |
" |
|
34 |
COPYRIGHT (c) 1995 by Claus Gittinger |
|
1164 | 35 |
All Rights Reserved |
100 | 36 |
|
37 |
This software is furnished under a license and may be used |
|
38 |
only in accordance with the terms of that license and with the |
|
39 |
inclusion of the above copyright notice. This software may not |
|
40 |
be provided or otherwise made available to, or used by, any |
|
41 |
other person. No title to or ownership of the software is |
|
42 |
hereby transferred. |
|
43 |
" |
|
44 |
! |
|
45 |
||
46 |
documentation |
|
47 |
" |
|
111 | 48 |
Instances of IPSocketAddress represent tcp/ip-domain socket addresses. |
49 |
These consist of an ip address (4 bytes) and a port number. |
|
50 |
||
1877
d143433fd007
cache: decay after some time to allow for dhcp-changing addresses
Claus Gittinger <cg@exept.de>
parents:
1585
diff
changeset
|
51 |
Notice that for performance, name and address translations are cached here |
d143433fd007
cache: decay after some time to allow for dhcp-changing addresses
Claus Gittinger <cg@exept.de>
parents:
1585
diff
changeset
|
52 |
for some time, in order to speed up heavy lookup such as when operating a webserver or similar |
d143433fd007
cache: decay after some time to allow for dhcp-changing addresses
Claus Gittinger <cg@exept.de>
parents:
1585
diff
changeset
|
53 |
application which does many address-to-hostname translations. |
100 | 54 |
|
1877
d143433fd007
cache: decay after some time to allow for dhcp-changing addresses
Claus Gittinger <cg@exept.de>
parents:
1585
diff
changeset
|
55 |
For systems, when the name translation is fast (unix), you may want to disable it, |
d143433fd007
cache: decay after some time to allow for dhcp-changing addresses
Claus Gittinger <cg@exept.de>
parents:
1585
diff
changeset
|
56 |
as it prevents new addresses to be detected for some time (for example, if the own address |
3994 | 57 |
changes due to a new dhcp address being acquired). |
1877
d143433fd007
cache: decay after some time to allow for dhcp-changing addresses
Claus Gittinger <cg@exept.de>
parents:
1585
diff
changeset
|
58 |
You can also change the cacheing-interval time |
d143433fd007
cache: decay after some time to allow for dhcp-changing addresses
Claus Gittinger <cg@exept.de>
parents:
1585
diff
changeset
|
59 |
(see caching protocol, flush*Cache, cachingIntervalTime, *CacheSize). |
d143433fd007
cache: decay after some time to allow for dhcp-changing addresses
Claus Gittinger <cg@exept.de>
parents:
1585
diff
changeset
|
60 |
|
d143433fd007
cache: decay after some time to allow for dhcp-changing addresses
Claus Gittinger <cg@exept.de>
parents:
1585
diff
changeset
|
61 |
[author:] |
d143433fd007
cache: decay after some time to allow for dhcp-changing addresses
Claus Gittinger <cg@exept.de>
parents:
1585
diff
changeset
|
62 |
Claus Gittinger |
100 | 63 |
" |
64 |
! ! |
|
65 |
||
1233 | 66 |
!IPSocketAddress class methodsFor:'instance creation'! |
67 |
||
1559
609a0707ab22
New: #addressString: to create an instance from a numeric string representation like
Stefan Vogel <sv@exept.de>
parents:
1452
diff
changeset
|
68 |
addressString:aString |
3508 | 69 |
"convert an address given in a dot notation like 123.456.78.9. |
70 |
handle IPv6SocketAddresses too." |
|
1559
609a0707ab22
New: #addressString: to create an instance from a numeric string representation like
Stefan Vogel <sv@exept.de>
parents:
1452
diff
changeset
|
71 |
|
3508 | 72 |
(self == IPSocketAddress and:[aString includesAny:'[:']) ifTrue:[ |
73 |
^ IPv6SocketAddress hostAddress:(IPv6SocketAddress hostAddressFromString:aString). |
|
74 |
]. |
|
1559
609a0707ab22
New: #addressString: to create an instance from a numeric string representation like
Stefan Vogel <sv@exept.de>
parents:
1452
diff
changeset
|
75 |
^ self hostAddress:(self hostAddressFromString:aString). |
609a0707ab22
New: #addressString: to create an instance from a numeric string representation like
Stefan Vogel <sv@exept.de>
parents:
1452
diff
changeset
|
76 |
|
609a0707ab22
New: #addressString: to create an instance from a numeric string representation like
Stefan Vogel <sv@exept.de>
parents:
1452
diff
changeset
|
77 |
" |
1585 | 78 |
IPSocketAddress addressString:'1.2.3.4' |
3508 | 79 |
IPSocketAddress addressString:'2001:4dd0:ffa3::1' |
80 |
IPSocketAddress addressString:'[2001:4dd0:ffa3::1]' |
|
1559
609a0707ab22
New: #addressString: to create an instance from a numeric string representation like
Stefan Vogel <sv@exept.de>
parents:
1452
diff
changeset
|
81 |
" |
609a0707ab22
New: #addressString: to create an instance from a numeric string representation like
Stefan Vogel <sv@exept.de>
parents:
1452
diff
changeset
|
82 |
! |
609a0707ab22
New: #addressString: to create an instance from a numeric string representation like
Stefan Vogel <sv@exept.de>
parents:
1452
diff
changeset
|
83 |
|
1909
c76efba26558
Methods to get all addresses for a given hostname (multihomed hosts)
Stefan Vogel <sv@exept.de>
parents:
1883
diff
changeset
|
84 |
allForHostName:name serviceName:portNrOrName type:socketTypeSymbol |
c76efba26558
Methods to get all addresses for a given hostname (multihomed hosts)
Stefan Vogel <sv@exept.de>
parents:
1883
diff
changeset
|
85 |
"get a collection of new instance given a hostname, port or service and type. |
c76efba26558
Methods to get all addresses for a given hostname (multihomed hosts)
Stefan Vogel <sv@exept.de>
parents:
1883
diff
changeset
|
86 |
Multi-homed hosts return more than one entry |
4339 | 87 |
Redefined to cache the result of the name-lookup. |
88 |
||
4618 | 89 |
Take care, this resolves to IPv4-addresses only!! |
4339 | 90 |
Use SocketAddress to resolve to both IPv4 and IPv6-addresses." |
1233 | 91 |
|
1909
c76efba26558
Methods to get all addresses for a given hostname (multihomed hosts)
Stefan Vogel <sv@exept.de>
parents:
1883
diff
changeset
|
92 |
|addressList| |
1233 | 93 |
|
94 |
portNrOrName isString ifTrue:[ |
|
1909
c76efba26558
Methods to get all addresses for a given hostname (multihomed hosts)
Stefan Vogel <sv@exept.de>
parents:
1883
diff
changeset
|
95 |
^ super allForHostName:name serviceName:portNrOrName type:socketTypeSymbol |
1233 | 96 |
]. |
97 |
||
1909
c76efba26558
Methods to get all addresses for a given hostname (multihomed hosts)
Stefan Vogel <sv@exept.de>
parents:
1883
diff
changeset
|
98 |
addressList := self addressCacheAt:name. |
c76efba26558
Methods to get all addresses for a given hostname (multihomed hosts)
Stefan Vogel <sv@exept.de>
parents:
1883
diff
changeset
|
99 |
addressList notNil ifTrue:[ |
4618 | 100 |
addressList := addressList collect:[:eachSocketAddress| |
4339 | 101 |
eachSocketAddress copy |
102 |
port:(portNrOrName ? 0); |
|
103 |
yourself |
|
2172 | 104 |
]. |
1233 | 105 |
] ifFalse:[ |
1909
c76efba26558
Methods to get all addresses for a given hostname (multihomed hosts)
Stefan Vogel <sv@exept.de>
parents:
1883
diff
changeset
|
106 |
addressList := super allForHostName:name serviceName:portNrOrName type:socketTypeSymbol. |
3582 | 107 |
addressList notNil ifTrue:[ |
108 |
self addressCacheAt:name put:(addressList deepCopy). |
|
109 |
]. |
|
1233 | 110 |
]. |
1909
c76efba26558
Methods to get all addresses for a given hostname (multihomed hosts)
Stefan Vogel <sv@exept.de>
parents:
1883
diff
changeset
|
111 |
^ addressList |
1233 | 112 |
|
113 |
" |
|
4618 | 114 |
IPSocketAddress allForHostName:nil serviceName:10 type:#stream |
115 |
SocketAddress allForHostName:'localhost' serviceName:10 type:#stream |
|
116 |
IPSocketAddress allForHostName:'localhost' serviceName:'echo' type:#datagram |
|
117 |
IPSocketAddress allForHostName:'www.google.com' serviceName:80 type:nil |
|
118 |
||
119 |
IPSocketAddress allForHostName:'localhost' serviceName:'echo' type:#datagram |
|
120 |
IPv6SocketAddress allForHostName:'localhost' serviceName:'echo' type:#datagram |
|
1233 | 121 |
" |
2172 | 122 |
|
123 |
"Modified: / 17-06-2009 / 15:21:00 / sr" |
|
4339 | 124 |
"Modified (comment): / 21-02-2017 / 20:59:52 / stefan" |
1559
609a0707ab22
New: #addressString: to create an instance from a numeric string representation like
Stefan Vogel <sv@exept.de>
parents:
1452
diff
changeset
|
125 |
! |
609a0707ab22
New: #addressString: to create an instance from a numeric string representation like
Stefan Vogel <sv@exept.de>
parents:
1452
diff
changeset
|
126 |
|
609a0707ab22
New: #addressString: to create an instance from a numeric string representation like
Stefan Vogel <sv@exept.de>
parents:
1452
diff
changeset
|
127 |
localHost |
1883 | 128 |
"get a new instance representing the local-host address as seen internally" |
1559
609a0707ab22
New: #addressString: to create an instance from a numeric string representation like
Stefan Vogel <sv@exept.de>
parents:
1452
diff
changeset
|
129 |
|
3070 | 130 |
^ self hostAddress:self local |
1877
d143433fd007
cache: decay after some time to allow for dhcp-changing addresses
Claus Gittinger <cg@exept.de>
parents:
1585
diff
changeset
|
131 |
|
d143433fd007
cache: decay after some time to allow for dhcp-changing addresses
Claus Gittinger <cg@exept.de>
parents:
1585
diff
changeset
|
132 |
" |
1909
c76efba26558
Methods to get all addresses for a given hostname (multihomed hosts)
Stefan Vogel <sv@exept.de>
parents:
1883
diff
changeset
|
133 |
self localHost |
1877
d143433fd007
cache: decay after some time to allow for dhcp-changing addresses
Claus Gittinger <cg@exept.de>
parents:
1585
diff
changeset
|
134 |
self localHost hostName |
d143433fd007
cache: decay after some time to allow for dhcp-changing addresses
Claus Gittinger <cg@exept.de>
parents:
1585
diff
changeset
|
135 |
" |
d143433fd007
cache: decay after some time to allow for dhcp-changing addresses
Claus Gittinger <cg@exept.de>
parents:
1585
diff
changeset
|
136 |
|
1883 | 137 |
"Modified: / 04-06-2007 / 21:34:22 / cg" |
138 |
! |
|
139 |
||
140 |
localHostAddress |
|
1909
c76efba26558
Methods to get all addresses for a given hostname (multihomed hosts)
Stefan Vogel <sv@exept.de>
parents:
1883
diff
changeset
|
141 |
"NO NO NEVER!! |
c76efba26558
Methods to get all addresses for a given hostname (multihomed hosts)
Stefan Vogel <sv@exept.de>
parents:
1883
diff
changeset
|
142 |
Do not use this, because: |
c76efba26558
Methods to get all addresses for a given hostname (multihomed hosts)
Stefan Vogel <sv@exept.de>
parents:
1883
diff
changeset
|
143 |
1. It does not work on hosts that get their IP address via dhcp (at least on unix/linux) |
c76efba26558
Methods to get all addresses for a given hostname (multihomed hosts)
Stefan Vogel <sv@exept.de>
parents:
1883
diff
changeset
|
144 |
2. Usually OperatingSystem>>#getHostName does not return a fully qualified domain name |
c76efba26558
Methods to get all addresses for a given hostname (multihomed hosts)
Stefan Vogel <sv@exept.de>
parents:
1883
diff
changeset
|
145 |
3. This does not work well on multi-homed hosts" |
1883 | 146 |
|
1909
c76efba26558
Methods to get all addresses for a given hostname (multihomed hosts)
Stefan Vogel <sv@exept.de>
parents:
1883
diff
changeset
|
147 |
<resource: #obsolete> |
c76efba26558
Methods to get all addresses for a given hostname (multihomed hosts)
Stefan Vogel <sv@exept.de>
parents:
1883
diff
changeset
|
148 |
|
c76efba26558
Methods to get all addresses for a given hostname (multihomed hosts)
Stefan Vogel <sv@exept.de>
parents:
1883
diff
changeset
|
149 |
self obsoleteMethodWarning:'Do not use this method'. |
c76efba26558
Methods to get all addresses for a given hostname (multihomed hosts)
Stefan Vogel <sv@exept.de>
parents:
1883
diff
changeset
|
150 |
^ self hostName:(OperatingSystem getHostName). |
1883 | 151 |
|
152 |
" |
|
1909
c76efba26558
Methods to get all addresses for a given hostname (multihomed hosts)
Stefan Vogel <sv@exept.de>
parents:
1883
diff
changeset
|
153 |
self localHostAddress |
1883 | 154 |
" |
155 |
||
1909
c76efba26558
Methods to get all addresses for a given hostname (multihomed hosts)
Stefan Vogel <sv@exept.de>
parents:
1883
diff
changeset
|
156 |
|
1883 | 157 |
"Modified: / 04-06-2007 / 21:17:25 / cg" |
1233 | 158 |
! ! |
159 |
||
1164 | 160 |
!IPSocketAddress class methodsFor:'addressing'! |
161 |
||
162 |
anyAddress |
|
1281 | 163 |
"return the anonymous addresses bytes" |
1164 | 164 |
|
1281 | 165 |
^ #[0 0 0 0] |
1164 | 166 |
! |
167 |
||
168 |
anyPort |
|
169 |
"return the anon port number" |
|
170 |
||
171 |
^ 0 |
|
172 |
! |
|
100 | 173 |
|
1281 | 174 |
broadcastAddress |
175 |
"return the broadcast address" |
|
176 |
||
177 |
^ #[255 255 255 255] |
|
178 |
! |
|
179 |
||
1164 | 180 |
firstUnreservedPort |
181 |
"return the first unreserved port number" |
|
182 |
||
183 |
^ 1024 |
|
184 |
! |
|
185 |
||
186 |
local |
|
187 |
"return IN_ADDR_ANY, the address matching any local address" |
|
188 |
||
189 |
^ #[127 0 0 1] |
|
100 | 190 |
! |
191 |
||
1164 | 192 |
maxPort |
193 |
"return the maximum port number" |
|
100 | 194 |
|
1164 | 195 |
^ 16rffff |
816 | 196 |
! |
197 |
||
1164 | 198 |
thisHost |
3044 | 199 |
<resource: #obsolete> |
1226
0114bd044aa9
vw compatible domainSymbols (afXXX)
Claus Gittinger <cg@exept.de>
parents:
1164
diff
changeset
|
200 |
"return the bytes of IN_ADDR_ANY, the address matching any local address" |
1164 | 201 |
|
3044 | 202 |
self obsoleteMethodWarning. |
203 |
^ self anyAddress |
|
1164 | 204 |
! ! |
205 |
||
1273
03afdf3f895e
addrCache and nameCache must be separate for
Claus Gittinger <cg@exept.de>
parents:
1262
diff
changeset
|
206 |
!IPSocketAddress class methodsFor:'caching'! |
03afdf3f895e
addrCache and nameCache must be separate for
Claus Gittinger <cg@exept.de>
parents:
1262
diff
changeset
|
207 |
|
03afdf3f895e
addrCache and nameCache must be separate for
Claus Gittinger <cg@exept.de>
parents:
1262
diff
changeset
|
208 |
addressCacheAt:aHostName |
1909
c76efba26558
Methods to get all addresses for a given hostname (multihomed hosts)
Stefan Vogel <sv@exept.de>
parents:
1883
diff
changeset
|
209 |
|addrAndTime addressList time| |
1877
d143433fd007
cache: decay after some time to allow for dhcp-changing addresses
Claus Gittinger <cg@exept.de>
parents:
1585
diff
changeset
|
210 |
|
1273
03afdf3f895e
addrCache and nameCache must be separate for
Claus Gittinger <cg@exept.de>
parents:
1262
diff
changeset
|
211 |
addrCache notNil ifTrue:[ |
1877
d143433fd007
cache: decay after some time to allow for dhcp-changing addresses
Claus Gittinger <cg@exept.de>
parents:
1585
diff
changeset
|
212 |
addrAndTime := addrCache at:aHostName ifAbsent:nil. |
d143433fd007
cache: decay after some time to allow for dhcp-changing addresses
Claus Gittinger <cg@exept.de>
parents:
1585
diff
changeset
|
213 |
addrAndTime notNil ifTrue:[ |
1909
c76efba26558
Methods to get all addresses for a given hostname (multihomed hosts)
Stefan Vogel <sv@exept.de>
parents:
1883
diff
changeset
|
214 |
addressList := addrAndTime key. |
1877
d143433fd007
cache: decay after some time to allow for dhcp-changing addresses
Claus Gittinger <cg@exept.de>
parents:
1585
diff
changeset
|
215 |
time := addrAndTime value. |
d143433fd007
cache: decay after some time to allow for dhcp-changing addresses
Claus Gittinger <cg@exept.de>
parents:
1585
diff
changeset
|
216 |
((Timestamp now) secondDeltaFrom:time) > self cacheInvalidationTimeInterval ifFalse:[ |
1909
c76efba26558
Methods to get all addresses for a given hostname (multihomed hosts)
Stefan Vogel <sv@exept.de>
parents:
1883
diff
changeset
|
217 |
^ addressList. |
1877
d143433fd007
cache: decay after some time to allow for dhcp-changing addresses
Claus Gittinger <cg@exept.de>
parents:
1585
diff
changeset
|
218 |
] |
d143433fd007
cache: decay after some time to allow for dhcp-changing addresses
Claus Gittinger <cg@exept.de>
parents:
1585
diff
changeset
|
219 |
]. |
1273
03afdf3f895e
addrCache and nameCache must be separate for
Claus Gittinger <cg@exept.de>
parents:
1262
diff
changeset
|
220 |
]. |
03afdf3f895e
addrCache and nameCache must be separate for
Claus Gittinger <cg@exept.de>
parents:
1262
diff
changeset
|
221 |
^ nil |
1877
d143433fd007
cache: decay after some time to allow for dhcp-changing addresses
Claus Gittinger <cg@exept.de>
parents:
1585
diff
changeset
|
222 |
|
d143433fd007
cache: decay after some time to allow for dhcp-changing addresses
Claus Gittinger <cg@exept.de>
parents:
1585
diff
changeset
|
223 |
"Modified: / 26-04-2007 / 11:21:37 / cg" |
1273
03afdf3f895e
addrCache and nameCache must be separate for
Claus Gittinger <cg@exept.de>
parents:
1262
diff
changeset
|
224 |
! |
03afdf3f895e
addrCache and nameCache must be separate for
Claus Gittinger <cg@exept.de>
parents:
1262
diff
changeset
|
225 |
|
1909
c76efba26558
Methods to get all addresses for a given hostname (multihomed hosts)
Stefan Vogel <sv@exept.de>
parents:
1883
diff
changeset
|
226 |
addressCacheAt:aName put:anAddressList |
1877
d143433fd007
cache: decay after some time to allow for dhcp-changing addresses
Claus Gittinger <cg@exept.de>
parents:
1585
diff
changeset
|
227 |
(addrCache isNil or:[addrCache size > self addressCacheSize]) ifTrue:[ |
1273
03afdf3f895e
addrCache and nameCache must be separate for
Claus Gittinger <cg@exept.de>
parents:
1262
diff
changeset
|
228 |
addrCache := Dictionary new. |
03afdf3f895e
addrCache and nameCache must be separate for
Claus Gittinger <cg@exept.de>
parents:
1262
diff
changeset
|
229 |
]. |
03afdf3f895e
addrCache and nameCache must be separate for
Claus Gittinger <cg@exept.de>
parents:
1262
diff
changeset
|
230 |
|
1909
c76efba26558
Methods to get all addresses for a given hostname (multihomed hosts)
Stefan Vogel <sv@exept.de>
parents:
1883
diff
changeset
|
231 |
addrCache at:aName put:(anAddressList -> Timestamp now). |
1877
d143433fd007
cache: decay after some time to allow for dhcp-changing addresses
Claus Gittinger <cg@exept.de>
parents:
1585
diff
changeset
|
232 |
|
d143433fd007
cache: decay after some time to allow for dhcp-changing addresses
Claus Gittinger <cg@exept.de>
parents:
1585
diff
changeset
|
233 |
"Modified: / 26-04-2007 / 11:23:26 / cg" |
d143433fd007
cache: decay after some time to allow for dhcp-changing addresses
Claus Gittinger <cg@exept.de>
parents:
1585
diff
changeset
|
234 |
! |
d143433fd007
cache: decay after some time to allow for dhcp-changing addresses
Claus Gittinger <cg@exept.de>
parents:
1585
diff
changeset
|
235 |
|
d143433fd007
cache: decay after some time to allow for dhcp-changing addresses
Claus Gittinger <cg@exept.de>
parents:
1585
diff
changeset
|
236 |
addressCacheSize |
d143433fd007
cache: decay after some time to allow for dhcp-changing addresses
Claus Gittinger <cg@exept.de>
parents:
1585
diff
changeset
|
237 |
^ AddressCacheSize ? 50 |
d143433fd007
cache: decay after some time to allow for dhcp-changing addresses
Claus Gittinger <cg@exept.de>
parents:
1585
diff
changeset
|
238 |
|
d143433fd007
cache: decay after some time to allow for dhcp-changing addresses
Claus Gittinger <cg@exept.de>
parents:
1585
diff
changeset
|
239 |
"Created: / 26-04-2007 / 11:22:40 / cg" |
d143433fd007
cache: decay after some time to allow for dhcp-changing addresses
Claus Gittinger <cg@exept.de>
parents:
1585
diff
changeset
|
240 |
! |
d143433fd007
cache: decay after some time to allow for dhcp-changing addresses
Claus Gittinger <cg@exept.de>
parents:
1585
diff
changeset
|
241 |
|
d143433fd007
cache: decay after some time to allow for dhcp-changing addresses
Claus Gittinger <cg@exept.de>
parents:
1585
diff
changeset
|
242 |
addressCacheSize:aNumber |
d143433fd007
cache: decay after some time to allow for dhcp-changing addresses
Claus Gittinger <cg@exept.de>
parents:
1585
diff
changeset
|
243 |
AddressCacheSize := aNumber |
d143433fd007
cache: decay after some time to allow for dhcp-changing addresses
Claus Gittinger <cg@exept.de>
parents:
1585
diff
changeset
|
244 |
|
d143433fd007
cache: decay after some time to allow for dhcp-changing addresses
Claus Gittinger <cg@exept.de>
parents:
1585
diff
changeset
|
245 |
"Created: / 26-04-2007 / 11:24:05 / cg" |
d143433fd007
cache: decay after some time to allow for dhcp-changing addresses
Claus Gittinger <cg@exept.de>
parents:
1585
diff
changeset
|
246 |
! |
d143433fd007
cache: decay after some time to allow for dhcp-changing addresses
Claus Gittinger <cg@exept.de>
parents:
1585
diff
changeset
|
247 |
|
d143433fd007
cache: decay after some time to allow for dhcp-changing addresses
Claus Gittinger <cg@exept.de>
parents:
1585
diff
changeset
|
248 |
cacheInvalidationTimeInterval |
d143433fd007
cache: decay after some time to allow for dhcp-changing addresses
Claus Gittinger <cg@exept.de>
parents:
1585
diff
changeset
|
249 |
^ CacheInvalidationTimeInterval ? 30 |
d143433fd007
cache: decay after some time to allow for dhcp-changing addresses
Claus Gittinger <cg@exept.de>
parents:
1585
diff
changeset
|
250 |
|
d143433fd007
cache: decay after some time to allow for dhcp-changing addresses
Claus Gittinger <cg@exept.de>
parents:
1585
diff
changeset
|
251 |
"Created: / 26-04-2007 / 11:21:31 / cg" |
d143433fd007
cache: decay after some time to allow for dhcp-changing addresses
Claus Gittinger <cg@exept.de>
parents:
1585
diff
changeset
|
252 |
! |
d143433fd007
cache: decay after some time to allow for dhcp-changing addresses
Claus Gittinger <cg@exept.de>
parents:
1585
diff
changeset
|
253 |
|
d143433fd007
cache: decay after some time to allow for dhcp-changing addresses
Claus Gittinger <cg@exept.de>
parents:
1585
diff
changeset
|
254 |
cacheInvalidationTimeInterval:seconds |
d143433fd007
cache: decay after some time to allow for dhcp-changing addresses
Claus Gittinger <cg@exept.de>
parents:
1585
diff
changeset
|
255 |
CacheInvalidationTimeInterval := seconds |
d143433fd007
cache: decay after some time to allow for dhcp-changing addresses
Claus Gittinger <cg@exept.de>
parents:
1585
diff
changeset
|
256 |
|
d143433fd007
cache: decay after some time to allow for dhcp-changing addresses
Claus Gittinger <cg@exept.de>
parents:
1585
diff
changeset
|
257 |
"Created: / 26-04-2007 / 11:29:28 / cg" |
1273
03afdf3f895e
addrCache and nameCache must be separate for
Claus Gittinger <cg@exept.de>
parents:
1262
diff
changeset
|
258 |
! |
1261 | 259 |
|
260 |
flushAddressCache |
|
1273
03afdf3f895e
addrCache and nameCache must be separate for
Claus Gittinger <cg@exept.de>
parents:
1262
diff
changeset
|
261 |
addrCache := nil |
1261 | 262 |
|
263 |
" |
|
264 |
self flushAddressCache |
|
265 |
" |
|
1273
03afdf3f895e
addrCache and nameCache must be separate for
Claus Gittinger <cg@exept.de>
parents:
1262
diff
changeset
|
266 |
! |
03afdf3f895e
addrCache and nameCache must be separate for
Claus Gittinger <cg@exept.de>
parents:
1262
diff
changeset
|
267 |
|
03afdf3f895e
addrCache and nameCache must be separate for
Claus Gittinger <cg@exept.de>
parents:
1262
diff
changeset
|
268 |
flushNameCache |
03afdf3f895e
addrCache and nameCache must be separate for
Claus Gittinger <cg@exept.de>
parents:
1262
diff
changeset
|
269 |
nameCache := Dictionary new. |
1877
d143433fd007
cache: decay after some time to allow for dhcp-changing addresses
Claus Gittinger <cg@exept.de>
parents:
1585
diff
changeset
|
270 |
|
d143433fd007
cache: decay after some time to allow for dhcp-changing addresses
Claus Gittinger <cg@exept.de>
parents:
1585
diff
changeset
|
271 |
" |
d143433fd007
cache: decay after some time to allow for dhcp-changing addresses
Claus Gittinger <cg@exept.de>
parents:
1585
diff
changeset
|
272 |
self flushNameCache |
d143433fd007
cache: decay after some time to allow for dhcp-changing addresses
Claus Gittinger <cg@exept.de>
parents:
1585
diff
changeset
|
273 |
" |
d143433fd007
cache: decay after some time to allow for dhcp-changing addresses
Claus Gittinger <cg@exept.de>
parents:
1585
diff
changeset
|
274 |
|
d143433fd007
cache: decay after some time to allow for dhcp-changing addresses
Claus Gittinger <cg@exept.de>
parents:
1585
diff
changeset
|
275 |
"Modified: / 26-04-2007 / 11:25:32 / cg" |
1273
03afdf3f895e
addrCache and nameCache must be separate for
Claus Gittinger <cg@exept.de>
parents:
1262
diff
changeset
|
276 |
! |
03afdf3f895e
addrCache and nameCache must be separate for
Claus Gittinger <cg@exept.de>
parents:
1262
diff
changeset
|
277 |
|
03afdf3f895e
addrCache and nameCache must be separate for
Claus Gittinger <cg@exept.de>
parents:
1262
diff
changeset
|
278 |
nameCacheAt:aHostAddress |
1877
d143433fd007
cache: decay after some time to allow for dhcp-changing addresses
Claus Gittinger <cg@exept.de>
parents:
1585
diff
changeset
|
279 |
|nameAndTime name time| |
d143433fd007
cache: decay after some time to allow for dhcp-changing addresses
Claus Gittinger <cg@exept.de>
parents:
1585
diff
changeset
|
280 |
|
1273
03afdf3f895e
addrCache and nameCache must be separate for
Claus Gittinger <cg@exept.de>
parents:
1262
diff
changeset
|
281 |
nameCache notNil ifTrue:[ |
1877
d143433fd007
cache: decay after some time to allow for dhcp-changing addresses
Claus Gittinger <cg@exept.de>
parents:
1585
diff
changeset
|
282 |
nameAndTime := nameCache at:aHostAddress ifAbsent:nil. |
d143433fd007
cache: decay after some time to allow for dhcp-changing addresses
Claus Gittinger <cg@exept.de>
parents:
1585
diff
changeset
|
283 |
nameAndTime notNil ifTrue:[ |
d143433fd007
cache: decay after some time to allow for dhcp-changing addresses
Claus Gittinger <cg@exept.de>
parents:
1585
diff
changeset
|
284 |
name := nameAndTime key. |
d143433fd007
cache: decay after some time to allow for dhcp-changing addresses
Claus Gittinger <cg@exept.de>
parents:
1585
diff
changeset
|
285 |
time := nameAndTime value. |
3228 | 286 |
(Timestamp now secondDeltaFrom:time) > self cacheInvalidationTimeInterval ifFalse:[ |
1877
d143433fd007
cache: decay after some time to allow for dhcp-changing addresses
Claus Gittinger <cg@exept.de>
parents:
1585
diff
changeset
|
287 |
^ name. |
d143433fd007
cache: decay after some time to allow for dhcp-changing addresses
Claus Gittinger <cg@exept.de>
parents:
1585
diff
changeset
|
288 |
] |
d143433fd007
cache: decay after some time to allow for dhcp-changing addresses
Claus Gittinger <cg@exept.de>
parents:
1585
diff
changeset
|
289 |
]. |
1273
03afdf3f895e
addrCache and nameCache must be separate for
Claus Gittinger <cg@exept.de>
parents:
1262
diff
changeset
|
290 |
]. |
03afdf3f895e
addrCache and nameCache must be separate for
Claus Gittinger <cg@exept.de>
parents:
1262
diff
changeset
|
291 |
^ nil |
1877
d143433fd007
cache: decay after some time to allow for dhcp-changing addresses
Claus Gittinger <cg@exept.de>
parents:
1585
diff
changeset
|
292 |
|
d143433fd007
cache: decay after some time to allow for dhcp-changing addresses
Claus Gittinger <cg@exept.de>
parents:
1585
diff
changeset
|
293 |
"Modified: / 26-04-2007 / 11:25:11 / cg" |
1273
03afdf3f895e
addrCache and nameCache must be separate for
Claus Gittinger <cg@exept.de>
parents:
1262
diff
changeset
|
294 |
! |
03afdf3f895e
addrCache and nameCache must be separate for
Claus Gittinger <cg@exept.de>
parents:
1262
diff
changeset
|
295 |
|
03afdf3f895e
addrCache and nameCache must be separate for
Claus Gittinger <cg@exept.de>
parents:
1262
diff
changeset
|
296 |
nameCacheAt:aHostAddress put:aName |
1877
d143433fd007
cache: decay after some time to allow for dhcp-changing addresses
Claus Gittinger <cg@exept.de>
parents:
1585
diff
changeset
|
297 |
(nameCache isNil or:[nameCache size > self nameCacheSize]) ifTrue:[ |
1273
03afdf3f895e
addrCache and nameCache must be separate for
Claus Gittinger <cg@exept.de>
parents:
1262
diff
changeset
|
298 |
nameCache := Dictionary new. |
03afdf3f895e
addrCache and nameCache must be separate for
Claus Gittinger <cg@exept.de>
parents:
1262
diff
changeset
|
299 |
]. |
03afdf3f895e
addrCache and nameCache must be separate for
Claus Gittinger <cg@exept.de>
parents:
1262
diff
changeset
|
300 |
|
1877
d143433fd007
cache: decay after some time to allow for dhcp-changing addresses
Claus Gittinger <cg@exept.de>
parents:
1585
diff
changeset
|
301 |
nameCache at:aHostAddress put:(aName -> Timestamp now). |
d143433fd007
cache: decay after some time to allow for dhcp-changing addresses
Claus Gittinger <cg@exept.de>
parents:
1585
diff
changeset
|
302 |
|
d143433fd007
cache: decay after some time to allow for dhcp-changing addresses
Claus Gittinger <cg@exept.de>
parents:
1585
diff
changeset
|
303 |
"Modified: / 26-04-2007 / 11:23:35 / cg" |
d143433fd007
cache: decay after some time to allow for dhcp-changing addresses
Claus Gittinger <cg@exept.de>
parents:
1585
diff
changeset
|
304 |
! |
d143433fd007
cache: decay after some time to allow for dhcp-changing addresses
Claus Gittinger <cg@exept.de>
parents:
1585
diff
changeset
|
305 |
|
d143433fd007
cache: decay after some time to allow for dhcp-changing addresses
Claus Gittinger <cg@exept.de>
parents:
1585
diff
changeset
|
306 |
nameCacheSize |
d143433fd007
cache: decay after some time to allow for dhcp-changing addresses
Claus Gittinger <cg@exept.de>
parents:
1585
diff
changeset
|
307 |
^ NameCacheSize ? 50 |
d143433fd007
cache: decay after some time to allow for dhcp-changing addresses
Claus Gittinger <cg@exept.de>
parents:
1585
diff
changeset
|
308 |
|
d143433fd007
cache: decay after some time to allow for dhcp-changing addresses
Claus Gittinger <cg@exept.de>
parents:
1585
diff
changeset
|
309 |
"Created: / 26-04-2007 / 11:22:44 / cg" |
d143433fd007
cache: decay after some time to allow for dhcp-changing addresses
Claus Gittinger <cg@exept.de>
parents:
1585
diff
changeset
|
310 |
! |
d143433fd007
cache: decay after some time to allow for dhcp-changing addresses
Claus Gittinger <cg@exept.de>
parents:
1585
diff
changeset
|
311 |
|
d143433fd007
cache: decay after some time to allow for dhcp-changing addresses
Claus Gittinger <cg@exept.de>
parents:
1585
diff
changeset
|
312 |
nameCacheSize:aNumber |
d143433fd007
cache: decay after some time to allow for dhcp-changing addresses
Claus Gittinger <cg@exept.de>
parents:
1585
diff
changeset
|
313 |
NameCacheSize := aNumber |
d143433fd007
cache: decay after some time to allow for dhcp-changing addresses
Claus Gittinger <cg@exept.de>
parents:
1585
diff
changeset
|
314 |
|
d143433fd007
cache: decay after some time to allow for dhcp-changing addresses
Claus Gittinger <cg@exept.de>
parents:
1585
diff
changeset
|
315 |
"Created: / 26-04-2007 / 11:23:49 / cg" |
1261 | 316 |
! ! |
317 |
||
1559
609a0707ab22
New: #addressString: to create an instance from a numeric string representation like
Stefan Vogel <sv@exept.de>
parents:
1452
diff
changeset
|
318 |
!IPSocketAddress class methodsFor:'conversion'! |
609a0707ab22
New: #addressString: to create an instance from a numeric string representation like
Stefan Vogel <sv@exept.de>
parents:
1452
diff
changeset
|
319 |
|
609a0707ab22
New: #addressString: to create an instance from a numeric string representation like
Stefan Vogel <sv@exept.de>
parents:
1452
diff
changeset
|
320 |
hostAddressFromString:aString |
609a0707ab22
New: #addressString: to create an instance from a numeric string representation like
Stefan Vogel <sv@exept.de>
parents:
1452
diff
changeset
|
321 |
"convert an address given in a dot notation like 123.456.78.9" |
609a0707ab22
New: #addressString: to create an instance from a numeric string representation like
Stefan Vogel <sv@exept.de>
parents:
1452
diff
changeset
|
322 |
|
609a0707ab22
New: #addressString: to create an instance from a numeric string representation like
Stefan Vogel <sv@exept.de>
parents:
1452
diff
changeset
|
323 |
|components bytes| |
609a0707ab22
New: #addressString: to create an instance from a numeric string representation like
Stefan Vogel <sv@exept.de>
parents:
1452
diff
changeset
|
324 |
|
609a0707ab22
New: #addressString: to create an instance from a numeric string representation like
Stefan Vogel <sv@exept.de>
parents:
1452
diff
changeset
|
325 |
components := aString asCollectionOfSubstringsSeparatedBy:$.. |
609a0707ab22
New: #addressString: to create an instance from a numeric string representation like
Stefan Vogel <sv@exept.de>
parents:
1452
diff
changeset
|
326 |
components size ~~ 4 ifTrue:[ |
609a0707ab22
New: #addressString: to create an instance from a numeric string representation like
Stefan Vogel <sv@exept.de>
parents:
1452
diff
changeset
|
327 |
^ NameLookupError raiseRequestWith:aString errorString:' - bad address string'. |
609a0707ab22
New: #addressString: to create an instance from a numeric string representation like
Stefan Vogel <sv@exept.de>
parents:
1452
diff
changeset
|
328 |
]. |
609a0707ab22
New: #addressString: to create an instance from a numeric string representation like
Stefan Vogel <sv@exept.de>
parents:
1452
diff
changeset
|
329 |
|
609a0707ab22
New: #addressString: to create an instance from a numeric string representation like
Stefan Vogel <sv@exept.de>
parents:
1452
diff
changeset
|
330 |
bytes := components collect:[:eachComponent| |
609a0707ab22
New: #addressString: to create an instance from a numeric string representation like
Stefan Vogel <sv@exept.de>
parents:
1452
diff
changeset
|
331 |
Integer readFromString:eachComponent onError:[^ NameLookupError raiseRequestWith:aString errorString:' - bad address string']. |
609a0707ab22
New: #addressString: to create an instance from a numeric string representation like
Stefan Vogel <sv@exept.de>
parents:
1452
diff
changeset
|
332 |
]. |
609a0707ab22
New: #addressString: to create an instance from a numeric string representation like
Stefan Vogel <sv@exept.de>
parents:
1452
diff
changeset
|
333 |
|
609a0707ab22
New: #addressString: to create an instance from a numeric string representation like
Stefan Vogel <sv@exept.de>
parents:
1452
diff
changeset
|
334 |
^ bytes asByteArray. |
609a0707ab22
New: #addressString: to create an instance from a numeric string representation like
Stefan Vogel <sv@exept.de>
parents:
1452
diff
changeset
|
335 |
|
609a0707ab22
New: #addressString: to create an instance from a numeric string representation like
Stefan Vogel <sv@exept.de>
parents:
1452
diff
changeset
|
336 |
|
609a0707ab22
New: #addressString: to create an instance from a numeric string representation like
Stefan Vogel <sv@exept.de>
parents:
1452
diff
changeset
|
337 |
" |
609a0707ab22
New: #addressString: to create an instance from a numeric string representation like
Stefan Vogel <sv@exept.de>
parents:
1452
diff
changeset
|
338 |
IPSocketAddress hostAddressFromString:'1.2.3.4' |
609a0707ab22
New: #addressString: to create an instance from a numeric string representation like
Stefan Vogel <sv@exept.de>
parents:
1452
diff
changeset
|
339 |
IPSocketAddress hostAddressFromString:'255.255.255.255' |
609a0707ab22
New: #addressString: to create an instance from a numeric string representation like
Stefan Vogel <sv@exept.de>
parents:
1452
diff
changeset
|
340 |
" |
609a0707ab22
New: #addressString: to create an instance from a numeric string representation like
Stefan Vogel <sv@exept.de>
parents:
1452
diff
changeset
|
341 |
! ! |
609a0707ab22
New: #addressString: to create an instance from a numeric string representation like
Stefan Vogel <sv@exept.de>
parents:
1452
diff
changeset
|
342 |
|
1164 | 343 |
!IPSocketAddress class methodsFor:'queries'! |
816 | 344 |
|
3052
e690209a5c52
changes #domainSymbol to #domain fpr comaptibility with Socket
Stefan Vogel <sv@exept.de>
parents:
3049
diff
changeset
|
345 |
domain |
1226
0114bd044aa9
vw compatible domainSymbols (afXXX)
Claus Gittinger <cg@exept.de>
parents:
1164
diff
changeset
|
346 |
^ #'AF_INET' |
0114bd044aa9
vw compatible domainSymbols (afXXX)
Claus Gittinger <cg@exept.de>
parents:
1164
diff
changeset
|
347 |
! |
0114bd044aa9
vw compatible domainSymbols (afXXX)
Claus Gittinger <cg@exept.de>
parents:
1164
diff
changeset
|
348 |
|
3044 | 349 |
hostAddressLen |
350 |
"answer the number of bytes of the host address" |
|
351 |
||
352 |
^ 4 |
|
353 |
! |
|
354 |
||
1226
0114bd044aa9
vw compatible domainSymbols (afXXX)
Claus Gittinger <cg@exept.de>
parents:
1164
diff
changeset
|
355 |
obsoleteDomainSymbol |
1164 | 356 |
^ #inet |
357 |
! |
|
358 |
||
1226
0114bd044aa9
vw compatible domainSymbols (afXXX)
Claus Gittinger <cg@exept.de>
parents:
1164
diff
changeset
|
359 |
vwDomainSymbol |
0114bd044aa9
vw compatible domainSymbols (afXXX)
Claus Gittinger <cg@exept.de>
parents:
1164
diff
changeset
|
360 |
^ #afInet |
100 | 361 |
! ! |
362 |
||
363 |
!IPSocketAddress methodsFor:'accessing'! |
|
364 |
||
818 | 365 |
hostAddress |
1164 | 366 |
^ (ByteArray new:4) replaceFrom:1 to:4 with:self startingAt:5 |
816 | 367 |
! |
368 |
||
1164 | 369 |
hostAddress:aByteArray |
370 |
^ self replaceFrom:5 to:8 with:aByteArray startingAt:1 |
|
818 | 371 |
! |
372 |
||
373 |
port |
|
3772 | 374 |
^ self unsignedInt16At:3 MSB:true |
818 | 375 |
! |
376 |
||
377 |
port:aPortNr |
|
3772 | 378 |
self unsignedInt16At:3 put:aPortNr MSB:true |
1164 | 379 |
! ! |
380 |
||
3510 | 381 |
!IPSocketAddress methodsFor:'comparing'! |
382 |
||
3518 | 383 |
sameHostAddress:aSocketAddress |
3510 | 384 |
"answer true, if myself and aSocketAddress have the same host address |
385 |
(but possibly different ports)." |
|
386 |
||
387 |
^ aSocketAddress class == self class |
|
388 |
and:[self sameContentsFrom:5 to:9 as:aSocketAddress startingAt:5] |
|
389 |
! ! |
|
390 |
||
3057 | 391 |
!IPSocketAddress methodsFor:'converting'! |
392 |
||
393 |
asIPv4SocketAddress |
|
394 |
^ self |
|
395 |
! |
|
396 |
||
397 |
asIPv6SocketAddress |
|
398 |
"convert a IPv4 address to a mapped IPv6SocketAddress" |
|
399 |
||
400 |
|ipv6| |
|
401 |
||
402 |
ipv6 := IPv6SocketAddress new. |
|
403 |
ipv6 |
|
404 |
port:self port; |
|
405 |
at:19 put:16rff; |
|
406 |
at:20 put:16rff. |
|
407 |
||
408 |
ipv6 replaceFrom:21 to:24 with:self startingAt:5. |
|
409 |
||
410 |
^ ipv6. |
|
411 |
||
412 |
" |
|
413 |
(self localHost port:80) asIPv6SocketAddress |
|
414 |
(self localHost port:80) asIPv6SocketAddress asIPv4SocketAddress |
|
415 |
" |
|
416 |
! ! |
|
417 |
||
1164 | 418 |
!IPSocketAddress methodsFor:'obsolete'! |
419 |
||
420 |
address |
|
1452 | 421 |
<resource: #obsolete> |
422 |
||
1164 | 423 |
^ self hostAddress |
100 | 424 |
! ! |
425 |
||
770 | 426 |
!IPSocketAddress methodsFor:'printing & storing'! |
427 |
||
1333 | 428 |
printAddressOn:aStream |
429 |
|i1 i2| |
|
100 | 430 |
|
1233 | 431 |
i1 := self adrBytesStart. |
432 |
i2 := i1 + self numAdrBytes - 1. |
|
433 |
i1 to:i2 do:[:i | |
|
434 |
i ~~ i1 ifTrue:[ |
|
435 |
aStream nextPut:$. |
|
436 |
]. |
|
437 |
(self at:i) printOn:aStream |
|
1164 | 438 |
]. |
4100 | 439 |
|
440 |
" |
|
441 |
String streamContents:[:s | (IPSocketAddress hostName:'localhost') printAddressOn:s] |
|
442 |
(IPSocketAddress hostName:'localhost') printString |
|
4101 | 443 |
(IPSocketAddress hostName:'localhost') hostAddress printString |
4100 | 444 |
(IPSocketAddress hostName:'exeptn') printString |
445 |
(IPSocketAddress hostName:'exeptn') printString |
|
446 |
" |
|
488 | 447 |
! ! |
448 |
||
1233 | 449 |
!IPSocketAddress methodsFor:'private'! |
450 |
||
451 |
adrBytesStart |
|
452 |
^ 5 |
|
453 |
! |
|
454 |
||
455 |
numAdrBytes |
|
456 |
^ 4 |
|
457 |
! ! |
|
458 |
||
459 |
!IPSocketAddress methodsFor:'queries'! |
|
460 |
||
461 |
hostName |
|
1273
03afdf3f895e
addrCache and nameCache must be separate for
Claus Gittinger <cg@exept.de>
parents:
1262
diff
changeset
|
462 |
|addr name| |
1233 | 463 |
|
1273
03afdf3f895e
addrCache and nameCache must be separate for
Claus Gittinger <cg@exept.de>
parents:
1262
diff
changeset
|
464 |
addr := self hostAddress. |
03afdf3f895e
addrCache and nameCache must be separate for
Claus Gittinger <cg@exept.de>
parents:
1262
diff
changeset
|
465 |
|
03afdf3f895e
addrCache and nameCache must be separate for
Claus Gittinger <cg@exept.de>
parents:
1262
diff
changeset
|
466 |
name := self class nameCacheAt:addr. |
03afdf3f895e
addrCache and nameCache must be separate for
Claus Gittinger <cg@exept.de>
parents:
1262
diff
changeset
|
467 |
name notNil ifTrue:[^ name]. |
1233 | 468 |
|
469 |
name := super hostName. |
|
470 |
name notNil ifTrue:[ |
|
1273
03afdf3f895e
addrCache and nameCache must be separate for
Claus Gittinger <cg@exept.de>
parents:
1262
diff
changeset
|
471 |
self class nameCacheAt:addr put:name. |
1233 | 472 |
]. |
473 |
^ name |
|
1337 | 474 |
! |
475 |
||
2567 | 476 |
netmaskSize |
477 |
"If this is an address mask: answer the number of mask bits in the address mask" |
|
478 |
||
479 |
|sz bI lowBit lowBitHere hostAddress| |
|
480 |
||
481 |
hostAddress := self hostAddress. |
|
482 |
||
483 |
lowBit := 0. |
|
484 |
bI := 1. |
|
485 |
sz := hostAddress size. |
|
486 |
[ |
|
487 |
lowBitHere := (hostAddress basicAt:bI) lowBit. |
|
488 |
lowBitHere ~= 0 ifTrue:[ |
|
489 |
lowBit := lowBit + (9 - lowBitHere). |
|
490 |
]. |
|
491 |
bI := bI + 1. |
|
492 |
] doWhile:[lowBitHere == 1 and:[bI < sz]]. |
|
493 |
||
494 |
^ lowBit |
|
495 |
||
496 |
" |
|
497 |
(IPSocketAddress addressString:'255.255.0.0') netmaskSize |
|
498 |
(IPSocketAddress addressString:'255.255.128.0') netmaskSize |
|
499 |
(IPv6SocketAddress addressString:'FFFF:FFFF:FFFF:FFFF::') netmaskSize |
|
500 |
(IPv6SocketAddress addressString:'FFFF:FFFF:FFFF:FFF0::') netmaskSize |
|
501 |
(IPv6SocketAddress addressString:'F0FF:FFFF:FFFF:FFF0::') netmaskSize |
|
502 |
" |
|
503 |
! |
|
504 |
||
1337 | 505 |
networkAddress |
2567 | 506 |
"THINK TWICE before using this!! |
3510 | 507 |
This is deprecated, it doesn't make sense any longer since CIDR, subnetting and netmasks |
2567 | 508 |
have been introduced. |
509 |
||
510 |
Extract and return the network part from the host address." |
|
511 |
<resource: #obsolete> |
|
1337 | 512 |
|
513 |
|highAddrByte| |
|
514 |
||
515 |
highAddrByte := self at: 5. |
|
516 |
(highAddrByte bitAnd: 16r80) == 0 ifTrue: [^ByteArray with: highAddrByte]. |
|
517 |
(highAddrByte bitAnd: 16rC0) == 16r80 ifTrue: [^ByteArray with: highAddrByte with: (self at: 6)]. |
|
518 |
(highAddrByte bitAnd: 16rC0) == 16rC0 ifTrue: [^ByteArray with: highAddrByte with: (self at: 6) with: (self at: 7)]. |
|
519 |
^ self error: 'unknown network class' |
|
520 |
||
521 |
" |
|
522 |
(IPSocketAddress hostName:'exept.eu.org') networkAddress |
|
523 |
(IPSocketAddress hostName:'exept.exept.de') networkAddress |
|
524 |
(IPSocketAddress hostName:'ibm.com') networkAddress |
|
525 |
(IPSocketAddress hostName:'cnn.com') networkAddress |
|
526 |
" |
|
527 |
! |
|
528 |
||
529 |
networkClass |
|
2567 | 530 |
"THINK TWICE before using this!! |
531 |
This is deprecated, it doesn't make sense any longer since subnetting and netmasks |
|
532 |
have been introduced. |
|
533 |
||
534 |
Extract and return the network class (as a symbol) from the host address. |
|
1337 | 535 |
Returns one of #classA #classB #classC." |
2567 | 536 |
<resource: #obsolete> |
1337 | 537 |
|
538 |
|highAddrByte| |
|
539 |
||
540 |
highAddrByte := self at: 5. |
|
541 |
(highAddrByte bitAnd: 16r80) == 0 ifTrue: [^#classA]. |
|
542 |
(highAddrByte bitAnd: 16rC0) == 16r80 ifTrue: [^#classB]. |
|
543 |
(highAddrByte bitAnd: 16rC0) == 16rC0 ifTrue: [^#classC]. |
|
544 |
^ self error: 'unknown network class' |
|
545 |
||
546 |
" |
|
547 |
(IPSocketAddress hostName:'exept.eu.org') networkClass |
|
548 |
" |
|
1937 | 549 |
! |
550 |
||
551 |
portOrName |
|
552 |
^ self port |
|
1233 | 553 |
! ! |
554 |
||
1366
e01d0f6c102a
New method #isLocal returning true for local addresses
Stefan Vogel <sv@exept.de>
parents:
1337
diff
changeset
|
555 |
!IPSocketAddress methodsFor:'testing'! |
e01d0f6c102a
New method #isLocal returning true for local addresses
Stefan Vogel <sv@exept.de>
parents:
1337
diff
changeset
|
556 |
|
3049 | 557 |
isBroadcast |
558 |
"answer true, if this is a broadcast address" |
|
559 |
||
560 |
^ (self at:5) == 255 |
|
561 |
and:[self sameContentsFrom:6 to:8 as:#[255 255 255] startingAt:1] |
|
562 |
||
563 |
" |
|
564 |
(self hostAddress:self broadcastAddress) isBroadcast |
|
565 |
" |
|
566 |
! |
|
567 |
||
568 |
isIPSocketAddress |
|
569 |
^ true |
|
570 |
! |
|
571 |
||
572 |
isIPv4SocketAddress |
|
573 |
^ self class == IPSocketAddress |
|
574 |
! |
|
575 |
||
1366
e01d0f6c102a
New method #isLocal returning true for local addresses
Stefan Vogel <sv@exept.de>
parents:
1337
diff
changeset
|
576 |
isLocal |
3986 | 577 |
"answer true, if this address addresses a peer on the same host" |
1366
e01d0f6c102a
New method #isLocal returning true for local addresses
Stefan Vogel <sv@exept.de>
parents:
1337
diff
changeset
|
578 |
|
3049 | 579 |
^ (self at:5) == 127 |
580 |
||
581 |
" |
|
582 |
self localHost isLocal |
|
583 |
" |
|
584 |
! |
|
585 |
||
586 |
isMulticast |
|
587 |
"answer true, if this address is a multicast address 224.0.0.0/4 or Class D" |
|
588 |
||
589 |
^ (self at:5) between:224 and:239 |
|
3044 | 590 |
! |
591 |
||
592 |
isMyAddress |
|
593 |
"answer true, if the address refers to my own host" |
|
594 |
||
595 |
self isLocal ifTrue:[^ true]. |
|
596 |
||
3518 | 597 |
OperatingSystem isLinuxLike ifTrue:[ |
598 |
"/ make this general later, when getNetworkAddressInfo is implemented in Win32OperatingSystem |
|
599 |
OperatingSystem getNetworkAddressInfo do:[:eachIfAddrInfoSet| |
|
600 |
eachIfAddrInfoSet do:[:eachIfAddrInfo| |
|
601 |
|address| |
|
602 |
address := eachIfAddrInfo at:#address ifAbsent:nil. |
|
603 |
address notNil ifTrue:[ |
|
604 |
(self sameHostAddress:address) ifTrue:[^ true]. |
|
605 |
] |
|
606 |
]. |
|
3044 | 607 |
]. |
3518 | 608 |
^ false |
609 |
] ifFalse:[ |
|
610 |
"/ For now - works only for ipv4... |
|
611 |
^ OperatingSystem getNetworkAddresses contains:[:each| |
|
612 |
self sameHostAddress:each |
|
613 |
]. |
|
614 |
]. |
|
3044 | 615 |
|
616 |
" |
|
617 |
(IPSocketAddress hostAddress:#[172 23 1 88] port:80) isMyAddress. |
|
3049 | 618 |
(IPSocketAddress hostAddress:#[192 168 23 29] port:80) isMyAddress. |
3518 | 619 |
(IPv6SocketAddress addressString:'2003:6A:682B:4500:A288:B4FF:FEC6:1514') isMyAddress. |
620 |
(IPv6SocketAddress addressString:'2001:6A:682B:4500:A288:B4FF:FEC6:1514') isMyAddress. |
|
3044 | 621 |
" |
1366
e01d0f6c102a
New method #isLocal returning true for local addresses
Stefan Vogel <sv@exept.de>
parents:
1337
diff
changeset
|
622 |
! ! |
e01d0f6c102a
New method #isLocal returning true for local addresses
Stefan Vogel <sv@exept.de>
parents:
1337
diff
changeset
|
623 |
|
488 | 624 |
!IPSocketAddress class methodsFor:'documentation'! |
100 | 625 |
|
488 | 626 |
version |
3582 | 627 |
^ '$Header$' |
2567 | 628 |
! |
629 |
||
630 |
version_CVS |
|
3582 | 631 |
^ '$Header$' |
100 | 632 |
! ! |
3044 | 633 |