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