summaryrefslogtreecommitdiffstats
path: root/src/sdp/sdp_parser.yxx
blob: 1e3baf31c5cf4903e9ae19c381fa4057f7f0a4e6 (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
/*
    Copyright (C) 2005-2009  Michel de Boer <michel@twinklephone.com>

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <https://www.gnu.org/licenses/>.
*/

%{
#include <algorithm>
#include <cstdio>
#include <string>
#include "sdp_parse_ctrl.h"
#include "sdp.h"
#include "util.h"
#include "audits/memman.h"

using namespace std;

extern int yysdplex(void);
void yysdperror(const char *s);
%}

// The %debug option causes a problem with the %destructor options later on.
// The bison compilor generates undefined symbols:
//
//   parser.y: In function `void yysymprint(FILE*, int, yystype)':
//   parser.y:0: error: `null' undeclared (first use this function)
//
// So if you need to debug, then outcomment the %destructor first. This will
// do no harm to your debugging, it only will cause memory leaks during
// error handling.
//
// %debug


%expect 2
/* See below for the expected shift/reduce conflicts. */

%union {
	int			yysdpt_int;
	string			*yysdpt_str;
	t_sdp_ntwk_type		yysdpt_ntwk_type;
	t_sdp_addr_type		yysdpt_addr_type;
	t_sdp_connection	*yysdpt_connection;
	list<t_sdp_attr>	*yysdpt_attributes;
	t_sdp_attr		*yysdpt_attribute;
	t_sdp_media		*yysdpt_media;
	list<string>		*yysdpt_token_list;
}

%token <yysdpt_int>	T_NUM
%token <yysdpt_str>	T_TOKEN
%token <yysdpt_str>	T_SAFE
%token <yysdpt_str>	T_LINE

%token			T_CRLF

%token			T_LINE_VERSION
%token			T_LINE_ORIGIN
%token			T_LINE_SESSION_NAME
%token			T_LINE_CONNECTION
%token			T_LINE_ATTRIBUTE
%token			T_LINE_MEDIA
%token			T_LINE_UNKNOWN

// The token T_NULL is never returned by the scanner.
%token			T_NULL

%destructor { MEMMAN_DELETE($$); delete $$; }	T_TOKEN
%destructor { MEMMAN_DELETE($$); delete $$; }	T_SAFE
%destructor { MEMMAN_DELETE($$); delete $$; }	T_LINE

%type <yysdpt_addr_type>	address_type
%type <yysdpt_connection>	connection
%type <yysdpt_ntwk_type>	network_type
%type <yysdpt_attributes>	attributes
%type <yysdpt_attribute>	attribute
%type <yysdpt_attribute>	attribute2
%type <yysdpt_media>		media
%type <yysdpt_str>		transport
%type <yysdpt_token_list>	formats

%destructor { MEMMAN_DELETE($$); delete $$; }	connection
%destructor { MEMMAN_DELETE($$); delete $$; }	attributes
%destructor { MEMMAN_DELETE($$); delete $$; }	attribute
%destructor { MEMMAN_DELETE($$); delete $$; }	attribute2
%destructor { MEMMAN_DELETE($$); delete $$; }	media
%destructor { MEMMAN_DELETE($$); delete $$; }	transport
%destructor { MEMMAN_DELETE($$); delete $$; }	formats

%%

/* The unknown_lines cause an expected shift/reduce conflict */
sdp_body:	  version
		  origin
		  session_name
		  unknown_lines
		  sess_connection
		  unknown_lines
		  sess_attributes
		  media_list {
		  	/* Parsing stops here. Remaining text is
			 * not parsed.
			 */
		  	YYACCEPT; }
		| error T_NULL {
			/* KLUDGE to avoid memory leak in bison.
			 * See the SIP parser for an explanation.
			 */
			YYABORT;
		}
;

version:	  T_LINE_VERSION { CTX_NUM; } T_NUM { CTX_INITIAL; }
		  T_CRLF {
			SDP->version = $3; }
;

origin:		  T_LINE_ORIGIN { CTX_SAFE; } T_SAFE { CTX_INITIAL; }
		  T_TOKEN T_TOKEN network_type address_type T_TOKEN T_CRLF {
			SDP->origin.username = *$3;
			SDP->origin.session_id = *$5;
			SDP->origin.session_version = *$6;
			SDP->origin.network_type = $7;
			SDP->origin.address_type = $8;
			SDP->origin.address = *$9;
			MEMMAN_DELETE($3); delete $3;
			MEMMAN_DELETE($5); delete $5;
			MEMMAN_DELETE($6); delete $6;
			MEMMAN_DELETE($9); delete $9; }
;

network_type:	  T_TOKEN { try {
				$$ = str2sdp_ntwk_type(*$1);
				MEMMAN_DELETE($1); delete $1;
			    }
			    catch (t_sdp_syntax_error) {
			    	// Invalid network type.
				// Set network type to NULL. This way the message
				// will not be discarded and the error can be
				// handled on the SIP level (error response or
				// call tear down).
			    	MEMMAN_DELETE($1); delete $1;
				$$ = SDP_NTWK_NULL;
			    } }
;

address_type:	  T_TOKEN { try {
				$$ = str2sdp_addr_type(*$1);
				MEMMAN_DELETE($1); delete $1;
			    }
			    catch (t_sdp_syntax_error) {
			    	// Invalid address type
			    	MEMMAN_DELETE($1); delete $1;
				$$ = SDP_ADDR_NULL;
			    } }
;

session_name:	  T_LINE_SESSION_NAME { CTX_LINE; } T_LINE
		  { CTX_INITIAL; } T_CRLF {
			SDP->session_name = *$3;
			MEMMAN_DELETE($3); delete $3; }
;

sess_connection:  connection {
			SDP->connection = *$1;
			MEMMAN_DELETE($1); delete $1; }
;

connection:	  /* empty */	{ $$ = new t_sdp_connection(); MEMMAN_NEW($$); }
		| T_LINE_CONNECTION network_type address_type T_TOKEN
		  T_CRLF {
			$$ = new t_sdp_connection();
			MEMMAN_NEW($$);
			$$->network_type = $2;
			$$->address_type = $3;
			$$->address = *$4;
			MEMMAN_DELETE($4); delete $4; }
;

sess_attributes:  attributes {
			SDP->attributes = *$1;
			MEMMAN_DELETE($1); delete $1; }
;

attributes:	  /* emtpy */	{ $$ = new list<t_sdp_attr>; MEMMAN_NEW($$); }
		| attributes attribute {
			$$->push_back(*$2);
			MEMMAN_DELETE($2); delete $2; }
;

attribute:	  T_LINE_ATTRIBUTE attribute2 T_CRLF {
			$$ = $2; }
;

attribute2:	  T_TOKEN {
			$$ = new t_sdp_attr(*$1);
			MEMMAN_NEW($$);
			MEMMAN_DELETE($1); delete $1; }
		| T_TOKEN ':' { CTX_LINE; } T_LINE { CTX_INITIAL; } {
			$$ = new t_sdp_attr(*$1, *$4);
			MEMMAN_NEW($$);
			MEMMAN_DELETE($1); delete $1;
			MEMMAN_DELETE($4); delete $4; }
;

media_list:	  /* empty */
		| media_list media {
			SDP->media.push_back(*$2);
			MEMMAN_DELETE($2); delete $2; }
;

/* The unknown_lines cause an expected shift/reduce conflict */
media:		  T_LINE_MEDIA T_TOKEN { CTX_NUM; } T_NUM { CTX_INITIAL; }
		  transport formats T_CRLF unknown_lines connection
		  unknown_lines attributes {
		  	$$ = new t_sdp_media();
			MEMMAN_NEW($$);
			
			if ($4 > 65535) YYERROR;
			
			$$->media_type = tolower(*$2);
			$$->port = $4;
			$$->transport = *$6;
			
			/* The format depends on the media type */
			switch($$->get_media_type()) {
			case SDP_AUDIO:
			case SDP_VIDEO:
				/* Numeric format */
				for (list<string>::const_iterator it = $7->begin(); it != $7->end(); ++it) {
					if (is_number(*it)) $$->formats.push_back(atoi(it->c_str()));
				}
				
				break;
			default:
				/* Alpha numeric format */
				$$->alpha_num_formats = *$7;
			}
			
			$$->connection = *$10;
			$$->attributes = *$12;
			MEMMAN_DELETE($2); delete $2;
			MEMMAN_DELETE($6); delete $6;
			MEMMAN_DELETE($7); delete $7;
			MEMMAN_DELETE($10); delete $10;
			MEMMAN_DELETE($12); delete $12; }
;

transport:	  T_TOKEN { 
			$$ = $1; }
		| T_TOKEN '/' T_TOKEN {
			$$ = new string(*$1 + '/' + *$3); 
			MEMMAN_NEW($$);
			MEMMAN_DELETE($1); delete $1; 
			MEMMAN_DELETE($3); delete $3; }

// For RTP/AVP a format is a number. For other transport protocols,
// non-numerical formats are possible.
formats:	  /* empty */	{ $$ = new list<string>; MEMMAN_NEW($$); }
		| formats T_TOKEN {
			$$->push_back(*$2);
			MEMMAN_DELETE($2);
			delete $2; }
;

/* Skip unknown lines */
unknown_lines:	  /* empty */
		| unknown_lines unknown_line
;

unknown_line:	  T_LINE_UNKNOWN { CTX_LINE; } T_LINE { CTX_INITIAL; }
		  T_CRLF {
		  	MEMMAN_DELETE($3); delete $3; }
;

%%

void
yysdperror (const char *s)  /* Called by yysdpparse on error */
{
  // printf ("%s\n", s);
}