hostapd
hostapd(Host Access Point Daemon)是一個(gè)用于創(chuàng)建無線接入點(diǎn)(Access Point, AP)的守護(hù)進(jìn)程矢赁。它允許一個(gè)無線網(wǎng)絡(luò)接口卡(Wireless Network Interface Card, WNIC)作為無線接入點(diǎn)科汗,從而使其他無線設(shè)備能夠連接到這個(gè)接入點(diǎn)并訪問網(wǎng)絡(luò)虽填。
主要功能:
創(chuàng)建無線接入點(diǎn):允許無線設(shè)備連接到你的網(wǎng)絡(luò)。
管理連接:管理連接到接入點(diǎn)的客戶端設(shè)備干茉。
認(rèn)證和加密:支持多種認(rèn)證和加密方式,如 WPA/WPA2/WPA3 等。
使用場景:
當(dāng)你需要將你的計(jì)算機(jī)或樹莓派等設(shè)備變成一個(gè)無線接入點(diǎn)時(shí)袋励。
當(dāng)你需要在家庭或辦公室中創(chuàng)建一個(gè)無線網(wǎng)絡(luò)時(shí)。
配置示例:
interface=wlan0
driver=nl80211
ssid=MyNetwork
hw_mode=g
channel=6
macaddr_acl=0
auth_algs=1
ignore_broadcast_ssid=0
wpa=2
wpa_passphrase=MyPassphrase
wpa_key_mgmt=WPA-PSK
wpa_pairwise=TKIP
rsn_pairwise=CCMP
wpa_supplicant
wpa_supplicant 是一個(gè)用于無線網(wǎng)絡(luò)連接的守護(hù)進(jìn)程当叭。它支持 WPA(Wi-Fi Protected Access)和 WPA2 等安全協(xié)議茬故,用于在客戶端設(shè)備上進(jìn)行無線網(wǎng)絡(luò)的認(rèn)證和連接。
主要功能:
連接到無線網(wǎng)絡(luò):允許客戶端設(shè)備連接到無線接入點(diǎn)蚁鳖。
認(rèn)證和加密:支持多種認(rèn)證和加密方式磺芭,如 WPA/WPA2/WPA3 等。
漫游管理:在多個(gè)無線接入點(diǎn)之間進(jìn)行無縫漫游醉箕。
使用場景:
當(dāng)你需要在筆記本電腦或智能手機(jī)等設(shè)備上連接到無線網(wǎng)絡(luò)時(shí)钾腺。
當(dāng)你需要在嵌入式設(shè)備(如樹莓派)上連接到無線網(wǎng)絡(luò)時(shí)。
配置示例:
network={
ssid="MyNetwork"
psk="MyPassphrase"
key_mgmt=WPA-PSK
}
接入方法
隨身wifi讥裤,默認(rèn)是啟動(dòng)hostapd的放棒,要改成wpa_supplicant,才可以接入我們的局域網(wǎng)己英,要先重啟一下無線網(wǎng)卡间螟,經(jīng)過分析,是由/dev/wifi_device 設(shè)備決定。
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/ioctl.h>
int main(int argc, char *argv[]) {
if (argc != 2) {
fprintf(stderr, "Usage: %s <up|down>\n", argv[0]);
return EXIT_FAILURE;
}
int control_command;
if (strcmp(argv[1], "up") == 0) {
control_command = 1;
} else if (strcmp(argv[1], "down") == 0) {
control_command = 0;
} else {
fprintf(stderr, "Invalid command: %s. Use 'up' or 'down'.\n", argv[1]);
return EXIT_FAILURE;
}
const char *device_path = "/dev/wifi_device";
int device_fd = open(device_path, O_RDWR);
if (device_fd < 0) {
perror("Failed to open device");
return EXIT_FAILURE;
}
int result = ioctl(device_fd, control_command);
if (result < 0) {
perror("IOCTL operation failed");
} else {
printf("IOCTL operation completed successfully.\n");
}
close(device_fd);
return result < 0 ? EXIT_FAILURE : EXIT_SUCCESS;
}
#killall hostapd
#./wifi down
#sleep 5
#./wifi up
#./wpa_supplicant -iwlan0 -Dnl80211 -c/etc_rw/wifi/wpa_supplicant.conf -dd
#./wpa_cli -i wlan0 -p /etc_rw/wifi/sockets scan #wlan0 掃描
#./wpa_cli -i wlan0 -p /etc_rw/wifi/sockets scan_result # 獲取當(dāng)前結(jié)果
# udhcpc -i wlan0
udhcpc (v1.21.0) started
Sending discover...
Sending discover...
Sending select for 192.168.1.111...
Lease of 192.168.1.111 obtained, lease time 43200
# ifconfig wlan0 192.168.1.111 netmask 255.255.255.0
# route add default gw 192.168.1.1. #添加默認(rèn)路由
# ping 114.114.114.114
PING 114.114.114.114 (114.114.114.114): 56 data bytes
64 bytes from 114.114.114.114: seq=0 ttl=92 time=25.940 ms
64 bytes from 114.114.114.114: seq=1 ttl=95 time=27.283 ms
64 bytes from 114.114.114.114: seq=2 ttl=76 time=33.173 ms
64 bytes from 114.114.114.114: seq=3 ttl=72 time=25.848 ms
^C
--- 114.114.114.114 ping statistics ---
4 packets transmitted, 4 packets received, 0% packet loss
round-trip min/avg/max = 25.848/28.061/33.173 ms