summaryrefslogtreecommitdiffstats
path: root/src/parser/parser.yxx
blob: f033e80e430599fd62dee04b4c4fdb57227ba3df (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
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
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
/*
    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 <cstdio>
#include <cstdlib>
#include <string>
#include "media_type.h"
#include "parameter.h"
#include "parse_ctrl.h"
#include "request.h"
#include "response.h"
#include "util.h"
#include "audits/memman.h"

using namespace std;

extern int yylex(void);
void yyerror(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

%union {
	int			yyt_int;
	ulong			yyt_ulong;
	float			yyt_float;
	string			*yyt_str;
	t_parameter		*yyt_param;
	list<t_parameter>	*yyt_params;
	t_media			*yyt_media;
	t_coding		*yyt_coding;
	t_language		*yyt_language;
	t_alert_param		*yyt_alert_param;
	t_info_param		*yyt_info_param;
	list<t_contact_param>	*yyt_contacts;
	t_contact_param		*yyt_contact;
	t_error_param		*yyt_error_param;
	t_identity		*yyt_from_addr;
	t_route			*yyt_route;
	t_server		*yyt_server;
	t_via			*yyt_via;
	t_warning		*yyt_warning;
	t_digest_response	*yyt_dig_resp;
	t_credentials		*yyt_credentials;
	t_digest_challenge	*yyt_dig_chlg;
	t_challenge		*yyt_challenge;
}

%token <yyt_ulong>	T_NUM
%token <yyt_str>	T_TOKEN
%token <yyt_str>	T_QSTRING
%token <yyt_str>	T_COMMENT
%token <yyt_str>	T_LINE
%token <yyt_str>	T_URI
%token			T_URI_WILDCARD
%token <yyt_str>	T_DISPLAY
%token <yyt_str>	T_LANG
%token <yyt_str>	T_WORD
%token <yyt_int>	T_WKDAY
%token <yyt_int>	T_MONTH
%token			T_GMT
%token			T_SIP
%token <yyt_str>	T_METHOD
%token			T_AUTH_DIGEST
%token <yyt_str>	T_AUTH_OTHER
%token <yyt_str>	T_IPV6ADDR
%token <yyt_str>	T_PARAMVAL

%token			T_HDR_ACCEPT
%token			T_HDR_ACCEPT_ENCODING
%token			T_HDR_ACCEPT_LANGUAGE
%token			T_HDR_ALERT_INFO
%token			T_HDR_ALLOW
%token			T_HDR_ALLOW_EVENTS
%token			T_HDR_AUTHENTICATION_INFO
%token			T_HDR_AUTHORIZATION
%token			T_HDR_CALL_ID
%token			T_HDR_CALL_INFO
%token			T_HDR_CONTACT
%token			T_HDR_CONTENT_DISP
%token			T_HDR_CONTENT_ENCODING
%token			T_HDR_CONTENT_LANGUAGE
%token			T_HDR_CONTENT_LENGTH
%token			T_HDR_CONTENT_TYPE
%token			T_HDR_CSEQ
%token			T_HDR_DATE
%token			T_HDR_ERROR_INFO
%token			T_HDR_EVENT
%token			T_HDR_EXPIRES
%token			T_HDR_FROM
%token			T_HDR_IN_REPLY_TO
%token			T_HDR_MAX_FORWARDS
%token			T_HDR_MIN_EXPIRES
%token			T_HDR_MIME_VERSION
%token			T_HDR_ORGANIZATION
%token			T_HDR_P_ASSERTED_IDENTITY
%token			T_HDR_P_PREFERRED_IDENTITY
%token			T_HDR_PRIORITY
%token			T_HDR_PRIVACY
%token			T_HDR_PROXY_AUTHENTICATE
%token			T_HDR_PROXY_AUTHORIZATION
%token			T_HDR_PROXY_REQUIRE
%token			T_HDR_RACK
%token			T_HDR_RECORD_ROUTE
%token			T_HDR_SERVICE_ROUTE
%token			T_HDR_REFER_SUB
%token			T_HDR_REFER_TO
%token			T_HDR_REFERRED_BY
%token			T_HDR_REPLACES
%token			T_HDR_REPLY_TO
%token			T_HDR_REQUIRE
%token			T_HDR_REQUEST_DISPOSITION
%token			T_HDR_RETRY_AFTER
%token			T_HDR_ROUTE
%token			T_HDR_RSEQ
%token			T_HDR_SERVER
%token			T_HDR_SIP_ETAG
%token			T_HDR_SIP_IF_MATCH
%token			T_HDR_SUBJECT
%token			T_HDR_SUBSCRIPTION_STATE
%token			T_HDR_SUPPORTED
%token			T_HDR_TIMESTAMP
%token			T_HDR_TO
%token			T_HDR_UNSUPPORTED
%token			T_HDR_USER_AGENT
%token			T_HDR_VIA
%token			T_HDR_WARNING
%token			T_HDR_WWW_AUTHENTICATE
%token <yyt_str>	T_HDR_UNKNOWN

%token			T_CRLF

%token			T_ERROR

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

%destructor { MEMMAN_DELETE($$); delete $$; } 	T_TOKEN;
%destructor { MEMMAN_DELETE($$); delete $$; }	T_QSTRING
%destructor { MEMMAN_DELETE($$); delete $$; }	T_COMMENT
%destructor { MEMMAN_DELETE($$); delete $$; }	T_LINE
%destructor { MEMMAN_DELETE($$); delete $$; }	T_URI
%destructor { MEMMAN_DELETE($$); delete $$; }	T_DISPLAY
%destructor { MEMMAN_DELETE($$); delete $$; }	T_LANG
%destructor { MEMMAN_DELETE($$); delete $$; }	T_WORD
%destructor { MEMMAN_DELETE($$); delete $$; }	T_METHOD
%destructor { MEMMAN_DELETE($$); delete $$; }	T_AUTH_OTHER
%destructor { MEMMAN_DELETE($$); delete $$; }	T_IPV6ADDR
%destructor { MEMMAN_DELETE($$); delete $$; }	T_PARAMVAL
%destructor { MEMMAN_DELETE($$); delete $$; }	T_HDR_UNKNOWN

%type <yyt_alert_param>	alert_param
%type <yyt_params>	auth_params
%type <yyt_str>		call_id
%type <yyt_challenge>	challenge
%type <yyt_str>		comment
%type <yyt_contact>	contact_addr
%type <yyt_contact>	contact_param
%type <yyt_contacts>	contacts
%type <yyt_coding>	content_coding
%type <yyt_credentials>	credentials
%type <yyt_float>	delay
%type <yyt_dig_chlg>	digest_challenge
%type <yyt_dig_resp>	digest_response
%type <yyt_str>		display_name
%type <yyt_error_param>	error_param
%type <yyt_from_addr>	from_addr
%type <yyt_str>		hdr_unknown
%type <yyt_via>		host
%type <yyt_str>		ipv6reference
%type <yyt_info_param>	info_param
%type <yyt_language>	language
%type <yyt_media>	media_range
%type <yyt_param>	parameter
%type <yyt_str>		parameter_val
%type <yyt_params>	parameters
%type <yyt_float>	q_factor
%type <yyt_route>	rec_route
%type <yyt_via>		sent_protocol
%type <yyt_server>	server
%type <yyt_str>		sip_version
%type <yyt_float>	timestamp
%type <yyt_via>		via_parm
%type <yyt_warning>	warning

%destructor { MEMMAN_DELETE($$); delete $$; }	alert_param
%destructor { MEMMAN_DELETE($$); delete $$; }	auth_params
%destructor { MEMMAN_DELETE($$); delete $$; }	call_id
%destructor { MEMMAN_DELETE($$); delete $$; }	challenge
%destructor { MEMMAN_DELETE($$); delete $$; }	comment
%destructor { MEMMAN_DELETE($$); delete $$; }	contact_addr
%destructor { MEMMAN_DELETE($$); delete $$; }	contact_param
%destructor { MEMMAN_DELETE($$); delete $$; }	contacts
%destructor { MEMMAN_DELETE($$); delete $$; }	content_coding
%destructor { MEMMAN_DELETE($$); delete $$; }	credentials
%destructor { MEMMAN_DELETE($$); delete $$; }	digest_challenge
%destructor { MEMMAN_DELETE($$); delete $$; }	digest_response
%destructor { MEMMAN_DELETE($$); delete $$; }	display_name
%destructor { MEMMAN_DELETE($$); delete $$; }	error_param
%destructor { MEMMAN_DELETE($$); delete $$; }	from_addr
%destructor { MEMMAN_DELETE($$); delete $$; }	hdr_unknown
%destructor { MEMMAN_DELETE($$); delete $$; }	host
%destructor { MEMMAN_DELETE($$); delete $$; }	ipv6reference
%destructor { MEMMAN_DELETE($$); delete $$; }	info_param
%destructor { MEMMAN_DELETE($$); delete $$; }	language
%destructor { MEMMAN_DELETE($$); delete $$; }	media_range
%destructor { MEMMAN_DELETE($$); delete $$; }	parameter
%destructor { MEMMAN_DELETE($$); delete $$; }	parameter_val
%destructor { MEMMAN_DELETE($$); delete $$; }	parameters
%destructor { MEMMAN_DELETE($$); delete $$; }	rec_route
%destructor { MEMMAN_DELETE($$); delete $$; }	sent_protocol
%destructor { MEMMAN_DELETE($$); delete $$; }	server
%destructor { MEMMAN_DELETE($$); delete $$; }	sip_version
%destructor { MEMMAN_DELETE($$); delete $$; }	via_parm
%destructor { MEMMAN_DELETE($$); delete $$; }	warning

%%
sip_message:	{ CTXT_NEW; } sip_message2
;

sip_message2:	  request
		| response
		| error T_NULL {
			/* KLUDGE to work around a memory leak in bison.
			 * T_NULL does never match, so the parser never
			 * gets here. The error keyword causes bison
			 * to eat all input and destroy all tokens returned
			 * by the parser.
			 * Without this workaround the following input causes
			 * the parser to leak:
			 *
			 *   INVITE INVITE ....
			 *
			 * In request_line a T_METHOD is returned as look ahead
			 * token when bison tries to match sip_version.
			 * This does not match, but the look ahead token is
			 * never destructed by Bison.
			 */
			YYABORT;
		}
;

request:	  request_line headers T_CRLF {
		  	/* Parsing stops here. Remaining text is
			 * not parsed.
			 */
		  	YYACCEPT; }
;

// KLUDGE: The use of CTXT_NEW is a kludge, to get the T_SIP symbol
//         for the SIP version
request_line:	  T_METHOD { CTXT_URI; } T_URI { CTXT_NEW; }
		  sip_version T_CRLF {
		  	MSG = new t_request();
			MEMMAN_NEW(MSG);
			((t_request *)MSG)->set_method(*$1);
			((t_request *)MSG)->uri.set_url(*$3);
			MSG->version = *$5;
			MEMMAN_DELETE($1); delete $1;
			MEMMAN_DELETE($3); delete $3;
			MEMMAN_DELETE($5); delete $5;

			if (!((t_request *)MSG)->uri.is_valid()) {
				MEMMAN_DELETE(MSG); delete MSG;
				MSG = NULL;
				YYABORT;
			} }
;

sip_version:	  T_SIP { CTXT_INITIAL; } '/' T_TOKEN {
			$$ = $4; }
;

response:	  status_line headers T_CRLF {
		  	/* Parsing stops here. Remaining text is
			 * not parsed.
			 */
		  	YYACCEPT; }
;

status_line:	  sip_version { CTXT_NUM; } T_NUM { CTXT_LINE; } T_LINE
		  { CTXT_INITIAL; } T_CRLF {
			MSG = new t_response();
			MEMMAN_NEW(MSG);
		  	MSG->version = *$1;
			((t_response *)MSG)->code = $3;
			((t_response *)MSG)->reason = trim(*$5);
			MEMMAN_DELETE($1); delete $1;
			MEMMAN_DELETE($5); delete $5; }
;

headers:	  /* empty */
		| headers header
;

header:		  hd_accept hdr_accept T_CRLF
		| hd_accept_encoding hdr_accept_encoding T_CRLF
		| hd_accept_language hdr_accept_language T_CRLF
		| hd_alert_info hdr_alert_info T_CRLF
		| hd_allow hdr_allow T_CRLF
		| hd_allow_events hdr_allow_events T_CRLF
		| hd_authentication_info hdr_authentication_info T_CRLF
		| hd_authorization hdr_authorization T_CRLF
		| hd_call_id hdr_call_id T_CRLF
		| hd_call_info hdr_call_info T_CRLF
		| hd_contact hdr_contact T_CRLF
		| hd_content_disp hdr_content_disp T_CRLF
		| hd_content_encoding hdr_content_encoding T_CRLF
		| hd_content_language hdr_content_language T_CRLF
		| hd_content_length hdr_content_length T_CRLF
		| hd_content_type hdr_content_type T_CRLF
		| hd_cseq hdr_cseq T_CRLF
		| hd_date hdr_date T_CRLF
		| hd_event hdr_event T_CRLF
		| hd_error_info hdr_error_info T_CRLF
		| hd_expires hdr_expires T_CRLF
		| hd_from hdr_from T_CRLF
		| hd_in_reply_to hdr_in_reply_to T_CRLF
		| hd_max_forwards hdr_max_forwards T_CRLF
		| hd_min_expires hdr_min_expires T_CRLF
		| hd_mime_version hdr_mime_version T_CRLF
		| hd_organization hdr_organization T_CRLF
		| hd_p_asserted_identity hdr_p_asserted_identity T_CRLF
		| hd_p_preferred_identity hdr_p_preferred_identity T_CRLF
		| hd_priority hdr_priority T_CRLF
		| hd_privacy hdr_privacy T_CRLF
		| hd_proxy_authenticate hdr_proxy_authenticate T_CRLF
		| hd_proxy_authorization hdr_proxy_authorization T_CRLF
		| hd_proxy_require hdr_proxy_require T_CRLF
		| hd_rack hdr_rack T_CRLF
		| hd_record_route hdr_record_route T_CRLF
		| hd_refer_sub hdr_refer_sub T_CRLF
		| hd_refer_to hdr_refer_to T_CRLF
		| hd_referred_by hdr_referred_by T_CRLF
		| hd_replaces hdr_replaces T_CRLF
		| hd_reply_to hdr_reply_to T_CRLF
		| hd_require hdr_require T_CRLF
		| hd_request_disposition hdr_request_disposition T_CRLF
		| hd_retry_after hdr_retry_after T_CRLF
		| hd_route hdr_route T_CRLF
		| hd_rseq hdr_rseq T_CRLF
		| hd_server hdr_server T_CRLF
		| hd_service_route hdr_service_route T_CRLF
		| hd_sip_etag hdr_sip_etag T_CRLF
		| hd_sip_if_match hdr_sip_if_match T_CRLF
		| hd_subject hdr_subject T_CRLF
		| hd_subscription_state hdr_subscription_state T_CRLF
		| hd_supported hdr_supported T_CRLF
		| hd_timestamp hdr_timestamp T_CRLF
		| hd_to hdr_to T_CRLF
		| hd_unsupported hdr_unsupported T_CRLF
		| hd_user_agent hdr_user_agent T_CRLF
		| hd_via hdr_via T_CRLF
		| hd_warning hdr_warning T_CRLF
		| hd_www_authenticate hdr_www_authenticate T_CRLF
		| T_HDR_UNKNOWN ':' hdr_unknown T_CRLF {
			MSG->add_unknown_header(*$1, trim(*$3));
			MEMMAN_DELETE($1); delete $1;
			MEMMAN_DELETE($3); delete $3; }
		| hd_accept error T_CRLF
			{ PARSE_ERROR("Accept"); }
		| hd_accept_encoding error T_CRLF
			{ PARSE_ERROR("Accept-Encoding"); }
		| hd_accept_language error T_CRLF
			{ PARSE_ERROR("Accept-Language"); }
		| hd_alert_info error T_CRLF
			{ PARSE_ERROR("Alert-Info"); }
		| hd_allow error T_CRLF
			{ PARSE_ERROR("Allow"); }
		| hd_allow_events error T_CRLF
			{ PARSE_ERROR("Allow-Events"); }
		| hd_authentication_info error T_CRLF
			{ PARSE_ERROR("Authentication-Info"); }
		| hd_authorization error T_CRLF
			{ PARSE_ERROR("Authorization"); }
		| hd_call_id error T_CRLF
			{ PARSE_ERROR("Call-ID"); }
		| hd_call_info error T_CRLF
			{ PARSE_ERROR("Call-Info"); }
		| hd_contact error T_CRLF
			{ PARSE_ERROR("Contact"); }
		| hd_content_disp error T_CRLF
			{ PARSE_ERROR("Content-Disposition"); }
		| hd_content_encoding error T_CRLF
			{ PARSE_ERROR("Content-Encoding"); }
		| hd_content_language error T_CRLF
			{ PARSE_ERROR("Content-Language"); }
		| hd_content_length 	error T_CRLF
			{ PARSE_ERROR("Content-Length"); }
		| hd_content_type error T_CRLF
			{ PARSE_ERROR("Content-Type"); }
		| hd_cseq error T_CRLF
			{ PARSE_ERROR("CSeq"); }
		| hd_date error T_CRLF
			{ PARSE_ERROR("Date"); }
		| hd_error_info error T_CRLF
			{ PARSE_ERROR("Error-Info"); }
		| hd_event error T_CRLF
			{ PARSE_ERROR("Event"); }
		| hd_expires error T_CRLF
			{ PARSE_ERROR("Expires"); }
		| hd_from error T_CRLF
			{ PARSE_ERROR("From"); }
		| hd_in_reply_to error T_CRLF
			{ PARSE_ERROR("In-Reply-To"); }
		| hd_max_forwards error T_CRLF
			{ PARSE_ERROR("Max-Forwards"); }
		| hd_min_expires error T_CRLF
			{ PARSE_ERROR("Min-Expires"); }
		| hd_mime_version error T_CRLF
			{ PARSE_ERROR("MIME-Version"); }
		| hd_organization error T_CRLF
			{ PARSE_ERROR("Organization"); }
		| hd_p_asserted_identity error T_CRLF
			{ PARSE_ERROR("P-Asserted-Identity"); }
		| hd_p_preferred_identity error T_CRLF
			{ PARSE_ERROR("P-Preferred-Identity"); }
		| hd_priority error T_CRLF
			{ PARSE_ERROR("Priority"); }
		| hd_privacy error T_CRLF
			{ PARSE_ERROR("Privacy"); }
		| hd_proxy_authenticate error T_CRLF
			{ PARSE_ERROR("Proxy-Authenticate"); }
		| hd_proxy_authorization error T_CRLF
			{ PARSE_ERROR("Proxy-Authorization"); }
		| hd_proxy_require error T_CRLF
			{ PARSE_ERROR("Proxy-Require"); }
		| hd_rack error T_CRLF
			{ PARSE_ERROR("RAck"); }
		| hd_record_route error T_CRLF
			{ PARSE_ERROR("Record-Route"); }
		| hd_refer_sub error T_CRLF
			{ PARSE_ERROR("Refer-Sub"); }
		| hd_refer_to error T_CRLF
			{ PARSE_ERROR("Refer-To"); }
		| hd_referred_by error T_CRLF
			{ PARSE_ERROR("Referred-By"); }
		| hd_replaces error T_CRLF
			{ PARSE_ERROR("Replaces"); }
		| hd_reply_to error T_CRLF
			{ PARSE_ERROR("Reply-To"); }
		| hd_require error T_CRLF
			{ PARSE_ERROR("Require"); }
		| hd_request_disposition error T_CRLF
			{ PARSE_ERROR("Request-Disposition"); }
		| hd_retry_after error T_CRLF
			{ PARSE_ERROR("Retry-After"); }
		| hd_route error T_CRLF
			{ PARSE_ERROR("Route"); }
		| hd_rseq error T_CRLF
			{ PARSE_ERROR("RSeq"); }
		| hd_server error T_CRLF
			{ PARSE_ERROR("Server"); }
		| hd_service_route error T_CRLF
			{ PARSE_ERROR("Service-Route"); }
		| hd_sip_etag error T_CRLF
			{ PARSE_ERROR("SIP-ETag"); }
		| hd_sip_if_match error T_CRLF
			{ PARSE_ERROR("SIP-If-Match"); }
		| hd_subject error T_CRLF
			{ PARSE_ERROR("Subject"); }
		| hd_subscription_state error T_CRLF
			{ PARSE_ERROR("Subscription-State"); }
		| hd_supported error T_CRLF
			{ PARSE_ERROR("Supported"); }
		| hd_timestamp error T_CRLF
			{ PARSE_ERROR("Timestamp"); }
		| hd_to error T_CRLF
			{ PARSE_ERROR("To"); }
		| hd_unsupported error T_CRLF
			{ PARSE_ERROR("Unsupported"); }
		| hd_user_agent error T_CRLF
			{ PARSE_ERROR("User-Agent"); }
		| hd_via error T_CRLF
			{ PARSE_ERROR("Via"); }
		| hd_warning error T_CRLF
			{ PARSE_ERROR("Warning"); }
		| hd_www_authenticate error T_CRLF
			{ PARSE_ERROR("WWW-Authenticate"); }
;


// KLUDGE
// These rules are needed to get the error recovery working.
// Many header rules start with a context change. As Bison uses
// one token look ahead to determine a matching rule, the context
// change must be done before Bison tries to match the error rule.
// Changing the context header and once again at the header rule
// does no harm as the context change operator is idem-potent.

hd_accept:		T_HDR_ACCEPT ':'
;
hd_accept_encoding:	T_HDR_ACCEPT_ENCODING ':'
;
hd_accept_language:	T_HDR_ACCEPT_LANGUAGE ':' { CTXT_LANG; }
;
hd_alert_info:		T_HDR_ALERT_INFO ':'
;
hd_allow:		T_HDR_ALLOW ':'
;
hd_allow_events:	T_HDR_ALLOW_EVENTS ':'
;
hd_authentication_info:	T_HDR_AUTHENTICATION_INFO ':'
;
hd_authorization:	T_HDR_AUTHORIZATION ':' { CTXT_AUTH_SCHEME; }
;
hd_call_id:		T_HDR_CALL_ID ':' { CTXT_WORD; }
;
hd_call_info:		T_HDR_CALL_INFO ':'
;
hd_contact:		T_HDR_CONTACT ':' { CTXT_URI_SPECIAL; }
;
hd_content_disp:	T_HDR_CONTENT_DISP ':'
;
hd_content_encoding:	T_HDR_CONTENT_ENCODING ':'
;
hd_content_language:	T_HDR_CONTENT_LANGUAGE ':' { CTXT_LANG; }
;
hd_content_length:	T_HDR_CONTENT_LENGTH ':' { CTXT_NUM; }
;
hd_content_type:	T_HDR_CONTENT_TYPE ':'
;
hd_cseq:		T_HDR_CSEQ ':' { CTXT_NUM; }
;
hd_date:		T_HDR_DATE ':' { CTXT_DATE;}
;
hd_error_info:		T_HDR_ERROR_INFO ':'
;
hd_event:		T_HDR_EVENT ':'
;
hd_expires:		T_HDR_EXPIRES ':' { CTXT_NUM; }
;
hd_from:		T_HDR_FROM ':' { CTXT_URI_SPECIAL; }
;
hd_in_reply_to:		T_HDR_IN_REPLY_TO ':' { CTXT_WORD; }
;
hd_max_forwards:	T_HDR_MAX_FORWARDS ':' { CTXT_NUM; }
;
hd_min_expires:		T_HDR_MIN_EXPIRES ':' { CTXT_NUM; }
;
hd_mime_version:	T_HDR_MIME_VERSION ':'
;
hd_organization:	T_HDR_ORGANIZATION ':' { CTXT_LINE; }
;
hd_p_asserted_identity:	T_HDR_P_ASSERTED_IDENTITY ':' { CTXT_URI_SPECIAL; }
;
hd_p_preferred_identity: T_HDR_P_PREFERRED_IDENTITY ':' { CTXT_URI_SPECIAL; }
;
hd_priority:		T_HDR_PRIORITY ':'
;
hd_privacy:		T_HDR_PRIVACY ':'
;
hd_proxy_authenticate:	T_HDR_PROXY_AUTHENTICATE ':' { CTXT_AUTH_SCHEME; }
;
hd_proxy_authorization:	T_HDR_PROXY_AUTHORIZATION ':' { CTXT_AUTH_SCHEME; }
;
hd_proxy_require:	T_HDR_PROXY_REQUIRE ':'
;
hd_rack:		T_HDR_RACK ':' { CTXT_NUM; }
;
hd_record_route:	T_HDR_RECORD_ROUTE ':' { CTXT_URI; }
;
hd_refer_sub:		T_HDR_REFER_SUB ':'
;
hd_refer_to:		T_HDR_REFER_TO ':' { CTXT_URI_SPECIAL; }
;
hd_referred_by:		T_HDR_REFERRED_BY ':' { CTXT_URI_SPECIAL; }
;
hd_replaces:		T_HDR_REPLACES ':' { CTXT_WORD; }
;
hd_reply_to:		T_HDR_REPLY_TO ':' { CTXT_URI_SPECIAL; }
;
hd_require:		T_HDR_REQUIRE ':'
;
hd_request_disposition:	T_HDR_REQUEST_DISPOSITION ':'
;
hd_retry_after:		T_HDR_RETRY_AFTER ':' { CTXT_NUM; }
;
hd_route:		T_HDR_ROUTE ':' { CTXT_URI; }
;
hd_rseq:		T_HDR_RSEQ ':' { CTXT_NUM; }
;
hd_server:		T_HDR_SERVER ':'
;
hd_service_route:	T_HDR_SERVICE_ROUTE ':' { CTXT_URI; }
;
hd_sip_etag:		T_HDR_SIP_ETAG ':'
;
hd_sip_if_match:	T_HDR_SIP_IF_MATCH ':'
;
hd_subject:		T_HDR_SUBJECT ':' { CTXT_LINE; }
;
hd_subscription_state:	T_HDR_SUBSCRIPTION_STATE ':'
;
hd_supported:		T_HDR_SUPPORTED ':'
;
hd_timestamp:		T_HDR_TIMESTAMP ':' { CTXT_NUM; }
;
hd_to:			T_HDR_TO ':' { CTXT_URI_SPECIAL; }
;
hd_unsupported:		T_HDR_UNSUPPORTED ':'
;
hd_user_agent:		T_HDR_USER_AGENT ':'
;
hd_via:			T_HDR_VIA ':'
;
hd_warning:		T_HDR_WARNING ':' { CTXT_NUM; }
;
hd_www_authenticate:	T_HDR_WWW_AUTHENTICATE ':' { CTXT_AUTH_SCHEME; }
;

hdr_accept:	  /* empty */
		| media_range parameters {
			$1->add_params(*$2);
			MSG->hdr_accept.add_media(*$1);
			MEMMAN_DELETE($1); delete $1;
			MEMMAN_DELETE($2); delete $2; }
		| hdr_accept ',' media_range parameters {
			$3->add_params(*$4);
			MSG->hdr_accept.add_media(*$3);
			MEMMAN_DELETE($3); delete $3;
			MEMMAN_DELETE($4); delete $4; }
;

media_range:	  T_TOKEN '/' T_TOKEN { $$ = new t_media(tolower(*$1), tolower(*$3));
					MEMMAN_NEW($$);
					MEMMAN_DELETE($1); delete $1;
					MEMMAN_DELETE($3); delete $3; }
;

parameters:	  /* empty */	{ $$ = new list<t_parameter>; MEMMAN_NEW($$); }
		| parameters ';' parameter {
			$1->push_back(*$3);
			$$ = $1;
			MEMMAN_DELETE($3); delete $3; }
;

parameter:	  T_TOKEN {
			$$ = new t_parameter(tolower(*$1));
			MEMMAN_NEW($$);
			MEMMAN_DELETE($1); delete $1; }
		| T_TOKEN '=' { CTXT_PARAMVAL; } parameter_val { CTXT_INITIAL; } {
			$$ = new t_parameter(tolower(*$1), *$4);
			MEMMAN_NEW($$);
			MEMMAN_DELETE($1); delete $1;
			MEMMAN_DELETE($4); delete $4; }
;

parameter_val:	  T_PARAMVAL {
			$$ = $1; }
		| T_QSTRING {
			$$ = $1; }
;

hdr_accept_encoding: content_coding {
			MSG->hdr_accept_encoding.add_coding(*$1);
			MEMMAN_DELETE($1); delete $1; }
		   | hdr_accept_encoding ',' content_coding {
			MSG->hdr_accept_encoding.add_coding(*$3);
			MEMMAN_DELETE($3); delete $3; }
;

content_coding:	  T_TOKEN {
			$$ = new t_coding(tolower(*$1));
			MEMMAN_NEW($$);
			MEMMAN_DELETE($1); delete $1; }
		| T_TOKEN q_factor {
			$$ = new t_coding(tolower(*$1));
			MEMMAN_NEW($$);
			$$->q = $2;
			MEMMAN_DELETE($1); delete $1; }
;

q_factor:	  ';' parameter {
			if ($2->name != "q") YYERROR;
			$$ = atof($2->value.c_str());
			MEMMAN_DELETE($2); delete $2;
			}
;

hdr_accept_language: { CTXT_LANG; } language {
			MSG->hdr_accept_language.add_language(*$2);
			MEMMAN_DELETE($2); delete $2; }
		   | hdr_accept_language ',' { CTXT_LANG; } language {
			MSG->hdr_accept_language.add_language(*$4);
			MEMMAN_DELETE($4); delete $4; }
;

language:	  T_LANG {
			CTXT_INITIAL;
		  	$$ = new t_language(tolower(*$1));
			MEMMAN_NEW($$);
			MEMMAN_DELETE($1); delete $1; }
		| T_LANG { CTXT_INITIAL; } q_factor {
			$$ = new t_language(tolower(*$1));
			MEMMAN_NEW($$);
			$$->q = $3;
			MEMMAN_DELETE($1); delete $1; }
;

hdr_alert_info:	  alert_param {
			MSG->hdr_alert_info.add_param(*$1);
			MEMMAN_DELETE($1); delete $1; }
		| hdr_alert_info ',' alert_param {
			MSG->hdr_alert_info.add_param(*$3);
			MEMMAN_DELETE($3); delete $3; }
;

alert_param:	  '<' { CTXT_URI; } T_URI { CTXT_INITIAL; } '>' parameters {
		  	$$ = new t_alert_param();
			MEMMAN_NEW($$);
			$$->uri.set_url(*$3);
			$$->parameter_list = *$6;

			if (!$$->uri.is_valid()) {
				MEMMAN_DELETE($$); delete $$;
				YYERROR;
			}
			 
			MEMMAN_DELETE($3); delete $3;
			MEMMAN_DELETE($6); delete $6; }
;

hdr_allow:	  T_TOKEN {
			MSG->hdr_allow.add_method(*$1);
			MEMMAN_DELETE($1); delete $1; }
		| hdr_allow ',' T_TOKEN {
			MSG->hdr_allow.add_method(*$3);
			MEMMAN_DELETE($3); delete $3; }
;

hdr_call_id:	  { CTXT_WORD; } call_id { CTXT_INITIAL; } {
			MSG->hdr_call_id.set_call_id(*$2);
			MEMMAN_DELETE($2); delete $2; }
;

call_id:	  T_WORD { $$ = $1; }
		| T_WORD '@' T_WORD {
			$$ = new string(*$1 + '@' + *$3);
			MEMMAN_NEW($$);
			MEMMAN_DELETE($1); delete $1;
			MEMMAN_DELETE($3); delete $3; }
;

hdr_call_info:	  info_param {
			MSG->hdr_call_info.add_param(*$1);
			MEMMAN_DELETE($1); delete $1; }
		| hdr_call_info ',' info_param {
			MSG->hdr_call_info.add_param(*$3);
			MEMMAN_DELETE($3); delete $3; }
;

info_param:	  '<' { CTXT_URI; } T_URI { CTXT_INITIAL; } '>' parameters {
		  	$$ = new t_info_param();
			MEMMAN_NEW($$);
			$$->uri.set_url(*$3);
			$$->parameter_list = *$6;

			if (!$$->uri.is_valid()) {
				MEMMAN_DELETE($$); delete $$;
				YYERROR;
			}
			
			MEMMAN_DELETE($3); delete $3;
			MEMMAN_DELETE($6); delete $6; }
;

hdr_contact:	  { CTXT_URI_SPECIAL; } T_URI_WILDCARD { CTXT_INITIAL; } {
			MSG->hdr_contact.set_any(); }
		| contacts {
			MSG->hdr_contact.add_contacts(*$1);
			MEMMAN_DELETE($1); delete $1; }
;

contacts:	  contact_param {
			$$ = new list<t_contact_param>;
			MEMMAN_NEW($$);
			$$->push_back(*$1);
			MEMMAN_DELETE($1); delete $1; }
		| contacts ',' contact_param {
			$1->push_back(*$3);
			$$ = $1;
			MEMMAN_DELETE($3); delete $3; }
;

contact_param:	  contact_addr parameters {
			$$ = $1;
			list<t_parameter>::const_iterator i;
			for (i = $2->begin(); i != $2->end(); i++) {
				if (i->name == "q") {
					$$->set_qvalue(atof(i->value.c_str()));
				} else if (i->name == "expires") {
					$$->set_expires(strtoul(
						i->value.c_str(), NULL, 10));
				} else {
					$$->add_extension(*i);
				}
			}
			MEMMAN_DELETE($2); delete $2; }
;

contact_addr:	  { CTXT_URI_SPECIAL; } T_URI { CTXT_INITIAL; } {
			$$ = new t_contact_param();
			MEMMAN_NEW($$);
			$$->uri.set_url(*$2);

			if (!$$->uri.is_valid()) {
				MEMMAN_DELETE($$); delete $$;
				YYERROR;
			}
			
			MEMMAN_DELETE($2); delete $2; }
		| { CTXT_URI_SPECIAL; } display_name '<' { CTXT_URI; } T_URI { CTXT_INITIAL; } '>' {
			$$ = new t_contact_param();
			MEMMAN_NEW($$);
			$$->display = *$2;
			$$->uri.set_url(*$5);

			if (!$$->uri.is_valid()) {
				MEMMAN_DELETE($$); delete $$;
				YYERROR;
			}
			 
			MEMMAN_DELETE($2); delete $2;
			MEMMAN_DELETE($5); delete $5; }
;

display_name:	  /* empty */ { $$ = new string(); MEMMAN_NEW($$); }
		| T_DISPLAY {
			$$ = new string(rtrim(*$1));
			MEMMAN_NEW($$);
			MEMMAN_DELETE($1); delete $1; }
		| T_QSTRING { $$ = $1; }
;

hdr_content_disp: T_TOKEN parameters {
			MSG->hdr_content_disp.set_type(tolower(*$1));
			
			list<t_parameter>::const_iterator i;
			for (i = $2->begin(); i != $2->end(); i++) {
				if (i->name == "filename") {
					MSG->hdr_content_disp.set_filename(i->value);
				} else {
					MSG->hdr_content_disp.add_param(*i);
				}
			}

			MEMMAN_DELETE($1); delete $1;
			MEMMAN_DELETE($2); delete $2; }
;

hdr_content_encoding: content_coding {
			MSG->hdr_content_encoding.add_coding(*$1);
			MEMMAN_DELETE($1); delete $1; }
		    | hdr_content_encoding ',' content_coding {
			MSG->hdr_content_encoding.add_coding(*$3);
			MEMMAN_DELETE($3); delete $3; }
;

hdr_content_language: { CTXT_LANG; } language {
			MSG->hdr_content_language.add_language(*$2);
			MEMMAN_DELETE($2); delete $2; }
		    | hdr_content_language ',' { CTXT_LANG; } language {
			MSG->hdr_content_language.add_language(*$4);
			MEMMAN_DELETE($4); delete $4; }
;

hdr_content_length: { CTXT_NUM; } T_NUM { CTXT_INITIAL; } {
			MSG->hdr_content_length.set_length($2); }
;

hdr_content_type:  media_range parameters {
			$1->add_params(*$2);
			MSG->hdr_content_type.set_media(*$1);
			MEMMAN_DELETE($1); delete $1;
			MEMMAN_DELETE($2); delete $2; }
;

hdr_cseq: 	{ CTXT_NUM; } T_NUM { CTXT_INITIAL; } T_TOKEN {
			MSG->hdr_cseq.set_seqnr($2);
			MSG->hdr_cseq.set_method(*$4);
			MEMMAN_DELETE($4); delete $4; }
;

hdr_date:	{ CTXT_DATE;}
		  T_WKDAY ',' T_NUM T_MONTH T_NUM
		  T_NUM ':' T_NUM ':' T_NUM T_GMT
		{ CTXT_INITIAL; } {
			struct tm t;
			t.tm_mday = $4;
			t.tm_mon = $5;
			t.tm_year = $6 - 1900;
			t.tm_hour = $7;
			t.tm_min = $9;
			t.tm_sec = $11;
			MSG->hdr_date.set_date_gm(&t); }
;

hdr_error_info:	  error_param {
			MSG->hdr_error_info.add_param(*$1);
			MEMMAN_DELETE($1); delete $1; }
		| hdr_error_info ',' error_param {
			MSG->hdr_error_info.add_param(*$3);
			MEMMAN_DELETE($3); delete $3; }
;

error_param:	  '<' { CTXT_URI; } T_URI { CTXT_INITIAL; } '>' parameters {
		  	$$ = new t_error_param();
			MEMMAN_NEW($$);
			$$->uri.set_url(*$3);
			$$->parameter_list = *$6;

			if (!$$->uri.is_valid()) {
				MEMMAN_DELETE($$); delete $$;
				YYERROR;
			}
			
			MEMMAN_DELETE($3); delete $3;
			MEMMAN_DELETE($6); delete $6; }
;

hdr_expires:	{ CTXT_NUM; } T_NUM { CTXT_INITIAL; } {
			MSG->hdr_expires.set_time($2); }
;

hdr_from:	  { CTXT_URI_SPECIAL; } from_addr parameters {
			MSG->hdr_from.set_display($2->display);
			MSG->hdr_from.set_uri($2->uri);
			list<t_parameter>::const_iterator i;
			for (i = $3->begin(); i != $3->end(); i++) {
				if (i->name == "tag") {
					MSG->hdr_from.set_tag(i->value);
				} else {
					MSG->hdr_from.add_param(*i);
				}
			}
			MEMMAN_DELETE($2); delete $2;
			MEMMAN_DELETE($3); delete $3; }
;

from_addr:	  T_URI { CTXT_INITIAL; } {
			$$ = new t_identity();
			MEMMAN_NEW($$);
			$$->set_uri(*$1);

			if (!$$->uri.is_valid()) {
				MEMMAN_DELETE($$); delete $$;
				YYERROR;
			}
			
			MEMMAN_DELETE($1); delete $1; }
		| display_name '<' { CTXT_URI; } T_URI { CTXT_INITIAL; } '>' {
			$$ = new t_identity();
			MEMMAN_NEW($$);
			$$->set_display(*$1);
			$$->set_uri(*$4);

			if (!$$->uri.is_valid()) {
				MEMMAN_DELETE($$); delete $$;
				YYERROR;
			}
			
			MEMMAN_DELETE($1); delete $1;
			MEMMAN_DELETE($4); delete $4; }
;

hdr_in_reply_to:  { CTXT_WORD; } call_id { CTXT_INITIAL; } {
			MSG->hdr_in_reply_to.add_call_id(*$2);
			MEMMAN_DELETE($2); delete $2; }
		| hdr_in_reply_to ',' { CTXT_WORD; } call_id { CTXT_INITIAL; } {
			MSG->hdr_in_reply_to.add_call_id(*$4);
			MEMMAN_DELETE($4); delete $4; }
;

hdr_max_forwards: { CTXT_NUM; } T_NUM { CTXT_INITIAL; } {
			MSG->hdr_max_forwards.set_max_forwards($2); }
;

hdr_min_expires:  { CTXT_NUM; } T_NUM { CTXT_INITIAL; } {
			MSG->hdr_min_expires.set_time($2); }
;

hdr_mime_version: T_TOKEN {
			MSG->hdr_mime_version.set_version(*$1);
			MEMMAN_DELETE($1); delete $1; }
;

hdr_organization: { CTXT_LINE; } T_LINE { CTXT_INITIAL; } {
			MSG->hdr_organization.set_name(trim(*$2));
			MEMMAN_DELETE($2); delete $2; }
;

hdr_p_asserted_identity:  { CTXT_URI_SPECIAL; } from_addr {
				MSG->hdr_p_asserted_identity.add_identity(*$2);
				MEMMAN_DELETE($2); delete $2; }	
			| hdr_p_asserted_identity ',' from_addr {
				MSG->hdr_p_asserted_identity.add_identity(*$3);
				MEMMAN_DELETE($3); delete $3; }
;

hdr_p_preferred_identity: { CTXT_URI_SPECIAL; } from_addr {
				MSG->hdr_p_preferred_identity.add_identity(*$2);
				MEMMAN_DELETE($2); delete $2; }	
			| hdr_p_preferred_identity ',' from_addr {
				MSG->hdr_p_preferred_identity.add_identity(*$3);
				MEMMAN_DELETE($3); delete $3; }
;

hdr_priority:	  T_TOKEN {
			MSG->hdr_priority.set_priority(tolower(*$1));
			MEMMAN_DELETE($1); delete $1; }
;

hdr_privacy:	  T_TOKEN {
			MSG->hdr_privacy.add_privacy(tolower(*$1));
			MEMMAN_DELETE($1); delete $1; }
		| hdr_privacy ';' T_TOKEN {
			MSG->hdr_privacy.add_privacy(tolower(*$3));
			MEMMAN_DELETE($3); delete $3; }
;

hdr_proxy_require:   T_TOKEN {
			MSG->hdr_proxy_require.add_feature(tolower(*$1));
			MEMMAN_DELETE($1); delete $1; }
		   | hdr_proxy_require ',' T_TOKEN {
			MSG->hdr_proxy_require.add_feature(tolower(*$3));
			MEMMAN_DELETE($3); delete $3; }
;

hdr_record_route:   rec_route {
			MSG->hdr_record_route.add_route(*$1);
			MEMMAN_DELETE($1); delete $1; }
		  | hdr_record_route ',' rec_route {
		  	MSG->hdr_record_route.add_route(*$3);
			MEMMAN_DELETE($3); delete $3; }
;

rec_route:	{ CTXT_URI; } display_name '<' T_URI { CTXT_INITIAL; } '>'
		parameters {
			$$ = new t_route;
			MEMMAN_NEW($$);
			$$->display = *$2;
			$$->uri.set_url(*$4);
			$$->set_params(*$7);

			if (!$$->uri.is_valid()) {
				MEMMAN_DELETE($$); delete $$;
				YYERROR;
			}
			 
			MEMMAN_DELETE($2); delete $2;
			MEMMAN_DELETE($4); delete $4;
			MEMMAN_DELETE($7); delete $7; }
;

hdr_service_route:  rec_route {
			MSG->hdr_service_route.add_route(*$1);
			MEMMAN_DELETE($1); delete $1; }
		  | hdr_service_route ',' rec_route {
		  	MSG->hdr_service_route.add_route(*$3);
			MEMMAN_DELETE($3); delete $3; }
;

hdr_replaces:	  { CTXT_WORD; } call_id { CTXT_INITIAL; } parameters {
			MSG->hdr_replaces.set_call_id(*$2);
			
			list<t_parameter>::const_iterator i;
			for (i = $4->begin(); i != $4->end(); i++) {
				if (i->name == "to-tag") {
					MSG->hdr_replaces.set_to_tag(i->value);
				} else if (i->name == "from-tag") {
					MSG->hdr_replaces.set_from_tag(i->value);
				} else if (i->name == "early-only") {
					MSG->hdr_replaces.set_early_only(true);
				} else {
					MSG->hdr_replaces.add_param(*i);
				}
			}
			
			if (!MSG->hdr_replaces.is_valid()) YYERROR;
			
			MEMMAN_DELETE($2); delete $2;
			MEMMAN_DELETE($4); delete $4; }
;

hdr_reply_to:  { CTXT_URI_SPECIAL; } from_addr parameters {
			MSG->hdr_reply_to.set_display($2->display);
			MSG->hdr_reply_to.set_uri($2->uri);
			MSG->hdr_reply_to.set_params(*$3);
			MEMMAN_DELETE($2); delete $2;
			MEMMAN_DELETE($3); delete $3; }
;

hdr_require:	  T_TOKEN {
			MSG->hdr_require.add_feature(tolower(*$1));
			MEMMAN_DELETE($1); delete $1; }
		| hdr_proxy_require ',' T_TOKEN {
			MSG->hdr_require.add_feature(tolower(*$3));
			MEMMAN_DELETE($3); delete $3; }
;

hdr_retry_after:  { CTXT_NUM; } T_NUM { CTXT_INITIAL; } comment parameters {
			MSG->hdr_retry_after.set_time($2);
			MSG->hdr_retry_after.set_comment(*$4);
			list<t_parameter>::const_iterator i;
			for (i = $5->begin(); i != $5->end(); i++) {
				if (i->name == "duration") {
					int d = strtoul(i->value.c_str(), NULL, 10);
					MSG->hdr_retry_after.set_duration(d);
				} else {
					MSG->hdr_retry_after.add_param(*i);
				}
			}
			MEMMAN_DELETE($4); delete $4;
			MEMMAN_DELETE($5); delete $5; }
;

comment:	  /* empty */ { $$ = new string(); MEMMAN_NEW($$); }
		| '(' { CTXT_COMMENT; } T_COMMENT { CTXT_INITIAL; } ')' {
			$$ = $3; }
;

hdr_route:	  rec_route {
			MSG->hdr_route.add_route(*$1);
			MEMMAN_DELETE($1); delete $1; }
		| hdr_route ',' rec_route {
		  	MSG->hdr_route.add_route(*$3);
			MEMMAN_DELETE($3); delete $3; }
;

hdr_server:	  server {
			MSG->hdr_server.add_server(*$1);
			MEMMAN_DELETE($1); delete $1; }
		| hdr_server server {
			MSG->hdr_server.add_server(*$2);
			MEMMAN_DELETE($2); delete $2; }
;

server:		  comment {
			$$ = new t_server();
			MEMMAN_NEW($$);
			$$->comment = *$1;
			MEMMAN_DELETE($1); delete $1; }
		| T_TOKEN comment {
			$$ = new t_server();
			MEMMAN_NEW($$);
			$$->product = *$1;
			$$->comment = *$2;
			MEMMAN_DELETE($1); delete $1;
			MEMMAN_DELETE($2); delete $2; }
		| T_TOKEN '/' T_TOKEN comment {
			$$ = new t_server();
			MEMMAN_NEW($$);
			$$->product = *$1;
			$$->version = *$3;
			$$->comment = *$4;
			MEMMAN_DELETE($1); delete $1;
			MEMMAN_DELETE($3); delete $3;
			MEMMAN_DELETE($4); delete $4; }
;

hdr_subject:	{ CTXT_LINE; } T_LINE { CTXT_INITIAL; } {
			MSG->hdr_subject.set_subject(trim(*$2));
			MEMMAN_DELETE($2); delete $2; }
;

hdr_supported:	  /* empty */ {
			MSG->hdr_supported.set_empty(); }
		| T_TOKEN {
			MSG->hdr_supported.add_feature(tolower(*$1));
			MEMMAN_DELETE($1); delete $1; }
		| hdr_supported ',' T_TOKEN {
			MSG->hdr_supported.add_feature(tolower(*$3));
			MEMMAN_DELETE($3); delete $3; }
;

hdr_timestamp:	  { CTXT_NUM; } hdr_timestamp1 { CTXT_INITIAL; }
;

hdr_timestamp1:	  timestamp {
			MSG->hdr_timestamp.set_timestamp($1); }
		| timestamp delay {
			MSG->hdr_timestamp.set_timestamp($1);
			MSG->hdr_timestamp.set_delay($2); }
;

timestamp:	  T_NUM { $$ = $1; }
		| T_NUM '.' T_NUM {
			string s = int2str($1) + '.' + int2str($3);
			$$ = atof(s.c_str()); }
;

delay:	  	  T_NUM { $$ = $1; }
		| T_NUM '.' T_NUM {
			string s = int2str($1) + '.' + int2str($3);
			$$ = atof(s.c_str()); }
;

hdr_to:		  { CTXT_URI_SPECIAL; } from_addr parameters {
			MSG->hdr_to.set_display($2->display);
			MSG->hdr_to.set_uri($2->uri);
			list<t_parameter>::const_iterator i;
			for (i = $3->begin(); i != $3->end(); i++) {
				if (i->name == "tag") {
					MSG->hdr_to.set_tag(i->value);
				} else {
					MSG->hdr_to.add_param(*i);
				}
			}
			MEMMAN_DELETE($2); delete $2;
			MEMMAN_DELETE($3); delete $3; }
;

hdr_unsupported:  T_TOKEN {
			MSG->hdr_unsupported.add_feature(tolower(*$1));
			MEMMAN_DELETE($1); delete $1; }
		| hdr_unsupported ',' T_TOKEN {
			MSG->hdr_unsupported.add_feature(tolower(*$3));
			MEMMAN_DELETE($3); delete $3; }
;

hdr_user_agent:	  server {
			MSG->hdr_user_agent.add_server(*$1);
			MEMMAN_DELETE($1); delete $1; }
		| hdr_user_agent server {
			MSG->hdr_user_agent.add_server(*$2);
			MEMMAN_DELETE($2); delete $2; }
;

hdr_via:	  via_parm {
			MSG->hdr_via.add_via(*$1);
			MEMMAN_DELETE($1); delete $1; }
		| hdr_via ',' via_parm {
			MSG->hdr_via.add_via(*$3);
			MEMMAN_DELETE($3); delete $3; }
;

via_parm:	  sent_protocol host parameters {
			$$ = $1;
			$$->host = $2->host;
			$$->port = $2->port;
			list<t_parameter>::const_iterator i;
			for (i = $3->begin(); i != $3->end(); i++) {
				if (i->name == "ttl") {
					$$->ttl = atoi(i->value.c_str());
				} else if (i->name == "maddr") {
					$$->maddr = i->value;
				} else if (i->name == "received") {
					$$->received = i->value;
				} else if (i->name == "branch") {
					$$->branch = i->value;
				} else if (i->name == "rport") {
					$$->rport_present = true;
					if (i->type == t_parameter::VALUE) {
						$$->rport =
							atoi(i->value.c_str());
					}
				} else {
					$$->add_extension(*i);
				}
			}
			MEMMAN_DELETE($2); delete $2;
			MEMMAN_DELETE($3); delete $3; }
;

sent_protocol:	  T_TOKEN '/' T_TOKEN '/' T_TOKEN {
			$$ = new t_via();
			MEMMAN_NEW($$);
			$$->protocol_name = toupper(*$1);
			$$->protocol_version = *$3;
			$$->transport = toupper(*$5);
			MEMMAN_DELETE($1); delete $1;
			MEMMAN_DELETE($3); delete $3;
			MEMMAN_DELETE($5); delete $5; }
;

host:		  T_TOKEN {
			$$ = new t_via();
			MEMMAN_NEW($$);
			$$->host = *$1;
			MEMMAN_DELETE($1); delete $1; }
		| T_TOKEN ':' { CTXT_NUM; } T_NUM { CTXT_INITIAL; } {
			if ($4 > 65535) YYERROR;
			
			$$ = new t_via();
			MEMMAN_NEW($$);
			$$->host = *$1;
			$$->port = $4;
			MEMMAN_DELETE($1); delete $1; }
		| ipv6reference {
			$$ = new t_via();
			MEMMAN_NEW($$);
			$$->host = *$1;
			MEMMAN_DELETE($1); delete $1; }
		| ipv6reference ':' { CTXT_NUM; } T_NUM { CTXT_INITIAL; } {
			$$ = new t_via();
			MEMMAN_NEW($$);
			$$->host = *$1;
			$$->port = $4;
			MEMMAN_DELETE($1); delete $1; }
;

ipv6reference:	 '[' { CTXT_IPV6ADDR; } T_IPV6ADDR { CTXT_INITIAL; } ']' {
			// TODO: check correct format of IPv6 address
			$$ = new string('[' + *$3 + ']');
			MEMMAN_NEW($$);
			MEMMAN_DELETE($3); }
;

hdr_warning:	  warning {
			MSG->hdr_warning.add_warning(*$1);
			MEMMAN_DELETE($1); delete $1; }
		| hdr_warning ',' warning {
			MSG->hdr_warning.add_warning(*$3);
			MEMMAN_DELETE($3); delete $3; }
;

warning:	  { CTXT_NUM; } T_NUM { CTXT_INITIAL; } host T_QSTRING {
			$$ = new t_warning();
			MEMMAN_NEW($$);
			$$->code = $2;
			$$->host = $4->host;
			$$->port = $4->port;
			$$->text = *$5;
			MEMMAN_DELETE($4); delete $4;
			MEMMAN_DELETE($5); delete $5; }
;

hdr_unknown:	  { CTXT_LINE; } T_LINE { CTXT_INITIAL; } { $$ = $2; }
;

ainfo:		  parameter {
			if ($1->name == "nextnonce")
				MSG->hdr_auth_info.set_next_nonce($1->value);
		 	else if ($1->name == "qop")
				MSG->hdr_auth_info.set_message_qop($1->value);
			else if ($1->name == "rspauth")
				MSG->hdr_auth_info.set_response_auth($1->value);
			else if ($1->name == "cnonce")
				MSG->hdr_auth_info.set_cnonce($1->value);
			else if ($1->name == "nc") {
				MSG->hdr_auth_info.set_nonce_count(
							hex2int($1->value));
			}
			else {
				YYERROR;
			}

			MEMMAN_DELETE($1); delete $1; }
;

hdr_authentication_info:  ainfo
			| hdr_authentication_info ',' ainfo
;

digest_response:  parameter {
			$$ = new t_digest_response();
			MEMMAN_NEW($$);
			if (!$$->set_attr(*$1)) {
				MEMMAN_DELETE($$); delete $$;
				YYERROR;
			}
			MEMMAN_DELETE($1); delete $1; }
		| digest_response ',' parameter {
			$$ = $1;
			if (!$$->set_attr(*$3)) {
				YYERROR;
			}
			MEMMAN_DELETE($3); delete $3; }
;

auth_params:	  parameter {
			$$ = new list<t_parameter>;
			MEMMAN_NEW($$);
			$$->push_back(*$1);
			MEMMAN_DELETE($1); delete $1; }
		| auth_params ',' parameter {
			$$ = $1;
			$$->push_back(*$3);
			MEMMAN_DELETE($3); delete $3; }
;

credentials:	  T_AUTH_DIGEST { CTXT_INITIAL; } digest_response {
			$$ = new t_credentials;
			MEMMAN_NEW($$);
			$$->auth_scheme = AUTH_DIGEST;
			$$->digest_response = *$3;
			MEMMAN_DELETE($3); delete $3; }
		| T_AUTH_OTHER { CTXT_INITIAL; } auth_params {
			$$ = new t_credentials;
			MEMMAN_NEW($$);
			$$->auth_scheme = *$1;
			$$->auth_params = *$3;
			MEMMAN_DELETE($1); delete $1;
			MEMMAN_DELETE($3); delete $3; }
;

hdr_authorization:  { CTXT_AUTH_SCHEME; } credentials {
			MSG->hdr_authorization.add_credentials(*$2);
			MEMMAN_DELETE($2); delete $2; }
;

digest_challenge: parameter {
			$$ = new t_digest_challenge();
			MEMMAN_NEW($$);
			if (!$$->set_attr(*$1)) {
				MEMMAN_DELETE($$); delete $$;
				YYERROR;
			}
			MEMMAN_DELETE($1); delete $1; }
		| digest_challenge ',' parameter {
			$$ = $1;
			if (!$$->set_attr(*$3)) {
				YYERROR;
			}
			MEMMAN_DELETE($3); delete $3; }
;

challenge:	  T_AUTH_DIGEST { CTXT_INITIAL; } digest_challenge {
			$$ = new t_challenge;
			MEMMAN_NEW($$);
			$$->auth_scheme = AUTH_DIGEST;
			$$->digest_challenge = *$3;
			MEMMAN_DELETE($3); delete $3; }
		| T_AUTH_OTHER { CTXT_INITIAL; } auth_params {
			$$ = new t_challenge;
			MEMMAN_NEW($$);
			$$->auth_scheme = *$1;
			$$->auth_params = *$3;
			MEMMAN_DELETE($1); delete $1;
			MEMMAN_DELETE($3); delete $3; }
;

hdr_proxy_authenticate:	{ CTXT_AUTH_SCHEME; } challenge {
				MSG->hdr_proxy_authenticate.set_challenge(*$2);
				MEMMAN_DELETE($2); delete $2; }
;

hdr_proxy_authorization: { CTXT_AUTH_SCHEME; } credentials {
				MSG->hdr_proxy_authorization.
							add_credentials(*$2);
				MEMMAN_DELETE($2); delete $2; }
;

hdr_www_authenticate:	{ CTXT_AUTH_SCHEME; } challenge {
				MSG->hdr_www_authenticate.set_challenge(*$2);
				MEMMAN_DELETE($2); delete $2; }
;

hdr_rseq: 	{ CTXT_NUM; } T_NUM { CTXT_INITIAL; } {
			MSG->hdr_rseq.set_resp_nr($2); }
;

hdr_rack: 	{ CTXT_NUM; } T_NUM T_NUM { CTXT_INITIAL; } T_TOKEN {
			MSG->hdr_rack.set_resp_nr($2);
			MSG->hdr_rack.set_cseq_nr($3);
			MSG->hdr_rack.set_method(*$5);
			MEMMAN_DELETE($5); delete $5; }
;

hdr_event:	T_TOKEN parameters {
			MSG->hdr_event.set_event_type(tolower(*$1));
			list<t_parameter>::const_iterator i;
			for (i = $2->begin(); i != $2->end(); i++) {
				if (i->name == "id") {
					MSG->hdr_event.set_id(i->value);
				} else {
					MSG->hdr_event.add_event_param(*i);
				}
			}
			MEMMAN_DELETE($1); delete $1;
			MEMMAN_DELETE($2); delete $2; }
;

hdr_allow_events:	T_TOKEN {
				MSG->hdr_allow_events.add_event_type(tolower(*$1));
				MEMMAN_DELETE($1); delete $1; }
		      | hdr_allow_events ',' T_TOKEN {
		      		MSG->hdr_allow_events.add_event_type(tolower(*$3));
				MEMMAN_DELETE($3); delete $3; }
;

hdr_subscription_state:	T_TOKEN parameters {
				MSG->hdr_subscription_state.set_substate(tolower(*$1));
				list<t_parameter>::const_iterator i;
				for (i = $2->begin(); i != $2->end(); i++) {
					if (i->name == "reason") {
						MSG->hdr_subscription_state.
							set_reason(tolower(i->value));
					} else if (i->name == "expires") {
						MSG->hdr_subscription_state.
							set_expires(strtoul(
							  i->value.c_str(),
							  NULL, 10));
					} else if (i->name == "retry-after") {
						MSG->hdr_subscription_state.
							set_retry_after(strtoul(
							  i->value.c_str(),
							  NULL, 10));
					} else {
						MSG->hdr_subscription_state.
							add_extension(*i);
					}
				}
				MEMMAN_DELETE($1); delete $1;
				MEMMAN_DELETE($2); delete $2; }
;

hdr_refer_to:	  { CTXT_URI_SPECIAL; } from_addr parameters {
			MSG->hdr_refer_to.set_display($2->display);
			MSG->hdr_refer_to.set_uri($2->uri);
			MSG->hdr_refer_to.set_params(*$3);
			MEMMAN_DELETE($2); delete $2;
			MEMMAN_DELETE($3); delete $3; }
;

hdr_referred_by:  { CTXT_URI_SPECIAL; } from_addr parameters {
			MSG->hdr_referred_by.set_display($2->display);
			MSG->hdr_referred_by.set_uri($2->uri);
			list<t_parameter>::const_iterator i;
			for (i = $3->begin(); i != $3->end(); i++) {
				if (i->name == "cid") {
					MSG->hdr_referred_by.set_cid(i->value);
				} else {
					MSG->hdr_referred_by.add_param(*i);
				}
			}
			MEMMAN_DELETE($2); delete $2;
			MEMMAN_DELETE($3); delete $3; }
;

hdr_refer_sub:	  T_TOKEN parameters {
			string value(tolower(*$1));
			if (value != "true" && value != "false") {
				YYERROR;
			}
			MSG->hdr_refer_sub.set_create_refer_sub(value == "true");
			MSG->hdr_refer_sub.set_extensions(*$2);
			MEMMAN_DELETE($1); delete $1;
			MEMMAN_DELETE($2); delete $2; }
;

hdr_sip_etag:	  T_TOKEN {
			MSG->hdr_sip_etag.set_etag(*$1);
			MEMMAN_DELETE($1); delete $1; }
;
			
hdr_sip_if_match: T_TOKEN {
			MSG->hdr_sip_if_match.set_etag(*$1);
			MEMMAN_DELETE($1); delete $1; }
;

hdr_request_disposition:  T_TOKEN {
				bool ret = MSG->hdr_request_disposition.set_directive(*$1);
				if (!ret) YYERROR;
				MEMMAN_DELETE($1); delete $1; }
			| hdr_request_disposition ',' T_TOKEN {
				bool ret = MSG->hdr_request_disposition.set_directive(*$3);
				if (!ret) YYERROR;
				MEMMAN_DELETE($3); delete $3; }

%%

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