#include "..\Include\sys.h"

#define NETWORK_TRANMODE    H_TO_L

struct tIpAddress = String
{
    UINT1 Address[4];
};

attribute name='tIpAddress'
{
    String Integrate(String strCurIdent)   
    {
        return strFormat("%d.%d.%d.%d",this.Address[0],this.Address[1],this.Address[2],this.Address[3]);
    }
};

//////////////////////////////////////////////////////////
struct tETHERNET
{
    OCTET   DestinationAddr {
                void Translate(String strCurIdent,dynamic& curItem,long nIdxInArr)
                {
                    sysFetchValue(strCurIdent,curItem,now(),6, true);  // FF FF FF FF FF FF  mean broadcast
                }
            }; 
    OCTET   SourceAddr {
                void Translate(String strCurIdent,dynamic& curItem,long nIdxInArr)
                {
                    sysFetchValue(strCurIdent,curItem,now(),6, true);
                }
            }; 
    WORD    Protocol{
                enum description {
                 
                    0x0800,   // IP (Internet Protocol)
                };
                dumpOF description (Equal)
            };
};

struct tTOS 
{
    UINT1   Precedence:3;   // Low 0 to High 7
    UINT1   Delay:1 {       
                enum description {
                 
                    0,   // Normal
                    1,   // High
                };
                dumpOF description (Equal)
            };
    UINT1   Throughput:1 {  
                enum description {
                 
                    0,   // Normal
                    1,   // High
                };
                dumpOF description (Equal)
            };
    UINT1   Reliability:1 { 
                enum description {
                 
                    0,   // Normal
                    1,   // High
                };
                dumpOF description (Equal)
            };
    UINT1   Cost:1;         
    UINT1   Congestion:1  { 
                enum description {
                 
                    0,   // No Congestion
                    1,   // Congestion
                };
                dumpOF description (Equal)
            };
};

struct tIPHeader
{
    UINT1   Version:4 { 
                enum description {
                 
                    4,   // IPv4
                    6,   // IPv6
                };
                dumpOF description (Equal)
            };
    
    UINT1   HeaderLength:4 {
                String Dump(dynamic& curItem)
                {
                    return strFormat("%d (=%d*4)",curItem*4,curItem);
                }
            };
            
    tTOS    TypeOfService;

    UINT2   Length;             // Total Length of the IP packet
    UINT2   Identification;
    
    UINT2   FragmentFlag:3 {
                String Dump(dynamic& curItem)
                {
                    return strFormat("%s %s",
                                     (curItem & 0x02)? "No_Fragment":"Allow_Fragment",
                                     (curItem & 0x01)? " ":"(last one in datagram)");
                }
            };
    
    UINT2   FragmentOffset:13;
    UINT1   TimeToLive;
    BYTE    Protocol {
               enum description {
                
                    0x01,   // ICMP
                    0x02,   // IGMP
                    0x03,   // GGP
                    0x04,   // IP
                    0x05,   // ST
                    0x06,   // TCP
                    0x08,   // EGP
                    0x11,   // UDP
                    0x1D,   // ISO-TP4
                    0x46,   // VISA
                    0x50,   // ISO-IP
                    0x58,   // ISO-IGRP
                    0x59,   // OSPF
               };
               String Dump(dynamic& curItem)
               {
                   String str;
                   str=sysGetEnumComment(curItem,"Description","Equal","-");
                   sysSetTitle(0,str);
                   return str;
               }
            };
    WORD    Checksum;
    String  SourceAddress { 
               TypeOf tIpAddress 

               String Dump(dynamic& curItem)
               {
                   return curItem;
               }
            };
    String  DestinationAddress { 

               TypeOf tIpAddress 

               String Dump(dynamic& curItem)
               {
                   sysSetTitle(1,strFormat("Src: %-16s  Dst: %s",this.SourceAddress,this.DestinationAddress));
                   return curItem;
               }
            };
    
    OCTET   OptionData {
                void Translate(String strCurIdent,dynamic& curItem,long nIdxInArr)
                {
                    sysFetchValue(strCurIdent,curItem,now(), this.HeaderLength*4-20, true); 
                }
            };                
};


