summaryrefslogtreecommitdiff
path: root/src/urtc.c
blob: 892b63228ea81261e91fee839a48e7949cbfd2ab (plain)
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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
/**
 * liburtc
 * Copyright 2020 Chris Hiszpanski
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to
 * deal in the Software without restriction, including without limitation the
 * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
 * sell copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
 * IN THE SOFTWARE.
 */

#include <assert.h>						// assert
#include <errno.h>						// errno
#include <poll.h>						// poll
#include <pthread.h>					// pthread_create
#include <stdbool.h>					// true
#include <stdio.h>						// fprintf
#include <stdlib.h>						// calloc, free
#include <string.h>						// strerror
#include <unistd.h>						// close

#include <arpa/inet.h>					// inet_ntoa
#include <netinet/in.h>					// IPPROTO_UDP
#include <sys/socket.h>					// socket
#include <sys/types.h>

#include "err.h"
#include "log.h"
#include "mdns.h"						// mdns_subscribe, mdns_unsubscribe
#include "prng.h"						// prng_init
#include "sdp.h"
#include "urtc.h"
#include "uuid.h"						// uuid_create_str

#define  RX_BUF_CAP               2048  // receive buffer capacity

#define  POLL_TIMEOUT_MS            50  // poll() timeout (in milliseconds)

enum signaling_state {
	SIGNALING_STATE_STABLE = 0,
	SIGNALING_STATE_HAVE_LOCAL_OFFER,
	SIGNALING_STATE_HAVE_REMOTE_OFFER,
	SIGNALING_STATE_HAVE_LOCAL_PRANSWER,
	SIGNALING_STATE_HAVE_REMOTE_PRANSWER,
};

enum rtc_state {
	STATE_NEW = 0,

	STATE_ICE_NEW,
	STATE_ICE_GATHERING,
	STATE_ICE_COMPLETE,

	STATE_CONNECTING,

	STATE_CONNECTED,

	STATE_DISCONNECTED,

	STATE_FAILED,

	STATE_CLOSED,

	NUM_STATES // must be last
};

enum rtc_event {
	EVENT_SOCKET = 0,
	EVENT_TIMER,
	EVENT_MDNS,
	NUM_EVENTS, // must be last
};

struct peerconn {
	// socket file descriptor
	int sockfd;

	// execution thread
	pthread_t thread;

	// poll file descriptors
	struct pollfd fds[NUM_EVENTS];

	// callbacks
	urtc_on_ice_candidate *on_ice_candidate;
	urtc_force_idr *force_idr;

	// stun servers
	const char **stun;

	// state machine
	enum signaling_state ss;

	// local and remote descriptions
	struct sdp ldesc, rdesc;

	// mDNS related state
	struct {
		char hostname[UUID_STR_LEN];	// .local hostname
		int  sockfd;					// port 5353 listener
	} mdns;
};


typedef int (*event_handler)(struct peerconn *pc);

const enum rtc_state state_table[NUM_STATES][NUM_EVENTS] = {
	{ STATE_NEW, STATE_NEW },
	{ STATE_NEW, STATE_NEW },
	{ STATE_NEW, STATE_NEW },
	{ STATE_NEW, STATE_NEW },
	{ STATE_NEW, STATE_NEW },
	{ STATE_NEW, STATE_NEW },
	{ STATE_NEW, STATE_NEW },
	{ STATE_NEW, STATE_NEW },
	{ STATE_NEW, STATE_NEW }
};

/**
 * Handle incoming STUN packet
 *
 * \param pc Peer connection.
 *
 * \return 0 on success, negative on error.
 */
static int stun_handler(
	struct peerconn *pc,
	const uint8_t *pkt,
	size_t n
) {
	return 0;
}

/**
 * Handle incoming DTLS packet
 *
 * \param pc Peer connection.
 *
 * \return 0 on success, negative on error.
 */
static int dtls_handler(
	struct peerconn *pc,
	const uint8_t *pkt,
	size_t n
) {
	return 0;
}

/**
 * Handle incoming SRTP (or SRTCP) packet
 *
 * \param pc Peer connection.
 *
 * \return 0 on success, negative on error.
 */
static int rtp_handler(
	struct peerconn *pc,
	const uint8_t *pkt,
	size_t n
) {
	return 0;
}

/**
 * Handle incoming packet
 *
 * Packet may be a DTLS, SRTP, SRTCP, or STUN packet. Other packet types
 * are discarded.
 *
 * \param pc Peer connection.
 *
 * \return 0 on success, negative on error.
 */
static int socket_event_handler(struct peerconn *pc) {
	uint8_t buffer[RX_BUF_CAP];
	struct sockaddr_in ra;
	socklen_t ralen;
	ssize_t n;

	// read packet
	ralen = sizeof(ra);
	n = recvfrom(
		pc->sockfd,
		buffer,
		sizeof(buffer),
		0,
		(struct sockaddr *)(&ra),
		&ralen
	);

	// rtp
	if ((127 < buffer[0]) && (buffer[0] < 192)) {
		log(INFO, "[rtp] %s", inet_ntoa(ra.sin_addr));
		rtp_handler(pc, buffer, n);
	} else
	// dtls
	if ((19 < buffer[0]) && (buffer[0] < 64)) {
		log(INFO, "[dtls] %s", inet_ntoa(ra.sin_addr));
		dtls_handler(pc, buffer, n);
	} else
	// stun
	if (buffer[0] < 2) {
		log(INFO, "[stun] %s", inet_ntoa(ra.sin_addr));
		stun_handler(pc, buffer, n);
	}

	return 0;
}

/**
 * Handle timer event
 *
 * May need to:
 * - resend STUN packet
 * - resend DTLS packet
 * - expire ICE candidate?
 *
 * \param pc Peer connection.
 *
 * \return 0 on success, negative on error.
 */
static int timer_event_handler(struct peerconn *pc) {
	return 0;
}

/**
 * Handle incoming mDNS query
 *
 * \param pc Peer connection.
 *
 * \return 0 on success, negative on error.
 */
static int mdns_handler(struct peerconn *pc) {
	uint8_t buffer[RX_BUF_CAP];
	ssize_t n;

	// read packet
	if (n = recvfrom(
		pc->mdns.sockfd,
		buffer,
		sizeof(buffer),
		0,
		NULL,
		NULL
	), -1 == n) {
		log(ERROR, "%s", strerror(errno));
		goto _fail_recvfrom;
	}

	// if valid query for peer connection's ephemeral hostname...
	int type = mdns_validate_query(buffer, n, pc->mdns.hostname);
	if (type > 0) {
		if (type | TYPE_A) {
			log(DEBUG, "[mdns] received A query");
			// ...respond to query with interface addresses
			mdns_send_response(pc->mdns.sockfd, pc->mdns.hostname);
		}
		if (type | TYPE_AAAA) {
			log(DEBUG, "[mdns] received AAAA query");
		}
	}

	return 0;

_fail_recvfrom:
	return -URTC_ERR;
}

const event_handler action_table[NUM_STATES][NUM_EVENTS] = {
	{ NULL, NULL, NULL },
	{ NULL, NULL, NULL },
	{ NULL, NULL, NULL },
	{ NULL, NULL, NULL },
	{ NULL, NULL, NULL },
	{ NULL, NULL, NULL },
	{ NULL, NULL, NULL },
	{ NULL, NULL, NULL },
	{ NULL, NULL, NULL }
};

/**
 * Runloop finite state machine (mealy) for handling timer and receive events
 */

// send stun request
// wait for stun reply
// 		timeout -> resend request

// called							dir			caller
// ------							---			------
// setRemoteDescription(offer)		<--			offer = createOffer()
// 												setLocalDescription(offer)
// answer = createAnswer()			-->			setRemoteDescription(answer)
// setLocalDescription(answer)
//
// cand = onIceCandidate			-->			addIceCandidate(cand)
// addIceCandidate(cand)			<--			cand = onIceCandidate
// 									...
//
//                       (connection established)
//
//                           (dtls handshake)
//
// srtp -->
// <-- rtcp, nack

/**
 * Execution thread per peer connection
 *
 * \param arg Peer connection
 *
 * \return Unused.
 */
static void * runloop(void *arg) {
	struct peerconn *pc;
	enum rtc_event event;
	int n;

	pc = (struct peerconn *)arg;
	assert(pc);

_loop:
	// block indefinitely (-1 timeout) until i/o event
	n = poll(pc->fds, NUM_EVENTS, -1);
	assert(n > 0);

	// which event(s) occurred?
	if (pc->fds[EVENT_SOCKET].revents & POLLIN) {
		event = EVENT_SOCKET;
	} else
	if (pc->fds[EVENT_MDNS].revents & POLLIN) {
		mdns_handler(pc);
	} else
	if (pc->fds[EVENT_TIMER].revents & POLLIN) {
		event = EVENT_TIMER;
	}

	goto _loop;


	return NULL;
};

urtc_peerconn_t * urtc_peerconn_create(const char *stun[]) {
	// seend pseudorandom number generator
	prng_init();

	// allocate peer connection
	struct peerconn *pc = (struct peerconn *)calloc(1, sizeof(struct peerconn));
	if (!pc) return pc;

	// copy pointer to stun servers
	pc->stun = stun;

	// open udp socket (for all dtls/srtp/srtcp/stun/turn communication)
	pc->sockfd = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP);
	if (-1 == pc->sockfd) goto _fail_socket;
	pc->fds[EVENT_SOCKET] = (struct pollfd){ pc->sockfd, POLLIN };

	// generate a unique local mDNS hostname
	uuid_create_str(pc->mdns.hostname);
	log(INFO, "mDNS hostname is %s.local", pc->mdns.hostname);

	// open multicast udp socket for replying to mDNS queries
	pc->mdns.sockfd = mdns_subscribe();
	if (-1 == pc->mdns.sockfd) goto _fail_mdns_subscribe;
	pc->fds[EVENT_MDNS] = (struct pollfd){
		pc->mdns.sockfd,
		POLLIN
	};

	// start new thread per peer connection
	if (0 != pthread_create(
		&(pc->thread), 
		NULL,
		runloop,
		pc
	)) goto _fail_pthread_create;

	return pc;

_fail_pthread_create:
	mdns_unsubscribe(pc->mdns.sockfd);
_fail_mdns_subscribe:
	close(pc->sockfd);
_fail_socket:
	free(pc);

	return NULL;
}

int urtc_set_on_ice_candidate(struct peerconn *pc, urtc_on_ice_candidate *cb) {
	if (!pc) return -1;
	if (!cb) return -1;
	pc->on_ice_candidate = cb;

	return -URTC_ERR_NOT_IMPLEMENTED;
}

int urtc_add_ice_candidate(struct peerconn *pc, const char *cand) {
	return -URTC_ERR_NOT_IMPLEMENTED;
}

int urtc_create_answer(struct peerconn *pc, char **answer) {
	return -URTC_ERR_NOT_IMPLEMENTED;
}

int urtc_create_offer(struct peerconn *pc, char **offer) {
	return -URTC_ERR_NOT_IMPLEMENTED;
}

int urtc_set_remote_description(struct peerconn *pc, const char *desc) {
	return -URTC_ERR_NOT_IMPLEMENTED;
}

int urtc_set_local_description(struct peerconn *pc, const char *desc) {
	return -URTC_ERR_NOT_IMPLEMENTED;
}

void urtc_peerconn_destroy(struct peerconn *pc) {
	if (pc) {
		pthread_cancel(pc->thread);
		pthread_join(pc->thread, NULL);
		mdns_unsubscribe(pc->mdns.sockfd);
		shutdown(pc->sockfd, SHUT_RDWR);
		close(pc->sockfd);
		free(pc);
	}
}