github.com/system-transparency/u-root@v6.0.1-0.20190919065413-ed07a650de4c+incompatible/pkg/strace/epsocket.go (about) 1 // Copyright 2018 Google LLC. 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 strace 16 17 import ( 18 "bytes" 19 "encoding/binary" 20 "strings" 21 22 "golang.org/x/sys/unix" 23 ) 24 25 const sizeOfInt32 int = 4 26 27 // Address is a byte slice cast as a string that represents the address of a 28 // network node. Or, in the case of unix endpoints, it may represent a path. 29 type Address string 30 31 type FullAddress struct { 32 // Addr is the network address. 33 Addr Address 34 35 // Port is the transport port. 36 // 37 // This may not be used by all endpoint types. 38 Port uint16 39 } 40 41 // GetAddress reads an sockaddr struct from the given address and converts it 42 // to the FullAddress format. It supports AF_UNIX, AF_INET and AF_INET6 43 // addresses. 44 func GetAddress(t *Tracer, addr []byte) (FullAddress, error) { 45 r := bytes.NewBuffer(addr[:2]) 46 var fam uint16 47 if err := binary.Read(r, ByteOrder, &fam); err != nil { 48 return FullAddress{}, unix.EFAULT 49 } 50 51 // Get the rest of the fields based on the address family. 52 switch fam { 53 case unix.AF_UNIX: 54 path := addr[2:] 55 if len(path) > unix.PathMax { 56 return FullAddress{}, unix.EINVAL 57 } 58 // Drop the terminating NUL (if one exists) and everything after 59 // it for filesystem (non-abstract) addresses. 60 if len(path) > 0 && path[0] != 0 { 61 if n := bytes.IndexByte(path[1:], 0); n >= 0 { 62 path = path[:n+1] 63 } 64 } 65 return FullAddress{ 66 Addr: Address(path), 67 }, nil 68 69 case unix.AF_INET: 70 var a unix.RawSockaddrInet4 71 r = bytes.NewBuffer(addr) 72 if err := binary.Read(r, binary.BigEndian, &a); err != nil { 73 return FullAddress{}, unix.EFAULT 74 } 75 out := FullAddress{ 76 Addr: Address(a.Addr[:]), 77 Port: uint16(a.Port), 78 } 79 if out.Addr == "\x00\x00\x00\x00" { 80 out.Addr = "" 81 } 82 return out, nil 83 case unix.AF_INET6: 84 var a unix.RawSockaddrInet6 85 r = bytes.NewBuffer(addr) 86 if err := binary.Read(r, binary.BigEndian, &a); err != nil { 87 return FullAddress{}, unix.EFAULT 88 } 89 90 out := FullAddress{ 91 Addr: Address(a.Addr[:]), 92 Port: uint16(a.Port), 93 } 94 95 //if isLinkLocal(out.Addr) { 96 // out.NIC = NICID(a.Scope_id) 97 //} 98 99 if out.Addr == Address(strings.Repeat("\x00", 16)) { 100 out.Addr = "" 101 } 102 return out, nil 103 default: 104 105 return FullAddress{}, unix.ENOTSUP 106 } 107 }