123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104 |
- package layers
- import (
- "errors"
- "fmt"
- "github.com/google/gopacket"
- )
- type tcpipchecksum struct {
- pseudoheader tcpipPseudoHeader
- }
- type tcpipPseudoHeader interface {
- pseudoheaderChecksum() (uint32, error)
- }
- func (ip *IPv4) pseudoheaderChecksum() (csum uint32, err error) {
- if err := ip.AddressTo4(); err != nil {
- return 0, err
- }
- csum += (uint32(ip.SrcIP[0]) + uint32(ip.SrcIP[2])) << 8
- csum += uint32(ip.SrcIP[1]) + uint32(ip.SrcIP[3])
- csum += (uint32(ip.DstIP[0]) + uint32(ip.DstIP[2])) << 8
- csum += uint32(ip.DstIP[1]) + uint32(ip.DstIP[3])
- return csum, nil
- }
- func (ip *IPv6) pseudoheaderChecksum() (csum uint32, err error) {
- if err := ip.AddressTo16(); err != nil {
- return 0, err
- }
- for i := 0; i < 16; i += 2 {
- csum += uint32(ip.SrcIP[i]) << 8
- csum += uint32(ip.SrcIP[i+1])
- csum += uint32(ip.DstIP[i]) << 8
- csum += uint32(ip.DstIP[i+1])
- }
- return csum, nil
- }
- func tcpipChecksum(data []byte, csum uint32) uint16 {
-
-
-
- length := len(data) - 1
- for i := 0; i < length; i += 2 {
-
-
- csum += uint32(data[i]) << 8
- csum += uint32(data[i+1])
- }
- if len(data)%2 == 1 {
- csum += uint32(data[length]) << 8
- }
- for csum > 0xffff {
- csum = (csum >> 16) + (csum & 0xffff)
- }
- return ^uint16(csum)
- }
- func (c *tcpipchecksum) computeChecksum(headerAndPayload []byte, headerProtocol IPProtocol) (uint16, error) {
- if c.pseudoheader == nil {
- return 0, errors.New("TCP/IP layer 4 checksum cannot be computed without network layer... call SetNetworkLayerForChecksum to set which layer to use")
- }
- length := uint32(len(headerAndPayload))
- csum, err := c.pseudoheader.pseudoheaderChecksum()
- if err != nil {
- return 0, err
- }
- csum += uint32(headerProtocol)
- csum += length & 0xffff
- csum += length >> 16
- return tcpipChecksum(headerAndPayload, csum), nil
- }
- func (i *tcpipchecksum) SetNetworkLayerForChecksum(l gopacket.NetworkLayer) error {
- switch v := l.(type) {
- case *IPv4:
- i.pseudoheader = v
- case *IPv6:
- i.pseudoheader = v
- default:
- return fmt.Errorf("cannot use layer type %v for tcp checksum network layer", l.LayerType())
- }
- return nil
- }
|