blob: 2ba6e6686168b89795a5c04e404af78224ac30a8 [file] [log] [blame]
Jeff Thompson47eecfc2013-07-07 22:56:46 -07001/**
2 * @author: Jeff Thompson
Jeff Thompson06f824a2013-07-08 17:14:30 -07003 * Derived from Interest.js by Meki Cheraoui.
Jeff Thompson47eecfc2013-07-07 22:56:46 -07004 * See COPYING for copyright and distribution information.
Jeff Thompsonb7f95562013-07-03 18:36:42 -07005 */
6
7#include "BinaryXMLEncoder.h"
8#include "BinaryXMLDecoder.h"
Jeff Thompson22552902013-07-07 21:26:20 -07009#include "BinaryXMLName.h"
Jeff Thompson8238d002013-07-10 11:56:49 -070010#include "BinaryXMLPublisherPublicKeyDigest.h"
Jeff Thompsonb7f95562013-07-03 18:36:42 -070011#include "BinaryXMLInterest.h"
12
Jeff Thompsonf084ec62013-07-09 12:32:52 -070013static ndn_Error encodeExclude(struct ndn_Exclude *exclude, struct ndn_BinaryXMLEncoder *encoder)
14{
15 if (exclude->nEntries == 0)
Jeff Thompson5e4a41e2013-07-09 13:52:09 -070016 return 0;
Jeff Thompsonf084ec62013-07-09 12:32:52 -070017
18 ndn_Error error;
19 if (error = ndn_BinaryXMLEncoder_writeElementStartDTag(encoder, ndn_BinaryXML_DTag_Exclude))
20 return error;
21
22 // TODO: Do we want to order the components (except for ANY)?
23 unsigned int i;
24 for (i = 0; i < exclude->nEntries; ++i) {
25 struct ndn_ExcludeEntry *entry = &exclude->entries[i];
26
27 if (entry->type == ndn_Exclude_COMPONENT) {
28 if (error = ndn_BinaryXMLEncoder_writeBlobDTagElement
29 (encoder, ndn_BinaryXML_DTag_Component, entry->component, entry->componentLength))
30 return error;
31 }
32 else if (entry->type == ndn_Exclude_ANY) {
33 if (error = ndn_BinaryXMLEncoder_writeElementStartDTag(encoder, ndn_BinaryXML_DTag_Any))
34 return error;
35 if (error = ndn_BinaryXMLEncoder_writeElementClose(encoder))
36 return error;
37 }
38 else
39 return NDN_ERROR_unrecognized_ndn_ExcludeType;
40 }
41
42 if (error = ndn_BinaryXMLEncoder_writeElementClose(encoder))
43 return error;
44
45 return 0;
46}
47
48static ndn_Error decodeExclude(struct ndn_Exclude *exclude, struct ndn_BinaryXMLDecoder *decoder)
49{
50 ndn_Error error;
51 if (error = ndn_BinaryXMLDecoder_readElementStartDTag(decoder, ndn_BinaryXML_DTag_Exclude))
52 return error;
53
54 exclude->nEntries = 0;
55 while (1) {
56 int gotExpectedTag;
57
58 if (error = ndn_BinaryXMLDecoder_peekDTag(decoder, ndn_BinaryXML_DTag_Component, &gotExpectedTag))
59 return error;
60 if (gotExpectedTag) {
61 // Component
62 unsigned char *component;
63 unsigned int componentLen;
64 if (error = ndn_BinaryXMLDecoder_readBinaryDTagElement(decoder, ndn_BinaryXML_DTag_Component, 0, &component, &componentLen))
65 return error;
66
67 // Add the component entry.
68 if (exclude->nEntries >= exclude->maxEntries)
69 return NDN_ERROR_read_an_entry_past_the_maximum_number_of_entries_allowed_in_the_exclude;
70 ndn_ExcludeEntry_init(exclude->entries + exclude->nEntries, ndn_Exclude_COMPONENT, component, componentLen);
71 ++exclude->nEntries;
72
73 continue;
74 }
75
76 if (error = ndn_BinaryXMLDecoder_peekDTag(decoder, ndn_BinaryXML_DTag_Any, &gotExpectedTag))
77 return error;
78 if (gotExpectedTag) {
79 // Any
80 if (error = ndn_BinaryXMLDecoder_readElementStartDTag(decoder, ndn_BinaryXML_DTag_Any))
81 return error;
82 if (error = ndn_BinaryXMLDecoder_readElementClose(decoder))
83 return error;
84
85 // Add the any entry.
86 if (exclude->nEntries >= exclude->maxEntries)
87 return NDN_ERROR_read_an_entry_past_the_maximum_number_of_entries_allowed_in_the_exclude;
88 ndn_ExcludeEntry_init(exclude->entries + exclude->nEntries, ndn_Exclude_ANY, 0, 0);
89 ++exclude->nEntries;
90
91 continue;
92 }
93
94 if (error = ndn_BinaryXMLDecoder_peekDTag(decoder, ndn_BinaryXML_DTag_Bloom, &gotExpectedTag))
95 return error;
96 if (gotExpectedTag) {
97 // Skip the Bloom and treat it as Any.
98 unsigned char *value;
99 unsigned int valueLen;
100 if (error = ndn_BinaryXMLDecoder_readBinaryDTagElement(decoder, ndn_BinaryXML_DTag_Bloom, 0, &value, &valueLen))
101 return error;
102
103 // Add the any entry.
104 if (exclude->nEntries >= exclude->maxEntries)
105 return NDN_ERROR_read_an_entry_past_the_maximum_number_of_entries_allowed_in_the_exclude;
106 ndn_ExcludeEntry_init(exclude->entries + exclude->nEntries, ndn_Exclude_ANY, 0, 0);
107 ++exclude->nEntries;
108
109 continue;
110 }
111
112 // Else no more entries.
113 break;
114 }
115
116 if (error = ndn_BinaryXMLDecoder_readElementClose(decoder))
117 return error;
118
119 return 0;
120}
121
Jeff Thompson06f824a2013-07-08 17:14:30 -0700122ndn_Error ndn_encodeBinaryXMLInterest(struct ndn_Interest *interest, struct ndn_BinaryXMLEncoder *encoder)
123{
124 ndn_Error error;
125 if (error = ndn_BinaryXMLEncoder_writeElementStartDTag(encoder, ndn_BinaryXML_DTag_Interest))
126 return error;
127
128 if (error = ndn_encodeBinaryXMLName(&interest->name, encoder))
129 return error;
130
Jeff Thompson45492a72013-07-11 12:02:47 -0700131 if (error = ndn_BinaryXMLEncoder_writeOptionalUnsignedDecimalIntDTagElement
132 (encoder, ndn_BinaryXML_DTag_MinSuffixComponents, interest->minSuffixComponents))
133 return error;
134 if (error = ndn_BinaryXMLEncoder_writeOptionalUnsignedDecimalIntDTagElement
135 (encoder, ndn_BinaryXML_DTag_MaxSuffixComponents, interest->maxSuffixComponents))
136 return error;
Jeff Thompson06f824a2013-07-08 17:14:30 -0700137
Jeff Thompson8238d002013-07-10 11:56:49 -0700138 // This will skip encoding if there is no publisherPublicKeyDigest.
139 if (error = ndn_encodeBinaryXMLPublisherPublicKeyDigest(&interest->publisherPublicKeyDigest, encoder))
140 return error;
Jeff Thompson06f824a2013-07-08 17:14:30 -0700141
Jeff Thompson8238d002013-07-10 11:56:49 -0700142 // This will skip encoding if there is no exclude.
Jeff Thompsonf084ec62013-07-09 12:32:52 -0700143 if (error = encodeExclude(&interest->exclude, encoder))
144 return error;
Jeff Thompson06f824a2013-07-08 17:14:30 -0700145
Jeff Thompson45492a72013-07-11 12:02:47 -0700146 if (error = ndn_BinaryXMLEncoder_writeOptionalUnsignedDecimalIntDTagElement
147 (encoder, ndn_BinaryXML_DTag_ChildSelector, interest->childSelector))
148 return error;
Jeff Thompson06f824a2013-07-08 17:14:30 -0700149 if (interest->answerOriginKind >= 0 && interest->answerOriginKind != ndn_Interest_DEFAULT_ANSWER_ORIGIN_KIND) {
150 if (error = ndn_BinaryXMLEncoder_writeUnsignedDecimalIntDTagElement
151 (encoder, ndn_BinaryXML_DTag_AnswerOriginKind, (unsigned int)interest->answerOriginKind))
152 return error;
153 }
Jeff Thompson45492a72013-07-11 12:02:47 -0700154 if (error = ndn_BinaryXMLEncoder_writeOptionalUnsignedDecimalIntDTagElement
155 (encoder, ndn_BinaryXML_DTag_Scope, interest->scope))
156 return error;
Jeff Thompson06f824a2013-07-08 17:14:30 -0700157
Jeff Thompson5a5e8b72013-07-11 14:28:03 -0700158 if (interest->interestLifetimeMilliseconds >= 0) {
Jeff Thompson06f824a2013-07-08 17:14:30 -0700159 if (error = ndn_BinaryXMLEncoder_writeElementStartDTag(encoder, ndn_BinaryXML_DTag_InterestLifetime))
160 return error;
161
Jeff Thompson5a5e8b72013-07-11 14:28:03 -0700162 unsigned int ticks = (unsigned int)((interest->interestLifetimeMilliseconds / 1000.0) * 4096.0);
Jeff Thompson06f824a2013-07-08 17:14:30 -0700163 if (error = ndn_BinaryXMLEncoder_writeUnsignedIntBigEndianBlob(encoder, ticks))
164 return error;
165
166 if (error = ndn_BinaryXMLEncoder_writeElementClose(encoder))
167 return error;
168 }
169
Jeff Thompsona8749a52013-07-11 11:48:05 -0700170 if (error = ndn_BinaryXMLEncoder_writeOptionalBlobDTagElement
171 (encoder, ndn_BinaryXML_DTag_Nonce, interest->nonce, interest->nonceLength))
172 return error;
Jeff Thompson06f824a2013-07-08 17:14:30 -0700173
174 if (error = ndn_BinaryXMLEncoder_writeElementClose(encoder))
175 return error;
176
177 return 0;
178}
179
Jeff Thompson8b666002013-07-08 01:16:26 -0700180ndn_Error ndn_decodeBinaryXMLInterest(struct ndn_Interest *interest, struct ndn_BinaryXMLDecoder *decoder)
Jeff Thompsonb7f95562013-07-03 18:36:42 -0700181{
Jeff Thompson8b666002013-07-08 01:16:26 -0700182 ndn_Error error;
Jeff Thompson22552902013-07-07 21:26:20 -0700183 if (error = ndn_BinaryXMLDecoder_readElementStartDTag(decoder, ndn_BinaryXML_DTag_Interest))
184 return error;
185
186 if (error = ndn_decodeBinaryXMLName(&interest->name, decoder))
187 return error;
188
189 if (error = ndn_BinaryXMLDecoder_readOptionalUnsignedIntegerDTagElement
190 (decoder, ndn_BinaryXML_DTag_MinSuffixComponents, &interest->minSuffixComponents))
191 return error;
192 if (error = ndn_BinaryXMLDecoder_readOptionalUnsignedIntegerDTagElement
193 (decoder, ndn_BinaryXML_DTag_MaxSuffixComponents, &interest->maxSuffixComponents))
194 return error;
Jeff Thompsonb7f95562013-07-03 18:36:42 -0700195
Jeff Thompson5633c302013-07-11 10:24:27 -0700196 if (error = ndn_decodeOptionalBinaryXMLPublisherPublicKeyDigest(&interest->publisherPublicKeyDigest, decoder))
Jeff Thompson22552902013-07-07 21:26:20 -0700197 return error;
Jeff Thompson22552902013-07-07 21:26:20 -0700198
Jeff Thompson5633c302013-07-11 10:24:27 -0700199 int gotExpectedTag;
Jeff Thompsonf084ec62013-07-09 12:32:52 -0700200 if (error = ndn_BinaryXMLDecoder_peekDTag(decoder, ndn_BinaryXML_DTag_Exclude, &gotExpectedTag))
201 return error;
202 if (gotExpectedTag) {
203 if (error = decodeExclude(&interest->exclude, decoder))
204 return error;
205 }
206 else
207 interest->exclude.nEntries = 0;
Jeff Thompson22552902013-07-07 21:26:20 -0700208
209 if (error = ndn_BinaryXMLDecoder_readOptionalUnsignedIntegerDTagElement
210 (decoder, ndn_BinaryXML_DTag_ChildSelector, &interest->childSelector))
211 return error;
212 if (error = ndn_BinaryXMLDecoder_readOptionalUnsignedIntegerDTagElement
213 (decoder, ndn_BinaryXML_DTag_AnswerOriginKind, &interest->answerOriginKind))
214 return error;
215 if (error = ndn_BinaryXMLDecoder_readOptionalUnsignedIntegerDTagElement
216 (decoder, ndn_BinaryXML_DTag_Scope, &interest->scope))
217 return error;
218
219 if (error = ndn_BinaryXMLDecoder_peekDTag(decoder, ndn_BinaryXML_DTag_InterestLifetime, &gotExpectedTag))
220 return error;
221 if (gotExpectedTag) {
222 unsigned char *interestLifetime;
223 unsigned int interestLifetimeLength;
224 if (error = ndn_BinaryXMLDecoder_readBinaryDTagElement
225 (decoder, ndn_BinaryXML_DTag_InterestLifetime, 0, &interestLifetime, &interestLifetimeLength))
226 return error;
227
Jeff Thompson5a5e8b72013-07-11 14:28:03 -0700228 interest->interestLifetimeMilliseconds = 1000.0 *
Jeff Thompson5553d992013-07-11 14:35:45 -0700229 ndn_BinaryXMLDecoder_unsignedBigEndianToDouble(interestLifetime, interestLifetimeLength) / 4096.0;
Jeff Thompson22552902013-07-07 21:26:20 -0700230 }
231 else
Jeff Thompson5a5e8b72013-07-11 14:28:03 -0700232 interest->interestLifetimeMilliseconds = -1.0;
Jeff Thompson22552902013-07-07 21:26:20 -0700233
Jeff Thompson033d7fd2013-07-11 10:44:03 -0700234 if (error = ndn_BinaryXMLDecoder_readOptionalBinaryDTagElement
235 (decoder, ndn_BinaryXML_DTag_Nonce, 0, &interest->nonce, &interest->nonceLength))
Jeff Thompson22552902013-07-07 21:26:20 -0700236 return error;
Jeff Thompson22552902013-07-07 21:26:20 -0700237
238 if (error = ndn_BinaryXMLDecoder_readElementClose(decoder))
239 return error;
240
241 return 0;
Jeff Thompson06f824a2013-07-08 17:14:30 -0700242}