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