-
Notifications
You must be signed in to change notification settings - Fork 36
/
parse_rtsp.c
597 lines (538 loc) · 17.9 KB
/
parse_rtsp.c
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
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
/*
Copyright (c) 2012, Paula Roquero Fuentes <paula.roquero.fuentes@gmail.com>
Permission to use, copy, modify, and/or distribute this software for any purpose with or without fee is hereby granted, provided that the above copyright notice and this permission notice appear in all copies.
THE SOFTWARE IS PROVIDED AS IS AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include "parse_rtsp.h"
#include "strnstr.h"
const int N_CAST = 2;
const char *CAST_STR[] = {"unicast\0", "multicast\0"};
const int N_ATTR = 6;
const char *ATTR_STR[] = {"Accept\0", "Content-Type\0", "Content-Length\0", "CSeq\0", "Session\0", "Transport\0"};
const int N_METHODS = 6;
const char *METHOD_STR[] = {"DESCRIBE\0", "PLAY\0", "PAUSE\0", "SETUP\0", "TEARDOWN\0", "OPTIONS\0"};
const char *RTSP_STR = "RTSP/1.0\0";
const char *RTSP_URI = "rtsp://\0";
const char *SDP_STR = "application/sdp\0";
const char *RTP_STR = "RTP/AVP\0";
const char *CLIENT_PORT_STR = "client_port=\0";
const char *SERVER_PORT_STR = "server_port=\0";
const char *OPTIONS_STR = "Public: DESCRIBE, SETUP, TEARDOWN, PLAY, PAUSE\0";
const char *OK_STR = "OK\0";
const char *SERVERERROR_STR = "Internal server error\0";
const char *NOTFOUND_STR = "Not found\0";
const char *NULL_STR = "\0";
int unpack_rtsp_req(RTSP_REQUEST *req, char *req_text, int text_size) {
char *tok_start;
int tok_len;
int attr;
tok_start = req_text;
/* Initialize structure */
req->method = -1;
req->uri = 0;
req->CSeq = -1;
req->Session = -1;
req->client_port = 0;
/* Get method token */
tok_len = strcspn(tok_start, " ");
if (tok_len == text_size)
return(0);
/* Discover method */
req->method = detect_method(tok_start, text_size);
if (req->method == -1)
return(0);
/* Prepare for next token */
tok_start += tok_len + 1;
text_size -= tok_len + 1;
/* Get uri token */
tok_len = strcspn(tok_start, " ");
if (tok_len == text_size)
return(0);
/* Reserve memory for uri */
req->uri = malloc(tok_len + 1);
if (!req->uri)
return(0);
/* Copy uri */
memcpy(req->uri, tok_start, tok_len);
req->uri[tok_len] = 0;
if (!check_uri(req->uri))
return(0);
/* Prepare for next token */
tok_start += tok_len + 1;
text_size -= tok_len + 1;
/* Get rtsp token */
tok_len = strcspn(tok_start, "\r\n");
if (tok_len == text_size) {
free(req->uri);
req->uri = 0;
return(0);
}
/* Check if the rtsp token is valid */
if (strncmp(tok_start, RTSP_STR, 8)) {
free(req->uri);
req->uri = 0;
return(0);
}
/* Ignore next \r or \n */
tok_start += tok_len + 1;
text_size -= tok_len + 1;
if (*tok_start == '\r' || *tok_start == '\n') {
++tok_start;
--text_size;
}
/* Get Attributes until we find an empty line */
while ( (tok_len = strcspn(tok_start, "\r\n")) ) {
if (tok_len == text_size) {
free(req->uri);
req->uri = 0;
return(0);
}
/* Get all the attributes */
attr = detect_attr_req(req, tok_start, text_size);
if (!attr) {
free(req->uri);
req->uri = 0;
return(0);
}
tok_start += tok_len + 1;
text_size -= tok_len + 1;
if (*tok_start == '\r' || *tok_start == '\n') {
++tok_start;
--text_size;
}
}
/* If text_size is 0, a last \r\n wasn't received */
if (text_size == 0)
return(0);
/* Obligatory */
if (req->method == -1)
return(0);
/* Obligatory */
if (req->uri == 0)
return(0);
/* Obligatory */
if (req->CSeq == -1) {
free(req->uri);
req->uri = 0;
return(0);
}
/* Session must be present if the method is PLAY, PAUSE or TEARDOWN */
if (req->Session == -1 && (req->method == PLAY || req->method == PAUSE || req->method == TEARDOWN)) {
free(req->uri);
req->uri = 0;
return(0);
}
/* client_port must be present if the method is SETUP */
if (req->client_port == 0 && req->method == SETUP) {
free(req->uri);
req->uri = 0;
return(0);
}
return(1);
}
int detect_method(char *tok_start, int text_size) {
int i;
int method_len;
method_len = strcspn(tok_start, " ");
if (method_len == text_size || method_len == 0)
return(-1);
/* Discover attribute */
for (i = 0; i < N_METHODS; ++i) {
if (!memcmp(METHOD_STR[i], tok_start, method_len))
return(i);
}
return(-1);
}
int detect_attr_req(RTSP_REQUEST *req, char *tok_start, int text_size) {
int i;
int attr = -1;
int attr_len;
attr_len = strcspn(tok_start, ":");
if (attr_len == text_size || attr_len == 0)
return(0);
/* Discover attribute */
for (i = 0; i < N_ATTR; ++i) {
if (!memcmp(ATTR_STR[i], tok_start, attr_len)) {
attr = i;
break;
}
}
tok_start += attr_len;
text_size -= attr_len - 1;
/* Ignore spaces after ':' */
while (*(++tok_start) == ' ')
--text_size;
attr_len = strcspn(tok_start, "\r\n");
if (attr_len == text_size || attr_len == 0)
return(0);
switch (attr) {
case ACCEPT_STR:
/* Only can send SDP */
if (!strnstr(tok_start, SDP_STR, attr_len))
return(0);
break;
case CSEQ_STR:
req->CSeq = atoi(tok_start);
break;
case SESSION_STR:
req->Session = atoi(tok_start);
break;
case TRANSPORT_STR:
/* The only acceptable transport is RTP */
if (!strnstr(tok_start, RTP_STR, attr_len))
return(0);
/* Check if the transport is unicast or multicast */
if (strnstr(tok_start, CAST_STR[UNICAST], attr_len))
req->cast = UNICAST;
else if (strnstr(tok_start, CAST_STR[MULTICAST], attr_len))
req->cast = MULTICAST;
else
return(0);
/* Get the client ports */
if ( (tok_start = strnstr(tok_start, CLIENT_PORT_STR, attr_len)) ) {
if (!tok_start)
return(0);
tok_start += strlen(CLIENT_PORT_STR);
req->client_port = (PORT)atoi(tok_start);
if (req->client_port == 0)
return(0);
}
break;
default:
return(1);
}
return(1);
}
/*
* Pack the structure RTSP_REQUEST in a string
* req: request structure
* req_text: String where the request will be packed
* text_size: Length of req_text
* return: number of characters written. 0 is error
*/
int pack_rtsp_req(RTSP_REQUEST *req, char *req_text, int text_size) {
int ret;
int written;
/* Save space for the last \0 */
--text_size;
req_text[text_size] = 0;
/* Write method and uri */
if (req->method == -1 || !req->uri)
return(0);
ret = written = snprintf(req_text, text_size, "%s %s RTSP/1.0\r\n", METHOD_STR[req->method], req->uri);
if (ret < 0 || ret >= text_size)
return(0);
/* CSeq must have a value always*/
if (!req->CSeq)
return(0);
/* Print to req_text */
ret = snprintf(req_text + written, text_size - written, "CSeq: %d\r\n", req->CSeq);
/* Check if the printed text was larger than the space available in req_text */
if (ret < 0 || ret >= text_size-written)
return(0);
/* Add the number of characters written to written */
written += ret;
/* Write session number */
if (req->Session != -1) {
ret = snprintf(req_text + written, text_size - written, "Session: %d\r\n", req->Session);
if (ret < 0 || ret >= text_size - written)
return(0);
written += ret;
} else {
/* Session must be present in PLAY, PASE and TEARDOWN */
if (req->method == PLAY || req->method == PAUSE || req->method == TEARDOWN)
return(0);
}
/* Accept only application/sdp in DESCRIBE */
if (req->method == DESCRIBE) {
ret = snprintf(req_text + written, text_size - written, "Accept: application/sdp\r\n");
if (ret < 0 || ret >= text_size - written)
return(0);
written += ret;
}
/* Write client port */
if (req->client_port) {
ret = snprintf(req_text + written, text_size - written, "Transport: RTP/AVP;%s;client_port=%d-%d\r\n", CAST_STR[req->cast], req->client_port, req->client_port + 1);
if (ret < 0 || ret >= text_size - written)
return(0);
written += ret;
} else {
/* client_port must be present in SETUP */
if (req->method == SETUP)
return(0);
}
ret = snprintf(req_text + written, text_size - written, "\r\n");
if (ret < 0 || ret >= text_size - written)
return(0);
written += ret;
req_text[written] = 0;
return(written);
}
/* Convenience function, reserves RTSP_RESPONSE */
int unpack_rtsp_res2(RTSP_RESPONSE **res, char *res_text, int text_size) {
*res = (RTSP_RESPONSE *)malloc(sizeof(RTSP_RESPONSE));
if (!(*res))
return 0;
return unpack_rtsp_res(*res, res_text, text_size);
}
int unpack_rtsp_res(RTSP_RESPONSE *res, char *res_text, int text_size) {
char *tok_start;
int tok_len;
int attr;
tok_start = res_text;
/* Initialize structure */
res->code = -1;
res->CSeq = -1;
res->Session = -1;
res->client_port = 0;
res->server_port = 0;
res->Content_Length = -1;
res->content = 0;
res->options = 0;
/* Get rtsp token */
tok_len = strcspn(tok_start, " ");
if (tok_len == text_size)
return(0);
/* Check if the rtsp token is valid */
if (strncmp(tok_start, RTSP_STR, 8))
return(0);
/* Prepare for next token */
tok_start += tok_len + 1;
text_size -= tok_len + 1;
/* Check response code */
if (*tok_start != '2')
return(0);
res->code = atoi(tok_start);
/* Ignore rest of line */
tok_len = strcspn(tok_start, "\r\n");
if (tok_len == text_size)
return(0);
/* Ignore next \r or \n */
tok_start += tok_len + 1;
text_size -= tok_len + 1;
if (*tok_start == '\r' || *tok_start == '\n') {
++tok_start;
--text_size;
}
/* Get Attributes until we find an empty line */
while ( (tok_len = strcspn(tok_start, "\r\n")) ) {
if (tok_len == text_size) {
if (res->content)
free(res->content);
res->content = 0;
return(0);
}
/* Get all the attributes */
attr = detect_attr_res(res, tok_start, text_size);
if (!attr) {
if (res->content)
free(res->content);
res->content = 0;
return(0);
}
tok_start += tok_len + 1;
text_size -= tok_len + 1;
if (*tok_start == '\r' || *tok_start == '\n') {
++tok_start;
--text_size;
}
}
/* If text_size is 0, a last \r\n wasn't received */
if (text_size == 0)
return(0);
/* If there is content */
if (res->Content_Length != -1) {
/* Ignore empty line */
tok_start += 2;
text_size -= 2;
if (res->Content_Length > text_size)
return(0);
res->content = malloc(res->Content_Length + 1);
if (!res->content)
return(0);
/* Copy content */
memcpy(res->content, tok_start, res->Content_Length);
res->content[res->Content_Length] = 0;
}
/* Obligatory */
if (res->code == -1) {
if (res->content)
free(res->content);
res->content = 0;
return(0);
}
/* Obligatory */
if (res->CSeq == -1) {
if (res->content)
free(res->content);
res->content = 0;
return(0);
}
/* Is an error having Content-Length but not having content */
if ((res->content && res->Content_Length == -1) ||
(!res->content && res->Content_Length != -1)) {
if (res->content)
free(res->content);
res->content = 0;
return(0);
}
/* Is an error having content and transport */
if (res->content && res->client_port) {
if (res->content)
free(res->content);
res->content = 0;
return(0);
}
return(1);
}
int pack_rtsp_res(RTSP_RESPONSE *res, char *res_text, int text_size) {
int ret;
int written;
const char *status_str;
/* Save space for the last \0 */
--text_size;
/* Write protocol and code*/
if (res->code == -1)
return(0);
if (res->code == 200)
status_str = OK_STR;
else if (res->code == 500)
status_str = SERVERERROR_STR;
else if (res->code == 404)
status_str = NOTFOUND_STR;
else
status_str = NULL_STR;
ret = written = snprintf(res_text, text_size, "RTSP/1.0 %d %s\r\n", res->code, status_str);
if (ret < 0 || ret >= text_size)
return(0);
/* CSeq must have a value always*/
if (!res->CSeq)
return(0);
/* Print to res_text */
ret = snprintf(res_text + written, text_size - written, "CSeq: %d\r\n", res->CSeq);
/* Check if the printed text was larger than the space available in res_text */
if (ret < 0 || ret >= text_size-written)
return(0);
/* Add the number of characters written to written */
written += ret;
/* Respond to options */
if (res->options != 0) {
ret = snprintf(res_text + written, text_size - written, "%s\r\n", OPTIONS_STR);
if (ret < 0 || ret >= text_size - written)
return(0);
written += ret;
}
/* Write session number */
if (res->Session != -1 && res->Session != 0) {
ret = snprintf(res_text + written, text_size - written, "Session: %d\r\n", res->Session);
if (ret < 0 || ret >= text_size - written)
return(0);
written += ret;
}
/* Write client and server ports*/
if (res->client_port && res->server_port) {
ret = snprintf(res_text + written, text_size - written, "Transport: RTP/AVP;%s;client_port=%d-%d;server_port=%d-%d\r\n", CAST_STR[res->cast], res->client_port, res->client_port + 1, res->server_port, res->server_port + 1);
if (ret < 0 || ret >= text_size - written)
return(0);
written += ret;
}
if (res->Content_Length > 0) {
ret = snprintf(res_text + written, text_size - written, "Content-Length: %d\r\n", res->Content_Length);
if (ret < 0 || ret >= text_size - written)
return(0);
written += ret;
}
/* Empty line */
ret = snprintf(res_text + written, text_size - written, "\r\n");
if (ret < 0 || ret >= text_size - written)
return(0);
written += ret;
if (res->content) {
ret = snprintf(res_text + written, text_size - written, "%s", res->content);
if (ret < 0 || ret >= text_size - written)
return(0);
written += ret;
}
return(written);
}
int check_uri(char *uri) {
if (strstr(uri, RTSP_URI) == uri)
return(1);
return(0);
}
int detect_attr_res(RTSP_RESPONSE *res, char *tok_start, int text_size) {
int i;
int attr = -1;
int attr_len;
attr_len = strcspn(tok_start, ":");
if (attr_len == text_size || attr_len == 0)
return(0);
/* Discover attribute */
for (i = 0; i < N_ATTR; ++i) {
if (!memcmp(ATTR_STR[i], tok_start, attr_len)) {
attr = i;
break;
}
}
tok_start += attr_len;
text_size -= attr_len - 1;
/* Ignore spaces after ':' */
while (*(++tok_start) == ' ')
--text_size;
attr_len = strcspn(tok_start, "\r\n");
if (attr_len == text_size || attr_len == 0)
return(0);
switch (attr) {
case CSEQ_STR:
res->CSeq = atoi(tok_start);
break;
case SESSION_STR:
res->Session = atoi(tok_start);
break;
case CONTENT_TYPE_STR:
if (memcmp(tok_start, SDP_STR, attr_len))
return(0);
break;
case CONTENT_LENGTH_STR:
res->Content_Length = atoi(tok_start);
if (!res->Content_Length)
return(0);
break;
case TRANSPORT_STR:
/* The only acceptable transport is RTP */
if (!strnstr(tok_start, RTP_STR, attr_len))
return(0);
/* Check if the transport is unicast or multicast */
if (strnstr(tok_start, CAST_STR[UNICAST], attr_len))
res->cast = UNICAST;
else if (strnstr(tok_start, CAST_STR[MULTICAST], attr_len))
res->cast = MULTICAST;
else
return(0);
/* Get the client ports */
if ( (tok_start = strnstr(tok_start, CLIENT_PORT_STR, attr_len)) ) {
if (!tok_start)
return(0);
tok_start += strlen(CLIENT_PORT_STR);
res->client_port = (PORT)atoi(tok_start);
if (res->client_port == 0)
return(0);
}
/* Get the client ports */
if ( (tok_start = strnstr(tok_start, SERVER_PORT_STR, attr_len)) ) {
if (!tok_start)
return(0);
tok_start += strlen(SERVER_PORT_STR);
res->server_port = (PORT)atoi(tok_start);
if (res->server_port == 0)
return(0);
}
break;
default:
return(1);
}
return(1);
}