blob: 1b1cc80de82aa3845f4dcdb70a77a97209c5725c [file] [log] [blame]
Shock Jiang6cce21a2014-09-07 10:14:12 -07001/* -*- 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"
Alexander Afanasyevaa46c272016-03-09 12:54:12 -080021
Shock Jiang6cce21a2014-09-07 10:14:12 -070022#include <ndn-cxx/data.hpp>
23#include <ndn-cxx/interest.hpp>
24
25namespace ndn {
26namespace ndns {
27namespace label {
28
29inline size_t
30calculateSkip(const Name& name,
31 const Name& hint, const Name& zone)
32{
33 size_t skip = 0;
34
35 if (!hint.empty()) {
36 // These are only asserts. The caller should supply the right parameters
37 skip = hint.size() + 1 + zone.size();
38 BOOST_ASSERT(name.size() > skip);
39
40 BOOST_ASSERT(name.getPrefix(hint.size()) == hint);
Shock Jiang895bc1b2014-10-01 20:00:58 -070041 BOOST_ASSERT(name.get(hint.size()) == FORWARDING_HINT_LABEL);
Shock Jiang6cce21a2014-09-07 10:14:12 -070042 BOOST_ASSERT(name.getSubName(hint.size() + 1, zone.size()) == zone);
43
44 }
45 else {
46 skip = zone.size();
47 BOOST_ASSERT(name.size() > skip);
48 BOOST_ASSERT(name.getPrefix(zone.size()) == zone);
49 }
50
Shock Jiang895bc1b2014-10-01 20:00:58 -070051 BOOST_ASSERT(name.get(skip) == NDNS_ITERATIVE_QUERY ||
52 name.get(skip) == NDNS_CERT_QUERY);
Shock Jiang6cce21a2014-09-07 10:14:12 -070053
54 ++skip;
55 return skip;
56}
57
58bool
59matchName(const Interest& interest,
60 const Name& hint, const Name& zone,
61 MatchResult& result)
62{
Alexander Afanasyevaa46c272016-03-09 12:54:12 -080063 // [hint / FHLabel] / zoneName / <Update>|rrLabel / UPDATE|rrType / [VERSION]
Shock Jiang6cce21a2014-09-07 10:14:12 -070064
65 const Name& name = interest.getName();
66 size_t skip = calculateSkip(name, hint, zone);
67
68 if (name.size() - skip < 1)
69 return false;
70
Alexander Afanasyevaa46c272016-03-09 12:54:12 -080071 size_t offset = 1;
72 if (name.get(-offset).isVersion()) {
73 result.version = name.get(-offset);
74 ++offset;
75 }
76 else {
77 result.version = name::Component();
78 }
79
80 result.rrType = name.get(-offset);
81 result.rrLabel = name.getSubName(skip, std::max<size_t>(0, name.size() - skip - offset));
Shock Jiang6cce21a2014-09-07 10:14:12 -070082
83 return true;
84}
85
86bool
87matchName(const Data& data,
88 const Name& hint, const Name& zone,
89 MatchResult& result)
90{
91 // [hint / FHLabel] / zoneName / <Update>|rrLabel / UPDATE|rrType
92
93 const Name& name = data.getName();
94 size_t skip = calculateSkip(name, hint, zone);
95
96 if (name.size() - skip < 2)
97 return false;
98
99 result.version = name.get(-1);
100 result.rrType = name.get(-2);
101 result.rrLabel = name.getSubName(skip, std::max<size_t>(0, name.size() - skip - 2));
102
103 return true;
104}
105
106} // namespace label
107} // namespace ndns
108} // namespace ndn