:: Re: [DNG] Systemd Shims
Αρχική Σελίδα
Delete this message
Reply to this message
Συντάκτης: Edward Bartolo
Ημερομηνία:  
Προς: dng
Καινούρια Θέματα: [DNG] Edward's Roaming handler: was Systemd Shims
Αντικείμενο: Re: [DNG] Systemd Shims
This is the completed C backend with all functions tested to work. Any
suggestions as to modifications are welcome.

The C code:


-----------------------------

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <dirent.h>


//using namespace std;


#define opSave                0
#define opSaveConnect            1
#define opQueryConnect            2
#define opDeleteConnect            3
#define opConnectionConnect            4
#define opDisconnectActiveConnection    5
#define opScan                6
#define opLoadExisting            7


const
    char* path_to_interfaces_files = "/etc/network/wifi";

    
/*
1) Glib::spawn_sync instead of a pipe stream, provides a slot.
2) cmd trying to call an inexistent command still returns a valid pointer!
verify cmd exists before calling exec
*/

inline int file_exists(char* name) {
return (access(name, F_OK) != -1);
}


int exec(const char* cmd, char* out)
{
    const int buf_size = 128;

        
    FILE * pipe = popen(cmd, "r");
    char buffer[buf_size];
    while(!feof(pipe)) {
        if(fgets(buffer, buf_size, pipe) != NULL)
        {
            if (out != NULL)
                strcat(out, buffer);
                else strcpy(out, buffer);
        }
    }

        
    return pclose(pipe);
}


/*        Interfaces file sample
auto lo
iface lo inet loopback


# The primary network interface
# allow-hotplug eth0
iface eth0 inet dhcp


# WIFI Configuration
# auto wlan0
iface wlan0 inet dhcp
    wpa-ssid   ESSID
    wpa-psk   "password"


*/
    int saveFile(char* essid, char* pw) //argv[2], argv[3]
    {
        char ifilename[1024];
        strcpy(ifilename, path_to_interfaces_files);

        
        strcat(ifilename, "/");
        strcat(ifilename, essid);

    
        FILE *fp = fopen(ifilename, "ab+");
        char text[1024];

        
        strcpy(text, "auto lo\n");
        fprintf(fp, text);

        
        strcpy(text, "iface lo inet loopback\n\n");
        fprintf(fp, text);

        
        strcpy(text, "iface wlan0 inet dhcp\n");
        fprintf(fp, text);

        
        strcpy(text, "   wpa-ssid ");
        strcat(text, essid);
        strcat(text, "\n");
        fprintf(fp, text);

        
        strcpy(text, "   wpa-psk \"");
        strcat(text, pw);
        strcat(text, "\"\n");
        fprintf(fp, text);

        
        fclose(fp);
    }

    
    int connectionConnect(char* essid) //argv[2]
    {
        char* s = 0;

        
        char ifilename[1024];
        strcpy(ifilename, path_to_interfaces_files);

        
        strcat(ifilename, "/");
        strcat(ifilename, essid);

        
        char command[1024];
        strcpy(command, "/sbin/ifup wlan0 -i ");

        
        strcat(command, ifilename);
        printf(command);
        int q = exec(command, s);
        printf(s);
        return q;
    }

    
    int queryConnect(char* essid) //argv[2]
    {
        char s[50*1024];
        char command[1024];
        strcpy(command, "/bin/cat /etc/network/wifi/");
        strcat(command, essid);
        strcat(command, " | grep -A 1 wpa-ssid");
        int q = exec(command, s);
        printf(s);
        return q;
    }

    
    int deleteConnect(char* essid) //argv[2]
    {
        //char* s = 0;
        char command[1024];
        strcpy(command, "/bin/rm /etc/network/wifi/");
        strcat(command, essid);
        int q = exec(command, 0);
        //printf(s);
        return q;
    }

    
    int disconnectActiveConnection()
    {
        char* s = 0;

        
        char command[1024];
        strcpy(command, "/sbin/ifdown wlan0");
        int q = exec(command, s);
        printf(s);
        return q;
    }

    
    int scan()
    {
        char s[50*1024];
        char command[1024];
        strcpy(command, "/sbin/iwlist wlan0 scanning | /bin/grep ESSID");
        int q = exec(command, s);
        printf(s);
        return q;
    }

    
    int loadExisting()
    {
        DIR *dir;
        struct dirent *ent;

        
        if ((dir = opendir ("/etc/network/wifi")) != 0)
        {
            while ((ent = readdir (dir)) != 0)
              if (!(strcmp(".", ent->d_name) == 0 || strcmp("..", ent->d_name) == 0))
                    printf ("%s\n", ent->d_name);

            
            closedir (dir);
            return 0;
        }
        else
        {
            perror ("");
            return EXIT_FAILURE;
        }
    }


int main(int argc, char *argv[])
{
    char *out = 0;
    int switch_item = -1, i;
    if (argc > 1) switch_item = atoi(argv[1]);

    
    switch (switch_item) {
        case opSave:
            i = saveFile(argv[2], argv[3]);

            
            //printf(out);
            return i;

            
        case opSaveConnect:
            i = saveFile(argv[2], argv[3]);
            if (i == 0) i = connectionConnect(argv[2]);
            return i;

            
        case opQueryConnect:
            i = queryConnect(argv[2]);
            return i;

            
        case opDeleteConnect:
            i = deleteConnect(argv[2]);
            return i;

            
        case opConnectionConnect:
            i = connectionConnect(argv[2]);
            return i;

            
        case opDisconnectActiveConnection:
            i = disconnectActiveConnection();
            return i;

            
        case opScan:
            i = scan();
            return i;

            
        case opLoadExisting:
            i = loadExisting();
            return i;
    }

    
    return -1; // parameter not in range
}


----------------------------


=====================================
On 19/08/2015, Edward Bartolo <edbarx@???> wrote:
> Ok, so it will be a separate interfaces file for every essid. I am
> proposing to place them under /etc/network/wifi-interfaces.
>
> I have just used my embryonic network manager to connect without
> typing the root password by pressing the connect button.
>
> On 19/08/2015, Steve Litt <slitt@???> wrote:
>> On Tue, 18 Aug 2015 20:43:18 +0100
>> Edward Bartolo <edbarx@???> wrote:
>>
>>> A) I am at the stage of trying to understand these:
>>> a) iw_sockets_open
>>> b) iw_get_range_info
>>> c) iw_scan
>>
>> Are those commands, or part of a C library? The only things I'm
>> familiar with is commands iwlist and iwconfig, as well as the ip and
>> ifconfig commands. "iwlist wlan1 scanning" at the command line or from
>> a shellscript or system() or fork() ore exec() is how you get a list of
>> essids and their strengths and encryption types.
>>
>>>
>>> The goal is to avoid to have the least number of dependencies.
>>
>> I think iwlist/iwconfig are supplied by one dependency.
>>
>>>
>>>
>>> B) I have yet another tempting idea. This is to use only one file for
>>> the essids and corresponding passwords. However, this would force me
>>> to somehow trick ifup into thinking it is being passed an interfaces
>>> file instead of the output from an executable.
>>
>> What's the benefit of only one file? Having them in one file requires
>> parsing. It makes it easier for the manual user to wreck all wifi with
>> a typo in one file, and he doesn't even have the troubleshooting
>> advantage of being able to compare a working and non-working interface
>> files. It would make it harder to use Vim to insert another interface
>> for troubleshooting purposes. Assuming I had essids reseda, library and
>> mcdonalds, I'd do this:
>>
>> /path/to/wifi/interfaces/library
>> /path/to/wifi/interfaces/reseda
>> /path/to/wifi/interfaces/mcdonalds
>>
>> That's the djb way, it makes programming trivial, it makes
>> troubleshooting pretty easy, and it uses the tools Unix gave you to
>> best possible advantage.
>>
>> Meanwhile, I see absolutely no advantage to putting them all in one
>> file.
>>
>>>
>>> Here is the command:
>>> ifup wlan0 -i $(tricky-executable(essid))
>>>
>>> tricky-executable would pass a series of text lines as they would
>>> appear in a dedicated interfaces file.
>>>
>>> I found it: it is not the above as that does not work. But this does
>>> work. :) cat /etc/network/interfaces | ifup wlan0
>>>
>>> What do you think.
>>
>> I'd have separate interface files for each essid.
>>
>> SteveT
>>
>> Steve Litt
>> August 2015 featured book: Troubleshooting: Just the Facts
>> http://www.troubleshooters.com/tjust
>>
>