Using the wpa_ctrl socket interface from an external project

John Beard john.j.beard at gmail.com
Mon May 13 13:33:11 EDT 2013


Hi,

Thank you very much! I applied that patch manually, made a couple of
other compilation tweaks, and it is now working! I didn't realise I had
to re-implement that handful of "os_" functions - I was trying to find a
way to compile them all in as untouched wpa_supplicant code.

I can now open a socket, send commands and receive replies. One other
thing occurs to me: how much space should the reply buffer be allocated?
I have seen an instance of it in use with a 4096-byte buffer - is this
correct in general, or should the buffer be larger? (I have scan results
of around 2.5kB, so I don't think smaller is a good idea)

Thanks again,

John Beard

On 13/05/13 10:25, Holger Schurig wrote:
> You can simply put wpa_ctrl.c into your own project, it's license is
> BSD, so it will work nicely with proprietary or GPLed code. I'm using
> that than in a C++/Qt4.x project, were I open the controlling interface
> via wpa_ctrl_open() and use that with a QSocketNotifier to react
> asynchronously.
> 
> I than made it compiled, with something like this (beware, this is a
> cut-and-paste patch, so probably whitespace damaged):
> 
> 
> 
> --- ../mkimage/hostap/src/common/wpa_ctrl.c2013-02-07 10:03:39.111172856
> +0100
> +++ wpa_ctrl.c2012-03-23 14:10:17.000000000 +0100
> @@ -6,18 +6,23 @@
>   * See README for more details.
>   */
>  
> +#ifdef ORIG
>  #include "includes.h"
> +#else
> +#include <stdlib.h>
> +#include <sys/types.h>
> +#include <sys/socket.h>
> +#include <unistd.h>
> +#include <stdio.h>
> +#include <errno.h>
> +#endif
> +
>  
>  #ifdef CONFIG_CTRL_IFACE
>  
>  #ifdef CONFIG_CTRL_IFACE_UNIX
>  #include <sys/un.h>
> -#include <unistd.h>
> -#include <fcntl.h>
>  #endif /* CONFIG_CTRL_IFACE_UNIX */
> -#ifdef CONFIG_CTRL_IFACE_UDP_REMOTE
> -#include <netdb.h>
> -#endif /* CONFIG_CTRL_IFACE_UDP_REMOTE */
>  
>  #ifdef ANDROID
>  #include <dirent.h>
> @@ -26,7 +31,41 @@
>  #endif /* ANDROID */
>  
>  #include "wpa_ctrl.h"
> +#ifdef ORIG
>  #include "common.h"
> +#else
> +#define os_malloc(size) malloc(size)
> +#define os_memset(s, c, n) memset(s, c, n)
> +#define os_free(ptr) free(ptr)
> +#define os_snprintf snprintf
> +#define os_getpid getpid
> +#define os_memcmp(s1, s2, n) memcmp(s1, s2, n)
> +
> +size_t os_strlcpy(char *dest, const char *src, size_t siz)
> +{
> +const char *s = src;
> +size_t left = siz;
> +
> +if (left) {
> +/* Copy string up to the maximum size of the dest buffer */
> +while (--left != 0) {
> +if ((*dest++ = *s++) == '\0')
> +break;
> +}
> +}
> +
> +if (left == 0) {
> +/* Not enough room for the string; force NUL-termination */
> +if (siz != 0)
> +*dest = '\0';
> +while (*s++)
> +; /* determine total src string length */
> +}
> +
> +return s - src - 1;
> +}
> +#endif
> +
>  
>  
>  #if defined(CONFIG_CTRL_IFACE_UNIX) || defined(CONFIG_CTRL_IFACE_UDP)
> 
> 
> 
> 2013/5/10 John Beard <john.j.beard at gmail.com
> <mailto:john.j.beard at gmail.com>>
> 
>     Hi,
> 
>     I am hoping to use the wpa_ctrl socket interface to communicate with
>     wpa_supplicant in order to get information about wifi signal strengths.
> 
>     I have tried to use the DBus interface, but I couldn't find much in the
>     way of useful information about the library I was using (libdbus-c++)
>     and when I asked further, I was told it is no longer usefully maintained
>     and that I should look elsewhere. I'd rather keep things smaller than
>     pulling in big deps, so I don't really want to use something like GDBus.
>     The control socket should be fine for my purposes, but I'm having a bit
>     of trouble using it.
> 
>     I can compile wpa_supplicant just fine, but I don't know how to compile
>     into a library suitable for linking into my project. I looked in
>     libutils.a, but that doesn't seem to have the wpa_ctrl_open() function
>     in it.
> 
>     I can see how I can add my own code to the wpa_supplicant tree (like
>     wpa_cli), but I'm more looking to add to another project, rather than
>     extend wpa_supplicant, and I couldn't see any likely-looking targets in
>     the Makefiles.
> 
>     The minimal code that I am trying to get to work is below (wpa_ctrl.h is
>     copied into my working dir for now):
> 
>     ===============
> 
>     #include <stdio.h>
>     #include "wpa_ctrl.h"
> 
>     #define CTRL_PATH "/var/run/wpa_supplicant.pid"
> 
>     int main(int argc, char** argv)
>     {
>         struct wpa_ctrl* wpac;
> 
>         wpac = wpa_ctrl_open(CTRL_PATH);
> 
>         if (!wpac){
>             printf("Could not get ctrl interface!\n");
>             return -1;
>         }
> 
>         return 0;
>     }
> 
>     ===============
> 
>     Could someone provide a quick pointer on how to correctly link against
>     wpa_ctrl for control socket access to wpa_supplicant?
> 
>     Thanks and best regards,
> 
>     John Beard
>     _______________________________________________
>     HostAP mailing list
>     HostAP at lists.shmoo.com <mailto:HostAP at lists.shmoo.com>
>     http://lists.shmoo.com/mailman/listinfo/hostap
> 
> 



More information about the HostAP mailing list