github.com/u-root/u-root@v7.0.1-0.20200915234505-ad7babab0a8e+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  	"github.com/u-root/u-root/pkg/ubinary"
    23  	"golang.org/x/sys/unix"
    24  )
    25  
    26  // Address is a byte slice cast as a string that represents the address of a
    27  // network node. Or, in the case of unix endpoints, it may represent a path.
    28  type Address string
    29  
    30  type FullAddress struct {
    31  	// Addr is the network address.
    32  	Addr Address
    33  
    34  	// Port is the transport port.
    35  	//
    36  	// This may not be used by all endpoint types.
    37  	Port uint16
    38  }
    39  
    40  // GetAddress reads an sockaddr struct from the given address and converts it
    41  // to the FullAddress format. It supports AF_UNIX, AF_INET and AF_INET6
    42  // addresses.
    43  func GetAddress(t Task, addr []byte) (FullAddress, error) {
    44  	r := bytes.NewBuffer(addr[:2])
    45  	var fam uint16
    46  	if err := binary.Read(r, ubinary.NativeEndian, &fam); err != nil {
    47  		return FullAddress{}, unix.EFAULT
    48  	}
    49  
    50  	// Get the rest of the fields based on the address family.
    51  	switch fam {
    52  	case unix.AF_UNIX:
    53  		path := addr[2:]
    54  		if len(path) > unix.PathMax {
    55  			return FullAddress{}, unix.EINVAL
    56  		}
    57  		// Drop the terminating NUL (if one exists) and everything after
    58  		// it for filesystem (non-abstract) addresses.
    59  		if len(path) > 0 && path[0] != 0 {
    60  			if n := bytes.IndexByte(path[1:], 0); n >= 0 {
    61  				path = path[:n+1]
    62  			}
    63  		}
    64  		return FullAddress{
    65  			Addr: Address(path),
    66  		}, nil
    67  
    68  	case unix.AF_INET:
    69  		var a unix.RawSockaddrInet4
    70  		r = bytes.NewBuffer(addr)
    71  		if err := binary.Read(r, binary.BigEndian, &a); err != nil {
    72  			return FullAddress{}, unix.EFAULT
    73  		}
    74  		out := FullAddress{
    75  			Addr: Address(a.Addr[:]),
    76  			Port: uint16(a.Port),
    77  		}
    78  		if out.Addr == "\x00\x00\x00\x00" {
    79  			out.Addr = ""
    80  		}
    81  		return out, nil
    82  
    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  
   104  	default:
   105  		return FullAddress{}, unix.ENOTSUP
   106  	}
   107  }