Dependencies:   NetServices

Dependents:   HvZ

Files at this revision

API Documentation at this revision

Comitter:
etherealflaim
Date:
Sun Dec 12 19:34:41 2010 +0000
Commit message:

Changed in this revision

NetServices.lib Show annotated file Show diff for this revision Revisions of this file
net/ethernet.h Show annotated file Show diff for this revision Revisions of this file
net/ip.h Show annotated file Show diff for this revision Revisions of this file
net/net.h Show annotated file Show diff for this revision Revisions of this file
net/udp.h Show annotated file Show diff for this revision Revisions of this file
util/types.h Show annotated file Show diff for this revision Revisions of this file
util/util.h Show annotated file Show diff for this revision Revisions of this file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/NetServices.lib	Sun Dec 12 19:34:41 2010 +0000
@@ -0,0 +1,1 @@
+http://mbed.org/users/simon/code/NetServices/#350011bf8be7
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/net/ethernet.h	Sun Dec 12 19:34:41 2010 +0000
@@ -0,0 +1,49 @@
+#ifndef ETHERNET_H
+#define ETHERNET_H
+
+#include "net.h"
+
+/**
+  \file ethernet.h
+  \brief Ethernet frame header
+  
+  This file contains the memory map and associated functions for Ethernet frame header
+  creation and deconstruction. 
+*/
+
+/// Ethernet MAC address memory map
+typedef struct {
+  unsigned char octet[6]; ///< Individual octsts of the MAC address
+} Ethernet_MAC;
+
+/// Ethernet II Frame Header Memory map
+typedef struct {
+  /// Destination MAC address (6 octets)
+  Ethernet_MAC destination;
+  /// Source MAC address (6 octets)
+  Ethernet_MAC source;
+  // (optional) VLAN Tag (unsupported)
+  /// Ethernet type or length (only <0x600 or 0x0800 IPv4 supported)
+  u16 ethertype;
+  /// Payload (used for memory mapping; has zero size)
+  unsigned char payload[];
+} Ethernet_FrameHeader;
+
+/// Convert from wire to host or host to wire endian-ness
+inline void fix_endian_ethernet(Ethernet_FrameHeader *header)
+{
+  fix_endian_u16(&header->ethertype);
+}
+
+/// Print out an ethernet packet
+/*inline void print_ethernet(Ethernet_FrameHeader *frame)
+{
+  main_log.printf("Ethernet frame:");
+  u8 *src = frame->source.octet;
+  u8 *dst = frame->destination.octet;
+  main_log.printf("  Source:    MAC - %02X:%02X:%02X:%02X:%02X:%02X", src[6], src[7], src[8], src[9], src[10], src[11]);
+  main_log.printf("  Dest:      MAC - %02X:%02X:%02X:%02X:%02X:%02X", dst[0], dst[1], dst[2], dst[3], dst[4], dst[5]);
+  main_log.printf("  Ethertype: 0x%04X", frame->ethertype);
+}
+*/
+#endif
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/net/ip.h	Sun Dec 12 19:34:41 2010 +0000
@@ -0,0 +1,92 @@
+#ifndef IP_H
+#define IP_H
+
+#include "net.h"
+
+/**
+  \file ip.h
+  \brief IP Packet header
+  
+  This file contains the memory map and associated functions for IP packet header
+  creation and deconstruction. 
+*/
+
+#define ETHERTYPE_IPV4 0x0800
+#define ETHERTYPE_IPV6 0x86DD
+
+/// IP Address memory map
+typedef struct {
+  unsigned char octet[4]; ///< Individual address octets
+} IP_Address;
+
+/// IP Packet memory map
+typedef struct {
+  /// 4 bits that contain the version, that specifies if it's an IPv4 or IPv6 packet,
+  unsigned version:4; // Only 0x4 supported
+  /// 4 bits that contain the Internet Header Length which is the length of the header in multiples of 4 bytes (eg. 5 means 20 bytes).
+  unsigned header_bytes_div4:4;
+  /// 8 bits that contain the Type of Service, also referred to as Quality of Service (QoS), which describes what priority the packet should have,
+  unsigned tos:8;
+  /// 16 bits that contain the total length of the IP packet (datagram) in bytes,
+  u16 packet_bytes;
+  /// 16 bits that contain an identification tag to help reconstruct the packet from several fragments,
+  u16 fragment_id;
+  /// 3 bits that contain a zero, a flag that says whether the packet is allowed to be fragmented or not (DF: Don't fragment), and a flag to state whether more fragments of a packet follow (MF: More Fragments)
+  unsigned unused_0:1;
+  unsigned dont_fragment:1;
+  unsigned more_follow:1;
+  /// 13 bits that contain the fragment offset, a field to identify position of fragment within original packet
+  unsigned fragment_offset:13; ///< This and the ones above may not work properly due to endianness
+  /// 8 bits that contain the Time to live (TTL) which is the number of hops (router, computer or device along a network) the packet is allowed to pass before it dies (for example, a packet with a TTL of 16 will be allowed to go across 16 routers to get to its destination before it is discarded),
+  unsigned ttl:8;
+  /// 8 bits that contain the protocol (TCP, UDP, ICMP, etc...)
+  ///   0x01 ICMP
+  ///   0x06 TCP
+  ///   0x11 UDP
+  unsigned protocol:8;
+  /// 16 bits that contain the Header Checksum, a number used in error detection,
+  u16 header_checksum;
+  /// 32 bits that contain the source IP address,
+  IP_Address source;
+  /// 32 bits that contain the destination address.
+  IP_Address destination;
+  /// Zero-length field for memory mapping the packet data
+  unsigned char data[];
+} IP_PacketHeader;
+
+/// Convert from wire to host or host to wire endianness
+inline void fix_endian_ip(IP_PacketHeader *packet)
+{
+  packet->version ^= packet->header_bytes_div4;
+  packet->header_bytes_div4 ^= packet->version;
+  packet->version ^= packet->header_bytes_div4;
+  fix_endian_u16(&packet->packet_bytes);
+  fix_endian_u16(&packet->fragment_id);
+  // Don't fix checksums; they are done bitwise
+}
+
+/// Get a constant string of the given IP protocol (e.g. "ICMP", "TCP", "UDP") if known
+inline const char *ipproto2name(u8 proto)
+{
+  switch (proto)
+  {
+    case 0x01: return "ICMP";
+    case 0x02: return "IGMP";
+    case 0x06: return "TCP";
+    case 0x11: return "UDP";
+  }
+  return "<UNKNOWN>";
+}
+
+/// Parse the string (in decimal triple-dot notation) into the IP address structure
+inline void str2ipaddr(const char *ip, IP_Address *addr)
+{
+  static short a,b,c,d;
+  sscanf(ip, "%3hd.%3hd.%3hd.%3hd", &a, &b, &c, &d);
+  addr->octet[0] = a;
+  addr->octet[1] = b;
+  addr->octet[2] = c;
+  addr->octet[3] = d;
+}
+
+#endif
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/net/net.h	Sun Dec 12 19:34:41 2010 +0000
@@ -0,0 +1,88 @@
+/**
+ @file net.h
+ @brief This file encompasses all of the networking headers and includes them automatically
+ 
+ This file has some utility functions and definitions used by all of the networking headers,
+ and includes them all.  This is the only file necessary to include to use all of the networking
+ facilities in nettool
+*/
+
+#ifndef NETWORK_H
+#define NETWORK_H
+
+#include "mbed.h"
+
+#include "ctype.h"
+
+/// General networking checksum - Used for IP, TCP, UDP, ICMP, etc.
+/// Computes the one's complement of the one's complement sum of all of the given bytes except for the memory
+/// at skip_byte for skip_count bytes (e.g. the checksum).  Optionally resumes computation from the (given) last checksum.
+inline u16 checksum(void *_mem, unsigned int bytes, void *skip_byte = NULL, unsigned int skip_count = 0, u16 last = 0)
+{
+  u32 sum = 0;
+  u16 *mem = (u16*)_mem;
+  unsigned int skip_start = (u16*)skip_byte - mem;
+  
+  if (last)
+    sum = last ^ 0xFFFF;
+  
+  //main_log.printf("CK:            0x%8X", sum);
+  for (register unsigned int i = 0; i < bytes/sizeof(u16); ++i)
+  {
+    // Skip bytes we don't use (e.g. the checksum itself)
+    if (i == skip_start)
+      i += skip_count/sizeof(u16);
+    
+    // Sum them up
+    sum += mem[i];
+    
+    //main_log.printf("CK: + 0x%04X = 0x%8X", mem[i], sum);
+  }
+    
+  // One's complement of the one's complement sum
+  sum += sum >> 16;
+  sum &= 0xFFFF;
+  sum ^= 0xFFFF;
+  
+  //main_log.printf("CK:          ~ 0x%8X", sum);
+  
+  return (u16)(sum);
+}
+
+/// Generic u16 endian swapping
+inline void fix_endian_u16(u16 *p)
+{
+  char *bytes = (char*)p;
+  bytes[0] ^= bytes[1];
+  bytes[1] ^= bytes[0];
+  bytes[0] ^= bytes[1];
+}
+
+/// Generic u32 endian swapping
+inline void fix_endian_u32(u32 *p)
+{
+  char *bytes = (char*)p;
+  // Swap outer bytes
+  bytes[0] ^= bytes[3];
+  bytes[3] ^= bytes[0];
+  bytes[0] ^= bytes[3];
+  // Swap inner bytes
+  bytes[1] ^= bytes[2];
+  bytes[2] ^= bytes[1];
+  bytes[1] ^= bytes[2];
+}
+
+/* Printing character */
+#define PCHAR(x) (isprint(x)?(x):'.')
+
+
+// Ethernet
+#include "ethernet.h"
+
+// ARP and IP
+#include "ip.h"
+
+// TCP, UDP, and ICMP
+#include "udp.h"
+
+#endif // NETWORK_H
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/net/udp.h	Sun Dec 12 19:34:41 2010 +0000
@@ -0,0 +1,35 @@
+#ifndef UDP_H
+#define UDP_H
+
+#include "net.h"
+
+/**
+  \file udp.h
+  \brief UDP packet
+  
+  This file contains the memory map and associated functions for UDP packet
+  creation and deconstruction. 
+*/
+
+#define IPPROTO_UDP 0x11
+#define HVZ_PORT 4489
+#define HVZ_HOSTNAME "kylelemons.net"
+
+/// UDP Packet memory map
+typedef struct {
+  u16 source_port;       ///< Source port (1-65535)
+  u16 destination_port;  ///< Destination port (1-65535) 
+  u16 length;            ///< Entire datagram size in bytes
+  u16 checksum;          ///< Checksum
+  u8 data[];             ///< Data memory map
+} UDP_Packet;
+
+/// Convert from wire to host or host to wire endianness
+inline void fix_endian_udp(UDP_Packet *segment)
+{
+  fix_endian_u16(&segment->source_port);
+  fix_endian_u16(&segment->destination_port);
+  fix_endian_u16(&segment->length);
+}
+
+#endif
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/util/types.h	Sun Dec 12 19:34:41 2010 +0000
@@ -0,0 +1,20 @@
+#ifndef TYPES_H
+#define TYPES_H
+
+/**
+  \file types.h
+  \brief Type definitions
+  
+  This file contains some utility type definitions
+*/
+
+/// Unsigned 8 bit value
+typedef unsigned char u8;
+
+/// Unsigned 16 bit value
+typedef unsigned short u16;
+
+/// Unsigned 32 bit value
+typedef unsigned int u32;
+
+#endif
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/util/util.h	Sun Dec 12 19:34:41 2010 +0000
@@ -0,0 +1,36 @@
+#ifndef UTIL_H
+#define UTIL_H
+
+#include "types.h"
+//#include "log.h"
+
+/**
+  \file util.h
+  \brief Primary utility header
+  
+  In addition to providing some utility functions, this file also includes
+  the other utility headers automatically.
+*/
+
+/// Is any byte memory at start for bytes nonzero?
+inline bool is_nonzero_mem(u8 *start, unsigned int bytes)
+{
+  for (; bytes--; ++start) if (*start) return true;
+  return false;
+}
+
+/// Are all bytes at start for bytes zero?
+inline bool is_zero_mem(u8 *start, unsigned int bytes)
+{
+  for (; bytes--; ++start) if (*start) return false;
+  return true;
+}
+
+/// Are the memory locations at and and b equal for bytes?
+inline bool is_equal_mem(u8 *a, u8 *b, unsigned int bytes)
+{
+  for (; bytes--; ++a, ++b) if (*a != *b) return false;
+  return true;
+}
+
+#endif
\ No newline at end of file