File: dtls-bio.c

package info (click to toggle)
janus 0.7.3-2
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, sid
  • size: 15,360 kB
  • sloc: ansic: 130,260; makefile: 554; sh: 153; lisp: 9
file content (183 lines) | stat: -rw-r--r-- 5,310 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
/*! \file    dtls-bio.h
 * \author   Lorenzo Miniero <lorenzo@meetecho.com>
 * \copyright GNU General Public License v3
 * \brief    OpenSSL BIO agent writer
 * \details  OpenSSL BIO that writes packets to a libnice agent.
 * 
 * \ingroup protocols
 * \ref protocols
 */

#include <glib.h>

#include "dtls-bio.h"
#include "debug.h"
#include "ice.h"
#include "mutex.h"

/* Starting MTU value for the DTLS BIO agent writer */
static int mtu = 1200;
void janus_dtls_bio_agent_set_mtu(int start_mtu) {
	if(start_mtu < 0) {
		JANUS_LOG(LOG_ERR, "Invalid MTU...\n");
		return;
	}
	mtu = start_mtu;
	JANUS_LOG(LOG_VERB, "Setting starting MTU in the DTLS BIO writer: %d\n", mtu);
}

/* BIO implementation */
static int janus_dtls_bio_agent_write(BIO *h, const char *buf, int num);
static long janus_dtls_bio_agent_ctrl(BIO *h, int cmd, long arg1, void *arg2);
static int janus_dtls_bio_agent_new(BIO *h);
static int janus_dtls_bio_agent_free(BIO *data);

/* BIO initialization */
#if JANUS_USE_OPENSSL_PRE_1_1_API
static BIO_METHOD janus_dtls_bio_agent_methods = {
	BIO_TYPE_BIO,
	"janus agent writer",
	janus_dtls_bio_agent_write,
	NULL,
	NULL,
	NULL,
	janus_dtls_bio_agent_ctrl,
	janus_dtls_bio_agent_new,
	janus_dtls_bio_agent_free,
	NULL
};
#else
static BIO_METHOD *janus_dtls_bio_agent_methods = NULL;
#endif
int janus_dtls_bio_agent_init(void) {
#if JANUS_USE_OPENSSL_PRE_1_1_API
	/* No initialization needed for OpenSSL pre-1.1.0 */
#else
	janus_dtls_bio_agent_methods = BIO_meth_new(BIO_TYPE_BIO, "janus agent writer");
	if(!janus_dtls_bio_agent_methods) {
		return -1;
	}
	BIO_meth_set_write(janus_dtls_bio_agent_methods, janus_dtls_bio_agent_write);
	BIO_meth_set_ctrl(janus_dtls_bio_agent_methods, janus_dtls_bio_agent_ctrl);
	BIO_meth_set_create(janus_dtls_bio_agent_methods, janus_dtls_bio_agent_new);
	BIO_meth_set_destroy(janus_dtls_bio_agent_methods, janus_dtls_bio_agent_free);
#endif
	return 0;
}

static BIO_METHOD *BIO_janus_dtls_agent_method(void) {
#if JANUS_USE_OPENSSL_PRE_1_1_API
	return(&janus_dtls_bio_agent_methods);
#else
	return janus_dtls_bio_agent_methods;
#endif
}

BIO *BIO_janus_dtls_agent_new(struct janus_dtls_srtp *dtls) {
	BIO* bio = BIO_new(BIO_janus_dtls_agent_method());
	if(bio == NULL) {
		return NULL;
	}
#if JANUS_USE_OPENSSL_PRE_1_1_API
	bio->ptr = dtls;
#else
	BIO_set_data(bio, dtls);
#endif
	return bio;
}

static int janus_dtls_bio_agent_new(BIO *bio) {
#if JANUS_USE_OPENSSL_PRE_1_1_API
	bio->init = 1;
	bio->ptr = NULL;
	bio->flags = 0;
#else
	BIO_set_init(bio, 1);
	BIO_set_data(bio, NULL);
	BIO_set_shutdown(bio, 0);
#endif
	return 1;
}

static int janus_dtls_bio_agent_free(BIO *bio) {
	if(bio == NULL) {
		return 0;
	}
#if JANUS_USE_OPENSSL_PRE_1_1_API
	bio->ptr = NULL;
#else
	BIO_set_data(bio, NULL);
#endif
	return 1;
}
	
static int janus_dtls_bio_agent_write(BIO *bio, const char *in, int inl) {
	JANUS_LOG(LOG_HUGE, "janus_dtls_bio_agent_write: %p, %d\n", in, inl);
	/* Forward data to the write BIO */
	if(inl <= 0) {
		/* ... unless the size is negative or zero */
		JANUS_LOG(LOG_WARN, "janus_dtls_bio_agent_write failed: negative size (%d)\n", inl);
		return inl;
	}
	janus_dtls_srtp *dtls;
#if JANUS_USE_OPENSSL_PRE_1_1_API
	dtls = (janus_dtls_srtp *)bio->ptr;
#else
	dtls = (janus_dtls_srtp *)BIO_get_data(bio);
#endif
	if(dtls == NULL) {
		JANUS_LOG(LOG_ERR, "No DTLS-SRTP stack, no DTLS bridge...\n");
		return -1;
	}
	janus_ice_component *component = (janus_ice_component *)dtls->component;
	if(component == NULL) {
		JANUS_LOG(LOG_ERR, "No component, no DTLS bridge...\n");
		return -1;
	}
	janus_ice_stream *stream = component->stream;
	if(!stream) {
		JANUS_LOG(LOG_ERR, "No stream, no DTLS bridge...\n");
		return -1;
	}
	janus_ice_handle *handle = stream->handle;
	if(!handle || !handle->agent || !dtls->write_bio) {
		JANUS_LOG(LOG_ERR, "No handle/agent/bio, no DTLS bridge...\n");
		return -1;
	}

	if(inl > 1500) {
		/* FIXME Just a warning for now, this will need to be solved with proper fragmentation */
		JANUS_LOG(LOG_WARN, "[%"SCNu64"] The DTLS stack is trying to send a packet of %d bytes, this may be larger than the MTU and get dropped!\n", handle->handle_id, inl);
	}
	int bytes = nice_agent_send(handle->agent, component->stream_id, component->component_id, inl, in);
	if(bytes < inl) {
		JANUS_LOG(LOG_ERR, "[%"SCNu64"] Error sending DTLS message on component %d of stream %d (%d)\n", handle->handle_id, component->component_id, stream->stream_id, bytes);
	} else {
		JANUS_LOG(LOG_HUGE, "[%"SCNu64"] >> >> ... and sent %d of those bytes on the socket\n", handle->handle_id, bytes);
	}
	/* Update stats (TODO Do the same for the last second window as well)
	 * FIXME: the Data stats includes the bytes used for the handshake */
	if(bytes > 0) {
		component->out_stats.data.packets++;
		component->out_stats.data.bytes += bytes;
	}
	return bytes;
}

static long janus_dtls_bio_agent_ctrl(BIO *bio, int cmd, long num, void *ptr) {
	switch(cmd) {
		case BIO_CTRL_FLUSH:
			/* The OpenSSL library needs this */
			return 1;
		case BIO_CTRL_DGRAM_QUERY_MTU:
			/* Let's force the MTU that was configured */
			JANUS_LOG(LOG_HUGE, "Advertizing MTU: %d\n", mtu);
			return mtu;
		case BIO_CTRL_WPENDING:
		case BIO_CTRL_PENDING:
			return 0L;
		default:
			JANUS_LOG(LOG_HUGE, "janus_dtls_bio_agent_ctrl: %d\n", cmd);
	}
	return 0;
}