helper.go 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. // Copyright 2012 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 ipv4
  5. import (
  6. "errors"
  7. "net"
  8. "runtime"
  9. "golang.org/x/net/internal/socket"
  10. )
  11. var (
  12. errInvalidConn = errors.New("invalid connection")
  13. errMissingAddress = errors.New("missing address")
  14. errMissingHeader = errors.New("missing header")
  15. errNilHeader = errors.New("nil header")
  16. errHeaderTooShort = errors.New("header too short")
  17. errExtHeaderTooShort = errors.New("extension header too short")
  18. errInvalidConnType = errors.New("invalid conn type")
  19. errNoSuchInterface = errors.New("no such interface")
  20. errNoSuchMulticastInterface = errors.New("no such multicast interface")
  21. errNotImplemented = errors.New("not implemented on " + runtime.GOOS + "/" + runtime.GOARCH)
  22. // See https://www.freebsd.org/doc/en/books/porters-handbook/versions.html.
  23. freebsdVersion uint32
  24. compatFreeBSD32 bool // 386 emulation on amd64
  25. )
  26. // See golang.org/issue/30899.
  27. func adjustFreeBSD32(m *socket.Message) {
  28. // FreeBSD 12.0-RELEASE is affected by https://bugs.freebsd.org/bugzilla/show_bug.cgi?id=236737
  29. if 1200086 <= freebsdVersion && freebsdVersion < 1201000 {
  30. l := (m.NN + 4 - 1) &^ (4 - 1)
  31. if m.NN < l && l <= len(m.OOB) {
  32. m.NN = l
  33. }
  34. }
  35. }
  36. func boolint(b bool) int {
  37. if b {
  38. return 1
  39. }
  40. return 0
  41. }
  42. func netAddrToIP4(a net.Addr) net.IP {
  43. switch v := a.(type) {
  44. case *net.UDPAddr:
  45. if ip := v.IP.To4(); ip != nil {
  46. return ip
  47. }
  48. case *net.IPAddr:
  49. if ip := v.IP.To4(); ip != nil {
  50. return ip
  51. }
  52. }
  53. return nil
  54. }
  55. func opAddr(a net.Addr) net.Addr {
  56. switch a.(type) {
  57. case *net.TCPAddr:
  58. if a == nil {
  59. return nil
  60. }
  61. case *net.UDPAddr:
  62. if a == nil {
  63. return nil
  64. }
  65. case *net.IPAddr:
  66. if a == nil {
  67. return nil
  68. }
  69. }
  70. return a
  71. }