github.com/nxtrace/NTrace-core@v1.3.1-0.20240513132635-39169291e8c9/util/trace.go (about)

     1  package util
     2  
     3  import (
     4  	"encoding/binary"
     5  	"errors"
     6  )
     7  
     8  func GetIPHeaderLength(data []byte) (int, error) {
     9  	if len(data) < 1 {
    10  		return 0, errors.New("received invalid IP header")
    11  	}
    12  	return int((data[0] & 0x0F) * 4), nil
    13  }
    14  
    15  func GetICMPResponsePayload(data []byte) ([]byte, error) {
    16  	length, err := GetIPHeaderLength(data)
    17  	if err != nil {
    18  		return nil, err
    19  	}
    20  
    21  	if len(data) < length {
    22  		return nil, errors.New("length of packet too short")
    23  	}
    24  
    25  	return data[length:], nil
    26  }
    27  
    28  func GetUDPSrcPort(data []byte) uint16 {
    29  	srcPortBytes := data[:2]
    30  	srcPort := binary.BigEndian.Uint16(srcPortBytes)
    31  	return srcPort
    32  }
    33  
    34  func GetTCPSeq(data []byte) uint32 {
    35  	seqBytes := data[4:8]
    36  	return binary.BigEndian.Uint32(seqBytes)
    37  }