Jeff Thompson | 25b4e61 | 2013-10-10 16:03:24 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil -*- */ |
Jeff Thompson | 5cae5e5 | 2013-07-10 19:41:20 -0700 | [diff] [blame] | 2 | /** |
Jeff Thompson | 7687dc0 | 2013-09-13 11:54:07 -0700 | [diff] [blame] | 3 | * Copyright (C) 2013 Regents of the University of California. |
| 4 | * @author: Jeff Thompson <jefft0@remap.ucla.edu> |
Jeff Thompson | 5cae5e5 | 2013-07-10 19:41:20 -0700 | [diff] [blame] | 5 | * See COPYING for copyright and distribution information. |
| 6 | */ |
| 7 | |
Jeff Thompson | 71b2f87 | 2013-12-17 12:03:17 -0800 | [diff] [blame] | 8 | #ifndef NDN_KEY_LOCATOR_HPP |
| 9 | #define NDN_KEY_LOCATOR_HPP |
Jeff Thompson | 5cae5e5 | 2013-07-10 19:41:20 -0700 | [diff] [blame] | 10 | |
Alexander Afanasyev | 2ba8f66 | 2014-01-05 22:53:18 -0800 | [diff] [blame] | 11 | #include "encoding/block.hpp" |
Jeff Thompson | 2e6269c | 2013-08-22 10:36:00 -0700 | [diff] [blame] | 12 | #include "name.hpp" |
Jeff Thompson | 5cae5e5 | 2013-07-10 19:41:20 -0700 | [diff] [blame] | 13 | |
| 14 | namespace ndn { |
Alexander Afanasyev | fadc97d | 2014-01-03 13:22:10 -0800 | [diff] [blame] | 15 | |
Jeff Thompson | 5cae5e5 | 2013-07-10 19:41:20 -0700 | [diff] [blame] | 16 | class KeyLocator { |
| 17 | public: |
Alexander Afanasyev | 2ba8f66 | 2014-01-05 22:53:18 -0800 | [diff] [blame] | 18 | struct Error : public std::runtime_error { Error(const std::string &what) : std::runtime_error(what) {} }; |
| 19 | |
Alexander Afanasyev | fadc97d | 2014-01-03 13:22:10 -0800 | [diff] [blame] | 20 | enum { |
Alexander Afanasyev | 9cae668 | 2014-02-19 14:18:56 -0800 | [diff] [blame^] | 21 | KeyLocator_None = 65535, // just an arbitrarily large number (used only internally) |
Alexander Afanasyev | 2ba8f66 | 2014-01-05 22:53:18 -0800 | [diff] [blame] | 22 | KeyLocator_Name = 0, |
| 23 | |
| 24 | KeyLocator_Unknown = 255 |
Alexander Afanasyev | fadc97d | 2014-01-03 13:22:10 -0800 | [diff] [blame] | 25 | }; |
Alexander Afanasyev | fa13f8e | 2014-01-03 15:19:07 -0800 | [diff] [blame] | 26 | |
| 27 | inline |
Jeff Thompson | 5cae5e5 | 2013-07-10 19:41:20 -0700 | [diff] [blame] | 28 | KeyLocator() |
Alexander Afanasyev | 809805d | 2014-02-17 17:20:33 -0800 | [diff] [blame] | 29 | : m_type(KeyLocator_None) |
Jeff Thompson | 5cae5e5 | 2013-07-10 19:41:20 -0700 | [diff] [blame] | 30 | { |
Jeff Thompson | 5cae5e5 | 2013-07-10 19:41:20 -0700 | [diff] [blame] | 31 | } |
Jeff Thompson | 5cae5e5 | 2013-07-10 19:41:20 -0700 | [diff] [blame] | 32 | |
Alexander Afanasyev | fa13f8e | 2014-01-03 15:19:07 -0800 | [diff] [blame] | 33 | inline |
| 34 | KeyLocator(const Name &name); |
| 35 | |
Alexander Afanasyev | 809805d | 2014-02-17 17:20:33 -0800 | [diff] [blame] | 36 | /////////////////////////////////////////////////////////////////////////////// |
| 37 | |
| 38 | template<bool T> |
| 39 | size_t |
| 40 | wireEncode(EncodingImpl<T> &block) const; |
Alexander Afanasyev | 2ba8f66 | 2014-01-05 22:53:18 -0800 | [diff] [blame] | 41 | |
Alexander Afanasyev | 809805d | 2014-02-17 17:20:33 -0800 | [diff] [blame] | 42 | const Block& |
| 43 | wireEncode() const; |
| 44 | |
| 45 | void |
| 46 | wireDecode(const Block &wire); |
| 47 | |
| 48 | /////////////////////////////////////////////////////////////////////////////// |
Alexander Afanasyev | 2ba8f66 | 2014-01-05 22:53:18 -0800 | [diff] [blame] | 49 | |
Alexander Afanasyev | fadc97d | 2014-01-03 13:22:10 -0800 | [diff] [blame] | 50 | inline bool |
| 51 | empty() const |
| 52 | { |
Alexander Afanasyev | 809805d | 2014-02-17 17:20:33 -0800 | [diff] [blame] | 53 | return m_type == KeyLocator_None; |
Alexander Afanasyev | fadc97d | 2014-01-03 13:22:10 -0800 | [diff] [blame] | 54 | } |
| 55 | |
| 56 | uint32_t |
Alexander Afanasyev | 809805d | 2014-02-17 17:20:33 -0800 | [diff] [blame] | 57 | getType() const { return m_type; } |
Alexander Afanasyev | 2ba8f66 | 2014-01-05 22:53:18 -0800 | [diff] [blame] | 58 | |
Alexander Afanasyev | fadc97d | 2014-01-03 13:22:10 -0800 | [diff] [blame] | 59 | //////////////////////////////////////////////////////// |
| 60 | // Helper methods for different types of key locators |
| 61 | // |
| 62 | // For now only Name type is actually supported |
| 63 | |
Alexander Afanasyev | 2ba8f66 | 2014-01-05 22:53:18 -0800 | [diff] [blame] | 64 | inline const Name& |
| 65 | getName() const; |
Alexander Afanasyev | fadc97d | 2014-01-03 13:22:10 -0800 | [diff] [blame] | 66 | |
Alexander Afanasyev | 2ba8f66 | 2014-01-05 22:53:18 -0800 | [diff] [blame] | 67 | inline void |
| 68 | setName(const Name &name); |
Jeff Thompson | 35a2bc1 | 2013-09-17 14:23:13 -0700 | [diff] [blame] | 69 | |
Jeff Thompson | 5cae5e5 | 2013-07-10 19:41:20 -0700 | [diff] [blame] | 70 | private: |
Alexander Afanasyev | 809805d | 2014-02-17 17:20:33 -0800 | [diff] [blame] | 71 | uint32_t m_type; |
| 72 | Name m_name; |
Alexander Afanasyev | 2ba8f66 | 2014-01-05 22:53:18 -0800 | [diff] [blame] | 73 | |
Alexander Afanasyev | 809805d | 2014-02-17 17:20:33 -0800 | [diff] [blame] | 74 | mutable Block m_wire; |
Jeff Thompson | 5cae5e5 | 2013-07-10 19:41:20 -0700 | [diff] [blame] | 75 | }; |
Alexander Afanasyev | fadc97d | 2014-01-03 13:22:10 -0800 | [diff] [blame] | 76 | |
Alexander Afanasyev | fa13f8e | 2014-01-03 15:19:07 -0800 | [diff] [blame] | 77 | inline |
| 78 | KeyLocator::KeyLocator(const Name &name) |
| 79 | { |
| 80 | setName(name); |
| 81 | } |
| 82 | |
Alexander Afanasyev | 809805d | 2014-02-17 17:20:33 -0800 | [diff] [blame] | 83 | template<bool T> |
| 84 | inline size_t |
| 85 | KeyLocator::wireEncode(EncodingImpl<T>& block) const |
Alexander Afanasyev | 2ba8f66 | 2014-01-05 22:53:18 -0800 | [diff] [blame] | 86 | { |
Alexander Afanasyev | 809805d | 2014-02-17 17:20:33 -0800 | [diff] [blame] | 87 | // KeyLocator ::= KEY-LOCATOR-TYPE TLV-LENGTH KeyLocatorValue |
Alexander Afanasyev | e9a66e5 | 2014-01-17 16:07:17 -0800 | [diff] [blame] | 88 | |
Alexander Afanasyev | 809805d | 2014-02-17 17:20:33 -0800 | [diff] [blame] | 89 | // KeyLocatorValue ::= Name | |
| 90 | // KeyLocatorDigest | (not supported yet) |
| 91 | // ... |
Alexander Afanasyev | e9a66e5 | 2014-01-17 16:07:17 -0800 | [diff] [blame] | 92 | |
Alexander Afanasyev | 809805d | 2014-02-17 17:20:33 -0800 | [diff] [blame] | 93 | // KeyLocatorDigest ::= KEY-LOCATOR-DIGEST-TYPE TLV-LENGTH BYTE+ |
| 94 | |
| 95 | size_t total_len = 0; |
| 96 | |
| 97 | switch (m_type) { |
Alexander Afanasyev | e9a66e5 | 2014-01-17 16:07:17 -0800 | [diff] [blame] | 98 | case KeyLocator_None: |
Alexander Afanasyev | e9a66e5 | 2014-01-17 16:07:17 -0800 | [diff] [blame] | 99 | break; |
| 100 | case KeyLocator_Name: |
Alexander Afanasyev | 809805d | 2014-02-17 17:20:33 -0800 | [diff] [blame] | 101 | total_len += m_name.wireEncode(block); |
Alexander Afanasyev | e9a66e5 | 2014-01-17 16:07:17 -0800 | [diff] [blame] | 102 | break; |
| 103 | default: |
| 104 | throw Error("Unsupported KeyLocator type"); |
| 105 | } |
Alexander Afanasyev | 809805d | 2014-02-17 17:20:33 -0800 | [diff] [blame] | 106 | |
| 107 | total_len += block.prependVarNumber (total_len); |
| 108 | total_len += block.prependVarNumber (Tlv::KeyLocator); |
| 109 | return total_len; |
| 110 | } |
| 111 | |
| 112 | inline const Block& |
| 113 | KeyLocator::wireEncode() const |
| 114 | { |
| 115 | if (m_wire.hasWire ()) |
| 116 | return m_wire; |
| 117 | |
| 118 | EncodingEstimator estimator; |
| 119 | size_t estimatedSize = wireEncode(estimator); |
Alexander Afanasyev | 2ba8f66 | 2014-01-05 22:53:18 -0800 | [diff] [blame] | 120 | |
Alexander Afanasyev | 809805d | 2014-02-17 17:20:33 -0800 | [diff] [blame] | 121 | EncodingBuffer buffer(estimatedSize, 0); |
| 122 | wireEncode(buffer); |
| 123 | |
| 124 | m_wire = buffer.block(); |
| 125 | return m_wire; |
Jeff Thompson | 5cae5e5 | 2013-07-10 19:41:20 -0700 | [diff] [blame] | 126 | } |
| 127 | |
Alexander Afanasyev | 2ba8f66 | 2014-01-05 22:53:18 -0800 | [diff] [blame] | 128 | inline void |
| 129 | KeyLocator::wireDecode(const Block &value) |
| 130 | { |
Alexander Afanasyev | 809805d | 2014-02-17 17:20:33 -0800 | [diff] [blame] | 131 | if (value.type() != Tlv::KeyLocator) |
| 132 | throw Error("Unexpected TLV type during KeyLocator decoding"); |
| 133 | |
| 134 | m_wire = value; |
| 135 | m_wire.parse(); |
Alexander Afanasyev | 2ba8f66 | 2014-01-05 22:53:18 -0800 | [diff] [blame] | 136 | |
Alexander Afanasyev | 809805d | 2014-02-17 17:20:33 -0800 | [diff] [blame] | 137 | if (!m_wire.elements().empty() && m_wire.elements_begin()->type() == Tlv::Name) |
Alexander Afanasyev | 2ba8f66 | 2014-01-05 22:53:18 -0800 | [diff] [blame] | 138 | { |
Alexander Afanasyev | 809805d | 2014-02-17 17:20:33 -0800 | [diff] [blame] | 139 | m_type = KeyLocator_Name; |
| 140 | m_name.wireDecode(*m_wire.elements_begin()); |
Alexander Afanasyev | 2ba8f66 | 2014-01-05 22:53:18 -0800 | [diff] [blame] | 141 | } |
| 142 | else |
| 143 | { |
Alexander Afanasyev | 809805d | 2014-02-17 17:20:33 -0800 | [diff] [blame] | 144 | m_type = KeyLocator_Unknown; |
Alexander Afanasyev | 2ba8f66 | 2014-01-05 22:53:18 -0800 | [diff] [blame] | 145 | } |
| 146 | } |
| 147 | |
| 148 | inline const Name& |
| 149 | KeyLocator::getName() const |
| 150 | { |
Alexander Afanasyev | 809805d | 2014-02-17 17:20:33 -0800 | [diff] [blame] | 151 | if (m_type != KeyLocator_Name) |
Alexander Afanasyev | 2ba8f66 | 2014-01-05 22:53:18 -0800 | [diff] [blame] | 152 | throw Error("Requested Name, but KeyLocator is not of the Name type"); |
| 153 | |
Alexander Afanasyev | 809805d | 2014-02-17 17:20:33 -0800 | [diff] [blame] | 154 | return m_name; |
Alexander Afanasyev | 2ba8f66 | 2014-01-05 22:53:18 -0800 | [diff] [blame] | 155 | } |
| 156 | |
| 157 | inline void |
| 158 | KeyLocator::setName(const Name &name) |
| 159 | { |
Alexander Afanasyev | 809805d | 2014-02-17 17:20:33 -0800 | [diff] [blame] | 160 | m_type = KeyLocator_Name; |
| 161 | m_name = name; |
Alexander Afanasyev | 2ba8f66 | 2014-01-05 22:53:18 -0800 | [diff] [blame] | 162 | } |
| 163 | |
| 164 | |
| 165 | } // namespace ndn |
| 166 | |
Jeff Thompson | 5cae5e5 | 2013-07-10 19:41:20 -0700 | [diff] [blame] | 167 | #endif |