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