helper.go 1.6 KB

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