summaryrefslogtreecommitdiffstats
path: root/prog/upravljalnik/pcap.c
diff options
context:
space:
mode:
Diffstat (limited to 'prog/upravljalnik/pcap.c')
-rw-r--r--prog/upravljalnik/pcap.c92
1 files changed, 92 insertions, 0 deletions
diff --git a/prog/upravljalnik/pcap.c b/prog/upravljalnik/pcap.c
new file mode 100644
index 0000000..fe89856
--- /dev/null
+++ b/prog/upravljalnik/pcap.c
@@ -0,0 +1,92 @@
+#define _GNU_SOURCE
+#include <pcap/pcap.h>
+#include <error.h>
+#include <stdlib.h>
+#include <poll.h>
+#include <errno.h>
+#include <string.h>
+#define error_here(status, errnum, ...) do { error_at_line(0, errnum, __FILE__, __LINE__, __VA_ARGS__); r = status; if (r) goto r; } while (0)
+#define S0(x) (x ? x : "")
+#define PKTSIZ 1024
+int main (int argc, char ** argv) {
+ struct bpf_program fp = { 0 };
+ char pcaperr[PCAP_ERRBUF_SIZE];
+ int r = 0;
+ pcap_if_t * vmesniki = NULL;
+ if (pcap_findalldevs(&vmesniki, pcaperr))
+ error_here(1, 0, "pcap_findalldevs: %s", pcaperr);
+ if (!vmesniki)
+ error_here(2, 0, "ni vmesnikov za prisluškovanje -- zaženi airmon-ng?");
+ int izbira = -1;
+ if (argc > 1)
+ izbira = atoi(argv[1]);
+ pcap_if_t * izbran = NULL;
+ int zap = 0;
+ for (pcap_if_t * vmesnik = vmesniki; vmesnik; vmesnik = vmesnik->next) {
+ printf("%d\t%s\t%s%s\n", zap, vmesnik->name, S0(vmesnik->description), zap == izbira ? "\tIZBRAN" : "");
+ if (zap == izbira)
+ izbran = vmesnik;
+ zap++;
+ }
+ if (!izbran)
+ error_here(3, 0, "v 1. argument napiši zaporedno številko vmesnika iz seznama");
+ pcap_t * pcap = pcap_create(izbran->name, pcaperr);
+ if (!pcap)
+ error_here(4, 0, "pcap_create(NULL, pcaperr): %s", pcaperr);
+ pcap_set_snaplen(pcap, PKTSIZ);
+ pcap_set_promisc(pcap, 1);
+ int ret = pcap_can_set_rfmon(pcap);
+ if (ret < 0)
+ error_here(5, 0, "pcap_can_set_rfmon(pcap): %s", pcap_geterr(pcap));
+ if (ret)
+ pcap_set_rfmon(pcap, 1);
+ else
+ error_here(0, 0, "!pcap_can_set_rfmon(pcap)");
+ pcap_set_immediate_mode(pcap, 1);
+ if ((ret = pcap_activate(pcap)))
+ error_here(6, 0, "pcap_activate(pcap): %s", pcap_statustostr(ret));
+ if (pcap_setdirection(pcap, PCAP_D_IN))
+ error_here(7, 0, "pcap_setdirection(pcap, PCAP_D_IN): %s", pcap_geterr(pcap));
+ if (pcap_compile(pcap, &fp, "wlan addr1 4b:40:34:41:2e:53", 1, PCAP_NETMASK_UNKNOWN))
+ error_here(8, 0, "pcap_compile(pcap, fp, ...): %s", pcap_geterr(pcap));
+ if (pcap_setfilter(pcap, &fp))
+ error_here(9, 0, "pcap_setfilter(pcap, fp): %s", pcap_geterr(pcap));
+ const unsigned char * packet = NULL;
+ struct pcap_pkthdr * pkt_header = NULL;
+ int fd = pcap_get_selectable_fd(pcap);
+ if (fd == -1)
+ error_here(10, 0, "pcap_get_selectable_fd(pcap) == -1");
+ struct pollfd pollfd = {
+ .fd = fd,
+ .events = POLLIN
+ };
+ while (poll(&pollfd, 1, -1) != -1) {
+ if ((ret = pcap_next_ex(pcap, &pkt_header, &packet)) == 0)
+ error_here(11, 0, "pcap_next_ex(pcap, &pkt_header, &packet) == 0");
+ if (ret == PCAP_ERROR)
+ error_here(12, 0, "pcap_next_ex(pcap, &pkt_header, &packet): %s", pcap_geterr(pcap));
+#define MAGIC "K@4A.SI "
+ const unsigned char * c = memmem(packet, PKTSIZ, MAGIC, strlen(MAGIC));
+ if (!c) {
+ error_here(0, 0, "malformed packet");
+ continue;
+ }
+ c += strlen(MAGIC);
+ if (c + 1 /* koliko bomo brali */ >= packet+PKTSIZ) {
+ error_here(0, 0, "malformed packet");
+ continue;
+ }
+ fprintf(stderr, "\r");
+ for (int i = 0; i < 128; i++)
+ fprintf(stderr, i < c[0]/2 ? "@" : " ");
+ fflush(stderr);
+ }
+ error_here(11, errno, "poll(&pollfd, 1, -1)");
+ r:
+ pcap_freecode(&fp);
+ if (vmesniki)
+ pcap_freealldevs(vmesniki);
+ if (pcap)
+ pcap_close(pcap);
+ return r;
+}