capture-pcap.c 2.77 KB
Newer Older
1 2
/* horst - Highly Optimized Radio Scanning Tool
 *
3
 * Copyright (C) 2005-2016 Bruno Randolf (br1@einfach.org)
Bruno Randolf's avatar
Bruno Randolf committed
4
 * Copyright (C) 2007 Sven-Ola Tuecke
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 */

#include <stdio.h>
#include <string.h>
#include <pcap.h>
Bruno Randolf's avatar
Bruno Randolf committed
24
#include <err.h>
25 26 27 28 29 30 31 32 33 34 35 36 37

#include "capture.h"
#include "util.h"

void __attribute__ ((format (printf, 1, 2)))
printlog(const char *fmt, ...);

#define PCAP_TIMEOUT 200

static unsigned char* pcap_buffer;
static size_t pcap_bufsize;
static pcap_t *pcap_fp = NULL;

Bruno Randolf's avatar
Bruno Randolf committed
38
static void handler(u_char *user, const struct pcap_pkthdr *h, const u_char *bytes)
39 40
{
	*((int *)user) = h->len;
Bruno Randolf's avatar
Bruno Randolf committed
41
	if (pcap_bufsize < h->len) {
42 43 44 45 46 47 48
		printlog("ERROR: Buffer(%d) too small for %d bytes",
			 (int)pcap_bufsize, h->len);
		*((int *)user) = pcap_bufsize;
	}
	memmove(pcap_buffer, bytes, *((int *)user));
}

Bruno Randolf's avatar
Bruno Randolf committed
49
int open_packet_socket(char* devname, __attribute__((unused)) int recv_buffer_size)
50 51
{
	char error[PCAP_ERRBUF_SIZE];
Bruno Randolf's avatar
Bruno Randolf committed
52 53 54
	int ret;

	pcap_fp = pcap_create(devname, error);
Bruno Randolf's avatar
Bruno Randolf committed
55
	if (pcap_fp == NULL) {
Bruno Randolf's avatar
Bruno Randolf committed
56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71
		fprintf(stderr, "Couldn't create pcap: %s\n", error);
		return -1;
	}

	pcap_set_promisc(pcap_fp, 1);

#if defined(__APPLE__)
	if (pcap_can_set_rfmon(pcap_fp))
		pcap_set_rfmon(pcap_fp, 1);
	else
		err(1, "Couldn't activate monitor mode");
#endif

	ret = pcap_activate(pcap_fp);
	if (ret < 0) {
		fprintf(stderr, "Can't activate pcap: %d\n", ret);
72
		return -1;
Bruno Randolf's avatar
Bruno Randolf committed
73 74 75
	}

	return pcap_fileno(pcap_fp);
76 77
}

Bruno Randolf's avatar
Bruno Randolf committed
78 79 80
int device_get_hwinfo(__attribute__((unused)) int fd,
		      __attribute__((unused)) char* ifname,
		      __attribute__((unused)) unsigned char* mac)
81 82 83 84 85 86 87 88 89 90 91
{
	if (pcap_fp != NULL) {
		switch (pcap_datalink(pcap_fp)) {
		case DLT_IEEE802_11_RADIO:
			return 803;
		case DLT_PRISM_HEADER:
			return 802;
		default:
			return 801;
		}
	}
92
	/* TODO: mac address not implemented */
93 94 95
	return -1;
}

Bruno Randolf's avatar
Bruno Randolf committed
96 97
int recv_packet(__attribute__((unused)) int fd,
		unsigned char* buffer, size_t bufsize)
98
{
99
	int ret = 0;
100 101 102 103 104 105 106 107
	pcap_buffer = buffer;
	pcap_bufsize = bufsize;
	if (0 == pcap_dispatch(pcap_fp, 1, handler, (u_char *)&ret))
		return -1;
	return ret;
}


Bruno Randolf's avatar
Bruno Randolf committed
108
void close_packet_socket(__attribute__((unused)) int fd)
109
{
Bruno Randolf's avatar
Bruno Randolf committed
110 111
	if (pcap_fp != NULL)
		pcap_close(pcap_fp);
112
}