github.com/inspektor-gadget/inspektor-gadget@v0.28.1/pkg/gadgets/helpers_all.go (about) 1 // Copyright 2023-2024 The Inspektor Gadget authors 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 // This file contains generic helpers that are not specific to an operating system nor architecture. 16 17 package gadgets 18 19 import ( 20 "fmt" 21 "syscall" 22 ) 23 24 func ProtoString(proto int) string { 25 // proto definitions: 26 // https://www.iana.org/assignments/protocol-numbers/protocol-numbers.xhtml 27 protoStr := fmt.Sprintf("UNKNOWN#%d", proto) 28 switch proto { 29 case syscall.IPPROTO_TCP: 30 protoStr = "TCP" 31 case syscall.IPPROTO_UDP: 32 protoStr = "UDP" 33 } 34 return protoStr 35 } 36 37 func FromCString(in []byte) string { 38 for i := 0; i < len(in); i++ { 39 if in[i] == 0 { 40 return string(in[:i]) 41 } 42 } 43 return string(in) 44 } 45 46 func FromCStringN(in []byte, length int) string { 47 l := len(in) 48 if length < l { 49 l = length 50 } 51 52 for i := 0; i < l; i++ { 53 if in[i] == 0 { 54 return string(in[:i]) 55 } 56 } 57 return string(in[:l]) 58 }