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