Skip to content

Commit f2568d1

Browse files
committed
Add support for DTLS fragmentation
1 parent 47fa855 commit f2568d1

File tree

5 files changed

+131
-9
lines changed

5 files changed

+131
-9
lines changed

bundlex.exs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,12 @@ defmodule ExDTLS.BundlexProject do
1010
defp natives() do
1111
[
1212
native: [
13-
sources: ["native.c", "dtls.c", "dyn_buff.c"],
13+
sources: ["native.c", "dtls.c", "dyn_buff.c", "bio_frag.c"],
1414
deps: [unifex: :unifex],
1515
os_deps: [openssl: :pkg_config],
1616
libs: ["pthread"],
1717
interface: [:nif],
18-
# compiler_flags: ["-DEXDTLS_DEBUG"],
18+
compiler_flags: ["-DEXDTLS_DEBUG"],
1919
preprocessor: Unifex
2020
]
2121
]

c_src/ex_dtls/bio_frag.c

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
2+
#include "bio_frag.h"
3+
4+
static int bwrite(BIO *bio, const char *buf, int len);
5+
static int bread(BIO *bio, char *buf, int len);
6+
static long ctrl(BIO *bio, int cmd, long arg1, void *arg2);
7+
static int create(BIO *bio);
8+
static int destroy(BIO *bio);
9+
static long callback_ctrl(BIO *bio, int cmd, BIO_info_cb *fp);
10+
11+
// static const BIO_METHOD bio_methods = {
12+
// BIO_TYPE_BIO,
13+
// "DTLS fragmentation for mem BIO",
14+
// bwrite_conv,
15+
// bwrite,
16+
// bread_conv,
17+
// bread,
18+
// NULL,
19+
// NULL,
20+
// ctrl,
21+
// create,
22+
// destroy,
23+
// callback_ctrl
24+
// };
25+
26+
static BIO_METHOD *bio_methods = NULL;
27+
28+
const BIO_METHOD *BIO_f_frag(void) {
29+
bio_methods = BIO_meth_new(BIO_TYPE_FILTER, "DTLS fragmentation for mem BIO");
30+
31+
BIO_meth_set_read(bio_methods, bread);
32+
BIO_meth_set_write(bio_methods, bwrite);
33+
BIO_meth_set_ctrl(bio_methods, ctrl);
34+
BIO_meth_set_create(bio_methods, create);
35+
BIO_meth_set_destroy(bio_methods, destroy);
36+
BIO_meth_set_callback_ctrl(bio_methods, callback_ctrl);
37+
38+
return bio_methods;
39+
}
40+
41+
static int create(BIO *bio) {
42+
DEBUG("BIO frag create");
43+
// indicate that BIO initialization is complete
44+
BIO_set_init(bio, 1);
45+
return 1;
46+
}
47+
48+
static int destroy(BIO *bio) {
49+
DEBUG("BIO frag destroy");
50+
if (bio == NULL) {
51+
return 0;
52+
}
53+
54+
BIO_set_init(bio, 0);
55+
return 1;
56+
}
57+
58+
static int bread(BIO *bio, char *buf, int len) {
59+
DEBUG("BIO frag bread");
60+
BIO *next = BIO_next(bio);
61+
if (next == NULL) {
62+
return 0;
63+
}
64+
65+
return BIO_read(next, buf, len);
66+
}
67+
68+
static int bwrite(BIO *bio, const char *buf, int len) {
69+
DEBUG("BIO frag bwrite %d", len);
70+
BIO *next = BIO_next(bio);
71+
if (next == NULL) {
72+
return 0;
73+
}
74+
75+
return BIO_write(next, buf, len);
76+
}
77+
78+
static long ctrl(BIO *bio, int cmd, long num, void *ptr) {
79+
DEBUG("BIO frag ctrl");
80+
81+
BIO *next = BIO_next(bio);
82+
if (next == NULL) {
83+
return 0;
84+
}
85+
86+
return BIO_ctrl(next, cmd, num, ptr);
87+
}
88+
89+
static long callback_ctrl(BIO *bio, int cmd, BIO_info_cb *fp) {
90+
BIO *next = BIO_next(bio);
91+
if (next == NULL) {
92+
return 0;
93+
}
94+
95+
return BIO_callback_ctrl(next, cmd, fp);
96+
}
97+
98+

c_src/ex_dtls/bio_frag.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
#include <openssl/opensslv.h>
2+
#include <openssl/err.h>
3+
#include <openssl/ssl.h>
4+
5+
#include "log.h"
6+
7+
const BIO_METHOD *BIO_f_frag(void);

c_src/ex_dtls/dtls.c

Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
#include "dtls.h"
22

3+
#include "bio_frag.h"
4+
35
SSL_CTX *create_ctx(int dtls_srtp) {
46
SSL_CTX *ssl_ctx = SSL_CTX_new(DTLS_method());
57
if (ssl_ctx == NULL) {
@@ -33,19 +35,33 @@ SSL *create_ssl(SSL_CTX *ssl_ctx, int mode) {
3335
return NULL;
3436
}
3537

36-
BIO *rbio = BIO_new(BIO_s_mem());
37-
if (rbio == NULL) {
38-
DEBUG("Cannot create rbio");
38+
BIO *frag_bio = BIO_new(BIO_f_frag());
39+
if (frag_bio == NULL) {
40+
DEBUG("Cannot create frag bio");
3941
return NULL;
4042
}
4143

42-
BIO *wbio = BIO_new(BIO_s_mem());
43-
if (wbio == NULL) {
44-
DEBUG("Cannot create wbio");
44+
BIO *wmem_bio = BIO_new(BIO_s_mem());
45+
if (wmem_bio == NULL) {
46+
DEBUG("Cannot create write mem bio");
47+
return NULL;
48+
}
49+
50+
BIO *wchain = BIO_push(frag_bio, wmem_bio);
51+
52+
BIO *rmem_bio = BIO_new(BIO_s_mem());
53+
if (rmem_bio == NULL) {
54+
DEBUG("Cannot create read mem bio");
4555
return NULL;
4656
}
4757

48-
SSL_set_bio(ssl, rbio, wbio);
58+
SSL_set_bio(ssl, rmem_bio, wchain);
59+
60+
// printf("Setting MTU to 1000\n");
61+
// if (SSL_set_mtu(ssl, 1500) == 0) {
62+
// return NULL;
63+
// }
64+
4965

5066
return ssl;
5167
}

test/integration_test.exs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
defmodule ExDTLS.IntegrationTest do
22
use ExUnit.Case, async: true
33

4+
@tag :debug
45
test "dtls_srtp" do
56
rx_dtls = ExDTLS.init(mode: :server, dtls_srtp: true, verify_peer: true)
67
tx_dtls = ExDTLS.init(mode: :client, dtls_srtp: true, verify_peer: true)

0 commit comments

Comments
 (0)