#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include <pcap.h>
#include "ieee80211.h"
#include "ieee80211_radiotap.h"


void handle_packet(u_char* args, const struct pcap_pkthdr* header, const u_char* packet);

int main(int argc, char** argv) {
	while (true) {
		char errbuf[PCAP_ERRBUF_SIZE];
		const char* ifname = "rtap0";

		// retrieve netmask and IP address of device
		bpf_u_int32 net = 0;
		bpf_u_int32 mask = 0;
		if (pcap_lookupnet(ifname, &net, &mask, errbuf) == -1) {
			// ignore
		}

		pcap_t* handle = pcap_open_live(ifname, BUFSIZ, 1, 1000, errbuf);
		if (handle == NULL) {
			return -1;
		}

		printf("Setting up filter...\n");

		struct bpf_program fp;
		char* szFilter  = "link[0] == 0x80";
		if (pcap_compile(handle, &fp, szFilter, 0, net) == -1) {
			printf(pcap_geterr(handle));
			printf("\n");
			pcap_close(handle);
			return -1;
		}
		if (pcap_setfilter(handle, &fp) == -1) {
			pcap_freecode(&fp);
			pcap_close(handle);
			return -1;
		}

		pcap_loop(handle, 1, handle_packet, 0);

		pcap_freecode(&fp);
		pcap_close(handle);
	}
}

void handle_packet(u_char* args, const struct pcap_pkthdr* header, const u_char* packet) {
	printf("Handling packet...\n");
}
