github.com/containerd/nerdctl/v2@v2.0.0-beta.5.0.20240520001846-b5758f54fa28/pkg/portutil/procnet/procnet.go (about) 1 /* 2 Copyright The containerd Authors. 3 4 Licensed under the Apache License, Version 2.0 (the "License"); 5 you may not use this file except in compliance with the License. 6 You may obtain a copy of the License at 7 8 http://www.apache.org/licenses/LICENSE-2.0 9 10 Unless required by applicable law or agreed to in writing, software 11 distributed under the License is distributed on an "AS IS" BASIS, 12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 See the License for the specific language governing permissions and 14 limitations under the License. 15 */ 16 17 package procnet 18 19 import ( 20 "encoding/hex" 21 "fmt" 22 "net" 23 "strconv" 24 "strings" 25 ) 26 27 type NetworkDetail struct { 28 LocalIP net.IP 29 LocalPort uint64 30 } 31 32 func Parse(data []string) (results []NetworkDetail) { 33 temp := removeEmpty(data) 34 for _, value := range temp { 35 lineData := removeEmpty(strings.Split(strings.TrimSpace(value), " ")) 36 ip, port, err := ParseAddress(lineData[1]) 37 if err != nil { 38 continue 39 } 40 results = append(results, NetworkDetail{ 41 LocalIP: ip, 42 LocalPort: uint64(port), 43 }) 44 } 45 return results 46 } 47 48 func removeEmpty(array []string) (results []string) { 49 for _, i := range array { 50 if i != "" { 51 results = append(results, i) 52 } 53 } 54 return results 55 } 56 57 // ParseAddress parses a string, e.g.,Akihiro Suda, 10 months ago: • initial commit 58 // "0100007F:0050" (127.0.0.1:80) 59 // "000080FE00000000FF57A6705DC771FE:0050" ([fe80::70a6:57ff:fe71:c75d]:80) 60 // "00000000000000000000000000000000:0050" (0.0.0.0:80) 61 // 62 // See https://serverfault.com/questions/592574/why-does-proc-net-tcp6-represents-1-as-1000 63 // 64 // ParseAddress is expected to be used for /proc/net/{tcp,tcp6} entries on 65 // little endian machines. 66 // Not sure how those entries look like on big endian machines. 67 // All the code below is copied from the lima project in https://github.com/lima-vm/lima/blob/v0.8.3/pkg/guestagent/procnettcp/procnettcp.go#L95-L137 68 // and is licensed under the Apache License, Version 2.0 69 func ParseAddress(s string) (net.IP, uint16, error) { 70 split := strings.SplitN(s, ":", 2) 71 if len(split) != 2 { 72 return nil, 0, fmt.Errorf("unparsable address %q", s) 73 } 74 switch l := len(split[0]); l { 75 case 8, 32: 76 default: 77 return nil, 0, fmt.Errorf("unparsable address %q, expected length of %q to be 8 or 32, got %d", 78 s, split[0], l) 79 } 80 81 ipBytes := make([]byte, len(split[0])/2) // 4 bytes (8 chars) or 16 bytes (32 chars) 82 for i := 0; i < len(split[0])/8; i++ { 83 quartet := split[0][8*i : 8*(i+1)] 84 quartetLE, err := hex.DecodeString(quartet) // surprisingly little endian, per 4 bytes 85 if err != nil { 86 return nil, 0, fmt.Errorf("unparsable address %q: unparsable quartet %q: %w", s, quartet, err) 87 } 88 for j := 0; j < len(quartetLE); j++ { 89 ipBytes[4*i+len(quartetLE)-1-j] = quartetLE[j] 90 } 91 } 92 ip := net.IP(ipBytes) 93 94 port64, err := strconv.ParseUint(split[1], 16, 16) 95 if err != nil { 96 return nil, 0, fmt.Errorf("unparsable address %q: unparsable port %q", s, split[1]) 97 } 98 port := uint16(port64) 99 100 return ip, port, nil 101 }