使用C开始使用wpa_supplicant [英] Getting started with wpa_supplicant using C

查看:602
本文介绍了使用C开始使用wpa_supplicant的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在人们得出结论说这是重复的帖子之前,我想指出,我已经阅读了有关该主题的其他帖子,但实际上还没有找到解决方案.

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.

我的目标是从C程序访问wpa_supplicant以执行以下操作:

My goal is to access the wpa_supplicant from a C program to do the following:

  1. 查看活动连接
  2. 在接口出现故障时检测
  3. 连接到AP/设置AP等

我发现的东西

  • 如果我需要编写C程序来与wpa_supplicant通信,则不需要DBus
  • 只要将这些文件包含在我的项目目录中,就可以使用wpa_ctrl.h中的功能
  • 以下是我发现的与此问题相关的一些链接. 1 2 官方文档,该文档讨论了使用外部程序的情况. wpa_ctrl.c
  • 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
      • 我在SO和其他相关网站上发现的有关此问题的大多数帖子都指向诸如的好文档,但不能解决问题
      • 在许多这样的帖子中,人们已经放弃追求这一点,或者已经制定了解决方案,但是还没有在线发布.
      • 对于本主题的新手来说,如果可以发布一个有效的示例(wpa_supplicant的"hello world"),将对您有所帮助.
      • 链接中,我将wpa_supplicant-2.5/src/common/wpa_ctrl.h复制到wpa_supplicant-2.5/src/utils目录中(由于common.h有很多依赖性).然后,我在下面显示的同一目录中编写了一个简单的C程序hostapd_cli.c. 我获得了对"wpa_ctrl_open"错误的未定义引用

      • From this link, I copied wpa_supplicant-2.5/src/common/wpa_ctrl.h into wpa_supplicant-2.5/src/utils directory (since common.h had many dependencies). I then wrote a simple C program hostapd_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;
      }
      

      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)/*~ 
      

      构建日志

       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

      推荐答案

      对"wpa_ctrl_open"的未定义引用

      undefined reference to `wpa_ctrl_open'

      这是一个链接器错误.如果是命令

      That's a linker error. If the command,

      $ nm wpa_ctrl.o
      

      显示它定义了 wpa_ctrl_open ,那么您的直接问题可能就是命令行顺序.试试:

      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
      

      因为hostapd_cli引用wpa_ctrl.o中的符号.否则,您需要找到确实定义该符号的源代码,以便可以链接到它.

      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.

      编辑:显然,您需要 HTH.

      这篇关于使用C开始使用wpa_supplicant的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆