Class oethernet
Introduction
This class implements an Ethernet header encoder/decoder. When instantiated, it provides access to Ethernet header source, destination and protocol fields in binary and string format. Class methods may be used to encode/decode or import/export a complete Ethernet header or individual header fields that are stored in external memory in network byte order. This is the base class for the ohomeplug and ointellon classes.
Encode and export methods write class properties to external memory in network byte order. Decode and import methods read class properties from external memory in network byte order. Encode and decode methods always return the next unencoded or undecoded memory byte. Import and Export methods always return the object instance reference allowing class methods to be chained.
The Protocol property and SetProtocol method perform implicit integer host-to-network and network-to-host byte ordering.
This class is declared in oethernet.hpp and defined in oethernet.cpp.
Inheritance
None.
Dependence
This class references static class omemory to manipulate memory.
This class references Ethernet related constants defined in header file net/ethernet.h which should be available on most systems. An abridged version is provided in VisualStudioNET/include/net/ethernet.h for systems that may not have it.
Properties
oethernet::BroadcastAddress
const uint8_t * BroadcastAddress
Return the standard Ethernet broadcast address as a constant 48-bit integer in network byte order. The value is FF:FF:FF:FF:FF:FF and the length is ETHER_ADDR_LEN bytes. Although shown here as a property, this is implemented as a constant. Parentheses are omitted when referencing it.
oethernet::HeaderLength
unsigned HeaderLengthvoid
Return the Ethernet header length in bytes. The header length is the sum of the Ethernet PeerAddress, HostAddress and Protocol lengths. For practical purposes, this property is a constant but should the VLAN tag be added then the length will change. Use this property to allocate buffer space in application programs.
oethernet::HostAddress
const uint8_t * HostAddressvoid
Return the host hardware address as a byte array in network byte order. The length is ETHER_ADDR_LEN bytes. The host hardware address is the same as the OSA field described in the HomePlug AV Specification and Firmware Technical Reference Manual. On instantiation, this property contains zeros.
oethernet::HostAddressString
const char * HostAddressStringvoid
Return the host hardware address as a string. The length is ETHER_ADDR_LEN*3 characters long including the NUL terminator. It may be used anywhere a NUL terminated string is expected. The string is formatted as a colon-separated, hexadecimal octets, as in 00:B0:52:BA:BE:15.
oethernet::PeerAddress
const uint8_t * PeerAddressvoid
Return the peer hardware address as a byte array in network byte order. The length is in ETHER_ADDR_LEN bytes. The peer hardware address is the same as the ODA field described in the HomePlug AV Specification and Firmware Technical Reference Manual. On instantiation, this property contains zeros.
oethernet::PeerAddressString
const char * PeerAddressStringvoid
Return the peer address as a string. The length is ETHER_ADDR_LEN*3 characters long including the NUL terminator. It may be used anywhere a NUL terminated string is expected. The string is formatted as a colon-separated, hexadecimal octets, as in 00:B0:52:DE:AD:99.
oethernet::Protocol
uint16_t Protocolvoid
Return the Ethernet protocol as a 16-bit integer in host byte order. The Ethernet protocol is the same as the MTYPE field described in the HomePlug AV Specification and Firmware Technical Reference Manual. On instantiation, this property is 0x0000.
oethernet::ProtocolString
char const * ProtocolStringvoid
Return the Ethernet protocol as a string. The length is ETHER_TYPE_LEN*3 characters long including the NUL terminator. It is formatted as colon-seperated, hexadecimal octets, as in 88:E1.
Methods
oethernet::ExportHeader
void * ExportHeadervoid * memory
Encode external memory with an Ethernet header and return the address of the next unencoded memory byte. No length argument is required. The number of bytes encoded will be HeaderLength. The memory address is incremented by that amount and returned as the method value for use in subsequent export operations. The encoded header will include the PeerAddress, HostAddress and Protocol. Instance properties are not modified by this operation.
oethernet::ExportHostAddress
void * EncodeHostAddressvoid * memory
Copy the host hardware address to external memory in binary format. Return the address of the next uncopied memory byte. No length argument is required. The host hardware address is ETHER_ADDR_LEN bytes. The memory address is incremented by that amount and returned as the method value for use in subsequent export operations.
oethernet::ExportPeerAddress
void * ExportPeerAddressvoid * memory
Copy the peer hardware address to external memory in binary format. Return the address of the next uncopied memory byte. No length argument is required. The peer hardware address is ETHER_ADDR_LEN bytes. The memory address is incremented by that amount and returned as the method value for use in subsequent export operations.
oethernet::ExportProtocol
void * ExportProtocolvoid * memory
Copy the Ethernet protocol to external memory in network byte order. Return the address of the next uncopied byte. No length argument is required. The Ethernet protocol is ETHER_TYPE_LEN bytes. The memory address is incremented by that amount and returned as the method value for use in subsequent export operations.
oethernet::ImportHeader
void const * ImportHeadervoid const * memory
Decode external memory containing an Ethernet header. Return the address of the next undecoded memory byte. No length argument is needed. The number of bytes decoded will be HeaderLength. The memory address is incremented by that amount and returned as the method value for use in subsequent import operations. External memory is unpacked and stored in the PeerAddress, HostAddress and Protocol properties. External memory is not modified by this operation.
oethernet::ImportHostAddress
void const * ImportHostAddressvoid const * memory
Copy the host hardware address from external memory in binary format. Return the address of the next uncopied memory byte. No length argument is needed. The host hardware address is ETHER_ADDR_LEN bytes. The memory address is incremented by that amount and returned as the method value for use in subsequent import operations.
oethernet::ImportPeerAddress
void const * ImportPeerAddressvoid const * memory
Copy the peer hardware address from external memory in binary format. Return the address of the next uncopied memory byte. No length argument is required. The host hardware address is ETHER_ADDR_LEN bytes. The memory address is incremented by that amount and returned as the method value for use in subsequent import operations.
oethernet::ImportProtocol
void const * ImportProtocolvoid * memory
Copy the Ethernet protocol from external memory in binary format. Return the address of the next uncopied memory byte. No length argument is required. The Ethernet protocol is ETHER_TYPE_LEN bytes. The memory address is incremented by that amount and returned as the method value for use in subsequent import operations.
oethernet::Print
oethernet & Printvoid
Print the Ethernet header on stdout in human-friendly format.
oethernet::SetProtocol
oethernet & SetProtocoluint16_t protocol
Specify the Ethernet protocol as an integer in host byte order. The protocol is stored internally in network byte order.
Constructors
oethernetvoid
Initialize all properties to 0.
oethernetuint16_t protocol
Initialize the PeerAddress and HostAddress properties to 0 and the Protocol property to protocol.
Examples
There are several ways to use this class. One way is to set class properties and then export them to external memory. The other way is to import class properties from external memory and inspect them.
Encoding an Ethernet Header
oethernet header;
byte hostaddress [] = { 0x00, 0xC7, 0x43, 0xDE, 0xAD, 0x02 }
byte framebuffer [ETHER_MAX_LEN];
byte framepointer;
header.ImportHostAddress (oethernet::BroadcastAddress);
header.ImportPeerAddress (hostaddress);
framepointer = header.ExportHeader (framebuffer);
This example instantiates an oethernet header and allocates a hostaddress buffer and an empty Ethernet framebuffer. It then invokes the class ImportHostAddress method to copy class constant Broadcast into the header source address property and the ImportPeerAddress method to copy the hostaddress value into the header destination address property. Finally, it invokes the ExportHeader method to encode the framebuffer with a complete Ethernet header. The framepointer now points to the next unencode framebuffer address which is the start of the Ethernet frame body.
Decoding an Ethernet Header
oethernet header;
byte framebuffer [ETHER_MAX_LEN];
...
(fill the frame buffer with a valid Ethernet frame)
...
header.Print ();
header.ImportHeader (framebuffer);
header.Print();
This example instantiates an oethernet header and allocates a framebuffer. Assuming that framebuffer contains a valid Ethernet frame from somewhere, it invokes the class Print method to print the contents of header then invokes the ImportHeader methoc to load the header portion of framebuffer in framebuffer then invokes the Print method again. This gives us a before and after picture of header content which illustrates frame decode functionality.