helper.go 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  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. )
  9. var (
  10. errInvalidConn = errors.New("invalid connection")
  11. errMissingAddress = errors.New("missing address")
  12. errMissingHeader = errors.New("missing header")
  13. errHeaderTooShort = errors.New("header too short")
  14. errBufferTooShort = errors.New("buffer too short")
  15. errInvalidConnType = errors.New("invalid conn type")
  16. errOpNoSupport = errors.New("operation not supported")
  17. errNoSuchInterface = errors.New("no such interface")
  18. errNoSuchMulticastInterface = errors.New("no such multicast interface")
  19. // See http://www.freebsd.org/doc/en/books/porters-handbook/freebsd-versions.html.
  20. freebsdVersion uint32
  21. )
  22. func boolint(b bool) int {
  23. if b {
  24. return 1
  25. }
  26. return 0
  27. }
  28. func netAddrToIP4(a net.Addr) net.IP {
  29. switch v := a.(type) {
  30. case *net.UDPAddr:
  31. if ip := v.IP.To4(); ip != nil {
  32. return ip
  33. }
  34. case *net.IPAddr:
  35. if ip := v.IP.To4(); ip != nil {
  36. return ip
  37. }
  38. }
  39. return nil
  40. }
  41. func opAddr(a net.Addr) net.Addr {
  42. switch a.(type) {
  43. case *net.TCPAddr:
  44. if a == nil {
  45. return nil
  46. }
  47. case *net.UDPAddr:
  48. if a == nil {
  49. return nil
  50. }
  51. case *net.IPAddr:
  52. if a == nil {
  53. return nil
  54. }
  55. }
  56. return a
  57. }