summaryrefslogtreecommitdiff
path: root/src/zcompile/zparser.y
blob: c35060e5fedfdd2d9f5e920210c3085189f24de7 (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
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
%{
/*!
 * \file zparser.y
 *
 * \author modifications by Jan Kadlec <jan.kadlec@nic.cz>,
 *         notable changes: normal allocation, parser is reentrant.
 *         most of the code by NLnet Labs
 *         Copyright (c) 2001-2011, NLnet Labs. All rights reserved.
 *
 * \brief yacc grammar for (DNS) zone files
 *
 * \addtogroup zoneparser
 * @{
 */

/*
 * Copyright (c) 2001-2011, NLnet Labs. All rights reserved.
 *
 * This software is open source.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *
 * Redistributions of source code must retain the above copyright notice,
 * this list of conditions and the following disclaimer.
 *
 * Redistributions in binary form must reproduce the above copyright notice,
 * this list of conditions and the following disclaimer in the documentation
 * and/or other materials provided with the distribution.
 *
 * Neither the name of the NLNET LABS nor the names of its contributors may
 * be used to endorse or promote products derived from this software without
 * specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 */

//#include "common.h"

#include <stdarg.h>
#include <stdio.h>
#include <string.h>
#include <assert.h>

#include "zcompile/parser-util.h"
#include "common/log.h"

#include "libknot/libknot.h"
#include "zcompile/zcompile.h"
#include "zcompile/parser-descriptor.h"
#include "zcompile/zcompile-error.h"
#include "zparser.h"

/* these need to be global, otherwise they cannot be used inside yacc */
zparser_type *parser;

#ifdef __cplusplus
extern "C"
#endif /* __cplusplus */
int zp_wrap(void);

/* this hold the nxt bits */
static uint8_t nxtbits[16];
static int dlv_warn = 1;

/* 256 windows of 256 bits (32 bytes) */
/* still need to reset the bastard somewhere */
static uint8_t nsecbits[NSEC_WINDOW_COUNT][NSEC_WINDOW_BITS_SIZE];

/* hold the highest rcode seen in a NSEC rdata , BUG #106 */
uint16_t nsec_highest_rcode;

void zp_error(void *scanner, const char *message);
int zp_lex(YYSTYPE *lvalp, void *scanner);

/* helper functions */
void zc_error(const char *fmt, ...);
void zc_warning(const char *fmt, ...);
void zc_error_prev_line(const char *fmt, ...);
void zc_warning_prev_line(const char *fmt, ...);

#define NSEC3
#ifdef NSEC3
/* parse nsec3 parameters and add the (first) rdata elements */
static void
nsec3_add_params(const char* hash_algo_str, const char* flag_str,
	const char* iter_str, const char* salt_str, int salt_len);
#endif /* NSEC3 */

knot_dname_t *error_dname; //XXX used to be const
knot_dname_t *error_domain;

%}
%union {
	knot_dname_t       *domain;
	knot_dname_t       *dname;
	struct lex_data      data;
	uint32_t             ttl;
	uint16_t             rclass;
	uint16_t             type;
	uint16_t             *unknown;
}

%pure-parser
%parse-param {void *scanner}
%lex-param {void *scanner}
%name-prefix = "zp_"

/*
 * Tokens to represent the known RR types of DNS.
 */
%token <type> T_A T_NS T_MX T_TXT T_CNAME T_AAAA T_PTR T_NXT T_KEY T_SOA T_SIG
%token <type> T_SRV T_CERT T_LOC T_MD T_MF T_MB T_MG T_MR T_NULL T_WKS T_HINFO
%token <type> T_MINFO T_RP T_AFSDB T_X25 T_ISDN T_RT T_NSAP T_NSAP_PTR T_PX
%token <type> T_GPOS T_EID T_NIMLOC T_ATMA T_NAPTR T_KX T_A6 T_DNAME T_SINK
%token <type> T_OPT T_APL T_UINFO T_UID T_GID T_UNSPEC T_TKEY T_TSIG T_IXFR
%token <type> T_AXFR T_MAILB T_MAILA T_DS T_DLV T_SSHFP T_RRSIG T_NSEC T_DNSKEY
%token <type> T_SPF T_NSEC3 T_IPSECKEY T_DHCID T_NSEC3PARAM T_TLSA

/* other tokens */
%token	       DOLLAR_TTL DOLLAR_ORIGIN NL SP NO_MEM
%token <data>  STR PREV BITLAB
%token <ttl>   T_TTL
%token <rclass> T_RRCLASS

/* unknown RRs */
%token	       URR
%token <type>  T_UTYPE

%type <type>	type_and_rdata
%type <domain>	owner dname abs_dname
%type <dname>	rel_dname label
%type <data>	wire_dname wire_abs_dname wire_rel_dname wire_label
%type <data>	concatenated_str_seq str_sp_seq str_dot_seq dotted_str
%type <data>	nxt_seq nsec_more
%type <unknown> rdata_unknown

%%
lines:	/* empty file */
    |	lines line
    ;

line:	NL
    |	sp NL
    |	NO_MEM {
    		zc_error_prev_line("Parser ran out of memory!");
		YYABORT;
	}
    |	PREV NL		{}    /* Lines containing only whitespace.  */
    |	ttl_directive
	{
	    parser->error_occurred = 0;
    }
    |	origin_directive
	{
	    parser->error_occurred = 0;
    }
    |	rr
    {	/* rr should be fully parsed */
	if (!parser->error_occurred) {
		/*!< \todo assign error to error occurred */
		/*! \todo Make sure this does not crash */
		if (parser->current_rrset->owner == NULL) {
			knot_rrset_deep_free(&(parser->current_rrset),
					       0, 0, 0);
			knot_zone_deep_free(&(parser->current_zone),
					      1);
			YYABORT;
		}
		knot_rdata_t *tmp_rdata = knot_rdata_new();
		if (tmp_rdata == NULL) {
			knot_rrset_deep_free(&(parser->current_rrset),
					       0, 0, 0);
			knot_zone_deep_free(&(parser->current_zone),
					      1);
			YYABORT;
		}

		if (knot_rdata_set_items(tmp_rdata,
		    parser->temporary_items,
		    parser->rdata_count) != 0) {
			knot_rdata_free(&tmp_rdata);
			knot_rrset_deep_free(&(parser->current_rrset), 0, 0, 0);
			knot_zone_deep_free(&(parser->current_zone), 1);
			YYABORT;
		}

		assert(parser->current_rrset->rdata == NULL);
		if (knot_rrset_add_rdata(parser->current_rrset, tmp_rdata)
		    != 0) {
		    	log_zone_error("Could not add rdata!\n");
		    }
//		tmp_rdata->next = tmp_rdata;
//		parser->current_rrset->rdata = tmp_rdata;

		if (!knot_dname_is_fqdn(parser->current_rrset->owner)) {
			knot_dname_t *tmp_dname =
				knot_dname_cat(parser->current_rrset->owner,
						 parser->root_domain);
			if (tmp_dname == NULL) {
				knot_rrset_deep_free(&(parser->current_rrset),
				                       0, 0, 0);
				knot_zone_deep_free(&(parser->current_zone),
				                      1);
				YYABORT;
			}
//			knot_rrset_set_owner(parser->current_rrset, tmp_dname);
		}

		assert(parser->current_rrset->owner != NULL);
		knot_dname_retain(parser->current_rrset->owner);
		int ret = 0;
		if ((ret = process_rr()) != 0) {
			char *name =
				knot_dname_to_str(parser->current_rrset->owner);
			log_zone_error("Error: could not process RRSet\n"
				"owner: %s reason: %s\n",
				name,
				error_to_str(knot_zcompile_error_msgs, ret));
			free(name);

			/* If the owner is not already in the table, free it. */
//			if (dnslib_dname_table_find_dname(parser->dname_table,
//				parser->current_rrset->owner) == NULL) {
//				dnslib_dname_free(&parser->
//				                  current_rrset->owner);
//			} /* This would never happen */

			if (ret == KNOTDZCOMPILE_EBADSOA) {
				knot_rrset_deep_free(&(parser->current_rrset),
						       0, 0, 0);
				knot_zone_deep_free(&(parser->current_zone),
						      1);
				YYABORT;
			} else {
				YYABORT;
				/* Free rdata, it will not be added
				 * and hence cannot be
				 * freed with rest of the zone. */
/*				knot_rdata_deep_free(&tmp_rdata,
				                       parser->
				                       current_rrset->type,
				                       0); */
			}
		}
	} else {
		/* Error occured. This could either be lack of memory, or one
		 * of the converting function was not able to convert. */
		if (parser->error_occurred == KNOTDZCOMPILE_ENOMEM) {
			/* Ran out of memory in converting functions. */
			log_zone_error("Parser ran out "
			                "of memory, aborting!\n");
			knot_rrset_deep_free(&(parser->current_rrset),
					       0, 0, 0);
			knot_zone_deep_free(&(parser->current_zone),
					      1);
			YYABORT;
		}
	}

//	printf("Current rrset name: %p (%s)\n", parser->current_rrset->owner->name,
//	knot_dname_to_str(parser->current_rrset->owner));

//	knot_dname_release(parser->current_rrset->owner);

	    parser->current_rrset->type = 0;
	    parser->rdata_count = 0;
	    parser->current_rrset->rdata = NULL;
	    parser->error_occurred = 0;
    }
    |	error NL
    ;

/* needed to cope with ( and ) in arbitary places */
sp:	SP
    |	sp SP
    ;

trail:	NL
    |	sp NL
    ;

ttl_directive:	DOLLAR_TTL sp STR trail
    {
	parser->default_ttl = zparser_ttl2int($3.str,
					      &(parser->error_occurred));
	if (parser->error_occurred == 1) {
		parser->default_ttl = DEFAULT_TTL;
		parser->error_occurred = 0;
	}

	free($3.str);
    }
    ;



origin_directive:	DOLLAR_ORIGIN sp abs_dname trail
    {
    /*!< \todo this will leak. */
	    knot_node_t *origin_node = knot_node_new($3 ,NULL, 0);
	if (parser->origin != NULL) {
//		knot_node_free(&parser->origin, 1);
	}
	    parser->origin = origin_node;
    }
    |	DOLLAR_ORIGIN sp rel_dname trail
    {
	    zc_error_prev_line("$ORIGIN directive requires"
			       "absolute domain name");
    }
    ;

rr:	owner classttl type_and_rdata
    {
	    /* Save the pointer, it might get freed! */
	    parser->current_rrset->owner = $1;
//	    parser->current_rrset->owner = $1;
//	    printf("new owner assigned: %p\n", $1);
	    parser->current_rrset->type = $3;
    }
	;

owner:	dname sp
    {
//    	char *name = knot_dname_to_str($1);
//	printf("Totally new dname: %p %s\n", $1,
//	name);
//	free(name);
	if (parser->prev_dname != NULL) {
	//	knot_dname_release(parser->prev_dname);
	}
	parser->prev_dname = $1;//knot_dname_deep_copy($1);
	knot_dname_retain(parser->prev_dname);
	$$ = $1;
    }
    |	PREV
    {
//	    printf("Name from prev_dname!: %p %s\n", parser->prev_dname,
//	    knot_dname_to_str(parser->prev_dname));
	    knot_dname_retain(parser->prev_dname);
	    $$ = parser->prev_dname;//knot_dname_deep_copy(parser->prev_dname);
    }
    ;

classttl:	/* empty - fill in the default, def. ttl and IN class */
    {
	    parser->current_rrset->ttl = parser->default_ttl;
	    parser->current_rrset->rclass = parser->default_class;
    }
    |	T_RRCLASS sp		/* no ttl */
    {
	    parser->current_rrset->ttl = parser->default_ttl;
	    parser->current_rrset->rclass = $1;
    }
    |	T_TTL sp		/* no class */
    {
	    parser->current_rrset->ttl = $1;
	    parser->current_rrset->rclass = parser->default_class;
    }
    |	T_TTL sp T_RRCLASS sp	/* the lot */
    {
	    parser->current_rrset->ttl = $1;
	    parser->current_rrset->rclass = $3;
    }
    |	T_RRCLASS sp T_TTL sp	/* the lot - reversed */
    {
	    parser->current_rrset->ttl = $3;
	    parser->current_rrset->rclass = $1;
    }
    ;

dname:	abs_dname
    |	rel_dname
    {
	    if ($1 == error_dname) {
		    $$ = error_domain;
	    } else if ($1->size + parser->origin->owner->size - 1 >
		       MAXDOMAINLEN) {
		    zc_error("domain name exceeds %d character limit",
			     MAXDOMAINLEN);
		    $$ = error_domain;
	    } else {
		    $$ = knot_dname_cat($1,
					  parser->origin->owner);
	    }
    }
    ;

abs_dname:	'.'
    {
	    $$ = parser->root_domain;
	    /* TODO how about concatenation now? */
    }
    |	'@'
    {
    	assert(parser->origin != NULL);
	$$ = parser->origin->owner;
    }
    |	rel_dname '.'
    {
	    if ($1 != error_dname) {
		    $$ = $1;
	    } else {
		    $$ = error_domain;
	    }
    }
    ;

label:	STR
    {
	    if ($1.len > MAXLABELLEN) {
		    zc_error("label exceeds %d character limit", MAXLABELLEN);
		    $$ = error_dname;
	    } else {
		    $$ = knot_dname_new_from_str($1.str, $1.len, NULL);
	$$->ref.count = 0;
	    }

	    free($1.str);

    }
    |	BITLAB
    {
	    zc_error("bitlabels are not supported."
		     "RFC2673 has status experimental.");
	    $$ = error_dname;
    }
    ;

rel_dname:	label
    |	rel_dname '.' label
    {
	    if ($1 == error_dname || $3 == error_dname) {
		    $$ = error_dname;
	    } else if ($1->size + $3->size - 1 > MAXDOMAINLEN) {
		    zc_error("domain name exceeds %d character limit",
			     MAXDOMAINLEN);
		    $$ = error_dname;
	    } else {
		    $$ = knot_dname_cat($1, $3);
//		    knot_dname_release($1); /*!< \todo check! */
		    knot_dname_free(&$3);
		}
    }
    ;

/*
 * Some dnames in rdata are handled as opaque blobs
 */

wire_dname:	wire_abs_dname
    |	wire_rel_dname
    ;

wire_abs_dname:	'.'
    {
	    char *result = malloc(2 * sizeof(char));
	    if (result == NULL) {
	    	ERR_ALLOC_FAILED;
	    	knot_rrset_deep_free(&(parser->current_rrset),
		                       0, 0, 0);
	        knot_zone_deep_free(&(parser->current_zone),
		                      1);
		YYABORT;
	    }
	    result[0] = 0;
	    result[1] = '\0';
	    $$.str = result;
	    $$.len = 1;
    }
    |	wire_rel_dname '.'
    {
	    char *result = malloc($1.len + 2 * sizeof(char));
	    if (result == NULL) {
	    	ERR_ALLOC_FAILED;
	    	knot_rrset_deep_free(&(parser->current_rrset),
		                       0, 0, 0);
	        knot_zone_deep_free(&(parser->current_zone),
		                      1);
		YYABORT;
	    }
	    memcpy(result, $1.str, $1.len);
	    result[$1.len] = 0;
	    result[$1.len+1] = '\0';
	    $$.str = result;
	    $$.len = $1.len + 1;

	    free($1.str);
;
    }
    ;

wire_label:	STR
    {
	    char *result = malloc($1.len + sizeof(char));
	    if (result == NULL) {
	    	ERR_ALLOC_FAILED;
	    	knot_rrset_deep_free(&(parser->current_rrset),
		                       0, 0, 0);
	        knot_zone_deep_free(&(parser->current_zone),
		                      1);
		YYABORT;
	    }

	    if ($1.len > MAXLABELLEN)
		    zc_error("label exceeds %d character limit", MAXLABELLEN);

	    /* make label anyway */
	    result[0] = $1.len;
	    memcpy(result+1, $1.str, $1.len);

	    $$.str = result;
	    $$.len = $1.len + 1;

	    free($1.str);
    }
    ;

wire_rel_dname:	wire_label
    |	wire_rel_dname '.' wire_label
    {
	    if ($1.len + $3.len - 3 > MAXDOMAINLEN)
		    zc_error("domain name exceeds %d character limit",
			     MAXDOMAINLEN);

	    /* make dname anyway */
	    $$.len = $1.len + $3.len;
	    $$.str = malloc($$.len + sizeof(char));
	    if ($$.str == NULL) {
	    	ERR_ALLOC_FAILED;
	    	knot_rrset_deep_free(&(parser->current_rrset),
		                       0, 0, 0);
		knot_zone_deep_free(&(parser->current_zone),
		                      1);
		YYABORT;
	    }
	    memcpy($$.str, $1.str, $1.len);
	    memcpy($$.str + $1.len, $3.str, $3.len);
	    $$.str[$$.len] = '\0';

	    free($1.str);
	    free($3.str);
    }
    ;



str_seq:	STR
    {
	    zadd_rdata_txt_wireformat(zparser_conv_text($1.str, $1.len), 1);

	    if(strcmp($1.str, ".") && strcmp($1.str, "@")
                && strcmp($1.str, "\\#")) {		// Lexer freed that
	    	free($1.str);
            }
    }
    |	str_seq sp STR
    {
	    zadd_rdata_txt_wireformat(zparser_conv_text($3.str, $3.len), 0);

	    if(strcmp($3.str, ".") && strcmp($3.str, "@")
                && strcmp($3.str, "\\#")) {		// Lexer freed that
	    	free($3.str);
            }
    }
    ;

/*
 * Generate a single string from multiple STR tokens, separated by
 * spaces or dots.
 */
concatenated_str_seq:	STR
    |	'.'
    {
	    $$.len = 1;
	    $$.str = strdup(".");
    }
    |	concatenated_str_seq sp STR
    {
	    $$.len = $1.len + $3.len + 1;
	    $$.str = malloc($$.len + 1);
	    if ($$.str == NULL) {
	    	ERR_ALLOC_FAILED;
	    	knot_rrset_deep_free(&(parser->current_rrset),
		                       0, 0, 0);
	        knot_zone_deep_free(&(parser->current_zone),
		                      1);
		YYABORT;
	    }

	    memcpy($$.str, $1.str, $1.len);
	    memcpy($$.str + $1.len, " ", 1);
	    memcpy($$.str + $1.len + 1, $3.str, $3.len);
	    $$.str[$$.len] = '\0';

	    free($1.str);
	    free($3.str);
    }
    |	concatenated_str_seq '.' STR
    {
	    $$.len = $1.len + $3.len + 1;
	    $$.str = malloc($$.len + 1);
	    if ($$.str == NULL) {
	    	ERR_ALLOC_FAILED;
	    	knot_rrset_deep_free(&(parser->current_rrset),
		                       0, 0, 0);
	        knot_zone_deep_free(&(parser->current_zone),
		                      1);
		YYABORT;
	    }
	    memcpy($$.str, $1.str, $1.len);
	    memcpy($$.str + $1.len, ".", 1);
	    memcpy($$.str + $1.len + 1, $3.str, $3.len);

	    free($1.str);
	    free($3.str);

	    $$.str[$$.len] = '\0';
    }
    ;

/* used to convert a nxt list of types */
nxt_seq:	STR
    {
	    uint16_t type = knot_rrtype_from_string($1.str);
	    if (type != 0 && type < 128) {
		    set_bit(nxtbits, type);
	    } else {
		    zc_error("bad type %d in NXT record", (int) type);
	    }

	    free($1.str);
    }
    |	nxt_seq sp STR
    {
	    uint16_t type = knot_rrtype_from_string($3.str);
	    if (type != 0 && type < 128) {
		    set_bit(nxtbits, type);
	    } else {
		    zc_error("bad type %d in NXT record", (int) type);
	    }

	    free($3.str);
    }
    ;

nsec_more:	SP nsec_more
    {
    }
    |	NL
    {
    }
    |	STR nsec_seq
    {
	    uint16_t type = knot_rrtype_from_string($1.str);
	    if (type != 0) {
		    if (type > nsec_highest_rcode) {
			    nsec_highest_rcode = type;
		    }
		    set_bitnsec(nsecbits, type);
	    } else {
		    zc_error("bad type %d in NSEC record", (int) type);
	    }

	    free($1.str);
    }
    ;

nsec_seq:	NL
	|	SP nsec_more
	;

/*
 * Sequence of STR tokens separated by spaces.	The spaces are not
 * preserved during concatenation.
 */
str_sp_seq:	STR
    |	str_sp_seq sp STR
    {
	    char *result = malloc($1.len + $3.len + 1);
	    if (result == NULL) {
	    	ERR_ALLOC_FAILED;
	    	log_zone_error("Parser ran out of memory, aborting!\n");
	    	knot_rrset_deep_free(&(parser->current_rrset),
		                       0, 0, 0);
	        knot_zone_deep_free(&(parser->current_zone),
		                      1);
		YYABORT;
	    }
	    memcpy(result, $1.str, $1.len);
	    memcpy(result + $1.len, $3.str, $3.len);
	    $$.str = result;
	    $$.len = $1.len + $3.len;
	    $$.str[$$.len] = '\0';

	    free($1.str);
	    free($3.str);
    }
    ;

/*
 * Sequence of STR tokens separated by dots.  The dots are not
 * preserved during concatenation.
 */
str_dot_seq:	STR
    |	str_dot_seq '.' STR
    {
	    char *result = malloc($1.len + $3.len + 1);
	    if (result == NULL) {
	    	ERR_ALLOC_FAILED;
	    	log_zone_error("Parser ran out of memory, aborting!\n");
	    	knot_rrset_deep_free(&(parser->current_rrset),
		                       0, 0, 0);
	        knot_zone_deep_free(&(parser->current_zone),
		                      1);
		YYABORT;
	    }
	    memcpy(result, $1.str, $1.len);
	    memcpy(result + $1.len, $3.str, $3.len);
	    $$.str = result;
	    $$.len = $1.len + $3.len;
	    $$.str[$$.len] = '\0';

	    free($1.str);
	    free($3.str);
    }
    ;

/*
 * A string that can contain dots.
 */
dotted_str:	STR
    |	'.'
    {
	$$.str = ".";
	$$.len = 1;
    }
    |	dotted_str '.'
    {
	    char *result = malloc($1.len + 2);
	    if (result == NULL) {
	    	ERR_ALLOC_FAILED;
	    	log_zone_error("Parser ran out of memory, aborting!\n");
	    	knot_rrset_deep_free(&(parser->current_rrset),
		                       0, 0, 0);
	        knot_zone_deep_free(&(parser->current_zone),
		                      1);
		YYABORT;
	    }
	    memcpy(result, $1.str, $1.len);
	    result[$1.len] = '.';
	    $$.str = result;
	    $$.len = $1.len + 1;
	    $$.str[$$.len] = '\0';

	    free($1.str);
    }
    |	dotted_str '.' STR
    {
	    char *result = malloc($1.len + $3.len + 2);
	    if (result == NULL) {
	    	ERR_ALLOC_FAILED;
	    	log_zone_error("Parser ran out of memory, aborting!\n");
	    	knot_rrset_deep_free(&(parser->current_rrset),
		                       0, 0, 0);
	        knot_zone_deep_free(&(parser->current_zone),
		                      1);
		YYABORT;
	    }
	    memcpy(result, $1.str, $1.len);
	    result[$1.len] = '.';
	    memcpy(result + $1.len + 1, $3.str, $3.len);
	    $$.str = result;
	    $$.len = $1.len + $3.len + 1;
	    $$.str[$$.len] = '\0';


	    free($1.str);
	    free($3.str);
    }
    ;

/* define what we can parse */
type_and_rdata:
    /*
     * All supported RR types.	We don't support NULL and types marked obsolete.
     */
	T_A sp rdata_a
    |	T_A sp rdata_unknown { $$ = $1; parse_unknown_rdata($1, $3); }
    |	T_NS sp rdata_domain_name
    |	T_NS sp rdata_unknown { $$ = $1; parse_unknown_rdata($1, $3); }
    |	T_MD sp rdata_domain_name { zc_warning_prev_line("MD is obsolete"); }
    |	T_MD sp rdata_unknown
    {
	    zc_warning_prev_line("MD is obsolete");
	    $$ = $1; parse_unknown_rdata($1, $3);
    }
    |	T_MF sp rdata_domain_name { zc_warning_prev_line("MF is obsolete"); }
    |	T_MF sp rdata_unknown
    {
	    zc_warning_prev_line("MF is obsolete");
	    $$ = $1;
	    parse_unknown_rdata($1, $3);
    }
    |	T_CNAME sp rdata_domain_name
    |	T_CNAME sp rdata_unknown { $$ = $1; parse_unknown_rdata($1, $3); }
    |	T_SOA sp rdata_soa
    |	T_SOA sp rdata_unknown { $$ = $1; parse_unknown_rdata($1, $3); }
    |	T_MB sp rdata_domain_name { zc_warning_prev_line("MB is obsolete"); }
    |	T_MB sp rdata_unknown
    {
	    zc_warning_prev_line("MB is obsolete");
	    $$ = $1;
	    parse_unknown_rdata($1, $3);
    }
    |	T_MG sp rdata_domain_name
    |	T_MG sp rdata_unknown { $$ = $1; parse_unknown_rdata($1, $3); }
    |	T_MR sp rdata_domain_name
    |	T_MR sp rdata_unknown { $$ = $1; parse_unknown_rdata($1, $3); }
      /* NULL */
    |	T_WKS sp rdata_wks
    |	T_WKS sp rdata_unknown { $$ = $1; parse_unknown_rdata($1, $3); }
    |	T_PTR sp rdata_domain_name
    |	T_PTR sp rdata_unknown { $$ = $1; parse_unknown_rdata($1, $3); }
    |	T_HINFO sp rdata_hinfo
    |	T_HINFO sp rdata_unknown { $$ = $1; parse_unknown_rdata($1, $3); }
    |	T_MINFO sp rdata_minfo /* Experimental */
    |	T_MINFO sp rdata_unknown { $$ = $1; parse_unknown_rdata($1, $3); }
    |	T_MX sp rdata_mx
    |	T_MX sp rdata_unknown { $$ = $1; parse_unknown_rdata($1, $3); }
    |	T_TXT sp rdata_txt
    |	T_TXT sp rdata_unknown { $$ = $1; parse_unknown_rdata($1, $3); }
    |	T_SPF sp rdata_txt
    |	T_SPF sp rdata_unknown { $$ = $1; parse_unknown_rdata($1, $3); }
    |	T_RP sp rdata_rp		/* RFC 1183 */
    |	T_RP sp rdata_unknown { $$ = $1; parse_unknown_rdata($1, $3); }
    |	T_AFSDB sp rdata_afsdb	/* RFC 1183 */
    |	T_AFSDB sp rdata_unknown { $$ = $1; parse_unknown_rdata($1, $3); }
    |	T_X25 sp rdata_x25	/* RFC 1183 */
    |	T_X25 sp rdata_unknown { $$ = $1; parse_unknown_rdata($1, $3); }
    |	T_ISDN sp rdata_isdn	/* RFC 1183 */
    |	T_ISDN sp rdata_unknown { $$ = $1; parse_unknown_rdata($1, $3); }
    |	T_IPSECKEY sp rdata_ipseckey	/* RFC 4025 */
    |	T_IPSECKEY sp rdata_unknown { $$ = $1; parse_unknown_rdata($1, $3); }
    |	T_DHCID sp rdata_dhcid
    |	T_DHCID sp rdata_unknown { $$ = $1; parse_unknown_rdata($1, $3); }
    |	T_RT sp rdata_rt		/* RFC 1183 */
    |	T_RT sp rdata_unknown { $$ = $1; parse_unknown_rdata($1, $3); }
    |	T_NSAP sp rdata_nsap	/* RFC 1706 */
    |	T_NSAP sp rdata_unknown { $$ = $1; parse_unknown_rdata($1, $3); }
    |	T_SIG sp rdata_rrsig
    |	T_SIG sp rdata_unknown { $$ = $1; parse_unknown_rdata($1, $3); }
    |	T_KEY sp rdata_dnskey
    |	T_KEY sp rdata_unknown { $$ = $1; parse_unknown_rdata($1, $3); }
    |	T_PX sp rdata_px		/* RFC 2163 */
    |	T_PX sp rdata_unknown { $$ = $1; parse_unknown_rdata($1, $3); }
    |	T_AAAA sp rdata_aaaa
    |	T_AAAA sp rdata_unknown { $$ = $1; parse_unknown_rdata($1, $3); }
    |	T_LOC sp rdata_loc
    |	T_LOC sp rdata_unknown { $$ = $1; parse_unknown_rdata($1, $3); }
    |	T_NXT sp rdata_nxt
    |	T_NXT sp rdata_unknown { $$ = $1; parse_unknown_rdata($1, $3); }
    |	T_SRV sp rdata_srv
    |	T_SRV sp rdata_unknown { $$ = $1; parse_unknown_rdata($1, $3); }
    |	T_NAPTR sp rdata_naptr	/* RFC 2915 */
    |	T_NAPTR sp rdata_unknown { $$ = $1; parse_unknown_rdata($1, $3); }
    |	T_KX sp rdata_kx		/* RFC 2230 */
    |	T_KX sp rdata_unknown { $$ = $1; parse_unknown_rdata($1, $3); }
    |	T_CERT sp rdata_cert	/* RFC 2538 */
    |	T_CERT sp rdata_unknown { $$ = $1; parse_unknown_rdata($1, $3); }
    |	T_DNAME sp rdata_domain_name /* RFC 2672 */
    |	T_DNAME sp rdata_unknown { $$ = $1; parse_unknown_rdata($1, $3); }
    |	T_APL trail		/* RFC 3123 */
    |	T_APL sp rdata_apl	/* RFC 3123 */
    |	T_APL sp rdata_unknown { $$ = $1; parse_unknown_rdata($1, $3); }
    |	T_DS sp rdata_ds
    |	T_DS sp rdata_unknown { $$ = $1; parse_unknown_rdata($1, $3); }
    |	T_DLV sp rdata_dlv
    {
	    if (dlv_warn) {
		    dlv_warn = 0;
		    zc_warning_prev_line("DLV is experimental");
	    }
    }
    |	T_DLV sp rdata_unknown
    {
	    if (dlv_warn) {
		    dlv_warn = 0;
		    zc_warning_prev_line("DLV is experimental");
	    }
	    $$ = $1;
	    parse_unknown_rdata($1, $3);
    }
    |	T_SSHFP sp rdata_sshfp
    |	T_SSHFP sp rdata_unknown { $$ = $1; parse_unknown_rdata($1, $3); }
    |	T_RRSIG sp rdata_rrsig
    |	T_RRSIG sp rdata_unknown { $$ = $1; parse_unknown_rdata($1, $3); }
    |	T_NSEC sp rdata_nsec
    |	T_NSEC sp rdata_unknown { $$ = $1; parse_unknown_rdata($1, $3); }
    |	T_NSEC3 sp rdata_nsec3
    |	T_NSEC3 sp rdata_unknown { $$ = $1; parse_unknown_rdata($1, $3); }
    |	T_NSEC3PARAM sp rdata_nsec3_param
    |	T_NSEC3PARAM sp rdata_unknown { $$ = $1; parse_unknown_rdata($1, $3); }
    |	T_TLSA sp rdata_tlsa
    |	T_TLSA sp rdata_unknown { $$ = $1; parse_unknown_rdata($1, $3); }
    |	T_DNSKEY sp rdata_dnskey
    |	T_DNSKEY sp rdata_unknown { $$ = $1; parse_unknown_rdata($1, $3); }
    |	T_UTYPE sp rdata_unknown { $$ = $1; parse_unknown_rdata($1, $3); }

    |	STR error NL
    {
	    zc_error_prev_line("unrecognized RR type '%s'", $1.str);
	    free($1.str);
    }
    |	NO_MEM
    {
    	zc_error_prev_line("parser ran out of memory!");
    	YYABORT;
    }
    ;

/*
 *
 * below are all the definition for all the different rdata
 *
 */

rdata_a:	dotted_str trail
    {
	    zadd_rdata_wireformat(zparser_conv_a($1.str));
	    free($1.str);
    }
    ;

rdata_domain_name:	dname trail
    {
	    /* convert a single dname record */
		if ($1 != NULL) {
			if (!knot_dname_is_fqdn($1)) {
			knot_dname_cat($1, parser->root_domain);
//			parser->current_rrset->owner =
//				knot_dname_cat($1, parser->root_domain);
			}
		}
	    zadd_rdata_domain($1);
    }
    ;

rdata_soa:	dname sp dname sp STR sp STR sp STR sp STR sp STR trail
    {
	    /* convert the soa data */
			if (!knot_dname_is_fqdn($1)) {
			knot_dname_cat($1, parser->root_domain);
//			parser->current_rrset->owner =
//				knot_dname_cat($1, parser->root_domain);

		}
			if (!knot_dname_is_fqdn($3)) {
			knot_dname_cat($3, parser->root_domain);
//			parser->current_rrset->owner =
//				knot_dname_cat($3, parser->root_domain);

		}
	    zadd_rdata_domain($1);	/* prim. ns */
	    zadd_rdata_domain($3);	/* email */
	    zadd_rdata_wireformat(zparser_conv_serial($5.str)); /* serial */
	    zadd_rdata_wireformat(zparser_conv_period($7.str)); /* refresh */
	    zadd_rdata_wireformat(zparser_conv_period($9.str)); /* retry */
	    zadd_rdata_wireformat(zparser_conv_period($11.str)); /* expire */
	    zadd_rdata_wireformat(zparser_conv_period($13.str)); /* minimum */

	    free($5.str);
	    free($7.str);
	    free($9.str);
	    free($11.str);
	    free($13.str);
    }
    ;

rdata_wks:	dotted_str sp STR sp concatenated_str_seq trail
    {
	    zadd_rdata_wireformat(zparser_conv_a($1.str)); /* address */
	    zadd_rdata_wireformat(zparser_conv_services($3.str, $5.str));
	    /* protocol and services */

	    free($1.str);
	    free($3.str);
	    free($5.str);
    }
    ;

rdata_hinfo:	STR sp STR trail
    {
	    zadd_rdata_wireformat(zparser_conv_text($1.str, $1.len)); /* CPU */
	    zadd_rdata_wireformat(zparser_conv_text($3.str, $3.len)); /* OS*/

	    free($1.str);
	    free($3.str);
    }
    ;

rdata_minfo:	dname sp dname trail
    {
				if (!knot_dname_is_fqdn($1)) {

			knot_dname_cat($1, parser->root_domain);

		}
					if (!knot_dname_is_fqdn($3)) {

			knot_dname_cat($3, parser->root_domain);

		}

	    /* convert a single dname record */
	    zadd_rdata_domain($1);
	    zadd_rdata_domain($3);
    }
    ;

rdata_mx:	STR sp dname trail
    {
					if (!knot_dname_is_fqdn($3)) {
			knot_dname_cat($3, parser->root_domain);
		}

	    zadd_rdata_wireformat(zparser_conv_short($1.str));  /* priority */
	    zadd_rdata_domain($3);	/* MX host */

	    free($1.str);
    }
    ;

rdata_txt:	str_seq trail
    {
	; //zadd_rdata_txt_clean_wireformat();
    }
    ;

/* RFC 1183 */
rdata_rp:	dname sp dname trail
    {
					if (!knot_dname_is_fqdn($1)) {
			knot_dname_cat($1, parser->root_domain);
		}
					if (!knot_dname_is_fqdn($3)) {
			knot_dname_cat($3, parser->root_domain);
		}

	    zadd_rdata_domain($1); /* mbox d-name */
	    zadd_rdata_domain($3); /* txt d-name */
    }
    ;

/* RFC 1183 */
rdata_afsdb:	STR sp dname trail
    {
					if (!knot_dname_is_fqdn($3)) {
			knot_dname_cat($3, parser->root_domain);
		}

	    zadd_rdata_wireformat(zparser_conv_short($1.str)); /* subtype */
	    zadd_rdata_domain($3); /* domain name */

	    free($1.str);
    }
    ;

/* RFC 1183 */
rdata_x25:	STR trail
    {
	    zadd_rdata_wireformat(zparser_conv_text($1.str, $1.len));
	    /* X.25 address. */

	    free($1.str);
    }
    ;

/* RFC 1183 */
rdata_isdn:	STR trail
    {
	    zadd_rdata_wireformat(zparser_conv_text($1.str, $1.len));
	    /* address */

	    free($1.str);
    }
    |	STR sp STR trail
    {
	    zadd_rdata_wireformat(zparser_conv_text($1.str, $1.len));
	    /* address */
	    zadd_rdata_wireformat(zparser_conv_text($3.str, $3.len));
	    /* sub-address */

	    free($1.str);
	    free($3.str);
    }
    ;

/* RFC 1183 */
rdata_rt:	STR sp dname trail
    {
						if (!knot_dname_is_fqdn($3)) {
			knot_dname_cat($3, parser->root_domain);
		}

	    zadd_rdata_wireformat(zparser_conv_short($1.str)); /* preference */
	    zadd_rdata_domain($3); /* intermediate host */

	    free($1.str);
    }
    ;

/* RFC 1706 */
rdata_nsap:	str_dot_seq trail
    {
	    /* String must start with "0x" or "0X".	 */
	    if (strncasecmp($1.str, "0x", 2) != 0) {
		    zc_error_prev_line("NSAP rdata must start with '0x'");
	    } else {
		    zadd_rdata_wireformat(zparser_conv_hex($1.str + 2,
							   $1.len - 2));
		    /* NSAP */
	    }

	    free($1.str);
    }
    ;

/* RFC 2163 */
rdata_px:	STR sp dname sp dname trail
    {
			if (!knot_dname_is_fqdn($3)) {
				knot_dname_cat($3, parser->root_domain);
		}
					if (!knot_dname_is_fqdn($5)) {
				knot_dname_cat($5, parser->root_domain);
		}
	    zadd_rdata_wireformat(zparser_conv_short($1.str)); /* preference */
	    zadd_rdata_domain($3); /* MAP822 */
	    zadd_rdata_domain($5); /* MAPX400 */

	    free($1.str);
    }
    ;

rdata_aaaa:	dotted_str trail
    {
	    zadd_rdata_wireformat(zparser_conv_aaaa($1.str));
	    /* IPv6 address */

	    free($1.str);
    }
    ;

rdata_loc:	concatenated_str_seq trail
    {
	    zadd_rdata_wireformat(zparser_conv_loc($1.str)); /* Location */

	    free($1.str);
    }
    ;

rdata_nxt:	dname sp nxt_seq trail
    {
				if (!knot_dname_is_fqdn($1)) {
				knot_dname_cat($1, parser->root_domain);
		}
	    zadd_rdata_domain($1); /* nxt name */
	    zadd_rdata_wireformat(zparser_conv_nxt(nxtbits)); /* nxt bitlist */
	    memset(nxtbits, 0, sizeof(nxtbits));
    }
    ;

rdata_srv:	STR sp STR sp STR sp dname trail
    {
				if (!knot_dname_is_fqdn($7)) {
				knot_dname_cat($7, parser->root_domain);

		}
	    zadd_rdata_wireformat(zparser_conv_short($1.str)); /* prio */
	    zadd_rdata_wireformat(zparser_conv_short($3.str)); /* weight */
	    zadd_rdata_wireformat(zparser_conv_short($5.str)); /* port */
	    zadd_rdata_domain($7); /* target name */

	    free($1.str);
	    free($3.str);
	    free($5.str);
    }
    ;

/* RFC 2915 */
rdata_naptr:	STR sp STR sp STR sp STR sp STR sp dname trail
    {
				if (!knot_dname_is_fqdn($11)) {
				knot_dname_cat($11, parser->root_domain);

		}
	    zadd_rdata_wireformat(zparser_conv_short($1.str)); /* order */
	    zadd_rdata_wireformat(zparser_conv_short($3.str)); /* preference */
	    zadd_rdata_wireformat(zparser_conv_text($5.str, $5.len));
	    /* flags */
	    zadd_rdata_wireformat(zparser_conv_text($7.str, $7.len));
	    /* service */
	    zadd_rdata_wireformat(zparser_conv_text($9.str, $9.len));
	    /* regexp */
	    zadd_rdata_domain($11); /* target name */

	    free($1.str);
	    free($3.str);
	    free($5.str);
	    free($7.str);
	    free($9.str);
    }
    ;

/* RFC 2230 */
rdata_kx:	STR sp dname trail
    {
				if (!knot_dname_is_fqdn($3)) {
				knot_dname_cat($3, parser->root_domain);
		}
	    zadd_rdata_wireformat(zparser_conv_short($1.str)); /* preference */
	    zadd_rdata_domain($3); /* exchanger */

	    free($1.str);
    }
    ;

/* RFC 2538 */
rdata_cert:	STR sp STR sp STR sp str_sp_seq trail
    {
	    zadd_rdata_wireformat(zparser_conv_certificate_type($1.str));
	    /* type */
	    zadd_rdata_wireformat(zparser_conv_short($3.str)); /* key tag */
	    zadd_rdata_wireformat(zparser_conv_algorithm($5.str));
	    /* algorithm */
	    zadd_rdata_wireformat(zparser_conv_b64($7.str));
	    /* certificate or CRL */

	    free($1.str);
	    free($3.str);
	    free($5.str);
	    free($7.str);
    }
    ;

/* RFC 3123 */
rdata_apl:	rdata_apl_seq trail
    ;

rdata_apl_seq:	dotted_str
    {
	    zadd_rdata_txt_wireformat(zparser_conv_apl_rdata($1.str), 1);

	    free($1.str);
    }
    |	rdata_apl_seq sp dotted_str
    {
	    zadd_rdata_txt_wireformat(zparser_conv_apl_rdata($3.str), 0);

	    free($3.str);
    }
    ;

rdata_ds:	STR sp STR sp STR sp str_sp_seq trail
    {
	    zadd_rdata_wireformat(zparser_conv_short($1.str)); /* keytag */
	    zadd_rdata_wireformat(zparser_conv_algorithm($3.str)); /* alg */
	    zadd_rdata_wireformat(zparser_conv_byte($5.str)); /* type */
	    zadd_rdata_wireformat(zparser_conv_hex($7.str, $7.len)); /* hash */

	    free($1.str);
	    free($3.str);
	    free($5.str);
	    free($7.str);
    }
    ;

rdata_dlv:	STR sp STR sp STR sp str_sp_seq trail
    {
	    zadd_rdata_wireformat(zparser_conv_short($1.str)); /* keytag */
	    zadd_rdata_wireformat(zparser_conv_algorithm($3.str)); /* alg */
	    zadd_rdata_wireformat(zparser_conv_byte($5.str)); /* type */
	    zadd_rdata_wireformat(zparser_conv_hex($7.str, $7.len)); /* hash */

	    free($1.str);
	    free($3.str);
	    free($5.str);
	    free($7.str);
    }
    ;

rdata_sshfp:	STR sp STR sp str_sp_seq trail
    {
	    zadd_rdata_wireformat(zparser_conv_byte($1.str)); /* alg */
	    zadd_rdata_wireformat(zparser_conv_byte($3.str)); /* fp type */
	    zadd_rdata_wireformat(zparser_conv_hex($5.str, $5.len)); /* hash */

	    free($1.str);
	    free($3.str);
	    free($5.str);
    }
    ;

rdata_dhcid:	str_sp_seq trail
    {
	    zadd_rdata_wireformat(zparser_conv_b64($1.str)); /* data blob */

	    free($1.str);
    }
    ;

rdata_rrsig:	STR sp STR sp STR sp STR sp STR sp STR
		sp STR sp wire_dname sp str_sp_seq trail
    {
	    zadd_rdata_wireformat(zparser_conv_rrtype($1.str));
	    /* rr covered */
	    zadd_rdata_wireformat(zparser_conv_algorithm($3.str)); /* alg */
	    zadd_rdata_wireformat(zparser_conv_byte($5.str)); /* # labels */
	    zadd_rdata_wireformat(zparser_conv_period($7.str));
	    /* # orig TTL */
	    zadd_rdata_wireformat(zparser_conv_time($9.str)); /* sig exp */
	    zadd_rdata_wireformat(zparser_conv_time($11.str)); /* sig inc */
	    zadd_rdata_wireformat(zparser_conv_short($13.str)); /* key id */
/*	    zadd_rdata_wireformat(zparser_conv_dns_name((const uint8_t*)
							 $15.str,
							 $15.len));*/
	    knot_dname_t *dname =
		knot_dname_new_from_wire((uint8_t *)$15.str, $15.len, NULL);
	    knot_dname_retain(dname);
	    if (dname == NULL) {
	    	parser->error_occurred = KNOTDZCOMPILE_ENOMEM;
	    } else {
	    	knot_dname_cat(dname, parser->root_domain);
	    }

	    zadd_rdata_domain(dname);
	    /* sig name */
	    zadd_rdata_wireformat(zparser_conv_b64($17.str)); /* sig data */

	    free($1.str);
	    free($3.str);
	    free($5.str);
	    free($7.str);
	    free($9.str);
	    free($11.str);
	    free($13.str);
	    free($15.str);
	    free($17.str);
    }
    ;

rdata_nsec:	wire_dname nsec_seq
    {
/*	    zadd_rdata_wireformat(zparser_conv_dns_name((const uint8_t*)
							$1.str,
							$1.len));*/

	    knot_dname_t *dname =
		knot_dname_new_from_wire((uint8_t *)$1.str, $1.len, NULL);
	    knot_dname_retain(dname);
	    free($1.str);

	    knot_dname_cat(dname, parser->root_domain);

	    zadd_rdata_domain(dname);
	    /* nsec name */
	    zadd_rdata_wireformat(zparser_conv_nsec(nsecbits));
	    /* nsec bitlist */
	    memset(nsecbits, 0, sizeof(nsecbits));
	    nsec_highest_rcode = 0;
    }
    ;

rdata_nsec3:   STR sp STR sp STR sp STR sp STR nsec_seq
    {
#ifdef NSEC3
	    nsec3_add_params($1.str, $3.str, $5.str, $7.str, $7.len);

/*	    knot_dname_t *dname =
		knot_dname_new_from_str($9.str, $9.len, NULL);

	    zadd_rdata_domain(dname); */

	    zadd_rdata_wireformat(zparser_conv_b32($9.str));
	    /* next hashed name */
	    zadd_rdata_wireformat(zparser_conv_nsec(nsecbits));
	    /* nsec bitlist */
	    memset(nsecbits, 0, sizeof(nsecbits));
	    nsec_highest_rcode = 0;
#else
	    zc_error_prev_line("nsec3 not supported");
#endif /* NSEC3 */

	    free($1.str);
	    free($3.str);
	    free($5.str);
	    free($7.str);
	    free($9.str);
    }
    ;

rdata_nsec3_param:   STR sp STR sp STR sp STR trail
    {
#ifdef NSEC3
	    nsec3_add_params($1.str, $3.str, $5.str, $7.str, $7.len);
#else
	    zc_error_prev_line("nsec3 not supported");
#endif /* NSEC3 */

	    free($1.str);
	    free($3.str);
	    free($5.str);
	    free($7.str);
    }
    ;
    
rdata_tlsa:   STR sp STR sp STR sp str_sp_seq trail
    {
	zadd_rdata_wireformat(zparser_conv_byte($1.str));
	zadd_rdata_wireformat(zparser_conv_byte($3.str));
	zadd_rdata_wireformat(zparser_conv_byte($5.str));
	zadd_rdata_wireformat(zparser_conv_hex($7.str, $7.len));
	
	free($1.str);
	free($3.str);
	free($5.str);
	free($7.str);
    }
    ;

rdata_dnskey:	STR sp STR sp STR sp str_sp_seq trail
    {
	    zadd_rdata_wireformat(zparser_conv_short($1.str)); /* flags */
	    zadd_rdata_wireformat(zparser_conv_byte($3.str)); /* proto */
	    zadd_rdata_wireformat(zparser_conv_algorithm($5.str)); /* alg */
	    zadd_rdata_wireformat(zparser_conv_b64($7.str)); /* hash */

	    free($1.str);
	    free($3.str);
	    free($5.str);
	    free($7.str);
    }
    ;

rdata_ipsec_base: STR sp STR sp STR sp dotted_str
    {
	    knot_dname_t* name = 0;
	    zadd_rdata_wireformat(zparser_conv_byte($1.str)); /* precedence */
	    zadd_rdata_wireformat(zparser_conv_byte($3.str));
	    /* gateway type */
	    zadd_rdata_wireformat(zparser_conv_byte($5.str)); /* algorithm */
	    switch(atoi($3.str)) {
		case IPSECKEY_NOGATEWAY:
			zadd_rdata_wireformat(alloc_rdata_init("", 0));
			break;
		case IPSECKEY_IP4:
			zadd_rdata_wireformat(zparser_conv_a($7.str));
			break;
		case IPSECKEY_IP6:
			zadd_rdata_wireformat(zparser_conv_aaaa($7.str));
			break;
		case IPSECKEY_DNAME:
			/* convert and insert the dname */
			if(strlen($7.str) == 0)
				zc_error_prev_line("IPSECKEY must specify"
						   "gateway name");
			name = knot_dname_new_from_str($7.str,
							  strlen($7.str),
							  NULL);
			if(!name) {
				zc_error_prev_line("IPSECKEY bad gateway"
						   "dname %s", $7.str);
				knot_rrset_deep_free(&(parser->current_rrset),
						                          0, 0, 0);
				knot_zone_deep_free(&(parser->current_zone),
						      1);
				YYABORT;
			}

			if(!knot_dname_is_fqdn(name)) {
			    assert(parser->origin);
			    name = knot_dname_cat(name,
				    parser->origin->owner);
			    if (name == NULL) {
			    	zc_error_prev_line("Cannot concatenete dnames, probably run out of memory!\n");
				YYABORT;
			    }
			}

			free($1.str);
			free($3.str);
			free($5.str);
			free($7.str);

			uint16_t* dncpy = malloc(sizeof(uint8_t) * name->size + 2);
			dncpy[0] = name->size;
			if (dncpy == NULL) {
			    ERR_ALLOC_FAILED;
			    knot_rrset_deep_free(&(parser->current_rrset),
			                           0, 0, 0);
			    knot_zone_deep_free(&(parser->current_zone),
			                          1);
			    YYABORT;
			}
			
			memcpy((uint8_t *)(dncpy + 1), name->name, name->size);
			zadd_rdata_wireformat(dncpy);
			knot_dname_free(&name);
			break;
		default:
			zc_error_prev_line("unknown IPSECKEY gateway type");
	    }
    }
    ;

rdata_ipseckey:	rdata_ipsec_base sp str_sp_seq trail
    {
	   zadd_rdata_wireformat(zparser_conv_b64($3.str)); /* public key */

		 free($3.str);
    }
    | rdata_ipsec_base trail
    ;

rdata_unknown:	URR sp STR sp str_sp_seq trail
    {
	    /* $2 is the number of octects, currently ignored */
	    $$ = zparser_conv_hex($5.str, $5.len);
	    free($5.str);
	    free($3.str);
    }
    |	URR sp STR trail
    {
	    $$ = zparser_conv_hex("", 0);
	    free($3.str);
    }
    |	URR error NL
    {
	    $$ = zparser_conv_hex("", 0);
    }
    ;
%%

int zp_wrap(void)
{
	return 1;
}

/*
 * Create the parser.
 */
zparser_type *zparser_create()
{
	zparser_type *result = malloc(sizeof(zparser_type));
	if (result == NULL) {
	    ERR_ALLOC_FAILED;
	    return NULL;
	}

	result->temporary_items = malloc(MAXRDATALEN *
					  sizeof(knot_rdata_item_t));
	if (result->temporary_items == NULL) {
		ERR_ALLOC_FAILED;
		free(result);
		return NULL;
	}

	result->current_rrset = knot_rrset_new(NULL, 0, 0, 0);
	if (result->current_rrset == NULL) {
		free(result->temporary_items);
		free(result);
		return NULL;
	}

	result->root_domain = knot_dname_new_from_str(".", 1, NULL);
	if (result->root_domain == NULL) {
		free(result->temporary_items);
		free(result->current_rrset);
		free(result);
		return NULL;
	}

	return result;
}

/*
 * Initialize the parser for a new zone file.
 */
void
zparser_init(const char *filename, uint32_t ttl, uint16_t rclass,
	     knot_node_t *origin, knot_dname_t *origin_from_config)
{
	memset(nxtbits, 0, sizeof(nxtbits));
	memset(nsecbits, 0, sizeof(nsecbits));
	nsec_highest_rcode = 0;

	parser->current_zone = NULL;
	parser->prev_dname = NULL;

	parser->default_ttl = ttl;
	parser->default_class = rclass;

	parser->origin = origin;
	parser->prev_dname = NULL;//parser->origin->owner;

	parser->default_apex = origin;
	parser->error_occurred = 0;
	parser->errors = 0;
	parser->line = 1;
	parser->filename = filename;
	parser->rdata_count = 0;
	parser->origin_from_config = origin_from_config;
	knot_dname_retain(origin_from_config);

	parser->last_node = origin;
//	parser->root_domain = NULL;

	/* Create zone */
	parser->current_zone = knot_zone_new(origin, 0, 1);

	parser->node_rrsigs = NULL;

	parser->current_rrset->rclass = parser->default_class;
	parser->current_rrset->rdata = NULL;
}


void zparser_free()
{
	knot_dname_release(parser->root_domain);
//	knot_dname_release(parser->prev_dname);
	knot_zone_deep_free(&parser->current_zone, 1);
	knot_dname_release(parser->origin_from_config);
	free(parser->temporary_items);
	if (parser->current_rrset != NULL) {
		free(parser->current_rrset);
	}
	free(parser);
}

void
yyerror(void *scanner, const char *message)
{
	zc_error("%s", message);
}

static void
error_va_list(unsigned line, const char *fmt, va_list args)
{
	char sbuf[4096] = {0};
	size_t buflen = sizeof(sbuf) - 1;
	char *buf = sbuf;
	int wb = 0;
	if (parser->filename) {
		wb = snprintf(buf, buflen, "%s:%u: ",
		              parser->filename, line);
		if (wb > 0) {
			buf += wb;
			buflen -= wb;
		}
	}
	
	wb = vsnprintf(buf, buflen, fmt, args);
	if (wb > 0) {
		buf += wb;
		buflen -= wb;
		*buf = '\n';
		*(buf + 1) = '\0';
	}
	
	log_zone_error("%s", sbuf);
	
	++parser->errors;
	parser->error_occurred = 1;
}

/* the line counting sux, to say the least
 * with this grose hack we try do give sane
 * numbers back */
void
zc_error_prev_line(const char *fmt, ...)
{
	va_list args;
	va_start(args, fmt);
	error_va_list(parser->line - 1, fmt, args);
	va_end(args);
}

void
zc_error(const char *fmt, ...)
{
	/* send an error message to stderr */
	va_list args;
	va_start(args, fmt);
	error_va_list(parser->line, fmt, args);
	va_end(args);
}

static void
warning_va_list(unsigned line, const char *fmt, va_list args)
{
	char sbuf[4096] = {0};
	size_t buflen = sizeof(sbuf) - 1;
	char *buf = sbuf;
	int wb = 0;
	if (parser->filename) {
		wb = snprintf(buf, buflen, "%s:%u: ",
		              parser->filename, line);
		if (wb > 0) {
			buf += wb;
			buflen -= wb;
		}
	}

	wb = vsnprintf(buf, buflen, fmt, args);
	if (wb > 0) {
		buf += wb;
		buflen -= wb;
		*buf = '\n';
		*(buf + 1) = '\0';
	}

	log_zone_warning("%s", sbuf);
}

void
zc_warning_prev_line(const char *fmt, ...)
{
	va_list args;
	va_start(args, fmt);
	warning_va_list(parser->line - 1, fmt, args);
	va_end(args);
}

void
zc_warning(const char *fmt, ... )
{
	va_list args;
	va_start(args, fmt);
	warning_va_list(parser->line, fmt, args);
	va_end(args);
}

#ifdef NSEC3
static void
nsec3_add_params(const char* hashalgo_str, const char* flag_str,
	const char* iter_str, const char* salt_str, int salt_len)
{
	zadd_rdata_wireformat(zparser_conv_byte(hashalgo_str));
	zadd_rdata_wireformat(zparser_conv_byte(flag_str));
	zadd_rdata_wireformat(zparser_conv_short(iter_str));

	/* salt */
	if(strcmp(salt_str, "-") != 0)
		zadd_rdata_wireformat(zparser_conv_hex_length(salt_str,
							      salt_len));
	else
		zadd_rdata_wireformat(alloc_rdata_init("", 1));

}
#endif /* NSEC3 */