:: Re: [DNG] Edward's Roaming handler:…
Page principale
Supprimer ce message
Répondre à ce message
Auteur: Edward Bartolo
Date:  
À: dng
Sujet: Re: [DNG] Edward's Roaming handler: was Systemd Shims
I am uploading the sources for anyone wanting to help themselves. :)

To compile the frontend Lazarus must be installed. Here, I am using version:
1.2.4+dfsg2-1

To compile the C source use:
gcc backend.c -o backend

Manual Configuration of the package: No yet post install script :(
create a directory:
/etc/network/wifi

Move both backend and front end to the same directory.

Outstanding TODO:
Before saving to an existing interfaces file, delete the old one
manually or edit the C source to do it for you.

ENJOY and LONG LIFE DEVUAN!

Compiled successfully on DEVUAN Alpha 64 bit.

On 19/08/2015, Edward Bartolo <edbarx@???> wrote:
> I am coding and testing on Devuan 64 bit.
>
> On 19/08/2015, Steve Litt <slitt@???> wrote:
>> On Wed, 19 Aug 2015 10:41:30 -0400
>> Steve Litt <slitt@???> wrote:
>>
>>> On Wed, 19 Aug 2015 14:29:02 +0100
>>> Edward Bartolo <edbarx@???> wrote:
>>>
>>> > This is the completed C backend with all functions tested to work.
>>> > Any suggestions as to modifications are welcome.
>>> >
>>> > The C code:
>>
>> Hi Edward,
>>
>> In the long run, you need to make the wifi device a variable instead of
>> hard-coding wlan0. I have no idea which method of deducing the wifi
>> device would require the least dependencies. I have a feeling it would
>> be to spawn /sbin/iwconfig, which you already have due to using iwlist.
>> Armed with the output of /sbin/iwconfig, finding the wifi device is a
>> very simple parsing task, *unless* there are multiple wifi-enabled
>> devices.
>>
>> LOL, I have to find a USB wifi dongle to continue testing your program.
>> Without any wifi device, when I did ./a.out opScan, I got a segfault.
>>
>> This thing is great. As far as I can tell, it's distro independent.
>> If you have Linux, it works. I'm going to use it on **all** my laptops,
>> and kick NetworkManager, Wicd and my homegrown wpa_supplicant helpers
>> to the curb.
>>
>> I wish all Linux software worked like this.
>>
>> Thanks,
>>
>> SteveT
>>
>> Steve Litt
>> August 2015 featured book: Troubleshooting: Just the Facts
>> http://www.troubleshooters.com/tjust
>> _______________________________________________
>> Dng mailing list
>> Dng@???
>> https://mailinglists.dyne.org/cgi-bin/mailman/listinfo/dng
>>
>

#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);

        
        return 0;
    }

    
    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];
        int u = exec("ifconfig wlan0 up", s);
        s[0] = '\0';    

        
        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
}