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 | 47eecfc | 2013-07-07 22:56:46 -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 | 47eecfc | 2013-07-07 22:56:46 -0700 | [diff] [blame] | 5 | * See COPYING for copyright and distribution information. |
Jeff Thompson | b7f9556 | 2013-07-03 18:36:42 -0700 | [diff] [blame] | 6 | */ |
| 7 | |
Jeff Thompson | fe55686 | 2013-07-09 13:52:55 -0700 | [diff] [blame] | 8 | #include <stdexcept> |
Jeff Thompson | 25b4e61 | 2013-10-10 16:03:24 -0700 | [diff] [blame] | 9 | #include <ndn-cpp/common.hpp> |
| 10 | #include <ndn-cpp/interest.hpp> |
Jeff Thompson | b7f9556 | 2013-07-03 18:36:42 -0700 | [diff] [blame] | 11 | |
Alexander Afanasyev | 8548084 | 2014-01-06 14:46:54 -0800 | [diff] [blame] | 12 | #if __clang__ |
| 13 | #pragma clang diagnostic push |
| 14 | #pragma clang diagnostic ignored "-Wreorder" |
| 15 | #pragma clang diagnostic ignored "-Wtautological-compare" |
| 16 | #pragma clang diagnostic ignored "-Wunused-variable" |
| 17 | #pragma clang diagnostic ignored "-Wunused-function" |
| 18 | #elif __GNUC__ |
| 19 | #pragma GCC diagnostic ignored "-Wreorder" |
Alexander Afanasyev | 8548084 | 2014-01-06 14:46:54 -0800 | [diff] [blame] | 20 | #pragma GCC diagnostic ignored "-Wunused-variable" |
| 21 | #pragma GCC diagnostic ignored "-Wunused-function" |
| 22 | #endif |
| 23 | |
Alexander Afanasyev | 840139f | 2013-12-28 15:02:50 -0800 | [diff] [blame] | 24 | #include <cryptopp/osrng.h> |
| 25 | |
Jeff Thompson | b7f9556 | 2013-07-03 18:36:42 -0700 | [diff] [blame] | 26 | using namespace std; |
| 27 | |
| 28 | namespace ndn { |
Alexander Afanasyev | 8468198 | 2014-01-03 13:26:09 -0800 | [diff] [blame] | 29 | |
Alexander Afanasyev | 8548084 | 2014-01-06 14:46:54 -0800 | [diff] [blame] | 30 | const uint32_t& |
Alexander Afanasyev | 840139f | 2013-12-28 15:02:50 -0800 | [diff] [blame] | 31 | Interest::getNonce() const |
| 32 | { |
| 33 | static CryptoPP::AutoSeededRandomPool rng; |
| 34 | |
| 35 | if (nonce_ == 0) |
| 36 | nonce_ = rng.GenerateWord32(); |
| 37 | |
| 38 | return nonce_; |
| 39 | } |
| 40 | |
| 41 | |
Alexander Afanasyev | 8468198 | 2014-01-03 13:26:09 -0800 | [diff] [blame] | 42 | bool |
| 43 | Interest::matchesName(const Name &name) const |
Jeff Thompson | 25b4e61 | 2013-10-10 16:03:24 -0700 | [diff] [blame] | 44 | { |
Alexander Afanasyev | 8468198 | 2014-01-03 13:26:09 -0800 | [diff] [blame] | 45 | if (!name_.isPrefixOf(name)) |
| 46 | return false; |
| 47 | |
| 48 | if (minSuffixComponents_ >= 0 && |
| 49 | // Add 1 for the implicit digest. |
| 50 | !(name.size() + 1 - name_.size() >= minSuffixComponents_)) |
| 51 | return false; |
| 52 | |
| 53 | if (maxSuffixComponents_ >= 0 && |
| 54 | // Add 1 for the implicit digest. |
| 55 | !(name.size() + 1 - name_.size() <= maxSuffixComponents_)) |
| 56 | return false; |
| 57 | |
| 58 | if (!exclude_.empty() && name.size() > name_.size() && |
| 59 | exclude_.isExcluded(name[name_.size()])) |
| 60 | return false; |
| 61 | |
| 62 | return true; |
Jeff Thompson | 25b4e61 | 2013-10-10 16:03:24 -0700 | [diff] [blame] | 63 | } |
| 64 | |
Alexander Afanasyev | 8468198 | 2014-01-03 13:26:09 -0800 | [diff] [blame] | 65 | std::ostream & |
| 66 | operator << (std::ostream &os, const Interest &interest) |
Jeff Thompson | fe55686 | 2013-07-09 13:52:55 -0700 | [diff] [blame] | 67 | { |
Alexander Afanasyev | 8468198 | 2014-01-03 13:26:09 -0800 | [diff] [blame] | 68 | os << interest.getName(); |
Jeff Thompson | fe55686 | 2013-07-09 13:52:55 -0700 | [diff] [blame] | 69 | |
Alexander Afanasyev | 8468198 | 2014-01-03 13:26:09 -0800 | [diff] [blame] | 70 | char delim = '?'; |
| 71 | |
| 72 | if (interest.getMinSuffixComponents() >= 0) { |
| 73 | os << delim << "ndn.MinSuffixComponents=" << interest.getMinSuffixComponents(); |
| 74 | delim = '&'; |
Jeff Thompson | fe55686 | 2013-07-09 13:52:55 -0700 | [diff] [blame] | 75 | } |
Alexander Afanasyev | 8468198 | 2014-01-03 13:26:09 -0800 | [diff] [blame] | 76 | if (interest.getMaxSuffixComponents() >= 0) { |
| 77 | os << delim << "ndn.MaxSuffixComponents=" << interest.getMaxSuffixComponents(); |
| 78 | delim = '&'; |
Jeff Thompson | 37527d6 | 2013-08-21 11:15:54 -0700 | [diff] [blame] | 79 | } |
Alexander Afanasyev | 8468198 | 2014-01-03 13:26:09 -0800 | [diff] [blame] | 80 | if (interest.getChildSelector() >= 0) { |
| 81 | os << delim << "ndn.ChildSelector=" << interest.getChildSelector(); |
| 82 | delim = '&'; |
Jeff Thompson | 13e280b | 2013-12-03 13:12:23 -0800 | [diff] [blame] | 83 | } |
Alexander Afanasyev | 8468198 | 2014-01-03 13:26:09 -0800 | [diff] [blame] | 84 | if (interest.getMustBeFresh()) { |
| 85 | os << delim << "ndn.MustBeFresh=" << interest.getMustBeFresh(); |
| 86 | delim = '&'; |
Jeff Thompson | 13e280b | 2013-12-03 13:12:23 -0800 | [diff] [blame] | 87 | } |
Alexander Afanasyev | 8468198 | 2014-01-03 13:26:09 -0800 | [diff] [blame] | 88 | if (interest.getScope() >= 0) { |
| 89 | os << delim << "ndn.Scope=" << interest.getScope(); |
| 90 | delim = '&'; |
Jeff Thompson | 13e280b | 2013-12-03 13:12:23 -0800 | [diff] [blame] | 91 | } |
Alexander Afanasyev | 8468198 | 2014-01-03 13:26:09 -0800 | [diff] [blame] | 92 | if (interest.getInterestLifetime() >= 0) { |
| 93 | os << delim << "ndn.InterestLifetime=" << interest.getInterestLifetime(); |
| 94 | delim = '&'; |
| 95 | } |
| 96 | |
| 97 | if (interest.getNonce() > 0) { |
| 98 | os << delim << "ndn.Nonce=" << interest.getNonce(); |
| 99 | delim = '&'; |
| 100 | } |
| 101 | if (!interest.getExclude().empty()) { |
| 102 | os << delim << "ndn.Exclude=" << interest.getExclude(); |
| 103 | delim = '&'; |
| 104 | } |
| 105 | |
| 106 | return os; |
Jeff Thompson | 13e280b | 2013-12-03 13:12:23 -0800 | [diff] [blame] | 107 | } |
| 108 | |
Alexander Afanasyev | 1eb961a | 2014-01-03 13:51:49 -0800 | [diff] [blame] | 109 | const Block& |
| 110 | Interest::wireEncode() const |
| 111 | { |
| 112 | if (wire_.hasWire()) |
| 113 | return wire_; |
| 114 | |
Alexander Afanasyev | ff0a394 | 2014-01-03 15:31:28 -0800 | [diff] [blame] | 115 | // Interest ::= INTEREST-TYPE TLV-LENGTH |
| 116 | // Name |
| 117 | // Selectors? |
| 118 | // Nonce |
| 119 | // Scope? |
| 120 | // InterestLifetime? |
| 121 | |
Alexander Afanasyev | 1eb961a | 2014-01-03 13:51:49 -0800 | [diff] [blame] | 122 | wire_ = Block(Tlv::Interest); |
| 123 | wire_.push_back(getName().wireEncode()); |
| 124 | |
| 125 | // selectors |
| 126 | { |
| 127 | Block selectors(Tlv::Selectors); |
| 128 | |
| 129 | if (getMinSuffixComponents() >= 0) { |
Alexander Afanasyev | ff0a394 | 2014-01-03 15:31:28 -0800 | [diff] [blame] | 130 | selectors.push_back |
| 131 | (nonNegativeIntegerBlock(Tlv::MinSuffixComponents, getMinSuffixComponents())); |
Alexander Afanasyev | 1eb961a | 2014-01-03 13:51:49 -0800 | [diff] [blame] | 132 | } |
| 133 | if (getMaxSuffixComponents() >= 0) { |
Alexander Afanasyev | ff0a394 | 2014-01-03 15:31:28 -0800 | [diff] [blame] | 134 | selectors.push_back |
| 135 | (nonNegativeIntegerBlock(Tlv::MaxSuffixComponents, getMaxSuffixComponents())); |
Alexander Afanasyev | 1eb961a | 2014-01-03 13:51:49 -0800 | [diff] [blame] | 136 | } |
| 137 | if (!getExclude().empty()) { |
Alexander Afanasyev | ff0a394 | 2014-01-03 15:31:28 -0800 | [diff] [blame] | 138 | selectors.push_back |
| 139 | (getExclude().wireEncode()); |
Alexander Afanasyev | 1eb961a | 2014-01-03 13:51:49 -0800 | [diff] [blame] | 140 | } |
| 141 | if (getChildSelector() >= 0) { |
Alexander Afanasyev | ff0a394 | 2014-01-03 15:31:28 -0800 | [diff] [blame] | 142 | selectors.push_back |
| 143 | (nonNegativeIntegerBlock(Tlv::ChildSelector, getChildSelector())); |
Alexander Afanasyev | 1eb961a | 2014-01-03 13:51:49 -0800 | [diff] [blame] | 144 | } |
| 145 | if (getMustBeFresh()) { |
Alexander Afanasyev | ff0a394 | 2014-01-03 15:31:28 -0800 | [diff] [blame] | 146 | selectors.push_back |
| 147 | (booleanBlock(Tlv::MustBeFresh)); |
Alexander Afanasyev | 1eb961a | 2014-01-03 13:51:49 -0800 | [diff] [blame] | 148 | } |
| 149 | |
Alexander Afanasyev | 840139f | 2013-12-28 15:02:50 -0800 | [diff] [blame] | 150 | if (!selectors.getAll().empty()) |
| 151 | { |
| 152 | selectors.encode(); |
| 153 | wire_.push_back(selectors); |
| 154 | } |
Alexander Afanasyev | 1eb961a | 2014-01-03 13:51:49 -0800 | [diff] [blame] | 155 | } |
| 156 | |
| 157 | // Nonce |
| 158 | { |
Alexander Afanasyev | ff0a394 | 2014-01-03 15:31:28 -0800 | [diff] [blame] | 159 | wire_.push_back |
| 160 | (nonNegativeIntegerBlock(Tlv::Nonce, getNonce())); |
Alexander Afanasyev | 1eb961a | 2014-01-03 13:51:49 -0800 | [diff] [blame] | 161 | } |
| 162 | |
| 163 | if (getScope() >= 0) { |
Alexander Afanasyev | ff0a394 | 2014-01-03 15:31:28 -0800 | [diff] [blame] | 164 | wire_.push_back |
| 165 | (nonNegativeIntegerBlock(Tlv::Scope, getScope())); |
Alexander Afanasyev | 1eb961a | 2014-01-03 13:51:49 -0800 | [diff] [blame] | 166 | } |
| 167 | if (getInterestLifetime() >= 0) { |
Alexander Afanasyev | ff0a394 | 2014-01-03 15:31:28 -0800 | [diff] [blame] | 168 | wire_.push_back |
| 169 | (nonNegativeIntegerBlock(Tlv::InterestLifetime, getInterestLifetime())); |
Alexander Afanasyev | 1eb961a | 2014-01-03 13:51:49 -0800 | [diff] [blame] | 170 | } |
| 171 | |
| 172 | wire_.encode(); |
| 173 | return wire_; |
| 174 | } |
| 175 | |
| 176 | void |
| 177 | Interest::wireDecode(const Block &wire) |
| 178 | { |
| 179 | wire_ = wire; |
| 180 | wire_.parse(); |
| 181 | |
Alexander Afanasyev | ff0a394 | 2014-01-03 15:31:28 -0800 | [diff] [blame] | 182 | // Interest ::= INTEREST-TYPE TLV-LENGTH |
| 183 | // Name |
| 184 | // Selectors? |
| 185 | // Nonce |
| 186 | // Scope? |
| 187 | // InterestLifetime? |
| 188 | |
Alexander Afanasyev | 1eb961a | 2014-01-03 13:51:49 -0800 | [diff] [blame] | 189 | // Name |
| 190 | name_.wireDecode(wire_.get(Tlv::Name)); |
| 191 | |
| 192 | // Selectors |
| 193 | Block::element_iterator selectors = wire_.find(Tlv::Selectors); |
| 194 | if (selectors != wire_.getAll().end()) |
| 195 | { |
| 196 | selectors->parse(); |
| 197 | |
| 198 | // MinSuffixComponents |
| 199 | Block::element_iterator val = selectors->find(Tlv::MinSuffixComponents); |
| 200 | if (val != selectors->getAll().end()) |
| 201 | { |
Alexander Afanasyev | ff0a394 | 2014-01-03 15:31:28 -0800 | [diff] [blame] | 202 | minSuffixComponents_ = readNonNegativeInteger(*val); |
Alexander Afanasyev | 1eb961a | 2014-01-03 13:51:49 -0800 | [diff] [blame] | 203 | } |
| 204 | |
| 205 | // MaxSuffixComponents |
| 206 | val = selectors->find(Tlv::MaxSuffixComponents); |
| 207 | if (val != selectors->getAll().end()) |
| 208 | { |
Alexander Afanasyev | ff0a394 | 2014-01-03 15:31:28 -0800 | [diff] [blame] | 209 | maxSuffixComponents_ = readNonNegativeInteger(*val); |
Alexander Afanasyev | 1eb961a | 2014-01-03 13:51:49 -0800 | [diff] [blame] | 210 | } |
| 211 | |
| 212 | // Exclude |
| 213 | val = selectors->find(Tlv::Exclude); |
| 214 | if (val != selectors->getAll().end()) |
| 215 | { |
| 216 | exclude_.wireDecode(*val); |
| 217 | } |
| 218 | |
| 219 | // ChildSelector |
| 220 | val = selectors->find(Tlv::ChildSelector); |
| 221 | if (val != selectors->getAll().end()) |
| 222 | { |
Alexander Afanasyev | ff0a394 | 2014-01-03 15:31:28 -0800 | [diff] [blame] | 223 | childSelector_ = readNonNegativeInteger(*val); |
Alexander Afanasyev | 1eb961a | 2014-01-03 13:51:49 -0800 | [diff] [blame] | 224 | } |
| 225 | |
| 226 | //MustBeFresh aka AnswerOriginKind |
| 227 | val = selectors->find(Tlv::MustBeFresh); |
| 228 | if (val != selectors->getAll().end()) |
| 229 | { |
| 230 | mustBeFresh_ = true; |
| 231 | } |
| 232 | } |
| 233 | |
| 234 | // Nonce |
| 235 | Block::element_iterator val = wire_.find(Tlv::Nonce); |
| 236 | if (val != wire_.getAll().end()) |
| 237 | { |
Alexander Afanasyev | ff0a394 | 2014-01-03 15:31:28 -0800 | [diff] [blame] | 238 | nonce_ = readNonNegativeInteger(*val); |
Alexander Afanasyev | 1eb961a | 2014-01-03 13:51:49 -0800 | [diff] [blame] | 239 | } |
| 240 | |
| 241 | // Scope |
| 242 | val = wire_.find(Tlv::Scope); |
| 243 | if (val != wire_.getAll().end()) |
| 244 | { |
Alexander Afanasyev | ff0a394 | 2014-01-03 15:31:28 -0800 | [diff] [blame] | 245 | scope_ = readNonNegativeInteger(*val); |
Alexander Afanasyev | 1eb961a | 2014-01-03 13:51:49 -0800 | [diff] [blame] | 246 | } |
| 247 | |
| 248 | // InterestLifetime |
| 249 | val = wire_.find(Tlv::InterestLifetime); |
| 250 | if (val != wire_.getAll().end()) |
| 251 | { |
Alexander Afanasyev | ff0a394 | 2014-01-03 15:31:28 -0800 | [diff] [blame] | 252 | interestLifetime_ = readNonNegativeInteger(*val); |
Alexander Afanasyev | 1eb961a | 2014-01-03 13:51:49 -0800 | [diff] [blame] | 253 | } |
| 254 | } |
| 255 | |
| 256 | |
Jeff Thompson | b7f9556 | 2013-07-03 18:36:42 -0700 | [diff] [blame] | 257 | } |