interface_classic.go 1.6 KB

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