:: Re: [DNG] netman GIT project
Forside
Slet denne besked
Besvar denne besked
Skribent: Edward Bartolo
Dato:  
Til: Steve Litt, dng
Emne: Re: [DNG] netman GIT project
I added another function to core_functions.c. It attempts to connect
to eth0 but for some weird reason, probably a bug not from our part,
it is failing.

The function is: int connectWired(const char ethx)

And the error is: (passed command to execl is "ifup eth0")
"This command should be called as ifup, ifdown, or ifquery"

Any pointers as to what is the cause are appreciated.

Edward

On 27/08/2015, Steve Litt <slitt@???> wrote:
> On Thu, 27 Aug 2015 14:18:02 +0100
> Edward Bartolo <edbarx@???> wrote:
>
>>
>> Now, in the case of frequently changing wifi hotspots, the frontend
>> need to be made to detect that a different wifi hotspot is required.
>> This can be achieved with TTimer calling a subroutine regularly in
>> which a connection is tested, and in case the active wifi is
>> unreachable, other wifi's are tried starting with the strongest
>> signal. In case, no essid file is found, the user is prompted to enter
>> password and essid. However, this can become a nuisance, so we have to
>> decide where to stop regarding automating wifi connections.
>
> You're right.
>
> Probably something in the network software sends a message (besides the
> one sent to dbus) indicating that the current active wifi just
> disconnected, and if that can be redirected to your software, that
> pretty much does the job.
>
> SteveT
>
> Steve Litt
> August 2015 featured book: Troubleshooting: Just the Facts
> http://www.troubleshooters.com/tjust
>

/*
    netman - A Network Connection Manager
    Copyright (C) 2015  Edward Bartolo


    "netman" is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.


    "netman" is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.


    You should have received a copy of the GNU General Public License
    along with "netman".  If not, see <http://www.gnu.org/licenses/>.
*/


#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <dirent.h>
#include <alloca.h>
#include <sys/types.h>
#include <sys/wait.h>

#include "core_functions.h"
#include "essid_encoder.h"
#include "paths.h"

#define SIMPLE_SCAN_OPT ""
//#define POWER_SCAN_OPT "-B 4"

int connectWired(
    const char * ethx
) {
    int err;
    int valid_eth = 0;

    
    if (ethx == NULL) 
        return 107; // ethx is NULL

    
    char ethN[10];
    strcpy(ethN, "eth");

    
    valid_eth = (strncmp(ethN, ethx, 3) == 0);
    if (valid_eth && (strlen(ethx) > 3 && strlen(ethx) < 6)) {
        if (strlen(ethx) == 4) {
            valid_eth = isdigit(ethx[3]);
        } else {
            valid_eth = ethx[3] != '0' && isdigit(ethx[3]) && isdigit(ethx[4]);
        }        
    }

    
        
    if (!valid_eth) {
        fprintf(stderr, "ERROR: ethX not recognized.\n");


        return 106;  //ethx not recognized
    }

        
    err = execl("/sbin/ifup", ethx, (char *) NULL);

        
    fprintf(
        stderr, 
        "ERROR: connectWired(): "
        "\"/sbin/ifup %s\" failed; err=%d\n",
        ethx, err
    );

            
    return err;
}


int connectionConnect(
    const char * essid
) {
    char filename[1024];
    char * encoded_essid;
    int err;


    err = encode_essid_alloc(essid, &encoded_essid);


    if(err!=0) {
        fprintf(stderr, "ERROR: essid_encode_alloc(essid=%s) failed; err=%d\n", essid, err);


        return err;
    }


    err = encode_essid(essid, &encoded_essid);


    if(err!=0) {
        fprintf(stderr, "ERROR: essid_encode(essid=%s) failed; err=%d\n", essid, err);

        
        return err;
    }


    err = snprintf(filename, 1024, "%s/%s", IFACES_PATH, encoded_essid);


    if(err<0) {
        fprintf(stderr, "ERROR: snprintf(filename) failed; err=%d\n", err);

        
        return err;
    }


    fprintf(stderr, "DEBUG: now executing: /sbin/ifup wlan0 -i %s\n", filename); // DEBUG


    err = execl("/sbin/ifup", "ifup", "wlan0", "-i", filename, (char *) NULL);


    fprintf(
        stderr, 
        "ERROR: connectionConnect(): "
        "\"/sbin/ifup wlan0 -i %s\" failed; err=%d\n",
        filename, err
    );


    return err;
}


int disconnectActiveConnection()
{
    int err = execl("/sbin/ifdown", "ifdown", "wlan0", (char *) NULL);


    fprintf(
        stderr, 
        "ERROR: disconnectActiveConnection(): "
        "\"/sbin/ifdown wlan0\" failed; err=%d\n",
        err
    );


    return err;
} 


int power_scan(scan_type scan)
{
    const char *command_up = "/sbin/ifconfig wlan0 up";
    int status;
    FILE * shell_reader;
    char * scan_buffer;
    int err;


    scan_buffer = calloc(1024, 1);


    if(!scan_buffer) {
        fprintf(
            stderr, 
            "ERROR: power_scan(): "
            "could not allocate 104 bytes for scan_buffer (Error: %s)\n", 
            strerror(errno)
        );


        return -1;
    }


    status = system(command_up);


    if(status==-1 || !WIFEXITED(status)) {
        fprintf(
            stderr, 
            "ERROR: power_scan(): "
            "\"%s\" did not exit regularly (status=%d).\n", 
            command_up, status
        );


        return status;
    }
    else if (0 != WEXITSTATUS(status)) {
        fprintf(
            stderr, "ERROR: power_scan(): "
            "\"%s\" did not exit successfully (status=%d).\n", 
            command_up, WEXITSTATUS(status)
        );


        return status;
    }


    if (scan == simple)
        shell_reader = popen(
            "/sbin/iwlist wlan0 scanning | "
            "/bin/grep " SIMPLE_SCAN_OPT " ESSID", 
            "r"
        );
    else 
        shell_reader = popen("/sbin/iwlist wlan0 scanning",
            //"/sbin/iwlist wlan0 scanning | "
            //"/bin/grep " POWER_SCAN_OPT " ESSID", 
            "r"
        );


    if(!shell_reader) {
        fprintf(
            stderr, 
            "ERROR: power_scan(): "
            "popen() failed (Error: %s)\n", 
            strerror(errno)
        );


        return -1;
    }


    while((scan_buffer = fgets(scan_buffer, 1024, shell_reader)))
        printf("%s", scan_buffer);


    err = pclose(shell_reader);


    if(err) {
        fprintf(
            stderr, 
            "ERROR: power_scan(): "
            "pclose(shell_reader) failed (Error: %s)\n", 
            strerror(errno)
        );


        return -1;
    }


    return 0;
}


int scan(void)
{
    return power_scan(simple);
}