blob: 28b3ec9d5f280e0eb5a86441f4c1896918c92090 [file] [log] [blame]
shockjiang99ad3892014-08-03 14:56:13 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
3 * Copyright (c) 2014, Regents of the University of California.
shockjiang85312022014-07-27 23:22:16 -07004 *
shockjiang99ad3892014-08-03 14:56:13 -07005 * This file is part of NDNS (Named Data Networking Domain Name Service).
6 * See AUTHORS.md for complete list of NDNS authors and contributors.
shockjiang85312022014-07-27 23:22:16 -07007 *
shockjiang99ad3892014-08-03 14:56:13 -07008 * 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/>.
shockjiang85312022014-07-27 23:22:16 -070018 */
19
shockjiang85312022014-07-27 23:22:16 -070020#include "app/name-dig.hpp"
21#include "boost/program_options.hpp"
22#include "boost/filesystem.hpp"
23
Alexander Afanasyevab430ee2014-08-05 21:11:25 -070024#include "version.hpp"
25
shockjiang85312022014-07-27 23:22:16 -070026using namespace ndn;
27using namespace ndn::ndns;
28using namespace std;
29
shockjiang85312022014-07-27 23:22:16 -070030
31int main(int argc, char * argv[])
32{
33 //char *programName, char *dstLabel, char *resolverName
34 //const char *programName = argv[0];
35 std::string appName = boost::filesystem::basename(argv[0]);
36
37 string dstLabel;
38 string resolver;
39 int waitingSeconds=10;
40 string rrType = "TXT";
41 int tryMax = 1;
42 try{
43
44 namespace po = boost::program_options;
45 po::variables_map vm;
46
shockjiang85312022014-07-27 23:22:16 -070047
48 po::options_description generic("Generic Options");
49 generic.add_options()
50 ("help,h", "print help message")
51 ;
52
53
54 po::options_description config("Configuration");
55 config.add_options()
56 ("waiting,w", po::value<int>(&waitingSeconds), "set waiting seconds for every Interest. default: 10")
shockjiang99ad3892014-08-03 14:56:13 -070057 ("rrtype,t", po::value<std::string>(&rrType), "set request RR Type. default: TXT")
shockjiang85312022014-07-27 23:22:16 -070058 ("trynum,n", po::value<int>(&tryMax), "set maximal Interest Tried Number. default: 2")
59 ;
60
61
62 po::options_description hidden("Hidden Options");
63 hidden.add_options()
shockjiang99ad3892014-08-03 14:56:13 -070064 ("name", po::value<string>(&dstLabel), "name to be resolved")
shockjiang85312022014-07-27 23:22:16 -070065 ("resolver", po::value<string>(&resolver), "routable prefix of resolver")
66 ;
67
68 po::positional_options_description postion;
69 postion.add("name", 1);
70 postion.add("resolver", 1);
71
72
73
74 po::options_description cmdline_options;
75 cmdline_options.add(generic).add(config).add(hidden);
76
77 po::options_description config_file_options;
78 config_file_options.add(config).add(hidden);
79
80 po::options_description visible("Allowed options");
81 visible.add(generic).add(config);
82
83
84 po::parsed_options parsed = po::command_line_parser(argc, argv).options(cmdline_options).positional(postion).run();
85
86
87 po::store(parsed, vm);
88 po::notify(vm);
89
90 if (vm.count("help"))
91 {
shockjiang99ad3892014-08-03 14:56:13 -070092 cout<<"E.g: dig /name/to/be/resolved /routable/prefix/of/resolver"<<endl;
shockjiang85312022014-07-27 23:22:16 -070093 cout<<visible<<endl;
94 return 0;
95 }
96
shockjiang99ad3892014-08-03 14:56:13 -070097 cout<<"name="<<dstLabel<<" resolver="<<resolver<<" waiting="<<waitingSeconds<<"s RRType="<<rrType<<" tryMax="<<tryMax<<endl;
shockjiang85312022014-07-27 23:22:16 -070098 }
99 catch(const std::exception& ex)
100 {
101 cout << "Parameter Error: " << ex.what() << endl;
shockjiang99ad3892014-08-03 14:56:13 -0700102 return 0;
shockjiang85312022014-07-27 23:22:16 -0700103 }
104 catch(...)
105 {
106 cout << "Parameter Unknown error" << endl;
shockjiang99ad3892014-08-03 14:56:13 -0700107 return 0;
shockjiang85312022014-07-27 23:22:16 -0700108 }
109
110
111 NameDig dig(argv[0], argv[1]);
112
113 dig.setResolverName(Name(resolver));
114
115 dig.setInterestTriedMax(tryMax);
116
117 dig.setRrType(RR::toRRType(rrType));
118
119 dig.run();
120
121 if (dig.hasError())
122 {
123 cout<<"\n\n"<<dig.getProgramName()<<" cannot find any records for Name"
124 <<dig.getDstLabel().toUri()<<" from resolver "
125 <<resolver<<". Due to:"<<std::endl;
126 cout<<"Error: "<<dig.getErr()<<endl;
127 } else
128 {
shockjiang99ad3892014-08-03 14:56:13 -0700129 Name re = dig.getResponse().getQueryName();
shockjiang85312022014-07-27 23:22:16 -0700130 if (dig.getRrs().size() == 0)
131 {
132 cout<<"Dig found no record(s) for Name "
133 <<dig.getDstLabel().toUri()<<std::endl;
shockjiang99ad3892014-08-03 14:56:13 -0700134 cout<<"Final Response Name: "<<re.toUri()<<std::endl;
shockjiang85312022014-07-27 23:22:16 -0700135 }
136 else {
137 cout<<"Success to the dig "<<dig.getDstLabel().toUri()<<" by Resolver "
138 <<resolver<<endl;
139
140 vector<RR> rrs = dig.getRrs();
141 vector<RR>::const_iterator iter = rrs.begin();
142 while (iter != rrs.end())
143 {
144 cout<<" "<<*iter<<"\n";
145 iter ++;
146 }
shockjiang99ad3892014-08-03 14:56:13 -0700147 cout<<"Final Response Name: "<<re.toUri()<<std::endl;
shockjiang85312022014-07-27 23:22:16 -0700148 }
149 }
150
151}