]> Joshua Wise's Git repositories - netwatch.git/blame_incremental - watcher/watcher.c
Don't write to the text console if we don't need to.
[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 printf("%c", c);
77 fflush(stdout);
78 }
79 }
80}
81
82
83
84
85#include <stdio.h>
86#include <stdlib.h>
87#include <string.h>
88#include <sys/types.h>
89#include <sys/socket.h>
90#include <netpacket/packet.h>
91#include <net/ethernet.h>
92
93typedef struct frame {
94 char header[14];
95 unsigned short datalen;
96 unsigned char cmd;
97 char data[];
98} frame;
99
100int sendsock;
101
102void send_text(char *d, int len)
103{
104 int datalen = sizeof(frame) + len;
105 frame * f = alloca(datalen + 1);
106 struct sockaddr_ll addr;
107
108 memcpy(f->header, "\x00\xb0\xd0\x97\xbc\xac\x00\x03\x93\x87\x84\x8C\x13\x38", 14);
109 f->datalen = len;
110 f->cmd = 0x42;
111 memcpy(f->data, d, len);
112
113 addr.sll_family = AF_PACKET;
114 addr.sll_ifindex = 2;
115 addr.sll_halen = 0;
116
117 if (sendto(sendsock, f, datalen, 0, (struct sockaddr *)&addr, sizeof(addr)) < datalen)
118 perror("sendto");
119}
120
121void send_reboot() {
122 frame f;
123 struct sockaddr_ll addr;
124
125 memcpy(f.header, "\x00\xb0\xd0\x97\xbc\xac\x00\x03\x93\x87\x84\x8C\x13\x38", 14);
126 f.datalen = 0;
127 f.cmd = 0xFE;
128 addr.sll_family = AF_PACKET;
129 addr.sll_ifindex = 2;
130 addr.sll_halen = 0;
131
132 if (sendto(sendsock, &f, sizeof(frame), 0, (struct sockaddr *)&addr, sizeof(addr)) < sizeof(frame))
133 perror("sendto");
134}
135
136void handler (u_char * user, const struct pcap_pkthdr *h, const u_char *bytes)
137{
138 netwatch_frame * f = (netwatch_frame *) bytes;
139 static int i = 0;
140
141 int data_len = h->caplen - sizeof(netwatch_frame) - 4;
142
143 if (data_len < 0) return;
144
145 if (ntohs(f->ethertype) != 0x1337) return;
146
147 if (data_len > sizeof(buf)) return;
148
149 if (f->buf_offset + data_len > sizeof(buf))
150 {
151 int wrap_pos = sizeof(buf) - f->buf_offset;
152 memcpy(buf + f->buf_offset, f->data, wrap_pos);
153 memcpy(buf, f->data + wrap_pos, data_len - wrap_pos);
154 } else {
155 memcpy(buf + f->buf_offset, f->data, data_len);
156 }
157
158 int c = getchar();
159 if (c > 0) {
160 if (c == 3)
161 pcap_breakloop(pcap);
162 if (c == '\r') c = '\n';
163 if (c == 127) c = '\b';
164 if (c == 0x1B)
165 {
166 /* Escape */
167 c = getchar();
168 if (c == '[')
169 {
170 c = getchar();
171 if (c == 'A')
172 c = 0x82;
173 if (c == 'B')
174 c = 0x83;
175 if (c == 'D')
176 c = 0x84;
177 if (c == 'C')
178 c = 0x85;
179 if (c == '4')
180 send_reboot();
181 }
182 }
183
184 char ch = c;
185 send_text(&ch, 1);
186 }
187
188 if (!(i++ % 2))
189 out(buf, f->buf_window);
190}
191
192int main() {
193 pcap = pcap_open_live(NULL, 65535, 1, 0, errbuf);
194
195 if (!pcap)
196 {
197 printf("pcap_open_live: %s\n", errbuf);
198 exit(1);
199 }
200 sendsock = socket(PF_PACKET, SOCK_RAW, htons(ETH_P_ALL));
201 if(sendsock < 0)
202 {
203 perror("socket");
204 exit(1);
205 }
206 fcntl(0, F_SETFL, O_NONBLOCK);
207 printf("\x1b[H\x1b[J\x1b[5C");
208
209 struct termios t, t_orig;
210 tcgetattr(0, &t);
211 tcgetattr(0, &t_orig);
212 cfmakeraw(&t);
213 tcsetattr(0, 0, &t);
214
215 pcap_loop(pcap, -1, handler, NULL);
216
217 tcsetattr(0, 0, &t_orig);
218 return 0;
219}
This page took 0.027432 seconds and 4 git commands to generate.