helper.go 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. // Copyright 2013 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 ipv6
  5. import (
  6. "errors"
  7. "net"
  8. "runtime"
  9. )
  10. var (
  11. errInvalidConn = errors.New("invalid connection")
  12. errMissingAddress = errors.New("missing address")
  13. errHeaderTooShort = errors.New("header too short")
  14. errInvalidConnType = errors.New("invalid conn type")
  15. errOpNoSupport = errors.New("operation not supported")
  16. errNoSuchInterface = errors.New("no such interface")
  17. errNotImplemented = errors.New("not implemented on " + runtime.GOOS + "/" + runtime.GOARCH)
  18. )
  19. func boolint(b bool) int {
  20. if b {
  21. return 1
  22. }
  23. return 0
  24. }
  25. func netAddrToIP16(a net.Addr) net.IP {
  26. switch v := a.(type) {
  27. case *net.UDPAddr:
  28. if ip := v.IP.To16(); ip != nil && ip.To4() == nil {
  29. return ip
  30. }
  31. case *net.IPAddr:
  32. if ip := v.IP.To16(); ip != nil && ip.To4() == nil {
  33. return ip
  34. }
  35. }
  36. return nil
  37. }
  38. func opAddr(a net.Addr) net.Addr {
  39. switch a.(type) {
  40. case *net.TCPAddr:
  41. if a == nil {
  42. return nil
  43. }
  44. case *net.UDPAddr:
  45. if a == nil {
  46. return nil
  47. }
  48. case *net.IPAddr:
  49. if a == nil {
  50. return nil
  51. }
  52. }
  53. return a
  54. }