helper.go 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  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. errNotImplemented = errors.New("not implemented on " + runtime.GOOS + "/" + runtime.GOARCH)
  16. )
  17. func boolint(b bool) int {
  18. if b {
  19. return 1
  20. }
  21. return 0
  22. }
  23. func netAddrToIP16(a net.Addr) net.IP {
  24. switch v := a.(type) {
  25. case *net.UDPAddr:
  26. if ip := v.IP.To16(); ip != nil && ip.To4() == nil {
  27. return ip
  28. }
  29. case *net.IPAddr:
  30. if ip := v.IP.To16(); ip != nil && ip.To4() == nil {
  31. return ip
  32. }
  33. }
  34. return nil
  35. }
  36. func opAddr(a net.Addr) net.Addr {
  37. switch a.(type) {
  38. case *net.TCPAddr:
  39. if a == nil {
  40. return nil
  41. }
  42. case *net.UDPAddr:
  43. if a == nil {
  44. return nil
  45. }
  46. case *net.IPAddr:
  47. if a == nil {
  48. return nil
  49. }
  50. }
  51. return a
  52. }