NetBase

Files CCore/inc/net/NetBase.h CCore/src/net/NetBase.cpp

The header net/NetBase.h contains a lot of definitions, related with Ethernet and IP protocols.

Types and constants

UDPort is a UDP port type, it is a 16-bit unsigned integral type.


using UDPort = uint16 ;

IPLen represents IP payload length, it is a 16-bit unsigned integral type.


using IPLen = uint16 ;

The following constants represent different protocol lengths.


const ulen EthHeaderLen = 14 ;

const ulen IPHeaderLen  = 20 ;

const ulen ARPacketLen  = 28 ;

const ulen ICMPEchoLen  =  8 ;

const ulen UDPHeaderLen =  8 ;

const ulen MaxEthFrameLen = 1514 ;

const ulen MaxEthDataLen  = MaxEthFrameLen - EthHeaderLen ; // 1500

const ulen MaxIPDataLen   = MaxEthDataLen - IPHeaderLen ;   // 1480

const ulen MaxUDPDataLen  = MaxIPDataLen - UDPHeaderLen ;   // 1472

struct EthType

EthType represents an Ethernet packet type.


enum KnownEthType : uint16
 {
  Eth_IP  = 0x800,
  Eth_ARP = 0x806
 };

struct EthType
 {
  uint16 type;
  
  // constructors
  
  EthType() : type() {}
  
  EthType(KnownEthType type_) : type(type_) {}
  
  // methods
  
  uint16 get() const { return type; }
  
  // save/load object
  
  enum { SaveLoadLen = 2 };
 
  template <class Dev>
  void save(Dev &dev) const;
   
  template <class Dev>
  void load(Dev &dev);
  
  // print object
  
  template <class P>
  void print(P &out) const;
 };

Default constructor creates a null value type.

The second implicit constructor creates the "known" type from the enumeration.

get() returns an uint16 representation of the type.

The class may participate in save/load operations. It is also printable in the verbose form.

struct IProto

IProto represents an IP protocol number.


enum KnownIProto : uint8
 {
  IP_ICMP =  1,
  IP_UDP  = 17
 };

struct IProto 
 {
  uint8 proto;
  
  // constructors
  
  IProto() : proto() {}
  
  IProto(KnownIProto proto_) : proto(proto_) {}
  
  // methods
  
  uint8 get() const { return proto; }
  
  // save/load object
  
  enum { SaveLoadLen = 1 };
 
  template <class Dev>
  void save(Dev &dev) const;
   
  template <class Dev>
  void load(Dev &dev);
  
  // print object
   
  template <class P>
  void print(P &out) const;
 };

Default constructor creates a null value number.

The second implicit constructor creates the "known" number from the enumeration.

get() returns an uint8 representation of the number.

The class may participate in save/load operations. It is also printable in the verbose form.

struct ARPType

ARPType represents an ARP packet type.


enum KnownARPType : uint16
 {
  ARP_Request = 1,
  ARP_Reply   = 2
 };

struct ARPType
 {
  uint16 type;
  
  // constructors
  
  ARPType() : type() {}
  
  ARPType(KnownARPType type_) : type(type_) {}
  
  // methods
  
  uint16 get() const { return type; }
  
  // save/load object
  
  enum { SaveLoadLen = 2 };
 
  template <class Dev>
  void save(Dev &dev) const;
   
  template <class Dev>
  void load(Dev &dev);
  
  // print object
   
  template <class P>
  void print(P &out) const;
 };

Default constructor creates a null value type.

The second implicit constructor creates the "known" type from the enumeration.

get() returns an uint16 representation of the type.

The class may participate in save/load operations. It is also printable in the verbose form.

struct ICMPEchoType

ICMPEchoType represents an ICMP Echo packet type.


enum KnownICMPEchoType : uint8
 {
  ICMPEcho_Request = 8,
  ICMPEcho_Reply   = 0
 };

struct ICMPEchoType
 {
  uint8 type;
  
  // constructors
  
  ICMPEchoType() : type() {}
  
  ICMPEchoType(KnownICMPEchoType type_) : type(type_) {}
  
  // methods
  
  uint8 get() const { return type; }
  
  // save/load object
  
  enum { SaveLoadLen = 1 };
 
  template <class Dev>
  void save(Dev &dev) const;
   
  template <class Dev>
  void load(Dev &dev);
  
  // print object
   
  template <class P>
  void print(P &out) const;
 };

Default constructor creates a null value type.

The second implicit constructor creates the "known" type from the enumeration.

get() returns an uint8 representation of the type.

The class may participate in save/load operations. It is also printable in the verbose form.

struct MACAddress

MACAddress represents a MAC address (48 bit).


struct MACAddress
 {
  uint8 address[6];
  
  // constructors
  
  MACAddress() : address() {}
  
  MACAddress(uint8 b0,uint8 b1,uint8 b2,uint8 b3,uint8 b4,uint8 b5);
  
  explicit MACAddress(XPoint point);
  
  // methods
   
  static MACAddress Broadcast() { return MACAddress(0xFF,0xFF,0xFF,0xFF,0xFF,0xFF); }
  
  XPoint get() const; 
  
  // save/load object
  
  enum { SaveLoadLen = 6 };
 
  template <class Dev>
  void save(Dev &dev) const;
   
  template <class Dev>
  void load(Dev &dev);
  
  // print object
  
  template <class P>
  void print(P &out) const;
 };

Default constructor creates a null address.

The second constructor creates the address from six byte components.

Broadcast() returns a broadcast MAC address.

get() returns an XPoint representation of the address. You may convert it back using the constructor with the XPoint argument.

The class may participate in save/load operations. It is also printable in the standard MAC address form.

struct IPAddress

IPAddress represents an IPv4 address (32 bit).


struct IPAddress
 {
  uint32 address;
  
  // constructors
  
  IPAddress() : address() {}
   
  IPAddress(uint8 b0,uint8 b1,uint8 b2,uint8 b3);
  
  explicit IPAddress(uint32 address_) { address=address_; }
  
  // methods
   
  uint32 get() const { return address; } 
  
  friend bool operator == (IPAddress a,IPAddress b);

  friend bool operator != (IPAddress a,IPAddress b);
  
  IPAddress getNet(IPAddress net_mask) const { return IPAddress(address&net_mask.address); }
  
  bool sameNet(IPAddress obj,IPAddress net_mask) const { return getNet(net_mask)==obj.getNet(net_mask); }
  
  // save/load object
   
  enum { SaveLoadLen = 4 };
 
  template <class Dev>
  void save(Dev &dev) const;
   
  template <class Dev>
  void load(Dev &dev);
  
  // print object
  
  template <class P>
  void print(P &out) const;

  // scan object

  template <class S>
  void scan(S &inp);
 };

Default constructor creates a null address.

The second constructor creates the address from four byte components.

get() returns an uint32 representation of the address. You may convert it back using the constructor with the uint32 argument.

You can use operator = and operator != to compare IP addresses.

getNet() masks IP address to extract the net address part.

sameNet() can be used to compare net address parts.

The class may participate in save/load operations. It is also printable and scannable in the standard IP address dot form.


template <class Dev>
void ParseIPAddress(Dev &dev,IPAddress &ret);

The function ParseIPAddress() can be used to parse an IP address from a character stream. The expected string looks like 192.168.1.1 .

struct EthHeader

EthHeader represents Ethernet packet header.


struct EthHeader
 {
  MACAddress dst;
  MACAddress src;
  EthType type;
  
  // constructors
  
  EthHeader() : dst(),src(),type() {}
  
  EthHeader(MACAddress src,MACAddress dst,EthType type);
  
  // save/load object
  
  enum { SaveLoadLen = 14 };
 
  template <class Dev>
  void save(Dev &dev) const;
   
  template <class Dev>
  void load(Dev &dev);
  
  // print object
   
  struct PrintOptType
   {
    static const bool Default_inbound = false ;
    
    bool inbound;
   
    void setDefault() { inbound=Default_inbound; }
    
    PrintOptType() { setDefault(); }
    
    PrintOptType(const char *ptr,const char *lim);
    
    //
    // [i|o|I|O]
    //
    
    template <class Dev>
    static void ParseInbound(Dev &dev,bool &inbound);
   }; 
   
  template <class P>
  void print(P &out,PrintOptType opt) const;
 };

Default constructor sets all fields to null.

The second constructor creates a header with given source and destination MAC addresses and the Ethernet packet type.

The class may participate in save/load operations. It is also printable. It accepts printing options: "i" or "I" to use the inbound format, "o" or "O" to use the outbound format.

struct CheckSum

CheckSum is an IP check sum accumulator.


struct CheckSum 
 {
  ....
  
  // constructors
  
  CheckSum();
  
  // add
  
  void add(uint8 b1,uint8 b2);
   
  void add(uint16 word); 
   
  void add(uint32 dword); 
   
  void add(PtrLen<const uint8> data);
  
  // (TT)
   
  void operator () () {}
  
  template <class ... TT>
  void operator () (uint8 b0,uint8 b1,TT... tt);
   
  template <class ... TT>
  void operator () (uint16 word,TT... tt);
   
  template <class ... TT>
  void operator () (uint32 dword,TT... tt);
  
  // complete 
   
  uint16 complete() const;
   
  bool test() const;
 };

The default constructor creates an object in the initial null state. To calculate a check sum it must be fed with data elements. Data elements can be a couple of uint8, an uint16 or uint32 value. You may use add() methods of operator () methods. To accumulate a tail use the add() method. The tail range may have odd number of elements.

complete() completes accumulate and return the check sum.

test() tests the check sum. For this method the check sum field must be included in the accumulation process.

struct IPHeader

IPHeader represents base IPv4 header (20 bytes). This class is design to support a simplified IP implementation, so some fields are expected to have some default values.


struct IPHeader
 {
  // consts
  
  static const uint8  DefaultVHL         = 0x45 ;
  static const uint8  DefaultTOS         = 0    ;
  static const uint16 DefaultFlagsOffset = 0    ;
  static const uint8  DefaultTTL         = 128  ;
  
  static const uint16 Flag_DF    = 1u<<14     ; // don't fragment
  static const uint16 Flag_MF    = 1u<<13     ; // more fragments
  static const uint16 OffsetMask = (1u<<13)-1 ;
  
  // data
 
  uint8 vhl; // default
  uint8 tos; // default
  IPLen len;
  uint16 id;
  
  uint16 flags_offset; // default
  uint8  ttl;          // default
  IProto proto;
  uint16 check_sum;
  
  IPAddress src;
  IPAddress dst;
  
  // constructors
  
  IPHeader();
    
  IPHeader(IPAddress src,IPAddress dst,IProto proto,IPLen len);
   
  // methods
  
  void setDefault();
   
  static uint16 GetId(); 
   
  void setId() { id=GetId(); }
   
  void setCheckSum(); 
  
  bool testCheckSum() const;
  
  bool isFragment() const;
  
  // save/load object
 
  enum { SaveLoadLen = 20 };
 
  template <class Dev>
  void save(Dev &dev) const;
   
  template <class Dev>
  void load(Dev &dev);
  
  // print object
   
  struct PrintOptType
   {
    static const bool Default_inbound = false ;
    static const bool Default_extra = false ;
    
    bool inbound;
    bool extra;
   
    void setDefault() { inbound=Default_inbound; extra=Default_extra; }
    
    PrintOptType() { setDefault(); }
    
    PrintOptType(const char *ptr,const char *lim);
    
    //
    // [i|o|I|O][x|X]
    //
    
    template <class Dev>
    static void ParseInbound(Dev &dev,bool &inbound);
     
    template <class Dev>
    void ParseExtra(Dev &dev,bool &extra);
   }; 
   
  template <class P>
  void print(P &out,PrintOptType opt) const;
 };

The default constructor sets default fields to its default values and other fields to null.

The second constructor sets default fields to its default values, but other fields sets to its proper values, i.e. this constructor creates a proper IP header with given fields src, dst, len and proto.

setDefault() sets default fields to its default values.

GetId() generates a "unique" packet header id.

setId() sets the packet header id.

setCheckSum() sets the check sum of the header. The header must have the basic length 20 bytes.

testCheckSum() tests the check sum of the header. The header must have the basic length 20 bytes.

isFragment() returns true, if the packet is a fragment.

The class may participate in save/load operations. It is also printable. The two printing options are supported: "i","I" or "o","O" controls inbound or outbound formatting of the text. You may also specify "x","X" options to print extra information. In the short form only four fields are printed: src, dst, len and proto.

struct ARPacket

ARPacket represents ARP packet. We implement only IP/MAC ARP. It means that some ARP packet fields have its defined value for this particular case.


struct ARPacket
 {
  // consts
  
  static const uint16 DefaultMACType = 1                       ;
  static const uint16 DefaultIPType  = Eth_IP                  ;
  static const uint8  DefaultMACLen  = MACAddress::SaveLoadLen ;
  static const uint8  DefaultIPLen   = IPAddress::SaveLoadLen  ;
  
  // data
 
  uint16 mac_type; // default
  uint16 ip_type;  // default
  uint8  mac_len;  // default
  uint8  ip_len;   // default
  
  ARPType type;

  MACAddress src_mac;
  IPAddress  src_ip;
  MACAddress dst_mac;
  IPAddress  dst_ip;
  
  // constructors
  
  ARPacket(); 
  
  ARPacket(ARPType type,MACAddress src_mac,IPAddress src_ip,MACAddress dst_mac,IPAddress dst_ip);
  
  // methods
  
  void setDefault();
  
  bool testDefault() const;
  
  // save/load object
  
  enum { SaveLoadLen = 28 };
 
  template <class Dev>
  void save(Dev &dev) const;
   
  template <class Dev>
  void load(Dev &dev);
 };

Both constructors set "default fields" to default values. Default constructor sets all other fields to null.

setDefault() sets "default fields" to default values.

testDefault() checks, if "default fields" have default values.

The class may participate in save/load operations.

struct ICMPEcho

ICMPEcho represents ICMP Echo packet header.


struct ICMPEcho 
 {
  ICMPEchoType type;
  uint8 code;
  uint16 check_sum;
  uint16 id;
  uint16 num;
  
  // constructors
  
  ICMPEcho();
  
  ICMPEcho(ICMPEchoType type,uint16 id,uint16 num);
  
  // methods
  
  void setCheckSum(PtrLen<const uint8> data);
  
  bool testCheckSum(PtrLen<const uint8> data) const;
  
  // save/load object
  
  enum { SaveLoadLen = 8 };
 
  template <class Dev>
  void save(Dev &dev) const;
   
  template <class Dev>
  void load(Dev &dev);
   
  // print object
  
  template <class P>
  void print(P &out) const;
 };

Default constructor sets all fields to null.

The second constructor sets code and check sum to null.

setCheckSum() sets the check sum, data is the ICMP data payload.

testCheckSum() checks the check sum, data is the ICMP data payload.

The class may participate in save/load operations. It is also printable.

struct UDPHeader

UDPHeader represents a UDP packet header.


struct UDPHeader 
 {
  UDPort src_port;
  UDPort dst_port;
  IPLen len;
  uint16 check_sum;
  
  // constructors
  
  UDPHeader();
   
  UDPHeader(UDPort src_port,UDPort dst_port,IPLen len);
  
  // methods
  
  void setCheckSum(IPAddress src,IPAddress dst,IPLen len,PtrLen<const uint8> data);
  
  bool testCheckSum(IPAddress src,IPAddress dst,IPLen len,PtrLen<const uint8> data) const;
  
  // save/load object
  
  enum { SaveLoadLen = 8 };
 
  template <class Dev>
  void save(Dev &dev) const;
   
  template <class Dev>
  void load(Dev &dev);
 };

Default constructor sets all fields to null.

The second constructor creates a header with given ports and the IP payload length (which includes UDP header). It sets check sum to zero.

setCheckSum() sets the check sum. The len is the IP payload length, data is the UDP payload.

testCheckSum() test the check sum.

The class may participate in save/load operations.