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:48:59 2010 +0000
Revision:
8:1c1f6ce348c6
Parent:
5:c56386b9fc33
Documentation

Who changed what in which revision?

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