takumi funada
/
pktwatcher
Packet sniffer (http header inspecter) Make: Japanese Edition Vol.10
main.cpp@0:5d9cf80e1669, 2010-08-14 (annotated)
- Committer:
- funax
- Date:
- Sat Aug 14 14:08:26 2010 +0000
- Revision:
- 0:5d9cf80e1669
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
funax | 0:5d9cf80e1669 | 1 | // mbed packet watcher by musashinodenpa |
funax | 0:5d9cf80e1669 | 2 | // |
funax | 0:5d9cf80e1669 | 3 | |
funax | 0:5d9cf80e1669 | 4 | #include "mbed.h" |
funax | 0:5d9cf80e1669 | 5 | #include "HTTPClient.h" |
funax | 0:5d9cf80e1669 | 6 | |
funax | 0:5d9cf80e1669 | 7 | void analyse(char *buf, int size); |
funax | 0:5d9cf80e1669 | 8 | int search(char* hay, int size, char* needle); |
funax | 0:5d9cf80e1669 | 9 | unsigned long getTime(); |
funax | 0:5d9cf80e1669 | 10 | void alert (); |
funax | 0:5d9cf80e1669 | 11 | void printtime(); |
funax | 0:5d9cf80e1669 | 12 | |
funax | 0:5d9cf80e1669 | 13 | DigitalOut led_link(LED1); |
funax | 0:5d9cf80e1669 | 14 | DigitalOut led_detect(LED2); |
funax | 0:5d9cf80e1669 | 15 | DigitalOut led_3(LED3); |
funax | 0:5d9cf80e1669 | 16 | DigitalOut led_4(LED4); |
funax | 0:5d9cf80e1669 | 17 | DigitalOut led_red(p20); |
funax | 0:5d9cf80e1669 | 18 | |
funax | 0:5d9cf80e1669 | 19 | PwmOut motor(p21); |
funax | 0:5d9cf80e1669 | 20 | |
funax | 0:5d9cf80e1669 | 21 | Serial usb(USBTX, USBRX); |
funax | 0:5d9cf80e1669 | 22 | Serial com(p13, p14); // for printer |
funax | 0:5d9cf80e1669 | 23 | |
funax | 0:5d9cf80e1669 | 24 | int count = 0; |
funax | 0:5d9cf80e1669 | 25 | #define LIMIT 5 |
funax | 0:5d9cf80e1669 | 26 | |
funax | 0:5d9cf80e1669 | 27 | int main() { |
funax | 0:5d9cf80e1669 | 28 | Ethernet ether; |
funax | 0:5d9cf80e1669 | 29 | char buffer[0x600]; |
funax | 0:5d9cf80e1669 | 30 | int size = 0; |
funax | 0:5d9cf80e1669 | 31 | |
funax | 0:5d9cf80e1669 | 32 | usb.baud(38400); |
funax | 0:5d9cf80e1669 | 33 | com.baud(9600); |
funax | 0:5d9cf80e1669 | 34 | com.format(8, Serial::Odd, 2); // stopbit=2, parity=odd |
funax | 0:5d9cf80e1669 | 35 | led_red = 1; |
funax | 0:5d9cf80e1669 | 36 | |
funax | 0:5d9cf80e1669 | 37 | set_time(getTime()); |
funax | 0:5d9cf80e1669 | 38 | printtime(); |
funax | 0:5d9cf80e1669 | 39 | com.printf(" initialized.\n"); |
funax | 0:5d9cf80e1669 | 40 | |
funax | 0:5d9cf80e1669 | 41 | while (1) { |
funax | 0:5d9cf80e1669 | 42 | if ((size = ether.receive()) != 0) { |
funax | 0:5d9cf80e1669 | 43 | led_link = 0; |
funax | 0:5d9cf80e1669 | 44 | ether.read(buffer, size); |
funax | 0:5d9cf80e1669 | 45 | analyse(buffer, size); |
funax | 0:5d9cf80e1669 | 46 | } |
funax | 0:5d9cf80e1669 | 47 | led_link = ether.link(); |
funax | 0:5d9cf80e1669 | 48 | if (count == LIMIT) { |
funax | 0:5d9cf80e1669 | 49 | alert(); |
funax | 0:5d9cf80e1669 | 50 | count++; // keep counting |
funax | 0:5d9cf80e1669 | 51 | } |
funax | 0:5d9cf80e1669 | 52 | } |
funax | 0:5d9cf80e1669 | 53 | } |
funax | 0:5d9cf80e1669 | 54 | |
funax | 0:5d9cf80e1669 | 55 | void analyse(char *buf, int size) { |
funax | 0:5d9cf80e1669 | 56 | if (size < 42) return; // too small packet |
funax | 0:5d9cf80e1669 | 57 | if (buf[23] != 0x06) return; // not TCP/IP |
funax | 0:5d9cf80e1669 | 58 | if ((buf[36]*256 + buf[37]) != 80) return; // not http port |
funax | 0:5d9cf80e1669 | 59 | |
funax | 0:5d9cf80e1669 | 60 | int r = search(buf, size, "Host:"); // retrieving host name |
funax | 0:5d9cf80e1669 | 61 | if (r > 0) { |
funax | 0:5d9cf80e1669 | 62 | char hostname[32] = ""; |
funax | 0:5d9cf80e1669 | 63 | strncpy(hostname, &buf[r+1], strcspn(&buf[r+1], "\r\n")); |
funax | 0:5d9cf80e1669 | 64 | |
funax | 0:5d9cf80e1669 | 65 | if (NULL != strstr(hostname, "komachi.yomiuri.co.jp")) { |
funax | 0:5d9cf80e1669 | 66 | //usb.printf("%s ", hostname); |
funax | 0:5d9cf80e1669 | 67 | |
funax | 0:5d9cf80e1669 | 68 | r = search(buf, r, "GET"); |
funax | 0:5d9cf80e1669 | 69 | if (r > 0) { |
funax | 0:5d9cf80e1669 | 70 | char uri[256] = ""; |
funax | 0:5d9cf80e1669 | 71 | strncpy(uri, &buf[r+1], strcspn(&buf[r+1], "\r\n")); |
funax | 0:5d9cf80e1669 | 72 | |
funax | 0:5d9cf80e1669 | 73 | if (NULL != strstr(uri, ".htm")) { |
funax | 0:5d9cf80e1669 | 74 | led_detect = 1; |
funax | 0:5d9cf80e1669 | 75 | printtime(); |
funax | 0:5d9cf80e1669 | 76 | com.printf(" %d/%d\n", ++count, LIMIT); |
funax | 0:5d9cf80e1669 | 77 | //com.printf("%s\n", uri); |
funax | 0:5d9cf80e1669 | 78 | } |
funax | 0:5d9cf80e1669 | 79 | } |
funax | 0:5d9cf80e1669 | 80 | } |
funax | 0:5d9cf80e1669 | 81 | } |
funax | 0:5d9cf80e1669 | 82 | } |
funax | 0:5d9cf80e1669 | 83 | |
funax | 0:5d9cf80e1669 | 84 | int search(char* hay, int size, char* needle) { |
funax | 0:5d9cf80e1669 | 85 | int i = 0, pos = 0; |
funax | 0:5d9cf80e1669 | 86 | int len = strlen(needle); |
funax | 0:5d9cf80e1669 | 87 | |
funax | 0:5d9cf80e1669 | 88 | for (i = 0; i < size; i++) { |
funax | 0:5d9cf80e1669 | 89 | if (hay[i] == needle[pos]) { |
funax | 0:5d9cf80e1669 | 90 | if (++pos > len-1) return(i+1); |
funax | 0:5d9cf80e1669 | 91 | } else { |
funax | 0:5d9cf80e1669 | 92 | pos = 0; |
funax | 0:5d9cf80e1669 | 93 | } |
funax | 0:5d9cf80e1669 | 94 | } |
funax | 0:5d9cf80e1669 | 95 | return(-1); |
funax | 0:5d9cf80e1669 | 96 | } |
funax | 0:5d9cf80e1669 | 97 | |
funax | 0:5d9cf80e1669 | 98 | unsigned long getTime() { |
funax | 0:5d9cf80e1669 | 99 | char t[64]; |
funax | 0:5d9cf80e1669 | 100 | char r[] = "1234567890.123"; |
funax | 0:5d9cf80e1669 | 101 | |
funax | 0:5d9cf80e1669 | 102 | HTTPClient http; |
funax | 0:5d9cf80e1669 | 103 | http.get("http://ntp-a1.nict.go.jp/cgi-bin/jst", t); |
funax | 0:5d9cf80e1669 | 104 | |
funax | 0:5d9cf80e1669 | 105 | strncpy(r, strpbrk(t,"12"), sizeof(r)-1); // hacking |
funax | 0:5d9cf80e1669 | 106 | return(atol(r) + 3600 * 9); // UCT to JST ;-) |
funax | 0:5d9cf80e1669 | 107 | } |
funax | 0:5d9cf80e1669 | 108 | |
funax | 0:5d9cf80e1669 | 109 | void printtime() { |
funax | 0:5d9cf80e1669 | 110 | time_t t = time(NULL); |
funax | 0:5d9cf80e1669 | 111 | char buf[32]; |
funax | 0:5d9cf80e1669 | 112 | strftime(buf, 32, "%Y/%m/%d %H:%M:%S", localtime(&t)); |
funax | 0:5d9cf80e1669 | 113 | com.printf("%s", buf); |
funax | 0:5d9cf80e1669 | 114 | } |
funax | 0:5d9cf80e1669 | 115 | |
funax | 0:5d9cf80e1669 | 116 | void alert () { |
funax | 0:5d9cf80e1669 | 117 | motor = 0.1f; // drive slow |
funax | 0:5d9cf80e1669 | 118 | wait_ms(3000); |
funax | 0:5d9cf80e1669 | 119 | motor = 0; |
funax | 0:5d9cf80e1669 | 120 | } |