blob: 793955659213feb15dcf886437b0830db97f8557 [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 Thompsonb7f95562013-07-03 18:36:42 -070010#include "BinaryXMLInterest.h"
11
Jeff Thompson06f824a2013-07-08 17:14:30 -070012ndn_Error ndn_encodeBinaryXMLInterest(struct ndn_Interest *interest, struct ndn_BinaryXMLEncoder *encoder)
13{
14 ndn_Error error;
15 if (error = ndn_BinaryXMLEncoder_writeElementStartDTag(encoder, ndn_BinaryXML_DTag_Interest))
16 return error;
17
18 if (error = ndn_encodeBinaryXMLName(&interest->name, encoder))
19 return error;
20
21 if (interest->minSuffixComponents >= 0) {
22 if (error = ndn_BinaryXMLEncoder_writeUnsignedDecimalIntDTagElement
23 (encoder, ndn_BinaryXML_DTag_MinSuffixComponents, (unsigned int)interest->minSuffixComponents))
24 return error;
25 }
26 if (interest->maxSuffixComponents >= 0) {
27 if (error = ndn_BinaryXMLEncoder_writeUnsignedDecimalIntDTagElement
28 (encoder, ndn_BinaryXML_DTag_MaxSuffixComponents, (unsigned int)interest->maxSuffixComponents))
29 return error;
30 }
31
32 if (interest->publisherPublicKeyDigest && interest->publisherPublicKeyDigestLength > 0) {
33 if (error = ndn_BinaryXMLEncoder_writeBlobDTagElement
34 (encoder, ndn_BinaryXML_DTag_PublisherPublicKeyDigest, interest->publisherPublicKeyDigest, interest->publisherPublicKeyDigestLength))
35 return error;
36 }
37
38 // TODO: Implement exclude
39
40 if (interest->childSelector >= 0) {
41 if (error = ndn_BinaryXMLEncoder_writeUnsignedDecimalIntDTagElement
42 (encoder, ndn_BinaryXML_DTag_ChildSelector, (unsigned int)interest->childSelector))
43 return error;
44 }
45 if (interest->answerOriginKind >= 0 && interest->answerOriginKind != ndn_Interest_DEFAULT_ANSWER_ORIGIN_KIND) {
46 if (error = ndn_BinaryXMLEncoder_writeUnsignedDecimalIntDTagElement
47 (encoder, ndn_BinaryXML_DTag_AnswerOriginKind, (unsigned int)interest->answerOriginKind))
48 return error;
49 }
50 if (interest->scope >= 0) {
51 if (error = ndn_BinaryXMLEncoder_writeUnsignedDecimalIntDTagElement
52 (encoder, ndn_BinaryXML_DTag_Scope, (unsigned int)interest->scope))
53 return error;
54 }
55
56 if (interest->interestLifetime >= 0) {
57 if (error = ndn_BinaryXMLEncoder_writeElementStartDTag(encoder, ndn_BinaryXML_DTag_InterestLifetime))
58 return error;
59
60 unsigned int ticks = (unsigned int)(((double)interest->interestLifetime / 1000.0) * 4096.0);
61 if (error = ndn_BinaryXMLEncoder_writeUnsignedIntBigEndianBlob(encoder, ticks))
62 return error;
63
64 if (error = ndn_BinaryXMLEncoder_writeElementClose(encoder))
65 return error;
66 }
67
68 if (interest->nonce && interest->nonceLength > 0) {
69 if (error = ndn_BinaryXMLEncoder_writeBlobDTagElement
70 (encoder, ndn_BinaryXML_DTag_Nonce, interest->nonce, interest->nonceLength))
71 return error;
72 }
73
74 if (error = ndn_BinaryXMLEncoder_writeElementClose(encoder))
75 return error;
76
77 return 0;
78}
79
Jeff Thompson8b666002013-07-08 01:16:26 -070080ndn_Error ndn_decodeBinaryXMLInterest(struct ndn_Interest *interest, struct ndn_BinaryXMLDecoder *decoder)
Jeff Thompsonb7f95562013-07-03 18:36:42 -070081{
Jeff Thompson8b666002013-07-08 01:16:26 -070082 ndn_Error error;
Jeff Thompson22552902013-07-07 21:26:20 -070083 if (error = ndn_BinaryXMLDecoder_readElementStartDTag(decoder, ndn_BinaryXML_DTag_Interest))
84 return error;
85
86 if (error = ndn_decodeBinaryXMLName(&interest->name, decoder))
87 return error;
88
89 if (error = ndn_BinaryXMLDecoder_readOptionalUnsignedIntegerDTagElement
90 (decoder, ndn_BinaryXML_DTag_MinSuffixComponents, &interest->minSuffixComponents))
91 return error;
92 if (error = ndn_BinaryXMLDecoder_readOptionalUnsignedIntegerDTagElement
93 (decoder, ndn_BinaryXML_DTag_MaxSuffixComponents, &interest->maxSuffixComponents))
94 return error;
Jeff Thompsonb7f95562013-07-03 18:36:42 -070095
Jeff Thompson22552902013-07-07 21:26:20 -070096 int gotExpectedTag;
97 if (error = ndn_BinaryXMLDecoder_peekDTag(decoder, ndn_BinaryXML_DTag_PublisherPublicKeyDigest, &gotExpectedTag))
98 return error;
99 if (gotExpectedTag) {
100 if (error = ndn_BinaryXMLDecoder_readBinaryDTagElement
101 (decoder, ndn_BinaryXML_DTag_PublisherPublicKeyDigest, 0, &interest->publisherPublicKeyDigest,
102 &interest->publisherPublicKeyDigestLength))
103 return error;
104 }
105 else {
106 interest->publisherPublicKeyDigest = 0;
107 interest->publisherPublicKeyDigestLength = 0;
108 }
109
110 // TODO: Implement exclude
111
112 if (error = ndn_BinaryXMLDecoder_readOptionalUnsignedIntegerDTagElement
113 (decoder, ndn_BinaryXML_DTag_ChildSelector, &interest->childSelector))
114 return error;
115 if (error = ndn_BinaryXMLDecoder_readOptionalUnsignedIntegerDTagElement
116 (decoder, ndn_BinaryXML_DTag_AnswerOriginKind, &interest->answerOriginKind))
117 return error;
118 if (error = ndn_BinaryXMLDecoder_readOptionalUnsignedIntegerDTagElement
119 (decoder, ndn_BinaryXML_DTag_Scope, &interest->scope))
120 return error;
121
122 if (error = ndn_BinaryXMLDecoder_peekDTag(decoder, ndn_BinaryXML_DTag_InterestLifetime, &gotExpectedTag))
123 return error;
124 if (gotExpectedTag) {
125 unsigned char *interestLifetime;
126 unsigned int interestLifetimeLength;
127 if (error = ndn_BinaryXMLDecoder_readBinaryDTagElement
128 (decoder, ndn_BinaryXML_DTag_InterestLifetime, 0, &interestLifetime, &interestLifetimeLength))
129 return error;
130
Jeff Thompson06f824a2013-07-08 17:14:30 -0700131 interest->interestLifetime = (int)(1000.0 *
132 (double)ndn_BinaryXMLDecoder_bigEndianToUnsignedInt(interestLifetime, interestLifetimeLength) / 4096.0);
Jeff Thompson22552902013-07-07 21:26:20 -0700133 }
134 else
135 interest->interestLifetime = -1;
136
137 if (error = ndn_BinaryXMLDecoder_peekDTag(decoder, ndn_BinaryXML_DTag_Nonce, &gotExpectedTag))
138 return error;
139 if (gotExpectedTag) {
140 if (error = ndn_BinaryXMLDecoder_readBinaryDTagElement
141 (decoder, ndn_BinaryXML_DTag_Nonce, 0, &interest->nonce, &interest->nonceLength))
142 return error;
143 }
144 else {
145 interest->nonce = 0;
146 interest->nonceLength = 0;
147 }
148
149 if (error = ndn_BinaryXMLDecoder_readElementClose(decoder))
150 return error;
151
152 return 0;
Jeff Thompson06f824a2013-07-08 17:14:30 -0700153}