Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Connection refused on wpa_ctrl_open

Tags:

c

wifi

I am trying to use C code to access wpa_supplicant in an ARM embedded system running linux. After searching, I realise that I could use wpa_supplicant control interface. I try to open a connection using wpa_ctrl_open(), and resulted in "Connection refused" error:

Failed to connect to wpa_supplicant global interface: /var/run/wpa_supplicant error: Connection refused

The code I am using to test:

#include <stdio.h>
#include <string.h>
#include <sys/socket.h>
#include <sys/ioctl.h>
#include <linux/sockios.h>
#include <netinet/if_ether.h>
#include <net/if.h>
#include <sys/socket.h>
#include <sys/un.h>
#include "includes.h"

#ifdef CONFIG_CTRL_IFACE_UNIX
#include <dirent.h>
#endif /* CONFIG_CTRL_IFACE_UNIX */
#include "wpa_ctrl.h"
#include "common.h"

struct wpa_ctrl {
    int s;
#ifdef CONFIG_CTRL_IFACE_UDP
    struct sockaddr_in local;
    struct sockaddr_in dest;
#else // CONFIG_CTRL_IFACE_UDP
    struct sockaddr_un local;
    struct sockaddr_un dest;
#endif // CONFIG_CTRL_IFACE_UDP
};

static struct wpa_ctrl *ctrl_conn;

int main(int argc,char **argv)
{
    const char *global = "/var/run/wpa_supplicant";
    ctrl_conn = wpa_ctrl_open(global);
    if (ctrl_conn == NULL)
    {
        fprintf(stderr, "Failed to connect to wpa_supplicant "
            "global interface: %s error: %s\n",global,strerror(errno));
        return -1;
    }
    else
    {
        printf("Success\n");
    }
    return 0;
}

After tracing the code in wpa_ctrl.c, I found the problem is on the following IF condition, inside wpa_ctrl_open2() function:

if (connect(ctrl->s, (struct sockaddr *) &ctrl->dest,sizeof(ctrl->dest)) < 0)

I do not know what is the problem and how I can solve it.

On the same machine, I tried to run wpa_cli and it can access wpa_supplicant perfectly.

I used the following command to compile:

gcc -o test_wpa main.c wpa_ctrl.c os_unix.c -DCONFIG_BACKEND_FILE -DCONFIG_IEEE80211W -DCONFIG_DRIVER_WEXT -DCONFIG_WIRELESS_EXTENSION -DCONFIG_DRIVER_NL80211 -DCONFIG_LIBNL20 -DEAP_PSK -DIEEE8021X_EAPOL -DCONFIG_SHA256 -DCONFIG_CTRL_IFACE -DCONFIG_CTRL_IFACE_UNIX -DCONFIG_SME -lrt

wpa_supplicant code (including wpa_ctrl.c & os_unix.c) can be download at here: http://w1.fi/releases/wpa_supplicant-2.5.tar.gz

Many thanks in advance!

like image 848
t-wing Avatar asked Sep 25 '22 09:09

t-wing


1 Answers

I found the way to solve the problem, add "wlan0" after the path:

const char *global = "/var/run/wpa_supplicant/wlan0";
ctrl_conn = wpa_ctrl_open(global);
like image 170
t-wing Avatar answered Oct 12 '22 10:10

t-wing