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