Skip to content

Commit

Permalink
WLAN interface can be a pcap file (useful for debugging and more test…
Browse files Browse the repository at this point in the history
…ing)
  • Loading branch information
schmittner committed Jun 5, 2019
1 parent efd10f6 commit c6c45b9
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 8 deletions.
3 changes: 2 additions & 1 deletion daemon/core.c
Expand Up @@ -282,7 +282,8 @@ void awdl_switch_channel(struct ev_loop *loop, ev_timer *timer, int revents) {

if (chan_num_new && (chan_num_new != chan_num_old)) {
log_debug("switch channel to %d (slot %d)", chan_num_new, slot);
set_channel(state->io.wlan_ifindex, chan_num_new);
if (!state->io.wlan_is_file)
set_channel(state->io.wlan_ifindex, chan_num_new);
awdl_state->channel.current = chan_new;
}

Expand Down
63 changes: 58 additions & 5 deletions daemon/io.c
Expand Up @@ -104,6 +104,25 @@ static int open_nonblocking_device(const char *dev, pcap_t **pcap_handle, const
return fd;
}

static int open_savefile(const char *filename, pcap_t **pcap_handle) {
char errbuf[PCAP_ERRBUF_SIZE];
int fd;

pcap_t *handle = pcap_open_offline(filename, errbuf);
if (!handle) {
log_error("pcap: unable to open savefile (%s)", errbuf);
return -1;
}

if ((fd = pcap_get_selectable_fd(handle)) == -1) {
log_error("pcap: unable to get fd");
return -1;
}

*pcap_handle = handle;

return fd;
}

static int open_tun(char *dev, const struct ether_addr *self) {
#ifndef __APPLE__
Expand Down Expand Up @@ -240,12 +259,30 @@ static int open_tun(char *dev, const struct ether_addr *self) {
#endif /* __APPLE__ */
}

int io_state_init(struct io_state *state, const char *wlan, const char *host, const struct ether_addr *bssid_filter) {
static int io_state_init_wlan_try_savefile(struct io_state *state) {
int err;

state->wlan_fd = open_savefile(state->wlan_ifname, &state->wlan_handle);
if ((err = state->wlan_fd) < 0) {
log_trace("Could not open device or file: %s", state->wlan_ifname);
return err;
}
state->wlan_is_file = 1;
state->wlan_ifindex = 0;

return 0;
}

static int io_state_init_wlan(struct io_state *state, const char *wlan, const struct ether_addr *bssid_filter) {
int err;

/* setup WLAN interface */
strcpy(state->wlan_ifname, wlan);

if (!io_state_init_wlan_try_savefile(state)) {
log_info("Using savefile instead of device");
return 0;
}

state->wlan_ifindex = if_nametoindex(state->wlan_ifname);
if (!state->wlan_ifindex) {
log_error("No such interface exists %s", state->wlan_ifname);
Expand All @@ -269,18 +306,22 @@ int io_state_init(struct io_state *state, const char *wlan, const char *host, co
return err;
}
state->wlan_fd = open_nonblocking_device(state->wlan_ifname, &state->wlan_handle, bssid_filter);
if ((err = state->wlan_fd) < 0) {
if (state->wlan_fd < 0) {
log_error("Could not open device: %s", state->wlan_ifname);
return err;
}

err = link_ether_addr_get(state->wlan_ifname, &state->if_ether_addr);
if (err < 0) {
log_error("Could not get LLC address from %s", state->wlan_ifname);
return err;
}

/* setup host interface */
return 0;
}

static int io_state_init_host(struct io_state *state, const char *host) {
int err;

if (strlen(host) > 0) {
strcpy(state->host_ifname, host);
/* Host interface needs to have same ether_addr, to make active (!) monitor mode work */
Expand All @@ -301,6 +342,18 @@ int io_state_init(struct io_state *state, const char *wlan, const char *host, co
return 0;
}

int io_state_init(struct io_state *state, const char *wlan, const char *host, const struct ether_addr *bssid_filter) {
int err;

if (!(err = io_state_init_wlan(state, wlan, bssid_filter)))
return err;

if (!(err = io_state_init_host(state, host)))
return err;

return 0;
}

void io_state_free(struct io_state *state) {
close(state->host_fd);
pcap_close(state->wlan_handle);
Expand Down
4 changes: 3 additions & 1 deletion daemon/io.h
Expand Up @@ -23,6 +23,7 @@
#include <stdint.h>
#include <pcap/pcap.h>
#include <net/if.h>
#include <limits.h>

#ifdef __APPLE__
#include <net/ethernet.h>
Expand All @@ -32,7 +33,7 @@

struct io_state {
pcap_t *wlan_handle;
char wlan_ifname[IFNAMSIZ]; /* name of WLAN iface */
char wlan_ifname[PATH_MAX]; /* name of WLAN iface */
int wlan_ifindex; /* index of WLAN iface */
char host_ifname[IFNAMSIZ]; /* name of host iface */
int host_ifindex; /* index of host iface */
Expand All @@ -41,6 +42,7 @@ struct io_state {
int host_fd;
char *dumpfile;
char wlan_no_monitor_mode;
int wlan_is_file;
};

int io_state_init(struct io_state *state, const char *wlan, const char *host, const struct ether_addr *bssid_filter);
Expand Down
2 changes: 1 addition & 1 deletion daemon/owl.c
Expand Up @@ -92,7 +92,7 @@ int main(int argc, char *argv[]) {
int filter_rssi = 1;
int no_monitor_mode = 0;

char wlan[IFNAMSIZ];
char wlan[PATH_MAX];
char host[IFNAMSIZ] = DEFAULT_AWDL_DEVICE;

struct ev_loop *loop;
Expand Down

0 comments on commit c6c45b9

Please sign in to comment.