blob: 00da3e49f610acd341f8ba9dac817d0dde2a9dfd [file] [log] [blame]
Jeff Thompson47eecfc2013-07-07 22:56:46 -07001/**
Jeff Thompson7687dc02013-09-13 11:54:07 -07002 * Copyright (C) 2013 Regents of the University of California.
3 * @author: Jeff Thompson <jefft0@remap.ucla.edu>
Jeff Thompson06f824a2013-07-08 17:14:30 -07004 * Derived from Interest.js by Meki Cheraoui.
Jeff Thompson47eecfc2013-07-07 22:56:46 -07005 * See COPYING for copyright and distribution information.
Jeff Thompsonb7f95562013-07-03 18:36:42 -07006 */
7
Jeff Thompson53412192013-08-06 13:35:50 -07008#include "binary-xml-encoder.h"
9#include "binary-xml-decoder.h"
10#include "binary-xml-name.h"
11#include "binary-xml-publisher-public-key-digest.h"
12#include "binary-xml-interest.h"
Jeff Thompsonb7f95562013-07-03 18:36:42 -070013
Jeff Thompsonf0fea002013-07-30 17:22:42 -070014static ndn_Error encodeExclude(struct ndn_Exclude *exclude, struct ndn_BinaryXmlEncoder *encoder)
Jeff Thompsonf084ec62013-07-09 12:32:52 -070015{
16 if (exclude->nEntries == 0)
Jeff Thompsonadaf9232013-08-08 14:30:29 -070017 return NDN_ERROR_success;
Jeff Thompsonf084ec62013-07-09 12:32:52 -070018
19 ndn_Error error;
Jeff Thompson94ddc272013-08-08 14:17:38 -070020 if ((error = ndn_BinaryXmlEncoder_writeElementStartDTag(encoder, ndn_BinaryXml_DTag_Exclude)))
Jeff Thompsonf084ec62013-07-09 12:32:52 -070021 return error;
22
23 // TODO: Do we want to order the components (except for ANY)?
Jeff Thompson97223af2013-09-24 17:01:27 -070024 size_t i;
Jeff Thompsonf084ec62013-07-09 12:32:52 -070025 for (i = 0; i < exclude->nEntries; ++i) {
26 struct ndn_ExcludeEntry *entry = &exclude->entries[i];
27
28 if (entry->type == ndn_Exclude_COMPONENT) {
Jeff Thompson93034532013-10-08 11:52:43 -070029 if ((error = ndn_BinaryXmlEncoder_writeBlobDTagElement(encoder, ndn_BinaryXml_DTag_Component, &entry->component.value)))
Jeff Thompsonf084ec62013-07-09 12:32:52 -070030 return error;
31 }
32 else if (entry->type == ndn_Exclude_ANY) {
Jeff Thompson94ddc272013-08-08 14:17:38 -070033 if ((error = ndn_BinaryXmlEncoder_writeElementStartDTag(encoder, ndn_BinaryXml_DTag_Any)))
Jeff Thompsonf084ec62013-07-09 12:32:52 -070034 return error;
Jeff Thompson2d27e2f2013-08-09 12:55:00 -070035 if ((error = ndn_BinaryXmlEncoder_writeElementClose(encoder)))
Jeff Thompsonf084ec62013-07-09 12:32:52 -070036 return error;
37 }
38 else
39 return NDN_ERROR_unrecognized_ndn_ExcludeType;
Jeff Thompson2d27e2f2013-08-09 12:55:00 -070040 }
Jeff Thompsonf084ec62013-07-09 12:32:52 -070041
Jeff Thompson2d27e2f2013-08-09 12:55:00 -070042 if ((error = ndn_BinaryXmlEncoder_writeElementClose(encoder)))
Jeff Thompsonf084ec62013-07-09 12:32:52 -070043 return error;
44
Jeff Thompsonadaf9232013-08-08 14:30:29 -070045 return NDN_ERROR_success;
Jeff Thompsonf084ec62013-07-09 12:32:52 -070046}
47
Jeff Thompsonf0fea002013-07-30 17:22:42 -070048static ndn_Error decodeExclude(struct ndn_Exclude *exclude, struct ndn_BinaryXmlDecoder *decoder)
Jeff Thompsonf084ec62013-07-09 12:32:52 -070049{
50 ndn_Error error;
Jeff Thompson94ddc272013-08-08 14:17:38 -070051 if ((error = ndn_BinaryXmlDecoder_readElementStartDTag(decoder, ndn_BinaryXml_DTag_Exclude)))
Jeff Thompsonf084ec62013-07-09 12:32:52 -070052 return error;
53
54 exclude->nEntries = 0;
55 while (1) {
56 int gotExpectedTag;
57
Jeff Thompson94ddc272013-08-08 14:17:38 -070058 if ((error = ndn_BinaryXmlDecoder_peekDTag(decoder, ndn_BinaryXml_DTag_Component, &gotExpectedTag)))
Jeff Thompsonf084ec62013-07-09 12:32:52 -070059 return error;
60 if (gotExpectedTag) {
61 // Component
Jeff Thompson93034532013-10-08 11:52:43 -070062 struct ndn_Blob component;
63 if ((error = ndn_BinaryXmlDecoder_readBinaryDTagElement(decoder, ndn_BinaryXml_DTag_Component, 0, &component)))
Jeff Thompsonf084ec62013-07-09 12:32:52 -070064 return error;
65
66 // Add the component entry.
67 if (exclude->nEntries >= exclude->maxEntries)
68 return NDN_ERROR_read_an_entry_past_the_maximum_number_of_entries_allowed_in_the_exclude;
Jeff Thompson93034532013-10-08 11:52:43 -070069 ndn_ExcludeEntry_initialize(exclude->entries + exclude->nEntries, ndn_Exclude_COMPONENT, component.value, component.length);
Jeff Thompsonf084ec62013-07-09 12:32:52 -070070 ++exclude->nEntries;
71
72 continue;
73 }
74
Jeff Thompson94ddc272013-08-08 14:17:38 -070075 if ((error = ndn_BinaryXmlDecoder_peekDTag(decoder, ndn_BinaryXml_DTag_Any, &gotExpectedTag)))
Jeff Thompsonf084ec62013-07-09 12:32:52 -070076 return error;
77 if (gotExpectedTag) {
78 // Any
Jeff Thompson94ddc272013-08-08 14:17:38 -070079 if ((error = ndn_BinaryXmlDecoder_readElementStartDTag(decoder, ndn_BinaryXml_DTag_Any)))
Jeff Thompsonf084ec62013-07-09 12:32:52 -070080 return error;
Jeff Thompson94ddc272013-08-08 14:17:38 -070081 if ((error = ndn_BinaryXmlDecoder_readElementClose(decoder)))
Jeff Thompsonf084ec62013-07-09 12:32:52 -070082 return error;
83
84 // Add the any entry.
85 if (exclude->nEntries >= exclude->maxEntries)
86 return NDN_ERROR_read_an_entry_past_the_maximum_number_of_entries_allowed_in_the_exclude;
Jeff Thompsond1427fb2013-08-29 17:20:32 -070087 ndn_ExcludeEntry_initialize(exclude->entries + exclude->nEntries, ndn_Exclude_ANY, 0, 0);
Jeff Thompsonf084ec62013-07-09 12:32:52 -070088 ++exclude->nEntries;
89
90 continue;
91 }
92
Jeff Thompson94ddc272013-08-08 14:17:38 -070093 if ((error = ndn_BinaryXmlDecoder_peekDTag(decoder, ndn_BinaryXml_DTag_Bloom, &gotExpectedTag)))
Jeff Thompsonf084ec62013-07-09 12:32:52 -070094 return error;
95 if (gotExpectedTag) {
96 // Skip the Bloom and treat it as Any.
Jeff Thompson93034532013-10-08 11:52:43 -070097 struct ndn_Blob value;
98 if ((error = ndn_BinaryXmlDecoder_readBinaryDTagElement(decoder, ndn_BinaryXml_DTag_Bloom, 0, &value)))
Jeff Thompsonf084ec62013-07-09 12:32:52 -070099 return error;
100
101 // Add the any entry.
102 if (exclude->nEntries >= exclude->maxEntries)
103 return NDN_ERROR_read_an_entry_past_the_maximum_number_of_entries_allowed_in_the_exclude;
Jeff Thompsond1427fb2013-08-29 17:20:32 -0700104 ndn_ExcludeEntry_initialize(exclude->entries + exclude->nEntries, ndn_Exclude_ANY, 0, 0);
Jeff Thompsonf084ec62013-07-09 12:32:52 -0700105 ++exclude->nEntries;
106
107 continue;
108 }
109
110 // Else no more entries.
111 break;
112 }
113
Jeff Thompson94ddc272013-08-08 14:17:38 -0700114 if ((error = ndn_BinaryXmlDecoder_readElementClose(decoder)))
Jeff Thompsonf084ec62013-07-09 12:32:52 -0700115 return error;
116
Jeff Thompsonadaf9232013-08-08 14:30:29 -0700117 return NDN_ERROR_success;
Jeff Thompsonf084ec62013-07-09 12:32:52 -0700118}
119
Jeff Thompsonf0fea002013-07-30 17:22:42 -0700120ndn_Error ndn_encodeBinaryXmlInterest(struct ndn_Interest *interest, struct ndn_BinaryXmlEncoder *encoder)
Jeff Thompson06f824a2013-07-08 17:14:30 -0700121{
122 ndn_Error error;
Jeff Thompson94ddc272013-08-08 14:17:38 -0700123 if ((error = ndn_BinaryXmlEncoder_writeElementStartDTag(encoder, ndn_BinaryXml_DTag_Interest)))
Jeff Thompson06f824a2013-07-08 17:14:30 -0700124 return error;
125
Jeff Thompson94ddc272013-08-08 14:17:38 -0700126 if ((error = ndn_encodeBinaryXmlName(&interest->name, encoder)))
Jeff Thompson06f824a2013-07-08 17:14:30 -0700127 return error;
128
Jeff Thompson94ddc272013-08-08 14:17:38 -0700129 if ((error = ndn_BinaryXmlEncoder_writeOptionalUnsignedDecimalIntDTagElement
130 (encoder, ndn_BinaryXml_DTag_MinSuffixComponents, interest->minSuffixComponents)))
Jeff Thompson45492a72013-07-11 12:02:47 -0700131 return error;
Jeff Thompson94ddc272013-08-08 14:17:38 -0700132 if ((error = ndn_BinaryXmlEncoder_writeOptionalUnsignedDecimalIntDTagElement
133 (encoder, ndn_BinaryXml_DTag_MaxSuffixComponents, interest->maxSuffixComponents)))
Jeff Thompson45492a72013-07-11 12:02:47 -0700134 return error;
Jeff Thompson06f824a2013-07-08 17:14:30 -0700135
Jeff Thompson8238d002013-07-10 11:56:49 -0700136 // This will skip encoding if there is no publisherPublicKeyDigest.
Jeff Thompson94ddc272013-08-08 14:17:38 -0700137 if ((error = ndn_encodeBinaryXmlPublisherPublicKeyDigest(&interest->publisherPublicKeyDigest, encoder)))
Jeff Thompson8238d002013-07-10 11:56:49 -0700138 return error;
Jeff Thompson06f824a2013-07-08 17:14:30 -0700139
Jeff Thompson8238d002013-07-10 11:56:49 -0700140 // This will skip encoding if there is no exclude.
Jeff Thompson94ddc272013-08-08 14:17:38 -0700141 if ((error = encodeExclude(&interest->exclude, encoder)))
Jeff Thompsonf084ec62013-07-09 12:32:52 -0700142 return error;
Jeff Thompson06f824a2013-07-08 17:14:30 -0700143
Jeff Thompson94ddc272013-08-08 14:17:38 -0700144 if ((error = ndn_BinaryXmlEncoder_writeOptionalUnsignedDecimalIntDTagElement
145 (encoder, ndn_BinaryXml_DTag_ChildSelector, interest->childSelector)))
Jeff Thompson45492a72013-07-11 12:02:47 -0700146 return error;
Jeff Thompson06f824a2013-07-08 17:14:30 -0700147 if (interest->answerOriginKind >= 0 && interest->answerOriginKind != ndn_Interest_DEFAULT_ANSWER_ORIGIN_KIND) {
Jeff Thompson94ddc272013-08-08 14:17:38 -0700148 if ((error = ndn_BinaryXmlEncoder_writeUnsignedDecimalIntDTagElement
149 (encoder, ndn_BinaryXml_DTag_AnswerOriginKind, (unsigned int)interest->answerOriginKind)))
Jeff Thompson06f824a2013-07-08 17:14:30 -0700150 return error;
151 }
Jeff Thompson94ddc272013-08-08 14:17:38 -0700152 if ((error = ndn_BinaryXmlEncoder_writeOptionalUnsignedDecimalIntDTagElement
153 (encoder, ndn_BinaryXml_DTag_Scope, interest->scope)))
Jeff Thompson45492a72013-07-11 12:02:47 -0700154 return error;
Jeff Thompson06f824a2013-07-08 17:14:30 -0700155
Jeff Thompson94ddc272013-08-08 14:17:38 -0700156 if ((error = ndn_BinaryXmlEncoder_writeOptionalTimeMillisecondsDTagElement
157 (encoder, ndn_BinaryXml_DTag_InterestLifetime, interest->interestLifetimeMilliseconds)))
Jeff Thompsonedc22252013-07-11 18:05:44 -0700158 return error;
Jeff Thompson06f824a2013-07-08 17:14:30 -0700159
Jeff Thompson93034532013-10-08 11:52:43 -0700160 if ((error = ndn_BinaryXmlEncoder_writeOptionalBlobDTagElement(encoder, ndn_BinaryXml_DTag_Nonce, &interest->nonce)))
Jeff Thompsona8749a52013-07-11 11:48:05 -0700161 return error;
Jeff Thompson06f824a2013-07-08 17:14:30 -0700162
Jeff Thompson2d27e2f2013-08-09 12:55:00 -0700163 if ((error = ndn_BinaryXmlEncoder_writeElementClose(encoder)))
Jeff Thompson06f824a2013-07-08 17:14:30 -0700164 return error;
165
Jeff Thompsonadaf9232013-08-08 14:30:29 -0700166 return NDN_ERROR_success;
Jeff Thompson06f824a2013-07-08 17:14:30 -0700167}
168
Jeff Thompsonf0fea002013-07-30 17:22:42 -0700169ndn_Error ndn_decodeBinaryXmlInterest(struct ndn_Interest *interest, struct ndn_BinaryXmlDecoder *decoder)
Jeff Thompsonb7f95562013-07-03 18:36:42 -0700170{
Jeff Thompson8b666002013-07-08 01:16:26 -0700171 ndn_Error error;
Jeff Thompson94ddc272013-08-08 14:17:38 -0700172 if ((error = ndn_BinaryXmlDecoder_readElementStartDTag(decoder, ndn_BinaryXml_DTag_Interest)))
Jeff Thompson22552902013-07-07 21:26:20 -0700173 return error;
174
Jeff Thompson94ddc272013-08-08 14:17:38 -0700175 if ((error = ndn_decodeBinaryXmlName(&interest->name, decoder)))
Jeff Thompson22552902013-07-07 21:26:20 -0700176 return error;
177
Jeff Thompson94ddc272013-08-08 14:17:38 -0700178 if ((error = ndn_BinaryXmlDecoder_readOptionalUnsignedIntegerDTagElement
179 (decoder, ndn_BinaryXml_DTag_MinSuffixComponents, &interest->minSuffixComponents)))
Jeff Thompson22552902013-07-07 21:26:20 -0700180 return error;
Jeff Thompson94ddc272013-08-08 14:17:38 -0700181 if ((error = ndn_BinaryXmlDecoder_readOptionalUnsignedIntegerDTagElement
182 (decoder, ndn_BinaryXml_DTag_MaxSuffixComponents, &interest->maxSuffixComponents)))
Jeff Thompson22552902013-07-07 21:26:20 -0700183 return error;
Jeff Thompsonb7f95562013-07-03 18:36:42 -0700184
Jeff Thompson94ddc272013-08-08 14:17:38 -0700185 if ((error = ndn_decodeOptionalBinaryXmlPublisherPublicKeyDigest(&interest->publisherPublicKeyDigest, decoder)))
Jeff Thompson22552902013-07-07 21:26:20 -0700186 return error;
Jeff Thompson22552902013-07-07 21:26:20 -0700187
Jeff Thompson5633c302013-07-11 10:24:27 -0700188 int gotExpectedTag;
Jeff Thompson94ddc272013-08-08 14:17:38 -0700189 if ((error = ndn_BinaryXmlDecoder_peekDTag(decoder, ndn_BinaryXml_DTag_Exclude, &gotExpectedTag)))
Jeff Thompsonf084ec62013-07-09 12:32:52 -0700190 return error;
191 if (gotExpectedTag) {
Jeff Thompson94ddc272013-08-08 14:17:38 -0700192 if ((error = decodeExclude(&interest->exclude, decoder)))
Jeff Thompsonf084ec62013-07-09 12:32:52 -0700193 return error;
194 }
195 else
196 interest->exclude.nEntries = 0;
Jeff Thompson22552902013-07-07 21:26:20 -0700197
Jeff Thompson94ddc272013-08-08 14:17:38 -0700198 if ((error = ndn_BinaryXmlDecoder_readOptionalUnsignedIntegerDTagElement
199 (decoder, ndn_BinaryXml_DTag_ChildSelector, &interest->childSelector)))
Jeff Thompson22552902013-07-07 21:26:20 -0700200 return error;
Jeff Thompson94ddc272013-08-08 14:17:38 -0700201 if ((error = ndn_BinaryXmlDecoder_readOptionalUnsignedIntegerDTagElement
202 (decoder, ndn_BinaryXml_DTag_AnswerOriginKind, &interest->answerOriginKind)))
Jeff Thompson22552902013-07-07 21:26:20 -0700203 return error;
Jeff Thompson94ddc272013-08-08 14:17:38 -0700204 if ((error = ndn_BinaryXmlDecoder_readOptionalUnsignedIntegerDTagElement
205 (decoder, ndn_BinaryXml_DTag_Scope, &interest->scope)))
Jeff Thompson22552902013-07-07 21:26:20 -0700206 return error;
207
Jeff Thompsonf68a74a2013-11-20 15:16:11 -0800208 if ((error = ndn_BinaryXmlDecoder_readOptionalTimeMillisecondsDTagElement
209 (decoder, ndn_BinaryXml_DTag_InterestLifetime, &interest->interestLifetimeMilliseconds)))
Jeff Thompson22552902013-07-07 21:26:20 -0700210 return error;
Jeff Thompson22552902013-07-07 21:26:20 -0700211
Jeff Thompson94ddc272013-08-08 14:17:38 -0700212 if ((error = ndn_BinaryXmlDecoder_readOptionalBinaryDTagElement
Jeff Thompson93034532013-10-08 11:52:43 -0700213 (decoder, ndn_BinaryXml_DTag_Nonce, 0, &interest->nonce)))
Jeff Thompson22552902013-07-07 21:26:20 -0700214 return error;
Jeff Thompson22552902013-07-07 21:26:20 -0700215
Jeff Thompson94ddc272013-08-08 14:17:38 -0700216 if ((error = ndn_BinaryXmlDecoder_readElementClose(decoder)))
Jeff Thompson22552902013-07-07 21:26:20 -0700217 return error;
218
Jeff Thompsonadaf9232013-08-08 14:30:29 -0700219 return NDN_ERROR_success;
Jeff Thompson06f824a2013-07-08 17:14:30 -0700220}