Skip to content

Commit 56a1efd

Browse files
authored
net: add DialUDPContext version of DialUDP
Fixes #49097
1 parent 6df0957 commit 56a1efd

File tree

1 file changed

+30
-0
lines changed

1 file changed

+30
-0
lines changed

src/net/udpsock.go

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -300,6 +300,36 @@ func DialUDP(network string, laddr, raddr *UDPAddr) (*UDPConn, error) {
300300
return c, nil
301301
}
302302

303+
// DialUDPContext acts like DialUDP but connects using
304+
// the provided context.
305+
//
306+
// The provided Context must be non-nil.
307+
//
308+
// The network must be a UDP network name; see func Dial for details.
309+
//
310+
// If laddr is nil, a local address is automatically chosen.
311+
// If the IP field of raddr is nil or an unspecified IP address, the
312+
// local system is assumed.
313+
func DialUDPContext(ctx context.Context, network string, laddr, raddr *UDPAddr) (*UDPConn, error) {
314+
if ctx == nil {
315+
panic("nil context")
316+
}
317+
switch network {
318+
case "udp", "udp4", "udp6":
319+
default:
320+
return nil, &OpError{Op: "dial", Net: network, Source: laddr.opAddr(), Addr: raddr.opAddr(), Err: UnknownNetworkError(network)}
321+
}
322+
if raddr == nil {
323+
return nil, &OpError{Op: "dial", Net: network, Source: laddr.opAddr(), Addr: nil, Err: errMissingAddress}
324+
}
325+
sd := &sysDialer{network: network, address: raddr.String()}
326+
c, err := sd.dialUDP(ctx, laddr, raddr)
327+
if err != nil {
328+
return nil, &OpError{Op: "dial", Net: network, Source: laddr.opAddr(), Addr: raddr.opAddr(), Err: err}
329+
}
330+
return c, nil
331+
}
332+
303333
// ListenUDP acts like ListenPacket for UDP networks.
304334
//
305335
// The network must be a UDP network name; see func Dial for details.

0 commit comments

Comments
 (0)