github.com/mh-cbon/go@v0.0.0-20160603070303-9e112a3fe4c0/src/net/udpsock.go (about)

     1  // Copyright 2009 The Go Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package net
     6  
     7  import (
     8  	"context"
     9  	"syscall"
    10  )
    11  
    12  // UDPAddr represents the address of a UDP end point.
    13  type UDPAddr struct {
    14  	IP   IP
    15  	Port int
    16  	Zone string // IPv6 scoped addressing zone
    17  }
    18  
    19  // Network returns the address's network name, "udp".
    20  func (a *UDPAddr) Network() string { return "udp" }
    21  
    22  func (a *UDPAddr) String() string {
    23  	if a == nil {
    24  		return "<nil>"
    25  	}
    26  	ip := ipEmptyString(a.IP)
    27  	if a.Zone != "" {
    28  		return JoinHostPort(ip+"%"+a.Zone, itoa(a.Port))
    29  	}
    30  	return JoinHostPort(ip, itoa(a.Port))
    31  }
    32  
    33  func (a *UDPAddr) isWildcard() bool {
    34  	if a == nil || a.IP == nil {
    35  		return true
    36  	}
    37  	return a.IP.IsUnspecified()
    38  }
    39  
    40  func (a *UDPAddr) opAddr() Addr {
    41  	if a == nil {
    42  		return nil
    43  	}
    44  	return a
    45  }
    46  
    47  // ResolveUDPAddr parses addr as a UDP address of the form "host:port"
    48  // or "[ipv6-host%zone]:port" and resolves a pair of domain name and
    49  // port name on the network net, which must be "udp", "udp4" or
    50  // "udp6".  A literal address or host name for IPv6 must be enclosed
    51  // in square brackets, as in "[::1]:80", "[ipv6-host]:http" or
    52  // "[ipv6-host%zone]:80".
    53  func ResolveUDPAddr(net, addr string) (*UDPAddr, error) {
    54  	switch net {
    55  	case "udp", "udp4", "udp6":
    56  	case "": // a hint wildcard for Go 1.0 undocumented behavior
    57  		net = "udp"
    58  	default:
    59  		return nil, UnknownNetworkError(net)
    60  	}
    61  	addrs, err := internetAddrList(context.Background(), net, addr)
    62  	if err != nil {
    63  		return nil, err
    64  	}
    65  	return addrs.first(isIPv4).(*UDPAddr), nil
    66  }
    67  
    68  // UDPConn is the implementation of the Conn and PacketConn interfaces
    69  // for UDP network connections.
    70  type UDPConn struct {
    71  	conn
    72  }
    73  
    74  // ReadFromUDP reads a UDP packet from c, copying the payload into b.
    75  // It returns the number of bytes copied into b and the return address
    76  // that was on the packet.
    77  //
    78  // ReadFromUDP can be made to time out and return an error with
    79  // Timeout() == true after a fixed time limit; see SetDeadline and
    80  // SetReadDeadline.
    81  func (c *UDPConn) ReadFromUDP(b []byte) (int, *UDPAddr, error) {
    82  	if !c.ok() {
    83  		return 0, nil, syscall.EINVAL
    84  	}
    85  	n, addr, err := c.readFrom(b)
    86  	if err != nil {
    87  		err = &OpError{Op: "read", Net: c.fd.net, Source: c.fd.laddr, Addr: c.fd.raddr, Err: err}
    88  	}
    89  	return n, addr, err
    90  }
    91  
    92  // ReadFrom implements the PacketConn ReadFrom method.
    93  func (c *UDPConn) ReadFrom(b []byte) (int, Addr, error) {
    94  	if !c.ok() {
    95  		return 0, nil, syscall.EINVAL
    96  	}
    97  	n, addr, err := c.readFrom(b)
    98  	if err != nil {
    99  		err = &OpError{Op: "read", Net: c.fd.net, Source: c.fd.laddr, Addr: c.fd.raddr, Err: err}
   100  	}
   101  	if addr == nil {
   102  		return n, nil, err
   103  	}
   104  	return n, addr, err
   105  }
   106  
   107  // ReadMsgUDP reads a packet from c, copying the payload into b and
   108  // the associated out-of-band data into oob. It returns the number
   109  // of bytes copied into b, the number of bytes copied into oob, the
   110  // flags that were set on the packet and the source address of the
   111  // packet.
   112  func (c *UDPConn) ReadMsgUDP(b, oob []byte) (n, oobn, flags int, addr *UDPAddr, err error) {
   113  	if !c.ok() {
   114  		return 0, 0, 0, nil, syscall.EINVAL
   115  	}
   116  	n, oobn, flags, addr, err = c.readMsg(b, oob)
   117  	if err != nil {
   118  		err = &OpError{Op: "read", Net: c.fd.net, Source: c.fd.laddr, Addr: c.fd.raddr, Err: err}
   119  	}
   120  	return
   121  }
   122  
   123  // WriteToUDP writes a UDP packet to addr via c, copying the payload
   124  // from b.
   125  //
   126  // WriteToUDP can be made to time out and return an error with
   127  // Timeout() == true after a fixed time limit; see SetDeadline and
   128  // SetWriteDeadline. On packet-oriented connections, write timeouts
   129  // are rare.
   130  func (c *UDPConn) WriteToUDP(b []byte, addr *UDPAddr) (int, error) {
   131  	if !c.ok() {
   132  		return 0, syscall.EINVAL
   133  	}
   134  	n, err := c.writeTo(b, addr)
   135  	if err != nil {
   136  		err = &OpError{Op: "write", Net: c.fd.net, Source: c.fd.laddr, Addr: addr.opAddr(), Err: err}
   137  	}
   138  	return n, err
   139  }
   140  
   141  // WriteTo implements the PacketConn WriteTo method.
   142  func (c *UDPConn) WriteTo(b []byte, addr Addr) (int, error) {
   143  	if !c.ok() {
   144  		return 0, syscall.EINVAL
   145  	}
   146  	a, ok := addr.(*UDPAddr)
   147  	if !ok {
   148  		return 0, &OpError{Op: "write", Net: c.fd.net, Source: c.fd.laddr, Addr: addr, Err: syscall.EINVAL}
   149  	}
   150  	n, err := c.writeTo(b, a)
   151  	if err != nil {
   152  		err = &OpError{Op: "write", Net: c.fd.net, Source: c.fd.laddr, Addr: a.opAddr(), Err: err}
   153  	}
   154  	return n, err
   155  }
   156  
   157  // WriteMsgUDP writes a packet to addr via c if c isn't connected, or
   158  // to c's remote destination address if c is connected (in which case
   159  // addr must be nil).  The payload is copied from b and the associated
   160  // out-of-band data is copied from oob. It returns the number of
   161  // payload and out-of-band bytes written.
   162  func (c *UDPConn) WriteMsgUDP(b, oob []byte, addr *UDPAddr) (n, oobn int, err error) {
   163  	if !c.ok() {
   164  		return 0, 0, syscall.EINVAL
   165  	}
   166  	n, oobn, err = c.writeMsg(b, oob, addr)
   167  	if err != nil {
   168  		err = &OpError{Op: "write", Net: c.fd.net, Source: c.fd.laddr, Addr: addr.opAddr(), Err: err}
   169  	}
   170  	return
   171  }
   172  
   173  func newUDPConn(fd *netFD) *UDPConn { return &UDPConn{conn{fd}} }
   174  
   175  // DialUDP connects to the remote address raddr on the network net,
   176  // which must be "udp", "udp4", or "udp6".  If laddr is not nil, it is
   177  // used as the local address for the connection.
   178  func DialUDP(net string, laddr, raddr *UDPAddr) (*UDPConn, error) {
   179  	switch net {
   180  	case "udp", "udp4", "udp6":
   181  	default:
   182  		return nil, &OpError{Op: "dial", Net: net, Source: laddr.opAddr(), Addr: raddr.opAddr(), Err: UnknownNetworkError(net)}
   183  	}
   184  	if raddr == nil {
   185  		return nil, &OpError{Op: "dial", Net: net, Source: laddr.opAddr(), Addr: nil, Err: errMissingAddress}
   186  	}
   187  	c, err := dialUDP(context.Background(), net, laddr, raddr)
   188  	if err != nil {
   189  		return nil, &OpError{Op: "dial", Net: net, Source: laddr.opAddr(), Addr: raddr.opAddr(), Err: err}
   190  	}
   191  	return c, nil
   192  }
   193  
   194  // ListenUDP listens for incoming UDP packets addressed to the local
   195  // address laddr. Net must be "udp", "udp4", or "udp6".  If laddr has
   196  // a port of 0, ListenUDP will choose an available port.
   197  // The LocalAddr method of the returned UDPConn can be used to
   198  // discover the port. The returned connection's ReadFrom and WriteTo
   199  // methods can be used to receive and send UDP packets with per-packet
   200  // addressing.
   201  func ListenUDP(net string, laddr *UDPAddr) (*UDPConn, error) {
   202  	switch net {
   203  	case "udp", "udp4", "udp6":
   204  	default:
   205  		return nil, &OpError{Op: "listen", Net: net, Source: nil, Addr: laddr.opAddr(), Err: UnknownNetworkError(net)}
   206  	}
   207  	if laddr == nil {
   208  		laddr = &UDPAddr{}
   209  	}
   210  	c, err := listenUDP(context.Background(), net, laddr)
   211  	if err != nil {
   212  		return nil, &OpError{Op: "listen", Net: net, Source: nil, Addr: laddr.opAddr(), Err: err}
   213  	}
   214  	return c, nil
   215  }
   216  
   217  // ListenMulticastUDP listens for incoming multicast UDP packets
   218  // addressed to the group address gaddr on the interface ifi.
   219  // Network must be "udp", "udp4" or "udp6".
   220  // ListenMulticastUDP uses the system-assigned multicast interface
   221  // when ifi is nil, although this is not recommended because the
   222  // assignment depends on platforms and sometimes it might require
   223  // routing configuration.
   224  //
   225  // ListenMulticastUDP is just for convenience of simple, small
   226  // applications. There are golang.org/x/net/ipv4 and
   227  // golang.org/x/net/ipv6 packages for general purpose uses.
   228  func ListenMulticastUDP(network string, ifi *Interface, gaddr *UDPAddr) (*UDPConn, error) {
   229  	switch network {
   230  	case "udp", "udp4", "udp6":
   231  	default:
   232  		return nil, &OpError{Op: "listen", Net: network, Source: nil, Addr: gaddr.opAddr(), Err: UnknownNetworkError(network)}
   233  	}
   234  	if gaddr == nil || gaddr.IP == nil {
   235  		return nil, &OpError{Op: "listen", Net: network, Source: nil, Addr: gaddr.opAddr(), Err: errMissingAddress}
   236  	}
   237  	c, err := listenMulticastUDP(context.Background(), network, ifi, gaddr)
   238  	if err != nil {
   239  		return nil, &OpError{Op: "listen", Net: network, Source: nil, Addr: gaddr.opAddr(), Err: err}
   240  	}
   241  	return c, nil
   242  }