helper.go 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  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 http://www.freebsd.org/doc/en/books/porters-handbook/freebsd-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. if freebsdVersion >= 1103000 {
  29. l := (m.NN + 4 - 1) &^ (4 - 1)
  30. if m.NN < l && l <= len(m.OOB) {
  31. m.NN = l
  32. }
  33. }
  34. }
  35. func boolint(b bool) int {
  36. if b {
  37. return 1
  38. }
  39. return 0
  40. }
  41. func netAddrToIP4(a net.Addr) net.IP {
  42. switch v := a.(type) {
  43. case *net.UDPAddr:
  44. if ip := v.IP.To4(); ip != nil {
  45. return ip
  46. }
  47. case *net.IPAddr:
  48. if ip := v.IP.To4(); ip != nil {
  49. return ip
  50. }
  51. }
  52. return nil
  53. }
  54. func opAddr(a net.Addr) net.Addr {
  55. switch a.(type) {
  56. case *net.TCPAddr:
  57. if a == nil {
  58. return nil
  59. }
  60. case *net.UDPAddr:
  61. if a == nil {
  62. return nil
  63. }
  64. case *net.IPAddr:
  65. if a == nil {
  66. return nil
  67. }
  68. }
  69. return a
  70. }