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

<blockquote>
  <p>Failed to connect to wpa_supplicant global interface: /var/run/wpa_supplicant error: Connection refused</p>
</blockquote>

<p>The code I am using to test:</p>

<pre><code>#include &lt;stdio.h&gt;
#include &lt;string.h&gt;
#include &lt;sys/socket.h&gt;
#include &lt;sys/ioctl.h&gt;
#include &lt;linux/sockios.h&gt;
#include &lt;netinet/if_ether.h&gt;
#include &lt;net/if.h&gt;
#include &lt;sys/socket.h&gt;
#include &lt;sys/un.h&gt;
#include "includes.h"

#ifdef CONFIG_CTRL_IFACE_UNIX
#include &lt;dirent.h&gt;
#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;
}
</code></pre>

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

<pre><code>if (connect(ctrl-&gt;s, (struct sockaddr *) &amp;ctrl-&gt;dest,sizeof(ctrl-&gt;dest)) &lt; 0)
</code></pre>

<p>I do not know what is the problem and how I can solve it.</p>

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

<p>I used the following command to compile:</p>

<blockquote>
  <p>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</p>
</blockquote>

<p>wpa_supplicant code (including wpa_ctrl.c &amp; os_unix.c) can be download at here:
<a href="http://w1.fi/releases/wpa_supplicant-2.5.tar.gz" rel="nofollow">http://w1.fi/releases/wpa_supplicant-2.5.tar.gz</a></p>

<p>Many thanks in advance!</p>