github.com/zartbot/goflow@v0.0.0-20221001080520-456f7dfad288/packet/layer3.go (about)

     1  package packet
     2  
     3  // IPv4HDR  : The struct of IPv4 Packet Header
     4  // 0                   1                   2                   3
     5  // 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
     6  // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
     7  // |Version|  IHL  |Type of Service|          Total Length         |
     8  // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
     9  // |         Identification        |Flags|      Fragment Offset    |
    10  // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
    11  // |  Time to Live |    Protocol   |         Header Checksum       |
    12  // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
    13  // |                       Source Address                          |
    14  // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
    15  // |                    Destination Address                        |
    16  // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
    17  // |                    Options                    |    Padding    |
    18  // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
    19  
    20  type IPv4HDR struct {
    21  	Version        uint8
    22  	IHL            uint8
    23  	TOS            uint8
    24  	Length         uint16
    25  	ID             uint16
    26  	Flags          uint8
    27  	FragmentOffset uint16
    28  	TTL            uint8
    29  	Protocol       uint8
    30  	Checksum       uint16
    31  	SrcIP          []byte
    32  	DstIP          []byte
    33  }
    34  
    35  /*
    36  
    37  const (
    38  	IP_ICMP = 1
    39  	IP_INIP = 4
    40  	IP_TCP  = 6
    41  	IP_UDP  = 17
    42  )
    43  
    44  type CiscoIDPElement struct {
    45  	Version  uint8
    46  	Length   uint16
    47  	Protocol uint8
    48  	SrcIP    []byte
    49  	DstIP    []byte
    50  }
    51  
    52  func ParseCiscoIDP(pkt []byte) CiscoIDPElement {
    53  	var r CiscoIDPElement
    54  
    55  	//pos := 0
    56  	r.Version = uint8(pkt[0]) >> 4
    57  	if r.Version == 4 {
    58  		r.Length = binary.BigEndian.Uint16(pkt[2:4])
    59  		r.Protocol = pkt[9]
    60  		r.SrcIP = pkt[12:16]
    61  		r.DstIP = pkt[16:20]
    62  		pos = 20
    63  	}
    64  	if r.Version == 6 {
    65  		r.Length = binary.BigEndian.Uint16(pkt[4:6])
    66  		r.Protocol = pkt[6] //NextHeader
    67  		r.SrcIP = pkt[8:24]
    68  		r.DstIP = pkt[24:40]
    69  		//pos = 40
    70  	}
    71  	switch r.Protocol {
    72  	case IP_TCP:
    73  
    74  	}
    75  
    76  	return r
    77  
    78  }
    79  */