Skip to content

Commit

Permalink
Add CID send only client example
Browse files Browse the repository at this point in the history
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
hasheddan committed Aug 27, 2023
1 parent 6df50a6 commit 37fbc04
Showing 1 changed file with 50 additions and 0 deletions.
50 changes: 50 additions & 0 deletions examples/dial/cid/main.go
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)
}

0 comments on commit 37fbc04

Please sign in to comment.