github.com/cilium/cilium@v1.16.2/pkg/u8proto/u8proto.go (about)

     1  // SPDX-License-Identifier: Apache-2.0
     2  // Copyright Authors of Cilium
     3  
     4  package u8proto
     5  
     6  import (
     7  	"fmt"
     8  	"strconv"
     9  	"strings"
    10  )
    11  
    12  // These definitions must contain and be compatible with the string
    13  // values defined for pkg/pollicy/api/L4Proto
    14  
    15  const (
    16  	// ANY represents all protocols.
    17  	ANY    U8proto = 0
    18  	ICMP   U8proto = 1
    19  	TCP    U8proto = 6
    20  	UDP    U8proto = 17
    21  	ICMPv6 U8proto = 58
    22  	SCTP   U8proto = 132
    23  )
    24  
    25  var protoNames = map[U8proto]string{
    26  	0:   "ANY",
    27  	1:   "ICMP",
    28  	6:   "TCP",
    29  	17:  "UDP",
    30  	58:  "ICMPv6",
    31  	132: "SCTP",
    32  }
    33  
    34  var ProtoIDs = map[string]U8proto{
    35  	"all":    0,
    36  	"any":    0,
    37  	"icmp":   1,
    38  	"tcp":    6,
    39  	"udp":    17,
    40  	"icmpv6": 58,
    41  	"sctp":   132,
    42  }
    43  
    44  type U8proto uint8
    45  
    46  func (p U8proto) String() string {
    47  	if _, ok := protoNames[p]; ok {
    48  		return protoNames[p]
    49  	}
    50  	return strconv.Itoa(int(p))
    51  }
    52  
    53  func ParseProtocol(proto string) (U8proto, error) {
    54  	if u, ok := ProtoIDs[strings.ToLower(proto)]; ok {
    55  		return u, nil
    56  	}
    57  	return 0, fmt.Errorf("unknown protocol '%s'", proto)
    58  }