interface_freebsd.go 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. // Copyright 2016 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. package route
  5. func (w *wireFormat) parseInterfaceMessage(typ RIBType, b []byte) (Message, error) {
  6. var extOff, bodyOff int
  7. if typ == sysNET_RT_IFLISTL {
  8. if len(b) < 20 {
  9. return nil, errMessageTooShort
  10. }
  11. extOff = int(nativeEndian.Uint16(b[18:20]))
  12. bodyOff = int(nativeEndian.Uint16(b[16:18]))
  13. } else {
  14. extOff = w.extOff
  15. bodyOff = w.bodyOff
  16. }
  17. if len(b) < extOff || len(b) < bodyOff {
  18. return nil, errInvalidMessage
  19. }
  20. l := int(nativeEndian.Uint16(b[:2]))
  21. if len(b) < l {
  22. return nil, errInvalidMessage
  23. }
  24. attrs := uint(nativeEndian.Uint32(b[4:8]))
  25. if attrs&sysRTA_IFP == 0 {
  26. return nil, nil
  27. }
  28. m := &InterfaceMessage{
  29. Version: int(b[2]),
  30. Type: int(b[3]),
  31. Flags: int(nativeEndian.Uint32(b[8:12])),
  32. Index: int(nativeEndian.Uint16(b[12:14])),
  33. Addrs: make([]Addr, sysRTAX_MAX),
  34. extOff: extOff,
  35. raw: b[:l],
  36. }
  37. a, err := parseLinkAddr(b[bodyOff:])
  38. if err != nil {
  39. return nil, err
  40. }
  41. m.Addrs[sysRTAX_IFP] = a
  42. m.Name = a.(*LinkAddr).Name
  43. return m, nil
  44. }
  45. func (w *wireFormat) parseInterfaceAddrMessage(typ RIBType, b []byte) (Message, error) {
  46. var bodyOff int
  47. if typ == sysNET_RT_IFLISTL {
  48. if len(b) < 24 {
  49. return nil, errMessageTooShort
  50. }
  51. bodyOff = int(nativeEndian.Uint16(b[16:18]))
  52. } else {
  53. bodyOff = w.bodyOff
  54. }
  55. if len(b) < bodyOff {
  56. return nil, errInvalidMessage
  57. }
  58. l := int(nativeEndian.Uint16(b[:2]))
  59. if len(b) < l {
  60. return nil, errInvalidMessage
  61. }
  62. m := &InterfaceAddrMessage{
  63. Version: int(b[2]),
  64. Type: int(b[3]),
  65. Flags: int(nativeEndian.Uint32(b[8:12])),
  66. Index: int(nativeEndian.Uint16(b[12:14])),
  67. raw: b[:l],
  68. }
  69. var err error
  70. m.Addrs, err = parseAddrs(uint(nativeEndian.Uint32(b[4:8])), parseKernelInetAddr, b[bodyOff:])
  71. if err != nil {
  72. return nil, err
  73. }
  74. return m, nil
  75. }