2016-07-22 20:15:38 +02:00
|
|
|
// Copyright 2009 The Go Authors. All rights reserved.
|
2010-12-03 05:34:57 +01:00
|
|
|
// Use of this source code is governed by a BSD-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
package net
|
|
|
|
|
2016-07-22 20:15:38 +02:00
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"syscall"
|
|
|
|
)
|
|
|
|
|
2020-01-03 00:05:27 +01:00
|
|
|
// BUG(mikio): On Plan 9, the ReadMsgUDP and
|
2017-01-14 01:05:42 +01:00
|
|
|
// WriteMsgUDP methods of UDPConn are not implemented.
|
|
|
|
|
|
|
|
// BUG(mikio): On Windows, the File method of UDPConn is not
|
|
|
|
// implemented.
|
|
|
|
|
2018-09-24 23:46:21 +02:00
|
|
|
// BUG(mikio): On JS, methods and functions related to UDPConn are not
|
|
|
|
// implemented.
|
|
|
|
|
2010-12-03 05:34:57 +01:00
|
|
|
// UDPAddr represents the address of a UDP end point.
|
|
|
|
type UDPAddr struct {
|
|
|
|
IP IP
|
|
|
|
Port int
|
2012-12-13 00:13:29 +01:00
|
|
|
Zone string // IPv6 scoped addressing zone
|
2010-12-03 05:34:57 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Network returns the address's network name, "udp".
|
|
|
|
func (a *UDPAddr) Network() string { return "udp" }
|
|
|
|
|
|
|
|
func (a *UDPAddr) String() string {
|
|
|
|
if a == nil {
|
|
|
|
return "<nil>"
|
|
|
|
}
|
2013-11-06 20:49:01 +01:00
|
|
|
ip := ipEmptyString(a.IP)
|
2013-07-16 08:54:42 +02:00
|
|
|
if a.Zone != "" {
|
2013-11-06 20:49:01 +01:00
|
|
|
return JoinHostPort(ip+"%"+a.Zone, itoa(a.Port))
|
2013-07-16 08:54:42 +02:00
|
|
|
}
|
2013-11-06 20:49:01 +01:00
|
|
|
return JoinHostPort(ip, itoa(a.Port))
|
|
|
|
}
|
|
|
|
|
2015-10-31 01:59:47 +01:00
|
|
|
func (a *UDPAddr) isWildcard() bool {
|
|
|
|
if a == nil || a.IP == nil {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
return a.IP.IsUnspecified()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a *UDPAddr) opAddr() Addr {
|
2013-11-06 20:49:01 +01:00
|
|
|
if a == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return a
|
2010-12-03 05:34:57 +01:00
|
|
|
}
|
|
|
|
|
2017-09-14 19:11:35 +02:00
|
|
|
// ResolveUDPAddr returns an address of UDP end point.
|
2017-01-14 01:05:42 +01:00
|
|
|
//
|
2017-09-14 19:11:35 +02:00
|
|
|
// The network must be a UDP network name.
|
|
|
|
//
|
|
|
|
// If the host in the address parameter is not a literal IP address or
|
|
|
|
// the port is not a literal port number, ResolveUDPAddr resolves the
|
|
|
|
// address to an address of UDP end point.
|
|
|
|
// Otherwise, it parses the address as a pair of literal IP address
|
|
|
|
// and port number.
|
|
|
|
// The address parameter can use a host name, but this is not
|
|
|
|
// recommended, because it will return at most one of the host name's
|
|
|
|
// IP addresses.
|
|
|
|
//
|
|
|
|
// See func Dial for a description of the network and address
|
|
|
|
// parameters.
|
|
|
|
func ResolveUDPAddr(network, address string) (*UDPAddr, error) {
|
|
|
|
switch network {
|
2012-12-13 00:13:29 +01:00
|
|
|
case "udp", "udp4", "udp6":
|
|
|
|
case "": // a hint wildcard for Go 1.0 undocumented behavior
|
2017-09-14 19:11:35 +02:00
|
|
|
network = "udp"
|
2012-12-13 00:13:29 +01:00
|
|
|
default:
|
2017-09-14 19:11:35 +02:00
|
|
|
return nil, UnknownNetworkError(network)
|
2012-12-13 00:13:29 +01:00
|
|
|
}
|
2017-09-14 19:11:35 +02:00
|
|
|
addrs, err := DefaultResolver.internetAddrList(context.Background(), network, address)
|
2010-12-03 05:34:57 +01:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2017-09-14 19:11:35 +02:00
|
|
|
return addrs.forResolve(network, address).(*UDPAddr), nil
|
2010-12-03 05:34:57 +01:00
|
|
|
}
|
2016-07-22 20:15:38 +02:00
|
|
|
|
|
|
|
// UDPConn is the implementation of the Conn and PacketConn interfaces
|
|
|
|
// for UDP network connections.
|
|
|
|
type UDPConn struct {
|
|
|
|
conn
|
|
|
|
}
|
|
|
|
|
2017-09-14 19:11:35 +02:00
|
|
|
// SyscallConn returns a raw network connection.
|
|
|
|
// This implements the syscall.Conn interface.
|
|
|
|
func (c *UDPConn) SyscallConn() (syscall.RawConn, error) {
|
|
|
|
if !c.ok() {
|
|
|
|
return nil, syscall.EINVAL
|
|
|
|
}
|
|
|
|
return newRawConn(c.fd)
|
|
|
|
}
|
|
|
|
|
|
|
|
// ReadFromUDP acts like ReadFrom but returns a UDPAddr.
|
2016-07-22 20:15:38 +02:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2017-09-14 19:11:35 +02:00
|
|
|
// ReadMsgUDP reads a message 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 message and the source address of the message.
|
|
|
|
//
|
|
|
|
// The packages golang.org/x/net/ipv4 and golang.org/x/net/ipv6 can be
|
|
|
|
// used to manipulate IP-level socket options in oob.
|
2016-07-22 20:15:38 +02:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2017-09-14 19:11:35 +02:00
|
|
|
// WriteToUDP acts like WriteTo but takes a UDPAddr.
|
2016-07-22 20:15:38 +02:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2017-09-14 19:11:35 +02:00
|
|
|
// WriteMsgUDP writes a message to addr via c if c isn't connected, or
|
|
|
|
// to c's remote 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.
|
|
|
|
//
|
|
|
|
// The packages golang.org/x/net/ipv4 and golang.org/x/net/ipv6 can be
|
|
|
|
// used to manipulate IP-level socket options in oob.
|
2016-07-22 20:15:38 +02:00
|
|
|
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}} }
|
|
|
|
|
2017-09-14 19:11:35 +02:00
|
|
|
// DialUDP acts like Dial for UDP networks.
|
|
|
|
//
|
|
|
|
// The network must be a UDP network name; see func Dial for details.
|
|
|
|
//
|
|
|
|
// If laddr is nil, a local address is automatically chosen.
|
|
|
|
// If the IP field of raddr is nil or an unspecified IP address, the
|
|
|
|
// local system is assumed.
|
|
|
|
func DialUDP(network string, laddr, raddr *UDPAddr) (*UDPConn, error) {
|
|
|
|
switch network {
|
2016-07-22 20:15:38 +02:00
|
|
|
case "udp", "udp4", "udp6":
|
|
|
|
default:
|
2017-09-14 19:11:35 +02:00
|
|
|
return nil, &OpError{Op: "dial", Net: network, Source: laddr.opAddr(), Addr: raddr.opAddr(), Err: UnknownNetworkError(network)}
|
2016-07-22 20:15:38 +02:00
|
|
|
}
|
|
|
|
if raddr == nil {
|
2017-09-14 19:11:35 +02:00
|
|
|
return nil, &OpError{Op: "dial", Net: network, Source: laddr.opAddr(), Addr: nil, Err: errMissingAddress}
|
2016-07-22 20:15:38 +02:00
|
|
|
}
|
2018-09-24 23:46:21 +02:00
|
|
|
sd := &sysDialer{network: network, address: raddr.String()}
|
|
|
|
c, err := sd.dialUDP(context.Background(), laddr, raddr)
|
2016-07-22 20:15:38 +02:00
|
|
|
if err != nil {
|
2017-09-14 19:11:35 +02:00
|
|
|
return nil, &OpError{Op: "dial", Net: network, Source: laddr.opAddr(), Addr: raddr.opAddr(), Err: err}
|
2016-07-22 20:15:38 +02:00
|
|
|
}
|
|
|
|
return c, nil
|
|
|
|
}
|
|
|
|
|
2017-09-14 19:11:35 +02:00
|
|
|
// ListenUDP acts like ListenPacket for UDP networks.
|
|
|
|
//
|
|
|
|
// The network must be a UDP network name; see func Dial for details.
|
|
|
|
//
|
|
|
|
// If the IP field of laddr is nil or an unspecified IP address,
|
|
|
|
// ListenUDP listens on all available IP addresses of the local system
|
|
|
|
// except multicast IP addresses.
|
|
|
|
// If the Port field of laddr is 0, a port number is automatically
|
|
|
|
// chosen.
|
|
|
|
func ListenUDP(network string, laddr *UDPAddr) (*UDPConn, error) {
|
|
|
|
switch network {
|
2016-07-22 20:15:38 +02:00
|
|
|
case "udp", "udp4", "udp6":
|
|
|
|
default:
|
2017-09-14 19:11:35 +02:00
|
|
|
return nil, &OpError{Op: "listen", Net: network, Source: nil, Addr: laddr.opAddr(), Err: UnknownNetworkError(network)}
|
2016-07-22 20:15:38 +02:00
|
|
|
}
|
|
|
|
if laddr == nil {
|
|
|
|
laddr = &UDPAddr{}
|
|
|
|
}
|
2018-09-24 23:46:21 +02:00
|
|
|
sl := &sysListener{network: network, address: laddr.String()}
|
|
|
|
c, err := sl.listenUDP(context.Background(), laddr)
|
2016-07-22 20:15:38 +02:00
|
|
|
if err != nil {
|
2017-09-14 19:11:35 +02:00
|
|
|
return nil, &OpError{Op: "listen", Net: network, Source: nil, Addr: laddr.opAddr(), Err: err}
|
2016-07-22 20:15:38 +02:00
|
|
|
}
|
|
|
|
return c, nil
|
|
|
|
}
|
|
|
|
|
2017-09-14 19:11:35 +02:00
|
|
|
// ListenMulticastUDP acts like ListenPacket for UDP networks but
|
|
|
|
// takes a group address on a specific network interface.
|
|
|
|
//
|
|
|
|
// The network must be a UDP network name; see func Dial for details.
|
|
|
|
//
|
|
|
|
// ListenMulticastUDP listens on all available IP addresses of the
|
|
|
|
// local system including the group, multicast IP address.
|
|
|
|
// If ifi is nil, ListenMulticastUDP uses the system-assigned
|
|
|
|
// multicast interface, although this is not recommended because the
|
2016-07-22 20:15:38 +02:00
|
|
|
// assignment depends on platforms and sometimes it might require
|
|
|
|
// routing configuration.
|
2017-09-14 19:11:35 +02:00
|
|
|
// If the Port field of gaddr is 0, a port number is automatically
|
|
|
|
// chosen.
|
2016-07-22 20:15:38 +02:00
|
|
|
//
|
|
|
|
// 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.
|
2020-12-23 18:57:37 +01:00
|
|
|
//
|
|
|
|
// Note that ListenMulticastUDP will set the IP_MULTICAST_LOOP socket option
|
|
|
|
// to 0 under IPPROTO_IP, to disable loopback of multicast packets.
|
2016-07-22 20:15:38 +02:00
|
|
|
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}
|
|
|
|
}
|
2018-09-24 23:46:21 +02:00
|
|
|
sl := &sysListener{network: network, address: gaddr.String()}
|
|
|
|
c, err := sl.listenMulticastUDP(context.Background(), ifi, gaddr)
|
2016-07-22 20:15:38 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, &OpError{Op: "listen", Net: network, Source: nil, Addr: gaddr.opAddr(), Err: err}
|
|
|
|
}
|
|
|
|
return c, nil
|
|
|
|
}
|