2016-03-01 15:57:46 -07:00
|
|
|
// Copyright 2009 The Go Authors. All rights reserved.
|
2009-11-02 19:37:30 -07:00
|
|
|
// Use of this source code is governed by a BSD-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
package net
|
|
|
|
|
2016-04-14 18:47:25 -06:00
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"syscall"
|
|
|
|
)
|
2016-03-02 14:22:11 -07:00
|
|
|
|
2016-08-21 14:52:15 -06:00
|
|
|
// BUG(mikio): On NaCl, Plan 9 and Windows, the ReadMsgUDP and
|
|
|
|
// WriteMsgUDP methods of UDPConn are not implemented.
|
|
|
|
|
|
|
|
// BUG(mikio): On Windows, the File method of UDPConn is not
|
|
|
|
// implemented.
|
|
|
|
|
2016-11-26 17:36:54 -07:00
|
|
|
// BUG(mikio): On NaCl, the ListenMulticastUDP function is not
|
|
|
|
// implemented.
|
2016-08-21 14:52:15 -06:00
|
|
|
|
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
|
net, cmd/fix: add IPv6 scoped addressing zone to INET, INET6 address structs
This CL starts to introduce IPv6 scoped addressing capability
into the net package.
The Public API changes are:
+pkg net, type IPAddr struct, Zone string
+pkg net, type IPNet struct, Zone string
+pkg net, type TCPAddr struct, Zone string
+pkg net, type UDPAddr struct, Zone string
Update #4234.
R=rsc, bradfitz, iant
CC=golang-dev
https://golang.org/cl/6849045
2012-11-26 08:45:42 -07:00
|
|
|
Zone string // IPv6 scoped addressing zone
|
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>"
|
|
|
|
}
|
2013-09-23 20:40:24 -06:00
|
|
|
ip := ipEmptyString(a.IP)
|
net: support IPv6 scoped addressing zone
This CL provides IPv6 scoped addressing zone support as defined
in RFC 4007 for internet protocol family connection setups.
Follwoing types and functions allow a literal IPv6 address with
zone identifer as theirs parameter.
pkg net, func Dial(string, string) (Conn, error)
pkg net, func DialOpt(string, ...DialOption) (Conn, error)
pkg net, func DialTimeout(string, string, time.Duration) (Conn, error)
pkg net, func Listen(string, string) (Listener, error)
pkg net, func ListenPacket(string, string) (PacketConn, error)
pkg net, func ResolveIPAddr(string, string) (*IPAddr, error)
pkg net, func ResolveTCPAddr(string, string) (*TCPAddr, error)
pkg net, func ResolveUDPAddr(string, string) (*UDPAddr, error)
pkg net, type IPAddr struct, Zone string
pkg net, type TCPAddr struct, Zone string
pkg net, type UDPAddr struct, Zone string
Also follwoing methods return a literal IPv6 address with zone
identifier string if possible.
pkg net, method (*IPAddr) String() string
pkg net, method (*TCPAddr) String() string
pkg net, method (*UDPAddr) String() string
Fixes #4234.
Fixes #4501.
Update #5081.
R=rsc, iant
CC=golang-dev
https://golang.org/cl/6816116
2013-03-22 18:57:40 -06:00
|
|
|
if a.Zone != "" {
|
2013-09-23 20:40:24 -06:00
|
|
|
return JoinHostPort(ip+"%"+a.Zone, itoa(a.Port))
|
net: support IPv6 scoped addressing zone
This CL provides IPv6 scoped addressing zone support as defined
in RFC 4007 for internet protocol family connection setups.
Follwoing types and functions allow a literal IPv6 address with
zone identifer as theirs parameter.
pkg net, func Dial(string, string) (Conn, error)
pkg net, func DialOpt(string, ...DialOption) (Conn, error)
pkg net, func DialTimeout(string, string, time.Duration) (Conn, error)
pkg net, func Listen(string, string) (Listener, error)
pkg net, func ListenPacket(string, string) (PacketConn, error)
pkg net, func ResolveIPAddr(string, string) (*IPAddr, error)
pkg net, func ResolveTCPAddr(string, string) (*TCPAddr, error)
pkg net, func ResolveUDPAddr(string, string) (*UDPAddr, error)
pkg net, type IPAddr struct, Zone string
pkg net, type TCPAddr struct, Zone string
pkg net, type UDPAddr struct, Zone string
Also follwoing methods return a literal IPv6 address with zone
identifier string if possible.
pkg net, method (*IPAddr) String() string
pkg net, method (*TCPAddr) String() string
pkg net, method (*UDPAddr) String() string
Fixes #4234.
Fixes #4501.
Update #5081.
R=rsc, iant
CC=golang-dev
https://golang.org/cl/6816116
2013-03-22 18:57:40 -06:00
|
|
|
}
|
2013-09-23 20:40:24 -06:00
|
|
|
return JoinHostPort(ip, itoa(a.Port))
|
2010-07-27 01:22:22 -06:00
|
|
|
}
|
2009-11-02 19:37:30 -07:00
|
|
|
|
2015-03-26 08:26:45 -06:00
|
|
|
func (a *UDPAddr) isWildcard() bool {
|
|
|
|
if a == nil || a.IP == nil {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
return a.IP.IsUnspecified()
|
|
|
|
}
|
|
|
|
|
2015-05-29 16:33:16 -06:00
|
|
|
func (a *UDPAddr) opAddr() Addr {
|
|
|
|
if a == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return a
|
|
|
|
}
|
|
|
|
|
net: support IPv6 scoped addressing zone
This CL provides IPv6 scoped addressing zone support as defined
in RFC 4007 for internet protocol family connection setups.
Follwoing types and functions allow a literal IPv6 address with
zone identifer as theirs parameter.
pkg net, func Dial(string, string) (Conn, error)
pkg net, func DialOpt(string, ...DialOption) (Conn, error)
pkg net, func DialTimeout(string, string, time.Duration) (Conn, error)
pkg net, func Listen(string, string) (Listener, error)
pkg net, func ListenPacket(string, string) (PacketConn, error)
pkg net, func ResolveIPAddr(string, string) (*IPAddr, error)
pkg net, func ResolveTCPAddr(string, string) (*TCPAddr, error)
pkg net, func ResolveUDPAddr(string, string) (*UDPAddr, error)
pkg net, type IPAddr struct, Zone string
pkg net, type TCPAddr struct, Zone string
pkg net, type UDPAddr struct, Zone string
Also follwoing methods return a literal IPv6 address with zone
identifier string if possible.
pkg net, method (*IPAddr) String() string
pkg net, method (*TCPAddr) String() string
pkg net, method (*UDPAddr) String() string
Fixes #4234.
Fixes #4501.
Update #5081.
R=rsc, iant
CC=golang-dev
https://golang.org/cl/6816116
2013-03-22 18:57:40 -06:00
|
|
|
// ResolveUDPAddr parses addr as a UDP address of the form "host:port"
|
|
|
|
// or "[ipv6-host%zone]:port" and resolves a pair of domain name and
|
|
|
|
// port name on the network net, which must be "udp", "udp4" or
|
|
|
|
// "udp6". A literal address or host name for IPv6 must be enclosed
|
|
|
|
// in square brackets, as in "[::1]:80", "[ipv6-host]:http" or
|
|
|
|
// "[ipv6-host%zone]:80".
|
2016-10-24 18:49:22 -06:00
|
|
|
//
|
|
|
|
// Resolving a hostname is not recommended because this returns at most
|
|
|
|
// one of its IP addresses.
|
2011-11-01 20:05:34 -06:00
|
|
|
func ResolveUDPAddr(net, addr string) (*UDPAddr, error) {
|
2012-11-27 14:36:05 -07:00
|
|
|
switch net {
|
|
|
|
case "udp", "udp4", "udp6":
|
2012-11-30 22:49:54 -07:00
|
|
|
case "": // a hint wildcard for Go 1.0 undocumented behavior
|
|
|
|
net = "udp"
|
2012-11-27 14:36:05 -07:00
|
|
|
default:
|
|
|
|
return nil, UnknownNetworkError(net)
|
|
|
|
}
|
2016-09-20 13:45:37 -06:00
|
|
|
addrs, err := DefaultResolver.internetAddrList(context.Background(), net, addr)
|
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
|
|
|
}
|
2015-04-01 16:17:09 -06:00
|
|
|
return addrs.first(isIPv4).(*UDPAddr), nil
|
2009-11-02 19:37:30 -07:00
|
|
|
}
|
2016-03-02 14:22:11 -07:00
|
|
|
|
|
|
|
// UDPConn is the implementation of the Conn and PacketConn interfaces
|
|
|
|
// for UDP network connections.
|
|
|
|
type UDPConn struct {
|
|
|
|
conn
|
|
|
|
}
|
|
|
|
|
|
|
|
// ReadFromUDP reads a UDP packet from c, copying the payload into b.
|
|
|
|
// It returns the number of bytes copied into b and the return address
|
|
|
|
// that was on the packet.
|
|
|
|
//
|
|
|
|
// ReadFromUDP can be made to time out and return an error with
|
|
|
|
// Timeout() == true after a fixed time limit; see SetDeadline and
|
|
|
|
// SetReadDeadline.
|
|
|
|
func (c *UDPConn) ReadFromUDP(b []byte) (int, *UDPAddr, error) {
|
|
|
|
if !c.ok() {
|
|
|
|
return 0, nil, syscall.EINVAL
|
|
|
|
}
|
|
|
|
n, addr, err := c.readFrom(b)
|
|
|
|
if err != nil {
|
|
|
|
err = &OpError{Op: "read", Net: c.fd.net, Source: c.fd.laddr, Addr: c.fd.raddr, Err: err}
|
|
|
|
}
|
|
|
|
return n, addr, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// ReadFrom implements the PacketConn ReadFrom method.
|
|
|
|
func (c *UDPConn) ReadFrom(b []byte) (int, Addr, error) {
|
|
|
|
if !c.ok() {
|
|
|
|
return 0, nil, syscall.EINVAL
|
|
|
|
}
|
|
|
|
n, addr, err := c.readFrom(b)
|
|
|
|
if err != nil {
|
|
|
|
err = &OpError{Op: "read", Net: c.fd.net, Source: c.fd.laddr, Addr: c.fd.raddr, Err: err}
|
|
|
|
}
|
|
|
|
if addr == nil {
|
|
|
|
return n, nil, err
|
|
|
|
}
|
|
|
|
return n, addr, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// ReadMsgUDP reads a packet from c, copying the payload into b and
|
|
|
|
// the associated out-of-band data into oob. It returns the number
|
|
|
|
// of bytes copied into b, the number of bytes copied into oob, the
|
|
|
|
// flags that were set on the packet and the source address of the
|
|
|
|
// packet.
|
|
|
|
func (c *UDPConn) ReadMsgUDP(b, oob []byte) (n, oobn, flags int, addr *UDPAddr, err error) {
|
|
|
|
if !c.ok() {
|
|
|
|
return 0, 0, 0, nil, syscall.EINVAL
|
|
|
|
}
|
|
|
|
n, oobn, flags, addr, err = c.readMsg(b, oob)
|
|
|
|
if err != nil {
|
|
|
|
err = &OpError{Op: "read", Net: c.fd.net, Source: c.fd.laddr, Addr: c.fd.raddr, Err: err}
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// WriteToUDP writes a UDP packet to addr via c, copying the payload
|
|
|
|
// from b.
|
|
|
|
//
|
|
|
|
// WriteToUDP can be made to time out and return an error with
|
|
|
|
// Timeout() == true after a fixed time limit; see SetDeadline and
|
|
|
|
// SetWriteDeadline. On packet-oriented connections, write timeouts
|
|
|
|
// are rare.
|
|
|
|
func (c *UDPConn) WriteToUDP(b []byte, addr *UDPAddr) (int, error) {
|
|
|
|
if !c.ok() {
|
|
|
|
return 0, syscall.EINVAL
|
|
|
|
}
|
|
|
|
n, err := c.writeTo(b, addr)
|
|
|
|
if err != nil {
|
|
|
|
err = &OpError{Op: "write", Net: c.fd.net, Source: c.fd.laddr, Addr: addr.opAddr(), Err: err}
|
|
|
|
}
|
|
|
|
return n, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// WriteTo implements the PacketConn WriteTo method.
|
|
|
|
func (c *UDPConn) WriteTo(b []byte, addr Addr) (int, error) {
|
|
|
|
if !c.ok() {
|
|
|
|
return 0, syscall.EINVAL
|
|
|
|
}
|
|
|
|
a, ok := addr.(*UDPAddr)
|
|
|
|
if !ok {
|
|
|
|
return 0, &OpError{Op: "write", Net: c.fd.net, Source: c.fd.laddr, Addr: addr, Err: syscall.EINVAL}
|
|
|
|
}
|
|
|
|
n, err := c.writeTo(b, a)
|
|
|
|
if err != nil {
|
|
|
|
err = &OpError{Op: "write", Net: c.fd.net, Source: c.fd.laddr, Addr: a.opAddr(), Err: err}
|
|
|
|
}
|
|
|
|
return n, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// WriteMsgUDP writes a packet to addr via c if c isn't connected, or
|
|
|
|
// to c's remote destination address if c is connected (in which case
|
|
|
|
// addr must be nil). The payload is copied from b and the associated
|
|
|
|
// out-of-band data is copied from oob. It returns the number of
|
|
|
|
// payload and out-of-band bytes written.
|
|
|
|
func (c *UDPConn) WriteMsgUDP(b, oob []byte, addr *UDPAddr) (n, oobn int, err error) {
|
|
|
|
if !c.ok() {
|
|
|
|
return 0, 0, syscall.EINVAL
|
|
|
|
}
|
|
|
|
n, oobn, err = c.writeMsg(b, oob, addr)
|
|
|
|
if err != nil {
|
|
|
|
err = &OpError{Op: "write", Net: c.fd.net, Source: c.fd.laddr, Addr: addr.opAddr(), Err: err}
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
func newUDPConn(fd *netFD) *UDPConn { return &UDPConn{conn{fd}} }
|
|
|
|
|
|
|
|
// DialUDP connects to the remote address raddr on the network net,
|
|
|
|
// which must be "udp", "udp4", or "udp6". If laddr is not nil, it is
|
|
|
|
// used as the local address for the connection.
|
|
|
|
func DialUDP(net string, laddr, raddr *UDPAddr) (*UDPConn, error) {
|
|
|
|
switch net {
|
|
|
|
case "udp", "udp4", "udp6":
|
|
|
|
default:
|
|
|
|
return nil, &OpError{Op: "dial", Net: net, Source: laddr.opAddr(), Addr: raddr.opAddr(), Err: UnknownNetworkError(net)}
|
|
|
|
}
|
|
|
|
if raddr == nil {
|
|
|
|
return nil, &OpError{Op: "dial", Net: net, Source: laddr.opAddr(), Addr: nil, Err: errMissingAddress}
|
|
|
|
}
|
2016-04-14 18:47:25 -06:00
|
|
|
c, err := dialUDP(context.Background(), net, laddr, raddr)
|
2016-03-02 14:22:11 -07:00
|
|
|
if err != nil {
|
|
|
|
return nil, &OpError{Op: "dial", Net: net, Source: laddr.opAddr(), Addr: raddr.opAddr(), Err: err}
|
|
|
|
}
|
|
|
|
return c, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// ListenUDP listens for incoming UDP packets addressed to the local
|
|
|
|
// address laddr. Net must be "udp", "udp4", or "udp6". If laddr has
|
|
|
|
// a port of 0, ListenUDP will choose an available port.
|
|
|
|
// The LocalAddr method of the returned UDPConn can be used to
|
|
|
|
// discover the port. The returned connection's ReadFrom and WriteTo
|
|
|
|
// methods can be used to receive and send UDP packets with per-packet
|
|
|
|
// addressing.
|
|
|
|
func ListenUDP(net string, laddr *UDPAddr) (*UDPConn, error) {
|
|
|
|
switch net {
|
|
|
|
case "udp", "udp4", "udp6":
|
|
|
|
default:
|
|
|
|
return nil, &OpError{Op: "listen", Net: net, Source: nil, Addr: laddr.opAddr(), Err: UnknownNetworkError(net)}
|
|
|
|
}
|
|
|
|
if laddr == nil {
|
|
|
|
laddr = &UDPAddr{}
|
|
|
|
}
|
2016-04-14 18:47:25 -06:00
|
|
|
c, err := listenUDP(context.Background(), net, laddr)
|
2016-03-02 14:22:11 -07:00
|
|
|
if err != nil {
|
|
|
|
return nil, &OpError{Op: "listen", Net: net, Source: nil, Addr: laddr.opAddr(), Err: err}
|
|
|
|
}
|
|
|
|
return c, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// ListenMulticastUDP listens for incoming multicast UDP packets
|
|
|
|
// addressed to the group address gaddr on the interface ifi.
|
|
|
|
// Network must be "udp", "udp4" or "udp6".
|
|
|
|
// ListenMulticastUDP uses the system-assigned multicast interface
|
|
|
|
// when ifi is nil, although this is not recommended because the
|
|
|
|
// assignment depends on platforms and sometimes it might require
|
|
|
|
// routing configuration.
|
|
|
|
//
|
|
|
|
// ListenMulticastUDP is just for convenience of simple, small
|
|
|
|
// applications. There are golang.org/x/net/ipv4 and
|
|
|
|
// golang.org/x/net/ipv6 packages for general purpose uses.
|
|
|
|
func ListenMulticastUDP(network string, ifi *Interface, gaddr *UDPAddr) (*UDPConn, error) {
|
|
|
|
switch network {
|
|
|
|
case "udp", "udp4", "udp6":
|
|
|
|
default:
|
|
|
|
return nil, &OpError{Op: "listen", Net: network, Source: nil, Addr: gaddr.opAddr(), Err: UnknownNetworkError(network)}
|
|
|
|
}
|
|
|
|
if gaddr == nil || gaddr.IP == nil {
|
|
|
|
return nil, &OpError{Op: "listen", Net: network, Source: nil, Addr: gaddr.opAddr(), Err: errMissingAddress}
|
|
|
|
}
|
2016-04-14 18:47:25 -06:00
|
|
|
c, err := listenMulticastUDP(context.Background(), network, ifi, gaddr)
|
2016-03-02 14:22:11 -07:00
|
|
|
if err != nil {
|
|
|
|
return nil, &OpError{Op: "listen", Net: network, Source: nil, Addr: gaddr.opAddr(), Err: err}
|
|
|
|
}
|
|
|
|
return c, nil
|
|
|
|
}
|