问题
Before people jump to conclusion saying this is a duplicate post, I'd like to point out that I have gone through the other posts on this topic but really haven't found a solution.
What I need
My goal is to access the wpa_supplicant from a C program to do the following:
- See active connections
- Detect when interfaces goes down
- Connect to AP/Setup an AP and so on
What I've found out
- I don't need DBus if I need to write a C program to communicate with wpa_supplicant
- I can make use of the functions in
wpa_ctrl.h
by just including these files in my project directory - Here are some links I found related to this question. 1, 2, 3
- I've also gone through the official documentation which talks about external programs using
wpa_ctrl.c
Why the above does not actually solve the problem
- Most of the posts I found on SO and other related websites regarding this issue point to resources like official documentation which is good but does not solve the problem
- In a lot of these posts, people have given up pursuing this or have worked out a solution but haven't posted it online.
- For a novice in this topic, it'll be helpful if one can post a working example -- the 'hello world' of wpa_supplicant.
What I've done so far
From this link, I copied
wpa_supplicant-2.5/src/common/wpa_ctrl.h
intowpa_supplicant-2.5/src/utils
directory (sincecommon.h
had many dependencies). I then wrote a simple C programhostapd_cli.c
in the same directory which is shown below. I get an undefined reference to 'wpa_ctrl_open' error#include "includes.h" #include <dirent.h> #include "wpa_ctrl.h" #include "common.h" static struct wpa_ctrl *ctrl_conn; static int hostapd_cli_quit = 0; static int hostapd_cli_attached = 0; static const char *ctrl_iface_dir = "/var/run/wpa_supplicant"; static char *ctrl_ifname = NULL; static int ping_interval = 5; int main() { ctrl_conn = wpa_ctrl_open(ctrl_iface_dir); if (!ctrl_conn){ printf("Could not get ctrl interface!\n"); return -1; } return 0; }
Makefile
C=gcc
CFLAGS=-lpthread
DEPS = includes.h wpa_ctrl.h common.h
OBJ = wpa_ctrl.o hostapd_cli.o
%.o: %.c $(DEPS)
$(CC) -c -o $@ $< $(CFLAGS)
main: $(OBJ)
gcc -o $@ $^ $(CFLAGS)
.PHONY: clean
clean:
rm -f *.o *~ core $(INCDIR)/*~
Build log
gcc -o main wpa_ctrl.o hostapd_cli.o -lpthread
hostapd_cli.o: In function `main':
hostapd_cli.c:(.text+0xf): undefined reference to `wpa_ctrl_open'
collect2: error: ld returned 1 exit status
Makefile:10: recipe for target 'main' failed
make: *** [main] Error 1
There's not much material on how to use these files and integrate it to an external project or how to compile it and I'm kinda clueless. Any help on how to proceed will be really appreciated.
Edit 1: Corrected typo and added build log
回答1:
undefined reference to `wpa_ctrl_open'
That's a linker error. If the command,
$ nm wpa_ctrl.o
reveals that it defines wpa_ctrl_open, then your immediate problem may be just the command-line order. Try:
gcc -o main hostapd_cli.o wpa_ctrl.o -lpthread
because hostapd_cli
references symbols in wpa_ctrl.o
. Otherwise, you need to find the source code that does define that symbol, so you can link to it.
Edit: Apparently you need to define a couple of symbols.
HTH.
来源:https://stackoverflow.com/questions/38215480/getting-started-with-wpa-supplicant-using-c