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
|
|
|
|
|
|
|
|
import (
|
2009-12-15 16:35:38 -07:00
|
|
|
"os"
|
|
|
|
"syscall"
|
2009-11-02 19:37:30 -07:00
|
|
|
)
|
|
|
|
|
|
|
|
func sockaddrToUDP(sa syscall.Sockaddr) Addr {
|
|
|
|
switch sa := sa.(type) {
|
|
|
|
case *syscall.SockaddrInet4:
|
2010-06-08 18:51:57 -06:00
|
|
|
return &UDPAddr{sa.Addr[0:], sa.Port}
|
2009-11-02 19:37:30 -07:00
|
|
|
case *syscall.SockaddrInet6:
|
2010-06-08 18:51:57 -06:00
|
|
|
return &UDPAddr{sa.Addr[0:], sa.Port}
|
2009-11-02 19:37:30 -07:00
|
|
|
}
|
2009-12-15 16:35:38 -07:00
|
|
|
return nil
|
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
|
|
|
|
2009-12-15 16:35:38 -07:00
|
|
|
func (a *UDPAddr) String() string { return joinHostPort(a.IP.String(), itoa(a.Port)) }
|
2009-11-02 19:37:30 -07:00
|
|
|
|
|
|
|
func (a *UDPAddr) family() int {
|
|
|
|
if a == nil || len(a.IP) <= 4 {
|
2009-11-09 13:07:39 -07:00
|
|
|
return syscall.AF_INET
|
2009-11-02 19:37:30 -07:00
|
|
|
}
|
|
|
|
if ip := a.IP.To4(); ip != nil {
|
2009-11-09 13:07:39 -07:00
|
|
|
return syscall.AF_INET
|
2009-11-02 19:37:30 -07:00
|
|
|
}
|
2009-12-15 16:35:38 -07:00
|
|
|
return syscall.AF_INET6
|
2009-11-02 19:37:30 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
func (a *UDPAddr) sockaddr(family int) (syscall.Sockaddr, os.Error) {
|
2009-11-09 13:07:39 -07:00
|
|
|
return ipToSockaddr(family, a.IP, a.Port)
|
2009-11-02 19:37:30 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
func (a *UDPAddr) toAddr() sockaddr {
|
2009-12-15 16:35:38 -07:00
|
|
|
if a == nil { // nil *UDPAddr
|
|
|
|
return nil // nil interface
|
2009-11-02 19:37:30 -07:00
|
|
|
}
|
2009-12-15 16:35:38 -07:00
|
|
|
return a
|
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
|
|
|
|
// numeric addresses. A literal IPv6 host address must be
|
|
|
|
// enclosed in square brackets, as in "[::]:80".
|
|
|
|
func ResolveUDPAddr(addr string) (*UDPAddr, os.Error) {
|
2009-12-15 16:35:38 -07:00
|
|
|
ip, port, err := hostPortToIP("udp", 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
|
|
|
}
|
2009-12-15 16:35:38 -07:00
|
|
|
return &UDPAddr{ip, port}, nil
|
2009-11-02 19:37:30 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// UDPConn is the implementation of the Conn and PacketConn
|
|
|
|
// interfaces for UDP network connections.
|
|
|
|
type UDPConn struct {
|
2009-12-15 16:35:38 -07:00
|
|
|
fd *netFD
|
2009-11-02 19:37:30 -07:00
|
|
|
}
|
|
|
|
|
2010-01-18 16:59:32 -07:00
|
|
|
func newUDPConn(fd *netFD) *UDPConn { return &UDPConn{fd} }
|
2009-11-02 19:37:30 -07:00
|
|
|
|
2009-12-15 16:35:38 -07:00
|
|
|
func (c *UDPConn) ok() bool { return c != nil && c.fd != nil }
|
2009-11-02 19:37:30 -07:00
|
|
|
|
|
|
|
// Implementation of the Conn interface - see Conn for documentation.
|
|
|
|
|
2010-04-26 23:15:25 -06:00
|
|
|
// Read implements the net.Conn Read method.
|
2009-11-02 19:37:30 -07:00
|
|
|
func (c *UDPConn) Read(b []byte) (n int, err os.Error) {
|
|
|
|
if !c.ok() {
|
2009-11-09 13:07:39 -07:00
|
|
|
return 0, os.EINVAL
|
2009-11-02 19:37:30 -07:00
|
|
|
}
|
2009-12-15 16:35:38 -07:00
|
|
|
return c.fd.Read(b)
|
2009-11-02 19:37:30 -07:00
|
|
|
}
|
|
|
|
|
2010-04-26 23:15:25 -06:00
|
|
|
// Write implements the net.Conn Write method.
|
2009-11-02 19:37:30 -07:00
|
|
|
func (c *UDPConn) Write(b []byte) (n int, err os.Error) {
|
|
|
|
if !c.ok() {
|
2009-11-09 13:07:39 -07:00
|
|
|
return 0, os.EINVAL
|
2009-11-02 19:37:30 -07:00
|
|
|
}
|
2009-12-15 16:35:38 -07:00
|
|
|
return c.fd.Write(b)
|
2009-11-02 19:37:30 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// Close closes the UDP connection.
|
|
|
|
func (c *UDPConn) Close() os.Error {
|
|
|
|
if !c.ok() {
|
2009-11-09 13:07:39 -07:00
|
|
|
return os.EINVAL
|
2009-11-02 19:37:30 -07:00
|
|
|
}
|
2009-12-15 16:35:38 -07:00
|
|
|
err := c.fd.Close()
|
|
|
|
c.fd = nil
|
|
|
|
return err
|
2009-11-02 19:37:30 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// LocalAddr returns the local network address.
|
|
|
|
func (c *UDPConn) LocalAddr() Addr {
|
|
|
|
if !c.ok() {
|
2009-11-09 13:07:39 -07:00
|
|
|
return nil
|
2009-11-02 19:37:30 -07:00
|
|
|
}
|
2009-12-15 16:35:38 -07:00
|
|
|
return c.fd.laddr
|
2009-11-02 19:37:30 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// RemoteAddr returns the remote network address, a *UDPAddr.
|
|
|
|
func (c *UDPConn) RemoteAddr() Addr {
|
|
|
|
if !c.ok() {
|
2009-11-09 13:07:39 -07:00
|
|
|
return nil
|
2009-11-02 19:37:30 -07:00
|
|
|
}
|
2009-12-15 16:35:38 -07:00
|
|
|
return c.fd.raddr
|
2009-11-02 19:37:30 -07:00
|
|
|
}
|
|
|
|
|
2010-04-26 23:15:25 -06:00
|
|
|
// SetTimeout implements the net.Conn SetTimeout method.
|
2009-11-02 19:37:30 -07:00
|
|
|
func (c *UDPConn) SetTimeout(nsec int64) os.Error {
|
|
|
|
if !c.ok() {
|
2009-11-09 13:07:39 -07:00
|
|
|
return os.EINVAL
|
2009-11-02 19:37:30 -07:00
|
|
|
}
|
2009-12-15 16:35:38 -07:00
|
|
|
return setTimeout(c.fd, nsec)
|
2009-11-02 19:37:30 -07:00
|
|
|
}
|
|
|
|
|
2010-04-26 23:15:25 -06:00
|
|
|
// SetReadTimeout implements the net.Conn SetReadTimeout method.
|
2009-11-02 19:37:30 -07:00
|
|
|
func (c *UDPConn) SetReadTimeout(nsec int64) os.Error {
|
|
|
|
if !c.ok() {
|
2009-11-09 13:07:39 -07:00
|
|
|
return os.EINVAL
|
2009-11-02 19:37:30 -07:00
|
|
|
}
|
2009-12-15 16:35:38 -07:00
|
|
|
return setReadTimeout(c.fd, nsec)
|
2009-11-02 19:37:30 -07:00
|
|
|
}
|
|
|
|
|
2010-04-26 23:15:25 -06:00
|
|
|
// SetWriteTimeout implements the net.Conn SetWriteTimeout method.
|
2009-11-02 19:37:30 -07:00
|
|
|
func (c *UDPConn) SetWriteTimeout(nsec int64) os.Error {
|
|
|
|
if !c.ok() {
|
2009-11-09 13:07:39 -07:00
|
|
|
return os.EINVAL
|
2009-11-02 19:37:30 -07:00
|
|
|
}
|
2009-12-15 16:35:38 -07:00
|
|
|
return setWriteTimeout(c.fd, nsec)
|
2009-11-02 19:37:30 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// SetReadBuffer sets the size of the operating system's
|
|
|
|
// receive buffer associated with the connection.
|
|
|
|
func (c *UDPConn) SetReadBuffer(bytes int) os.Error {
|
|
|
|
if !c.ok() {
|
2009-11-09 13:07:39 -07:00
|
|
|
return os.EINVAL
|
2009-11-02 19:37:30 -07:00
|
|
|
}
|
2009-12-15 16:35:38 -07:00
|
|
|
return setReadBuffer(c.fd, bytes)
|
2009-11-02 19:37:30 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// SetWriteBuffer sets the size of the operating system's
|
|
|
|
// transmit buffer associated with the connection.
|
|
|
|
func (c *UDPConn) SetWriteBuffer(bytes int) os.Error {
|
|
|
|
if !c.ok() {
|
2009-11-09 13:07:39 -07:00
|
|
|
return os.EINVAL
|
2009-11-02 19:37:30 -07:00
|
|
|
}
|
2009-12-15 16:35:38 -07:00
|
|
|
return setWriteBuffer(c.fd, bytes)
|
2009-11-02 19:37:30 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// UDP-specific methods.
|
|
|
|
|
|
|
|
// 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.
|
|
|
|
//
|
2010-04-26 23:15:25 -06:00
|
|
|
// ReadFromUDP can be made to time out and return an error with Timeout() == true
|
2009-11-02 19:37:30 -07:00
|
|
|
// after a fixed time limit; see SetTimeout and SetReadTimeout.
|
|
|
|
func (c *UDPConn) ReadFromUDP(b []byte) (n int, addr *UDPAddr, err os.Error) {
|
|
|
|
if !c.ok() {
|
2009-11-09 13:07:39 -07:00
|
|
|
return 0, nil, os.EINVAL
|
2009-11-02 19:37:30 -07:00
|
|
|
}
|
2009-12-15 16:35:38 -07:00
|
|
|
n, sa, err := c.fd.ReadFrom(b)
|
2009-11-02 19:37:30 -07:00
|
|
|
switch sa := sa.(type) {
|
|
|
|
case *syscall.SockaddrInet4:
|
2010-06-08 18:51:57 -06:00
|
|
|
addr = &UDPAddr{sa.Addr[0:], sa.Port}
|
2009-11-02 19:37:30 -07:00
|
|
|
case *syscall.SockaddrInet6:
|
2010-06-08 18:51:57 -06:00
|
|
|
addr = &UDPAddr{sa.Addr[0:], sa.Port}
|
2009-11-02 19:37:30 -07:00
|
|
|
}
|
2009-12-15 16:35:38 -07:00
|
|
|
return
|
2009-11-02 19:37:30 -07:00
|
|
|
}
|
|
|
|
|
2010-04-26 23:15:25 -06:00
|
|
|
// ReadFrom implements the net.PacketConn ReadFrom method.
|
2009-11-02 19:37:30 -07:00
|
|
|
func (c *UDPConn) ReadFrom(b []byte) (n int, addr Addr, err os.Error) {
|
|
|
|
if !c.ok() {
|
2009-11-09 13:07:39 -07:00
|
|
|
return 0, nil, os.EINVAL
|
2009-11-02 19:37:30 -07:00
|
|
|
}
|
2009-12-15 16:35:38 -07:00
|
|
|
n, uaddr, err := c.ReadFromUDP(b)
|
|
|
|
return n, uaddr.toAddr(), err
|
2009-11-02 19:37:30 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// WriteToUDP writes a UDP packet to addr via c, copying the payload from b.
|
|
|
|
//
|
2010-04-26 23:15:25 -06:00
|
|
|
// WriteToUDP can be made to time out and return
|
|
|
|
// an error with Timeout() == true after a fixed time limit;
|
|
|
|
// see SetTimeout and SetWriteTimeout.
|
|
|
|
// On packet-oriented connections, write timeouts are rare.
|
2009-11-02 19:37:30 -07:00
|
|
|
func (c *UDPConn) WriteToUDP(b []byte, addr *UDPAddr) (n int, err os.Error) {
|
|
|
|
if !c.ok() {
|
2009-11-09 13:07:39 -07:00
|
|
|
return 0, os.EINVAL
|
2009-11-02 19:37:30 -07:00
|
|
|
}
|
2010-04-26 23:15:25 -06:00
|
|
|
sa, err1 := addr.sockaddr(c.fd.family)
|
|
|
|
if err1 != nil {
|
|
|
|
return 0, &OpError{Op: "write", Net: "udp", Addr: addr, Error: err1}
|
2009-11-02 19:37:30 -07:00
|
|
|
}
|
2009-12-15 16:35:38 -07:00
|
|
|
return c.fd.WriteTo(b, sa)
|
2009-11-02 19:37:30 -07:00
|
|
|
}
|
|
|
|
|
2010-04-26 23:15:25 -06:00
|
|
|
// WriteTo implements the net.PacketConn WriteTo method.
|
2009-11-02 19:37:30 -07:00
|
|
|
func (c *UDPConn) WriteTo(b []byte, addr Addr) (n int, err os.Error) {
|
|
|
|
if !c.ok() {
|
2009-11-09 13:07:39 -07:00
|
|
|
return 0, os.EINVAL
|
2009-11-02 19:37:30 -07:00
|
|
|
}
|
2009-12-15 16:35:38 -07:00
|
|
|
a, ok := addr.(*UDPAddr)
|
2009-11-02 19:37:30 -07:00
|
|
|
if !ok {
|
2009-11-09 13:07:39 -07:00
|
|
|
return 0, &OpError{"writeto", "udp", addr, os.EINVAL}
|
2009-11-02 19:37:30 -07:00
|
|
|
}
|
2009-12-15 16:35:38 -07:00
|
|
|
return c.WriteToUDP(b, a)
|
2009-11-02 19:37:30 -07: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.
|
|
|
|
func DialUDP(net string, laddr, raddr *UDPAddr) (c *UDPConn, err os.Error) {
|
|
|
|
switch net {
|
|
|
|
case "udp", "udp4", "udp6":
|
|
|
|
default:
|
2009-11-09 13:07:39 -07:00
|
|
|
return nil, UnknownNetworkError(net)
|
2009-11-02 19:37:30 -07:00
|
|
|
}
|
|
|
|
if raddr == nil {
|
2009-11-09 13:07:39 -07:00
|
|
|
return nil, &OpError{"dial", "udp", nil, errMissingAddress}
|
2009-11-02 19:37:30 -07:00
|
|
|
}
|
2010-05-21 18:30:40 -06:00
|
|
|
fd, e := internetSocket(net, laddr.toAddr(), raddr.toAddr(), syscall.SOCK_DGRAM, 0, "dial", sockaddrToUDP)
|
2009-11-02 19:37:30 -07:00
|
|
|
if e != nil {
|
2009-11-09 13:07:39 -07:00
|
|
|
return nil, e
|
2009-11-02 19:37:30 -07:00
|
|
|
}
|
2009-12-15 16:35:38 -07:00
|
|
|
return newUDPConn(fd), nil
|
2009-11-02 19:37:30 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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.
|
|
|
|
func ListenUDP(net string, laddr *UDPAddr) (c *UDPConn, err os.Error) {
|
|
|
|
switch net {
|
|
|
|
case "udp", "udp4", "udp6":
|
|
|
|
default:
|
2009-11-09 13:07:39 -07:00
|
|
|
return nil, UnknownNetworkError(net)
|
2009-11-02 19:37:30 -07:00
|
|
|
}
|
|
|
|
if laddr == nil {
|
2009-11-09 13:07:39 -07:00
|
|
|
return nil, &OpError{"listen", "udp", nil, errMissingAddress}
|
2009-11-02 19:37:30 -07:00
|
|
|
}
|
2010-05-21 18:30:40 -06:00
|
|
|
fd, e := internetSocket(net, laddr.toAddr(), nil, syscall.SOCK_DGRAM, 0, "dial", sockaddrToUDP)
|
2009-11-02 19:37:30 -07:00
|
|
|
if e != nil {
|
2009-11-09 13:07:39 -07:00
|
|
|
return nil, e
|
2009-11-02 19:37:30 -07:00
|
|
|
}
|
2009-12-15 16:35:38 -07:00
|
|
|
return newUDPConn(fd), nil
|
2009-11-02 19:37:30 -07:00
|
|
|
}
|
2010-05-24 17:53:23 -06:00
|
|
|
|
|
|
|
// BindToDevice binds a UDPConn to a network interface.
|
|
|
|
func (c *UDPConn) BindToDevice(device string) os.Error {
|
|
|
|
if !c.ok() {
|
|
|
|
return os.EINVAL
|
|
|
|
}
|
|
|
|
c.fd.incref()
|
|
|
|
defer c.fd.decref()
|
|
|
|
return os.NewSyscallError("setsockopt", syscall.BindToDevice(c.fd.sysfd, device))
|
|
|
|
}
|