2011-09-16 17:47:21 +02: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.
|
|
|
|
|
2012-01-13 06:11:45 +01:00
|
|
|
// +build darwin freebsd linux netbsd openbsd windows
|
2011-10-27 01:57:58 +02:00
|
|
|
|
2012-12-13 00:13:29 +01:00
|
|
|
// UDP sockets for POSIX
|
2011-09-16 17:47:21 +02:00
|
|
|
|
|
|
|
package net
|
|
|
|
|
2012-11-21 08:03:38 +01:00
|
|
|
import (
|
|
|
|
"syscall"
|
|
|
|
"time"
|
|
|
|
)
|
2012-02-01 20:26:59 +01:00
|
|
|
|
2011-09-16 17:47:21 +02:00
|
|
|
func sockaddrToUDP(sa syscall.Sockaddr) Addr {
|
|
|
|
switch sa := sa.(type) {
|
|
|
|
case *syscall.SockaddrInet4:
|
2012-12-13 00:13:29 +01:00
|
|
|
return &UDPAddr{IP: sa.Addr[0:], Port: sa.Port}
|
2011-09-16 17:47:21 +02:00
|
|
|
case *syscall.SockaddrInet6:
|
2012-12-13 00:13:29 +01:00
|
|
|
return &UDPAddr{IP: sa.Addr[0:], Port: sa.Port, Zone: zoneToString(int(sa.ZoneId))}
|
2011-09-16 17:47:21 +02:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a *UDPAddr) family() int {
|
2011-10-27 01:57:58 +02:00
|
|
|
if a == nil || len(a.IP) <= IPv4len {
|
2011-09-16 17:47:21 +02:00
|
|
|
return syscall.AF_INET
|
|
|
|
}
|
|
|
|
if a.IP.To4() != nil {
|
|
|
|
return syscall.AF_INET
|
|
|
|
}
|
|
|
|
return syscall.AF_INET6
|
|
|
|
}
|
|
|
|
|
2012-03-30 23:27:11 +02:00
|
|
|
func (a *UDPAddr) isWildcard() bool {
|
|
|
|
if a == nil || a.IP == nil {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
return a.IP.IsUnspecified()
|
|
|
|
}
|
|
|
|
|
2011-12-03 03:17:34 +01:00
|
|
|
func (a *UDPAddr) sockaddr(family int) (syscall.Sockaddr, error) {
|
2012-12-13 00:13:29 +01:00
|
|
|
return ipToSockaddr(family, a.IP, a.Port, a.Zone)
|
2011-09-16 17:47:21 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (a *UDPAddr) toAddr() sockaddr {
|
|
|
|
if a == nil { // nil *UDPAddr
|
|
|
|
return nil // nil interface
|
|
|
|
}
|
|
|
|
return a
|
|
|
|
}
|
|
|
|
|
|
|
|
// UDPConn is the implementation of the Conn and PacketConn
|
|
|
|
// interfaces for UDP network connections.
|
|
|
|
type UDPConn struct {
|
2012-10-23 06:31:11 +02:00
|
|
|
conn
|
2011-09-16 17:47:21 +02:00
|
|
|
}
|
|
|
|
|
2012-10-23 06:31:11 +02:00
|
|
|
func newUDPConn(fd *netFD) *UDPConn { return &UDPConn{conn{fd}} }
|
2011-09-16 17:47:21 +02:00
|
|
|
|
|
|
|
// 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
|
2012-01-25 22:54:22 +01:00
|
|
|
// after a fixed time limit; see SetDeadline and SetReadDeadline.
|
2011-12-03 03:17:34 +01:00
|
|
|
func (c *UDPConn) ReadFromUDP(b []byte) (n int, addr *UDPAddr, err error) {
|
2011-09-16 17:47:21 +02:00
|
|
|
if !c.ok() {
|
2012-03-02 21:01:37 +01:00
|
|
|
return 0, nil, syscall.EINVAL
|
2011-09-16 17:47:21 +02:00
|
|
|
}
|
|
|
|
n, sa, err := c.fd.ReadFrom(b)
|
|
|
|
switch sa := sa.(type) {
|
|
|
|
case *syscall.SockaddrInet4:
|
2012-12-13 00:13:29 +01:00
|
|
|
addr = &UDPAddr{IP: sa.Addr[0:], Port: sa.Port}
|
2011-09-16 17:47:21 +02:00
|
|
|
case *syscall.SockaddrInet6:
|
2012-12-13 00:13:29 +01:00
|
|
|
addr = &UDPAddr{IP: sa.Addr[0:], Port: sa.Port, Zone: zoneToString(int(sa.ZoneId))}
|
2011-09-16 17:47:21 +02:00
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2012-02-09 09:19:58 +01:00
|
|
|
// ReadFrom implements the PacketConn ReadFrom method.
|
|
|
|
func (c *UDPConn) ReadFrom(b []byte) (int, Addr, error) {
|
2011-09-16 17:47:21 +02:00
|
|
|
if !c.ok() {
|
2012-03-02 21:01:37 +01:00
|
|
|
return 0, nil, syscall.EINVAL
|
2011-09-16 17:47:21 +02:00
|
|
|
}
|
2012-12-13 00:13:29 +01:00
|
|
|
n, addr, err := c.ReadFromUDP(b)
|
|
|
|
return n, addr.toAddr(), err
|
2011-09-16 17:47:21 +02:00
|
|
|
}
|
|
|
|
|
2012-10-23 06:31:11 +02:00
|
|
|
// ReadMsgUDP reads a packet from c, copying the payload into b and
|
|
|
|
// the associdated 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
|
|
|
|
}
|
|
|
|
var sa syscall.Sockaddr
|
|
|
|
n, oobn, flags, sa, err = c.fd.ReadMsg(b, oob)
|
|
|
|
switch sa := sa.(type) {
|
|
|
|
case *syscall.SockaddrInet4:
|
2012-12-13 00:13:29 +01:00
|
|
|
addr = &UDPAddr{IP: sa.Addr[0:], Port: sa.Port}
|
2012-10-23 06:31:11 +02:00
|
|
|
case *syscall.SockaddrInet6:
|
2012-12-13 00:13:29 +01:00
|
|
|
addr = &UDPAddr{IP: sa.Addr[0:], Port: sa.Port, Zone: zoneToString(int(sa.ZoneId))}
|
2012-10-23 06:31:11 +02:00
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2011-09-16 17:47:21 +02:00
|
|
|
// 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;
|
2012-01-25 22:54:22 +01:00
|
|
|
// see SetDeadline and SetWriteDeadline.
|
2011-09-16 17:47:21 +02:00
|
|
|
// On packet-oriented connections, write timeouts are rare.
|
2012-02-01 20:26:59 +01:00
|
|
|
func (c *UDPConn) WriteToUDP(b []byte, addr *UDPAddr) (int, error) {
|
2011-09-16 17:47:21 +02:00
|
|
|
if !c.ok() {
|
2012-03-02 21:01:37 +01:00
|
|
|
return 0, syscall.EINVAL
|
2011-09-16 17:47:21 +02:00
|
|
|
}
|
2012-02-01 20:26:59 +01:00
|
|
|
if c.fd.isConnected {
|
|
|
|
return 0, &OpError{"write", c.fd.net, addr, ErrWriteToConnected}
|
|
|
|
}
|
|
|
|
sa, err := addr.sockaddr(c.fd.family)
|
|
|
|
if err != nil {
|
|
|
|
return 0, &OpError{"write", c.fd.net, addr, err}
|
2011-09-16 17:47:21 +02:00
|
|
|
}
|
|
|
|
return c.fd.WriteTo(b, sa)
|
|
|
|
}
|
|
|
|
|
2012-02-09 09:19:58 +01:00
|
|
|
// WriteTo implements the PacketConn WriteTo method.
|
2012-02-01 20:26:59 +01:00
|
|
|
func (c *UDPConn) WriteTo(b []byte, addr Addr) (int, error) {
|
2011-09-16 17:47:21 +02:00
|
|
|
if !c.ok() {
|
2012-03-02 21:01:37 +01:00
|
|
|
return 0, syscall.EINVAL
|
2011-09-16 17:47:21 +02:00
|
|
|
}
|
|
|
|
a, ok := addr.(*UDPAddr)
|
|
|
|
if !ok {
|
2012-03-02 21:01:37 +01:00
|
|
|
return 0, &OpError{"write", c.fd.net, addr, syscall.EINVAL}
|
2011-09-16 17:47:21 +02:00
|
|
|
}
|
|
|
|
return c.WriteToUDP(b, a)
|
|
|
|
}
|
|
|
|
|
2012-10-23 06:31:11 +02:00
|
|
|
// WriteMsgUDP writes a packet to addr via c, copying the payload from
|
|
|
|
// b and the associated out-of-band data 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
|
|
|
|
}
|
|
|
|
if c.fd.isConnected {
|
|
|
|
return 0, 0, &OpError{"write", c.fd.net, addr, ErrWriteToConnected}
|
|
|
|
}
|
|
|
|
sa, err := addr.sockaddr(c.fd.family)
|
|
|
|
if err != nil {
|
|
|
|
return 0, 0, &OpError{"write", c.fd.net, addr, err}
|
|
|
|
}
|
|
|
|
return c.fd.WriteMsg(b, oob, sa)
|
|
|
|
}
|
2012-02-09 09:19:58 +01:00
|
|
|
|
2011-09-16 17:47:21 +02:00
|
|
|
// 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.
|
2012-02-09 09:19:58 +01:00
|
|
|
func DialUDP(net string, laddr, raddr *UDPAddr) (*UDPConn, error) {
|
2012-11-21 08:03:38 +01:00
|
|
|
return dialUDP(net, laddr, raddr, noDeadline)
|
|
|
|
}
|
|
|
|
|
|
|
|
func dialUDP(net string, laddr, raddr *UDPAddr, deadline time.Time) (*UDPConn, error) {
|
2011-09-16 17:47:21 +02:00
|
|
|
switch net {
|
|
|
|
case "udp", "udp4", "udp6":
|
|
|
|
default:
|
|
|
|
return nil, UnknownNetworkError(net)
|
|
|
|
}
|
|
|
|
if raddr == nil {
|
2012-02-01 20:26:59 +01:00
|
|
|
return nil, &OpError{"dial", net, nil, errMissingAddress}
|
2011-09-16 17:47:21 +02:00
|
|
|
}
|
2012-11-21 08:03:38 +01:00
|
|
|
fd, err := internetSocket(net, laddr.toAddr(), raddr.toAddr(), deadline, syscall.SOCK_DGRAM, 0, "dial", sockaddrToUDP)
|
2012-02-09 09:19:58 +01:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2011-09-16 17:47:21 +02:00
|
|
|
}
|
|
|
|
return newUDPConn(fd), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// ListenUDP listens for incoming UDP packets addressed to the
|
|
|
|
// local address laddr. The returned connection c's ReadFrom
|
|
|
|
// and WriteTo methods can be used to receive and send UDP
|
|
|
|
// packets with per-packet addressing.
|
2012-02-01 20:26:59 +01:00
|
|
|
func ListenUDP(net string, laddr *UDPAddr) (*UDPConn, error) {
|
2011-09-16 17:47:21 +02:00
|
|
|
switch net {
|
|
|
|
case "udp", "udp4", "udp6":
|
|
|
|
default:
|
|
|
|
return nil, UnknownNetworkError(net)
|
|
|
|
}
|
|
|
|
if laddr == nil {
|
2012-11-21 08:03:38 +01:00
|
|
|
laddr = &UDPAddr{}
|
2011-09-16 17:47:21 +02:00
|
|
|
}
|
2012-11-21 08:03:38 +01:00
|
|
|
fd, err := internetSocket(net, laddr.toAddr(), nil, noDeadline, syscall.SOCK_DGRAM, 0, "listen", sockaddrToUDP)
|
2012-02-01 20:26:59 +01:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2011-09-16 17:47:21 +02:00
|
|
|
}
|
|
|
|
return newUDPConn(fd), nil
|
|
|
|
}
|
|
|
|
|
2012-02-09 09:19:58 +01:00
|
|
|
// ListenMulticastUDP listens for incoming multicast UDP packets
|
|
|
|
// addressed to the group address gaddr on ifi, which specifies
|
|
|
|
// the interface to join. ListenMulticastUDP uses default
|
|
|
|
// multicast interface if ifi is nil.
|
|
|
|
func ListenMulticastUDP(net string, ifi *Interface, gaddr *UDPAddr) (*UDPConn, error) {
|
|
|
|
switch net {
|
|
|
|
case "udp", "udp4", "udp6":
|
|
|
|
default:
|
|
|
|
return nil, UnknownNetworkError(net)
|
|
|
|
}
|
|
|
|
if gaddr == nil || gaddr.IP == nil {
|
2013-01-29 21:52:43 +01:00
|
|
|
return nil, &OpError{"listen", net, nil, errMissingAddress}
|
2012-02-09 09:19:58 +01:00
|
|
|
}
|
2012-11-21 08:03:38 +01:00
|
|
|
fd, err := internetSocket(net, gaddr.toAddr(), nil, noDeadline, syscall.SOCK_DGRAM, 0, "listen", sockaddrToUDP)
|
2012-02-09 09:19:58 +01:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
c := newUDPConn(fd)
|
2013-01-29 21:52:43 +01:00
|
|
|
if ip4 := gaddr.IP.To4(); ip4 != nil {
|
|
|
|
if err := listenIPv4MulticastUDP(c, ifi, ip4); err != nil {
|
2012-02-09 09:19:58 +01:00
|
|
|
c.Close()
|
2013-01-29 21:52:43 +01:00
|
|
|
return nil, &OpError{"listen", net, &IPAddr{IP: ip4}, err}
|
2012-02-09 09:19:58 +01:00
|
|
|
}
|
|
|
|
} else {
|
2013-01-29 21:52:43 +01:00
|
|
|
if err := listenIPv6MulticastUDP(c, ifi, gaddr.IP); err != nil {
|
2012-02-09 09:19:58 +01:00
|
|
|
c.Close()
|
2013-01-29 21:52:43 +01:00
|
|
|
return nil, &OpError{"listen", net, &IPAddr{IP: gaddr.IP}, err}
|
2012-02-09 09:19:58 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return c, nil
|
|
|
|
}
|
2011-09-16 17:47:21 +02:00
|
|
|
|
2012-02-09 09:19:58 +01:00
|
|
|
func listenIPv4MulticastUDP(c *UDPConn, ifi *Interface, ip IP) error {
|
|
|
|
if ifi != nil {
|
2013-01-29 21:52:43 +01:00
|
|
|
if err := setIPv4MulticastInterface(c.fd, ifi); err != nil {
|
2012-02-09 09:19:58 +01:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
2013-01-29 21:52:43 +01:00
|
|
|
if err := setIPv4MulticastLoopback(c.fd, false); err != nil {
|
2012-02-09 09:19:58 +01:00
|
|
|
return err
|
2011-10-27 01:57:58 +02:00
|
|
|
}
|
2013-01-29 21:52:43 +01:00
|
|
|
if err := joinIPv4Group(c.fd, ifi, ip); err != nil {
|
2012-02-09 09:19:58 +01:00
|
|
|
return err
|
2011-10-27 01:57:58 +02:00
|
|
|
}
|
2012-02-09 09:19:58 +01:00
|
|
|
return nil
|
2011-10-27 01:57:58 +02:00
|
|
|
}
|
2011-09-16 17:47:21 +02:00
|
|
|
|
2012-02-09 09:19:58 +01:00
|
|
|
func listenIPv6MulticastUDP(c *UDPConn, ifi *Interface, ip IP) error {
|
|
|
|
if ifi != nil {
|
2013-01-29 21:52:43 +01:00
|
|
|
if err := setIPv6MulticastInterface(c.fd, ifi); err != nil {
|
2012-02-09 09:19:58 +01:00
|
|
|
return err
|
|
|
|
}
|
2011-09-16 17:47:21 +02:00
|
|
|
}
|
2013-01-29 21:52:43 +01:00
|
|
|
if err := setIPv6MulticastLoopback(c.fd, false); err != nil {
|
2012-02-09 09:19:58 +01:00
|
|
|
return err
|
|
|
|
}
|
2013-01-29 21:52:43 +01:00
|
|
|
if err := joinIPv6Group(c.fd, ifi, ip); err != nil {
|
2012-02-09 09:19:58 +01:00
|
|
|
return err
|
2011-09-16 17:47:21 +02:00
|
|
|
}
|
2012-02-09 09:19:58 +01:00
|
|
|
return nil
|
2011-10-27 01:57:58 +02:00
|
|
|
}
|