blob: d7ed52a956a1a5f1736e4bd05373e17cb7eb5a31 [file] [log] [blame]
Jeff Thompson990599b2013-08-27 15:14:25 -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 Thompson990599b2013-08-27 15:14:25 -07004 * Derived from ForwardingEntry.js by Meki Cheraoui.
5 * See COPYING for copyright and distribution information.
6 */
7
8#include "binary-xml.h"
9#include "binary-xml-forwarding-entry.h"
Jeff Thompson3c166032013-11-20 15:12:19 -080010#include "binary-xml-name.h"
11#include "binary-xml-key.h"
12#include "binary-xml-publisher-public-key-digest.h"
Jeff Thompson990599b2013-08-27 15:14:25 -070013
14ndn_Error ndn_encodeBinaryXmlForwardingEntry(struct ndn_ForwardingEntry *forwardingEntry, struct ndn_BinaryXmlEncoder *encoder)
15{
16 ndn_Error error;
17 if ((error = ndn_BinaryXmlEncoder_writeElementStartDTag(encoder, ndn_BinaryXml_DTag_ForwardingEntry)))
18 return error;
19
20 if ((error = ndn_BinaryXmlEncoder_writeOptionalUDataDTagElement
Jeff Thompson93034532013-10-08 11:52:43 -070021 (encoder, ndn_BinaryXml_DTag_Action, &forwardingEntry->action)))
Jeff Thompson990599b2013-08-27 15:14:25 -070022 return error;
23 if ((error = ndn_encodeBinaryXmlName(&forwardingEntry->prefix, encoder)))
24 return error;
25 // This will skip encoding if there is no publisherPublicKeyDigest.
26 if ((error = ndn_encodeBinaryXmlPublisherPublicKeyDigest(&forwardingEntry->publisherPublicKeyDigest, encoder)))
27 return error;
28 if ((error = ndn_BinaryXmlEncoder_writeOptionalUnsignedDecimalIntDTagElement
29 (encoder, ndn_BinaryXml_DTag_FaceID, forwardingEntry->faceId)))
30 return error;
Jeff Thompson1f8a31a2013-09-30 16:18:47 -070031 if ((error = ndn_BinaryXmlEncoder_writeUnsignedDecimalIntDTagElement
32 (encoder, ndn_BinaryXml_DTag_ForwardingFlags,
33 ndn_ForwardingFlags_getForwardingEntryFlags(&forwardingEntry->forwardingFlags))))
Jeff Thompson990599b2013-08-27 15:14:25 -070034 return error;
35 if ((error = ndn_BinaryXmlEncoder_writeOptionalUnsignedDecimalIntDTagElement
36 (encoder, ndn_BinaryXml_DTag_FreshnessSeconds, forwardingEntry->freshnessSeconds)))
37 return error;
38
39 if ((error = ndn_BinaryXmlEncoder_writeElementClose(encoder)))
40 return error;
41
42 return NDN_ERROR_success;
43}
44
45ndn_Error ndn_decodeBinaryXmlForwardingEntry(struct ndn_ForwardingEntry *forwardingEntry, struct ndn_BinaryXmlDecoder *decoder)
46{
47 ndn_Error error;
48 if ((error = ndn_BinaryXmlDecoder_readElementStartDTag(decoder, ndn_BinaryXml_DTag_ForwardingEntry)))
49 return error;
50
51 if ((error = ndn_BinaryXmlDecoder_readOptionalUDataDTagElement
Jeff Thompson93034532013-10-08 11:52:43 -070052 (decoder, ndn_BinaryXml_DTag_Action, &forwardingEntry->action)))
Jeff Thompson990599b2013-08-27 15:14:25 -070053 return error;
54 if ((error = ndn_decodeBinaryXmlName(&forwardingEntry->prefix, decoder)))
55 return error;
56 if ((error = ndn_decodeOptionalBinaryXmlPublisherPublicKeyDigest(&forwardingEntry->publisherPublicKeyDigest, decoder)))
57 return error;
58 if ((error = ndn_BinaryXmlDecoder_readOptionalUnsignedIntegerDTagElement
59 (decoder, ndn_BinaryXml_DTag_FaceID, &forwardingEntry->faceId)))
60 return error;
Jeff Thompson1f8a31a2013-09-30 16:18:47 -070061
62 int forwardingEntryFlags;
Jeff Thompson990599b2013-08-27 15:14:25 -070063 if ((error = ndn_BinaryXmlDecoder_readOptionalUnsignedIntegerDTagElement
Jeff Thompson1f8a31a2013-09-30 16:18:47 -070064 (decoder, ndn_BinaryXml_DTag_ForwardingFlags, &forwardingEntryFlags)))
Jeff Thompson990599b2013-08-27 15:14:25 -070065 return error;
Jeff Thompson1f8a31a2013-09-30 16:18:47 -070066 if (forwardingEntryFlags >= 0)
67 ndn_ForwardingFlags_setForwardingEntryFlags(&forwardingEntry->forwardingFlags, forwardingEntryFlags);
68 else
69 // This sets the default flags.
70 ndn_ForwardingFlags_initialize(&forwardingEntry->forwardingFlags);
71
Jeff Thompson990599b2013-08-27 15:14:25 -070072 if ((error = ndn_BinaryXmlDecoder_readOptionalUnsignedIntegerDTagElement
73 (decoder, ndn_BinaryXml_DTag_FreshnessSeconds, &forwardingEntry->freshnessSeconds)))
74 return error;
75
76 if ((error = ndn_BinaryXmlDecoder_readElementClose(decoder)))
77 return error;
78
79 return NDN_ERROR_success;
80}