1 /* 2 * Conditions Of Use 3 * 4 * This software was developed by employees of the National Institute of 5 * Standards and Technology (NIST), an agency of the Federal Government. 6 * Pursuant to title 15 Untied States Code Section 105, works of NIST 7 * employees are not subject to copyright protection in the United States 8 * and are considered to be in the public domain. As a result, a formal 9 * license is not needed to use the software. 10 * 11 * This software is provided by NIST as a service and is expressly 12 * provided "AS IS." NIST MAKES NO WARRANTY OF ANY KIND, EXPRESS, IMPLIED 13 * OR STATUTORY, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTY OF 14 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, NON-INFRINGEMENT 15 * AND DATA ACCURACY. NIST does not warrant or make any representations 16 * regarding the use of the software or the results thereof, including but 17 * not limited to the correctness, accuracy, reliability or usefulness of 18 * the software. 19 * 20 * Permission to use this software is contingent upon your acceptance 21 * of the terms of this agreement 22 * 23 * . 24 * 25 */ 26 package gov.nist.core.net; 27 28 import java.io.IOException; 29 import java.net.DatagramSocket; 30 import java.net.InetAddress; 31 import java.net.InetSocketAddress; 32 import java.net.MulticastSocket; 33 import java.net.ServerSocket; 34 import java.net.Socket; 35 import java.net.SocketException; 36 37 /* Added by Daniel J. Martinez Manzano <dani (at) dif.um.es> */ 38 import javax.net.ssl.SSLSocket; 39 import javax.net.ssl.SSLSocketFactory; 40 import javax.net.ssl.SSLServerSocket; 41 import javax.net.ssl.SSLServerSocketFactory; 42 43 /** 44 * default implementation which passes straight through to java platform 45 * 46 * @author m.andrews 47 * @version 1.2 48 * @since 1.1 49 * 50 */ 51 public class DefaultNetworkLayer implements NetworkLayer { 52 53 private SSLSocketFactory sslSocketFactory; 54 55 private SSLServerSocketFactory sslServerSocketFactory; 56 57 /** 58 * single default network layer; for flexibility, it may be better not to 59 * make it a singleton, but singleton seems to make sense currently. 60 */ 61 public static final DefaultNetworkLayer SINGLETON = new DefaultNetworkLayer(); 62 63 private DefaultNetworkLayer() { 64 sslServerSocketFactory = (SSLServerSocketFactory) SSLServerSocketFactory 65 .getDefault(); 66 sslSocketFactory = (SSLSocketFactory) SSLSocketFactory.getDefault(); 67 } 68 69 public ServerSocket createServerSocket(int port, int backlog, 70 InetAddress bindAddress) throws IOException { 71 return new ServerSocket(port, backlog, bindAddress); 72 } 73 74 public Socket createSocket(InetAddress address, int port) 75 throws IOException { 76 return new Socket(address, port); 77 } 78 79 public DatagramSocket createDatagramSocket() throws SocketException { 80 return new DatagramSocket(); 81 } 82 83 public DatagramSocket createDatagramSocket(int port, InetAddress laddr) 84 throws SocketException { 85 86 if ( laddr.isMulticastAddress() ) { 87 try { 88 MulticastSocket ds = new MulticastSocket( port ); 89 ds.joinGroup( laddr ); 90 return ds; 91 } catch (IOException e) { 92 throw new SocketException( e.getLocalizedMessage() ); 93 } 94 } else return new DatagramSocket(port, laddr); 95 } 96 97 /* Added by Daniel J. Martinez Manzano <dani (at) dif.um.es> */ 98 public SSLServerSocket createSSLServerSocket(int port, int backlog, 99 InetAddress bindAddress) throws IOException { 100 return (SSLServerSocket) sslServerSocketFactory.createServerSocket( 101 port, backlog, bindAddress); 102 } 103 104 /* Added by Daniel J. Martinez Manzano <dani (at) dif.um.es> */ 105 public SSLSocket createSSLSocket(InetAddress address, int port) 106 throws IOException { 107 return (SSLSocket) sslSocketFactory.createSocket(address, port); 108 } 109 110 /* Added by Daniel J. Martinez Manzano <dani (at) dif.um.es> */ 111 public SSLSocket createSSLSocket(InetAddress address, int port, 112 InetAddress myAddress) throws IOException { 113 return (SSLSocket) sslSocketFactory.createSocket(address, port, 114 myAddress, 0); 115 } 116 117 public Socket createSocket(InetAddress address, int port, 118 InetAddress myAddress) throws IOException { 119 if (myAddress != null) 120 return new Socket(address, port, myAddress, 0); 121 else 122 return new Socket(address, port); 123 } 124 125 /** 126 * Creates a new Socket, binds it to myAddress:myPort and connects it to 127 * address:port. 128 * 129 * @param address the InetAddress that we'd like to connect to. 130 * @param port the port that we'd like to connect to 131 * @param myAddress the address that we are supposed to bind on or null 132 * for the "any" address. 133 * @param myPort the port that we are supposed to bind on or 0 for a random 134 * one. 135 * 136 * @return a new Socket, bound on myAddress:myPort and connected to 137 * address:port. 138 * @throws IOException if binding or connecting the socket fail for a reason 139 * (exception relayed from the correspoonding Socket methods) 140 */ 141 public Socket createSocket(InetAddress address, int port, 142 InetAddress myAddress, int myPort) 143 throws IOException 144 { 145 if (myAddress != null) 146 return new Socket(address, port, myAddress, myPort); 147 else if (port != 0) 148 { 149 //myAddress is null (i.e. any) but we have a port number 150 Socket sock = new Socket(); 151 sock.bind(new InetSocketAddress(port)); 152 sock.connect(new InetSocketAddress(address, port)); 153 return sock; 154 } 155 else 156 return new Socket(address, port); 157 } 158 159 } 160