]> Joshua Wise's Git repositories - netwatch.git/blob - watcher/watcher.c
add watcher
[netwatch.git] / watcher / watcher.c
1 #include <pcap.h>
2 #include <arpa/inet.h>
3 #include <stdlib.h>
4 #include <string.h>
5
6 char errbuf[PCAP_ERRBUF_SIZE];
7
8 char buf[0x8000];
9
10 typedef struct netwatch_frame {
11         char dest_mac[6];
12         char src_mac[6];
13         unsigned short whatthefuck;
14         unsigned short ethertype;
15         unsigned short buf_offset;
16         unsigned short buf_window;
17         char data[];
18 } netwatch_frame;
19
20 void out(char *buf, unsigned short start)
21 {
22   int y,x;
23   buf += start;
24   printf("\x1B[0;0H");
25   for (y = 0; y < 25; y++)
26   {
27     for (x = 0; x < 80; x++)
28     {
29       unsigned char c, col;
30       
31       c = *(buf++);
32       col = *(buf++);
33       
34       printf("\x1B[0;");
35       switch (col & 0x7)
36       {
37       case 0: printf("30;"); break;
38       case 1: printf("34;"); break;
39       case 2: printf("32;"); break;
40       case 3: printf("36;"); break;
41       case 4: printf("31;"); break;
42       case 5: printf("35;"); break;
43       case 6: printf("33;"); break;
44       case 7: printf("37;"); break;
45       }
46       if (col & 0x8)
47         printf("1;");
48       if (col & 0x80)
49         printf("5;");
50       switch ((col >> 4) & 0x7)
51       {
52       case 0: printf("40"); break;
53       case 1: printf("44"); break;
54       case 2: printf("42"); break;
55       case 3: printf("46"); break;
56       case 4: printf("41"); break;
57       case 5: printf("45"); break;
58       case 6: printf("43"); break;
59       case 7: printf("47"); break;
60       }
61       
62       printf("m%c", c);
63     }
64     printf("\n");
65   }
66 }
67
68 void handler (u_char * user, const struct pcap_pkthdr *h, const u_char *bytes)
69 {
70         netwatch_frame * f = (netwatch_frame *) bytes;
71
72         int data_len = h->caplen - sizeof(netwatch_frame);
73
74         if (data_len < 0) return;
75
76         if (ntohs(f->ethertype) != 0x1337) return;
77
78         if (data_len > sizeof(buf)) return;
79
80         if (f->buf_offset + data_len > sizeof(buf))
81         {
82                 int wrap_pos = sizeof(buf) - f->buf_offset;
83                 memcpy(buf + f->buf_offset, f->data, wrap_pos);
84                 memcpy(buf, f->data + wrap_pos, data_len - wrap_pos);
85         } else {
86                 memcpy(buf + f->buf_offset, f->data, data_len);
87         }
88
89         out(buf, f->buf_window);
90 }
91
92 int main() {
93         pcap_t * pcap;
94
95         pcap = pcap_open_live(NULL, 65535, 1, 0, errbuf);
96  
97         if (!pcap)
98         {
99                 printf("pcap_open_live: %s\n", errbuf);
100                 exit(1);
101         }
102
103         pcap_loop(pcap, -1, handler, NULL);
104
105         return 0;
106 }
This page took 0.028882 seconds and 4 git commands to generate.