abi_sockaddr.mx raw

   1  //:build unix
   2  
   3  package syscall
   4  
   5  // The sockaddr layouts, as offsets into the buffer the kernel reads and writes.
   6  //
   7  // A sockaddr is not a Go struct at the boundary: the kernel reads AF_* at
   8  // offset 0, a big-endian port at 2, an address at a family-specific offset, and
   9  // so on, for a length the caller states separately. In Moxie an `[N]T` field is
  10  // a slice header, which cannot sit where the kernel writes its first field, so
  11  // the extent is a byte buffer and the fields are these offsets (W7a). The
  12  // address families ported here are the ones the tree uses; both supported word
  13  // sizes agree on all of them.
  14  const (
  15  	sockOffFamily = 0
  16  	sockOffPort   = 2
  17  
  18  	sockIn4OffAddr = 4 // [4]byte in_addr
  19  	sockIn4OffZero = 8 // [8]byte
  20  	sockIn4Len     = 16
  21  
  22  	sockIn6OffFlow  = 4 // uint32 flowinfo
  23  	sockIn6OffAddr  = 8 // [16]byte in6_addr
  24  	sockIn6OffScope = 24
  25  	sockIn6Len      = 28
  26  
  27  	sockUnOffPath = 2   // [108]int8
  28  	sockUnPathLen = 108
  29  
  30  	sockLlOffProtocol = 2  // uint16
  31  	sockLlOffIfindex  = 4  // int32
  32  	sockLlOffHatype   = 8  // uint16
  33  	sockLlOffPkttype  = 10 // uint8
  34  	sockLlOffHalen    = 11 // uint8
  35  	sockLlOffAddr     = 12 // [8]uint8
  36  	sockLlAddrLen     = 8
  37  
  38  	sockNlOffPad    = 2 // uint16
  39  	sockNlOffPid    = 4 // uint32
  40  	sockNlOffGroups = 8 // uint32
  41  )
  42  
  43  // sockFamily reads the AF_* every sockaddr starts with.
  44  func sockFamily(b []byte) (v uint16) {
  45  	return abiGet16(b, sockOffFamily)
  46  }
  47  
  48  func sockSetFamily(b []byte, v uint16) {
  49  	abiPut16(b, sockOffFamily, v)
  50  }
  51  
  52  // sockPort reads the 16-bit port in NETWORK byte order that inet sockaddrs
  53  // carry at offset 2. It is the one field the ABI does not store host-endian.
  54  func sockPort(b []byte) (v int32) {
  55  	w := abiWindow(b, sockOffPort, 2)
  56  	if w == nil {
  57  		return 0
  58  	}
  59  	return int32(w[0])<<8 | int32(w[1])
  60  }
  61  
  62  func sockSetPort(b []byte, v int32) (ok bool) {
  63  	w := abiWindow(b, sockOffPort, 2)
  64  	if w == nil || v < 0 || v > 0xFFFF {
  65  		return false
  66  	}
  67  	w[0] = byte(v >> 8)
  68  	w[1] = byte(v)
  69  	return true
  70  }
  71  
  72  // sockIPv4Addr is the 4-byte address window of an AF_INET sockaddr.
  73  func sockIPv4Addr(b []byte) (w []byte) {
  74  	return abiWindow(b, sockIn4OffAddr, 4)
  75  }
  76  
  77  // sockIPv6Addr is the 16-byte address window of an AF_INET6 sockaddr.
  78  func sockIPv6Addr(b []byte) (w []byte) {
  79  	return abiWindow(b, sockIn6OffAddr, 16)
  80  }
  81  
  82  // sockUnixPath is the path window of an AF_UNIX sockaddr.
  83  func sockUnixPath(b []byte) (w []byte) {
  84  	return abiWindow(b, sockUnOffPath, sockUnPathLen)
  85  }
  86