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.

Dependencies:   mbed

Committer:
etherealflaim
Date:
Tue Oct 12 06:10:41 2010 +0000
Revision:
2:e8e09adc41fc
Parent:
0:d494b853ce97
Child:
5:c56386b9fc33
Finished documentation for net/ directory

Who changed what in which revision?

UserRevisionLine numberNew 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 2:e8e09adc41fc 9 /// \file Overall networking header - Includes all other net/ headers
etherealflaim 2:e8e09adc41fc 10
etherealflaim 2:e8e09adc41fc 11 /// General networking checksum - Used for IP, TCP, UDP, ICMP, etc.
etherealflaim 2:e8e09adc41fc 12 /// Computes the one's complement of the one's complement sum of all of the given bytes except for the memory
etherealflaim 2:e8e09adc41fc 13 /// at skip_byte for skip_count bytes (e.g. the checksum). Optionally resumes computation from the (given) last checksum.
etherealflaim 0:d494b853ce97 14 inline u16 checksum(void *_mem, unsigned int bytes, void *skip_byte = NULL, unsigned int skip_count = 0, u16 last = 0)
etherealflaim 0:d494b853ce97 15 {
etherealflaim 0:d494b853ce97 16 u32 sum = 0;
etherealflaim 0:d494b853ce97 17 u16 *mem = (u16*)_mem;
etherealflaim 0:d494b853ce97 18 unsigned int skip_start = (u16*)skip_byte - mem;
etherealflaim 0:d494b853ce97 19
etherealflaim 0:d494b853ce97 20 if (last)
etherealflaim 0:d494b853ce97 21 sum = last ^ 0xFFFF;
etherealflaim 0:d494b853ce97 22
etherealflaim 0:d494b853ce97 23 //main_log.printf("CK: 0x%8X", sum);
etherealflaim 0:d494b853ce97 24 for (register unsigned int i = 0; i < bytes/sizeof(u16); ++i)
etherealflaim 0:d494b853ce97 25 {
etherealflaim 0:d494b853ce97 26 // Skip bytes we don't use (e.g. the checksum itself)
etherealflaim 0:d494b853ce97 27 if (i == skip_start)
etherealflaim 0:d494b853ce97 28 i += skip_count/sizeof(u16);
etherealflaim 0:d494b853ce97 29
etherealflaim 0:d494b853ce97 30 // Sum them up
etherealflaim 0:d494b853ce97 31 sum += mem[i];
etherealflaim 0:d494b853ce97 32
etherealflaim 0:d494b853ce97 33 //main_log.printf("CK: + 0x%04X = 0x%8X", mem[i], sum);
etherealflaim 0:d494b853ce97 34 }
etherealflaim 0:d494b853ce97 35
etherealflaim 0:d494b853ce97 36 // One's complement of the one's complement sum
etherealflaim 0:d494b853ce97 37 sum += sum >> 16;
etherealflaim 0:d494b853ce97 38 sum &= 0xFFFF;
etherealflaim 0:d494b853ce97 39 sum ^= 0xFFFF;
etherealflaim 0:d494b853ce97 40
etherealflaim 0:d494b853ce97 41 //main_log.printf("CK: ~ 0x%8X", sum);
etherealflaim 0:d494b853ce97 42
etherealflaim 0:d494b853ce97 43 return (u16)(sum);
etherealflaim 0:d494b853ce97 44 }
etherealflaim 0:d494b853ce97 45
etherealflaim 2:e8e09adc41fc 46 /// Generic u16 endian swapping
etherealflaim 0:d494b853ce97 47 inline void fix_endian_u16(u16 *p)
etherealflaim 0:d494b853ce97 48 {
etherealflaim 0:d494b853ce97 49 char *bytes = (char*)p;
etherealflaim 0:d494b853ce97 50 bytes[0] ^= bytes[1];
etherealflaim 0:d494b853ce97 51 bytes[1] ^= bytes[0];
etherealflaim 0:d494b853ce97 52 bytes[0] ^= bytes[1];
etherealflaim 0:d494b853ce97 53 }
etherealflaim 0:d494b853ce97 54
etherealflaim 2:e8e09adc41fc 55 /// Generic u32 endian swapping
etherealflaim 0:d494b853ce97 56 inline void fix_endian_u32(u32 *p)
etherealflaim 0:d494b853ce97 57 {
etherealflaim 0:d494b853ce97 58 char *bytes = (char*)p;
etherealflaim 0:d494b853ce97 59 // Swap outer bytes
etherealflaim 0:d494b853ce97 60 bytes[0] ^= bytes[3];
etherealflaim 0:d494b853ce97 61 bytes[3] ^= bytes[0];
etherealflaim 0:d494b853ce97 62 bytes[0] ^= bytes[3];
etherealflaim 0:d494b853ce97 63 // Swap inner bytes
etherealflaim 0:d494b853ce97 64 bytes[1] ^= bytes[2];
etherealflaim 0:d494b853ce97 65 bytes[2] ^= bytes[1];
etherealflaim 0:d494b853ce97 66 bytes[1] ^= bytes[2];
etherealflaim 0:d494b853ce97 67 }
etherealflaim 0:d494b853ce97 68
etherealflaim 0:d494b853ce97 69 /* Printing character */
etherealflaim 0:d494b853ce97 70 #define PCHAR(x) (isprint(x)?(x):'.')
etherealflaim 0:d494b853ce97 71
etherealflaim 2:e8e09adc41fc 72 /// Hex dump a word-aligned number of bytes (will print extra bytes if length is not a multiple of 32 bits)
etherealflaim 0:d494b853ce97 73 inline void hex_dump(void *base, unsigned int length)
etherealflaim 0:d494b853ce97 74 {
etherealflaim 0:d494b853ce97 75 char line[/*indent*/ 2 + /*0x*/ 2 + /*addr*/ 8 + /* : */ 3 + /*4xXXXX */ 4*5 + /*: */ 2 + /*8x.*/ 8 + /*\0*/ 1];
etherealflaim 0:d494b853ce97 76 for (char *p = (char*)base; p - (char*)base < length; p += 8)
etherealflaim 0:d494b853ce97 77 {
etherealflaim 0:d494b853ce97 78 sprintf(line, " 0x%08X : %02X%02X %02X%02X %02X%02X %02X%02X : %c%c%c%c%c%c%c%c", p,
etherealflaim 0:d494b853ce97 79 p[0], p[1], p[2], p[3], p[4], p[5], p[6], p[7],
etherealflaim 0:d494b853ce97 80 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 81 main_log.printf(line);
etherealflaim 0:d494b853ce97 82 }
etherealflaim 0:d494b853ce97 83 }
etherealflaim 0:d494b853ce97 84
etherealflaim 0:d494b853ce97 85 // Ethernet
etherealflaim 0:d494b853ce97 86 #include "ethernet.h"
etherealflaim 0:d494b853ce97 87
etherealflaim 0:d494b853ce97 88 // ARP and IP
etherealflaim 0:d494b853ce97 89 #include "arp.h"
etherealflaim 0:d494b853ce97 90 #include "ip.h"
etherealflaim 0:d494b853ce97 91
etherealflaim 0:d494b853ce97 92 // TCP, UDP, and ICMP
etherealflaim 0:d494b853ce97 93 #include "tcp.h"
etherealflaim 0:d494b853ce97 94 #include "udp.h"
etherealflaim 0:d494b853ce97 95 #include "icmp.h"
etherealflaim 0:d494b853ce97 96
etherealflaim 0:d494b853ce97 97 #endif // NETWORK_H