Beispiel #1
5
int main(void) {

	GuiStrings gStrings;
	GuiElems gElems;
	Settings settings;
	FileNames fNames;

	initGuiStrings(&gStrings);          // initializes strings to be displayed by gui
	initFileNames(&fNames);             // initializes data file and config file names
	initSettings(&settings, &fNames);   // initializes setting data structure
	initListStore(&gElems, &fNames);    // initializes data structures to display marks

    createGui(&gElems, &gStrings, &settings, &fNames);

    dumpSettings(&settings, &fNames);   // writes settings to file
    compactDataFile(&fNames);           // makes row numbers progressive eliminating "holes"

    freeGuiStrings(&gStrings);
    freeFileNames(&fNames);

	return EXIT_SUCCESS;
}
void NetworkSettings::loadOnRaspbian(void)
{
    // initialise settings, DHCP by default (if not found in 
    initNetSettings(&eth0);
    initNetSettings(&wlan0);
    
    Debug::out(LOG_DEBUG, "NetworkSettings::loadOnRaspbian() - reading from file %s", NETWORK_DHCPCD_FILE);
    FILE *f = fopen(NETWORK_DHCPCD_FILE, "rt");                 // read static IP config from DHCPCD file
    
    if(!f) {
        Debug::out(LOG_ERROR, "NetworkSettings::loadOnRaspbian - failed to open network settings file.\n");
        return;
    }
    
    #define MAX_LINE_LEN    1024
    char line[MAX_LINE_LEN];
    char tmp1[128];
    int  ires, cidr;
    
    TNetInterface *currentIface = NULL;                         // store the settings to the struct pointed by this pointer
    
    while(!feof(f)) {
        char *res = fgets(line, MAX_LINE_LEN, f);               // get single line
        
        if(!res) {                                              // if failed to get the line
            break;
        }
        
        if(line[0] == '#') {                                    // if it's a line with a comment, skip it
            continue;
        }
        
        removeEol(line);                                        // remove EOL from string
        
        if(strlen(line) < 2) {                                  // skip empty line
            continue;
        }
        
        // found start of interface section?
        if(strstr(line, "interface ") != NULL) {
            if(strstr(line, "eth0") != NULL) {                  // found eth0 section?
                currentIface = &eth0;
                initNetSettings(currentIface);                  // clear the struct
            }

            if(strstr(line, "wlan0") != NULL) {                 // found wlan0 section?
                currentIface = &wlan0;
                initNetSettings(currentIface);                  // clear the struct
            }

            continue;                                           // nothing usefull in this line
        }

        if(!currentIface) {                                     // current interface not (yet) set? skip the rest
            continue;
        }

        if(strstr(line, "static ip_address=") != NULL) {        // static IP?
            currentIface->dhcpNotStatic = false;                // static config
            
            ires = sscanf(line + 18, "%[^/]/%d", tmp1, &cidr);     // try to read IP and netmask

            if(ires == 2) {
                currentIface->address = tmp1;
                
                switch(cidr) {
                    case  8:    currentIface->netmask = "255.0.0.0";        break;
                    case 16:    currentIface->netmask = "255.255.0.0";      break;
                    case 24:    currentIface->netmask = "255.255.255.0";    break;
                    case 32:    currentIface->netmask = "255.255.255.255";  break;
                    default:    currentIface->netmask = "255.255.255.0";    break;
                }
            } else {
                currentIface->netmask = "255.255.255.0";
            }
        }
        
        if(strstr(line, "static routers=") != NULL) {               // static gateway?
            currentIface->dhcpNotStatic = false;                    // static config 
            currentIface->gateway = line + 15;
        }
        
        if(strstr(line, "static domain_name_servers=") != NULL) {   // static nameserver? 
            currentIface->dhcpNotStatic = false;                    // static config 
            nameserver = line + 27;
        }
    }
    
    fclose(f);

    loadWpaSupplicant();
    replaceIPonDhcpIface();
    
    loadHostname();
    
    dumpSettings();
}