Pour une raison X ou Y, j'ai eu besoin d'accèder à mon interface wifi sous OpenBSD, je suis tombé sur cette page, mais évidemment la stack ieee80211 n'est pas pareille… donc vla un pti bout de code qui montre comment_ca_marche ™ (ce code est honteusement pompé sur ifconfig.c)

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
 
#include <sys/ioctl.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/param.h>
 
#include <net/if.h>
#include <netinet/in.h>
#include <netinet/if_ether.h>
#include <dev/ic/if_wi_ieee.h>
 
#include <net80211/ieee80211.h>
#include <net80211/ieee80211_ioctl.h>
 
int
main(int argc, char **argv)
{
        struct ifreq    ifr;
        char            name[256] = "iwi0";
        int             len, i, nwkey_verbose, inwid, inwkey, ichan, ipwr,
                        ibssid, itxpower;
        struct ieee80211_nwid nwid;
        struct ieee80211_nwkey nwkey;
        struct ieee80211_power power;
        struct ieee80211chanreq channel;
        struct ieee80211_bssid bssid;
        struct ieee80211_txpower txpower;
        struct ieee80211_nodereq nr;
        u_int8_t        zero_bssid[IEEE80211_ADDR_LEN];
        u_int8_t        keybuf[IEEE80211_WEP_NKID][16];
        struct ether_addr ea;
        int             s;
        s = socket(AF_INET, SOCK_DGRAM, 0);
 
        if (s < 0) {
                perror("socket()");
                return -1;
        }
        /* get current status via ioctls */
        memset(&ifr, 0, sizeof(ifr));
        ifr.ifr_data = (caddr_t) & nwid;
        strlcpy(ifr.ifr_name, name, sizeof(ifr.ifr_name));
        inwid = ioctl(s, SIOCG80211NWID, (caddr_t) & ifr);
 
        memset(&nwkey, 0, sizeof(nwkey));
        strlcpy(nwkey.i_name, name, sizeof(nwkey.i_name));
        inwkey = ioctl(s, SIOCG80211NWKEY, (caddr_t) & nwkey);
 
        memset(&power, 0, sizeof(power));
        strlcpy(power.i_name, name, sizeof(power.i_name));
        ipwr = ioctl(s, SIOCG80211POWER, &power);
 
        memset(&channel, 0, sizeof(channel));
        strlcpy(channel.i_name, name, sizeof(channel.i_name));
        ichan = ioctl(s, SIOCG80211CHANNEL, (caddr_t) & channel);
 
        memset(&bssid, 0, sizeof(bssid));
        strlcpy(bssid.i_name, name, sizeof(bssid.i_name));
        ibssid = ioctl(s, SIOCG80211BSSID, &bssid);
 
        memset(&txpower, 0, sizeof(txpower));
        strlcpy(txpower.i_name, name, sizeof(txpower.i_name));
        itxpower = ioctl(s, SIOCG80211TXPOWER, &txpower);
 
        if (inwid == 0) {
                /* nwid.i_nwid is not NUL terminated. */
                len = nwid.i_len;
                if (len > IEEE80211_NWID_LEN)
                        len = IEEE80211_NWID_LEN;
                printf("nwid %s", nwid.i_nwid);
        }
        if (ichan == 0 && channel.i_channel != 0 &&
            channel.i_channel != (u_int16_t) - 1)
                printf(" chan %u", channel.i_channel);
 
        memset(&zero_bssid, 0, sizeof(zero_bssid));
        if (ibssid == 0 &&
            memcmp(bssid.i_bssid, zero_bssid, IEEE80211_ADDR_LEN) != 0) {
                memcpy(&ea.ether_addr_octet, bssid.i_bssid,
                       sizeof(ea.ether_addr_octet));
                printf(" bssid %s", ether_ntoa(&ea));
 
                bzero(&nr, sizeof(nr));
                bcopy(bssid.i_bssid, &nr.nr_macaddr, sizeof(nr.nr_macaddr));
                strlcpy(nr.nr_ifname, name, sizeof(nr.nr_ifname));
                if (ioctl(s, SIOCG80211NODE, &nr) == 0 && nr.nr_rssi) {
                        if (nr.nr_max_rssi)
                                printf(" %u%%", IEEE80211_NODEREQ_RSSI(&nr));
                        else
                                printf(" %udB", nr.nr_rssi);
                }
        }
        if (itxpower == 0)
                printf(" %ddBm%s", txpower.i_val,
                       txpower.i_mode == IEEE80211_TXPOWER_MODE_AUTO ?
                       " (auto)" : "");
        puts("");
}

On change éventuellement iwi0 pour le nom de son iface wifi, un pti coup de gcc, et hop ./a.out nous donne :

nwid mon-nwid chan 8 bssid de:ad:be:ef:29:bb 59dB 100dBm

pour la suite, voir ieee80211(9) et ieee80211_ioctl.h

openbsd/c_wifi.txt · Last modified: 2010/01/12 13:29 (external edit)