2009-11-02 19:37:30 -07:00
|
|
|
// Copyright 2009 The Go Authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a BSD-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
// UDP sockets
|
|
|
|
|
|
|
|
package net
|
|
|
|
|
2012-11-08 09:35:16 -07:00
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"time"
|
|
|
|
)
|
2012-06-06 16:38:56 -06:00
|
|
|
|
|
|
|
var ErrWriteToConnected = errors.New("use of WriteTo with pre-connected UDP")
|
|
|
|
|
2009-11-02 19:37:30 -07:00
|
|
|
// UDPAddr represents the address of a UDP end point.
|
|
|
|
type UDPAddr struct {
|
2009-12-15 16:35:38 -07:00
|
|
|
IP IP
|
|
|
|
Port int
|
2009-11-02 19:37:30 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// Network returns the address's network name, "udp".
|
2009-12-15 16:35:38 -07:00
|
|
|
func (a *UDPAddr) Network() string { return "udp" }
|
2009-11-02 19:37:30 -07:00
|
|
|
|
2010-07-27 01:22:22 -06:00
|
|
|
func (a *UDPAddr) String() string {
|
|
|
|
if a == nil {
|
|
|
|
return "<nil>"
|
|
|
|
}
|
2011-03-28 21:28:42 -06:00
|
|
|
return JoinHostPort(a.IP.String(), itoa(a.Port))
|
2010-07-27 01:22:22 -06:00
|
|
|
}
|
2009-11-02 19:37:30 -07:00
|
|
|
|
|
|
|
// ResolveUDPAddr parses addr as a UDP address of the form
|
|
|
|
// host:port and resolves domain names or port names to
|
2011-05-16 15:03:06 -06:00
|
|
|
// numeric addresses on the network net, which must be "udp",
|
|
|
|
// "udp4" or "udp6". A literal IPv6 host address must be
|
2009-11-02 19:37:30 -07:00
|
|
|
// enclosed in square brackets, as in "[::]:80".
|
2011-11-01 20:05:34 -06:00
|
|
|
func ResolveUDPAddr(net, addr string) (*UDPAddr, error) {
|
2012-11-08 09:35:16 -07:00
|
|
|
return resolveUDPAddr(net, addr, noDeadline)
|
|
|
|
}
|
|
|
|
|
|
|
|
func resolveUDPAddr(net, addr string, deadline time.Time) (*UDPAddr, error) {
|
|
|
|
ip, port, err := hostPortToIP(net, addr, deadline)
|
2009-11-02 19:37:30 -07:00
|
|
|
if err != nil {
|
2009-11-09 13:07:39 -07:00
|
|
|
return nil, err
|
2009-11-02 19:37:30 -07:00
|
|
|
}
|
2009-12-15 16:35:38 -07:00
|
|
|
return &UDPAddr{ip, port}, nil
|
2009-11-02 19:37:30 -07:00
|
|
|
}
|