Search in sources :

Example 1 with VersionedDataInputStream

use of org.apache.geode.internal.VersionedDataInputStream in project geode by apache.

the class TcpClient method getServerVersion.

private Short getServerVersion(InetSocketAddress ipAddr, int timeout) throws IOException, ClassNotFoundException {
    int gossipVersion = TcpServer.getCurrentGossipVersion();
    Short serverVersion = null;
    // Get GemFire version of TcpServer first, before sending any other request.
    synchronized (serverVersions) {
        serverVersion = serverVersions.get(ipAddr);
    }
    if (serverVersion != null) {
        return serverVersion;
    }
    gossipVersion = TcpServer.getOldGossipVersion();
    Socket sock = null;
    try {
        sock = socketCreator.connect(ipAddr.getAddress(), ipAddr.getPort(), timeout, null, false);
        sock.setSoTimeout(timeout);
    } catch (SSLException e) {
        throw new LocatorCancelException("Unable to form SSL connection", e);
    }
    try {
        DataOutputStream out = new DataOutputStream(sock.getOutputStream());
        out = new VersionedDataOutputStream(out, Version.GFE_57);
        out.writeInt(gossipVersion);
        VersionRequest verRequest = new VersionRequest();
        DataSerializer.writeObject(verRequest, out);
        out.flush();
        InputStream inputStream = sock.getInputStream();
        DataInputStream in = new DataInputStream(inputStream);
        in = new VersionedDataInputStream(in, Version.GFE_57);
        try {
            Object readObject = DataSerializer.readObject(in);
            if (!(readObject instanceof VersionResponse)) {
                throw new LocatorCancelException("Unrecognisable response received: object is null. This could be the result of trying to connect a non-SSL-enabled locator to an SSL-enabled locator.");
            }
            VersionResponse response = (VersionResponse) readObject;
            if (response != null) {
                serverVersion = Short.valueOf(response.getVersionOrdinal());
                synchronized (serverVersions) {
                    serverVersions.put(ipAddr, serverVersion);
                }
                return serverVersion;
            }
        } catch (EOFException ex) {
        // old locators will not recognize the version request and will close the connection
        }
    } finally {
        try {
            // initiate an abort on close to shut down the server's socket
            sock.setSoLinger(true, 0);
            sock.close();
        } catch (Exception e) {
            logger.error("Error closing socket ", e);
        }
    }
    if (logger.isDebugEnabled()) {
        logger.debug("Locator " + ipAddr + " did not respond to a request for its version.  I will assume it is using v5.7 for safety.");
    }
    synchronized (serverVersions) {
        serverVersions.put(ipAddr, Version.GFE_57.ordinal());
    }
    return Short.valueOf(Version.GFE_57.ordinal());
}
Also used : DataOutputStream(java.io.DataOutputStream) VersionedDataOutputStream(org.apache.geode.internal.VersionedDataOutputStream) DataInputStream(java.io.DataInputStream) VersionedDataInputStream(org.apache.geode.internal.VersionedDataInputStream) InputStream(java.io.InputStream) DataInputStream(java.io.DataInputStream) VersionedDataInputStream(org.apache.geode.internal.VersionedDataInputStream) SSLException(javax.net.ssl.SSLException) SSLHandshakeException(javax.net.ssl.SSLHandshakeException) IOException(java.io.IOException) EOFException(java.io.EOFException) SSLException(javax.net.ssl.SSLException) UnsupportedVersionException(org.apache.geode.cache.UnsupportedVersionException) VersionedDataOutputStream(org.apache.geode.internal.VersionedDataOutputStream) EOFException(java.io.EOFException) Socket(java.net.Socket) VersionedDataInputStream(org.apache.geode.internal.VersionedDataInputStream)

Example 2 with VersionedDataInputStream

use of org.apache.geode.internal.VersionedDataInputStream in project geode by apache.

the class CacheClientNotifier method registerClient.

/**
   * Registers a new client updater that wants to receive updates with this server.
   *
   * @param socket The socket over which the server communicates with the client.
   */
public void registerClient(Socket socket, boolean isPrimary, long acceptorId, boolean notifyBySubscription) throws IOException {
    // Since no remote ports were specified in the message, wait for them.
    long startTime = this.statistics.startTime();
    DataInputStream dis = new DataInputStream(socket.getInputStream());
    DataOutputStream dos = new DataOutputStream(socket.getOutputStream());
    // Read the client version
    short clientVersionOrdinal = Version.readOrdinal(dis);
    Version clientVersion = null;
    try {
        clientVersion = Version.fromOrdinal(clientVersionOrdinal, true);
        if (logger.isDebugEnabled()) {
            logger.debug("{}: Registering client with version: {}", this, clientVersion);
        }
    } catch (UnsupportedVersionException e) {
        SocketAddress sa = socket.getRemoteSocketAddress();
        UnsupportedVersionException uve = e;
        if (sa != null) {
            String sInfo = " Client: " + sa.toString() + ".";
            uve = new UnsupportedVersionException(e.getMessage() + sInfo);
        }
        logger.warn(LocalizedMessage.create(LocalizedStrings.CacheClientNotifier_CACHECLIENTNOTIFIER_CAUGHT_EXCEPTION_ATTEMPTING_TO_CLIENT), uve);
        writeException(dos, Acceptor.UNSUCCESSFUL_SERVER_TO_CLIENT, uve, clientVersion);
        return;
    }
    // Read and ignore the reply code. This is used on the client to server
    // handshake.
    // replyCode
    dis.readByte();
    if (Version.GFE_57.compareTo(clientVersion) <= 0) {
        if (Version.CURRENT.compareTo(clientVersion) > 0) {
            dis = new VersionedDataInputStream(dis, clientVersion);
            dos = new VersionedDataOutputStream(dos, clientVersion);
        }
        registerGFEClient(dis, dos, socket, isPrimary, startTime, clientVersion, acceptorId, notifyBySubscription);
    } else {
        Exception e = new UnsupportedVersionException(clientVersionOrdinal);
        throw new IOException(e.toString());
    }
}
Also used : Version(org.apache.geode.internal.Version) DataOutputStream(java.io.DataOutputStream) VersionedDataOutputStream(org.apache.geode.internal.VersionedDataOutputStream) VersionedDataOutputStream(org.apache.geode.internal.VersionedDataOutputStream) IOException(java.io.IOException) VersionedDataInputStream(org.apache.geode.internal.VersionedDataInputStream) DataInputStream(java.io.DataInputStream) SocketAddress(java.net.SocketAddress) VersionedDataInputStream(org.apache.geode.internal.VersionedDataInputStream) CqException(org.apache.geode.cache.query.CqException) RegionDestroyedException(org.apache.geode.cache.RegionDestroyedException) AuthenticationFailedException(org.apache.geode.security.AuthenticationFailedException) IOException(java.io.IOException) CacheException(org.apache.geode.cache.CacheException) UnsupportedVersionException(org.apache.geode.cache.UnsupportedVersionException) RegionExistsException(org.apache.geode.cache.RegionExistsException) CancelException(org.apache.geode.CancelException) AuthenticationRequiredException(org.apache.geode.security.AuthenticationRequiredException) UnsupportedVersionException(org.apache.geode.cache.UnsupportedVersionException)

Example 3 with VersionedDataInputStream

use of org.apache.geode.internal.VersionedDataInputStream in project geode by apache.

the class HandShake method handshakeWithServer.

/**
   * Client-side handshake with a Server
   */
public ServerQueueStatus handshakeWithServer(Connection conn, ServerLocation location, byte communicationMode) throws IOException, AuthenticationRequiredException, AuthenticationFailedException, ServerRefusedConnectionException {
    try {
        ServerQueueStatus serverQStatus = null;
        Socket sock = conn.getSocket();
        DataOutputStream dos = new DataOutputStream(sock.getOutputStream());
        final InputStream in = sock.getInputStream();
        DataInputStream dis = new DataInputStream(in);
        DistributedMember member = getIDForSocket(sock);
        // if running in a loner system, use the new port number in the ID to
        // help differentiate from other clients
        DM dm = ((InternalDistributedSystem) this.system).getDistributionManager();
        InternalDistributedMember idm = dm.getDistributionManagerId();
        synchronized (idm) {
            if (idm.getPort() == 0 && dm instanceof LonerDistributionManager) {
                int port = sock.getLocalPort();
                ((LonerDistributionManager) dm).updateLonerPort(port);
                updateProxyID(dm.getDistributionManagerId());
            }
        }
        if (communicationMode == Acceptor.GATEWAY_TO_GATEWAY) {
            this.credentials = getCredentials(member);
        }
        byte intermediateAcceptanceCode = write(dos, dis, communicationMode, REPLY_OK, this.clientReadTimeout, null, this.credentials, member, false);
        String authInit = this.system.getProperties().getProperty(SECURITY_CLIENT_AUTH_INIT);
        if (communicationMode != Acceptor.GATEWAY_TO_GATEWAY && intermediateAcceptanceCode != REPLY_AUTH_NOT_REQUIRED && (authInit != null && authInit.length() != 0)) {
            location.compareAndSetRequiresCredentials(true);
        }
        // Read the acceptance code
        byte acceptanceCode = dis.readByte();
        if (acceptanceCode == (byte) 21 && !(sock instanceof SSLSocket)) {
            // SSL
            throw new AuthenticationRequiredException(LocalizedStrings.HandShake_SERVER_EXPECTING_SSL_CONNECTION.toLocalizedString());
        }
        if (acceptanceCode == REPLY_SERVER_IS_LOCATOR) {
            throw new GemFireConfigException("Improperly configured client detected.  " + "Server at " + location + " is actually a locator.  Use addPoolLocator to configure locators.");
        }
        // Successful handshake for GATEWAY_TO_GATEWAY mode sets the peer version in connection
        if (communicationMode == Acceptor.GATEWAY_TO_GATEWAY && !(acceptanceCode == REPLY_EXCEPTION_AUTHENTICATION_REQUIRED || acceptanceCode == REPLY_EXCEPTION_AUTHENTICATION_FAILED)) {
            short wanSiteVersion = Version.readOrdinal(dis);
            conn.setWanSiteVersion(wanSiteVersion);
            // establish a versioned stream for the other site, if necessary
            if (wanSiteVersion < Version.CURRENT_ORDINAL) {
                dis = new VersionedDataInputStream(dis, Version.fromOrdinalOrCurrent(wanSiteVersion));
            }
        }
        // No need to check for return value since DataInputStream already throws
        // EOFException in case of EOF
        byte epType = dis.readByte();
        int qSize = dis.readInt();
        // Read the server member
        member = readServerMember(dis);
        serverQStatus = new ServerQueueStatus(epType, qSize, member);
        // Read the message (if any)
        readMessage(dis, dos, acceptanceCode, member);
        // DSes with different values of this. It shoule be a member variable.
        if (communicationMode != Acceptor.GATEWAY_TO_GATEWAY && currentClientVersion.compareTo(Version.GFE_61) >= 0) {
            deltaEnabledOnServer = dis.readBoolean();
        }
        // validate that the remote side has a different distributed system id.
        if (communicationMode == Acceptor.GATEWAY_TO_GATEWAY && Version.GFE_66.compareTo(conn.getWanSiteVersion()) <= 0 && currentClientVersion.compareTo(Version.GFE_66) >= 0) {
            int remoteDistributedSystemId = in.read();
            int localDistributedSystemId = ((InternalDistributedSystem) system).getDistributionManager().getDistributedSystemId();
            if (localDistributedSystemId >= 0 && localDistributedSystemId == remoteDistributedSystemId) {
                throw new GatewayConfigurationException("Remote WAN site's distributed system id " + remoteDistributedSystemId + " matches this sites distributed system id " + localDistributedSystemId);
            }
        }
        // Read the PDX registry size from the remote size
        if (communicationMode == Acceptor.GATEWAY_TO_GATEWAY && Version.GFE_80.compareTo(conn.getWanSiteVersion()) <= 0 && currentClientVersion.compareTo(Version.GFE_80) >= 0) {
            int remotePdxSize = dis.readInt();
            serverQStatus.setPdxSize(remotePdxSize);
        }
        return serverQStatus;
    } catch (IOException ex) {
        CancelCriterion stopper = this.system.getCancelCriterion();
        stopper.checkCancelInProgress(null);
        throw ex;
    }
}
Also used : GatewayConfigurationException(org.apache.geode.cache.GatewayConfigurationException) DataOutputStream(java.io.DataOutputStream) VersionedDataOutputStream(org.apache.geode.internal.VersionedDataOutputStream) HeapDataOutputStream(org.apache.geode.internal.HeapDataOutputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) VersionedDataInputStream(org.apache.geode.internal.VersionedDataInputStream) DataInputStream(java.io.DataInputStream) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) SSLSocket(javax.net.ssl.SSLSocket) CancelCriterion(org.apache.geode.CancelCriterion) DM(org.apache.geode.distributed.internal.DM) AuthenticationRequiredException(org.apache.geode.security.AuthenticationRequiredException) IOException(java.io.IOException) VersionedDataInputStream(org.apache.geode.internal.VersionedDataInputStream) DataInputStream(java.io.DataInputStream) InternalDistributedMember(org.apache.geode.distributed.internal.membership.InternalDistributedMember) GemFireConfigException(org.apache.geode.GemFireConfigException) InternalDistributedMember(org.apache.geode.distributed.internal.membership.InternalDistributedMember) DistributedMember(org.apache.geode.distributed.DistributedMember) InternalDistributedSystem(org.apache.geode.distributed.internal.InternalDistributedSystem) LonerDistributionManager(org.apache.geode.distributed.internal.LonerDistributionManager) SSLSocket(javax.net.ssl.SSLSocket) Socket(java.net.Socket) VersionedDataInputStream(org.apache.geode.internal.VersionedDataInputStream)

Example 4 with VersionedDataInputStream

use of org.apache.geode.internal.VersionedDataInputStream in project geode by apache.

the class HandShake method readServerMember.

protected DistributedMember readServerMember(DataInputStream p_dis) throws IOException {
    byte[] memberBytes = DataSerializer.readByteArray(p_dis);
    ByteArrayInputStream bais = new ByteArrayInputStream(memberBytes);
    DataInputStream dis = new DataInputStream(bais);
    Version v = InternalDataSerializer.getVersionForDataStreamOrNull(p_dis);
    if (v != null) {
        dis = new VersionedDataInputStream(dis, v);
    }
    try {
        return DataSerializer.readObject(dis);
    } catch (EOFException e) {
        throw e;
    } catch (Exception e) {
        throw new InternalGemFireException(LocalizedStrings.HandShake_UNABLE_TO_DESERIALIZE_MEMBER.toLocalizedString(), e);
    }
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) Version(org.apache.geode.internal.Version) InternalGemFireException(org.apache.geode.InternalGemFireException) EOFException(java.io.EOFException) VersionedDataInputStream(org.apache.geode.internal.VersionedDataInputStream) DataInputStream(java.io.DataInputStream) VersionedDataInputStream(org.apache.geode.internal.VersionedDataInputStream) ServerRefusedConnectionException(org.apache.geode.cache.client.ServerRefusedConnectionException) GemFireSecurityException(org.apache.geode.security.GemFireSecurityException) InternalGemFireException(org.apache.geode.InternalGemFireException) GatewayConfigurationException(org.apache.geode.cache.GatewayConfigurationException) EOFException(java.io.EOFException) AuthenticationFailedException(org.apache.geode.security.AuthenticationFailedException) GemFireConfigException(org.apache.geode.GemFireConfigException) IOException(java.io.IOException) AuthenticationRequiredException(org.apache.geode.security.AuthenticationRequiredException)

Example 5 with VersionedDataInputStream

use of org.apache.geode.internal.VersionedDataInputStream in project geode by apache.

the class OldClientSupportDUnitTest method testConversionOfArrayTypes.

@Test
public void testConversionOfArrayTypes() throws Exception {
    OldClientSupportService oldClientSupport = OldClientSupportProvider.getService(myCache);
    Version oldClientVersion = Version.GFE_82;
    VersionedDataOutputStream dout = new VersionedDataOutputStream(new HeapDataOutputStream(10, oldClientVersion), oldClientVersion);
    for (String geodeClassName : newArrayClassNames) {
        String newName = oldClientSupport.processOutgoingClassName(geodeClassName, dout);
        Assert.assertNotEquals(geodeClassName, newName);
    }
    for (String className : allNonconformingArrayClassNames) {
        String newName = oldClientSupport.processOutgoingClassName(className, dout);
        Assert.assertEquals(className, newName);
    }
    VersionedDataInputStream din = new VersionedDataInputStream(new DataInputStream(new ByteArrayInputStream(new byte[10])), oldClientVersion);
    for (String oldClassName : oldArrayClassNames) {
        String newName = oldClientSupport.processIncomingClassName(oldClassName, din);
        Assert.assertNotEquals(oldClassName, newName);
    }
}
Also used : Version(org.apache.geode.internal.Version) ByteArrayInputStream(java.io.ByteArrayInputStream) HeapDataOutputStream(org.apache.geode.internal.HeapDataOutputStream) VersionedDataOutputStream(org.apache.geode.internal.VersionedDataOutputStream) OldClientSupportService(org.apache.geode.internal.cache.tier.sockets.OldClientSupportService) DataInputStream(java.io.DataInputStream) VersionedDataInputStream(org.apache.geode.internal.VersionedDataInputStream) VersionedDataInputStream(org.apache.geode.internal.VersionedDataInputStream) Test(org.junit.Test) DistributedTest(org.apache.geode.test.junit.categories.DistributedTest)

Aggregations

VersionedDataInputStream (org.apache.geode.internal.VersionedDataInputStream)13 DataInputStream (java.io.DataInputStream)12 IOException (java.io.IOException)8 ByteArrayInputStream (java.io.ByteArrayInputStream)7 DataOutputStream (java.io.DataOutputStream)7 VersionedDataOutputStream (org.apache.geode.internal.VersionedDataOutputStream)7 HeapDataOutputStream (org.apache.geode.internal.HeapDataOutputStream)5 EOFException (java.io.EOFException)4 GemFireConfigException (org.apache.geode.GemFireConfigException)4 InternalDistributedMember (org.apache.geode.distributed.internal.membership.InternalDistributedMember)4 InputStream (java.io.InputStream)3 Socket (java.net.Socket)3 SSLException (javax.net.ssl.SSLException)3 UnsupportedVersionException (org.apache.geode.cache.UnsupportedVersionException)3 Version (org.apache.geode.internal.Version)3 AuthenticationRequiredException (org.apache.geode.security.AuthenticationRequiredException)3 Test (org.junit.Test)3 InvocationTargetException (java.lang.reflect.InvocationTargetException)2 UnknownHostException (java.net.UnknownHostException)2 SSLHandshakeException (javax.net.ssl.SSLHandshakeException)2