]> Joshua Wise's Git repositories - netwatch.git/blame_incremental - watcher/watcher.c
Merge branch 'master' of git+ssh://jacob@git.joshuawise.com/storage/git/netwatch
[netwatch.git] / watcher / watcher.c
... / ...
CommitLineData
1#include <pcap.h>
2#include <ctype.h>
3#include <termios.h>
4#include <unistd.h>
5#include <fcntl.h>
6#include <arpa/inet.h>
7#include <stdlib.h>
8#include <string.h>
9
10char errbuf[PCAP_ERRBUF_SIZE];
11
12char buf[0x8000];
13
14typedef struct netwatch_frame {
15 char dest_mac[6];
16 char src_mac[6];
17 unsigned short whatthefuck;
18 unsigned short ethertype;
19 unsigned short buf_offset;
20 unsigned short buf_window;
21 char data[];
22} netwatch_frame;
23
24pcap_t * pcap;
25
26void out(char *buf, unsigned short start)
27{
28 int y,x;
29 buf += start;
30 printf("\x1B[H");
31 for (y = 0; y < 25; y++)
32 {
33 printf("\x1B[%d;%df",y+1,1);
34 for (x = 0; x < 80; x++)
35 {
36 unsigned char c, col;
37 static unsigned char last = 0xFF;
38
39 c = *(buf++);
40 col = *(buf++);
41
42 if (col != last)
43 {
44 printf("\x1B[0m\x1B[");
45 switch (col & 0x7)
46 {
47 case 0: printf("30;"); break;
48 case 1: printf("34;"); break;
49 case 2: printf("32;"); break;
50 case 3: printf("36;"); break;
51 case 4: printf("31;"); break;
52 case 5: printf("35;"); break;
53 case 6: printf("33;"); break;
54 case 7: printf("37;"); break;
55 }
56 if (col & 0x8)
57 printf("m\x1B[1;");
58 if (col & 0x80)
59 printf("m\x1B[5;");
60 switch ((col >> 4) & 0x7)
61 {
62 case 0: printf("40"); break;
63 case 1: printf("44"); break;
64 case 2: printf("42"); break;
65 case 3: printf("46"); break;
66 case 4: printf("41"); break;
67 case 5: printf("45"); break;
68 case 6: printf("43"); break;
69 case 7: printf("47"); break;
70 }
71 printf("m");
72 }
73 last = col;
74 if (c == 0)
75 c = ' ';
76 if (!isprint(c))
77 c = 'X';
78 printf("%c", c);
79 fflush(stdout);
80 }
81 }
82}
83
84
85
86
87#include <stdio.h>
88#include <stdlib.h>
89#include <string.h>
90#include <sys/types.h>
91#include <sys/socket.h>
92#include <netpacket/packet.h>
93#include <net/ethernet.h>
94
95typedef struct frame {
96 char header[14];
97 unsigned short datalen;
98 unsigned char cmd;
99 char data[];
100} frame;
101
102int sendsock;
103
104void send_text(char *d, int len)
105{
106 int datalen = sizeof(frame) + len;
107 frame * f = alloca(datalen + 1);
108 struct sockaddr_ll addr;
109
110 memcpy(f->header, "\x00\xb0\xd0\x97\xbc\xac\x00\x03\x93\x87\x84\x8C\x13\x38", 14);
111 f->datalen = len;
112 f->cmd = 0x42;
113 memcpy(f->data, d, len);
114
115 addr.sll_family = AF_PACKET;
116 addr.sll_ifindex = 2;
117 addr.sll_halen = 0;
118
119 if (sendto(sendsock, f, datalen, 0, (struct sockaddr *)&addr, sizeof(addr)) < datalen)
120 perror("sendto");
121}
122
123void handler (u_char * user, const struct pcap_pkthdr *h, const u_char *bytes)
124{
125 netwatch_frame * f = (netwatch_frame *) bytes;
126 static int i = 0;
127
128 int data_len = h->caplen - sizeof(netwatch_frame) - 4;
129
130 if (data_len < 0) return;
131
132 if (ntohs(f->ethertype) != 0x1337) return;
133
134 if (data_len > sizeof(buf)) return;
135
136 if (f->buf_offset + data_len > sizeof(buf))
137 {
138 int wrap_pos = sizeof(buf) - f->buf_offset;
139 memcpy(buf + f->buf_offset, f->data, wrap_pos);
140 memcpy(buf, f->data + wrap_pos, data_len - wrap_pos);
141 } else {
142 memcpy(buf + f->buf_offset, f->data, data_len);
143 }
144
145 int c = getchar();
146 if (c > 0) {
147 if (c == 3)
148 pcap_breakloop(pcap);
149 if (c == '\r') c = '\n';
150 if (c == 127) c = '\b';
151
152 char ch = c;
153 send_text(&ch, 1);
154 }
155
156 if (!(i++ % 2))
157 out(buf, f->buf_window);
158}
159
160int main() {
161 pcap = pcap_open_live(NULL, 65535, 1, 0, errbuf);
162
163 if (!pcap)
164 {
165 printf("pcap_open_live: %s\n", errbuf);
166 exit(1);
167 }
168 sendsock = socket(PF_PACKET, SOCK_RAW, htons(ETH_P_ALL));
169 if(sendsock < 0)
170 {
171 perror("socket");
172 exit(1);
173 }
174 fcntl(0, F_SETFL, O_NONBLOCK);
175 printf("\x1b[H\x1b[J\x1b[5C");
176
177 struct termios t, t_orig;
178 tcgetattr(0, &t);
179 tcgetattr(0, &t_orig);
180 cfmakeraw(&t);
181 tcsetattr(0, 0, &t);
182
183 pcap_loop(pcap, -1, handler, NULL);
184
185 tcsetattr(0, 0, &t_orig);
186 return 0;
187}
This page took 0.023473 seconds and 4 git commands to generate.