-
Notifications
You must be signed in to change notification settings - Fork 161
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adds an example for a client that only sends connection IDs (i.e. does not request to received them). This is the most common scenario for DTLS clients. Signed-off-by: Daniel Mangum <georgedanielmangum@gmail.com>
- Loading branch information
Showing
1 changed file
with
50 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,50 @@ | ||
// SPDX-FileCopyrightText: 2023 The Pion community <https://pion.ly> | ||
// SPDX-License-Identifier: MIT | ||
|
||
// Package main implements an example DTLS client using a pre-shared key. | ||
package main | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"net" | ||
"time" | ||
|
||
"github.com/pion/dtls/v2" | ||
"github.com/pion/dtls/v2/examples/util" | ||
) | ||
|
||
func main() { | ||
// Prepare the IP to connect to | ||
addr := &net.UDPAddr{IP: net.ParseIP("127.0.0.1"), Port: 4444} | ||
|
||
// | ||
// Everything below is the pion-DTLS API! Thanks for using it ❤️. | ||
// | ||
|
||
// Prepare the configuration of the DTLS connection | ||
config := &dtls.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 Server"), | ||
CipherSuites: []dtls.CipherSuiteID{dtls.TLS_PSK_WITH_AES_128_CCM_8}, | ||
ExtendedMasterSecret: dtls.RequireExtendedMasterSecret, | ||
ConnectionIDGenerator: dtls.OnlySendCIDGenerator(), | ||
} | ||
|
||
// Connect to a DTLS server | ||
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second) | ||
defer cancel() | ||
dtlsConn, err := dtls.DialWithContext(ctx, "udp", addr, config) | ||
util.Check(err) | ||
defer func() { | ||
util.Check(dtlsConn.Close()) | ||
}() | ||
|
||
fmt.Println("Connected; type 'exit' to shutdown gracefully") | ||
|
||
// Simulate a chat session | ||
util.Chat(dtlsConn) | ||
} |