Skip to content
This repository has been archived by the owner on Jan 20, 2022. It is now read-only.

Commit

Permalink
Merge cd84b2a into 4feb990
Browse files Browse the repository at this point in the history
  • Loading branch information
hechaoli committed Apr 19, 2018
2 parents 4feb990 + cd84b2a commit 828882a
Show file tree
Hide file tree
Showing 7 changed files with 523 additions and 368 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -178,35 +178,11 @@ public void close() {

jsonRpcServer = new JsonRpcV1ServerImpl(transporter, new OvsdbRequestHandler());

ovsdbClient = new OvsdbClientImpl(getConnectionInfo(channel));
ovsdbClient = new OvsdbClientImpl(OvsdbConnectionInfo.fromChannel(channel));

executorService.submit(() -> connectionCallback.connected(ovsdbClient));
}

private OvsdbConnectionInfo getConnectionInfo(Channel channel) {
InetSocketAddress remoteSocketAddress
= (InetSocketAddress) channel.remoteAddress();
InetAddress remoteAddress = remoteSocketAddress.getAddress();
int remotePort = remoteSocketAddress.getPort();
InetSocketAddress localSocketAddress
= (InetSocketAddress) channel.localAddress();
InetAddress localAddress = localSocketAddress.getAddress();
int localPort = localSocketAddress.getPort();

SslHandler sslHandler = channel.pipeline().get(SslHandler.class);
Certificate remoteCertificate = null;
if (sslHandler != null) {
try {
remoteCertificate = sslHandler.engine().getSession().getPeerCertificates()[0];
} catch (SSLPeerUnverifiedException ex) {
LOGGER.error("Failed to get peer certificate of channel " + channel, ex);
}
}
return new OvsdbConnectionInfo(
localAddress, localPort, remoteAddress, remotePort, remoteCertificate
);
}

private String getNextId() {
return String.valueOf(callId.getAndIncrement());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,22 @@

package com.vmware.ovsdb.service;

import io.netty.channel.Channel;
import io.netty.handler.ssl.SslHandler;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.lang.invoke.MethodHandles;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.security.cert.Certificate;
import javax.net.ssl.SSLPeerUnverifiedException;

public class OvsdbConnectionInfo {

private static final Logger LOGGER =
LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());

private final InetAddress localAddress;

private final int localPort;
Expand All @@ -27,6 +38,8 @@ public class OvsdbConnectionInfo {

private final int remotePort;

private final Certificate localCertificate;

private final Certificate remoteCertificate;

/**
Expand All @@ -38,15 +51,16 @@ public class OvsdbConnectionInfo {
* @param remotePort remove port of the connection
* @param remoteCertificate remote certificate
*/
public OvsdbConnectionInfo(
private OvsdbConnectionInfo(
InetAddress localAddress, int localPort,
InetAddress remoteAddress, int remotePort,
Certificate remoteCertificate
Certificate localCertificate, Certificate remoteCertificate
) {
this.localAddress = localAddress;
this.localPort = localPort;
this.remoteAddress = remoteAddress;
this.remotePort = remotePort;
this.localCertificate = localCertificate;
this.remoteCertificate = remoteCertificate;
}

Expand All @@ -66,10 +80,46 @@ public int getRemotePort() {
return remotePort;
}

public Certificate getLocalCertificate() {
return localCertificate;
}

public Certificate getRemoteCertificate() {
return remoteCertificate;
}

/**
* Get the connection info from a Netty channel.
*
* @param channel the netty channel
* @return an {@link OvsdbConnectionInfo} object
*/
public static OvsdbConnectionInfo fromChannel(Channel channel) {
InetSocketAddress remoteSocketAddress
= (InetSocketAddress) channel.remoteAddress();
InetAddress remoteAddress = remoteSocketAddress.getAddress();
int remotePort = remoteSocketAddress.getPort();
InetSocketAddress localSocketAddress
= (InetSocketAddress) channel.localAddress();
InetAddress localAddress = localSocketAddress.getAddress();
int localPort = localSocketAddress.getPort();

SslHandler sslHandler = channel.pipeline().get(SslHandler.class);
Certificate localCertificate = null;
Certificate remoteCertificate = null;
if (sslHandler != null) {
try {
remoteCertificate = sslHandler.engine().getSession().getPeerCertificates()[0];
} catch (SSLPeerUnverifiedException ex) {
LOGGER.error("Failed to get peer certificate of channel " + channel, ex);
}
localCertificate = sslHandler.engine().getSession().getLocalCertificates()[0];
}
return new OvsdbConnectionInfo(
localAddress, localPort, remoteAddress, remotePort, localCertificate, remoteCertificate
);
}

@Override
public String toString() {
return getClass().getSimpleName() + " ["
Expand Down

This file was deleted.

Loading

0 comments on commit 828882a

Please sign in to comment.