PageRenderTime 48ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/java/src/IceInternal/Network.java

https://bitbucket.org/cleto/zeroc-ice-package
Java | 1296 lines | 1092 code | 110 blank | 94 comment | 148 complexity | 7bec660cc20748c0656e76fa33e72de6 MD5 | raw file
Possible License(s): BSD-3-Clause-No-Nuclear-License-2014, GPL-2.0, BSD-3-Clause
  1. // **********************************************************************
  2. //
  3. // Copyright (c) 2003-2013 ZeroC, Inc. All rights reserved.
  4. //
  5. // This copy of Ice is licensed to you under the terms described in the
  6. // ICE_LICENSE file included in this distribution.
  7. //
  8. // **********************************************************************
  9. package IceInternal;
  10. public final class Network
  11. {
  12. // ProtocolSupport
  13. public final static int EnableIPv4 = 0;
  14. public final static int EnableIPv6 = 1;
  15. public final static int EnableBoth = 2;
  16. public static boolean
  17. connectionRefused(java.net.ConnectException ex)
  18. {
  19. //
  20. // The JDK raises a generic ConnectException when the server
  21. // actively refuses a connection. Unfortunately, our only
  22. // choice is to search the exception message for
  23. // distinguishing phrases.
  24. //
  25. String msg = ex.getMessage();
  26. if(msg != null)
  27. {
  28. msg = msg.toLowerCase();
  29. final String[] msgs =
  30. {
  31. "connection refused", // ECONNREFUSED
  32. "remote host refused an attempted connect operation" // ECONNREFUSED (AIX JDK 1.4.2)
  33. };
  34. for(String m : msgs)
  35. {
  36. if(msg.indexOf(m) != -1)
  37. {
  38. return true;
  39. }
  40. }
  41. }
  42. return false;
  43. }
  44. public static boolean
  45. noMoreFds(java.lang.Throwable ex)
  46. {
  47. String msg = ex.getMessage();
  48. if(msg != null)
  49. {
  50. msg = msg.toLowerCase();
  51. final String[] msgs =
  52. {
  53. "too many open files", // EMFILE
  54. "file table overflow", // ENFILE
  55. "too many open files in system" // ENFILE
  56. };
  57. for(String m : msgs)
  58. {
  59. if(msg.indexOf(m) != -1)
  60. {
  61. return true;
  62. }
  63. }
  64. }
  65. return false;
  66. }
  67. public static java.nio.channels.SocketChannel
  68. createTcpSocket()
  69. {
  70. try
  71. {
  72. java.nio.channels.SocketChannel fd = java.nio.channels.SocketChannel.open();
  73. java.net.Socket socket = fd.socket();
  74. socket.setTcpNoDelay(true);
  75. socket.setKeepAlive(true);
  76. return fd;
  77. }
  78. catch(java.io.IOException ex)
  79. {
  80. throw new Ice.SocketException(ex);
  81. }
  82. }
  83. public static java.nio.channels.ServerSocketChannel
  84. createTcpServerSocket()
  85. {
  86. try
  87. {
  88. java.nio.channels.ServerSocketChannel fd = java.nio.channels.ServerSocketChannel.open();
  89. //
  90. // It's not possible to set TCP_NODELAY or KEEP_ALIVE
  91. // on a server socket in Java
  92. //
  93. //java.net.Socket socket = fd.socket();
  94. //socket.setTcpNoDelay(true);
  95. //socket.setKeepAlive(true);
  96. return fd;
  97. }
  98. catch(java.io.IOException ex)
  99. {
  100. throw new Ice.SocketException(ex);
  101. }
  102. }
  103. public static java.nio.channels.DatagramChannel
  104. createUdpSocket(java.net.InetSocketAddress addr)
  105. {
  106. try
  107. {
  108. //
  109. // Use reflection so this code still compiles with older JDK versions.
  110. // java.net.StandardProtocolFamily is new in JDK 1.7
  111. //
  112. Class<?> c = Util.findClass("java.net.StandardProtocolFamily", null);
  113. if(addr.getAddress().isMulticastAddress() && c != null)
  114. {
  115. //
  116. // For multicast sockets with JDK 7 we must use the open overload that accepts
  117. // ProtocolFamily and specify the ProtocolFamily that corresponds to the address
  118. // type of the multicast groups that the channel will join.
  119. //
  120. String family = "INET";
  121. if(addr.getAddress() instanceof java.net.Inet6Address)
  122. {
  123. family = "INET6";
  124. }
  125. java.lang.reflect.Method valueOf = c.getDeclaredMethod("valueOf", new Class<?>[]{String.class});
  126. Object[] args = new Object[]{valueOf.invoke(null, new Object[]{family})};
  127. java.lang.reflect.Method open = java.nio.channels.DatagramChannel.class.getDeclaredMethod(
  128. "open", new Class<?>[]{Util.findClass("java.net.ProtocolFamily", null)});
  129. return (java.nio.channels.DatagramChannel)open.invoke(null, args);
  130. }
  131. else
  132. {
  133. return java.nio.channels.DatagramChannel.open();
  134. }
  135. }
  136. catch(IllegalAccessException ex)
  137. {
  138. throw new Ice.SocketException(ex);
  139. }
  140. catch(java.lang.reflect.InvocationTargetException ex)
  141. {
  142. throw new Ice.SocketException(ex);
  143. }
  144. catch(NoSuchMethodException ex)
  145. {
  146. throw new Ice.SocketException(ex);
  147. }
  148. catch(java.io.IOException ex)
  149. {
  150. throw new Ice.SocketException(ex);
  151. }
  152. }
  153. public static void
  154. closeSocketNoThrow(java.nio.channels.SelectableChannel fd)
  155. {
  156. try
  157. {
  158. fd.close();
  159. }
  160. catch(java.io.IOException ex)
  161. {
  162. // Ignore
  163. }
  164. }
  165. public static void
  166. closeSocket(java.nio.channels.SelectableChannel fd)
  167. {
  168. try
  169. {
  170. fd.close();
  171. }
  172. catch(java.io.IOException ex)
  173. {
  174. throw new Ice.SocketException(ex);
  175. }
  176. }
  177. public static void
  178. setBlock(java.nio.channels.SelectableChannel fd, boolean block)
  179. {
  180. try
  181. {
  182. fd.configureBlocking(block);
  183. }
  184. catch(java.io.IOException ex)
  185. {
  186. closeSocketNoThrow(fd);
  187. throw new Ice.SocketException(ex);
  188. }
  189. }
  190. public static void
  191. setReuseAddress(java.nio.channels.DatagramChannel fd, boolean reuse)
  192. {
  193. try
  194. {
  195. fd.socket().setReuseAddress(reuse);
  196. }
  197. catch(java.io.IOException ex)
  198. {
  199. closeSocketNoThrow(fd);
  200. throw new Ice.SocketException(ex);
  201. }
  202. }
  203. public static void
  204. setReuseAddress(java.nio.channels.ServerSocketChannel fd, boolean reuse)
  205. {
  206. try
  207. {
  208. fd.socket().setReuseAddress(reuse);
  209. }
  210. catch(java.io.IOException ex)
  211. {
  212. closeSocketNoThrow(fd);
  213. throw new Ice.SocketException(ex);
  214. }
  215. }
  216. public static java.net.InetSocketAddress
  217. doBind(java.nio.channels.ServerSocketChannel fd, java.net.InetSocketAddress addr, int backlog)
  218. {
  219. try
  220. {
  221. java.net.ServerSocket sock = fd.socket();
  222. sock.bind(addr, backlog);
  223. return (java.net.InetSocketAddress)sock.getLocalSocketAddress();
  224. }
  225. catch(java.io.IOException ex)
  226. {
  227. closeSocketNoThrow(fd);
  228. throw new Ice.SocketException(ex);
  229. }
  230. }
  231. public static java.net.InetSocketAddress
  232. doBind(java.nio.channels.DatagramChannel fd, java.net.InetSocketAddress addr)
  233. {
  234. try
  235. {
  236. java.net.DatagramSocket sock = fd.socket();
  237. sock.bind(addr);
  238. return (java.net.InetSocketAddress)sock.getLocalSocketAddress();
  239. }
  240. catch(java.io.IOException ex)
  241. {
  242. closeSocketNoThrow(fd);
  243. throw new Ice.SocketException(ex);
  244. }
  245. }
  246. public static java.nio.channels.SocketChannel
  247. doAccept(java.nio.channels.ServerSocketChannel afd)
  248. {
  249. java.nio.channels.SocketChannel fd = null;
  250. while(true)
  251. {
  252. try
  253. {
  254. fd = afd.accept();
  255. break;
  256. }
  257. catch(java.io.IOException ex)
  258. {
  259. if(interrupted(ex))
  260. {
  261. continue;
  262. }
  263. throw new Ice.SocketException(ex);
  264. }
  265. }
  266. try
  267. {
  268. java.net.Socket socket = fd.socket();
  269. socket.setTcpNoDelay(true);
  270. socket.setKeepAlive(true);
  271. }
  272. catch(java.io.IOException ex)
  273. {
  274. throw new Ice.SocketException(ex);
  275. }
  276. return fd;
  277. }
  278. public static boolean
  279. doConnect(java.nio.channels.SocketChannel fd, java.net.InetSocketAddress addr)
  280. {
  281. try
  282. {
  283. if(!fd.connect(addr))
  284. {
  285. return false;
  286. }
  287. }
  288. catch(java.net.ConnectException ex)
  289. {
  290. closeSocketNoThrow(fd);
  291. if(connectionRefused(ex))
  292. {
  293. throw new Ice.ConnectionRefusedException(ex);
  294. }
  295. else
  296. {
  297. throw new Ice.ConnectFailedException(ex);
  298. }
  299. }
  300. catch(java.io.IOException ex)
  301. {
  302. closeSocketNoThrow(fd);
  303. throw new Ice.SocketException(ex);
  304. }
  305. catch(java.lang.SecurityException ex)
  306. {
  307. closeSocketNoThrow(fd);
  308. throw new Ice.SocketException(ex);
  309. }
  310. if(System.getProperty("os.name").equals("Linux"))
  311. {
  312. //
  313. // Prevent self connect (self connect happens on Linux when a client tries to connect to
  314. // a server which was just deactivated if the client socket re-uses the same ephemeral
  315. // port as the server).
  316. //
  317. if(addr.equals(fd.socket().getLocalSocketAddress()))
  318. {
  319. closeSocketNoThrow(fd);
  320. throw new Ice.ConnectionRefusedException();
  321. }
  322. }
  323. return true;
  324. }
  325. public static void
  326. doFinishConnect(java.nio.channels.SocketChannel fd)
  327. {
  328. //
  329. // Note: we don't close the socket if there's an exception. It's the responsibility
  330. // of the caller to do so.
  331. //
  332. try
  333. {
  334. if(!fd.finishConnect())
  335. {
  336. throw new Ice.ConnectFailedException();
  337. }
  338. if(System.getProperty("os.name").equals("Linux"))
  339. {
  340. //
  341. // Prevent self connect (self connect happens on Linux when a client tries to connect to
  342. // a server which was just deactivated if the client socket re-uses the same ephemeral
  343. // port as the server).
  344. //
  345. java.net.SocketAddress addr = fd.socket().getRemoteSocketAddress();
  346. if(addr != null && addr.equals(fd.socket().getLocalSocketAddress()))
  347. {
  348. throw new Ice.ConnectionRefusedException();
  349. }
  350. }
  351. }
  352. catch(java.net.ConnectException ex)
  353. {
  354. if(connectionRefused(ex))
  355. {
  356. throw new Ice.ConnectionRefusedException(ex);
  357. }
  358. else
  359. {
  360. throw new Ice.ConnectFailedException(ex);
  361. }
  362. }
  363. catch(java.io.IOException ex)
  364. {
  365. throw new Ice.SocketException(ex);
  366. }
  367. }
  368. public static void
  369. doConnect(java.nio.channels.DatagramChannel fd, java.net.InetSocketAddress addr)
  370. {
  371. try
  372. {
  373. fd.connect(addr);
  374. }
  375. catch(java.net.ConnectException ex)
  376. {
  377. closeSocketNoThrow(fd);
  378. if(connectionRefused(ex))
  379. {
  380. throw new Ice.ConnectionRefusedException(ex);
  381. }
  382. else
  383. {
  384. throw new Ice.ConnectFailedException(ex);
  385. }
  386. }
  387. catch(java.io.IOException ex)
  388. {
  389. closeSocketNoThrow(fd);
  390. throw new Ice.SocketException(ex);
  391. }
  392. }
  393. public static java.nio.channels.SocketChannel
  394. doAccept(java.nio.channels.ServerSocketChannel fd, int timeout)
  395. {
  396. java.nio.channels.SocketChannel result = null;
  397. while(result == null)
  398. {
  399. try
  400. {
  401. result = fd.accept();
  402. if(result == null)
  403. {
  404. java.nio.channels.Selector selector = java.nio.channels.Selector.open();
  405. try
  406. {
  407. while(true)
  408. {
  409. try
  410. {
  411. fd.register(selector, java.nio.channels.SelectionKey.OP_ACCEPT);
  412. int n;
  413. if(timeout > 0)
  414. {
  415. n = selector.select(timeout);
  416. }
  417. else if(timeout == 0)
  418. {
  419. n = selector.selectNow();
  420. }
  421. else
  422. {
  423. n = selector.select();
  424. }
  425. if(n == 0)
  426. {
  427. throw new Ice.TimeoutException();
  428. }
  429. break;
  430. }
  431. catch(java.io.IOException ex)
  432. {
  433. if(interrupted(ex))
  434. {
  435. continue;
  436. }
  437. throw new Ice.SocketException(ex);
  438. }
  439. }
  440. }
  441. finally
  442. {
  443. try
  444. {
  445. selector.close();
  446. }
  447. catch(java.io.IOException ex)
  448. {
  449. // Ignore
  450. }
  451. }
  452. }
  453. }
  454. catch(java.io.IOException ex)
  455. {
  456. if(interrupted(ex))
  457. {
  458. continue;
  459. }
  460. throw new Ice.SocketException(ex);
  461. }
  462. }
  463. try
  464. {
  465. java.net.Socket socket = result.socket();
  466. socket.setTcpNoDelay(true);
  467. socket.setKeepAlive(true);
  468. }
  469. catch(java.io.IOException ex)
  470. {
  471. throw new Ice.SocketException(ex);
  472. }
  473. return result;
  474. }
  475. public static void
  476. setSendBufferSize(java.nio.channels.SocketChannel fd, int size)
  477. {
  478. try
  479. {
  480. java.net.Socket socket = fd.socket();
  481. socket.setSendBufferSize(size);
  482. }
  483. catch(java.io.IOException ex)
  484. {
  485. closeSocketNoThrow(fd);
  486. throw new Ice.SocketException(ex);
  487. }
  488. }
  489. public static int
  490. getSendBufferSize(java.nio.channels.SocketChannel fd)
  491. {
  492. int size;
  493. try
  494. {
  495. java.net.Socket socket = fd.socket();
  496. size = socket.getSendBufferSize();
  497. }
  498. catch(java.io.IOException ex)
  499. {
  500. closeSocketNoThrow(fd);
  501. throw new Ice.SocketException(ex);
  502. }
  503. return size;
  504. }
  505. public static void
  506. setRecvBufferSize(java.nio.channels.SocketChannel fd, int size)
  507. {
  508. try
  509. {
  510. java.net.Socket socket = fd.socket();
  511. socket.setReceiveBufferSize(size);
  512. }
  513. catch(java.io.IOException ex)
  514. {
  515. closeSocketNoThrow(fd);
  516. throw new Ice.SocketException(ex);
  517. }
  518. }
  519. public static int
  520. getRecvBufferSize(java.nio.channels.SocketChannel fd)
  521. {
  522. int size;
  523. try
  524. {
  525. java.net.Socket socket = fd.socket();
  526. size = socket.getReceiveBufferSize();
  527. }
  528. catch(java.io.IOException ex)
  529. {
  530. closeSocketNoThrow(fd);
  531. throw new Ice.SocketException(ex);
  532. }
  533. return size;
  534. }
  535. public static void
  536. setRecvBufferSize(java.nio.channels.ServerSocketChannel fd, int size)
  537. {
  538. try
  539. {
  540. java.net.ServerSocket socket = fd.socket();
  541. socket.setReceiveBufferSize(size);
  542. }
  543. catch(java.io.IOException ex)
  544. {
  545. closeSocketNoThrow(fd);
  546. throw new Ice.SocketException(ex);
  547. }
  548. }
  549. public static int
  550. getRecvBufferSize(java.nio.channels.ServerSocketChannel fd)
  551. {
  552. int size;
  553. try
  554. {
  555. java.net.ServerSocket socket = fd.socket();
  556. size = socket.getReceiveBufferSize();
  557. }
  558. catch(java.io.IOException ex)
  559. {
  560. closeSocketNoThrow(fd);
  561. throw new Ice.SocketException(ex);
  562. }
  563. return size;
  564. }
  565. public static void
  566. setSendBufferSize(java.nio.channels.DatagramChannel fd, int size)
  567. {
  568. try
  569. {
  570. java.net.DatagramSocket socket = fd.socket();
  571. socket.setSendBufferSize(size);
  572. }
  573. catch(java.io.IOException ex)
  574. {
  575. closeSocketNoThrow(fd);
  576. throw new Ice.SocketException(ex);
  577. }
  578. }
  579. public static int
  580. getSendBufferSize(java.nio.channels.DatagramChannel fd)
  581. {
  582. int size;
  583. try
  584. {
  585. java.net.DatagramSocket socket = fd.socket();
  586. size = socket.getSendBufferSize();
  587. }
  588. catch(java.io.IOException ex)
  589. {
  590. closeSocketNoThrow(fd);
  591. throw new Ice.SocketException(ex);
  592. }
  593. return size;
  594. }
  595. public static void
  596. setRecvBufferSize(java.nio.channels.DatagramChannel fd, int size)
  597. {
  598. try
  599. {
  600. java.net.DatagramSocket socket = fd.socket();
  601. socket.setReceiveBufferSize(size);
  602. }
  603. catch(java.io.IOException ex)
  604. {
  605. closeSocketNoThrow(fd);
  606. throw new Ice.SocketException(ex);
  607. }
  608. }
  609. public static int
  610. getRecvBufferSize(java.nio.channels.DatagramChannel fd)
  611. {
  612. int size;
  613. try
  614. {
  615. java.net.DatagramSocket socket = fd.socket();
  616. size = socket.getReceiveBufferSize();
  617. }
  618. catch(java.io.IOException ex)
  619. {
  620. closeSocketNoThrow(fd);
  621. throw new Ice.SocketException(ex);
  622. }
  623. return size;
  624. }
  625. public static java.net.InetSocketAddress
  626. getAddressForServer(String host, int port, int protocol, boolean preferIPv6)
  627. {
  628. if(host == null || host.length() == 0)
  629. {
  630. try
  631. {
  632. if(protocol != EnableIPv4)
  633. {
  634. return new java.net.InetSocketAddress(java.net.InetAddress.getByName("::0"), port);
  635. }
  636. else
  637. {
  638. return new java.net.InetSocketAddress(java.net.InetAddress.getByName("0.0.0.0"), port);
  639. }
  640. }
  641. catch(java.net.UnknownHostException ex)
  642. {
  643. assert(false);
  644. return null;
  645. }
  646. catch(java.lang.SecurityException ex)
  647. {
  648. throw new Ice.SocketException(ex);
  649. }
  650. }
  651. return getAddresses(host, port, protocol, Ice.EndpointSelectionType.Ordered, preferIPv6).get(0);
  652. }
  653. public static int
  654. compareAddress(java.net.InetSocketAddress addr1, java.net.InetSocketAddress addr2)
  655. {
  656. if(addr1.getPort() < addr2.getPort())
  657. {
  658. return -1;
  659. }
  660. else if(addr2.getPort() < addr1.getPort())
  661. {
  662. return 1;
  663. }
  664. byte[] larr = addr1.getAddress().getAddress();
  665. byte[] rarr = addr2.getAddress().getAddress();
  666. if(larr.length < rarr.length)
  667. {
  668. return -1;
  669. }
  670. else if(rarr.length < larr.length)
  671. {
  672. return 1;
  673. }
  674. assert(larr.length == rarr.length);
  675. for(int i = 0; i < larr.length; i++)
  676. {
  677. if(larr[i] < rarr[i])
  678. {
  679. return -1;
  680. }
  681. else if(rarr[i] < larr[i])
  682. {
  683. return 1;
  684. }
  685. }
  686. return 0;
  687. }
  688. public static java.net.InetAddress
  689. getLocalAddress(int protocol)
  690. {
  691. java.net.InetAddress addr = null;
  692. try
  693. {
  694. addr = java.net.InetAddress.getLocalHost();
  695. }
  696. catch(java.net.UnknownHostException ex)
  697. {
  698. //
  699. // May be raised on DHCP systems.
  700. //
  701. }
  702. catch(NullPointerException ex)
  703. {
  704. //
  705. // Workaround for bug in JDK.
  706. //
  707. }
  708. if(addr == null || !isValidAddr(addr, protocol))
  709. {
  710. //
  711. // Iterate over the network interfaces and pick an IP
  712. // address (preferably not the loopback address).
  713. //
  714. java.util.ArrayList<java.net.InetAddress> addrs = getLocalAddresses(protocol);
  715. java.util.Iterator<java.net.InetAddress> iter = addrs.iterator();
  716. while(addr == null && iter.hasNext())
  717. {
  718. java.net.InetAddress a = iter.next();
  719. if(protocol == EnableBoth || isValidAddr(a, protocol))
  720. {
  721. addr = a;
  722. }
  723. }
  724. if(addr == null)
  725. {
  726. addr = getLoopbackAddresses(protocol)[0]; // Use the loopback address as the last resort.
  727. }
  728. }
  729. assert(addr != null);
  730. return addr;
  731. }
  732. public static java.util.List<java.net.InetSocketAddress>
  733. getAddresses(String host, int port, int protocol, Ice.EndpointSelectionType selType, boolean preferIPv6)
  734. {
  735. java.util.List<java.net.InetSocketAddress> addresses = new java.util.ArrayList<java.net.InetSocketAddress>();
  736. try
  737. {
  738. java.net.InetAddress[] addrs;
  739. if(host == null || host.length() == 0)
  740. {
  741. addrs = getLoopbackAddresses(protocol);
  742. }
  743. else
  744. {
  745. addrs = java.net.InetAddress.getAllByName(host);
  746. }
  747. for(java.net.InetAddress addr : addrs)
  748. {
  749. if(protocol == EnableBoth || isValidAddr(addr, protocol))
  750. {
  751. addresses.add(new java.net.InetSocketAddress(addr, port));
  752. }
  753. }
  754. if(selType == Ice.EndpointSelectionType.Random)
  755. {
  756. java.util.Collections.shuffle(addresses);
  757. }
  758. if(protocol == EnableBoth)
  759. {
  760. if(preferIPv6)
  761. {
  762. java.util.Collections.sort(addresses, _preferIPv6Comparator);
  763. }
  764. else
  765. {
  766. java.util.Collections.sort(addresses, _preferIPv4Comparator);
  767. }
  768. }
  769. }
  770. catch(java.net.UnknownHostException ex)
  771. {
  772. throw new Ice.DNSException(0, host, ex);
  773. }
  774. catch(java.lang.SecurityException ex)
  775. {
  776. throw new Ice.SocketException(ex);
  777. }
  778. //
  779. // No Inet4Address/Inet6Address available.
  780. //
  781. if(addresses.isEmpty())
  782. {
  783. throw new Ice.DNSException(0, host);
  784. }
  785. return addresses;
  786. }
  787. public static java.util.ArrayList<java.net.InetAddress>
  788. getLocalAddresses(int protocol)
  789. {
  790. java.util.ArrayList<java.net.InetAddress> result = new java.util.ArrayList<java.net.InetAddress>();
  791. try
  792. {
  793. java.util.Enumeration<java.net.NetworkInterface> ifaces = java.net.NetworkInterface.getNetworkInterfaces();
  794. while(ifaces.hasMoreElements())
  795. {
  796. java.net.NetworkInterface iface = ifaces.nextElement();
  797. java.util.Enumeration<java.net.InetAddress> addrs = iface.getInetAddresses();
  798. while(addrs.hasMoreElements())
  799. {
  800. java.net.InetAddress addr = addrs.nextElement();
  801. if(!addr.isLoopbackAddress())
  802. {
  803. if(protocol == EnableBoth || isValidAddr(addr, protocol))
  804. {
  805. result.add(addr);
  806. }
  807. }
  808. }
  809. }
  810. }
  811. catch(java.net.SocketException ex)
  812. {
  813. throw new Ice.SocketException(ex);
  814. }
  815. catch(java.lang.SecurityException ex)
  816. {
  817. throw new Ice.SocketException(ex);
  818. }
  819. return result;
  820. }
  821. public static final class SocketPair
  822. {
  823. public java.nio.channels.spi.AbstractSelectableChannel source;
  824. public java.nio.channels.WritableByteChannel sink;
  825. }
  826. public static SocketPair
  827. createPipe()
  828. {
  829. SocketPair fds = new SocketPair();
  830. try
  831. {
  832. java.nio.channels.Pipe pipe = java.nio.channels.Pipe.open();
  833. fds.sink = pipe.sink();
  834. fds.source = pipe.source();
  835. }
  836. catch(java.io.IOException ex)
  837. {
  838. throw new Ice.SocketException(ex);
  839. }
  840. return fds;
  841. }
  842. public static java.util.ArrayList<String>
  843. getHostsForEndpointExpand(String host, int protocolSupport, boolean includeLoopback)
  844. {
  845. boolean wildcard = (host == null || host.length() == 0);
  846. if(!wildcard)
  847. {
  848. try
  849. {
  850. wildcard = java.net.InetAddress.getByName(host).isAnyLocalAddress();
  851. }
  852. catch(java.net.UnknownHostException ex)
  853. {
  854. }
  855. catch(java.lang.SecurityException ex)
  856. {
  857. throw new Ice.SocketException(ex);
  858. }
  859. }
  860. java.util.ArrayList<String> hosts = new java.util.ArrayList<String>();
  861. if(wildcard)
  862. {
  863. java.util.ArrayList<java.net.InetAddress> addrs = getLocalAddresses(protocolSupport);
  864. for(java.net.InetAddress addr : addrs)
  865. {
  866. //
  867. // NOTE: We don't publish link-local IPv6 addresses as these addresses can only
  868. // be accessed in general with a scope-id.
  869. //
  870. if(!addr.isLinkLocalAddress())
  871. {
  872. hosts.add(addr.getHostAddress());
  873. }
  874. }
  875. if(includeLoopback || hosts.isEmpty())
  876. {
  877. if(protocolSupport != EnableIPv6)
  878. {
  879. hosts.add("127.0.0.1");
  880. }
  881. if(protocolSupport != EnableIPv4)
  882. {
  883. hosts.add("0:0:0:0:0:0:0:1");
  884. }
  885. }
  886. }
  887. return hosts;
  888. }
  889. public static void
  890. setTcpBufSize(java.nio.channels.SocketChannel socket, Ice.Properties properties, Ice.Logger logger)
  891. {
  892. //
  893. // By default, on Windows we use a 128KB buffer size. On Unix
  894. // platforms, we use the system defaults.
  895. //
  896. int dfltBufSize = 0;
  897. if(System.getProperty("os.name").startsWith("Windows"))
  898. {
  899. dfltBufSize = 128 * 1024;
  900. }
  901. int sizeRequested = properties.getPropertyAsIntWithDefault("Ice.TCP.RcvSize", dfltBufSize);
  902. if(sizeRequested > 0)
  903. {
  904. //
  905. // Try to set the buffer size. The kernel will silently adjust
  906. // the size to an acceptable value. Then read the size back to
  907. // get the size that was actually set.
  908. //
  909. setRecvBufferSize(socket, sizeRequested);
  910. int size = getRecvBufferSize(socket);
  911. if(size < sizeRequested) // Warn if the size that was set is less than the requested size.
  912. {
  913. logger.warning("TCP receive buffer size: requested size of " + sizeRequested + " adjusted to " + size);
  914. }
  915. }
  916. sizeRequested = properties.getPropertyAsIntWithDefault("Ice.TCP.SndSize", dfltBufSize);
  917. if(sizeRequested > 0)
  918. {
  919. //
  920. // Try to set the buffer size. The kernel will silently adjust
  921. // the size to an acceptable value. Then read the size back to
  922. // get the size that was actually set.
  923. //
  924. setSendBufferSize(socket, sizeRequested);
  925. int size = getSendBufferSize(socket);
  926. if(size < sizeRequested) // Warn if the size that was set is less than the requested size.
  927. {
  928. logger.warning("TCP send buffer size: requested size of " + sizeRequested + " adjusted to " + size);
  929. }
  930. }
  931. }
  932. public static void
  933. setTcpBufSize(java.nio.channels.ServerSocketChannel socket, Ice.Properties properties, Ice.Logger logger)
  934. {
  935. //
  936. // By default, on Windows we use a 128KB buffer size. On Unix
  937. // platforms, we use the system defaults.
  938. //
  939. int dfltBufSize = 0;
  940. if(System.getProperty("os.name").startsWith("Windows"))
  941. {
  942. dfltBufSize = 128 * 1024;
  943. }
  944. //
  945. // Get property for buffer size.
  946. //
  947. int sizeRequested = properties.getPropertyAsIntWithDefault("Ice.TCP.RcvSize", dfltBufSize);
  948. if(sizeRequested > 0)
  949. {
  950. //
  951. // Try to set the buffer size. The kernel will silently adjust
  952. // the size to an acceptable value. Then read the size back to
  953. // get the size that was actually set.
  954. //
  955. setRecvBufferSize(socket, sizeRequested);
  956. int size = getRecvBufferSize(socket);
  957. if(size < sizeRequested) // Warn if the size that was set is less than the requested size.
  958. {
  959. logger.warning("TCP receive buffer size: requested size of " + sizeRequested + " adjusted to " + size);
  960. }
  961. }
  962. }
  963. public static String
  964. fdToString(java.nio.channels.SelectableChannel fd, NetworkProxy proxy, java.net.InetSocketAddress target)
  965. {
  966. if(fd == null)
  967. {
  968. return "<closed>";
  969. }
  970. java.net.InetAddress localAddr = null, remoteAddr = null;
  971. int localPort = -1, remotePort = -1;
  972. if(fd instanceof java.nio.channels.SocketChannel)
  973. {
  974. java.net.Socket socket = ((java.nio.channels.SocketChannel)fd).socket();
  975. localAddr = socket.getLocalAddress();
  976. localPort = socket.getLocalPort();
  977. remoteAddr = socket.getInetAddress();
  978. remotePort = socket.getPort();
  979. }
  980. else if(fd instanceof java.nio.channels.DatagramChannel)
  981. {
  982. java.net.DatagramSocket socket = ((java.nio.channels.DatagramChannel)fd).socket();
  983. localAddr = socket.getLocalAddress();
  984. localPort = socket.getLocalPort();
  985. remoteAddr = socket.getInetAddress();
  986. remotePort = socket.getPort();
  987. }
  988. else
  989. {
  990. assert(false);
  991. }
  992. return addressesToString(localAddr, localPort, remoteAddr, remotePort, proxy, target);
  993. }
  994. public static String
  995. fdToString(java.nio.channels.SelectableChannel fd)
  996. {
  997. if(fd == null)
  998. {
  999. return "<closed>";
  1000. }
  1001. java.net.InetAddress localAddr = null, remoteAddr = null;
  1002. int localPort = -1, remotePort = -1;
  1003. if(fd instanceof java.nio.channels.SocketChannel)
  1004. {
  1005. java.net.Socket socket = ((java.nio.channels.SocketChannel)fd).socket();
  1006. localAddr = socket.getLocalAddress();
  1007. localPort = socket.getLocalPort();
  1008. remoteAddr = socket.getInetAddress();
  1009. remotePort = socket.getPort();
  1010. }
  1011. else if(fd instanceof java.nio.channels.DatagramChannel)
  1012. {
  1013. java.net.DatagramSocket socket = ((java.nio.channels.DatagramChannel)fd).socket();
  1014. localAddr = socket.getLocalAddress();
  1015. localPort = socket.getLocalPort();
  1016. remoteAddr = socket.getInetAddress();
  1017. remotePort = socket.getPort();
  1018. }
  1019. else
  1020. {
  1021. assert(false);
  1022. }
  1023. return addressesToString(localAddr, localPort, remoteAddr, remotePort);
  1024. }
  1025. public static String
  1026. fdToString(java.net.Socket fd)
  1027. {
  1028. if(fd == null)
  1029. {
  1030. return "<closed>";
  1031. }
  1032. java.net.InetAddress localAddr = fd.getLocalAddress();
  1033. int localPort = fd.getLocalPort();
  1034. java.net.InetAddress remoteAddr = fd.getInetAddress();
  1035. int remotePort = fd.getPort();
  1036. return addressesToString(localAddr, localPort, remoteAddr, remotePort);
  1037. }
  1038. public static String
  1039. addressesToString(java.net.InetAddress localAddr, int localPort, java.net.InetAddress remoteAddr, int remotePort,
  1040. NetworkProxy proxy, java.net.InetSocketAddress target)
  1041. {
  1042. StringBuilder s = new StringBuilder(128);
  1043. s.append("local address = ");
  1044. s.append(addrToString(localAddr, localPort));
  1045. if(proxy != null)
  1046. {
  1047. if(remoteAddr == null)
  1048. {
  1049. java.net.InetSocketAddress addr = proxy.getAddress();
  1050. remoteAddr = addr.getAddress();
  1051. remotePort = addr.getPort();
  1052. }
  1053. s.append("\n");
  1054. s.append(proxy.getName());
  1055. s.append(" proxy address = ");
  1056. s.append(addrToString(remoteAddr, remotePort));
  1057. s.append("\nremote address = ");
  1058. s.append(addrToString(target.getAddress(), target.getPort()));
  1059. }
  1060. else
  1061. {
  1062. if(remoteAddr == null && target != null)
  1063. {
  1064. remoteAddr = target.getAddress();
  1065. remotePort = target.getPort();
  1066. }
  1067. if(remoteAddr == null)
  1068. {
  1069. s.append("\nremote address = <not connected>");
  1070. }
  1071. else
  1072. {
  1073. s.append("\nremote address = ");
  1074. s.append(addrToString(remoteAddr, remotePort));
  1075. }
  1076. }
  1077. return s.toString();
  1078. }
  1079. public static String
  1080. addressesToString(java.net.InetAddress localAddr, int localPort, java.net.InetAddress remoteAddr, int remotePort)
  1081. {
  1082. return addressesToString(localAddr, localPort, remoteAddr, remotePort, null, null);
  1083. }
  1084. public static String
  1085. addrToString(java.net.InetSocketAddress addr)
  1086. {
  1087. StringBuilder s = new StringBuilder(128);
  1088. s.append(addr.getAddress().getHostAddress());
  1089. s.append(':');
  1090. s.append(addr.getPort());
  1091. return s.toString();
  1092. }
  1093. public static boolean
  1094. interrupted(java.io.IOException ex)
  1095. {
  1096. return ex instanceof java.io.InterruptedIOException;
  1097. }
  1098. private static boolean
  1099. isValidAddr(java.net.InetAddress addr, int protocol)
  1100. {
  1101. byte[] bytes = null;
  1102. if(addr != null)
  1103. {
  1104. bytes = addr.getAddress();
  1105. }
  1106. return bytes != null &&
  1107. ((bytes.length == 16 && protocol == EnableIPv6) ||
  1108. (bytes.length == 4 && protocol == EnableIPv4));
  1109. }
  1110. public static String
  1111. addrToString(java.net.InetAddress addr, int port)
  1112. {
  1113. StringBuffer s = new StringBuffer();
  1114. //
  1115. // In early Android releases, sockets don't correctly report their address and
  1116. // port information.
  1117. //
  1118. if(addr == null || addr.isAnyLocalAddress())
  1119. {
  1120. s.append("<not available>");
  1121. }
  1122. else
  1123. {
  1124. s.append(addr.getHostAddress());
  1125. }
  1126. if(port > 0)
  1127. {
  1128. s.append(':');
  1129. s.append(port);
  1130. }
  1131. return s.toString();
  1132. }
  1133. private static java.net.InetAddress[]
  1134. getLoopbackAddresses(int protocol)
  1135. {
  1136. try
  1137. {
  1138. java.net.InetAddress[] addrs = new java.net.InetAddress[protocol == EnableBoth ? 2 : 1];
  1139. int i = 0;
  1140. if(protocol != EnableIPv6)
  1141. {
  1142. addrs[i++] = java.net.InetAddress.getByName("127.0.0.1");
  1143. }
  1144. if(protocol != EnableIPv4)
  1145. {
  1146. addrs[i++] = java.net.InetAddress.getByName("::1");
  1147. }
  1148. return addrs;
  1149. }
  1150. catch(java.net.UnknownHostException ex)
  1151. {
  1152. assert(false);
  1153. return null;
  1154. }
  1155. catch(java.lang.SecurityException ex)
  1156. {
  1157. throw new Ice.SocketException(ex);
  1158. }
  1159. }
  1160. static class IPAddressComparator implements java.util.Comparator<java.net.InetSocketAddress>
  1161. {
  1162. IPAddressComparator(boolean ipv6)
  1163. {
  1164. _ipv6 = ipv6;
  1165. }
  1166. public int
  1167. compare(java.net.InetSocketAddress lhs, java.net.InetSocketAddress rhs)
  1168. {
  1169. if(lhs.getAddress().getAddress().length < rhs.getAddress().getAddress().length)
  1170. {
  1171. return _ipv6 ? 1 : -1;
  1172. }
  1173. else if(lhs.getAddress().getAddress().length > rhs.getAddress().getAddress().length)
  1174. {
  1175. return _ipv6 ? -1 : 1;
  1176. }
  1177. else
  1178. {
  1179. return 0;
  1180. }
  1181. }
  1182. final private boolean _ipv6;
  1183. }
  1184. private static IPAddressComparator _preferIPv4Comparator = new IPAddressComparator(false);
  1185. private static IPAddressComparator _preferIPv6Comparator = new IPAddressComparator(true);
  1186. }