iprawsock.go 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. // Copyright 2010 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 net
  5. // IPAddr represents the address of an IP end point.
  6. type IPAddr struct {
  7. IP IP
  8. Zone string // IPv6 scoped addressing zone
  9. }
  10. // Network returns the address's network name, "ip".
  11. func (a *IPAddr) Network() string { return "ip" }
  12. func (a *IPAddr) String() string {
  13. if a == nil {
  14. return "<nil>"
  15. }
  16. if a.Zone != "" {
  17. return a.IP.String() + "%" + a.Zone
  18. }
  19. return a.IP.String()
  20. }
  21. func (a *IPAddr) toAddr() Addr {
  22. if a == nil {
  23. return nil
  24. }
  25. return a
  26. }
  27. // ResolveIPAddr parses addr as an IP address of the form "host" or
  28. // "ipv6-host%zone" and resolves the domain name on the network net,
  29. // which must be "ip", "ip4" or "ip6".
  30. func ResolveIPAddr(net, addr string) (*IPAddr, error) {
  31. if net == "" { // a hint wildcard for Go 1.0 undocumented behavior
  32. net = "ip"
  33. }
  34. afnet, _, err := parseNetwork(net)
  35. if err != nil {
  36. return nil, err
  37. }
  38. switch afnet {
  39. case "ip", "ip4", "ip6":
  40. default:
  41. return nil, UnknownNetworkError(net)
  42. }
  43. a, err := resolveInternetAddr(afnet, addr, noDeadline)
  44. if err != nil {
  45. return nil, err
  46. }
  47. return a.toAddr().(*IPAddr), nil
  48. }