Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

- Pick the bind_addr based on pattern matching (https://issues.jboss.…

  • Loading branch information...
commit a75a0b3b818fe2badaa3c0a438f9f37bdb09eeb5 1 parent 329f08b
@belaban authored
View
29 doc/manual/en/modules/protocols.xml
@@ -127,12 +127,39 @@
</para>
</listitem>
</varlistentry>
+ <varlistentry>
+ <term>match-interface</term>
+ <listitem>
+ <para>
+ Pick an address which matches a pattern against the interface name,
+ e.g. <literal>match-interface:eth.*</literal>
+ </para>
+ </listitem>
+ </varlistentry>
+ <varlistentry>
+ <term>match-address</term>
+ <listitem>
+ <para>
+ Pick an address which matches a pattern against the host address,
+ e.g. <literal>match-address:192.168.*</literal>
+ </para>
+ </listitem>
+ </varlistentry>
+ <varlistentry>
+ <term>match-host</term>
+ <listitem>
+ <para>
+ Pick an address which matches a pattern against the host name,
+ e.g. <literal>match-host:linux.*</literal>
+ </para>
+ </listitem>
+ </varlistentry>
</variablelist>
<para>
An example of setting the bind address in UDP to use a site local address is:
</para>
<programlisting language="Java">
-&lt;UDP bind_addr="SITE_LOCAL" /&gt;
+ &lt;UDP bind_addr="SITE_LOCAL" /&gt;
</programlisting>
<para>
This will pick any address of any interface that's site-local, e.g. a <literal>192.168.x.x</literal> or
View
4 src/org/jgroups/Global.java
@@ -75,6 +75,10 @@
public static final long THREAD_SHUTDOWN_WAIT_TIME=300;
public static final String DUMMY="dummy-";
+ public static final String MATCH_ADDR="match-address";
+ public static final String MATCH_HOST="match-host";
+ public static final String MATCH_INTF="match-interface";
+
public static final String PREFIX="org.jgroups.protocols.";
public static final String XML_VALIDATION="jgroups.xml.validation";
View
8 src/org/jgroups/conf/PropertyConverters.java
@@ -253,8 +253,12 @@ public Object convert(Object obj, Class<?> propertyFieldType, String propertyNam
if(addr_scope != null)
retval=Util.getAddress(addr_scope);
- else
- retval=InetAddress.getByName(propertyValue);
+ else {
+ if(propertyValue.startsWith("match"))
+ retval=Util.getAddressByPatternMatch(propertyValue);
+ else
+ retval=InetAddress.getByName(propertyValue);
+ }
if(retval instanceof Inet4Address && retval.isMulticastAddress() && Util.getIpStackType() == StackType.IPv6) {
String tmp=prefix + propertyValue;
View
2  src/org/jgroups/protocols/TP.java
@@ -71,7 +71,7 @@
@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",
+ "are also recognized: GLOBAL, SITE_LOCAL, LINK_LOCAL, NON_LOOPBACK, match-interface, match-host, match-address",
defaultValueIPv4=Global.NON_LOOPBACK_ADDRESS, defaultValueIPv6=Global.NON_LOOPBACK_ADDRESS,
systemProperty={Global.BIND_ADDR},writable=false)
protected InetAddress bind_addr=null;
View
103 src/org/jgroups/util/Util.java
@@ -3836,6 +3836,67 @@ public static InetAddress getAddress(AddressScope scope) throws SocketException
return null ;
}
+ /**
+ * Returns a valid interface address based on a pattern. Iterates over all interfaces that are up and
+ * returns the first match, based on the address or interface name
+ * @param pattern Can be "match-addr:<pattern></pattern>" or "match-interface:<pattern></pattern>". Example:<p/>
+ * match-addr:192.168.*
+ * @return InetAddress or null if not found
+ */
+ public static InetAddress getAddressByPatternMatch(String pattern) throws Exception {
+ if(pattern == null) return null;
+ String real_pattern=null;
+ byte type=0; // 1=match-interface, 2: match-addr, 3: match-host,
+
+ if(pattern.startsWith(Global.MATCH_INTF)) {
+ type=1;
+ real_pattern=pattern.substring(Global.MATCH_INTF.length() +1);
+ }
+ else if(pattern.startsWith(Global.MATCH_ADDR)) {
+ type=2;
+ real_pattern=pattern.substring(Global.MATCH_ADDR.length() +1);
+ }
+ else if(pattern.startsWith(Global.MATCH_HOST)) {
+ type=3;
+ real_pattern=pattern.substring(Global.MATCH_HOST.length() +1);
+ }
+
+ if(real_pattern == null)
+ throw new IllegalArgumentException("expected " + Global.MATCH_ADDR + ":<pattern>, " +
+ Global.MATCH_HOST + ":<pattern> or " + Global.MATCH_INTF + ":<pattern>");
+
+ Pattern pat=Pattern.compile(real_pattern);
+ Enumeration intfs=NetworkInterface.getNetworkInterfaces();
+ while(intfs.hasMoreElements()) {
+ NetworkInterface intf=(NetworkInterface)intfs.nextElement();
+ try {
+ if(!intf.isUp())
+ continue;
+ switch(type) {
+ case 1: // match by interface name
+ String interface_name=intf.getName();
+ Matcher matcher=pat.matcher(interface_name);
+ if(matcher.matches())
+ return getAddress(intf, null);
+ break;
+ case 2: // match by host address
+ case 3: // match by host name
+ for(Enumeration<InetAddress> en=intf.getInetAddresses(); en.hasMoreElements();) {
+ InetAddress addr=en.nextElement();
+ String name=type == 3? addr.getHostName() : addr.getHostAddress();
+ matcher=pat.matcher(name);
+ if(matcher.matches())
+ return addr;
+ }
+ break;
+ }
+ }
+ catch (SocketException e) {
+ }
+ }
+ return null;
+ }
+
/**
* Returns the first address on the given interface on the current host, which satisfies scope
@@ -3846,30 +3907,32 @@ public static InetAddress getAddress(NetworkInterface intf, AddressScope scope)
StackType ip_version=Util.getIpStackType();
for(Enumeration addresses=intf.getInetAddresses(); addresses.hasMoreElements();) {
InetAddress addr=(InetAddress)addresses.nextElement();
- boolean match;
- switch(scope) {
- case GLOBAL:
- match=!addr.isLoopbackAddress() && !addr.isLinkLocalAddress() && !addr.isSiteLocalAddress();
- break;
- case SITE_LOCAL:
- match=addr.isSiteLocalAddress();
- break;
- case LINK_LOCAL:
- match=addr.isLinkLocalAddress();
- break;
- case LOOPBACK:
- match=addr.isLoopbackAddress();
- break;
- case NON_LOOPBACK:
- match=!addr.isLoopbackAddress();
- break;
- default:
- throw new IllegalArgumentException("scope " + scope + " is unknown");
+ boolean match=scope == null;
+ if(scope != null) {
+ switch(scope) {
+ case GLOBAL:
+ match=!addr.isLoopbackAddress() && !addr.isLinkLocalAddress() && !addr.isSiteLocalAddress();
+ break;
+ case SITE_LOCAL:
+ match=addr.isSiteLocalAddress();
+ break;
+ case LINK_LOCAL:
+ match=addr.isLinkLocalAddress();
+ break;
+ case LOOPBACK:
+ match=addr.isLoopbackAddress();
+ break;
+ case NON_LOOPBACK:
+ match=!addr.isLoopbackAddress();
+ break;
+ default:
+ throw new IllegalArgumentException("scope " + scope + " is unknown");
+ }
}
if(match) {
if((addr instanceof Inet4Address && ip_version == StackType.IPv4) ||
- (addr instanceof Inet6Address && ip_version == StackType.IPv6))
+ (addr instanceof Inet6Address && ip_version == StackType.IPv6))
return addr;
}
}
Please sign in to comment.
Something went wrong with that request. Please try again.