2525#endif
2626
2727#include " dnsbackend.hh"
28+ #include " json.hh"
2829
29- void CatalogInfo::fromJson (const std::string& json, CatalogType type)
30+ bool CatalogInfo::parseJson (const std::string& json, CatalogType type)
3031{
31- d_type = type;
32- if (d_type == CatalogType::None) {
32+ if (type == CatalogType::None) {
3333 throw std::runtime_error (" CatalogType is set to None" );
3434 }
35+
36+ d_type = type;
37+
3538 if (json.empty ()) {
36- return ;
39+ d_doc = nullptr ;
40+ return false ;
3741 }
42+
3843 std::string err;
3944 d_doc = json11::Json::parse (json, err);
40- if (!d_doc.is_null ()) {
41- if (!d_doc[getTypeString (d_type)].is_null ()) {
42- auto items = d_doc[getTypeString (type)].object_items ();
43- if (!items[" coo" ].is_null ()) {
44- if (items[" coo" ].is_string ()) {
45- if (!items[" coo" ].string_value ().empty ()) {
46- d_coo = DNSName (items[" coo" ].string_value ());
47- }
48- }
49- else {
50- throw std::out_of_range (" Key 'coo' is not a string" );
51- }
45+ if (d_doc.is_null ()) {
46+ throw std::runtime_error (" Parsing of JSON options failed: " + err);
47+ }
48+
49+ return !d_doc[getTypeString (d_type)].is_null ();
50+ }
51+
52+ void CatalogInfo::fromJson (const std::string& json, CatalogType type)
53+ {
54+ if (parseJson (json, type)) {
55+ auto items = d_doc[getTypeString (type)].object_items ();
56+
57+ // coo property
58+ if (!items[" coo" ].is_null ()) {
59+ d_coo = DNSName (stringFromJson (items, " coo" ));
60+ }
61+
62+ // unique property
63+ if (!items[" unique" ].is_null ()) {
64+ d_unique = DNSName (stringFromJson (items, " unique" ));
65+ if (d_unique.countLabels () != 1 ) {
66+ throw std::out_of_range (" Invalid number of labels in unique value" );
5267 }
53- if (!items[" unique" ].is_null ()) {
54- if (items[" unique" ].is_string ()) {
55- if (!items[" unique" ].string_value ().empty ()) {
56- d_unique = DNSName (items[" unique" ].string_value ());
57- }
58- }
59- else {
60- throw std::out_of_range (" Key 'unique' is not a string" );
61- }
68+ }
69+
70+ // group properties
71+ if (!items[" group" ].is_null ()) {
72+ if (!items[" group" ].is_array ()) {
73+ throw std::out_of_range (" Key 'group' is not an array" );
6274 }
63- if (!items[" group" ].is_null ()) {
64- if (items[" group" ].is_array ()) {
65- for (const auto & value : items[" group" ].array_items ()) {
66- d_group.insert (value.string_value ());
67- }
68- }
69- else {
70- throw std::out_of_range (" Key 'group' is not an array" );
71- }
75+ for (const auto & value : items[" group" ].array_items ()) {
76+ d_group.insert (value.string_value ());
7277 }
7378 }
7479 }
75- else {
76- throw std::runtime_error (" Parsing of JSON options failed: " + err);
77- }
7880}
7981
8082std::string CatalogInfo::toJson () const
@@ -83,41 +85,48 @@ std::string CatalogInfo::toJson() const
8385 throw std::runtime_error (" CatalogType is set to None" );
8486 }
8587 json11::Json::object object;
88+
89+ // coo property
8690 if (!d_coo.empty ()) {
8791 object[" coo" ] = d_coo.toString ();
8892 }
93+
94+ // unique property
8995 if (!d_unique.empty ()) {
90- if (d_unique.countLabels () > 1 ) {
91- throw std::out_of_range (" Multiple labels in a unique value are not allowed " );
96+ if (d_unique.countLabels () != 1 ) {
97+ throw std::out_of_range (" Invalid number of labels in unique value" );
9298 }
9399 object[" unique" ] = d_unique.toString ();
94100 }
101+
102+ // group properties
95103 if (!d_group.empty ()) {
96104 json11::Json::array entries;
97105 for (const auto & group : d_group) {
98106 entries.push_back (group);
99107 }
100108 object[" group" ] = entries;
101109 }
110+
102111 auto tmp = d_doc.object_items ();
103112 tmp[getTypeString (d_type)] = object;
104113 const json11::Json ret = tmp;
105114 return ret.dump ();
106115}
107116
108- void CatalogInfo::updateHash (CatalogHashMap& hashes, const DomainInfo& di) const
117+ void CatalogInfo::updateCatalogHash (CatalogHashMap& hashes, const DomainInfo& di)
109118{
110- hashes[di.catalog ].process (std::to_string (di.id ) + di.zone .toLogString () + string (" \0 " , 1 ) + d_coo.toLogString () + string (" \0 " , 1 ) + d_unique.toLogString ());
111- for (const auto & group : d_group) {
112- hashes[di.catalog ].process (std::to_string (group.length ()) + group);
119+ CatalogInfo ci;
120+ hashes[di.catalog ].process (std::to_string (di.id ) + di.zone .toLogString ());
121+ if (ci.parseJson (di.options , CatalogType::Producer)) {
122+ hashes[di.catalog ].process (ci.d_doc [" producer" ].dump ());
113123 }
114124}
115125
116126DNSZoneRecord CatalogInfo::getCatalogVersionRecord (const ZoneName& zone)
117127{
118128 DNSZoneRecord dzr;
119129 dzr.dr .d_name = g_versiondnsname + zone.operator const DNSName&();
120- dzr.dr .d_ttl = 0 ;
121130 dzr.dr .d_type = QType::TXT ;
122131 dzr.dr .setContent (std::make_shared<TXTRecordContent>(" 2" ));
123132 return dzr;
@@ -134,24 +143,24 @@ void CatalogInfo::toDNSZoneRecords(const ZoneName& zone, vector<DNSZoneRecord>&
134143 }
135144 prefix += g_zonesdnsname + zone.operator const DNSName&();
136145
146+ // member zone
137147 DNSZoneRecord dzr;
138148 dzr.dr .d_name = prefix;
139- dzr.dr .d_ttl = 0 ;
140149 dzr.dr .d_type = QType::PTR ;
141150 dzr.dr .setContent (std::make_shared<PTRRecordContent>(d_zone.operator const DNSName&().toString ()));
142151 dzrs.emplace_back (dzr);
143152
153+ // coo property
144154 if (!d_coo.empty ()) {
145155 dzr.dr .d_name = g_coodnsname + prefix;
146- dzr.dr .d_ttl = 0 ;
147156 dzr.dr .d_type = QType::PTR ;
148157 dzr.dr .setContent (std::make_shared<PTRRecordContent>(d_coo));
149158 dzrs.emplace_back (dzr);
150159 }
151160
161+ // group properties
152162 for (const auto & group : d_group) {
153163 dzr.dr .d_name = g_groupdnsname + prefix;
154- dzr.dr .d_ttl = 0 ;
155164 dzr.dr .d_type = QType::TXT ;
156165 dzr.dr .setContent (std::make_shared<TXTRecordContent>(" \" " + group + " \" " ));
157166 dzrs.emplace_back (dzr);
0 commit comments