This is a low-level network debugging utility that utilizes raw packet i/o to construct and deconstruct tcp, udp, ipv4, arp, and icmp packets over ethernet.
net/net.h@0:d494b853ce97, 2010-10-12 (annotated)
- Committer:
- etherealflaim
- Date:
- Tue Oct 12 05:32:59 2010 +0000
- Revision:
- 0:d494b853ce97
- Child:
- 2:e8e09adc41fc
Initial Publish - Slightly unfinished, but usable
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
etherealflaim | 0:d494b853ce97 | 1 | #ifndef NETWORK_H |
etherealflaim | 0:d494b853ce97 | 2 | #define NETWORK_H |
etherealflaim | 0:d494b853ce97 | 3 | |
etherealflaim | 0:d494b853ce97 | 4 | #include "mbed.h" |
etherealflaim | 0:d494b853ce97 | 5 | #include "main.h" |
etherealflaim | 0:d494b853ce97 | 6 | |
etherealflaim | 0:d494b853ce97 | 7 | #include "ctype.h" |
etherealflaim | 0:d494b853ce97 | 8 | |
etherealflaim | 0:d494b853ce97 | 9 | // General networking checksum |
etherealflaim | 0:d494b853ce97 | 10 | inline u16 checksum(void *_mem, unsigned int bytes, void *skip_byte = NULL, unsigned int skip_count = 0, u16 last = 0) |
etherealflaim | 0:d494b853ce97 | 11 | { |
etherealflaim | 0:d494b853ce97 | 12 | u32 sum = 0; |
etherealflaim | 0:d494b853ce97 | 13 | u16 *mem = (u16*)_mem; |
etherealflaim | 0:d494b853ce97 | 14 | unsigned int skip_start = (u16*)skip_byte - mem; |
etherealflaim | 0:d494b853ce97 | 15 | |
etherealflaim | 0:d494b853ce97 | 16 | if (last) |
etherealflaim | 0:d494b853ce97 | 17 | sum = last ^ 0xFFFF; |
etherealflaim | 0:d494b853ce97 | 18 | |
etherealflaim | 0:d494b853ce97 | 19 | //main_log.printf("CK: 0x%8X", sum); |
etherealflaim | 0:d494b853ce97 | 20 | for (register unsigned int i = 0; i < bytes/sizeof(u16); ++i) |
etherealflaim | 0:d494b853ce97 | 21 | { |
etherealflaim | 0:d494b853ce97 | 22 | // Skip bytes we don't use (e.g. the checksum itself) |
etherealflaim | 0:d494b853ce97 | 23 | if (i == skip_start) |
etherealflaim | 0:d494b853ce97 | 24 | i += skip_count/sizeof(u16); |
etherealflaim | 0:d494b853ce97 | 25 | |
etherealflaim | 0:d494b853ce97 | 26 | // Sum them up |
etherealflaim | 0:d494b853ce97 | 27 | sum += mem[i]; |
etherealflaim | 0:d494b853ce97 | 28 | |
etherealflaim | 0:d494b853ce97 | 29 | //main_log.printf("CK: + 0x%04X = 0x%8X", mem[i], sum); |
etherealflaim | 0:d494b853ce97 | 30 | } |
etherealflaim | 0:d494b853ce97 | 31 | |
etherealflaim | 0:d494b853ce97 | 32 | // One's complement of the one's complement sum |
etherealflaim | 0:d494b853ce97 | 33 | sum += sum >> 16; |
etherealflaim | 0:d494b853ce97 | 34 | sum &= 0xFFFF; |
etherealflaim | 0:d494b853ce97 | 35 | sum ^= 0xFFFF; |
etherealflaim | 0:d494b853ce97 | 36 | |
etherealflaim | 0:d494b853ce97 | 37 | //main_log.printf("CK: ~ 0x%8X", sum); |
etherealflaim | 0:d494b853ce97 | 38 | |
etherealflaim | 0:d494b853ce97 | 39 | return (u16)(sum); |
etherealflaim | 0:d494b853ce97 | 40 | } |
etherealflaim | 0:d494b853ce97 | 41 | |
etherealflaim | 0:d494b853ce97 | 42 | // Generic u16 endian swapping |
etherealflaim | 0:d494b853ce97 | 43 | inline void fix_endian_u16(u16 *p) |
etherealflaim | 0:d494b853ce97 | 44 | { |
etherealflaim | 0:d494b853ce97 | 45 | char *bytes = (char*)p; |
etherealflaim | 0:d494b853ce97 | 46 | bytes[0] ^= bytes[1]; |
etherealflaim | 0:d494b853ce97 | 47 | bytes[1] ^= bytes[0]; |
etherealflaim | 0:d494b853ce97 | 48 | bytes[0] ^= bytes[1]; |
etherealflaim | 0:d494b853ce97 | 49 | } |
etherealflaim | 0:d494b853ce97 | 50 | |
etherealflaim | 0:d494b853ce97 | 51 | // Generic u32 endian swapping |
etherealflaim | 0:d494b853ce97 | 52 | inline void fix_endian_u32(u32 *p) |
etherealflaim | 0:d494b853ce97 | 53 | { |
etherealflaim | 0:d494b853ce97 | 54 | char *bytes = (char*)p; |
etherealflaim | 0:d494b853ce97 | 55 | // Swap outer bytes |
etherealflaim | 0:d494b853ce97 | 56 | bytes[0] ^= bytes[3]; |
etherealflaim | 0:d494b853ce97 | 57 | bytes[3] ^= bytes[0]; |
etherealflaim | 0:d494b853ce97 | 58 | bytes[0] ^= bytes[3]; |
etherealflaim | 0:d494b853ce97 | 59 | // Swap inner bytes |
etherealflaim | 0:d494b853ce97 | 60 | bytes[1] ^= bytes[2]; |
etherealflaim | 0:d494b853ce97 | 61 | bytes[2] ^= bytes[1]; |
etherealflaim | 0:d494b853ce97 | 62 | bytes[1] ^= bytes[2]; |
etherealflaim | 0:d494b853ce97 | 63 | } |
etherealflaim | 0:d494b853ce97 | 64 | |
etherealflaim | 0:d494b853ce97 | 65 | /* Printing character */ |
etherealflaim | 0:d494b853ce97 | 66 | #define PCHAR(x) (isprint(x)?(x):'.') |
etherealflaim | 0:d494b853ce97 | 67 | |
etherealflaim | 0:d494b853ce97 | 68 | /* Hex dump a word-aligned number of bytes (may misbehave if length is not a multiple of 32 bits */ |
etherealflaim | 0:d494b853ce97 | 69 | inline void hex_dump(void *base, unsigned int length) |
etherealflaim | 0:d494b853ce97 | 70 | { |
etherealflaim | 0:d494b853ce97 | 71 | char line[/*indent*/ 2 + /*0x*/ 2 + /*addr*/ 8 + /* : */ 3 + /*4xXXXX */ 4*5 + /*: */ 2 + /*8x.*/ 8 + /*\0*/ 1]; |
etherealflaim | 0:d494b853ce97 | 72 | for (char *p = (char*)base; p - (char*)base < length; p += 8) |
etherealflaim | 0:d494b853ce97 | 73 | { |
etherealflaim | 0:d494b853ce97 | 74 | sprintf(line, " 0x%08X : %02X%02X %02X%02X %02X%02X %02X%02X : %c%c%c%c%c%c%c%c", p, |
etherealflaim | 0:d494b853ce97 | 75 | p[0], p[1], p[2], p[3], p[4], p[5], p[6], p[7], |
etherealflaim | 0:d494b853ce97 | 76 | PCHAR(p[0]), PCHAR(p[1]), PCHAR(p[2]), PCHAR(p[3]), PCHAR(p[4]), PCHAR(p[5]), PCHAR(p[6]), PCHAR(p[7])); |
etherealflaim | 0:d494b853ce97 | 77 | main_log.printf(line); |
etherealflaim | 0:d494b853ce97 | 78 | } |
etherealflaim | 0:d494b853ce97 | 79 | } |
etherealflaim | 0:d494b853ce97 | 80 | |
etherealflaim | 0:d494b853ce97 | 81 | // Ethernet |
etherealflaim | 0:d494b853ce97 | 82 | #include "ethernet.h" |
etherealflaim | 0:d494b853ce97 | 83 | |
etherealflaim | 0:d494b853ce97 | 84 | // ARP and IP |
etherealflaim | 0:d494b853ce97 | 85 | #include "arp.h" |
etherealflaim | 0:d494b853ce97 | 86 | #include "ip.h" |
etherealflaim | 0:d494b853ce97 | 87 | |
etherealflaim | 0:d494b853ce97 | 88 | // TCP, UDP, and ICMP |
etherealflaim | 0:d494b853ce97 | 89 | #include "tcp.h" |
etherealflaim | 0:d494b853ce97 | 90 | #include "udp.h" |
etherealflaim | 0:d494b853ce97 | 91 | #include "icmp.h" |
etherealflaim | 0:d494b853ce97 | 92 | |
etherealflaim | 0:d494b853ce97 | 93 | #endif // NETWORK_H |