blob: 585509cd5599d2ad5532ebe4aefbfc5c01a5709a [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"
21#include <ndn-cxx/data.hpp>
22#include <ndn-cxx/interest.hpp>
23
24namespace ndn {
25namespace ndns {
26namespace label {
27
28inline size_t
29calculateSkip(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 Jiang895bc1b2014-10-01 20:00:58 -070040 BOOST_ASSERT(name.get(hint.size()) == FORWARDING_HINT_LABEL);
Shock Jiang6cce21a2014-09-07 10:14:12 -070041 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 Jiang895bc1b2014-10-01 20:00:58 -070050 BOOST_ASSERT(name.get(skip) == NDNS_ITERATIVE_QUERY ||
51 name.get(skip) == NDNS_CERT_QUERY);
Shock Jiang6cce21a2014-09-07 10:14:12 -070052
53 ++skip;
54 return skip;
55}
56
57bool
58matchName(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
77bool
78matchName(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