use of org.eclipse.kura.net.IPAddress in project kura by eclipse.
the class GwtNetworkServiceImpl method updateDeviceFirewallOpenPorts.
@Override
public void updateDeviceFirewallOpenPorts(GwtXSRFToken xsrfToken, List<GwtFirewallOpenPortEntry> entries) throws GwtKuraException {
checkXSRFToken(xsrfToken);
NetworkAdminService nas = ServiceLocator.getInstance().getService(NetworkAdminService.class);
List<FirewallOpenPortConfigIP<? extends IPAddress>> firewallOpenPortConfigIPs = new ArrayList<FirewallOpenPortConfigIP<? extends IPAddress>>();
s_logger.debug("updating open ports");
try {
for (GwtFirewallOpenPortEntry entry : entries) {
String network = null;
String prefix = null;
if (entry.getPermittedNetwork() != null) {
String[] parts = entry.getPermittedNetwork().split("/");
network = parts[0];
prefix = parts[1];
}
FirewallOpenPortConfigIP<IP4Address> firewallOpenPortConfigIP = new FirewallOpenPortConfigIP4();
if (entry.getPortRange() != null) {
if (entry.getPortRange().indexOf(':') > 0) {
firewallOpenPortConfigIP.setPortRange(entry.getPortRange());
} else {
firewallOpenPortConfigIP.setPort(Integer.parseInt(entry.getPortRange()));
}
}
firewallOpenPortConfigIP.setProtocol(NetProtocol.valueOf(GwtSafeHtmlUtils.htmlEscape(entry.getProtocol())));
if (network != null && prefix != null) {
firewallOpenPortConfigIP.setPermittedNetwork(new NetworkPair<IP4Address>((IP4Address) IPAddress.parseHostAddress(network), Short.parseShort(prefix)));
}
firewallOpenPortConfigIP.setPermittedInterfaceName(GwtSafeHtmlUtils.htmlEscape(entry.getPermittedInterfaceName()));
firewallOpenPortConfigIP.setUnpermittedInterfaceName(GwtSafeHtmlUtils.htmlEscape(entry.getUnpermittedInterfaceName()));
firewallOpenPortConfigIP.setPermittedMac(GwtSafeHtmlUtils.htmlEscape(entry.getPermittedMAC()));
firewallOpenPortConfigIP.setSourcePortRange(GwtSafeHtmlUtils.htmlEscape(entry.getSourcePortRange()));
s_logger.debug("adding open port entry for {}", entry.getPortRange());
firewallOpenPortConfigIPs.add(firewallOpenPortConfigIP);
}
nas.setFirewallOpenPortConfiguration(firewallOpenPortConfigIPs);
} catch (KuraException e) {
s_logger.warn("Exception while updating firewall open ports", e);
throw new GwtKuraException(GwtKuraErrorCode.INTERNAL_ERROR, e);
} catch (NumberFormatException e) {
s_logger.warn("Exception while updating firewall open ports", e);
throw new GwtKuraException(GwtKuraErrorCode.INTERNAL_ERROR, e);
} catch (UnknownHostException e) {
s_logger.warn("Exception while updating firewall open ports", e);
throw new GwtKuraException(GwtKuraErrorCode.INTERNAL_ERROR, e);
}
}
use of org.eclipse.kura.net.IPAddress in project kura by eclipse.
the class DnsMonitorServiceImpl method updateDnsProxyConfig.
private void updateDnsProxyConfig() {
this.m_enabled = false;
this.m_allowedNetworks = new HashSet<NetworkPair<IP4Address>>();
this.m_forwarders = new HashSet<IP4Address>();
if (this.m_networkConfiguration != null) {
if (this.m_networkConfiguration.getNetInterfaceConfigs() != null) {
List<NetInterfaceConfig<? extends NetInterfaceAddressConfig>> netInterfaceConfigs = this.m_networkConfiguration.getNetInterfaceConfigs();
for (NetInterfaceConfig<? extends NetInterfaceAddressConfig> netInterfaceConfig : netInterfaceConfigs) {
if (netInterfaceConfig.getType() == NetInterfaceType.ETHERNET || netInterfaceConfig.getType() == NetInterfaceType.WIFI || netInterfaceConfig.getType() == NetInterfaceType.MODEM) {
try {
getAllowedNetworks(netInterfaceConfig);
} catch (KuraException e) {
s_logger.error("Error updating dns proxy", e);
}
}
}
}
}
Set<IPAddress> dnsServers = LinuxDns.getInstance().getDnServers();
if (dnsServers != null && !dnsServers.isEmpty()) {
for (IPAddress dnsServer : dnsServers) {
s_logger.debug("Found DNS Server: {}", dnsServer.getHostAddress());
this.m_forwarders.add((IP4Address) dnsServer);
}
}
try {
LinuxNamed linuxNamed = LinuxNamed.getInstance();
s_logger.debug("Disabling DNS proxy");
linuxNamed.disable();
s_logger.debug("Writing config");
DnsServerConfigIP4 dnsServerConfigIP4 = new DnsServerConfigIP4(this.m_forwarders, this.m_allowedNetworks);
linuxNamed.setConfig(dnsServerConfigIP4);
if (this.m_enabled) {
sleep(500);
s_logger.debug("Starting DNS proxy");
linuxNamed.enable();
} else {
s_logger.debug("DNS proxy not enabled");
}
} catch (KuraException e) {
e.printStackTrace();
}
}
use of org.eclipse.kura.net.IPAddress in project kura by eclipse.
the class DnsMonitorServiceImpl method setDnsServers.
private void setDnsServers(Set<IPAddress> newServers) {
LinuxDns linuxDns = LinuxDns.getInstance();
Set<IPAddress> currentServers = linuxDns.getDnServers();
if (newServers == null || newServers.isEmpty()) {
s_logger.warn("Not Setting DNS servers to empty");
return;
}
if (currentServers != null && !currentServers.isEmpty()) {
if (!currentServers.equals(newServers)) {
s_logger.info("Change to DNS - setting dns servers: " + newServers);
linuxDns.setDnServers(newServers);
} else {
s_logger.debug("No change to DNS servers - not updating");
}
} else {
s_logger.info("Current DNS servers are null - setting dns servers: " + newServers);
linuxDns.setDnServers(newServers);
}
}
use of org.eclipse.kura.net.IPAddress in project kura by eclipse.
the class DnsMonitorServiceImpl method activate.
protected void activate(ComponentContext componentContext) {
s_logger.debug("Activating DnsProxyMonitor Service...");
Dictionary<String, String[]> d = new Hashtable<String, String[]>();
d.put(EventConstants.EVENT_TOPIC, EVENT_TOPICS);
componentContext.getBundleContext().registerService(EventHandler.class.getName(), this, d);
try {
this.m_networkConfiguration = this.m_netConfigService.getNetworkConfiguration();
} catch (KuraException e) {
s_logger.error("Could not get initial network configuration", e);
}
stopThread = new AtomicBoolean();
// FIXME - brute force handler for DNS updates
// m_executorUtil = ExecutorUtil.getInstance();
this.m_executor = Executors.newSingleThreadExecutor();
stopThread.set(false);
s_monitorTask = this.m_executor.submit(new Runnable() {
@Override
public void run() {
while (!stopThread.get()) {
Thread.currentThread().setName("DnsMonitorServiceImpl");
Set<IPAddress> dnsServers = LinuxDns.getInstance().getDnServers();
// Check that resolv.conf matches what is configured
Set<IPAddress> configuredServers = getConfiguredDnsServers();
if (!configuredServers.equals(dnsServers)) {
setDnsServers(configuredServers);
dnsServers = configuredServers;
}
Set<IP4Address> forwarders = new HashSet<IP4Address>();
if (dnsServers != null && !dnsServers.isEmpty()) {
for (IPAddress dnsServer : dnsServers) {
s_logger.debug("Found DNS Server: {}", dnsServer.getHostAddress());
forwarders.add((IP4Address) dnsServer);
}
}
if (forwarders != null && !forwarders.isEmpty()) {
if (!forwarders.equals(DnsMonitorServiceImpl.this.m_forwarders)) {
// there was a change - deal with it
s_logger.info("Detected DNS resolv.conf change - restarting DNS proxy");
DnsMonitorServiceImpl.this.m_forwarders = forwarders;
try {
LinuxNamed linuxNamed = LinuxNamed.getInstance();
DnsServerConfigIP4 currentDnsServerConfig = linuxNamed.getDnsServerConfig();
DnsServerConfigIP4 newDnsServerConfig = new DnsServerConfigIP4(DnsMonitorServiceImpl.this.m_forwarders, DnsMonitorServiceImpl.this.m_allowedNetworks);
if (currentDnsServerConfig.equals(newDnsServerConfig)) {
s_logger.debug("DNS server config has changed - updating from " + currentDnsServerConfig + " to " + newDnsServerConfig);
s_logger.debug("Disabling DNS proxy");
linuxNamed.disable();
s_logger.debug("Writing config");
linuxNamed.setConfig(newDnsServerConfig);
if (DnsMonitorServiceImpl.this.m_enabled) {
sleep(500);
s_logger.debug("Starting DNS proxy");
linuxNamed.enable();
} else {
s_logger.debug("DNS proxy not enabled");
}
}
} catch (KuraException e) {
e.printStackTrace();
}
}
}
try {
monitorWait();
} catch (InterruptedException e) {
s_logger.debug("DNS monitor interrupted", e);
}
}
}
});
}
use of org.eclipse.kura.net.IPAddress in project kura by eclipse.
the class RouteFile method findRoute.
private RouteConfig findRoute(Properties props, int index) {
RouteConfig route = null;
IPAddress dest = null;
IPAddress gw = null;
IPAddress mask = null;
if (!props.containsKey("ADDRESS" + index)) {
return null;
}
try {
dest = IPAddress.parseHostAddress((String) props.get("ADDRESS" + index));
gw = IPAddress.parseHostAddress((String) props.get("GATEWAY" + index));
mask = IPAddress.parseHostAddress((String) props.get("NETMASK" + index));
} catch (UnknownHostException e) {
e.printStackTrace();
}
if (dest != null && gw != null && mask != null) {
if (dest instanceof IP4Address) {
route = new RouteConfigIP4((IP4Address) dest, (IP4Address) gw, (IP4Address) mask, this.interfaceName, -1);
} else if (dest instanceof IP6Address) {
route = new RouteConfigIP6((IP6Address) dest, (IP6Address) gw, (IP6Address) mask, this.interfaceName, -1);
}
}
return route;
}
Aggregations