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