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