blob: 9386452370ce3103561f0985a6ad96342d799b4b [file] [log] [blame]
Shock Jiang6cce21a2014-09-07 10:14:12 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
Yumin Xia6343c5b2016-10-20 15:45:50 -07003 * Copyright (c) 2014-2016, Regents of the University of California.
Shock Jiang6cce21a2014-09-07 10:14:12 -07004 *
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,
Yumin Xia6343c5b2016-10-20 15:45:50 -070031 const Name& zone)
Shock Jiang6cce21a2014-09-07 10:14:12 -070032{
33 size_t skip = 0;
34
Yumin Xia6343c5b2016-10-20 15:45:50 -070035 skip = zone.size();
36 BOOST_ASSERT(name.size() > skip);
37 BOOST_ASSERT(name.getPrefix(zone.size()) == zone);
Shock Jiang6cce21a2014-09-07 10:14:12 -070038
Shock Jiang895bc1b2014-10-01 20:00:58 -070039 BOOST_ASSERT(name.get(skip) == NDNS_ITERATIVE_QUERY ||
40 name.get(skip) == NDNS_CERT_QUERY);
Shock Jiang6cce21a2014-09-07 10:14:12 -070041
42 ++skip;
43 return skip;
44}
45
46bool
47matchName(const Interest& interest,
Yumin Xia6343c5b2016-10-20 15:45:50 -070048 const Name& zone,
Shock Jiang6cce21a2014-09-07 10:14:12 -070049 MatchResult& result)
50{
Yumin Xia6343c5b2016-10-20 15:45:50 -070051 // zoneName / <Update>|rrLabel / UPDATE|rrType / [VERSION]
Shock Jiang6cce21a2014-09-07 10:14:12 -070052
53 const Name& name = interest.getName();
Yumin Xia6343c5b2016-10-20 15:45:50 -070054 size_t skip = calculateSkip(name, zone);
Shock Jiang6cce21a2014-09-07 10:14:12 -070055
56 if (name.size() - skip < 1)
57 return false;
58
Alexander Afanasyevaa46c272016-03-09 12:54:12 -080059 size_t offset = 1;
60 if (name.get(-offset).isVersion()) {
61 result.version = name.get(-offset);
62 ++offset;
63 }
64 else {
65 result.version = name::Component();
66 }
67
68 result.rrType = name.get(-offset);
69 result.rrLabel = name.getSubName(skip, std::max<size_t>(0, name.size() - skip - offset));
Shock Jiang6cce21a2014-09-07 10:14:12 -070070
71 return true;
72}
73
74bool
75matchName(const Data& data,
Yumin Xia6343c5b2016-10-20 15:45:50 -070076 const Name& zone,
Shock Jiang6cce21a2014-09-07 10:14:12 -070077 MatchResult& result)
78{
Yumin Xia6343c5b2016-10-20 15:45:50 -070079 // zoneName / <Update>|rrLabel / UPDATE|rrType
Shock Jiang6cce21a2014-09-07 10:14:12 -070080
81 const Name& name = data.getName();
Yumin Xia6343c5b2016-10-20 15:45:50 -070082 size_t skip = calculateSkip(name, zone);
Shock Jiang6cce21a2014-09-07 10:14:12 -070083
84 if (name.size() - skip < 2)
85 return false;
86
87 result.version = name.get(-1);
88 result.rrType = name.get(-2);
89 result.rrLabel = name.getSubName(skip, std::max<size_t>(0, name.size() - skip - 2));
90
91 return true;
92}
93
94} // namespace label
95} // namespace ndns
96} // namespace ndn