blob: 73d8fd66ba77a06033a15a6b60c54fbbd495b691 [file] [log] [blame]
Jeff Thompson25b4e612013-10-10 16:03:24 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil -*- */
Jeff Thompson47eecfc2013-07-07 22:56:46 -07002/**
Jeff Thompson7687dc02013-09-13 11:54:07 -07003 * Copyright (C) 2013 Regents of the University of California.
4 * @author: Jeff Thompson <jefft0@remap.ucla.edu>
Jeff Thompson47eecfc2013-07-07 22:56:46 -07005 * See COPYING for copyright and distribution information.
Jeff Thompson9c41dfe2013-06-27 12:10:25 -07006 */
7
Jeff Thompson4c89ad62013-06-28 12:50:13 -07008#include <stdexcept>
Jeff Thompson25b4e612013-10-10 16:03:24 -07009#include <ndn-cpp/interest.hpp>
10#include <ndn-cpp/data.hpp>
11#include <ndn-cpp/forwarding-entry.hpp>
12#include <ndn-cpp/encoding/binary-xml-wire-format.hpp>
Jeff Thompson53412192013-08-06 13:35:50 -070013#include "../c/encoding/binary-xml-interest.h"
Jeff Thompson56ec9e22013-08-02 11:34:07 -070014#include "../c/encoding/binary-xml-data.h"
Jeff Thompson990599b2013-08-27 15:14:25 -070015#include "../c/encoding/binary-xml-forwarding-entry.h"
Jeff Thompson53412192013-08-06 13:35:50 -070016#include "binary-xml-encoder.hpp"
17#include "binary-xml-decoder.hpp"
Jeff Thompson4c89ad62013-06-28 12:50:13 -070018
Jeff Thompson1f3f5172013-07-01 19:02:36 -070019using namespace std;
20
Jeff Thompson4c89ad62013-06-28 12:50:13 -070021namespace ndn {
22
Alexander Afanasyev85480842014-01-06 14:46:54 -080023namespace c {
24
25class Exclude : public ndn::Exclude
26{
27public:
28 void
29 get(struct ndn_Exclude& excludeStruct) const
30 {
31 if (excludeStruct.maxEntries < size())
32 throw runtime_error("excludeStruct.maxEntries must be >= this exclude getEntryCount()");
33
34 int entries = 0;
35 for (Exclude::const_reverse_iterator i = rbegin (); i != rend (); i++)
36 {
37 if (!i->first.empty())
38 {
39 excludeStruct.entries[entries].type = ndn_Exclude_COMPONENT;
40 excludeStruct.entries[entries].component.value.value = const_cast<uint8_t*>(i->first.getValue().buf());
41 excludeStruct.entries[entries].component.value.length = i->first.getValue().size();
42 ++entries;
43 }
44 if (i->second)
45 {
46 excludeStruct.entries[entries].type = ndn_Exclude_ANY;
47 ++entries;
48 }
49 }
50
51 excludeStruct.nEntries = entries;
52 }
53
54 void
55 set(const struct ndn_Exclude& excludeStruct)
56 {
57 clear();
58
59 if (excludeStruct.nEntries == 0)
60 return;
61
62 int i = 0;
63 if (excludeStruct.entries[i].type == ndn_Exclude_ANY) {
64 appendExclude("/", true);
65 i++;
66 }
67
68 while (i < excludeStruct.nEntries) {
69 ndn_ExcludeEntry *entry = &excludeStruct.entries[i];
70
71 if (entry->type != ndn_Exclude_COMPONENT)
72 throw runtime_error("unrecognized ndn_ExcludeType");
73
74 Name::Component excludedComponent (entry->component.value.value, entry->component.value.length);
75 ++i;
76 entry = &excludeStruct.entries[i];
77
78 if (i < excludeStruct.nEntries) {
79 if (entry->type == ndn_Exclude_ANY) {
80 appendExclude(excludedComponent, true);
81 ++i;
82 }
83 else
84 appendExclude(excludedComponent, false);
85 }
86 else
87 appendExclude(excludedComponent, false);
88 }
89 }
90};
91
92class Name : public ndn::Name
93{
94public:
95 void
96 get(struct ndn_Name& nameStruct) const
97 {
98 if (nameStruct.maxComponents < size())
99 throw runtime_error("nameStruct.maxComponents must be >= this name getNComponents()");
100
101 nameStruct.nComponents = size();
102 for (size_t i = 0; i < nameStruct.nComponents; ++i) {
103 nameStruct.components[i].value.value = const_cast<uint8_t*>(ndn::Name::get(i).getValue().buf());
104 nameStruct.components[i].value.length = ndn::Name::get(i).getValue().size();
105 }
106 }
107
108 void
109 set(const struct ndn_Name& nameStruct)
110 {
111 clear();
112 for (size_t i = 0; i < nameStruct.nComponents; ++i)
113 append(nameStruct.components[i].value.value, nameStruct.components[i].value.length);
114 }
115};
116
117class Interest : public ndn::Interest
118{
119public:
120 void
121 get(struct ndn_Interest& interestStruct) const
122 {
123 reinterpret_cast<const c::Name&>(getName()).get(interestStruct.name);
124 interestStruct.minSuffixComponents = getMinSuffixComponents();
125 interestStruct.maxSuffixComponents = getMaxSuffixComponents();
126 // publisherPublicKeyDigest_.get(interestStruct.publisherPublicKeyDigest);
127 reinterpret_cast<const c::Exclude&>(getExclude()).get(interestStruct.exclude);
128 interestStruct.childSelector = getChildSelector();
129 interestStruct.answerOriginKind = getMustBeFresh() ?
130 (ndn_Interest_ANSWER_CONTENT_STORE | ndn_Interest_ANSWER_GENERATED)
131 :
132 (ndn_Interest_ANSWER_CONTENT_STORE | ndn_Interest_ANSWER_GENERATED | ndn_Interest_ANSWER_STALE);
133 interestStruct.scope = getScope();
134 interestStruct.interestLifetimeMilliseconds = getInterestLifetime();
135
136 interestStruct.nonce.length = 4;
137 interestStruct.nonce.value = const_cast<uint8_t*>(reinterpret_cast<const uint8_t *>(getNonce()));
138 }
139
140 void
141 set(const struct ndn_Interest& interestStruct)
142 {
143 reinterpret_cast<c::Name&>(getName()).set(interestStruct.name);
144 setMinSuffixComponents(interestStruct.minSuffixComponents);
145 setMaxSuffixComponents(interestStruct.maxSuffixComponents);
146
147 // publisherPublicKeyDigest_.set(interestStruct.publisherPublicKeyDigest);
148
149 reinterpret_cast<c::Exclude&>(getExclude()).set(interestStruct.exclude);
150 setChildSelector(interestStruct.childSelector);
151 // answerOriginKind_ = interestStruct.answerOriginKind;
152 setScope(interestStruct.scope);
153 setInterestLifetime(interestStruct.interestLifetimeMilliseconds);
154
155 setNonce(*reinterpret_cast<uint32_t*>(interestStruct.nonce.value));
156 }
157};
158
159class Data : public ndn::Data
160{
161public:
162 void
163 get(struct ndn_Data& dataStruct) const
164 {
165 // signature_->get(dataStruct.signature);
166 // name_.get(dataStruct.name);
167 // metaInfo_.get(dataStruct.metaInfo);
168 // content_.get(dataStruct.content);
169 }
170
171 void
172 set(const struct ndn_Data& dataStruct)
173 {
174 // signature_->set(dataStruct.signature);
175 // name_.set(dataStruct.name);
176 // metaInfo_.set(dataStruct.metaInfo);
177 // content_ = Blob(dataStruct.content);
178 }
179};
180
181class ForwardingEntry : public ndn::ForwardingEntry
182{
183public:
184 void
185 get(struct ndn_ForwardingEntry& forwardingEntryStruct) const
186 {
187 reinterpret_cast<const c::Name&>(getPrefix()).get(forwardingEntryStruct.prefix);
188 // publisherPublicKeyDigest_.get(forwardingEntryStruct.publisherPublicKeyDigest);
189 forwardingEntryStruct.faceId = getFaceId();
190 // forwardingEntryStruct.forwardingFlags = getForwardingFlags();
191 forwardingEntryStruct.freshnessSeconds = getFreshnessPeriod() / 1000;
192
193 forwardingEntryStruct.action.length = getAction().size();
194 if (getAction().size() > 0)
195 forwardingEntryStruct.action.value = (uint8_t *)&getAction();
196 else
197 forwardingEntryStruct.action.value = 0;
198 }
199
200 void
201 set(const struct ndn_ForwardingEntry& forwardingEntryStruct)
202 {
203 if (forwardingEntryStruct.action.value && forwardingEntryStruct.action.length > 0)
204 setAction(string(forwardingEntryStruct.action.value, forwardingEntryStruct.action.value + forwardingEntryStruct.action.length));
205 else
206 setAction("");
207
208 Name prefix;
209 reinterpret_cast<c::Name&>(prefix).set(forwardingEntryStruct.prefix);
210 setPrefix(prefix);
211 // publisherPublicKeyDigest_.set(forwardingEntryStruct.publisherPublicKeyDigest);
212 setFaceId(forwardingEntryStruct.faceId);
213 // setForwardingFlags(forwardingEntryStruct.forwardingFlags);
214 setFreshnessPeriod(forwardingEntryStruct.freshnessSeconds * 1000);
215 }
216};
217
218}
219
220
Jeff Thompsonfa181ac2013-08-02 19:00:51 -0700221// This is declared in the WireFormat class.
Jeff Thompson0050abe2013-09-17 12:50:25 -0700222WireFormat*
223WireFormat::newInitialDefaultWireFormat()
Jeff Thompsonfa181ac2013-08-02 19:00:51 -0700224{
225 return new BinaryXmlWireFormat();
226}
227
Jeff Thompson0050abe2013-09-17 12:50:25 -0700228Blob
229BinaryXmlWireFormat::encodeInterest(const Interest& interest)
Jeff Thompson214c7be2013-07-08 15:23:00 -0700230{
Jeff Thompsonf084ec62013-07-09 12:32:52 -0700231 struct ndn_NameComponent nameComponents[100];
232 struct ndn_ExcludeEntry excludeEntries[100];
Jeff Thompson214c7be2013-07-08 15:23:00 -0700233 struct ndn_Interest interestStruct;
Jeff Thompsond1427fb2013-08-29 17:20:32 -0700234 ndn_Interest_initialize
Jeff Thompsonf084ec62013-07-09 12:32:52 -0700235 (&interestStruct, nameComponents, sizeof(nameComponents) / sizeof(nameComponents[0]),
236 excludeEntries, sizeof(excludeEntries) / sizeof(excludeEntries[0]));
Alexander Afanasyev85480842014-01-06 14:46:54 -0800237 reinterpret_cast<const c::Interest&>(interest).get(interestStruct);
Jeff Thompson214c7be2013-07-08 15:23:00 -0700238
Jeff Thompsonf0fea002013-07-30 17:22:42 -0700239 BinaryXmlEncoder encoder;
Jeff Thompsonaa020712013-08-08 21:20:06 -0700240 ndn_Error error;
241 if ((error = ndn_encodeBinaryXmlInterest(&interestStruct, &encoder)))
Jeff Thompson4affbf52013-10-18 14:36:46 -0700242 throw runtime_error(ndn_getErrorString(error));
Jeff Thompson214c7be2013-07-08 15:23:00 -0700243
Jeff Thompsonb0979fd2013-07-30 15:48:21 -0700244 return encoder.getOutput();
Jeff Thompson214c7be2013-07-08 15:23:00 -0700245}
246
Jeff Thompson0050abe2013-09-17 12:50:25 -0700247void
Jeff Thompson97223af2013-09-24 17:01:27 -0700248BinaryXmlWireFormat::decodeInterest(Interest& interest, const uint8_t *input, size_t inputLength)
Jeff Thompson7c30eda2013-07-03 18:37:07 -0700249{
Jeff Thompsonf084ec62013-07-09 12:32:52 -0700250 struct ndn_NameComponent nameComponents[100];
251 struct ndn_ExcludeEntry excludeEntries[100];
Jeff Thompson7c30eda2013-07-03 18:37:07 -0700252 struct ndn_Interest interestStruct;
Jeff Thompsond1427fb2013-08-29 17:20:32 -0700253 ndn_Interest_initialize
Jeff Thompsonf084ec62013-07-09 12:32:52 -0700254 (&interestStruct, nameComponents, sizeof(nameComponents) / sizeof(nameComponents[0]),
255 excludeEntries, sizeof(excludeEntries) / sizeof(excludeEntries[0]));
Jeff Thompson7c30eda2013-07-03 18:37:07 -0700256
Jeff Thompsonf0fea002013-07-30 17:22:42 -0700257 BinaryXmlDecoder decoder(input, inputLength);
Jeff Thompsonb0e4fad2013-07-08 01:16:48 -0700258 ndn_Error error;
Jeff Thompson94ddc272013-08-08 14:17:38 -0700259 if ((error = ndn_decodeBinaryXmlInterest(&interestStruct, &decoder)))
Jeff Thompson4affbf52013-10-18 14:36:46 -0700260 throw runtime_error(ndn_getErrorString(error));
Jeff Thompson7c30eda2013-07-03 18:37:07 -0700261
Alexander Afanasyev85480842014-01-06 14:46:54 -0800262 reinterpret_cast<c::Interest&>(interest).set(interestStruct);
Jeff Thompson7c30eda2013-07-03 18:37:07 -0700263}
264
Jeff Thompson0050abe2013-09-17 12:50:25 -0700265Blob
Jeff Thompson97223af2013-09-24 17:01:27 -0700266BinaryXmlWireFormat::encodeData(const Data& data, size_t *signedPortionBeginOffset, size_t *signedPortionEndOffset)
Jeff Thompson5cae5e52013-07-10 19:41:20 -0700267{
268 struct ndn_NameComponent nameComponents[100];
Jeff Thompson7329a132013-08-16 15:57:37 -0700269 struct ndn_NameComponent keyNameComponents[100];
Jeff Thompson56ec9e22013-08-02 11:34:07 -0700270 struct ndn_Data dataStruct;
Jeff Thompsond1427fb2013-08-29 17:20:32 -0700271 ndn_Data_initialize
Jeff Thompson7329a132013-08-16 15:57:37 -0700272 (&dataStruct, nameComponents, sizeof(nameComponents) / sizeof(nameComponents[0]),
273 keyNameComponents, sizeof(keyNameComponents) / sizeof(keyNameComponents[0]));
Alexander Afanasyev85480842014-01-06 14:46:54 -0800274 reinterpret_cast<const c::Data&>(data).get(dataStruct);
Jeff Thompson5cae5e52013-07-10 19:41:20 -0700275
Jeff Thompson3aa2d542013-12-10 18:14:22 -0800276 BinaryXmlEncoder encoder(1500);
Jeff Thompsonaa020712013-08-08 21:20:06 -0700277 ndn_Error error;
Jeff Thompson9c661702013-09-13 14:35:44 -0700278 if ((error = ndn_encodeBinaryXmlData(&dataStruct, signedPortionBeginOffset, signedPortionEndOffset, &encoder)))
Jeff Thompson4affbf52013-10-18 14:36:46 -0700279 throw runtime_error(ndn_getErrorString(error));
Jeff Thompson5cae5e52013-07-10 19:41:20 -0700280
Jeff Thompsonb0979fd2013-07-30 15:48:21 -0700281 return encoder.getOutput();
Jeff Thompson5cae5e52013-07-10 19:41:20 -0700282}
283
Jeff Thompson0050abe2013-09-17 12:50:25 -0700284void
285BinaryXmlWireFormat::decodeData
Jeff Thompson97223af2013-09-24 17:01:27 -0700286 (Data& data, const uint8_t *input, size_t inputLength, size_t *signedPortionBeginOffset, size_t *signedPortionEndOffset)
Jeff Thompson5cae5e52013-07-10 19:41:20 -0700287{
288 struct ndn_NameComponent nameComponents[100];
Jeff Thompson7329a132013-08-16 15:57:37 -0700289 struct ndn_NameComponent keyNameComponents[100];
Jeff Thompson56ec9e22013-08-02 11:34:07 -0700290 struct ndn_Data dataStruct;
Jeff Thompsond1427fb2013-08-29 17:20:32 -0700291 ndn_Data_initialize
Jeff Thompson7329a132013-08-16 15:57:37 -0700292 (&dataStruct, nameComponents, sizeof(nameComponents) / sizeof(nameComponents[0]),
293 keyNameComponents, sizeof(keyNameComponents) / sizeof(keyNameComponents[0]));
Jeff Thompson5cae5e52013-07-10 19:41:20 -0700294
Jeff Thompsonf0fea002013-07-30 17:22:42 -0700295 BinaryXmlDecoder decoder(input, inputLength);
Jeff Thompson5cae5e52013-07-10 19:41:20 -0700296 ndn_Error error;
Jeff Thompson9c661702013-09-13 14:35:44 -0700297 if ((error = ndn_decodeBinaryXmlData(&dataStruct, signedPortionBeginOffset, signedPortionEndOffset, &decoder)))
Jeff Thompson4affbf52013-10-18 14:36:46 -0700298 throw runtime_error(ndn_getErrorString(error));
Jeff Thompson5cae5e52013-07-10 19:41:20 -0700299
Alexander Afanasyev85480842014-01-06 14:46:54 -0800300 reinterpret_cast<c::Data&>(data).set(dataStruct);
Jeff Thompson5cae5e52013-07-10 19:41:20 -0700301}
302
Jeff Thompson0050abe2013-09-17 12:50:25 -0700303Blob
304BinaryXmlWireFormat::encodeForwardingEntry(const ForwardingEntry& forwardingEntry)
Jeff Thompson990599b2013-08-27 15:14:25 -0700305{
306 struct ndn_NameComponent prefixNameComponents[100];
307 struct ndn_ForwardingEntry forwardingEntryStruct;
Jeff Thompsond1427fb2013-08-29 17:20:32 -0700308 ndn_ForwardingEntry_initialize
Jeff Thompson990599b2013-08-27 15:14:25 -0700309 (&forwardingEntryStruct, prefixNameComponents, sizeof(prefixNameComponents) / sizeof(prefixNameComponents[0]));
Alexander Afanasyev85480842014-01-06 14:46:54 -0800310 reinterpret_cast<const c::ForwardingEntry&>(forwardingEntry).get(forwardingEntryStruct);
Jeff Thompson990599b2013-08-27 15:14:25 -0700311
312 BinaryXmlEncoder encoder;
313 ndn_Error error;
314 if ((error = ndn_encodeBinaryXmlForwardingEntry(&forwardingEntryStruct, &encoder)))
Jeff Thompson4affbf52013-10-18 14:36:46 -0700315 throw runtime_error(ndn_getErrorString(error));
Jeff Thompson990599b2013-08-27 15:14:25 -0700316
317 return encoder.getOutput();
318}
319
Jeff Thompson0050abe2013-09-17 12:50:25 -0700320void
Jeff Thompson97223af2013-09-24 17:01:27 -0700321BinaryXmlWireFormat::decodeForwardingEntry(ForwardingEntry& forwardingEntry, const uint8_t *input, size_t inputLength)
Jeff Thompson990599b2013-08-27 15:14:25 -0700322{
323 struct ndn_NameComponent prefixNameComponents[100];
324 struct ndn_ForwardingEntry forwardingEntryStruct;
Jeff Thompsond1427fb2013-08-29 17:20:32 -0700325 ndn_ForwardingEntry_initialize
Jeff Thompson990599b2013-08-27 15:14:25 -0700326 (&forwardingEntryStruct, prefixNameComponents, sizeof(prefixNameComponents) / sizeof(prefixNameComponents[0]));
327
328 BinaryXmlDecoder decoder(input, inputLength);
329 ndn_Error error;
330 if ((error = ndn_decodeBinaryXmlForwardingEntry(&forwardingEntryStruct, &decoder)))
Jeff Thompson4affbf52013-10-18 14:36:46 -0700331 throw runtime_error(ndn_getErrorString(error));
Jeff Thompson990599b2013-08-27 15:14:25 -0700332
Alexander Afanasyev85480842014-01-06 14:46:54 -0800333 reinterpret_cast<c::ForwardingEntry&>(forwardingEntry).set(forwardingEntryStruct);
Jeff Thompson990599b2013-08-27 15:14:25 -0700334}
335
Jeff Thompson4c89ad62013-06-28 12:50:13 -0700336}