author | michaelm |
Wed, 13 Feb 2013 10:40:31 +0000 | |
changeset 16870 | f35b2bd19761 |
parent 10704 | db5816a6e8c7 |
child 18192 | fa6bd0992104 |
permissions | -rw-r--r-- |
2 | 1 |
/* |
9035
1255eb81cc2f
7033660: Update copyright year to 2011 on any files changed in 2011
ohair
parents:
8819
diff
changeset
|
2 |
* Copyright (c) 2007, 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 |
package java.net; |
|
26 |
||
27 |
import java.io.IOException; |
|
28 |
import java.io.FileDescriptor; |
|
8819 | 29 |
import sun.net.ResourceManager; |
2 | 30 |
|
31 |
/** |
|
32 |
* This class defines the plain DatagramSocketImpl that is used for all |
|
33 |
* Windows versions lower than Vista. It adds support for IPv6 on |
|
34 |
* these platforms where available. |
|
35 |
* |
|
36 |
* For backward compatibility windows platforms that do not have IPv6 |
|
37 |
* support also use this implementation, and fd1 gets set to null |
|
38 |
* during socket creation. |
|
39 |
* |
|
40 |
* @author Chris Hegarty |
|
41 |
*/ |
|
42 |
||
43 |
class TwoStacksPlainDatagramSocketImpl extends AbstractPlainDatagramSocketImpl |
|
44 |
{ |
|
45 |
/* Used for IPv6 on Windows only */ |
|
46 |
private FileDescriptor fd1; |
|
47 |
||
48 |
/* |
|
49 |
* Needed for ipv6 on windows because we need to know |
|
50 |
* if the socket was bound to ::0 or 0.0.0.0, when a caller |
|
51 |
* asks for it. In this case, both sockets are used, but we |
|
52 |
* don't know whether the caller requested ::0 or 0.0.0.0 |
|
53 |
* and need to remember it here. |
|
54 |
*/ |
|
55 |
private InetAddress anyLocalBoundAddr=null; |
|
56 |
||
57 |
private int fduse=-1; /* saved between peek() and receive() calls */ |
|
58 |
||
59 |
/* saved between successive calls to receive, if data is detected |
|
60 |
* on both sockets at same time. To ensure that one socket is not |
|
61 |
* starved, they rotate using this field |
|
62 |
*/ |
|
63 |
private int lastfd=-1; |
|
64 |
||
65 |
static { |
|
66 |
init(); |
|
67 |
} |
|
68 |
||
69 |
protected synchronized void create() throws SocketException { |
|
70 |
fd1 = new FileDescriptor(); |
|
10599
79e95f5aaffb
7091369: DatagramSocket/Limit.java failing on 8 and 7u2
michaelm
parents:
10596
diff
changeset
|
71 |
try { |
79e95f5aaffb
7091369: DatagramSocket/Limit.java failing on 8 and 7u2
michaelm
parents:
10596
diff
changeset
|
72 |
super.create(); |
10704
db5816a6e8c7
7098719: -Dsun.net.maxDatagramSockets and Socket constructor does not work correctly with System.gc()
chegar
parents:
10693
diff
changeset
|
73 |
} catch (SocketException e) { |
10599
79e95f5aaffb
7091369: DatagramSocket/Limit.java failing on 8 and 7u2
michaelm
parents:
10596
diff
changeset
|
74 |
fd1 = null; |
79e95f5aaffb
7091369: DatagramSocket/Limit.java failing on 8 and 7u2
michaelm
parents:
10596
diff
changeset
|
75 |
throw e; |
79e95f5aaffb
7091369: DatagramSocket/Limit.java failing on 8 and 7u2
michaelm
parents:
10596
diff
changeset
|
76 |
} |
2 | 77 |
} |
78 |
||
79 |
protected synchronized void bind(int lport, InetAddress laddr) |
|
80 |
throws SocketException { |
|
81 |
super.bind(lport, laddr); |
|
82 |
if (laddr.isAnyLocalAddress()) { |
|
83 |
anyLocalBoundAddr = laddr; |
|
84 |
} |
|
85 |
} |
|
86 |
||
87 |
protected synchronized void receive(DatagramPacket p) |
|
88 |
throws IOException { |
|
89 |
try { |
|
90 |
receive0(p); |
|
91 |
} finally { |
|
92 |
fduse = -1; |
|
93 |
} |
|
94 |
} |
|
95 |
||
96 |
public Object getOption(int optID) throws SocketException { |
|
97 |
if (isClosed()) { |
|
98 |
throw new SocketException("Socket Closed"); |
|
99 |
} |
|
100 |
||
101 |
if (optID == SO_BINDADDR) { |
|
10693
6e9ebed2e783
7084030: DatagramSocket.getLocalAddress inconsistent on XP/2003 when IPv6 enabled and socket is connected
chegar
parents:
10599
diff
changeset
|
102 |
if ((fd != null && fd1 != null) && !connected) { |
2 | 103 |
return anyLocalBoundAddr; |
104 |
} |
|
16870 | 105 |
int family = connectedAddress == null ? -1 : connectedAddress.holder().getFamily(); |
10693
6e9ebed2e783
7084030: DatagramSocket.getLocalAddress inconsistent on XP/2003 when IPv6 enabled and socket is connected
chegar
parents:
10599
diff
changeset
|
106 |
return socketLocalAddress(family); |
2 | 107 |
} else |
108 |
return super.getOption(optID); |
|
109 |
} |
|
110 |
||
111 |
protected boolean isClosed() { |
|
112 |
return (fd == null && fd1 == null) ? true : false; |
|
113 |
} |
|
114 |
||
115 |
protected void close() { |
|
116 |
if (fd != null || fd1 != null) { |
|
117 |
datagramSocketClose(); |
|
8819 | 118 |
ResourceManager.afterUdpClose(); |
2 | 119 |
fd = null; |
120 |
fd1 = null; |
|
121 |
} |
|
122 |
} |
|
123 |
||
124 |
/* Native methods */ |
|
125 |
||
126 |
protected synchronized native void bind0(int lport, InetAddress laddr) |
|
127 |
throws SocketException; |
|
128 |
||
129 |
protected native void send(DatagramPacket p) throws IOException; |
|
130 |
||
131 |
protected synchronized native int peek(InetAddress i) throws IOException; |
|
132 |
||
133 |
protected synchronized native int peekData(DatagramPacket p) throws IOException; |
|
134 |
||
135 |
protected synchronized native void receive0(DatagramPacket p) |
|
136 |
throws IOException; |
|
137 |
||
138 |
protected native void setTimeToLive(int ttl) throws IOException; |
|
139 |
||
140 |
protected native int getTimeToLive() throws IOException; |
|
141 |
||
10596
39b3a979e600
7090158: Networking Libraries don't build with javac -Werror
chegar
parents:
9035
diff
changeset
|
142 |
@Deprecated |
2 | 143 |
protected native void setTTL(byte ttl) throws IOException; |
144 |
||
10596
39b3a979e600
7090158: Networking Libraries don't build with javac -Werror
chegar
parents:
9035
diff
changeset
|
145 |
@Deprecated |
2 | 146 |
protected native byte getTTL() throws IOException; |
147 |
||
148 |
protected native void join(InetAddress inetaddr, NetworkInterface netIf) |
|
149 |
throws IOException; |
|
150 |
||
151 |
protected native void leave(InetAddress inetaddr, NetworkInterface netIf) |
|
152 |
throws IOException; |
|
153 |
||
154 |
protected native void datagramSocketCreate() throws SocketException; |
|
155 |
||
156 |
protected native void datagramSocketClose(); |
|
157 |
||
158 |
protected native void socketSetOption(int opt, Object val) |
|
159 |
throws SocketException; |
|
160 |
||
161 |
protected native Object socketGetOption(int opt) throws SocketException; |
|
162 |
||
163 |
protected native void connect0(InetAddress address, int port) throws SocketException; |
|
164 |
||
10693
6e9ebed2e783
7084030: DatagramSocket.getLocalAddress inconsistent on XP/2003 when IPv6 enabled and socket is connected
chegar
parents:
10599
diff
changeset
|
165 |
protected native Object socketLocalAddress(int family) throws SocketException; |
6e9ebed2e783
7084030: DatagramSocket.getLocalAddress inconsistent on XP/2003 when IPv6 enabled and socket is connected
chegar
parents:
10599
diff
changeset
|
166 |
|
2 | 167 |
protected native void disconnect0(int family); |
168 |
||
169 |
/** |
|
170 |
* Perform class load-time initializations. |
|
171 |
*/ |
|
172 |
private native static void init(); |
|
173 |
} |