github.com/dolthub/go-mysql-server@v0.18.0/internal/sockstate/netstat.go (about) 1 // Copyright 2020-2021 Dolthub, Inc. 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 sockstate 16 17 import ( 18 "fmt" 19 "net" 20 21 "gopkg.in/src-d/go-errors.v1" 22 ) 23 24 // OS independent part of the netstat_[OS].go modules 25 // Taken (simplified, privatized and with utility functions added) from: 26 // https://github.com/cakturk/go-netstat 27 28 // skState type represents socket connection state 29 type skState uint8 30 31 func (s skState) String() string { 32 return skStates[s] 33 } 34 35 // ErrSocketCheckNotImplemented will be returned for OS where the socket checks is not implemented yet 36 var ErrSocketCheckNotImplemented = errors.NewKind("socket checking not implemented for this OS") 37 38 // Socket states 39 const ( 40 Established skState = 0x01 41 SynSent skState = 0x02 42 SynRecv skState = 0x03 43 FinWait1 skState = 0x04 44 FinWait2 skState = 0x05 45 TimeWait skState = 0x06 46 Close skState = 0x07 47 CloseWait skState = 0x08 48 LastAck skState = 0x09 49 Listen skState = 0x0a 50 Closing skState = 0x0b 51 ) 52 53 var skStates = [...]string{ 54 "UNKNOWN", 55 "ESTABLISHED", 56 "SYN_SENT", 57 "SYN_RECV", 58 "FIN_WAIT1", 59 "FIN_WAIT2", 60 "TIME_WAIT", 61 "", // CLOSE 62 "CLOSE_WAIT", 63 "LAST_ACK", 64 "LISTEN", 65 "CLOSING", 66 } 67 68 // sockAddr represents an ip:port pair 69 type sockAddr struct { 70 IP net.IP 71 Port uint16 72 } 73 74 func (s *sockAddr) String() string { 75 return fmt.Sprintf("%v:%d", s.IP, s.Port) 76 } 77 78 // sockTabEntry type represents each line of the /proc/net/tcp 79 type sockTabEntry struct { 80 Ino string 81 LocalAddr *sockAddr 82 RemoteAddr *sockAddr 83 State skState 84 UID uint32 85 Process *process 86 } 87 88 // process holds the PID and process name to which each socket belongs 89 type process struct { 90 pid int 91 name string 92 } 93 94 func (p *process) String() string { 95 return fmt.Sprintf("%d/%s", p.pid, p.name) 96 } 97 98 // AcceptFn is used to filter socket entries. The value returned indicates 99 // whether the element is to be appended to the socket list. 100 type AcceptFn func(*sockTabEntry) bool