-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdial.go
More file actions
52 lines (46 loc) · 1.26 KB
/
dial.go
File metadata and controls
52 lines (46 loc) · 1.26 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
// Copyright (c) 2025, Grigory Buteyko aka Hrissan
// Licensed under the MIT License. See LICENSE for details.
package dtls
import (
"context"
"net"
"net/netip"
"time"
"github.com/hrissan/dtls/dtlscore"
)
func Dial(t *dtlscore.Transport, network, address string) (*Conn, error) {
return DialTimeout(t, network, address, 0)
}
func DialTimeout(t *dtlscore.Transport, network, address string, timeout time.Duration) (*Conn, error) {
return DialTimeoutEarlyData(t, network, address, timeout, nil)
}
func DialTimeoutEarlyData(t *dtlscore.Transport, network, address string, timeout time.Duration, earlyData []byte) (*Conn, error) {
netipAddr, err := netip.ParseAddrPort(address)
if err != nil {
return nil, err
}
netAddr, err := net.ResolveUDPAddr(network, address)
if err != nil {
return nil, err
}
conn := newConn(netAddr, netAddr)
if len(earlyData) != 0 {
conn.writing = append(conn.writing, append([]byte{}, earlyData...))
}
err = t.StartConnection(&conn.tc, conn, netipAddr)
if err != nil {
return nil, err
}
ctx := context.Background()
if timeout != 0 {
var cancel context.CancelFunc
ctx, cancel = context.WithTimeout(ctx, timeout)
defer cancel()
}
select {
case <-conn.condDial:
return conn, nil
case <-ctx.Done():
return nil, ctx.Err()
}
}