use of java.net.InterfaceAddress in project bnd by bndtools.
the class ConnectionSettings method isActive.
private boolean isActive(ProxyDTO proxy) throws SocketException {
if (!proxy.active)
return false;
String mask = proxy.mask;
if (mask == null)
return true;
String[] clauses = mask.split("\\s*,\\s*");
for (String clause : clauses) try {
String[] parts = clause.split("\\s*:\\s*");
Glob g = new Glob(parts[0]);
byte[] address = null;
int maskLength = 0;
if (parts.length > 1) {
String[] pp = parts[1].split("/");
address = InetAddress.getByName(pp[0]).getAddress();
maskLength = pp.length > 1 ? Integer.parseInt(pp[1]) : address.length * 8;
}
Enumeration<NetworkInterface> e = NetworkInterface.getNetworkInterfaces();
while (e.hasMoreElements()) {
NetworkInterface ni = e.nextElement();
if (ni == null)
continue;
if (!ni.isUp())
continue;
if (g.matcher(ni.getName()).matches()) {
if (address == null)
return true;
for (InterfaceAddress ia : ni.getInterfaceAddresses()) {
byte[] iaa = ia.getAddress().getAddress();
if (address.length != iaa.length)
continue;
if (maskLength != 0 && ia.getNetworkPrefixLength() != maskLength)
continue;
if (Arrays.equals(address, iaa))
return true;
}
}
}
} catch (Exception e) {
exception(e, "Failed to parse proxy 'mask' clause in settings: %s", clause);
}
return false;
}
use of java.net.InterfaceAddress in project ha-bridge by bwssytems.
the class LifxHome method createHome.
@Override
public Home createHome(BridgeSettings bridgeSettings) {
lifxMap = null;
aGsonHandler = null;
validLifx = bridgeSettings.getBridgeSettingsDescriptor().isValidLifx();
log.info("LifxDevice Home created." + (validLifx ? "" : " No LifxDevices configured."));
if (validLifx) {
try {
log.info("Open Lifx client....");
InetAddress configuredAddress = InetAddress.getByName(bridgeSettings.getBridgeSettingsDescriptor().getUpnpConfigAddress());
NetworkInterface networkInterface = NetworkInterface.getByInetAddress(configuredAddress);
InetAddress bcastInetAddr = null;
if (networkInterface != null) {
for (InterfaceAddress ifaceAddr : networkInterface.getInterfaceAddresses()) {
InetAddress addr = ifaceAddr.getAddress();
if (addr instanceof Inet4Address) {
bcastInetAddr = ifaceAddr.getBroadcast();
break;
}
}
}
if (bcastInetAddr != null) {
lifxMap = new HashMap<String, LifxDevice>();
log.info("Opening LFX Client with broadcast address: " + bcastInetAddr.getHostAddress());
client = new LFXClient(bcastInetAddr.getHostAddress());
client.getLights().addLightCollectionListener(new MyLightListener(lifxMap));
client.getGroups().addGroupCollectionListener(new MyGroupListener(lifxMap));
client.open(false);
aGsonHandler = new GsonBuilder().create();
} else {
log.warn("Could not open LIFX, no bcast addr available, check your upnp config address.");
client = null;
validLifx = false;
return this;
}
} catch (IOException e) {
log.warn("Could not open LIFX, with IO Exception", e);
client = null;
validLifx = false;
return this;
} catch (InterruptedException e) {
log.warn("Could not open LIFX, with Interruprted Exception", e);
client = null;
validLifx = false;
return this;
}
}
return this;
}
use of java.net.InterfaceAddress in project Terasology by MovingBlocks.
the class NetworkSystemImpl method host.
@Override
public void host(int port, boolean dedicatedServer) throws HostingFailedException {
if (mode == NetworkMode.NONE) {
try {
if (hibernationSettings.isPresent()) {
hibernationSettings.get().setHibernationAllowed(false);
}
mode = dedicatedServer ? NetworkMode.DEDICATED_SERVER : NetworkMode.LISTEN_SERVER;
for (EntityRef entity : entityManager.getEntitiesWith(NetworkComponent.class)) {
registerNetworkEntity(entity);
}
generateSerializationTables();
factory = new NioServerSocketChannelFactory(Executors.newCachedThreadPool(), Executors.newCachedThreadPool());
ServerBootstrap bootstrap = new ServerBootstrap(factory);
bootstrap.setPipelineFactory(new TerasologyServerPipelineFactory(this));
bootstrap.setOption("child.tcpNoDelay", true);
bootstrap.setOption("child.keepAlive", true);
Channel listenChannel = bootstrap.bind(new InetSocketAddress(port));
allChannels.add(listenChannel);
logger.info("Started server on port {}", port);
if (config.getServerMOTD() != null) {
logger.info("Server MOTD is \"{}\"", config.getServerMOTD());
} else {
logger.info("No server MOTD is defined");
}
// enumerate all network interfaces that listen
Enumeration<NetworkInterface> interfaces = NetworkInterface.getNetworkInterfaces();
while (interfaces.hasMoreElements()) {
NetworkInterface ifc = interfaces.nextElement();
if (!ifc.isLoopback()) {
for (InterfaceAddress ifadr : ifc.getInterfaceAddresses()) {
InetAddress adr = ifadr.getAddress();
logger.info("Listening on network interface \"{}\", hostname \"{}\" ({})", ifc.getDisplayName(), adr.getCanonicalHostName(), adr.getHostAddress());
}
}
}
nextNetworkTick = time.getRealTimeInMs();
} catch (SocketException e) {
throw new HostingFailedException("Could not identify network interfaces", e);
} catch (ChannelException e) {
if (e.getCause() instanceof BindException) {
throw new HostingFailedException("Port already in use (are you already hosting a game?)", e.getCause());
} else {
throw new HostingFailedException("Failed to host game", e.getCause());
}
}
}
}
use of java.net.InterfaceAddress in project graal by oracle.
the class JavaNetNetworkInterface method createNetworkInterface.
/*
* Translated from jdk/src/solaris/native/java/net/NetworkInterface.c?v=Java_1.8.0_40_b10
*/
// 630 /*
// 631 * Create a NetworkInterface object, populate the name and index, and
// 632 * populate the InetAddress array based on the IP addresses for this
// 633 * interface.
// 634 */
// 635 jobject createNetworkInterface(JNIEnv *env, netif *ifs) {
static NetworkInterface createNetworkInterface(netif ifs) {
// 636 jobject netifObj;
NetworkInterface netifObj;
Target_java_net_NetworkInterface netif_TJNNI;
// 637 jobject name;
String name;
// 638 jobjectArray addrArr;
InetAddress[] addrArr;
// 639 jobjectArray bindArr;
InterfaceAddress[] bindArr;
// 640 jobjectArray childArr;
NetworkInterface[] childArr;
// 641 netaddr *addrs;
/* `addrs` is unused. */
// 642 jint addr_index, addr_count, bind_index;
int addr_index;
int addr_count;
int bind_index;
// 643 jint child_count, child_index;
int child_count;
int child_index;
// 644 netaddr *addrP;
netaddr addrP;
// 645 netif *childP;
netif childP;
// 646 jobject tmp;
NetworkInterface tmp;
// 647
// 648 /*
// 649 * Create a NetworkInterface object and populate it
// 650 */
// 651 netifObj = (*env)->NewObject(env, ni_class, ni_ctrID);
netif_TJNNI = new Target_java_net_NetworkInterface();
netifObj = Util_java_net_NetworkInterface.toNetworkInterface(netif_TJNNI);
// 652 CHECK_NULL_RETURN(netifObj, NULL);
// 653 name = (*env)->NewStringUTF(env, ifs->name);
name = CTypeConversion.toJavaString(ifs.name);
// 654 CHECK_NULL_RETURN(name, NULL);
// 655 (*env)->SetObjectField(env, netifObj, ni_nameID, name);
netif_TJNNI.name = name;
// 656 (*env)->SetObjectField(env, netifObj, ni_descID, name);
netif_TJNNI.displayName = name;
// 657 (*env)->SetIntField(env, netifObj, ni_indexID, ifs->index);
netif_TJNNI.index = ifs.index;
// 658 (*env)->SetBooleanField(env, netifObj, ni_virutalID, ifs->virtual ? JNI_TRUE : JNI_FALSE);
netif_TJNNI.virtual = CTypeConversion.toBoolean(ifs.virtual);
// 659
// 660 /*
// 661 * Count the number of address on this interface
// 662 */
// 663 addr_count = 0;
addr_count = 0;
// 664 addrP = ifs->addr;
addrP = ifs.addr;
// 665 while (addrP != NULL) {
while (addrP != null) {
// 666 addr_count++;
addr_count++;
// 667 addrP = addrP->next;
addrP = addrP.next;
}
// 669
// 670 /*
// 671 * Create the array of InetAddresses
// 672 */
// 673 addrArr = (*env)->NewObjectArray(env, addr_count, ni_iacls, NULL);
addrArr = new InetAddress[addr_count];
/* `new` never returns null. */
// 674 if (addrArr == NULL) {
// 675 return NULL;
// 676 }
// 677
// 678 bindArr = (*env)->NewObjectArray(env, addr_count, ni_ibcls, NULL);
bindArr = new InterfaceAddress[addr_count];
/* `new` never returns null. */
// 679 if (bindArr == NULL) {
// 680 return NULL;
// 681 }
// 682 addrP = ifs->addr;
addrP = ifs.addr;
// 683 addr_index = 0;
addr_index = 0;
// 684 bind_index = 0;
bind_index = 0;
// 685 while (addrP != NULL) {
while (addrP != null) {
// 686 jobject iaObj = NULL;
Object iaObj = null;
// 687 jobject ibObj = NULL;
Object ibObj = null;
// 689 if (addrP->family == AF_INET) {
if (addrP.family == Socket.AF_INET()) {
// 690 iaObj = (*env)->NewObject(env, ni_ia4cls, ni_ia4ctrID);
final Inet4Address ia_I4A = Util_java_net_Inet4Address.new_Inet4Address();
iaObj = ia_I4A;
// 691 if (iaObj) {
if (iaObj != null) {
// 692 setInetAddress_addr(env, iaObj, htonl(((struct sockaddr_in*)addrP->addr)->sin_addr.s_addr));
JavaNetNetUtil.setInetAddress_addr(ia_I4A, NetinetIn.htonl(((NetinetIn.sockaddr_in) addrP.addr).sin_addr().s_addr()));
} else {
// 694 return NULL;
return null;
}
// 696 ibObj = (*env)->NewObject(env, ni_ibcls, ni_ibctrID);
final Target_java_net_InterfaceAddress ib_TJNIA = new Target_java_net_InterfaceAddress();
final InterfaceAddress ib_IA = Util_java_net_InterfaceAddress.toInterfaceAddress(ib_TJNIA);
ibObj = ib_IA;
// 697 if (ibObj) {
if (ibObj != null) {
// 698 (*env)->SetObjectField(env, ibObj, ni_ibaddressID, iaObj);
ib_TJNIA.address = ia_I4A;
// 699 if (addrP->brdcast) {
if (CTypeConversion.toBoolean(addrP.brdcast)) {
// 700 jobject ia2Obj = NULL;
Inet4Address ia2Obj = null;
// 701 ia2Obj = (*env)->NewObject(env, ni_ia4cls, ni_ia4ctrID);
ia2Obj = Util_java_net_Inet4Address.new_Inet4Address();
/* `new` never returns null. */
// 702 if (ia2Obj) {
// 703 setInetAddress_addr(env, ia2Obj, htonl(((struct sockaddr_in*)addrP->brdcast)->sin_addr.s_addr));
JavaNetNetUtil.setInetAddress_addr(ia2Obj, NetinetIn.htonl(((NetinetIn.sockaddr_in) addrP.brdcast).sin_addr().s_addr()));
// 704 (*env)->SetObjectField(env, ibObj, ni_ib4broadcastID, ia2Obj);
ib_TJNIA.broadcast = ia2Obj;
// 705 } else {
// 706 return NULL;
// 707 }
}
// 709 (*env)->SetShortField(env, ibObj, ni_ib4maskID, addrP->mask);
ib_TJNIA.maskLength = addrP.mask;
// 710 (*env)->SetObjectArrayElement(env, bindArr, bind_index++, ibObj);
bindArr[bind_index++] = ib_IA;
} else {
// 712 return NULL;
return null;
}
}
// 716 #ifdef AF_INET6
if (IsDefined.socket_AF_INET6()) {
// 717 if (addrP->family == AF_INET6) {
if (addrP.family == Socket.AF_INET6()) {
// 718 int scope=0;
int scope = 0;
// 719 iaObj = (*env)->NewObject(env, ni_ia6cls, ni_ia6ctrID);
final Inet6Address ia_I6A = Util_java_net_Inet6Address.new_Inet6Address();
iaObj = ia_I6A;
// 720 if (iaObj) {
if (iaObj != null) {
// 721 int ret = setInet6Address_ipaddress(env, iaObj, (char *)&(((struct sockaddr_in6*)addrP->addr)->sin6_addr));
final CCharPointer address = (CCharPointer) ((NetinetIn.sockaddr_in6) addrP.addr).sin6_addr();
int ret = JavaNetNetUtil.setInet6Address_ipaddress(ia_I6A, address);
// 722 if (ret == JNI_FALSE) {
if (ret == Target_jni.JNI_FALSE()) {
// 723 return NULL;
return null;
}
// 725
// 726 scope = ((struct sockaddr_in6*)addrP->addr)->sin6_scope_id;
scope = ((NetinetIn.sockaddr_in6) addrP.addr).sin6_scope_id();
// 728 if (scope != 0) { /* zero is default value, no need to set */
if (scope != 0) {
// 729 setInet6Address_scopeid(env, iaObj, scope);
JavaNetNetUtil.setInet6Address_scopeid(ia_I6A, scope);
// 730 setInet6Address_scopeifname(env, iaObj, netifObj);
JavaNetNetUtil.setInet6Address_scopeifname(ia_I6A, netifObj);
}
} else {
// 733 return NULL;
return null;
}
// 735 ibObj = (*env)->NewObject(env, ni_ibcls, ni_ibctrID);
Target_java_net_InterfaceAddress ib_TJNIA = new Target_java_net_InterfaceAddress();
InterfaceAddress ib_IA = Util_java_net_InterfaceAddress.toInterfaceAddress(ib_TJNIA);
ibObj = ib_IA;
// 736 if (ibObj) {
if (ibObj != null) {
// 737 (*env)->SetObjectField(env, ibObj, ni_ibaddressID, iaObj);
ib_TJNIA.address = (InetAddress) iaObj;
// 738 (*env)->SetShortField(env, ibObj, ni_ib4maskID, addrP->mask);
ib_TJNIA.maskLength = addrP.mask;
// 739 (*env)->SetObjectArrayElement(env, bindArr, bind_index++, ibObj);
bindArr[bind_index++] = ib_IA;
} else {
// 741 return NULL;
return null;
}
}
}
// 744 #endif
// 745
// 746 (*env)->SetObjectArrayElement(env, addrArr, addr_index++, iaObj);
addrArr[addr_index++] = (InetAddress) iaObj;
// 747 addrP = addrP->next;
addrP = addrP.next;
}
// 749
// 750 /*
// 751 * See if there is any virtual interface attached to this one.
// 752 */
// 753 child_count = 0;
child_count = 0;
// 754 childP = ifs->childs;
childP = ifs.childs;
// 755 while (childP) {
while (childP != null) {
// 756 child_count++;
child_count++;
// 757 childP = childP->next;
childP = childP.next;
}
// 759
// 760 childArr = (*env)->NewObjectArray(env, child_count, ni_class, NULL);
childArr = new NetworkInterface[child_count];
// 761 if (childArr == NULL) {
/* Dead code. */
// 762 return NULL;
// 763 }
// 764
// 765 /*
// 766 * Create the NetworkInterface instances for the sub-interfaces as
// 767 * well.
// 768 */
// 769 child_index = 0;
child_index = 0;
// 770 childP = ifs->childs;
childP = ifs.childs;
// 771 while(childP) {
while (childP != null) {
// 772 tmp = createNetworkInterface(env, childP);
tmp = createNetworkInterface(childP);
// 773 if (tmp == NULL) {
if (tmp == null) {
// 774 return NULL;
return null;
}
// 776 (*env)->SetObjectField(env, tmp, ni_parentID, netifObj);
Util_java_net_NetworkInterface.fromNetworkInterface(tmp).parent = netifObj;
// 777 (*env)->SetObjectArrayElement(env, childArr, child_index++, tmp);
childArr[child_index++] = tmp;
// 778 childP = childP->next;
childP = childP.next;
}
// 780 (*env)->SetObjectField(env, netifObj, ni_addrsID, addrArr);
netif_TJNNI.addrs = addrArr;
// 781 (*env)->SetObjectField(env, netifObj, ni_bindsID, bindArr);
netif_TJNNI.bindings = bindArr;
// 782 (*env)->SetObjectField(env, netifObj, ni_childsID, childArr);
netif_TJNNI.childs = childArr;
// 785 return netifObj;
return netifObj;
}
use of java.net.InterfaceAddress in project x-pipe by ctripcorp.
the class IpUtils method getFistNonLocalIpv4ServerAddress.
public static InetAddress getFistNonLocalIpv4ServerAddress(String ipPrefixPrefer) {
InetAddress first = null;
try {
Enumeration<NetworkInterface> interfaces = NetworkInterface.getNetworkInterfaces();
if (interfaces == null) {
return null;
}
while (interfaces.hasMoreElements()) {
NetworkInterface current = interfaces.nextElement();
if (current.isLoopback()) {
continue;
}
List<InterfaceAddress> addresses = current.getInterfaceAddresses();
if (addresses.size() == 0) {
continue;
}
for (InterfaceAddress interfaceAddress : addresses) {
InetAddress address = interfaceAddress.getAddress();
if (address instanceof Inet4Address) {
if (first == null) {
first = address;
}
if (address.getHostAddress().startsWith(ipPrefixPrefer)) {
return address;
}
}
}
}
} catch (SocketException e) {
}
if (first != null) {
return first;
}
throw new IllegalStateException("[can not find a qualified address]");
}
Aggregations