Testing changes to support wireless network adapters configuration.

All network setup GUI related code now part of gui/network_setup.cpp,
old code in setting_helpers.cpp commented.
Removed recursive menu call in network setup gui.
When compiled with -DDEBUG, ifup/ifdown not executed, only printed.


git-svn-id: file:///home/bas/coolstream_public_svn/THIRDPARTY/applications/neutrino-experimental@1305 e54a6e83-5905-42d5-8d5c-058d10e6a962
This commit is contained in:
focus
2011-03-16 12:57:09 +00:00
parent c49b09e7c9
commit 18f85cd9cd
5 changed files with 482 additions and 83 deletions

View File

@@ -21,21 +21,23 @@
#include <config.h>
#include <cstdio> /* perror... */
#include <sys/wait.h>
#include <string.h>
#include "configure_network.h"
#include "libnet.h" /* netGetNameserver, netSetNameserver */
#include "network_interfaces.h" /* getInetAttributes, setInetAttributes */
#include <stdlib.h> /* system */
#include <iostream>
#include <iomanip>
#include <sstream>
#include <fstream>
CNetworkConfig::CNetworkConfig(void)
CNetworkConfig::CNetworkConfig()
{
char our_nameserver[16];
netGetNameserver(our_nameserver);
nameserver = our_nameserver;
inet_static = getInetAttributes("eth0", automatic_start, address, netmask, broadcast, gateway);
init_vars();
copy_to_orig();
ifname = "eth0";
}
CNetworkConfig* CNetworkConfig::getInstance()
@@ -52,9 +54,16 @@ CNetworkConfig* CNetworkConfig::getInstance()
CNetworkConfig::~CNetworkConfig()
{
}
void CNetworkConfig::readConfig(std::string iname)
{
ifname = iname;
inet_static = getInetAttributes(ifname, automatic_start, address, netmask, broadcast, gateway);
init_vars();
copy_to_orig();
}
void CNetworkConfig::init_vars(void)
{
@@ -62,15 +71,41 @@ void CNetworkConfig::init_vars(void)
char _broadcast[16];
char router[16];
char ip[16];
unsigned char addr[6];
hostname = netGetHostname();
netGetDefaultRoute(router);
gateway = router;
netGetIP((char *) "eth0", ip, mask, _broadcast);
netmask = mask;
broadcast = _broadcast;
address = ip;
/* FIXME its enough to read IP for dhcp only ?
* static config should not be different from settings in etc/network/interfaces */
if(!inet_static) {
netGetIP((char *) ifname.c_str(), ip, mask, _broadcast);
netmask = mask;
broadcast = _broadcast;
address = ip;
}
netGetMacAddr((char *) ifname.c_str(), addr);
std::stringstream mac_tmp;
for(int i=0;i<6;++i)
mac_tmp<<std::hex<<std::setfill('0')<<std::setw(2)<<(int)addr[i]<<':';
mac_addr = mac_tmp.str().substr(0,17);
key = "";
ssid = "";
wireless = 0;
std::string tmp = "/sys/class/net/" + ifname + "/wireless";
if(access(tmp.c_str(), R_OK) == 0)
wireless = 1;
if(wireless)
readWpaConfig();
printf("CNetworkConfig: %s loaded, wireless %s\n", ifname.c_str(), wireless ? "yes" : "no");
}
void CNetworkConfig::copy_to_orig(void)
@@ -82,10 +117,35 @@ void CNetworkConfig::copy_to_orig(void)
orig_gateway = gateway;
orig_inet_static = inet_static;
orig_hostname = hostname;
orig_ifname = ifname;
orig_ssid = ssid;
orig_key = key;
}
bool CNetworkConfig::modified_from_orig(void)
{
#ifdef DEBUG
if(orig_automatic_start != automatic_start)
printf("CNetworkConfig::modified_from_orig: automatic_start changed\n");
if(orig_address != address )
printf("CNetworkConfig::modified_from_orig: address changed\n");
if(orig_netmask != netmask )
printf("CNetworkConfig::modified_from_orig: netmask changed\n");
if(orig_broadcast != broadcast )
printf("CNetworkConfig::modified_from_orig: broadcast changed\n");
if(orig_gateway != gateway )
printf("CNetworkConfig::modified_from_orig: gateway changed\n");
if(orig_hostname != hostname )
printf("CNetworkConfig::modified_from_orig: hostname changed\n");
if(orig_inet_static != inet_static )
printf("CNetworkConfig::modified_from_orig: inet_static changed\n");
if(orig_ifname != ifname)
printf("CNetworkConfig::modified_from_orig: ifname changed\n");
#endif
if(wireless) {
if((ssid != orig_ssid) || (key != orig_key))
return 1;
}
return (
(orig_automatic_start != automatic_start) ||
(orig_address != address ) ||
@@ -93,7 +153,8 @@ bool CNetworkConfig::modified_from_orig(void)
(orig_broadcast != broadcast ) ||
(orig_gateway != gateway ) ||
(orig_hostname != hostname ) ||
(orig_inet_static != inet_static )
(orig_inet_static != inet_static ) ||
(orig_ifname != ifname)
);
}
@@ -101,21 +162,27 @@ void CNetworkConfig::commitConfig(void)
{
if (modified_from_orig())
{
#ifdef DEBUG
printf("CNetworkConfig::commitConfig: modified, saving (wireless %d, ssid %s key %s)...\n", wireless, ssid.c_str(), key.c_str());
#endif
if(orig_hostname != hostname)
netSetHostname((char *) hostname.c_str());
copy_to_orig();
if (inet_static)
{
addLoopbackDevice("lo", true);
setStaticAttributes("eth0", automatic_start, address, netmask, broadcast, gateway);
setStaticAttributes(ifname, automatic_start, address, netmask, broadcast, gateway, wireless);
}
else
{
addLoopbackDevice("lo", true);
setDhcpAttributes("eth0", automatic_start);
setDhcpAttributes(ifname, automatic_start, wireless);
}
if(wireless && ((key != orig_key) || (ssid != orig_ssid)))
saveWpaConfig();
copy_to_orig();
}
if (nameserver != orig_nameserver)
{
@@ -150,7 +217,12 @@ int mysystem(char * cmd, char * arg1, char * arg2)
void CNetworkConfig::startNetwork(void)
{
system("/sbin/ifup -v eth0");
std::string cmd = "/sbin/ifup " + ifname;
#ifdef DEBUG
printf("CNetworkConfig::startNetwork: %s\n", cmd.c_str());
#else
system(cmd.c_str());
#endif
if (!inet_static) {
init_vars();
}
@@ -159,6 +231,72 @@ void CNetworkConfig::startNetwork(void)
void CNetworkConfig::stopNetwork(void)
{
//mysystem("ifdown eth0", NULL, NULL);
system("/sbin/ifdown eth0");
std::string cmd = "/sbin/ifdown " + ifname;
#ifdef DEBUG
printf("CNetworkConfig::stopNetwork: %s\n", cmd.c_str());
#else
system(cmd.c_str());
#endif
}
void CNetworkConfig::readWpaConfig()
{
std::string s;
std::ifstream in("/etc/wpa_supplicant.conf");
ssid = "";
key = "";
if(!in.is_open()) {
perror("/etc/wpa_supplicant.conf read error");
return;
}
while(getline(in, s)) {
if(s[0] == '#')
continue;
std::string::size_type i = s.find('=');
if (i != std::string::npos) {
std::string n = s.substr(0, i);
std::string val = s.substr(i + 1, s.length() - (i + 1));
while((i = n.find(' ')) != std::string::npos)
n.erase(i, 1);
while((i = n.find('\t')) != std::string::npos)
n.erase(i, 1);
if((i = val.find('"')) != std::string::npos)
val.erase(i, 1);
if((i = val.rfind('"')) != std::string::npos)
val.erase(i, 1);
if(n == "ssid")
ssid = val;
else if(n == "psk")
key = val;
}
}
#ifdef DEBUG
printf("CNetworkConfig::readWpaConfig: ssid %s key %s\n", ssid.c_str(), key.c_str());
#endif
}
void CNetworkConfig::saveWpaConfig()
{
#ifdef DEBUG
printf("CNetworkConfig::saveWpaConfig\n");
#endif
std::ofstream out("/etc/wpa_supplicant.conf");
if(!out.is_open()) {
perror("/etc/wpa_supplicant.conf write error");
return;
}
out << "# generated by neutrino\n";
out << "ctrl_interface=/var/run/wpa_supplicant\n";
out << "network={\n";
out << " ssid=\"" + ssid + "\"\n";
out << " psk=\"" + key + "\"\n";;
out << " proto=WPA WPA2\n";
out << " key_mgmt=WPA-PSK\n";
out << " pairwise=CCMP TKIP\n";
out << " group=CCMP TKIP\n";
out << "}\n";
}