選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

143 行
4.3 KiB

  1. // Copyright 2021 The Go Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style
  3. // license that can be found in the LICENSE file.
  4. //go:build linux
  5. // +build linux
  6. package unix
  7. import (
  8. "unsafe"
  9. )
  10. // Helpers for dealing with ifreq since it contains a union and thus requires a
  11. // lot of unsafe.Pointer casts to use properly.
  12. // An Ifreq is a type-safe wrapper around the raw ifreq struct. An Ifreq
  13. // contains an interface name and a union of arbitrary data which can be
  14. // accessed using the Ifreq's methods. To create an Ifreq, use the NewIfreq
  15. // function.
  16. //
  17. // Use the Name method to access the stored interface name. The union data
  18. // fields can be get and set using the following methods:
  19. // - Uint16/SetUint16: flags
  20. // - Uint32/SetUint32: ifindex, metric, mtu
  21. type Ifreq struct{ raw ifreq }
  22. // NewIfreq creates an Ifreq with the input network interface name after
  23. // validating the name does not exceed IFNAMSIZ-1 (trailing NULL required)
  24. // bytes.
  25. func NewIfreq(name string) (*Ifreq, error) {
  26. // Leave room for terminating NULL byte.
  27. if len(name) >= IFNAMSIZ {
  28. return nil, EINVAL
  29. }
  30. var ifr ifreq
  31. copy(ifr.Ifrn[:], name)
  32. return &Ifreq{raw: ifr}, nil
  33. }
  34. // TODO(mdlayher): get/set methods for hardware address sockaddr, char array, etc.
  35. // Name returns the interface name associated with the Ifreq.
  36. func (ifr *Ifreq) Name() string {
  37. return ByteSliceToString(ifr.raw.Ifrn[:])
  38. }
  39. // According to netdevice(7), only AF_INET addresses are returned for numerous
  40. // sockaddr ioctls. For convenience, we expose these as Inet4Addr since the Port
  41. // field and other data is always empty.
  42. // Inet4Addr returns the Ifreq union data from an embedded sockaddr as a C
  43. // in_addr/Go []byte (4-byte IPv4 address) value. If the sockaddr family is not
  44. // AF_INET, an error is returned.
  45. func (ifr *Ifreq) Inet4Addr() ([]byte, error) {
  46. raw := *(*RawSockaddrInet4)(unsafe.Pointer(&ifr.raw.Ifru[:SizeofSockaddrInet4][0]))
  47. if raw.Family != AF_INET {
  48. // Cannot safely interpret raw.Addr bytes as an IPv4 address.
  49. return nil, EINVAL
  50. }
  51. return raw.Addr[:], nil
  52. }
  53. // SetInet4Addr sets a C in_addr/Go []byte (4-byte IPv4 address) value in an
  54. // embedded sockaddr within the Ifreq's union data. v must be 4 bytes in length
  55. // or an error will be returned.
  56. func (ifr *Ifreq) SetInet4Addr(v []byte) error {
  57. if len(v) != 4 {
  58. return EINVAL
  59. }
  60. var addr [4]byte
  61. copy(addr[:], v)
  62. ifr.clear()
  63. *(*RawSockaddrInet4)(
  64. unsafe.Pointer(&ifr.raw.Ifru[:SizeofSockaddrInet4][0]),
  65. ) = RawSockaddrInet4{
  66. // Always set IP family as ioctls would require it anyway.
  67. Family: AF_INET,
  68. Addr: addr,
  69. }
  70. return nil
  71. }
  72. // Uint16 returns the Ifreq union data as a C short/Go uint16 value.
  73. func (ifr *Ifreq) Uint16() uint16 {
  74. return *(*uint16)(unsafe.Pointer(&ifr.raw.Ifru[:2][0]))
  75. }
  76. // SetUint16 sets a C short/Go uint16 value as the Ifreq's union data.
  77. func (ifr *Ifreq) SetUint16(v uint16) {
  78. ifr.clear()
  79. *(*uint16)(unsafe.Pointer(&ifr.raw.Ifru[:2][0])) = v
  80. }
  81. // Uint32 returns the Ifreq union data as a C int/Go uint32 value.
  82. func (ifr *Ifreq) Uint32() uint32 {
  83. return *(*uint32)(unsafe.Pointer(&ifr.raw.Ifru[:4][0]))
  84. }
  85. // SetUint32 sets a C int/Go uint32 value as the Ifreq's union data.
  86. func (ifr *Ifreq) SetUint32(v uint32) {
  87. ifr.clear()
  88. *(*uint32)(unsafe.Pointer(&ifr.raw.Ifru[:4][0])) = v
  89. }
  90. // clear zeroes the ifreq's union field to prevent trailing garbage data from
  91. // being sent to the kernel if an ifreq is reused.
  92. func (ifr *Ifreq) clear() {
  93. for i := range ifr.raw.Ifru {
  94. ifr.raw.Ifru[i] = 0
  95. }
  96. }
  97. // TODO(mdlayher): export as IfreqData? For now we can provide helpers such as
  98. // IoctlGetEthtoolDrvinfo which use these APIs under the hood.
  99. // An ifreqData is an Ifreq which carries pointer data. To produce an ifreqData,
  100. // use the Ifreq.withData method.
  101. type ifreqData struct {
  102. name [IFNAMSIZ]byte
  103. // A type separate from ifreq is required in order to comply with the
  104. // unsafe.Pointer rules since the "pointer-ness" of data would not be
  105. // preserved if it were cast into the byte array of a raw ifreq.
  106. data unsafe.Pointer
  107. // Pad to the same size as ifreq.
  108. _ [len(ifreq{}.Ifru) - SizeofPtr]byte
  109. }
  110. // withData produces an ifreqData with the pointer p set for ioctls which require
  111. // arbitrary pointer data.
  112. func (ifr Ifreq) withData(p unsafe.Pointer) ifreqData {
  113. return ifreqData{
  114. name: ifr.raw.Ifrn,
  115. data: p,
  116. }
  117. }