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.h
00001 /** 00002 @file net.h 00003 @brief This file encompasses all of the networking headers and includes them automatically 00004 00005 This file has some utility functions and definitions used by all of the networking headers, 00006 and includes them all. This is the only file necessary to include to use all of the networking 00007 facilities in nettool 00008 */ 00009 00010 #ifndef NETWORK_H 00011 #define NETWORK_H 00012 00013 #include "mbed.h" 00014 #include "main.h" 00015 00016 #include "ctype.h" 00017 00018 /// General networking checksum - Used for IP, TCP, UDP, ICMP, etc. 00019 /// Computes the one's complement of the one's complement sum of all of the given bytes except for the memory 00020 /// at skip_byte for skip_count bytes (e.g. the checksum). Optionally resumes computation from the (given) last checksum. 00021 inline u16 checksum(void *_mem, unsigned int bytes, void *skip_byte = NULL, unsigned int skip_count = 0, u16 last = 0) 00022 { 00023 u32 sum = 0; 00024 u16 *mem = (u16*)_mem; 00025 unsigned int skip_start = (u16*)skip_byte - mem; 00026 00027 if (last) 00028 sum = last ^ 0xFFFF; 00029 00030 //main_log.printf("CK: 0x%8X", sum); 00031 for (register unsigned int i = 0; i < bytes/sizeof(u16); ++i) 00032 { 00033 // Skip bytes we don't use (e.g. the checksum itself) 00034 if (i == skip_start) 00035 i += skip_count/sizeof(u16); 00036 00037 // Sum them up 00038 sum += mem[i]; 00039 00040 //main_log.printf("CK: + 0x%04X = 0x%8X", mem[i], sum); 00041 } 00042 00043 // One's complement of the one's complement sum 00044 sum += sum >> 16; 00045 sum &= 0xFFFF; 00046 sum ^= 0xFFFF; 00047 00048 //main_log.printf("CK: ~ 0x%8X", sum); 00049 00050 return (u16)(sum); 00051 } 00052 00053 /// Generic u16 endian swapping 00054 inline void fix_endian_u16(u16 *p) 00055 { 00056 char *bytes = (char*)p; 00057 bytes[0] ^= bytes[1]; 00058 bytes[1] ^= bytes[0]; 00059 bytes[0] ^= bytes[1]; 00060 } 00061 00062 /// Generic u32 endian swapping 00063 inline void fix_endian_u32(u32 *p) 00064 { 00065 char *bytes = (char*)p; 00066 // Swap outer bytes 00067 bytes[0] ^= bytes[3]; 00068 bytes[3] ^= bytes[0]; 00069 bytes[0] ^= bytes[3]; 00070 // Swap inner bytes 00071 bytes[1] ^= bytes[2]; 00072 bytes[2] ^= bytes[1]; 00073 bytes[1] ^= bytes[2]; 00074 } 00075 00076 /* Printing character */ 00077 #define PCHAR(x) (isprint(x)?(x):'.') 00078 00079 /// Hex dump a word-aligned number of bytes (will print extra bytes if length is not a multiple of 32 bits) 00080 inline void hex_dump(void *base, unsigned int length) 00081 { 00082 char line[/*indent*/ 2 + /*0x*/ 2 + /*addr*/ 8 + /* : */ 3 + /*4xXXXX */ 4*5 + /*: */ 2 + /*8x.*/ 8 + /*\0*/ 1]; 00083 for (char *p = (char*)base; p - (char*)base < length; p += 8) 00084 { 00085 sprintf(line, " 0x%08X : %02X%02X %02X%02X %02X%02X %02X%02X : %c%c%c%c%c%c%c%c", p, 00086 p[0], p[1], p[2], p[3], p[4], p[5], p[6], p[7], 00087 PCHAR(p[0]), PCHAR(p[1]), PCHAR(p[2]), PCHAR(p[3]), PCHAR(p[4]), PCHAR(p[5]), PCHAR(p[6]), PCHAR(p[7])); 00088 main_log.printf(line); 00089 } 00090 } 00091 00092 // Ethernet 00093 #include "ethernet.h" 00094 00095 // ARP and IP 00096 #include "arp.h" 00097 #include "ip.h" 00098 00099 // TCP, UDP, and ICMP 00100 #include "tcp.h" 00101 #include "udp.h" 00102 #include "icmp.h" 00103 00104 #endif // NETWORK_H
Generated on Tue Jul 12 2022 11:59:40 by 1.7.2