X-Git-Url: http://git.joshuawise.com/netwatch.git/blobdiff_plain/172a5ba7dad6e9c7e38e4e5aa2607de3728f0ebf..6d6494e427a90913216c94306593a65cb5361c0d:/watcher/watcher.c?ds=inline diff --git a/watcher/watcher.c b/watcher/watcher.c index 8b1eef7..65d7996 100644 --- a/watcher/watcher.c +++ b/watcher/watcher.c @@ -1,4 +1,8 @@ #include +#include +#include +#include +#include #include #include #include @@ -17,21 +21,27 @@ typedef struct netwatch_frame { char data[]; } netwatch_frame; +pcap_t * pcap; + void out(char *buf, unsigned short start) { int y,x; buf += start; - printf("\x1B[0;0H"); + printf("\x1B[H"); for (y = 0; y < 25; y++) { + printf("\x1B[%d;%df",y+1,1); for (x = 0; x < 80; x++) { unsigned char c, col; + static unsigned char last = 0xFF; c = *(buf++); col = *(buf++); - - printf("\x1B[0;"); + + if (col != last) + { + printf("\x1B[0m\x1B["); switch (col & 0x7) { case 0: printf("30;"); break; @@ -44,9 +54,9 @@ void out(char *buf, unsigned short start) case 7: printf("37;"); break; } if (col & 0x8) - printf("1;"); + printf("m\x1B[1;"); if (col & 0x80) - printf("5;"); + printf("m\x1B[5;"); switch ((col >> 4) & 0x7) { case 0: printf("40"); break; @@ -58,18 +68,77 @@ void out(char *buf, unsigned short start) case 6: printf("43"); break; case 7: printf("47"); break; } - - printf("m%c", c); + printf("m"); + } + last = col; + if (c == 0) + c = ' '; + printf("%c", c); + fflush(stdout); } - printf("\n"); } } + + + +#include +#include +#include +#include +#include +#include +#include + +typedef struct frame { + char header[14]; + unsigned short datalen; + unsigned char cmd; + char data[]; +} frame; + +int sendsock; + +void send_text(char *d, int len) +{ + int datalen = sizeof(frame) + len; + frame * f = alloca(datalen + 1); + struct sockaddr_ll addr; + + memcpy(f->header, "\x00\xb0\xd0\x97\xbc\xac\x00\x03\x93\x87\x84\x8C\x13\x38", 14); + f->datalen = len; + f->cmd = 0x42; + memcpy(f->data, d, len); + + addr.sll_family = AF_PACKET; + addr.sll_ifindex = 2; + addr.sll_halen = 0; + + if (sendto(sendsock, f, datalen, 0, (struct sockaddr *)&addr, sizeof(addr)) < datalen) + perror("sendto"); +} + +void send_reboot() { + frame f; + struct sockaddr_ll addr; + + memcpy(f.header, "\x00\xb0\xd0\x97\xbc\xac\x00\x03\x93\x87\x84\x8C\x13\x38", 14); + f.datalen = 0; + f.cmd = 0xFE; + addr.sll_family = AF_PACKET; + addr.sll_ifindex = 2; + addr.sll_halen = 0; + + if (sendto(sendsock, &f, sizeof(frame), 0, (struct sockaddr *)&addr, sizeof(addr)) < sizeof(frame)) + perror("sendto"); +} + void handler (u_char * user, const struct pcap_pkthdr *h, const u_char *bytes) { netwatch_frame * f = (netwatch_frame *) bytes; + static int i = 0; - int data_len = h->caplen - sizeof(netwatch_frame); + int data_len = h->caplen - sizeof(netwatch_frame) - 4; if (data_len < 0) return; @@ -86,12 +155,41 @@ void handler (u_char * user, const struct pcap_pkthdr *h, const u_char *bytes) memcpy(buf + f->buf_offset, f->data, data_len); } - out(buf, f->buf_window); + int c = getchar(); + if (c > 0) { + if (c == 3) + pcap_breakloop(pcap); + if (c == '\r') c = '\n'; + if (c == 127) c = '\b'; + if (c == 0x1B) + { + /* Escape */ + c = getchar(); + if (c == '[') + { + c = getchar(); + if (c == 'A') + c = 0x82; + if (c == 'B') + c = 0x83; + if (c == 'D') + c = 0x84; + if (c == 'C') + c = 0x85; + if (c == '4') + send_reboot(); + } + } + + char ch = c; + send_text(&ch, 1); + } + + if (!(i++ % 2)) + out(buf, f->buf_window); } int main() { - pcap_t * pcap; - pcap = pcap_open_live(NULL, 65535, 1, 0, errbuf); if (!pcap) @@ -99,8 +197,23 @@ int main() { printf("pcap_open_live: %s\n", errbuf); exit(1); } + sendsock = socket(PF_PACKET, SOCK_RAW, htons(ETH_P_ALL)); + if(sendsock < 0) + { + perror("socket"); + exit(1); + } + fcntl(0, F_SETFL, O_NONBLOCK); + printf("\x1b[H\x1b[J\x1b[5C"); + + struct termios t, t_orig; + tcgetattr(0, &t); + tcgetattr(0, &t_orig); + cfmakeraw(&t); + tcsetattr(0, 0, &t); pcap_loop(pcap, -1, handler, NULL); + tcsetattr(0, 0, &t_orig); return 0; }