-
-
Notifications
You must be signed in to change notification settings - Fork 116
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adds a DTLS Connection ID client example. Two different "connections" are used with the second resuming using the state from the first, allowing it to bypass performing a second DTLS handshake. Signed-off-by: Daniel Mangum <[email protected]>
- Loading branch information
Showing
1 changed file
with
77 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
package main | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"log" | ||
"net" | ||
|
||
piondtls "github.com/pion/dtls/v2" | ||
"github.com/plgd-dev/go-coap/v3/dtls" | ||
) | ||
|
||
func main() { | ||
conf := &piondtls.Config{ | ||
PSK: func(hint []byte) ([]byte, error) { | ||
fmt.Printf("Server's hint: %s \n", hint) | ||
return []byte{0xAB, 0xC1, 0x23}, nil | ||
}, | ||
PSKIdentityHint: []byte("Pion DTLS Client"), | ||
CipherSuites: []piondtls.CipherSuiteID{piondtls.TLS_PSK_WITH_AES_128_CCM_8}, | ||
ConnectionIDGenerator: piondtls.OnlySendCIDGenerator(), | ||
} | ||
raddr, err := net.ResolveUDPAddr("udp", "127.0.0.1:5688") | ||
if err != nil { | ||
panic(err) | ||
} | ||
|
||
// Setup first UDP listener. | ||
udpconn, err := net.ListenUDP("udp", nil) | ||
if err != nil { | ||
panic(err) | ||
} | ||
|
||
// Create DTLS client on UDP listener. | ||
client, err := piondtls.Client(udpconn, raddr, conf) | ||
if err != nil { | ||
panic(err) | ||
} | ||
co := dtls.Client(client) | ||
resp, err := co.Get(context.Background(), "/a") | ||
if err != nil { | ||
panic(err) | ||
} | ||
log.Printf("Response payload: %+v", resp) | ||
resp, err = co.Get(context.Background(), "/b") | ||
if err != nil { | ||
panic(err) | ||
} | ||
log.Printf("Response payload: %+v", resp) | ||
|
||
// Export state to resume connection from another address. | ||
state := client.ConnectionState() | ||
|
||
// Setup second UDP listener on a different address. | ||
udpconn, err = net.ListenUDP("udp", nil) | ||
if err != nil { | ||
panic(err) | ||
} | ||
|
||
// Resume connection on new address with previous state. | ||
client, err = piondtls.Resume(&state, udpconn, raddr, conf) | ||
if err != nil { | ||
panic(err) | ||
} | ||
co = dtls.Client(client) | ||
// Requests can be performed without performing a second handshake. | ||
resp, err = co.Get(context.Background(), "/a") | ||
if err != nil { | ||
panic(err) | ||
} | ||
log.Printf("Response payload: %+v", resp) | ||
resp, err = co.Get(context.Background(), "/b") | ||
if err != nil { | ||
panic(err) | ||
} | ||
log.Printf("Response payload: %+v", resp) | ||
} |