github.com/20yyq/packet@v0.1.4-0.20231013092308-386a004a3baa/udp.go (about)

     1  // @@
     2  // @ Author       : Eacher
     3  // @ Date         : 2023-07-13 16:56:05
     4  // @ LastEditTime : 2023-09-04 09:40:16
     5  // @ LastEditors  : Eacher
     6  // @ --------------------------------------------------------------------------------<
     7  // @ Description  : 
     8  // @ --------------------------------------------------------------------------------<
     9  // @ FilePath     : /20yyq/packet/udp.go
    10  // @@
    11  package packet
    12  
    13  import (
    14  	"encoding/binary"
    15  )
    16  
    17  const (
    18  	SizeofDUPPacket = 0x08
    19  )
    20  
    21  type DUPPacket struct {
    22  	SrcPort 	uint16
    23  	DstPort 	uint16
    24  	Len  		uint16
    25  	CheckSum 	uint16
    26  }
    27  
    28  // 14.byte  EthernetPacket
    29  // 20.byte  IPv4Packet 或者 IPv6Packet
    30  func NewDUPPacket(b [SizeofDUPPacket]byte) (udp DUPPacket) {
    31  	udp = DUPPacket{}
    32  	udp.SrcPort 	= binary.BigEndian.Uint16(b[:2])
    33  	udp.DstPort 	= binary.BigEndian.Uint16(b[2:4])
    34  	udp.Len 		= binary.BigEndian.Uint16(b[4:6])
    35  	udp.CheckSum 	= binary.BigEndian.Uint16(b[6:8])
    36  	return
    37  }
    38  
    39  func (udp DUPPacket) WireFormat() []byte {
    40  	var b [SizeofDUPPacket]byte
    41  	binary.BigEndian.PutUint16(b[:2], udp.SrcPort)
    42  	binary.BigEndian.PutUint16(b[2:4], udp.DstPort)
    43  	binary.BigEndian.PutUint16(b[4:6], udp.Len)
    44  	binary.BigEndian.PutUint16(b[6:8], udp.CheckSum)
    45  	return b[:]
    46  }