Kyle Lemons / HvZServerLib

Dependencies:   NetServices

Dependents:   HvZ

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 
00079 // Ethernet
00080 #include "ethernet.h"
00081 
00082 // ARP and IP
00083 #include "ip.h"
00084 
00085 // TCP, UDP, and ICMP
00086 #include "udp.h"
00087 
00088 #endif // NETWORK_H