test icmp

Dependencies:   mbed

Fork of ethspam by Rolf Meyer

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers net.h Source File

net.h

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