Shock Jiang | 6cce21a | 2014-09-07 10:14:12 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
| 3 | * Copyright (c) 2014, Regents of the University of California. |
| 4 | * |
| 5 | * This file is part of NDNS (Named Data Networking Domain Name Service). |
| 6 | * See AUTHORS.md for complete list of NDNS authors and contributors. |
| 7 | * |
| 8 | * NDNS is free software: you can redistribute it and/or modify it under the terms |
| 9 | * of the GNU General Public License as published by the Free Software Foundation, |
| 10 | * either version 3 of the License, or (at your option) any later version. |
| 11 | * |
| 12 | * NDNS is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; |
| 13 | * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR |
| 14 | * PURPOSE. See the GNU General Public License for more details. |
| 15 | * |
| 16 | * You should have received a copy of the GNU General Public License along with |
| 17 | * NDNS, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>. |
| 18 | */ |
| 19 | |
| 20 | #include "ndns-label.hpp" |
| 21 | #include <ndn-cxx/data.hpp> |
| 22 | #include <ndn-cxx/interest.hpp> |
| 23 | |
| 24 | namespace ndn { |
| 25 | namespace ndns { |
| 26 | namespace label { |
| 27 | |
| 28 | inline size_t |
| 29 | calculateSkip(const Name& name, |
| 30 | const Name& hint, const Name& zone) |
| 31 | { |
| 32 | size_t skip = 0; |
| 33 | |
| 34 | if (!hint.empty()) { |
| 35 | // These are only asserts. The caller should supply the right parameters |
| 36 | skip = hint.size() + 1 + zone.size(); |
| 37 | BOOST_ASSERT(name.size() > skip); |
| 38 | |
| 39 | BOOST_ASSERT(name.getPrefix(hint.size()) == hint); |
Shock Jiang | 895bc1b | 2014-10-01 20:00:58 -0700 | [diff] [blame] | 40 | BOOST_ASSERT(name.get(hint.size()) == FORWARDING_HINT_LABEL); |
Shock Jiang | 6cce21a | 2014-09-07 10:14:12 -0700 | [diff] [blame] | 41 | BOOST_ASSERT(name.getSubName(hint.size() + 1, zone.size()) == zone); |
| 42 | |
| 43 | } |
| 44 | else { |
| 45 | skip = zone.size(); |
| 46 | BOOST_ASSERT(name.size() > skip); |
| 47 | BOOST_ASSERT(name.getPrefix(zone.size()) == zone); |
| 48 | } |
| 49 | |
Shock Jiang | 895bc1b | 2014-10-01 20:00:58 -0700 | [diff] [blame] | 50 | BOOST_ASSERT(name.get(skip) == NDNS_ITERATIVE_QUERY || |
| 51 | name.get(skip) == NDNS_CERT_QUERY); |
Shock Jiang | 6cce21a | 2014-09-07 10:14:12 -0700 | [diff] [blame] | 52 | |
| 53 | ++skip; |
| 54 | return skip; |
| 55 | } |
| 56 | |
| 57 | bool |
| 58 | matchName(const Interest& interest, |
| 59 | const Name& hint, const Name& zone, |
| 60 | MatchResult& result) |
| 61 | { |
| 62 | // [hint / FHLabel] / zoneName / <Update>|rrLabel / UPDATE|rrType |
| 63 | |
| 64 | const Name& name = interest.getName(); |
| 65 | size_t skip = calculateSkip(name, hint, zone); |
| 66 | |
| 67 | if (name.size() - skip < 1) |
| 68 | return false; |
| 69 | |
| 70 | result.rrType = name.get(-1); |
| 71 | result.rrLabel = name.getSubName(skip, std::max<size_t>(0, name.size() - skip - 1)); |
| 72 | result.version = name::Component(); |
| 73 | |
| 74 | return true; |
| 75 | } |
| 76 | |
| 77 | bool |
| 78 | matchName(const Data& data, |
| 79 | const Name& hint, const Name& zone, |
| 80 | MatchResult& result) |
| 81 | { |
| 82 | // [hint / FHLabel] / zoneName / <Update>|rrLabel / UPDATE|rrType |
| 83 | |
| 84 | const Name& name = data.getName(); |
| 85 | size_t skip = calculateSkip(name, hint, zone); |
| 86 | |
| 87 | if (name.size() - skip < 2) |
| 88 | return false; |
| 89 | |
| 90 | result.version = name.get(-1); |
| 91 | result.rrType = name.get(-2); |
| 92 | result.rrLabel = name.getSubName(skip, std::max<size_t>(0, name.size() - skip - 2)); |
| 93 | |
| 94 | return true; |
| 95 | } |
| 96 | |
| 97 | } // namespace label |
| 98 | } // namespace ndns |
| 99 | } // namespace ndn |