helper.go 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  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. )
  10. var (
  11. errInvalidConn = errors.New("invalid connection")
  12. errMissingAddress = errors.New("missing address")
  13. errMissingHeader = errors.New("missing header")
  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. errOpNoSupport = errors.New("operation not supported")
  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. )
  25. func boolint(b bool) int {
  26. if b {
  27. return 1
  28. }
  29. return 0
  30. }
  31. func netAddrToIP4(a net.Addr) net.IP {
  32. switch v := a.(type) {
  33. case *net.UDPAddr:
  34. if ip := v.IP.To4(); ip != nil {
  35. return ip
  36. }
  37. case *net.IPAddr:
  38. if ip := v.IP.To4(); ip != nil {
  39. return ip
  40. }
  41. }
  42. return nil
  43. }
  44. func opAddr(a net.Addr) net.Addr {
  45. switch a.(type) {
  46. case *net.TCPAddr:
  47. if a == nil {
  48. return nil
  49. }
  50. case *net.UDPAddr:
  51. if a == nil {
  52. return nil
  53. }
  54. case *net.IPAddr:
  55. if a == nil {
  56. return nil
  57. }
  58. }
  59. return a
  60. }