udpsock_plan9.go 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  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. package net
  5. import (
  6. "errors"
  7. "os"
  8. "syscall"
  9. "time"
  10. )
  11. // UDPConn is the implementation of the Conn and PacketConn interfaces
  12. // for UDP network connections.
  13. type UDPConn struct {
  14. conn
  15. }
  16. func newUDPConn(fd *netFD) *UDPConn { return &UDPConn{conn{fd}} }
  17. // ReadFromUDP reads a UDP packet from c, copying the payload into b.
  18. // It returns the number of bytes copied into b and the return address
  19. // that was on the packet.
  20. //
  21. // ReadFromUDP can be made to time out and return an error with
  22. // Timeout() == true after a fixed time limit; see SetDeadline and
  23. // SetReadDeadline.
  24. func (c *UDPConn) ReadFromUDP(b []byte) (n int, addr *UDPAddr, err error) {
  25. if !c.ok() || c.fd.data == nil {
  26. return 0, nil, syscall.EINVAL
  27. }
  28. buf := make([]byte, udpHeaderSize+len(b))
  29. m, err := c.fd.data.Read(buf)
  30. if err != nil {
  31. return
  32. }
  33. if m < udpHeaderSize {
  34. return 0, nil, errors.New("short read reading UDP header")
  35. }
  36. buf = buf[:m]
  37. h, buf := unmarshalUDPHeader(buf)
  38. n = copy(b, buf)
  39. return n, &UDPAddr{IP: h.raddr, Port: int(h.rport)}, nil
  40. }
  41. // ReadFrom implements the PacketConn ReadFrom method.
  42. func (c *UDPConn) ReadFrom(b []byte) (int, Addr, error) {
  43. if !c.ok() {
  44. return 0, nil, syscall.EINVAL
  45. }
  46. return c.ReadFromUDP(b)
  47. }
  48. // ReadMsgUDP reads a packet from c, copying the payload into b and
  49. // the associated out-of-band data into oob. It returns the number
  50. // of bytes copied into b, the number of bytes copied into oob, the
  51. // flags that were set on the packet and the source address of the
  52. // packet.
  53. func (c *UDPConn) ReadMsgUDP(b, oob []byte) (n, oobn, flags int, addr *UDPAddr, err error) {
  54. return 0, 0, 0, nil, syscall.EPLAN9
  55. }
  56. // WriteToUDP writes a UDP packet to addr via c, copying the payload
  57. // from b.
  58. //
  59. // WriteToUDP can be made to time out and return an error with
  60. // Timeout() == true after a fixed time limit; see SetDeadline and
  61. // SetWriteDeadline. On packet-oriented connections, write timeouts
  62. // are rare.
  63. func (c *UDPConn) WriteToUDP(b []byte, addr *UDPAddr) (int, error) {
  64. if !c.ok() || c.fd.data == nil {
  65. return 0, syscall.EINVAL
  66. }
  67. if addr == nil {
  68. return 0, &OpError{Op: "write", Net: c.fd.dir, Addr: nil, Err: errMissingAddress}
  69. }
  70. h := new(udpHeader)
  71. h.raddr = addr.IP.To16()
  72. h.laddr = c.fd.laddr.(*UDPAddr).IP.To16()
  73. h.ifcaddr = IPv6zero // ignored (receive only)
  74. h.rport = uint16(addr.Port)
  75. h.lport = uint16(c.fd.laddr.(*UDPAddr).Port)
  76. buf := make([]byte, udpHeaderSize+len(b))
  77. i := copy(buf, h.Bytes())
  78. copy(buf[i:], b)
  79. return c.fd.data.Write(buf)
  80. }
  81. // WriteTo implements the PacketConn WriteTo method.
  82. func (c *UDPConn) WriteTo(b []byte, addr Addr) (int, error) {
  83. if !c.ok() {
  84. return 0, syscall.EINVAL
  85. }
  86. a, ok := addr.(*UDPAddr)
  87. if !ok {
  88. return 0, &OpError{"write", c.fd.dir, addr, syscall.EINVAL}
  89. }
  90. return c.WriteToUDP(b, a)
  91. }
  92. // WriteMsgUDP writes a packet to addr via c, copying the payload from
  93. // b and the associated out-of-band data from oob. It returns the
  94. // number of payload and out-of-band bytes written.
  95. func (c *UDPConn) WriteMsgUDP(b, oob []byte, addr *UDPAddr) (n, oobn int, err error) {
  96. return 0, 0, syscall.EPLAN9
  97. }
  98. // DialUDP connects to the remote address raddr on the network net,
  99. // which must be "udp", "udp4", or "udp6". If laddr is not nil, it is
  100. // used as the local address for the connection.
  101. func DialUDP(net string, laddr, raddr *UDPAddr) (*UDPConn, error) {
  102. return dialUDP(net, laddr, raddr, noDeadline)
  103. }
  104. func dialUDP(net string, laddr, raddr *UDPAddr, deadline time.Time) (*UDPConn, error) {
  105. if !deadline.IsZero() {
  106. panic("net.dialUDP: deadline not implemented on Plan 9")
  107. }
  108. switch net {
  109. case "udp", "udp4", "udp6":
  110. default:
  111. return nil, UnknownNetworkError(net)
  112. }
  113. if raddr == nil {
  114. return nil, &OpError{"dial", net, nil, errMissingAddress}
  115. }
  116. fd, err := dialPlan9(net, laddr, raddr)
  117. if err != nil {
  118. return nil, err
  119. }
  120. return newUDPConn(fd), nil
  121. }
  122. const udpHeaderSize = 16*3 + 2*2
  123. type udpHeader struct {
  124. raddr, laddr, ifcaddr IP
  125. rport, lport uint16
  126. }
  127. func (h *udpHeader) Bytes() []byte {
  128. b := make([]byte, udpHeaderSize)
  129. i := 0
  130. i += copy(b[i:i+16], h.raddr)
  131. i += copy(b[i:i+16], h.laddr)
  132. i += copy(b[i:i+16], h.ifcaddr)
  133. b[i], b[i+1], i = byte(h.rport>>8), byte(h.rport), i+2
  134. b[i], b[i+1], i = byte(h.lport>>8), byte(h.lport), i+2
  135. return b
  136. }
  137. func unmarshalUDPHeader(b []byte) (*udpHeader, []byte) {
  138. h := new(udpHeader)
  139. h.raddr, b = IP(b[:16]), b[16:]
  140. h.laddr, b = IP(b[:16]), b[16:]
  141. h.ifcaddr, b = IP(b[:16]), b[16:]
  142. h.rport, b = uint16(b[0])<<8|uint16(b[1]), b[2:]
  143. h.lport, b = uint16(b[0])<<8|uint16(b[1]), b[2:]
  144. return h, b
  145. }
  146. // ListenUDP listens for incoming UDP packets addressed to the local
  147. // address laddr. Net must be "udp", "udp4", or "udp6". If laddr has
  148. // a port of 0, ListenUDP will choose an available port.
  149. // The LocalAddr method of the returned UDPConn can be used to
  150. // discover the port. The returned connection's ReadFrom and WriteTo
  151. // methods can be used to receive and send UDP packets with per-packet
  152. // addressing.
  153. func ListenUDP(net string, laddr *UDPAddr) (*UDPConn, error) {
  154. switch net {
  155. case "udp", "udp4", "udp6":
  156. default:
  157. return nil, UnknownNetworkError(net)
  158. }
  159. if laddr == nil {
  160. laddr = &UDPAddr{}
  161. }
  162. l, err := listenPlan9(net, laddr)
  163. if err != nil {
  164. return nil, err
  165. }
  166. _, err = l.ctl.WriteString("headers")
  167. if err != nil {
  168. return nil, err
  169. }
  170. l.data, err = os.OpenFile(l.dir+"/data", os.O_RDWR, 0)
  171. if err != nil {
  172. return nil, err
  173. }
  174. fd, err := l.netFD()
  175. return newUDPConn(fd), err
  176. }
  177. // ListenMulticastUDP listens for incoming multicast UDP packets
  178. // addressed to the group address gaddr on ifi, which specifies the
  179. // interface to join. ListenMulticastUDP uses default multicast
  180. // interface if ifi is nil.
  181. func ListenMulticastUDP(net string, ifi *Interface, gaddr *UDPAddr) (*UDPConn, error) {
  182. return nil, syscall.EPLAN9
  183. }