Skip to content

Commit

Permalink
Incorrect bind_addrs are now detected (https://issues.jboss.org/brows…
Browse files Browse the repository at this point in the history
  • Loading branch information
belaban committed Jan 28, 2011
1 parent 49248f1 commit fed1182
Show file tree
Hide file tree
Showing 12 changed files with 71 additions and 11 deletions.
18 changes: 18 additions & 0 deletions src/org/jgroups/annotations/LocalAddress.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package org.jgroups.annotations;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

/**
* This is an assertion, checked at startup time. It ensures that the field this annotation is on is (1) an InetAddress
* and (2) a valid address on a local network interface
* @author Bela Ban
* @since 2.11.2
*/
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.FIELD)
public @interface LocalAddress {
}

2 changes: 2 additions & 0 deletions src/org/jgroups/protocols/FD_ICMP.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import org.jgroups.Event;
import org.jgroups.Global;
import org.jgroups.annotations.Experimental;
import org.jgroups.annotations.LocalAddress;
import org.jgroups.annotations.Property;
import org.jgroups.conf.PropertyConverters;
import org.jgroups.stack.IpAddress;
Expand Down Expand Up @@ -30,6 +31,7 @@ public class FD_ICMP extends FD {
/** network interface to be used to send the ICMP packets */
private NetworkInterface intf=null;

@LocalAddress
@Property(name="bind_addr",
description="The NIC on which the ServerSocket should listen on. " +
"The following special values are also recognized: GLOBAL, SITE_LOCAL, LINK_LOCAL and NON_LOOPBACK",
Expand Down
1 change: 1 addition & 0 deletions src/org/jgroups/protocols/FD_SOCK.java
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ public class FD_SOCK extends Protocol implements Runnable {

/* ----------------------------------------- Properties -------------------------------------------------- */

@LocalAddress
@Property(description="The NIC on which the ServerSocket should listen on. " +
"The following special values are also recognized: GLOBAL, SITE_LOCAL, LINK_LOCAL and NON_LOOPBACK",
systemProperty={Global.BIND_ADDR, Global.BIND_ADDR_OLD},
Expand Down
2 changes: 2 additions & 0 deletions src/org/jgroups/protocols/MPING.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import org.jgroups.Event;
import org.jgroups.Global;
import org.jgroups.Message;
import org.jgroups.annotations.LocalAddress;
import org.jgroups.annotations.Property;
import org.jgroups.annotations.DeprecatedProperty;
import org.jgroups.conf.PropertyConverters;
Expand Down Expand Up @@ -34,6 +35,7 @@ public class MPING extends PING implements Runnable {

/* ----------------------------------------- Properties -------------------------------------------------- */

@LocalAddress
@Property(description="Bind address for multicast socket. " +
"The following special values are also recognized: GLOBAL, SITE_LOCAL, LINK_LOCAL and NON_LOOPBACK",
systemProperty={Global.BIND_ADDR, Global.BIND_ADDR_OLD},
Expand Down
1 change: 1 addition & 0 deletions src/org/jgroups/protocols/STOMP.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ public class STOMP extends Protocol implements Runnable {

/* ----------------------------------------- Properties ----------------------------------------------- */

@LocalAddress
@Property(name="bind_addr",
description="The bind address which should be used by the server socket. The following special values " +
"are also recognized: GLOBAL, SITE_LOCAL, LINK_LOCAL and NON_LOOPBACK",
Expand Down
2 changes: 1 addition & 1 deletion src/org/jgroups/protocols/TP.java
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ public abstract class TP extends Protocol {
/* ------------------------------------------ JMX and Properties ------------------------------------------ */



@LocalAddress
@Property(name="bind_addr",
description="The bind address which should be used by this transport. The following special values " +
"are also recognized: GLOBAL, SITE_LOCAL, LINK_LOCAL and NON_LOOPBACK",
Expand Down
4 changes: 1 addition & 3 deletions src/org/jgroups/protocols/UFC.java
Original file line number Diff line number Diff line change
Expand Up @@ -105,10 +105,8 @@ protected Object handleDownMessage(final Event evt, final Message msg, Address d
}

Credit cred=sent.get(dest);
if(cred == null) {
log.warn("destination " + dest + " not found; passing message down");
if(cred == null)
return down_prot.down(evt);
}

long block_time=max_block_times != null? getMaxBlockTime(length) : max_block_time;

Expand Down
4 changes: 3 additions & 1 deletion src/org/jgroups/protocols/VERIFY_SUSPECT.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
package org.jgroups.protocols;

import org.jgroups.*;
import org.jgroups.annotations.LocalAddress;
import org.jgroups.annotations.Property;
import org.jgroups.conf.PropertyConverters;
import org.jgroups.stack.IpAddress;
Expand Down Expand Up @@ -32,7 +33,8 @@ public class VERIFY_SUSPECT extends Protocol implements Runnable {

@Property(description="Use InetAddress.isReachable() to verify suspected member instead of regular messages")
private boolean use_icmp=false;


@LocalAddress
@Property(description="Interface for ICMP pings. Used if use_icmp is true " +
"The following special values are also recognized: GLOBAL, SITE_LOCAL, LINK_LOCAL and NON_LOOPBACK",
systemProperty={Global.BIND_ADDR, Global.BIND_ADDR_OLD},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ public class STREAMING_STATE_TRANSFER extends Protocol {
*
*/

@LocalAddress
@Property(description = "The interface (NIC) used to accept state requests. " +
"The following special values are also recognized: GLOBAL, SITE_LOCAL, LINK_LOCAL and NON_LOOPBACK",
systemProperty={Global.BIND_ADDR, Global.BIND_ADDR_OLD},
Expand Down
34 changes: 29 additions & 5 deletions src/org/jgroups/stack/Configurator.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@
import org.jgroups.Event;
import org.jgroups.Global;
import org.jgroups.annotations.DeprecatedProperty;
import org.jgroups.annotations.LocalAddress;
import org.jgroups.annotations.Property;
import org.jgroups.conf.PropertyConverters;
import org.jgroups.conf.PropertyHelper;
import org.jgroups.conf.ProtocolConfiguration;
import org.jgroups.logging.Log;
Expand All @@ -19,10 +21,7 @@
import java.io.Reader;
import java.io.StringReader;
import java.lang.reflect.*;
import java.net.Inet4Address;
import java.net.Inet6Address;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.*;
import java.util.*;
import java.util.concurrent.ConcurrentMap;

Expand Down Expand Up @@ -112,7 +111,8 @@ private static Protocol setupProtocolStack(List<ProtocolConfiguration> protocol_


// process default values
setDefaultValues(protocol_configs, protocols, ip_version) ;
setDefaultValues(protocol_configs, protocols, ip_version);
ensureValidBindAddresses(protocols);

return connectProtocols(protocols);
}
Expand Down Expand Up @@ -858,6 +858,30 @@ public static void setDefaultValues(List<Protocol> protocols, StackType ip_versi
}
}


/**
* Makes sure that all fields annotated with @LocalAddress is (1) an InetAddress and (2) a valid address on any
* local network interface
* @param protocols
* @throws Exception
*/
public static void ensureValidBindAddresses(List<Protocol> protocols) throws Exception {
for(Protocol protocol : protocols) {
String protocolName=protocol.getName();

//traverse class hierarchy and find all annotated fields and add them to the list if annotated
Field[] fields=Util.getAllDeclaredFieldsWithAnnotations(protocol.getClass(), LocalAddress.class);
for(int i=0; i < fields.length; i++) {
Object val=getValueFromProtocol(protocol, fields[i]);
if(!(val instanceof InetAddress))
throw new Exception("field " + protocolName + "." + fields[i].getName() + " is not an InetAddress");
Util.checkIfValidAddress((InetAddress)val, protocolName);
}
}
}



public static Object getValueFromProtocol(Protocol protocol, Field field) throws IllegalAccessException {
if(protocol == null || field == null) return null;
if(!Modifier.isPublic(field.getModifiers()))
Expand Down
11 changes: 11 additions & 0 deletions src/org/jgroups/util/Util.java
Original file line number Diff line number Diff line change
Expand Up @@ -3487,6 +3487,17 @@ public static Collection<InetAddress> getAllAvailableAddresses() {
return retval;
}

public static void checkIfValidAddress(InetAddress bind_addr, String prot_name) throws Exception {
Collection<InetAddress> addrs=getAllAvailableAddresses();
for(InetAddress addr: addrs) {
if(addr.equals(bind_addr))
return;
}
if(!bind_addr.isLoopbackAddress())
throw new BindException("[" + prot_name + "] " + bind_addr + " is not a valid address on any local network interface");
}



/**
* Returns a value associated wither with one or more system properties, or found in the props map
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ public static void testInitBCAsymProperties() throws Exception {

ENCRYPT encrypt=new ENCRYPT();
encrypt.asymAlgorithm = "RSA";
encrypt.asymProvider = "BC";
// encrypt.asymProvider = "BC";
encrypt.init();

// test the default asymetric key
Expand Down

0 comments on commit fed1182

Please sign in to comment.