use of org.eclipse.kura.linux.net.dns.LinuxDns 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.linux.net.dns.LinuxDns in project kura by eclipse.
the class Ppp method disconnect.
@Override
public void disconnect() throws KuraException {
s_logger.info("disconnecting :: stopping PPP monitor ...");
PppLinux.disconnect(this.m_iface, this.m_port);
try {
LinuxDns linuxDns = LinuxDns.getInstance();
if (linuxDns.isPppDnsSet()) {
linuxDns.unsetPppDns();
}
} catch (Exception e) {
throw new KuraException(KuraErrorCode.INTERNAL_ERROR, e);
}
}
use of org.eclipse.kura.linux.net.dns.LinuxDns in project kura by eclipse.
the class GenericNetworkInterface method getCurrentConfig.
protected static NetInterfaceConfig<?> getCurrentConfig(String interfaceName, NetInterfaceType type, NetInterfaceStatus status, boolean dhcpServerEnabled, boolean passDns, Properties kuraProps) throws KuraException {
try {
NetInterfaceConfig<?> netInterfaceConfig = null;
boolean autoConnect = false;
int mtu = -1;
boolean dhcp = false;
IP4Address address = null;
String ipAddress = null;
String prefixString = null;
String netmask = null;
String gateway = null;
boolean interfaceEnabled = false;
if (kuraProps != null) {
String onBoot = kuraProps.getProperty("ONBOOT");
if ("yes".equals(onBoot)) {
autoConnect = true;
// we are enabled - just not sure if for LAN or WAN
if (status == NetInterfaceStatus.netIPv4StatusUnknown) {
interfaceEnabled = true;
}
} else {
autoConnect = false;
}
// override MTU with what is in config if it is present
String stringMtu = kuraProps.getProperty("MTU");
if (stringMtu == null) {
try {
mtu = LinuxNetworkUtil.getCurrentMtu(interfaceName);
} catch (KuraException e) {
// just assume ???
if (interfaceName.equals("lo")) {
mtu = 16436;
} else {
mtu = 1500;
}
}
} else {
mtu = Short.parseShort(stringMtu);
}
// get the bootproto
String bootproto = kuraProps.getProperty("BOOTPROTO");
if (bootproto == null) {
bootproto = "static";
}
// get the defroute
String defroute = kuraProps.getProperty("DEFROUTE");
if (defroute == null) {
defroute = "no";
}
if (interfaceEnabled) {
if (defroute.equals("yes")) {
status = NetInterfaceStatus.netIPv4StatusEnabledWAN;
} else {
status = NetInterfaceStatus.netIPv4StatusEnabledLAN;
}
}
// check for dhcp or static configuration
try {
ipAddress = kuraProps.getProperty("IPADDR");
prefixString = kuraProps.getProperty("PREFIX");
netmask = kuraProps.getProperty("NETMASK");
kuraProps.getProperty("BROADCAST");
try {
gateway = kuraProps.getProperty("GATEWAY");
s_logger.debug("got gateway for {}: {}", interfaceName, gateway);
} catch (Exception e) {
s_logger.warn("missing gateway stanza for " + interfaceName);
}
if (bootproto.equals("dhcp")) {
s_logger.debug("currently set for DHCP");
dhcp = true;
ipAddress = null;
netmask = null;
} else {
s_logger.debug("currently set for static address");
dhcp = false;
}
} catch (Exception e) {
e.printStackTrace();
throw new KuraException(KuraErrorCode.INTERNAL_ERROR, "malformatted config file: " + NET_CONFIGURATION_DIRECTORY + "ifcfg-" + interfaceName);
}
if (ipAddress != null && !ipAddress.isEmpty()) {
address = (IP4Address) IPAddress.parseHostAddress(ipAddress);
}
// make sure at least prefix or netmask is present if static
if (!dhcp && prefixString == null && netmask == null) {
throw new KuraException(KuraErrorCode.INTERNAL_ERROR, "malformatted config file: " + NET_CONFIGURATION_DIRECTORY + "ifcfg-" + interfaceName + " must contain NETMASK and/or PREFIX");
}
}
ConnectionInfo conInfo = new ConnectionInfoImpl(interfaceName);
LinuxDns dnsService = LinuxDns.getInstance();
// note - we only add the fields we need/care about from a configuration standpoint
if (type == NetInterfaceType.LOOPBACK) {
s_logger.debug("Adding a Loopback interface");
netInterfaceConfig = new LoopbackInterfaceConfigImpl(interfaceName);
((LoopbackInterfaceImpl<?>) netInterfaceConfig).setMTU(mtu);
// loopback autoConnect
((LoopbackInterfaceImpl<?>) netInterfaceConfig).setAutoConnect(true);
// should always be true?
((LoopbackInterfaceImpl<?>) netInterfaceConfig).setLoopback(true);
List<NetInterfaceAddressConfig> netInterfaceAddressConfigs = new ArrayList<NetInterfaceAddressConfig>();
List<NetInterfaceAddress> netInterfaceAddresses = new ArrayList<NetInterfaceAddress>();
NetInterfaceAddressConfigImpl netInterfaceAddressConfig = new NetInterfaceAddressConfigImpl();
netInterfaceAddressConfigs.add(netInterfaceAddressConfig);
netInterfaceAddresses.add(netInterfaceAddressConfig);
LinuxIfconfig ifconfig = LinuxNetworkUtil.getInterfaceConfiguration(interfaceName);
if (ifconfig != null && ifconfig.isUp()) {
netInterfaceAddressConfig.setAddress(IPAddress.parseHostAddress(ifconfig.getInetAddress()));
netInterfaceAddressConfig.setBroadcast(IPAddress.parseHostAddress(ifconfig.getInetBcast()));
netInterfaceAddressConfig.setNetmask(IPAddress.parseHostAddress(ifconfig.getInetMask()));
netInterfaceAddressConfig.setNetworkPrefixLength(NetworkUtil.getNetmaskShortForm(ifconfig.getInetMask()));
netInterfaceAddressConfig.setGateway(conInfo.getGateway());
if (dhcp) {
netInterfaceAddressConfig.setDnsServers(dnsService.getDhcpDnsServers(interfaceName, netInterfaceAddressConfig.getAddress()));
} else {
netInterfaceAddressConfig.setDnsServers(conInfo.getDnsServers());
}
}
((LoopbackInterfaceConfigImpl) netInterfaceConfig).setNetInterfaceAddresses(netInterfaceAddressConfigs);
List<NetConfig> netConfigs = new ArrayList<NetConfig>();
netInterfaceAddressConfig.setNetConfigs(netConfigs);
// FIXME - hardcoded
NetConfig netConfig = new NetConfigIP4(NetInterfaceStatus.netIPv4StatusEnabledLAN, true);
((NetConfigIP4) netConfig).setAddress(address);
((NetConfigIP4) netConfig).setDhcp(dhcp);
((NetConfigIP4) netConfig).setDnsServers(null);
((NetConfigIP4) netConfig).setDomains(null);
((NetConfigIP4) netConfig).setGateway(null);
((NetConfigIP4) netConfig).setNetworkPrefixLength((short) 8);
((NetConfigIP4) netConfig).setSubnetMask((IP4Address) IPAddress.parseHostAddress("255.0.0.0"));
((NetConfigIP4) netConfig).setWinsServers(null);
netConfigs.add(netConfig);
} else if (type == NetInterfaceType.ETHERNET) {
s_logger.debug("Adding an Ethernet interface - {}", interfaceName);
netInterfaceConfig = new EthernetInterfaceConfigImpl(interfaceName);
((EthernetInterfaceImpl<?>) netInterfaceConfig).setMTU(mtu);
((EthernetInterfaceImpl<?>) netInterfaceConfig).setAutoConnect(autoConnect);
((EthernetInterfaceImpl<?>) netInterfaceConfig).setLoopback(false);
List<NetInterfaceAddressConfig> netInterfaceAddressConfigs = new ArrayList<NetInterfaceAddressConfig>();
List<NetInterfaceAddress> netInterfaceAddresses = new ArrayList<NetInterfaceAddress>();
NetInterfaceAddressConfigImpl netInterfaceAddressConfig = new NetInterfaceAddressConfigImpl();
netInterfaceAddressConfigs.add(netInterfaceAddressConfig);
netInterfaceAddresses.add(netInterfaceAddressConfig);
LinuxIfconfig ifconfig = LinuxNetworkUtil.getInterfaceConfiguration(interfaceName);
if (ifconfig != null) {
((EthernetInterfaceImpl<?>) netInterfaceConfig).setHardwareAddress(ifconfig.getMacAddressBytes());
if (ifconfig.isUp()) {
try {
netInterfaceAddressConfig.setAddress(IPAddress.parseHostAddress(ifconfig.getInetAddress()));
netInterfaceAddressConfig.setBroadcast(IPAddress.parseHostAddress(ifconfig.getInetBcast()));
netInterfaceAddressConfig.setNetmask(IPAddress.parseHostAddress(ifconfig.getInetMask()));
netInterfaceAddressConfig.setNetworkPrefixLength(NetworkUtil.getNetmaskShortForm(ifconfig.getInetMask()));
netInterfaceAddressConfig.setGateway(conInfo.getGateway());
if (dhcp) {
netInterfaceAddressConfig.setDnsServers(dnsService.getDhcpDnsServers(interfaceName, netInterfaceAddressConfig.getAddress()));
} else {
netInterfaceAddressConfig.setDnsServers(conInfo.getDnsServers());
}
} catch (KuraException e) {
s_logger.warn("The interface went down " + interfaceName + " not including current state in status because it is not up");
netInterfaceAddressConfig.setAddress(null);
netInterfaceAddressConfig.setBroadcast(null);
netInterfaceAddressConfig.setNetmask(null);
netInterfaceAddressConfig.setNetworkPrefixLength((short) -1);
netInterfaceAddressConfig.setGateway(null);
netInterfaceAddressConfig.setDnsServers(null);
}
}
}
((EthernetInterfaceConfigImpl) netInterfaceConfig).setNetInterfaceAddresses(netInterfaceAddressConfigs);
// add the config
List<NetConfig> netConfigs = new ArrayList<NetConfig>();
netInterfaceAddressConfig.setNetConfigs(netConfigs);
NetConfigIP4 netConfig = new NetConfigIP4(NetInterfaceStatus.netIPv4StatusDisabled, autoConnect);
setNetConfigIP4(netConfig, status, autoConnect, dhcp, address, gateway, prefixString, netmask, kuraProps);
netConfigs.add(netConfig);
if (dhcpServerEnabled) {
// add DHCP server configuration to the list
DhcpServerImpl dhcpServer = DhcpServerFactory.getInstance(interfaceName, dhcpServerEnabled, passDns);
DhcpServerConfig4 dhcpServerConfig = dhcpServer.getDhcpServerConfig(dhcpServerEnabled, passDns);
if (dhcpServerConfig != null) {
netConfigs.add(dhcpServerConfig);
}
}
} else if (type == NetInterfaceType.WIFI) {
s_logger.debug("Adding a Wireless interface - {}", interfaceName);
WifiInterfaceConfigImpl wifiInterfaceConfig = new WifiInterfaceConfigImpl(interfaceName);
netInterfaceConfig = wifiInterfaceConfig;
wifiInterfaceConfig.setMTU(mtu);
wifiInterfaceConfig.setAutoConnect(autoConnect);
wifiInterfaceConfig.setLoopback(false);
List<WifiInterfaceAddressConfig> wifiInterfaceAddressConfigs = new ArrayList<WifiInterfaceAddressConfig>();
List<WifiInterfaceAddress> wifiInterfaceAddresses = new ArrayList<WifiInterfaceAddress>();
WifiInterfaceAddressConfigImpl wifiInterfaceAddressConfig = new WifiInterfaceAddressConfigImpl();
wifiInterfaceAddressConfigs.add(wifiInterfaceAddressConfig);
wifiInterfaceAddresses.add(wifiInterfaceAddressConfig);
String currentSSID = LinuxNetworkUtil.getSSID(interfaceName);
LinuxIfconfig ifconfig = LinuxNetworkUtil.getInterfaceConfiguration(interfaceName);
if (ifconfig != null) {
wifiInterfaceConfig.setHardwareAddress(ifconfig.getMacAddressBytes());
if (ifconfig.isUp()) {
wifiInterfaceAddressConfig.setAddress(IPAddress.parseHostAddress(ifconfig.getInetAddress()));
wifiInterfaceAddressConfig.setBroadcast(IPAddress.parseHostAddress(ifconfig.getInetBcast()));
String currentNetmask = ifconfig.getInetMask();
if (currentNetmask != null) {
wifiInterfaceAddressConfig.setNetmask(IPAddress.parseHostAddress(currentNetmask));
wifiInterfaceAddressConfig.setNetworkPrefixLength(NetworkUtil.getNetmaskShortForm(currentNetmask));
}
wifiInterfaceAddressConfig.setBitrate(LinuxNetworkUtil.getWifiBitrate(interfaceName));
wifiInterfaceAddressConfig.setGateway(conInfo.getGateway());
if (dhcp) {
wifiInterfaceAddressConfig.setDnsServers(dnsService.getDhcpDnsServers(interfaceName, wifiInterfaceAddressConfig.getAddress()));
} else {
wifiInterfaceAddressConfig.setDnsServers(conInfo.getDnsServers());
}
WifiAccessPointImpl ap = null;
if (currentSSID != null) {
s_logger.debug("Adding access point SSID: {}", currentSSID);
ap = new WifiAccessPointImpl(currentSSID);
// TODO: fill in other info
ap.setMode(WifiMode.INFRA);
List<Long> bitrate = new ArrayList<Long>();
bitrate.add(54000000L);
ap.setBitrate(bitrate);
ap.setFrequency(12345);
ap.setHardwareAddress("20AA4B8A6442".getBytes());
ap.setRsnSecurity(EnumSet.allOf(WifiSecurity.class));
ap.setStrength(1234);
ap.setWpaSecurity(EnumSet.allOf(WifiSecurity.class));
}
wifiInterfaceAddressConfig.setWifiAccessPoint(ap);
}
}
// mode
WifiMode wifiMode = WifiMode.UNKNOWN;
s_logger.debug("Get WifiMode...");
try {
// get from config file
String mode = kuraProps.getProperty("MODE");
if (mode != null) {
s_logger.debug("Getting wifi mode from {}", kuraFile.getAbsolutePath());
if (mode.equalsIgnoreCase("Managed")) {
wifiMode = WifiMode.INFRA;
} else if (mode.equalsIgnoreCase("Master")) {
wifiMode = WifiMode.MASTER;
} else if (mode.equalsIgnoreCase("Ad-Hoc")) {
wifiMode = WifiMode.ADHOC;
} else {
wifiMode = WifiMode.valueOf(mode);
}
} else {
// get current setting using iwconfig
s_logger.debug("Getting wifi mode from iwconfig");
wifiMode = LinuxNetworkUtil.getWifiMode(interfaceName);
}
} catch (Exception e) {
// leave as unknown
}
s_logger.debug("Current WifiMode: {}", wifiMode);
wifiInterfaceAddressConfig.setMode(wifiMode);
wifiInterfaceConfig.setNetInterfaceAddresses(wifiInterfaceAddressConfigs);
// TODO: fix
wifiInterfaceConfig.setCapabilities(EnumSet.allOf(Capability.class));
// add the configs - one for client (managed) mode, one for access point (master) mode
List<NetConfig> netConfigs = new ArrayList<NetConfig>();
wifiInterfaceAddressConfig.setNetConfigs(netConfigs);
// get the NetConfig
NetConfigIP4 netConfig = new NetConfigIP4(NetInterfaceStatus.netIPv4StatusDisabled, autoConnect);
setNetConfigIP4(netConfig, status, autoConnect, dhcp, address, gateway, prefixString, netmask, kuraProps);
netConfigs.add(netConfig);
// get the wpa_supplicant configuration
WifiConfig wifiClientConfig = new WifiConfig();
setWifiClientConfig(interfaceName, wifiClientConfig, wifiMode);
// get the hostapd configuration
WifiConfig wifiAPConfig = new WifiConfig();
setWifiAccessPointConfig(wifiAPConfig);
// add WiFi configurations to the list
netConfigs.add(wifiClientConfig);
netConfigs.add(wifiAPConfig);
if (dhcpServerEnabled) {
// add DHCP server configuration to the list
DhcpServerImpl dhcpServer = DhcpServerFactory.getInstance(interfaceName, dhcpServerEnabled, passDns);
DhcpServerConfig4 dhcpServerConfig = dhcpServer.getDhcpServerConfig(dhcpServerEnabled, passDns);
if (dhcpServerConfig != null) {
netConfigs.add(dhcpServerConfig);
}
}
} else if (type == NetInterfaceType.MODEM) {
s_logger.debug("Adding a Modem interface");
netInterfaceConfig = new ModemInterfaceConfigImpl(interfaceName);
((ModemInterfaceConfigImpl) netInterfaceConfig).setMTU(mtu);
((ModemInterfaceConfigImpl) netInterfaceConfig).setAutoConnect(autoConnect);
((ModemInterfaceConfigImpl) netInterfaceConfig).setLoopback(false);
((ModemInterfaceConfigImpl) netInterfaceConfig).setPointToPoint(true);
List<ModemInterfaceAddressConfig> modemInterfaceAddressConfigs = new ArrayList<ModemInterfaceAddressConfig>();
List<ModemInterfaceAddress> netInterfaceAddresses = new ArrayList<ModemInterfaceAddress>();
ModemInterfaceAddressConfigImpl netInterfaceAddressConfig = new ModemInterfaceAddressConfigImpl();
modemInterfaceAddressConfigs.add(netInterfaceAddressConfig);
netInterfaceAddresses.add(netInterfaceAddressConfig);
LinuxIfconfig ifconfig = LinuxNetworkUtil.getInterfaceConfiguration(interfaceName);
if (ifconfig != null) {
((ModemInterfaceConfigImpl) netInterfaceConfig).setHardwareAddress(ifconfig.getMacAddressBytes());
if (ifconfig.isUp()) {
netInterfaceAddressConfig.setAddress(IPAddress.parseHostAddress(ifconfig.getInetAddress()));
netInterfaceAddressConfig.setBroadcast(IPAddress.parseHostAddress(ifconfig.getInetBcast()));
netInterfaceAddressConfig.setNetmask(IPAddress.parseHostAddress(ifconfig.getInetMask()));
netInterfaceAddressConfig.setNetworkPrefixLength(NetworkUtil.getNetmaskShortForm(ifconfig.getInetMask()));
netInterfaceAddressConfig.setGateway(conInfo.getGateway());
netInterfaceAddressConfig.setDnsServers(conInfo.getDnsServers());
}
}
((ModemInterfaceConfigImpl) netInterfaceConfig).setNetInterfaceAddresses(modemInterfaceAddressConfigs);
// add the config
List<NetConfig> netConfigs = new ArrayList<NetConfig>();
netInterfaceAddressConfig.setNetConfigs(netConfigs);
NetConfigIP4 netConfig = new NetConfigIP4(NetInterfaceStatus.netIPv4StatusDisabled, autoConnect);
setNetConfigIP4(netConfig, status, autoConnect, dhcp, address, gateway, prefixString, netmask, kuraProps);
netConfigs.add(netConfig);
} else {
s_logger.warn("Unsupported Type: " + type);
}
return netInterfaceConfig;
} catch (UnknownHostException e) {
throw new KuraException(KuraErrorCode.INTERNAL_ERROR, e);
}
}
use of org.eclipse.kura.linux.net.dns.LinuxDns in project kura by eclipse.
the class DnsMonitorServiceImpl method getConfiguredDnsServers.
// Get a list of dns servers for the specified NetInterfaceConfig
private Set<IPAddress> getConfiguredDnsServers(NetInterfaceConfig<? extends NetInterfaceAddressConfig> netInterfaceConfig) throws KuraException {
String interfaceName = netInterfaceConfig.getName();
s_logger.trace("Getting dns servers for {}", interfaceName);
LinuxDns linuxDns = LinuxDns.getInstance();
LinkedHashSet<IPAddress> serverList = new LinkedHashSet<IPAddress>();
for (NetInterfaceAddressConfig netInterfaceAddressConfig : netInterfaceConfig.getNetInterfaceAddresses()) {
for (NetConfig netConfig : netInterfaceAddressConfig.getConfigs()) {
if (netConfig instanceof NetConfigIP4) {
NetConfigIP4 netConfigIP4 = (NetConfigIP4) netConfig;
List<IP4Address> userServers = netConfigIP4.getDnsServers();
if (netConfigIP4.isDhcp()) {
// If DHCP but there are user defined entries, use those instead
if (userServers != null && !userServers.isEmpty()) {
s_logger.debug("Configured for DHCP with user-defined servers - adding: {}", userServers);
serverList.addAll(userServers);
} else {
if (netInterfaceConfig.getType().equals(NetInterfaceType.MODEM)) {
// FIXME - don't like this
// cannot use interfaceName here because it one config behind
int pppNo = ((ModemInterfaceConfigImpl) netInterfaceConfig).getPppNum();
if (LinuxNetworkUtil.hasAddress("ppp" + pppNo)) {
List<IPAddress> servers = linuxDns.getPppDnServers();
if (servers != null) {
s_logger.debug("Adding PPP dns servers: {}", servers);
serverList.addAll(servers);
}
}
} else {
String currentAddress = LinuxNetworkUtil.getCurrentIpAddress(interfaceName);
List<IPAddress> servers = linuxDns.getDhcpDnsServers(interfaceName, currentAddress);
if (servers != null) {
s_logger.debug("Configured for DHCP - adding DHCP servers: {}", servers);
serverList.addAll(servers);
}
}
}
} else {
// If static, use the user defined entries
s_logger.debug("Configured for static - adding user-defined servers: {}", userServers);
serverList.addAll(userServers);
}
}
}
}
return serverList;
}
Aggregations