41 import sun.net.ConnectionResetException; |
41 import sun.net.ConnectionResetException; |
42 import sun.net.NetHooks; |
42 import sun.net.NetHooks; |
43 import sun.net.PlatformSocketImpl; |
43 import sun.net.PlatformSocketImpl; |
44 import sun.net.ResourceManager; |
44 import sun.net.ResourceManager; |
45 import sun.net.ext.ExtendedSocketOptions; |
45 import sun.net.ext.ExtendedSocketOptions; |
|
46 import sun.net.util.IPAddressUtil; |
46 import sun.net.util.SocketExceptions; |
47 import sun.net.util.SocketExceptions; |
47 |
48 |
48 /** |
49 /** |
49 * Default Socket Implementation. This implementation does |
50 * Default Socket Implementation. This implementation does |
50 * not implement any security checks. |
51 * not implement any security checks. |
91 |
92 |
92 /** |
93 /** |
93 * Load net library into runtime. |
94 * Load net library into runtime. |
94 */ |
95 */ |
95 static { |
96 static { |
96 java.security.AccessController.doPrivileged( |
97 jdk.internal.loader.BootLoader.loadLibrary("net"); |
97 new java.security.PrivilegedAction<>() { |
|
98 public Void run() { |
|
99 System.loadLibrary("net"); |
|
100 return null; |
|
101 } |
|
102 }); |
|
103 } |
98 } |
104 |
99 |
105 private static volatile boolean checkedReusePort; |
100 private static volatile boolean checkedReusePort; |
106 private static volatile boolean isReusePortAvailable; |
101 private static volatile boolean isReusePortAvailable; |
107 |
102 |
155 throws UnknownHostException, IOException |
150 throws UnknownHostException, IOException |
156 { |
151 { |
157 boolean connected = false; |
152 boolean connected = false; |
158 try { |
153 try { |
159 InetAddress address = InetAddress.getByName(host); |
154 InetAddress address = InetAddress.getByName(host); |
|
155 // recording this.address as supplied by caller before calling connect |
|
156 this.address = address; |
160 this.port = port; |
157 this.port = port; |
161 this.address = address; |
158 if (address.isLinkLocalAddress()) { |
|
159 address = IPAddressUtil.toScopedAddress(address); |
|
160 } |
162 |
161 |
163 connectToAddress(address, port, timeout); |
162 connectToAddress(address, port, timeout); |
164 connected = true; |
163 connected = true; |
165 } finally { |
164 } finally { |
166 if (!connected) { |
165 if (!connected) { |
180 * the specified port. |
179 * the specified port. |
181 * @param address the address |
180 * @param address the address |
182 * @param port the specified port |
181 * @param port the specified port |
183 */ |
182 */ |
184 protected void connect(InetAddress address, int port) throws IOException { |
183 protected void connect(InetAddress address, int port) throws IOException { |
|
184 // recording this.address as supplied by caller before calling connect |
|
185 this.address = address; |
185 this.port = port; |
186 this.port = port; |
186 this.address = address; |
187 if (address.isLinkLocalAddress()) { |
|
188 address = IPAddressUtil.toScopedAddress(address); |
|
189 } |
187 |
190 |
188 try { |
191 try { |
189 connectToAddress(address, port, timeout); |
192 connectToAddress(address, port, timeout); |
190 isConnected = true; |
193 isConnected = true; |
191 return; |
194 return; |
213 if (address == null || !(address instanceof InetSocketAddress)) |
216 if (address == null || !(address instanceof InetSocketAddress)) |
214 throw new IllegalArgumentException("unsupported address type"); |
217 throw new IllegalArgumentException("unsupported address type"); |
215 InetSocketAddress addr = (InetSocketAddress) address; |
218 InetSocketAddress addr = (InetSocketAddress) address; |
216 if (addr.isUnresolved()) |
219 if (addr.isUnresolved()) |
217 throw new UnknownHostException(addr.getHostName()); |
220 throw new UnknownHostException(addr.getHostName()); |
|
221 // recording this.address as supplied by caller before calling connect |
|
222 InetAddress ia = addr.getAddress(); |
|
223 this.address = ia; |
218 this.port = addr.getPort(); |
224 this.port = addr.getPort(); |
219 this.address = addr.getAddress(); |
225 if (ia.isLinkLocalAddress()) { |
220 |
226 ia = IPAddressUtil.toScopedAddress(ia); |
221 connectToAddress(this.address, port, timeout); |
227 } |
|
228 connectToAddress(ia, port, timeout); |
222 connected = true; |
229 connected = true; |
223 } finally { |
230 } finally { |
224 if (!connected) { |
231 if (!connected) { |
225 try { |
232 try { |
226 close(); |
233 close(); |
444 } else if (name == StandardSocketOptions.SO_REUSEADDR) { |
451 } else if (name == StandardSocketOptions.SO_REUSEADDR) { |
445 setOption(SocketOptions.SO_REUSEADDR, value); |
452 setOption(SocketOptions.SO_REUSEADDR, value); |
446 } else if (name == StandardSocketOptions.SO_REUSEPORT) { |
453 } else if (name == StandardSocketOptions.SO_REUSEPORT) { |
447 setOption(SocketOptions.SO_REUSEPORT, value); |
454 setOption(SocketOptions.SO_REUSEPORT, value); |
448 } else if (name == StandardSocketOptions.SO_LINGER ) { |
455 } else if (name == StandardSocketOptions.SO_LINGER ) { |
449 setOption(SocketOptions.SO_LINGER, value); |
456 if (((Integer)value).intValue() < 0) |
|
457 setOption(SocketOptions.SO_LINGER, false); |
|
458 else |
|
459 setOption(SocketOptions.SO_LINGER, value); |
450 } else if (name == StandardSocketOptions.IP_TOS) { |
460 } else if (name == StandardSocketOptions.IP_TOS) { |
451 int i = ((Integer)value).intValue(); |
461 int i = ((Integer)value).intValue(); |
452 if (i < 0 || i > 255) |
462 if (i < 0 || i > 255) |
453 throw new IllegalArgumentException("Invalid IP_TOS value: " + value); |
463 throw new IllegalArgumentException("Invalid IP_TOS value: " + value); |
454 setOption(SocketOptions.IP_TOS, value); |
464 setOption(SocketOptions.IP_TOS, value); |
480 } else if (name == StandardSocketOptions.SO_REUSEADDR) { |
490 } else if (name == StandardSocketOptions.SO_REUSEADDR) { |
481 return (T)getOption(SocketOptions.SO_REUSEADDR); |
491 return (T)getOption(SocketOptions.SO_REUSEADDR); |
482 } else if (name == StandardSocketOptions.SO_REUSEPORT) { |
492 } else if (name == StandardSocketOptions.SO_REUSEPORT) { |
483 return (T)getOption(SocketOptions.SO_REUSEPORT); |
493 return (T)getOption(SocketOptions.SO_REUSEPORT); |
484 } else if (name == StandardSocketOptions.SO_LINGER) { |
494 } else if (name == StandardSocketOptions.SO_LINGER) { |
485 return (T)getOption(SocketOptions.SO_LINGER); |
495 Object value = getOption(SocketOptions.SO_LINGER); |
|
496 if (value instanceof Boolean) { |
|
497 assert ((Boolean)value).booleanValue() == false; |
|
498 value = -1; |
|
499 } |
|
500 return (T)value; |
486 } else if (name == StandardSocketOptions.IP_TOS) { |
501 } else if (name == StandardSocketOptions.IP_TOS) { |
487 return (T)getOption(SocketOptions.IP_TOS); |
502 return (T)getOption(SocketOptions.IP_TOS); |
488 } else if (name == StandardSocketOptions.TCP_NODELAY) { |
503 } else if (name == StandardSocketOptions.TCP_NODELAY) { |
489 return (T)getOption(SocketOptions.TCP_NODELAY); |
504 return (T)getOption(SocketOptions.TCP_NODELAY); |
490 } else if (extendedOptions.isOptionSupported(name)) { |
505 } else if (extendedOptions.isOptionSupported(name)) { |
535 { |
550 { |
536 synchronized (fdLock) { |
551 synchronized (fdLock) { |
537 if (!closePending && !isBound) { |
552 if (!closePending && !isBound) { |
538 NetHooks.beforeTcpBind(fd, address, lport); |
553 NetHooks.beforeTcpBind(fd, address, lport); |
539 } |
554 } |
|
555 } |
|
556 if (address.isLinkLocalAddress()) { |
|
557 address = IPAddressUtil.toScopedAddress(address); |
540 } |
558 } |
541 socketBind(address, lport); |
559 socketBind(address, lport); |
542 isBound = true; |
560 isBound = true; |
543 } |
561 } |
544 |
562 |