github.com/slackhq/nebula@v1.9.0/firewall/packet.go (about)

     1  package firewall
     2  
     3  import (
     4  	"encoding/json"
     5  	"fmt"
     6  
     7  	"github.com/slackhq/nebula/iputil"
     8  )
     9  
    10  type m map[string]interface{}
    11  
    12  const (
    13  	ProtoAny  = 0 // When we want to handle HOPOPT (0) we can change this, if ever
    14  	ProtoTCP  = 6
    15  	ProtoUDP  = 17
    16  	ProtoICMP = 1
    17  
    18  	PortAny      = 0  // Special value for matching `port: any`
    19  	PortFragment = -1 // Special value for matching `port: fragment`
    20  )
    21  
    22  type Packet struct {
    23  	LocalIP    iputil.VpnIp
    24  	RemoteIP   iputil.VpnIp
    25  	LocalPort  uint16
    26  	RemotePort uint16
    27  	Protocol   uint8
    28  	Fragment   bool
    29  }
    30  
    31  func (fp *Packet) Copy() *Packet {
    32  	return &Packet{
    33  		LocalIP:    fp.LocalIP,
    34  		RemoteIP:   fp.RemoteIP,
    35  		LocalPort:  fp.LocalPort,
    36  		RemotePort: fp.RemotePort,
    37  		Protocol:   fp.Protocol,
    38  		Fragment:   fp.Fragment,
    39  	}
    40  }
    41  
    42  func (fp Packet) MarshalJSON() ([]byte, error) {
    43  	var proto string
    44  	switch fp.Protocol {
    45  	case ProtoTCP:
    46  		proto = "tcp"
    47  	case ProtoICMP:
    48  		proto = "icmp"
    49  	case ProtoUDP:
    50  		proto = "udp"
    51  	default:
    52  		proto = fmt.Sprintf("unknown %v", fp.Protocol)
    53  	}
    54  	return json.Marshal(m{
    55  		"LocalIP":    fp.LocalIP.String(),
    56  		"RemoteIP":   fp.RemoteIP.String(),
    57  		"LocalPort":  fp.LocalPort,
    58  		"RemotePort": fp.RemotePort,
    59  		"Protocol":   proto,
    60  		"Fragment":   fp.Fragment,
    61  	})
    62  }