public void allowX11Forwarding(String display, File f) throws SshException {

		if (remoteforwardings.containsKey(X11_KEY))
			throw new SshException("X11 forwarding is already in use!",
					SshException.FORWARDING_ERROR);

		if (!incomingtunnels.containsKey(X11_KEY)) {
			incomingtunnels.put(X11_KEY, new Vector<ActiveTunnel>());
		}
		ssh.getContext().setX11Display(display);
		ssh.getContext().setX11RequestListener(forwardinglistener);

		try {

			// Find the users real cookie
			if (f.exists()) {

				String hostname = "";
				int screen = 0;

				int idx = display.indexOf(':');

				if (idx != -1) {
					hostname = display.substring(0, idx);
					screen = Integer.parseInt(display.substring(idx + 1));
				}

				FileInputStream in = new FileInputStream(f);
				ByteArrayOutputStream out = new ByteArrayOutputStream();
				int read;
				while ((read = in.read()) != -1)
					out.write(read);

				in.close();

				byte[] tmp = out.toByteArray();

				ByteArrayReader bar = new ByteArrayReader(tmp);

				try {
					while (bar.available() > 0) {

						short family = bar.readShort();
						short len = bar.readShort();
						byte[] address = new byte[len];
						bar.read(address);

						len = bar.readShort();
						byte[] number = new byte[len];
						bar.read(number);

						len = bar.readShort();
						byte[] name = new byte[len];
						bar.read(name);

						len = bar.readShort();
						byte[] data = new byte[len];
						bar.read(data);

						String n = new String(number);
						int d = Integer.parseInt(n);

						String protocol = new String(name);
						if (protocol.equals("MIT-MAGIC-COOKIE-1")) {
							if (family == 0) {
								// We cannot use InetAddress.getByAddress since
								// it
								// was only introduced in 1.4 :(
								// So we're going to do this really crude
								// formating
								// of the IP Address and get by name
								// which works just as well!
								String ip = (address[0] & 0xFF) + "."
										+ (address[1] & 0xFF) + "."
										+ (address[2] & 0xFF) + "."
										+ (address[3] & 0xFF);
								InetAddress addr = java.net.InetAddress
										.getByName(ip);
								if (addr.getHostAddress().equals(hostname)
										|| addr.getHostName().equals(hostname)) {
									if (screen == d) {
										ssh.getContext().setX11RealCookie(data);
										break;
									}
								}
							} else if (family == 256) {
								String h = new String(address);
								if (h.equals(hostname)) {
									if (screen == d) {
										ssh.getContext().setX11RealCookie(data);
										break;
									}
								}
							}
						}
					}
				} finally {
					bar.close();
				}
			}
			String hostname = "localhost";
			int screen = 0;

			int idx = display.indexOf(':');
			if (idx != -1) {
				hostname = display.substring(0, idx);
				display = display.substring(idx + 1);
			}

			if ((idx = display.indexOf('.')) > -1) {
				screen = Integer.parseInt(display.substring(idx + 1));
			}

			for (int i = 0; i < clientlisteners.size(); i++) {
				((ForwardingClientListener) clientlisteners.elementAt(i))
						.forwardingStarted(
								ForwardingClientListener.X11_FORWARDING,
								X11_KEY, hostname, screen);
			}

			isXForwarding = true;
		} catch (IOException ioe) {
			throw new SshException(ioe.getMessage(),
					SshException.FORWARDING_ERROR);
		}

	}
public void allowX11Forwarding ( String display , File f ) throws SshException { if ( remoteforwardings . containsKey ( X11_KEY ) ) throw new SshException ( "X11 forwarding is already in use!" , SshException . FORWARDING_ERROR ) ; if ( ! incomingtunnels . containsKey ( X11_KEY ) ) { incomingtunnels . put ( X11_KEY , new Vector < ActiveTunnel > ( ) ) ; } ssh . getContext ( ) . setX11Display ( display ) ; ssh . getContext ( ) . setX11RequestListener ( forwardinglistener ) ; try { if ( f . exists ( ) ) { String hostname = "" ; int screen = 0 ; int idx = display . indexOf ( ':' ) ; if ( idx != - 1 ) { hostname = display . substring ( 0 , idx ) ; screen = Integer . parseInt ( display . substring ( idx + 1 ) ) ; } FileInputStream in = new FileInputStream ( f ) ; ByteArrayOutputStream out = new ByteArrayOutputStream ( ) ; int read ; while ( ( read = in . read ( ) ) != - 1 ) out . write ( read ) ; in . close ( ) ; byte [ ] tmp = out . toByteArray ( ) ; ByteArrayReader bar = new ByteArrayReader ( tmp ) ; try { while ( bar . available ( ) > 0 ) { short family = bar . readShort ( ) ; short len = bar . readShort ( ) ; byte [ ] address = new byte [ len ] ; bar . read ( address ) ; len = bar . readShort ( ) ; byte [ ] number = new byte [ len ] ; bar . read ( number ) ; len = bar . readShort ( ) ; byte [ ] name = new byte [ len ] ; bar . read ( name ) ; len = bar . readShort ( ) ; byte [ ] data = new byte [ len ] ; bar . read ( data ) ; String n = new String ( number ) ; int d = Integer . parseInt ( n ) ; String protocol = new String ( name ) ; if ( protocol . equals ( "MIT-MAGIC-COOKIE-1" ) ) { if ( family == 0 ) { String ip = ( address [ 0 ] & 0xFF ) + "." + ( address [ 1 ] & 0xFF ) + "." + ( address [ 2 ] & 0xFF ) + "." + ( address [ 3 ] & 0xFF ) ; InetAddress addr = java . net . InetAddress . getByName ( ip ) ; if ( addr . getHostAddress ( ) . equals ( hostname ) || addr . getHostName ( ) . equals ( hostname ) ) { if ( screen == d ) { ssh . getContext ( ) . setX11RealCookie ( data ) ; break ; } } } else if ( family == 256 ) { String h = new String ( address ) ; if ( h . equals ( hostname ) ) { if ( screen == d ) { ssh . getContext ( ) . setX11RealCookie ( data ) ; break ; } } } } } } finally { bar . close ( ) ; } } String hostname = "localhost" ; int screen = 0 ; int idx = display . indexOf ( ':' ) ; if ( idx != - 1 ) { hostname = display . substring ( 0 , idx ) ; display = display . substring ( idx + 1 ) ; } if ( ( idx = display . indexOf ( '.' ) ) > - 1 ) { screen = Integer . parseInt ( display . substring ( idx + 1 ) ) ; } for ( int i = 0 ; i < clientlisteners . size ( ) ; i ++ ) { ( ( ForwardingClientListener ) clientlisteners . elementAt ( i ) ) . forwardingStarted ( ForwardingClientListener . X11_FORWARDING , X11_KEY , hostname , screen ) ; } isXForwarding = true ; } catch ( IOException ioe ) { throw new SshException ( ioe . getMessage ( ) , SshException . FORWARDING_ERROR ) ; } }
Configure the forwarding client to manage X11 connections. This method
will configure the {@link com.sshtools.ssh.SshClient} for X11 forwarding
and will generate a fake cookie which will be used to spoof incoming X11
requests. When a request is received the fake cookie will be replaced in
the authentication packet by a real cookie which is extracted from the
.Xauthority file provided in the File parameter.

@param display
String
@throws IOException