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