github.com/shaardie/u-root@v4.0.1-0.20190127173353-f24a1c26aa2e+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, sfamily int, 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  	if fam != uint16(sfamily) {
    51  		return FullAddress{}, unix.ENOTSUP
    52  	}
    53  
    54  	// Get the rest of the fields based on the address family.
    55  	switch fam {
    56  	case unix.AF_UNIX:
    57  		path := addr[2:]
    58  		if len(path) > unix.PathMax {
    59  			return FullAddress{}, unix.EINVAL
    60  		}
    61  		// Drop the terminating NUL (if one exists) and everything after
    62  		// it for filesystem (non-abstract) addresses.
    63  		if len(path) > 0 && path[0] != 0 {
    64  			if n := bytes.IndexByte(path[1:], 0); n >= 0 {
    65  				path = path[:n+1]
    66  			}
    67  		}
    68  		return FullAddress{
    69  			Addr: Address(path),
    70  		}, nil
    71  
    72  	case unix.AF_INET:
    73  		var a unix.RawSockaddrInet4
    74  		r = bytes.NewBuffer(addr)
    75  		if err := binary.Read(r, binary.BigEndian, &a); err != nil {
    76  			return FullAddress{}, unix.EFAULT
    77  		}
    78  		out := FullAddress{
    79  			Addr: Address(a.Addr[:]),
    80  			Port: uint16(a.Port),
    81  		}
    82  		if out.Addr == "\x00\x00\x00\x00" {
    83  			out.Addr = ""
    84  		}
    85  		return out, nil
    86  	case unix.AF_INET6:
    87  		var a unix.RawSockaddrInet6
    88  		r = bytes.NewBuffer(addr)
    89  		if err := binary.Read(r, binary.BigEndian, &a); err != nil {
    90  			return FullAddress{}, unix.EFAULT
    91  		}
    92  
    93  		out := FullAddress{
    94  			Addr: Address(a.Addr[:]),
    95  			Port: uint16(a.Port),
    96  		}
    97  
    98  		//if isLinkLocal(out.Addr) {
    99  		//			out.NIC = NICID(a.Scope_id)
   100  		//}
   101  
   102  		if out.Addr == Address(strings.Repeat("\x00", 16)) {
   103  			out.Addr = ""
   104  		}
   105  		return out, nil
   106  	default:
   107  
   108  		return FullAddress{}, unix.ENOTSUP
   109  	}
   110  }