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

     1  // Copyright 2016-2017 Authors of Cilium
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package u8proto
    16  
    17  import (
    18  	"fmt"
    19  	"strconv"
    20  	"strings"
    21  )
    22  
    23  // These definitions must contain and be compatible with the string
    24  // values defined for pkg/pollicy/api/L4Proto
    25  
    26  const (
    27  	// ANY represents all protocols.
    28  	ANY    U8proto = 0
    29  	ICMP   U8proto = 1
    30  	TCP    U8proto = 6
    31  	UDP    U8proto = 17
    32  	ICMPv6 U8proto = 58
    33  )
    34  
    35  var protoNames = map[U8proto]string{
    36  	0:  "ANY",
    37  	1:  "ICMP",
    38  	6:  "TCP",
    39  	17: "UDP",
    40  	58: "ICMPv6",
    41  }
    42  
    43  var ProtoIDs = map[string]U8proto{
    44  	"all":    0,
    45  	"any":    0,
    46  	"icmp":   1,
    47  	"tcp":    6,
    48  	"udp":    17,
    49  	"icmpv6": 58,
    50  }
    51  
    52  type U8proto uint8
    53  
    54  func (p U8proto) String() string {
    55  	if _, ok := protoNames[p]; ok {
    56  		return protoNames[p]
    57  	}
    58  	return strconv.Itoa(int(p))
    59  }
    60  
    61  func ParseProtocol(proto string) (U8proto, error) {
    62  	if u, ok := ProtoIDs[strings.ToLower(proto)]; ok {
    63  		return u, nil
    64  	}
    65  	return 0, fmt.Errorf("unknown protocol '%s'", proto)
    66  }