2
* Copyright (C) 2005 Justin Karneges
4
* Permission is hereby granted, free of charge, to any person obtaining a
5
* copy of this software and associated documentation files (the
6
* "Software"), to deal in the Software without restriction, including
7
* without limitation the rights to use, copy, modify, merge, publish,
8
* distribute, sublicense, and/or sell copies of the Software, and to
9
* permit persons to whom the Software is furnished to do so, subject to
10
* the following conditions:
12
* The above copyright notice and this permission notice shall be included
13
* in all copies or substantial portions of the Software.
15
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
16
* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
18
* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
19
* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
20
* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
21
* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
28
QString dataToString(const QByteArray &buf)
31
for(int n = 0; n < buf.size(); ++n)
33
unsigned char c = (unsigned char)buf[n];
36
else if(c >= 0x20 && c < 0x7f)
39
out += QString().sprintf("\\x%02x", (unsigned int)c);
44
void print_record(const QJDns::Record &r)
49
printf(" A: [%s] (ttl=%d)\n", qPrintable(r.address.toString()), r.ttl);
52
printf(" AAAA: [%s] (ttl=%d)\n", qPrintable(r.address.toString()), r.ttl);
55
printf(" MX: [%s] priority=%d (ttl=%d)\n", r.name.data(), r.priority, r.ttl);
58
printf(" SRV: [%s] port=%d priority=%d weight=%d (ttl=%d)\n", r.name.data(), r.port, r.priority, r.weight, r.ttl);
61
printf(" CNAME: [%s] (ttl=%d)\n", r.name.data(), r.ttl);
64
printf(" PTR: [%s] (ttl=%d)\n", r.name.data(), r.ttl);
68
printf(" TXT: count=%d (ttl=%d)\n", r.texts.count(), r.ttl);
69
for(int n = 0; n < r.texts.count(); ++n)
70
printf(" len=%d [%s]\n", r.texts[n].size(), qPrintable(dataToString(r.texts[n])));
74
printf(" HINFO: [%s] [%s] (ttl=%d)\n", r.cpu.data(), r.os.data(), r.ttl);
77
printf(" NS: [%s] (ttl=%d)\n", r.name.data(), r.ttl);
80
printf(" (Unknown): type=%d, size=%d (ttl=%d)\n", r.type, r.rdata.size(), r.ttl);
85
class App : public QObject
89
bool opt_debug, opt_ipv6, opt_quit;
91
QString mode, type, name, ipaddr;
93
QList<QJDns::Record> pubitems;
99
connect(&jdns, SIGNAL(resultsReady(int, const QJDns::Response &)), SLOT(jdns_resultsReady(int, const QJDns::Response &)));
100
connect(&jdns, SIGNAL(published(int)), SLOT(jdns_published(int)));
101
connect(&jdns, SIGNAL(error(int, QJDns::Error)), SLOT(jdns_error(int, QJDns::Error)));
102
connect(&jdns, SIGNAL(shutdownFinished()), SLOT(jdns_shutdownFinished()));
103
connect(&jdns, SIGNAL(debugLinesReady()), SLOT(jdns_debugLinesReady()));
115
if(!jdns.init(QJDns::Unicast, opt_ipv6 ? QHostAddress::AnyIPv6 : QHostAddress::Any))
117
jdns_debugLinesReady();
118
printf("unable to bind\n");
123
QList<QJDns::NameServer> addrs;
124
for(int n = 0; n < nslist.count(); ++n)
126
QJDns::NameServer host;
127
QString str = nslist[n];
131
host.address = QHostAddress("FF02::FB");
133
host.address = QHostAddress("224.0.0.251");
138
int at = str.indexOf(';');
141
host.address = QHostAddress(str.mid(0, at));
142
host.port = str.mid(at + 1).toInt();
146
host.address = QHostAddress(str);
150
if(host.address.isNull() || host.port <= 0)
152
printf("bad nameserver: [%s]\n", qPrintable(nslist[n]));
160
addrs = QJDns::systemInfo().nameServers;
164
printf("no nameservers were detected or specified\n");
169
jdns.setNameServers(addrs);
173
if(!jdns.init(QJDns::Multicast, opt_ipv6 ? QHostAddress::AnyIPv6 : QHostAddress::Any))
175
jdns_debugLinesReady();
176
printf("unable to bind\n");
182
if(mode == "uni" || mode == "mul")
187
else if(type == "srv")
191
else if(type == "aaaa")
193
else if(type == "mx")
195
else if(type == "txt")
197
else if(type == "hinfo")
199
else if(type == "cname")
201
else if(type == "any")
206
int y = type.toInt(&ok);
211
req_id = jdns.queryStart(name.toLatin1(), x);
212
printf("[%d] Querying for [%s] type=%d ...\n", req_id, qPrintable(name), x);
216
for(int n = 0; n < pubitems.count(); ++n)
218
const QJDns::Record &rr = pubitems[n];
219
QJDns::PublishMode m = QJDns::Unique;
220
if(rr.type == QJDns::Ptr)
222
int id = jdns.publishStart(m, rr);
223
printf("[%d] Publishing [%s] type=%d ...\n", id, rr.owner.data(), rr.type);
228
QTimer::singleShot(quit_time * 1000, this, SLOT(doShutdown()));
235
void jdns_resultsReady(int id, const QJDns::Response &results)
237
printf("[%d] Results\n", id);
238
for(int n = 0; n < results.answerRecords.count(); ++n)
239
print_record(results.answerRecords[n]);
245
void jdns_published(int id)
247
printf("[%d] Published\n", id);
250
void jdns_error(int id, QJDns::Error e)
253
if(e == QJDns::ErrorGeneric)
255
else if(e == QJDns::ErrorNXDomain)
257
else if(e == QJDns::ErrorTimeout)
259
else if(e == QJDns::ErrorConflict)
261
printf("[%d] Error: %s\n", id, qPrintable(str));
265
void jdns_shutdownFinished()
270
void jdns_debugLinesReady()
272
QStringList lines = jdns.debugLines();
275
for(int n = 0; n < lines.count(); ++n)
276
printf("jdns: %s\n", qPrintable(lines[n]));
290
printf("usage: jdns (options) uni [type] [name] (nameserver(;port)|mul ...)\n");
291
printf(" jdns (options) mul [type] [name]\n");
292
printf(" jdns (options) pub [items ...]\n");
293
printf(" jdns sys\n");
295
printf("options:\n");
296
printf(" -d show debug output\n");
297
printf(" -6 use ipv6\n");
298
printf(" -q x quit x seconds after starting\n");
300
printf("uni/mul types: a aaaa ptr srv mx txt hinfo cname any\n");
301
printf("pub items: ptr:name,answer srv:name,answer,port a:name,ipaddr\n");
302
printf(" txt:name,str0,...,strn aaaa:name,ipaddr\n");
304
printf("examples:\n");
305
printf(" jdns uni a jabber.org 192.168.0.1\n");
306
printf(" jdns uni srv _xmpp-client._tcp.jabber.org 192.168.0.1;53\n");
307
printf(" jdns uni 10 user@host._presence._tcp.local mul\n");
308
printf(" jdns mul a foobar.local\n");
309
printf(" jdns mul ptr _services._dns-sd._udp.local\n");
310
printf(" jdns pub a:mybox.local.,192.168.0.55\n");
314
int main(int argc, char **argv)
316
QCoreApplication app(argc, argv);
326
for(int n = 1; n < argc; ++n)
327
args += QString(argv[n]);
329
bool opt_debug = false;
330
bool opt_ipv6 = false;
331
bool opt_quit = false;
333
QString mode, type, name, ipaddr;
335
QList<QJDns::Record> pubitems;
338
for(int n = 0; n < args.count(); ++n)
340
if(args[n].left(1) == "-")
344
else if(args[n] == "-6")
346
else if(args[n] == "-q")
348
if(n + 1 >= args.count())
350
printf("need to specify number of seconds\n");
355
int x = args[n + 1].toInt();
362
args.removeAt(n + 1);
366
printf("bad option\n");
371
--n; // adjust position
376
if(mode == "uni" || mode == "mul")
380
printf("not enough args\n");
388
for(int n = 3; n < args.count(); ++n)
389
nslist += QString(args[n]);
392
else if(mode == "pub")
396
printf("not enough args\n");
400
for(int n = 1; n < args.count(); ++n)
402
QString arg = args[n];
403
int at = arg.indexOf(':');
406
printf("missing colon\n");
410
QString type = arg.mid(0, at).toLower();
411
QString val = arg.mid(at + 1);
414
QStringList list = val.split(',');
415
if(list.count() != 2)
417
printf("bad format for A type\n");
421
QHostAddress host(list[1]);
422
if(host.isNull() || host.protocol() != QAbstractSocket::IPv4Protocol)
424
printf("bad format for A type IP address\n");
430
rec.owner = list[0].toLatin1();
433
rec.haveKnown = true;
437
else if(type == "aaaa")
439
QStringList list = val.split(',');
440
if(list.count() != 2)
442
printf("bad format for AAAA type\n");
446
QHostAddress host(list[1]);
447
if(host.isNull() || host.protocol() != QAbstractSocket::IPv6Protocol)
449
printf("bad format for AAAA type IP address\n");
455
rec.owner = list[0].toLatin1();
456
rec.type = QJDns::Aaaa;
458
rec.haveKnown = true;
462
else if(type == "srv")
464
QStringList list = val.split(',');
465
if(list.count() != 3)
467
printf("bad format for SRV type\n");
473
rec.owner = list[0].toLatin1();
474
rec.type = QJDns::Srv;
476
rec.haveKnown = true;
477
rec.name = list[1].toLatin1();
480
rec.port = list[2].toInt();
483
else if(type == "ptr")
485
QStringList list = val.split(',');
486
if(list.count() != 2)
488
printf("bad format for PTR type\n");
494
rec.owner = list[0].toLatin1();
495
rec.type = QJDns::Ptr;
497
rec.haveKnown = true;
498
rec.name = list[1].toLatin1();
501
else if(type == "txt")
503
QStringList list = val.split(',');
504
QList<QByteArray> texts;
505
for(int n = 1; n < list.count(); ++n)
506
texts += list[n].toLatin1();
509
rec.owner = list[0].toLatin1();
510
rec.type = QJDns::Txt;
512
rec.haveKnown = true;
518
printf("bad record type [%s]\n", qPrintable(type));
524
else if(mode == "sys")
526
QJDns::SystemInfo info = QJDns::systemInfo();
528
printf("DNS System Information\n");
529
printf(" Name Servers:\n");
530
if(!info.nameServers.isEmpty())
532
for(int n = 0; n < info.nameServers.count(); ++n)
533
printf(" %s\n", qPrintable(info.nameServers[n].address.toString()));
538
printf(" Domains:\n");
539
if(!info.domains.isEmpty())
541
for(int n = 0; n < info.domains.count(); ++n)
542
printf(" [%s]\n", info.domains[n].data());
548
if(!info.hosts.isEmpty())
550
for(int n = 0; n < info.hosts.count(); ++n)
552
const QJDns::DnsHost &h = info.hosts[n];
553
printf(" [%s] -> %s\n", h.name.data(), qPrintable(h.address.toString()));
560
printf("Primary IPv4 Multicast Address: ");
561
addr = QJDns::detectPrimaryMulticast(QHostAddress::Any);
563
printf("%s\n", qPrintable(addr.toString()));
566
printf("Primary IPv6 Multicast Address: ");
567
addr = QJDns::detectPrimaryMulticast(QHostAddress::AnyIPv6);
569
printf("%s\n", qPrintable(addr.toString()));
582
a.opt_debug = opt_debug;
583
a.opt_ipv6 = opt_ipv6;
584
a.opt_quit = opt_quit;
585
a.quit_time = quit_time;
587
a.type = type.toLower();
591
a.pubitems = pubitems;
592
QObject::connect(&a, SIGNAL(quit()), &app, SLOT(quit()));
593
QTimer::singleShot(0, &a, SLOT(start()));