C/airpcap-nl.c
/* TODO:
 *   - Check if interface supports monitor mode (NL80211_ATTR_SUPPORTED_IFTYPES)
 *     at all - in GET_WIPHY? seems to make sense for it to be there.
 *   - Check if interface is NL80211_ATTR_IFTYPE
 *     NL80211_IFTYPE_MONITOR (use NL80211_CMD_GET_INTERFACE)
 *   - If interface is NOT IFTYPE_MONITOR, search all interfaces
 *     for the same WIPHY index, maybe with NLM_F_DUMP and try to find
 *     an existing one.
 *     Add a flag in AirpcapHandle internally to make this easier?
 *   - If no monitor interface is defined, create one.
 *     Attempt to destroy monitor interface on close? onexit()?
 */
- Check if interface supports monitor mode (NL80211_ATTR_SUPPORTED_IFTYPES)
at all - in GET_WIPHY. seems to make sense for it to be there.
- Check if interface is NL80211_ATTR_IFTYPE
NL80211_IFTYPE_MONITOR (use NL80211_CMD_GET_INTERFACE)
- If interface is NOT IFTYPE_MONITOR, search all interfaces
for the same WIPHY index, maybe with NLM_F_DUMP and try to find
an existing one.
Add a flag in AirpcapHandle internally to make this easier.
- If no monitor interface is defined, create one.
Attempt to destroy monitor interface on close.
- Check if interface supports monitor mode ( NL80211_ATTR_SUPPORTED_IFTYPES ) at all - in GET_WIPHY . seems to make sense for it to be there . - Check if interface is NL80211_ATTR_IFTYPE NL80211_IFTYPE_MONITOR ( use NL80211_CMD_GET_INTERFACE ) - If interface is NOT IFTYPE_MONITOR search all interfaces for the same WIPHY index maybe with NLM_F_DUMP and try to find an existing one . Add a flag in AirpcapHandle internally to make this easier . - If no monitor interface is defined create one . Attempt to destroy monitor interface on close .
PAirpcapHandle AirpcapOpen(PCHAR DeviceName, PCHAR Ebuf)
{
    PAirpcapHandle handle;
    if (NULL != Ebuf) {
        Ebuf[0] = 0;
    }
    unsigned ifindex = if_nametoindex((char *)DeviceName);
    if (ifindex <= 0) {
        setebuf(Ebuf, "Invalid device specified.");
        return NULL;
    }
    handle = airpcap_handle_new();
    if (NULL == handle) {
        setebuf(Ebuf, "Error allocating handle.");
        return NULL;
    }
    handle->ifindex = ifindex;
    strncpy(handle->master_ifname, DeviceName, IF_NAMESIZE);
    if (-1 == nl80211_state_init(handle, Ebuf)) {
        return NULL;
    }
    if (-1 == nl80211_device_init(handle, Ebuf)) {
        return NULL;
    }
    if (-1 == nl80211_set_monitor(handle, AIRPCAP_VT_ACCEPT_EVERYTHING, Ebuf)) {
        AirpcapClose(handle);
        return NULL;
    }
    if (-1 == ifconfig_ifupdown(handle->master_ifname, 1)) {
        setebuf(Ebuf, "Unable to bring interface up: %s", strerror(errno));
        AirpcapClose(handle);
        return NULL;
    }
    if (FALSE == AirpcapSetDeviceChannel(handle, 6)) {
        if (NULL != Ebuf) {
            strncpy(Ebuf, handle->last_error, AIRPCAP_ERRBUF_SIZE);
            Ebuf[AIRPCAP_ERRBUF_SIZE] = 0;
        }
        AirpcapClose(handle);
        return NULL;
    }
    return handle;
}
PAirpcapHandle AirpcapOpen ( PCHAR DeviceName , PCHAR Ebuf ) { PAirpcapHandle handle ; if ( NULL != Ebuf ) { Ebuf [ 0 ] = 0 ; } unsigned ifindex = if_nametoindex ( ( char * ) DeviceName ) ; if ( ifindex <= 0 ) { setebuf ( Ebuf , " " ) ; return NULL ; } handle = airpcap_handle_new ( ) ; if ( NULL == handle ) { setebuf ( Ebuf , " " ) ; return NULL ; } handle -> ifindex = ifindex ; strncpy ( handle -> master_ifname , DeviceName , IF_NAMESIZE ) ; if ( -1 == nl80211_state_init ( handle , Ebuf ) ) { return NULL ; } if ( -1 == nl80211_device_init ( handle , Ebuf ) ) { return NULL ; } if ( -1 == nl80211_set_monitor ( handle , AIRPCAP_VT_ACCEPT_EVERYTHING , Ebuf ) ) { AirpcapClose ( handle ) ; return NULL ; } if ( -1 == ifconfig_ifupdown ( handle -> master_ifname , 1 ) ) { setebuf ( Ebuf , " " , strerror ( errno ) ) ; AirpcapClose ( handle ) ; return NULL ; } if ( FALSE == AirpcapSetDeviceChannel ( handle , 6 ) ) { if ( NULL != Ebuf ) { strncpy ( Ebuf , handle -> last_error , AIRPCAP_ERRBUF_SIZE ) ; Ebuf [ AIRPCAP_ERRBUF_SIZE ] = 0 ; } AirpcapClose ( handle ) ; return NULL ; } return handle ; }
TODO:
   - Check if interface supports monitor mode (NL80211_ATTR_SUPPORTED_IFTYPES)
     at all - in GET_WIPHY?
TODO : - Check if interface supports monitor mode ( NL80211_ATTR_SUPPORTED_IFTYPES ) at all - in GET_WIPHY?
/* Assign interface index after allocation. */ /* FIXME: handle if name + "mon" is too long for IF_NAMESIZE. */ /* Initialize unique netlink/nl80211 connection and
     * state for this handle. */ /* FIXME: proper deallocation. */ /* if (-1 == nl80211_create_monitor(handle, Ebuf)) { */ /*     return NULL; */ /* } */ /* By default, we must set to accept everything, as noted is the default.
     */ /* We might as well just call close here, since
         * all state is essentially allocated and ready.
         */ /* Finally, bring the device up (and leave it up - do not bring
     * down the interface in AirpcapClose!). */