author | chegar |
Fri, 16 Sep 2011 12:09:04 -0700 | |
changeset 10596 | 39b3a979e600 |
parent 10437 | dfca69ed7f87 |
child 12047 | 320a714614e9 |
permissions | -rw-r--r-- |
2 | 1 |
/* |
10596
39b3a979e600
7090158: Networking Libraries don't build with javac -Werror
chegar
parents:
10437
diff
changeset
|
2 |
* Copyright (c) 1995, 2011, Oracle and/or its affiliates. All rights reserved. |
2 | 3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
4 |
* |
|
5 |
* This code is free software; you can redistribute it and/or modify it |
|
6 |
* under the terms of the GNU General Public License version 2 only, as |
|
5506 | 7 |
* published by the Free Software Foundation. Oracle designates this |
2 | 8 |
* particular file as subject to the "Classpath" exception as provided |
5506 | 9 |
* by Oracle in the LICENSE file that accompanied this code. |
2 | 10 |
* |
11 |
* This code is distributed in the hope that it will be useful, but WITHOUT |
|
12 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
13 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
14 |
* version 2 for more details (a copy is included in the LICENSE file that |
|
15 |
* accompanied this code). |
|
16 |
* |
|
17 |
* You should have received a copy of the GNU General Public License version |
|
18 |
* 2 along with this work; if not, write to the Free Software Foundation, |
|
19 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
|
20 |
* |
|
5506 | 21 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
22 |
* or visit www.oracle.com if you need additional information or have any |
|
23 |
* questions. |
|
2 | 24 |
*/ |
25 |
||
26 |
package java.net; |
|
27 |
||
28 |
import java.io.IOException; |
|
29 |
import java.util.Enumeration; |
|
30 |
||
31 |
/** |
|
32 |
* The multicast datagram socket class is useful for sending |
|
33 |
* and receiving IP multicast packets. A MulticastSocket is |
|
34 |
* a (UDP) DatagramSocket, with additional capabilities for |
|
35 |
* joining "groups" of other multicast hosts on the internet. |
|
36 |
* <P> |
|
37 |
* A multicast group is specified by a class D IP address |
|
38 |
* and by a standard UDP port number. Class D IP addresses |
|
39 |
* are in the range <CODE>224.0.0.0</CODE> to <CODE>239.255.255.255</CODE>, |
|
40 |
* inclusive. The address 224.0.0.0 is reserved and should not be used. |
|
41 |
* <P> |
|
42 |
* One would join a multicast group by first creating a MulticastSocket |
|
43 |
* with the desired port, then invoking the |
|
44 |
* <CODE>joinGroup(InetAddress groupAddr)</CODE> |
|
45 |
* method: |
|
46 |
* <PRE> |
|
47 |
* // join a Multicast group and send the group salutations |
|
48 |
* ... |
|
49 |
* String msg = "Hello"; |
|
50 |
* InetAddress group = InetAddress.getByName("228.5.6.7"); |
|
51 |
* MulticastSocket s = new MulticastSocket(6789); |
|
52 |
* s.joinGroup(group); |
|
53 |
* DatagramPacket hi = new DatagramPacket(msg.getBytes(), msg.length(), |
|
54 |
* group, 6789); |
|
55 |
* s.send(hi); |
|
56 |
* // get their responses! |
|
57 |
* byte[] buf = new byte[1000]; |
|
58 |
* DatagramPacket recv = new DatagramPacket(buf, buf.length); |
|
59 |
* s.receive(recv); |
|
60 |
* ... |
|
61 |
* // OK, I'm done talking - leave the group... |
|
62 |
* s.leaveGroup(group); |
|
63 |
* </PRE> |
|
64 |
* |
|
65 |
* When one sends a message to a multicast group, <B>all</B> subscribing |
|
66 |
* recipients to that host and port receive the message (within the |
|
67 |
* time-to-live range of the packet, see below). The socket needn't |
|
68 |
* be a member of the multicast group to send messages to it. |
|
69 |
* <P> |
|
70 |
* When a socket subscribes to a multicast group/port, it receives |
|
71 |
* datagrams sent by other hosts to the group/port, as do all other |
|
72 |
* members of the group and port. A socket relinquishes membership |
|
73 |
* in a group by the leaveGroup(InetAddress addr) method. <B> |
|
74 |
* Multiple MulticastSocket's</B> may subscribe to a multicast group |
|
75 |
* and port concurrently, and they will all receive group datagrams. |
|
76 |
* <P> |
|
77 |
* Currently applets are not allowed to use multicast sockets. |
|
78 |
* |
|
79 |
* @author Pavani Diwanji |
|
80 |
* @since JDK1.1 |
|
81 |
*/ |
|
82 |
public |
|
83 |
class MulticastSocket extends DatagramSocket { |
|
84 |
/** |
|
85 |
* Create a multicast socket. |
|
86 |
* |
|
87 |
* <p>If there is a security manager, |
|
88 |
* its <code>checkListen</code> method is first called |
|
89 |
* with 0 as its argument to ensure the operation is allowed. |
|
90 |
* This could result in a SecurityException. |
|
91 |
* <p> |
|
92 |
* When the socket is created the |
|
93 |
* {@link DatagramSocket#setReuseAddress(boolean)} method is |
|
94 |
* called to enable the SO_REUSEADDR socket option. |
|
95 |
* |
|
96 |
* @exception IOException if an I/O exception occurs |
|
97 |
* while creating the MulticastSocket |
|
98 |
* @exception SecurityException if a security manager exists and its |
|
99 |
* <code>checkListen</code> method doesn't allow the operation. |
|
100 |
* @see SecurityManager#checkListen |
|
101 |
* @see java.net.DatagramSocket#setReuseAddress(boolean) |
|
102 |
*/ |
|
103 |
public MulticastSocket() throws IOException { |
|
104 |
this(new InetSocketAddress(0)); |
|
105 |
} |
|
106 |
||
107 |
/** |
|
108 |
* Create a multicast socket and bind it to a specific port. |
|
109 |
* |
|
110 |
* <p>If there is a security manager, |
|
111 |
* its <code>checkListen</code> method is first called |
|
112 |
* with the <code>port</code> argument |
|
113 |
* as its argument to ensure the operation is allowed. |
|
114 |
* This could result in a SecurityException. |
|
115 |
* <p> |
|
116 |
* When the socket is created the |
|
117 |
* {@link DatagramSocket#setReuseAddress(boolean)} method is |
|
118 |
* called to enable the SO_REUSEADDR socket option. |
|
119 |
* |
|
120 |
* @param port port to use |
|
121 |
* @exception IOException if an I/O exception occurs |
|
122 |
* while creating the MulticastSocket |
|
123 |
* @exception SecurityException if a security manager exists and its |
|
124 |
* <code>checkListen</code> method doesn't allow the operation. |
|
125 |
* @see SecurityManager#checkListen |
|
126 |
* @see java.net.DatagramSocket#setReuseAddress(boolean) |
|
127 |
*/ |
|
128 |
public MulticastSocket(int port) throws IOException { |
|
129 |
this(new InetSocketAddress(port)); |
|
130 |
} |
|
131 |
||
132 |
/** |
|
133 |
* Create a MulticastSocket bound to the specified socket address. |
|
134 |
* <p> |
|
135 |
* Or, if the address is <code>null</code>, create an unbound socket. |
|
136 |
* <p> |
|
137 |
* <p>If there is a security manager, |
|
138 |
* its <code>checkListen</code> method is first called |
|
139 |
* with the SocketAddress port as its argument to ensure the operation is allowed. |
|
140 |
* This could result in a SecurityException. |
|
141 |
* <p> |
|
142 |
* When the socket is created the |
|
143 |
* {@link DatagramSocket#setReuseAddress(boolean)} method is |
|
144 |
* called to enable the SO_REUSEADDR socket option. |
|
145 |
* |
|
146 |
* @param bindaddr Socket address to bind to, or <code>null</code> for |
|
147 |
* an unbound socket. |
|
148 |
* @exception IOException if an I/O exception occurs |
|
149 |
* while creating the MulticastSocket |
|
150 |
* @exception SecurityException if a security manager exists and its |
|
151 |
* <code>checkListen</code> method doesn't allow the operation. |
|
152 |
* @see SecurityManager#checkListen |
|
153 |
* @see java.net.DatagramSocket#setReuseAddress(boolean) |
|
154 |
* |
|
155 |
* @since 1.4 |
|
156 |
*/ |
|
157 |
public MulticastSocket(SocketAddress bindaddr) throws IOException { |
|
158 |
super((SocketAddress) null); |
|
159 |
||
160 |
// Enable SO_REUSEADDR before binding |
|
161 |
setReuseAddress(true); |
|
162 |
||
163 |
if (bindaddr != null) { |
|
10437
dfca69ed7f87
7085981: XXSocket types depend on impl finalizer to close if constructor throws exception
michaelm
parents:
5506
diff
changeset
|
164 |
try { |
dfca69ed7f87
7085981: XXSocket types depend on impl finalizer to close if constructor throws exception
michaelm
parents:
5506
diff
changeset
|
165 |
bind(bindaddr); |
dfca69ed7f87
7085981: XXSocket types depend on impl finalizer to close if constructor throws exception
michaelm
parents:
5506
diff
changeset
|
166 |
} finally { |
dfca69ed7f87
7085981: XXSocket types depend on impl finalizer to close if constructor throws exception
michaelm
parents:
5506
diff
changeset
|
167 |
if (!isBound()) |
dfca69ed7f87
7085981: XXSocket types depend on impl finalizer to close if constructor throws exception
michaelm
parents:
5506
diff
changeset
|
168 |
close(); |
dfca69ed7f87
7085981: XXSocket types depend on impl finalizer to close if constructor throws exception
michaelm
parents:
5506
diff
changeset
|
169 |
} |
2 | 170 |
} |
171 |
} |
|
172 |
||
173 |
/** |
|
174 |
* The lock on the socket's TTL. This is for set/getTTL and |
|
175 |
* send(packet,ttl). |
|
176 |
*/ |
|
177 |
private Object ttlLock = new Object(); |
|
178 |
||
179 |
/** |
|
180 |
* The lock on the socket's interface - used by setInterface |
|
181 |
* and getInterface |
|
182 |
*/ |
|
183 |
private Object infLock = new Object(); |
|
184 |
||
185 |
/** |
|
186 |
* The "last" interface set by setInterface on this MulticastSocket |
|
187 |
*/ |
|
188 |
private InetAddress infAddress = null; |
|
189 |
||
190 |
||
191 |
/** |
|
192 |
* Set the default time-to-live for multicast packets sent out |
|
193 |
* on this <code>MulticastSocket</code> in order to control the |
|
194 |
* scope of the multicasts. |
|
195 |
* |
|
196 |
* <p>The ttl is an <b>unsigned</b> 8-bit quantity, and so <B>must</B> be |
|
197 |
* in the range <code> 0 <= ttl <= 0xFF </code>. |
|
198 |
* |
|
199 |
* @param ttl the time-to-live |
|
200 |
* @exception IOException if an I/O exception occurs |
|
201 |
* while setting the default time-to-live value |
|
202 |
* @deprecated use the setTimeToLive method instead, which uses |
|
203 |
* <b>int</b> instead of <b>byte</b> as the type for ttl. |
|
204 |
* @see #getTTL() |
|
205 |
*/ |
|
206 |
@Deprecated |
|
207 |
public void setTTL(byte ttl) throws IOException { |
|
208 |
if (isClosed()) |
|
209 |
throw new SocketException("Socket is closed"); |
|
210 |
getImpl().setTTL(ttl); |
|
211 |
} |
|
212 |
||
213 |
/** |
|
214 |
* Set the default time-to-live for multicast packets sent out |
|
215 |
* on this {@code MulticastSocket} in order to control the |
|
216 |
* scope of the multicasts. |
|
217 |
* |
|
218 |
* <P> The ttl <B>must</B> be in the range {@code 0 <= ttl <= |
|
219 |
* 255} or an {@code IllegalArgumentException} will be thrown. |
|
220 |
* Multicast packets sent with a TTL of {@code 0} are not transmitted |
|
221 |
* on the network but may be delivered locally. |
|
222 |
* |
|
223 |
* @param ttl |
|
224 |
* the time-to-live |
|
225 |
* |
|
226 |
* @throws IOException |
|
227 |
* if an I/O exception occurs while setting the |
|
228 |
* default time-to-live value |
|
229 |
* |
|
230 |
* @see #getTimeToLive() |
|
231 |
*/ |
|
232 |
public void setTimeToLive(int ttl) throws IOException { |
|
233 |
if (ttl < 0 || ttl > 255) { |
|
234 |
throw new IllegalArgumentException("ttl out of range"); |
|
235 |
} |
|
236 |
if (isClosed()) |
|
237 |
throw new SocketException("Socket is closed"); |
|
238 |
getImpl().setTimeToLive(ttl); |
|
239 |
} |
|
240 |
||
241 |
/** |
|
242 |
* Get the default time-to-live for multicast packets sent out on |
|
243 |
* the socket. |
|
244 |
* |
|
245 |
* @exception IOException if an I/O exception occurs |
|
246 |
* while getting the default time-to-live value |
|
247 |
* @return the default time-to-live value |
|
248 |
* @deprecated use the getTimeToLive method instead, which returns |
|
249 |
* an <b>int</b> instead of a <b>byte</b>. |
|
250 |
* @see #setTTL(byte) |
|
251 |
*/ |
|
252 |
@Deprecated |
|
253 |
public byte getTTL() throws IOException { |
|
254 |
if (isClosed()) |
|
255 |
throw new SocketException("Socket is closed"); |
|
256 |
return getImpl().getTTL(); |
|
257 |
} |
|
258 |
||
259 |
/** |
|
260 |
* Get the default time-to-live for multicast packets sent out on |
|
261 |
* the socket. |
|
262 |
* @exception IOException if an I/O exception occurs while |
|
263 |
* getting the default time-to-live value |
|
264 |
* @return the default time-to-live value |
|
265 |
* @see #setTimeToLive(int) |
|
266 |
*/ |
|
267 |
public int getTimeToLive() throws IOException { |
|
268 |
if (isClosed()) |
|
269 |
throw new SocketException("Socket is closed"); |
|
270 |
return getImpl().getTimeToLive(); |
|
271 |
} |
|
272 |
||
273 |
/** |
|
274 |
* Joins a multicast group. Its behavior may be affected by |
|
275 |
* <code>setInterface</code> or <code>setNetworkInterface</code>. |
|
276 |
* |
|
277 |
* <p>If there is a security manager, this method first |
|
278 |
* calls its <code>checkMulticast</code> method |
|
279 |
* with the <code>mcastaddr</code> argument |
|
280 |
* as its argument. |
|
281 |
* |
|
282 |
* @param mcastaddr is the multicast address to join |
|
283 |
* |
|
284 |
* @exception IOException if there is an error joining |
|
285 |
* or when the address is not a multicast address. |
|
286 |
* @exception SecurityException if a security manager exists and its |
|
287 |
* <code>checkMulticast</code> method doesn't allow the join. |
|
288 |
* |
|
289 |
* @see SecurityManager#checkMulticast(InetAddress) |
|
290 |
*/ |
|
291 |
public void joinGroup(InetAddress mcastaddr) throws IOException { |
|
292 |
if (isClosed()) { |
|
293 |
throw new SocketException("Socket is closed"); |
|
294 |
} |
|
295 |
||
5180
8161f879d704
6893954: Subclasses of InetAddress may incorrectly interpret network addresses
michaelm
parents:
2
diff
changeset
|
296 |
checkAddress(mcastaddr, "joinGroup"); |
2 | 297 |
SecurityManager security = System.getSecurityManager(); |
298 |
if (security != null) { |
|
299 |
security.checkMulticast(mcastaddr); |
|
300 |
} |
|
301 |
||
302 |
if (!mcastaddr.isMulticastAddress()) { |
|
303 |
throw new SocketException("Not a multicast address"); |
|
304 |
} |
|
305 |
||
306 |
getImpl().join(mcastaddr); |
|
307 |
} |
|
308 |
||
309 |
/** |
|
310 |
* Leave a multicast group. Its behavior may be affected by |
|
311 |
* <code>setInterface</code> or <code>setNetworkInterface</code>. |
|
312 |
* |
|
313 |
* <p>If there is a security manager, this method first |
|
314 |
* calls its <code>checkMulticast</code> method |
|
315 |
* with the <code>mcastaddr</code> argument |
|
316 |
* as its argument. |
|
317 |
* |
|
318 |
* @param mcastaddr is the multicast address to leave |
|
319 |
* @exception IOException if there is an error leaving |
|
320 |
* or when the address is not a multicast address. |
|
321 |
* @exception SecurityException if a security manager exists and its |
|
322 |
* <code>checkMulticast</code> method doesn't allow the operation. |
|
323 |
* |
|
324 |
* @see SecurityManager#checkMulticast(InetAddress) |
|
325 |
*/ |
|
326 |
public void leaveGroup(InetAddress mcastaddr) throws IOException { |
|
327 |
if (isClosed()) { |
|
328 |
throw new SocketException("Socket is closed"); |
|
329 |
} |
|
330 |
||
5180
8161f879d704
6893954: Subclasses of InetAddress may incorrectly interpret network addresses
michaelm
parents:
2
diff
changeset
|
331 |
checkAddress(mcastaddr, "leaveGroup"); |
2 | 332 |
SecurityManager security = System.getSecurityManager(); |
333 |
if (security != null) { |
|
334 |
security.checkMulticast(mcastaddr); |
|
335 |
} |
|
336 |
||
337 |
if (!mcastaddr.isMulticastAddress()) { |
|
338 |
throw new SocketException("Not a multicast address"); |
|
339 |
} |
|
340 |
||
341 |
getImpl().leave(mcastaddr); |
|
342 |
} |
|
343 |
||
344 |
/** |
|
345 |
* Joins the specified multicast group at the specified interface. |
|
346 |
* |
|
347 |
* <p>If there is a security manager, this method first |
|
348 |
* calls its <code>checkMulticast</code> method |
|
349 |
* with the <code>mcastaddr</code> argument |
|
350 |
* as its argument. |
|
351 |
* |
|
352 |
* @param mcastaddr is the multicast address to join |
|
353 |
* @param netIf specifies the local interface to receive multicast |
|
354 |
* datagram packets, or <i>null</i> to defer to the interface set by |
|
355 |
* {@link MulticastSocket#setInterface(InetAddress)} or |
|
356 |
* {@link MulticastSocket#setNetworkInterface(NetworkInterface)} |
|
357 |
* |
|
358 |
* @exception IOException if there is an error joining |
|
359 |
* or when the address is not a multicast address. |
|
360 |
* @exception SecurityException if a security manager exists and its |
|
361 |
* <code>checkMulticast</code> method doesn't allow the join. |
|
362 |
* @throws IllegalArgumentException if mcastaddr is null or is a |
|
363 |
* SocketAddress subclass not supported by this socket |
|
364 |
* |
|
365 |
* @see SecurityManager#checkMulticast(InetAddress) |
|
366 |
* @since 1.4 |
|
367 |
*/ |
|
368 |
public void joinGroup(SocketAddress mcastaddr, NetworkInterface netIf) |
|
369 |
throws IOException { |
|
370 |
if (isClosed()) |
|
371 |
throw new SocketException("Socket is closed"); |
|
372 |
||
373 |
if (mcastaddr == null || !(mcastaddr instanceof InetSocketAddress)) |
|
374 |
throw new IllegalArgumentException("Unsupported address type"); |
|
375 |
||
376 |
if (oldImpl) |
|
377 |
throw new UnsupportedOperationException(); |
|
378 |
||
5180
8161f879d704
6893954: Subclasses of InetAddress may incorrectly interpret network addresses
michaelm
parents:
2
diff
changeset
|
379 |
checkAddress(((InetSocketAddress)mcastaddr).getAddress(), "joinGroup"); |
2 | 380 |
SecurityManager security = System.getSecurityManager(); |
381 |
if (security != null) { |
|
382 |
security.checkMulticast(((InetSocketAddress)mcastaddr).getAddress()); |
|
383 |
} |
|
384 |
||
385 |
if (!((InetSocketAddress)mcastaddr).getAddress().isMulticastAddress()) { |
|
386 |
throw new SocketException("Not a multicast address"); |
|
387 |
} |
|
388 |
||
389 |
getImpl().joinGroup(mcastaddr, netIf); |
|
390 |
} |
|
391 |
||
392 |
/** |
|
393 |
* Leave a multicast group on a specified local interface. |
|
394 |
* |
|
395 |
* <p>If there is a security manager, this method first |
|
396 |
* calls its <code>checkMulticast</code> method |
|
397 |
* with the <code>mcastaddr</code> argument |
|
398 |
* as its argument. |
|
399 |
* |
|
400 |
* @param mcastaddr is the multicast address to leave |
|
401 |
* @param netIf specifies the local interface or <i>null</i> to defer |
|
402 |
* to the interface set by |
|
403 |
* {@link MulticastSocket#setInterface(InetAddress)} or |
|
404 |
* {@link MulticastSocket#setNetworkInterface(NetworkInterface)} |
|
405 |
* @exception IOException if there is an error leaving |
|
406 |
* or when the address is not a multicast address. |
|
407 |
* @exception SecurityException if a security manager exists and its |
|
408 |
* <code>checkMulticast</code> method doesn't allow the operation. |
|
409 |
* @throws IllegalArgumentException if mcastaddr is null or is a |
|
410 |
* SocketAddress subclass not supported by this socket |
|
411 |
* |
|
412 |
* @see SecurityManager#checkMulticast(InetAddress) |
|
413 |
* @since 1.4 |
|
414 |
*/ |
|
415 |
public void leaveGroup(SocketAddress mcastaddr, NetworkInterface netIf) |
|
416 |
throws IOException { |
|
417 |
if (isClosed()) |
|
418 |
throw new SocketException("Socket is closed"); |
|
419 |
||
420 |
if (mcastaddr == null || !(mcastaddr instanceof InetSocketAddress)) |
|
421 |
throw new IllegalArgumentException("Unsupported address type"); |
|
422 |
||
423 |
if (oldImpl) |
|
424 |
throw new UnsupportedOperationException(); |
|
425 |
||
5180
8161f879d704
6893954: Subclasses of InetAddress may incorrectly interpret network addresses
michaelm
parents:
2
diff
changeset
|
426 |
checkAddress(((InetSocketAddress)mcastaddr).getAddress(), "leaveGroup"); |
2 | 427 |
SecurityManager security = System.getSecurityManager(); |
428 |
if (security != null) { |
|
429 |
security.checkMulticast(((InetSocketAddress)mcastaddr).getAddress()); |
|
430 |
} |
|
431 |
||
432 |
if (!((InetSocketAddress)mcastaddr).getAddress().isMulticastAddress()) { |
|
433 |
throw new SocketException("Not a multicast address"); |
|
434 |
} |
|
435 |
||
436 |
getImpl().leaveGroup(mcastaddr, netIf); |
|
437 |
} |
|
438 |
||
439 |
/** |
|
440 |
* Set the multicast network interface used by methods |
|
441 |
* whose behavior would be affected by the value of the |
|
442 |
* network interface. Useful for multihomed hosts. |
|
443 |
* @param inf the InetAddress |
|
444 |
* @exception SocketException if there is an error in |
|
445 |
* the underlying protocol, such as a TCP error. |
|
446 |
* @see #getInterface() |
|
447 |
*/ |
|
448 |
public void setInterface(InetAddress inf) throws SocketException { |
|
449 |
if (isClosed()) { |
|
450 |
throw new SocketException("Socket is closed"); |
|
451 |
} |
|
5180
8161f879d704
6893954: Subclasses of InetAddress may incorrectly interpret network addresses
michaelm
parents:
2
diff
changeset
|
452 |
checkAddress(inf, "setInterface"); |
2 | 453 |
synchronized (infLock) { |
454 |
getImpl().setOption(SocketOptions.IP_MULTICAST_IF, inf); |
|
455 |
infAddress = inf; |
|
456 |
} |
|
457 |
} |
|
458 |
||
459 |
/** |
|
460 |
* Retrieve the address of the network interface used for |
|
461 |
* multicast packets. |
|
462 |
* |
|
463 |
* @return An <code>InetAddress</code> representing |
|
464 |
* the address of the network interface used for |
|
465 |
* multicast packets. |
|
466 |
* |
|
467 |
* @exception SocketException if there is an error in |
|
468 |
* the underlying protocol, such as a TCP error. |
|
469 |
* |
|
470 |
* @see #setInterface(java.net.InetAddress) |
|
471 |
*/ |
|
472 |
public InetAddress getInterface() throws SocketException { |
|
473 |
if (isClosed()) { |
|
474 |
throw new SocketException("Socket is closed"); |
|
475 |
} |
|
476 |
synchronized (infLock) { |
|
477 |
InetAddress ia = |
|
478 |
(InetAddress)getImpl().getOption(SocketOptions.IP_MULTICAST_IF); |
|
479 |
||
480 |
/** |
|
481 |
* No previous setInterface or interface can be |
|
482 |
* set using setNetworkInterface |
|
483 |
*/ |
|
484 |
if (infAddress == null) { |
|
485 |
return ia; |
|
486 |
} |
|
487 |
||
488 |
/** |
|
489 |
* Same interface set with setInterface? |
|
490 |
*/ |
|
491 |
if (ia.equals(infAddress)) { |
|
492 |
return ia; |
|
493 |
} |
|
494 |
||
495 |
/** |
|
496 |
* Different InetAddress from what we set with setInterface |
|
497 |
* so enumerate the current interface to see if the |
|
498 |
* address set by setInterface is bound to this interface. |
|
499 |
*/ |
|
500 |
try { |
|
501 |
NetworkInterface ni = NetworkInterface.getByInetAddress(ia); |
|
10596
39b3a979e600
7090158: Networking Libraries don't build with javac -Werror
chegar
parents:
10437
diff
changeset
|
502 |
Enumeration<InetAddress> addrs = ni.getInetAddresses(); |
2 | 503 |
while (addrs.hasMoreElements()) { |
10596
39b3a979e600
7090158: Networking Libraries don't build with javac -Werror
chegar
parents:
10437
diff
changeset
|
504 |
InetAddress addr = addrs.nextElement(); |
2 | 505 |
if (addr.equals(infAddress)) { |
506 |
return infAddress; |
|
507 |
} |
|
508 |
} |
|
509 |
||
510 |
/** |
|
511 |
* No match so reset infAddress to indicate that the |
|
512 |
* interface has changed via means |
|
513 |
*/ |
|
514 |
infAddress = null; |
|
515 |
return ia; |
|
516 |
} catch (Exception e) { |
|
517 |
return ia; |
|
518 |
} |
|
519 |
} |
|
520 |
} |
|
521 |
||
522 |
/** |
|
523 |
* Specify the network interface for outgoing multicast datagrams |
|
524 |
* sent on this socket. |
|
525 |
* |
|
526 |
* @param netIf the interface |
|
527 |
* @exception SocketException if there is an error in |
|
528 |
* the underlying protocol, such as a TCP error. |
|
529 |
* @see #getNetworkInterface() |
|
530 |
* @since 1.4 |
|
531 |
*/ |
|
532 |
public void setNetworkInterface(NetworkInterface netIf) |
|
533 |
throws SocketException { |
|
534 |
||
535 |
synchronized (infLock) { |
|
536 |
getImpl().setOption(SocketOptions.IP_MULTICAST_IF2, netIf); |
|
537 |
infAddress = null; |
|
538 |
} |
|
539 |
} |
|
540 |
||
541 |
/** |
|
542 |
* Get the multicast network interface set. |
|
543 |
* |
|
544 |
* @exception SocketException if there is an error in |
|
545 |
* the underlying protocol, such as a TCP error. |
|
546 |
* @return the multicast <code>NetworkInterface</code> currently set |
|
547 |
* @see #setNetworkInterface(NetworkInterface) |
|
548 |
* @since 1.4 |
|
549 |
*/ |
|
550 |
public NetworkInterface getNetworkInterface() throws SocketException { |
|
551 |
NetworkInterface ni |
|
552 |
= (NetworkInterface)getImpl().getOption(SocketOptions.IP_MULTICAST_IF2); |
|
553 |
if (ni.getIndex() == 0) { |
|
554 |
InetAddress[] addrs = new InetAddress[1]; |
|
555 |
addrs[0] = InetAddress.anyLocalAddress(); |
|
556 |
return new NetworkInterface(addrs[0].getHostName(), 0, addrs); |
|
557 |
} else { |
|
558 |
return ni; |
|
559 |
} |
|
560 |
} |
|
561 |
||
562 |
/** |
|
563 |
* Disable/Enable local loopback of multicast datagrams |
|
564 |
* The option is used by the platform's networking code as a hint |
|
565 |
* for setting whether multicast data will be looped back to |
|
566 |
* the local socket. |
|
567 |
* |
|
568 |
* <p>Because this option is a hint, applications that want to |
|
569 |
* verify what loopback mode is set to should call |
|
570 |
* {@link #getLoopbackMode()} |
|
571 |
* @param disable <code>true</code> to disable the LoopbackMode |
|
572 |
* @throws SocketException if an error occurs while setting the value |
|
573 |
* @since 1.4 |
|
574 |
* @see #getLoopbackMode |
|
575 |
*/ |
|
576 |
public void setLoopbackMode(boolean disable) throws SocketException { |
|
577 |
getImpl().setOption(SocketOptions.IP_MULTICAST_LOOP, Boolean.valueOf(disable)); |
|
578 |
} |
|
579 |
||
580 |
/** |
|
581 |
* Get the setting for local loopback of multicast datagrams. |
|
582 |
* |
|
583 |
* @throws SocketException if an error occurs while getting the value |
|
584 |
* @return true if the LoopbackMode has been disabled |
|
585 |
* @since 1.4 |
|
586 |
* @see #setLoopbackMode |
|
587 |
*/ |
|
588 |
public boolean getLoopbackMode() throws SocketException { |
|
589 |
return ((Boolean)getImpl().getOption(SocketOptions.IP_MULTICAST_LOOP)).booleanValue(); |
|
590 |
} |
|
591 |
||
592 |
/** |
|
593 |
* Sends a datagram packet to the destination, with a TTL (time- |
|
594 |
* to-live) other than the default for the socket. This method |
|
595 |
* need only be used in instances where a particular TTL is desired; |
|
596 |
* otherwise it is preferable to set a TTL once on the socket, and |
|
597 |
* use that default TTL for all packets. This method does <B>not |
|
598 |
* </B> alter the default TTL for the socket. Its behavior may be |
|
599 |
* affected by <code>setInterface</code>. |
|
600 |
* |
|
601 |
* <p>If there is a security manager, this method first performs some |
|
602 |
* security checks. First, if <code>p.getAddress().isMulticastAddress()</code> |
|
603 |
* is true, this method calls the |
|
604 |
* security manager's <code>checkMulticast</code> method |
|
605 |
* with <code>p.getAddress()</code> and <code>ttl</code> as its arguments. |
|
606 |
* If the evaluation of that expression is false, |
|
607 |
* this method instead calls the security manager's |
|
608 |
* <code>checkConnect</code> method with arguments |
|
609 |
* <code>p.getAddress().getHostAddress()</code> and |
|
610 |
* <code>p.getPort()</code>. Each call to a security manager method |
|
611 |
* could result in a SecurityException if the operation is not allowed. |
|
612 |
* |
|
613 |
* @param p is the packet to be sent. The packet should contain |
|
614 |
* the destination multicast ip address and the data to be sent. |
|
615 |
* One does not need to be the member of the group to send |
|
616 |
* packets to a destination multicast address. |
|
617 |
* @param ttl optional time to live for multicast packet. |
|
618 |
* default ttl is 1. |
|
619 |
* |
|
620 |
* @exception IOException is raised if an error occurs i.e |
|
621 |
* error while setting ttl. |
|
622 |
* @exception SecurityException if a security manager exists and its |
|
623 |
* <code>checkMulticast</code> or <code>checkConnect</code> |
|
624 |
* method doesn't allow the send. |
|
625 |
* |
|
626 |
* @deprecated Use the following code or its equivalent instead: |
|
627 |
* ...... |
|
628 |
* int ttl = mcastSocket.getTimeToLive(); |
|
629 |
* mcastSocket.setTimeToLive(newttl); |
|
630 |
* mcastSocket.send(p); |
|
631 |
* mcastSocket.setTimeToLive(ttl); |
|
632 |
* ...... |
|
633 |
* |
|
634 |
* @see DatagramSocket#send |
|
635 |
* @see DatagramSocket#receive |
|
636 |
* @see SecurityManager#checkMulticast(java.net.InetAddress, byte) |
|
637 |
* @see SecurityManager#checkConnect |
|
638 |
*/ |
|
639 |
@Deprecated |
|
640 |
public void send(DatagramPacket p, byte ttl) |
|
641 |
throws IOException { |
|
642 |
if (isClosed()) |
|
643 |
throw new SocketException("Socket is closed"); |
|
5180
8161f879d704
6893954: Subclasses of InetAddress may incorrectly interpret network addresses
michaelm
parents:
2
diff
changeset
|
644 |
checkAddress(p.getAddress(), "send"); |
2 | 645 |
synchronized(ttlLock) { |
646 |
synchronized(p) { |
|
647 |
if (connectState == ST_NOT_CONNECTED) { |
|
648 |
// Security manager makes sure that the multicast address |
|
649 |
// is allowed one and that the ttl used is less |
|
650 |
// than the allowed maxttl. |
|
651 |
SecurityManager security = System.getSecurityManager(); |
|
652 |
if (security != null) { |
|
653 |
if (p.getAddress().isMulticastAddress()) { |
|
654 |
security.checkMulticast(p.getAddress(), ttl); |
|
655 |
} else { |
|
656 |
security.checkConnect(p.getAddress().getHostAddress(), |
|
657 |
p.getPort()); |
|
658 |
} |
|
659 |
} |
|
660 |
} else { |
|
661 |
// we're connected |
|
662 |
InetAddress packetAddress = null; |
|
663 |
packetAddress = p.getAddress(); |
|
664 |
if (packetAddress == null) { |
|
665 |
p.setAddress(connectedAddress); |
|
666 |
p.setPort(connectedPort); |
|
667 |
} else if ((!packetAddress.equals(connectedAddress)) || |
|
668 |
p.getPort() != connectedPort) { |
|
669 |
throw new SecurityException("connected address and packet address" + |
|
670 |
" differ"); |
|
671 |
} |
|
672 |
} |
|
673 |
byte dttl = getTTL(); |
|
674 |
try { |
|
675 |
if (ttl != dttl) { |
|
676 |
// set the ttl |
|
677 |
getImpl().setTTL(ttl); |
|
678 |
} |
|
679 |
// call the datagram method to send |
|
680 |
getImpl().send(p); |
|
681 |
} finally { |
|
682 |
// set it back to default |
|
683 |
if (ttl != dttl) { |
|
684 |
getImpl().setTTL(dttl); |
|
685 |
} |
|
686 |
} |
|
687 |
} // synch p |
|
688 |
} //synch ttl |
|
689 |
} //method |
|
690 |
} |