Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement SCTP multi-homing for JDiameter, both client and server #170

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ public SCTPClientConnection(Configuration config, IConcurrentFactory concurrentF
localAddress, localPort });
client.setDestAddress(new InetSocketAddress(remoteAddress, remotePort));
client.setOrigAddress(new InetSocketAddress(localAddress, localPort));
client.setExtraHostAddresses(getExtraLocalIPAddresses(config));
}

public SCTPClientConnection(Configuration config, IConcurrentFactory concurrentFactory, InetAddress remoteAddress,
Expand All @@ -87,9 +88,32 @@ public SCTPClientConnection(Configuration config, IConcurrentFactory concurrentF
localAddress, localPort });
client.setDestAddress(new InetSocketAddress(remoteAddress, remotePort));
client.setOrigAddress(new InetSocketAddress(localAddress, localPort));
client.setExtraHostAddresses(getExtraLocalIPAddresses(config));
listeners.add(listener);
}

private static String[] getExtraLocalIPAddresses(Configuration config) {
String[] extraLocalIPAddresses = null;

if (!config.isAttributeExist(org.jdiameter.server.impl.helpers.Parameters.OwnIPAddresses.ordinal())) {
throw new IllegalArgumentException("No IPAddresses attribute present in local peer!");
} else {
final Configuration[] ownIPAddresses = config.getChildren(org.jdiameter.server.impl.helpers.Parameters.OwnIPAddresses.ordinal());
if (ownIPAddresses.length > 4) {
throw new IllegalArgumentException("Maximum of 4 IPAddress attributes allowed");
}

extraLocalIPAddresses = new String[ownIPAddresses.length - 1];

for(int i = 1; i < ownIPAddresses.length; i++) {
final String localIPAddress = ownIPAddresses[i].getStringValue(org.jdiameter.server.impl.helpers.Parameters.OwnIPAddress.ordinal(), "");
extraLocalIPAddresses[i - 1] = localIPAddress;
logger.debug("Adding extra local IP address [{}]", localIPAddress);
}
}
return extraLocalIPAddresses;
}

@Override
public long getCreatedTime() {
return createdTime;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,8 @@ public class SCTPTransportClient {
private String clientAssociationName;
protected InetSocketAddress destAddress;
protected InetSocketAddress origAddress;

protected String[] extraHostAddresses;
private int payloadProtocolId = 0;
private int streamNumber = 0;

Expand Down Expand Up @@ -109,7 +111,7 @@ public void initialize() throws IOException, NotInitializedException {
clientAssociationName, origAddress, destAddress });
this.clientAssociation = this.management.addAssociation(origAddress.getAddress().getHostAddress(),
origAddress.getPort(), destAddress.getAddress().getHostAddress(), destAddress.getPort(), clientAssociationName,
IpChannelType.SCTP, null);
IpChannelType.SCTP, extraHostAddresses);
}
else {
logger.debug("CLIENT ASSOCIATION '{}'. Origin Address [{}:{}] <=> Dest Address [{}:{}] already present. Re-using it.",
Expand Down Expand Up @@ -289,6 +291,19 @@ public InetSocketAddress getOrigAddress() {
return this.origAddress;
}

public void setExtraHostAddresses(String[] extraHostAddresses) {
this.extraHostAddresses = extraHostAddresses;
if (logger.isDebugEnabled() && extraHostAddresses != null) {
for(final String address : extraHostAddresses) {
logger.debug("Extra host address is set to [{}]", address);
}
}
}

public String[] getExtraHostAddresses() {
return this.extraHostAddresses;
}

public void sendMessage(ByteBuffer bytes) throws IOException {
if (logger.isDebugEnabled()) {
logger.debug("About to send a byte buffer of size [{}] over the SCTP", bytes.array().length);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,10 +73,19 @@ public NetworkGuard(InetAddress[] inetAddresses, int port, IConcurrentFactory co
this.serverConnections = new ArrayList<SCTPServerConnection>();

try {
for (InetAddress ia : inetAddresses) {
final SCTPServerConnection sctpServerConnection = new SCTPServerConnection(null, ia, port, parser, null, this);
this.serverConnections.add(sctpServerConnection);
if (localAddresses.length < 1) {
throw new Exception("Need at least one IP address configured");
} else if (localAddresses.length > 4) {
throw new IllegalArgumentException("Maximum of 4 IPAddress attributes allowed");
}

final InetAddress firstAddress = localAddresses[0];
final String[] extraHostAddresses = localAddresses.length > 1 ? new String[localAddresses.length - 1] : null;
for(int i = 1; i < localAddresses.length; i++) {
extraHostAddresses[i - 1] = localAddresses[i].getHostAddress();
}
final SCTPServerConnection sctpServerConnection = new SCTPServerConnection(null, firstAddress, port, parser, null, this, extraHostAddresses);
this.serverConnections.add(sctpServerConnection);
}
catch (Exception exc) {
try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,11 +72,12 @@ protected SCTPServerConnection(IMessageParser parser, NetworkGuard guard) {

// this creates the listening server - no dest address is passed it is automatically 0.0.0.0:0
public SCTPServerConnection(Configuration config, InetAddress localAddress, int localPort, IMessageParser parser, String ref,
NetworkGuard guard) throws Exception {
NetworkGuard guard, String[] extraHostAddresses) throws Exception {
this(parser, guard);

logger.debug("SCTP Server constructor for listening server @ {}:{}", localAddress, localPort);
server.setOrigAddress(new InetSocketAddress(localAddress, localPort));
server.setExtraHostAddresses(extraHostAddresses);
server.startServer();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ public class SCTPTransportServer {
private String serverName;
protected InetSocketAddress destAddress;
protected InetSocketAddress origAddress;
protected String[] extraHostAddresses;
private Server server = null;
private static final Logger logger = LoggerFactory.getLogger(SCTPTransportServer.class);
private int payloadProtocolId = 0;
Expand Down Expand Up @@ -161,7 +162,7 @@ public void startServer() throws NotInitializedException {
// We don't have any, let's create it
if (server == null) {
server = this.management.addServer(serverName, origAddress.getAddress().getHostAddress(), origAddress.getPort(),
IpChannelType.SCTP, true, 10, null);
IpChannelType.SCTP, true, 10, extraHostAddresses);
}

for (String assocName : server.getAssociations()) {
Expand Down Expand Up @@ -381,6 +382,15 @@ public void setOrigAddress(InetSocketAddress address) {
}
}

public void setExtraHostAddresses(String[] extraHostAddresses) {
this.extraHostAddresses = extraHostAddresses;
if (logger.isDebugEnabled() && extraHostAddresses != null) {
for(final String address : extraHostAddresses) {
logger.debug("Extra host address is set to [{}]", address);
}
}
}

public InetSocketAddress getOrigAddress() {
return this.origAddress;
}
Expand Down