Skip to content

Commit bb0164e

Browse files
committed
Add DTLS 1.2 Mulicast Example.
This commit leaned heavily on Claude.
1 parent 4561614 commit bb0164e

3 files changed

Lines changed: 598 additions & 0 deletions

File tree

dtls-mcast/Makefile

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
# DTLS Multicast Examples Makefile
2+
CC = gcc
3+
4+
# Path to wolfSSL source directory (adjust as needed)
5+
WOLFSSL_DIR = ../../wolfssl
6+
7+
CFLAGS = -Wall -Wextra -I$(WOLFSSL_DIR) -I$(WOLFSSL_DIR)/wolfssl
8+
LIBS = $(WOLFSSL_DIR)/src/.libs/libwolfssl.a -lm -lpthread
9+
10+
# option variables
11+
DEBUG_FLAGS = -g3 -DDEBUG -O0
12+
OPTIMIZE = -Os
13+
14+
# Options - uncomment DEBUG_FLAGS for debugging
15+
#CFLAGS+=$(DEBUG_FLAGS)
16+
CFLAGS+=$(OPTIMIZE)
17+
18+
# build targets
19+
TARGETS = mcast-peer
20+
21+
.PHONY: clean all debug
22+
23+
all: $(TARGETS)
24+
25+
debug: CFLAGS+=$(DEBUG_FLAGS)
26+
debug: all
27+
28+
mcast-peer: mcast-peer.c
29+
$(CC) -o $@ $< $(CFLAGS) $(LIBS)
30+
31+
clean:
32+
rm -f $(TARGETS)
33+
34+
# Helper targets for running the example
35+
run-node0:
36+
./mcast-peer 0
37+
38+
run-node1:
39+
./mcast-peer 1
40+
41+
run-node2:
42+
./mcast-peer 2

dtls-mcast/README.md

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
# DTLS 1.2 Multicast Example
2+
3+
This example demonstrates DTLS 1.2 multicast communication using wolfSSL's multicast APIs. Three peer applications can securely communicate with each other over UDP multicast.
4+
5+
## Overview
6+
7+
DTLS multicast allows multiple peers to share encrypted/authenticated communication over IP multicast. Unlike traditional TLS/DTLS which uses a handshake to establish keys, multicast DTLS uses pre-shared secrets that are distributed out-of-band to all participants.
8+
9+
This example uses:
10+
- **WDM-NULL-SHA256** cipher suite (NULL encryption with SHA-256 for message authentication)
11+
- Pre-shared secret material (PMS, client random, server random)
12+
- Multicast group `239.255.0.1:12345`
13+
14+
## Requirements
15+
16+
wolfSSL must be built with multicast support:
17+
18+
```bash
19+
cd /path/to/wolfssl
20+
./configure --enable-dtls --enable-mcast
21+
make
22+
```
23+
24+
For static linking (used by this example's Makefile):
25+
```bash
26+
./configure --enable-dtls --enable-mcast --enable-static
27+
make
28+
```
29+
30+
## Building
31+
32+
```bash
33+
make
34+
```
35+
## Usage
36+
37+
Run each peer in a separate terminal with a unique node ID (0, 1, or 2):
38+
39+
```bash
40+
# Terminal 1
41+
./mcast-peer 0
42+
43+
# Terminal 2
44+
./mcast-peer 1
45+
46+
# Terminal 3
47+
./mcast-peer 2
48+
```
49+
50+
Each peer will:
51+
1. Join the multicast group
52+
2. Send a message every 3 seconds
53+
3. Receive and display messages from other peers
54+
4. Exit cleanly on Ctrl+C
55+
56+
## Example Output
57+
58+
```
59+
=== DTLS Multicast Peer - Node 0 ===
60+
Node 0: Sockets ready, joined multicast group 239.255.0.1:12345
61+
Node 0: Added peer 1 to receive tracking
62+
Node 0: Added peer 2 to receive tracking
63+
Node 0: Ready. Press Ctrl+C to exit.
64+
Node 0: Sending messages every 3 seconds...
65+
66+
Node 0: Sent: "Hello from node 0, message #1"
67+
Node 0: Received from peer 1: "Hello from node 1, message #1"
68+
Node 0: Received from peer 2: "Hello from node 2, message #1"
69+
```
70+
71+
## API Usage Notes
72+
73+
The wolfSSL multicast APIs must be called in a specific order:
74+
75+
1. `wolfSSL_CTX_mcast_set_member_id()` - Set this node's ID
76+
2. `wolfSSL_CTX_set_cipher_list()` - Set multicast cipher suite
77+
3. `wolfSSL_new()` - Create SSL objects
78+
4. `wolfSSL_mcast_peer_add()` - Register expected peers (**before** setting secret)
79+
5. `wolfSSL_set_secret()` - Set the pre-shared secret (**after** adding peers)
80+
6. `wolfSSL_write()` / `wolfSSL_mcast_read()` - Send/receive messages
81+
82+
**Important**: `wolfSSL_mcast_peer_add()` must be called BEFORE `wolfSSL_set_secret()` because `wolfSSL_mcast_peer_add()` zeros the peer entry (including the epoch), and `wolfSSL_set_secret()` sets the epoch for all registered peers.
83+
84+
## Key Concepts
85+
86+
### Multicast Member ID
87+
Each peer has a unique ID (0-255) set via `wolfSSL_CTX_mcast_set_member_id()`. This ID is embedded in outgoing DTLS records and used by receivers to identify the sender.
88+
89+
### Pre-Shared Secret
90+
All peers must use identical secret material:
91+
- Pre-master secret (PMS)
92+
- Client random
93+
- Server random
94+
- Cipher suite identifier
95+
96+
In production, this material would be distributed securely out-of-band (e.g., via a key server).
97+
98+
## References
99+
100+
- [wolfSSL Manual - DTLS](https://www.wolfssl.com/documentation/manuals/wolfssl/chapter02.html)
101+
- [RFC 6347 - DTLS 1.2](https://tools.ietf.org/html/rfc6347)

0 commit comments

Comments
 (0)