You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

main.cpp 3.0KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. #include <iostream>
  2. #include <QCoreApplication>
  3. #include "dns-client/QDnsUdpClient.h"
  4. #include "dns-client/QDnsTcpClient.h"
  5. #include "dns-server/QDnsUdpServer.h"
  6. #include "dns-server/QDnsTcpServer.h"
  7. int main(int argc, char* argv[])
  8. {
  9. QCoreApplication app(argc, argv);
  10. QHostAddress bindAddress("127.0.0.1");
  11. quint16 bindPort = 5053;
  12. auto onRequest = [](QDns::Server::QDnsUdpServer::OnRequestParams params)
  13. {
  14. QDns::Base::QDnsPacket answer = params.query.makeAnswer();
  15. auto header = answer.getHeader();
  16. header.setReplyCode(QDns::Base::HeaderReplyCode::Refused);
  17. answer.setHeader(header);
  18. params.sendAnswer(QDns::Server::QDnsServer::SendAnswerParams
  19. {
  20. answer
  21. });
  22. };
  23. QDns::Server::QDnsTcpServer tcpServer;
  24. tcpServer.start(bindAddress, bindPort);
  25. QObject::connect(&tcpServer, &QDns::Server::QDnsServer::onRequest, onRequest);
  26. QDns::Server::QDnsUdpServer udpServer;
  27. udpServer.start(bindAddress, bindPort);
  28. QObject::connect(&udpServer, &QDns::Server::QDnsServer::onRequest, onRequest);
  29. // QDns::Client::QDnsUdpClient client(QHostAddress("127.0.0.1"));
  30. // QDns::Client::QDnsUdpClient client(QHostAddress("8.8.8.8"));
  31. QDns::Client::QDnsTcpClient client(QHostAddress("127.0.0.1"), 5053);
  32. // QDns::Client::QDnsTcpClient client(QHostAddress("8.8.8.8"));
  33. QList<QString> labels;
  34. labels << "www.google.fr" << "www.google.com" << "www.rthoni.com";
  35. QObject::connect(&client, &QDns::Client::QDnsClient::onAnswer, [&client, &labels](QDns::Client::QDnsClient::OnAnswerParams params)
  36. {
  37. labels.removeOne(params.query.getQueries().at(0).getName());
  38. for (const auto& answer : params.answer.getAnswerRecords())
  39. {
  40. qDebug() << "Answer:" << answer.getName();
  41. }
  42. qDebug() << "Answer:" << params.answer.getAnswerRecords().size();
  43. if (labels.empty())
  44. {
  45. client.disconnectFromHost();
  46. }
  47. });
  48. QObject::connect(&client, &QDns::Client::QDnsClient::onError, [&client, &labels](QDns::Client::QDnsClient::OnErrorParams params)
  49. {
  50. if (params.query.isValid())
  51. {
  52. labels.removeOne(params.query.getQueries().at(0).getName());
  53. }
  54. qDebug() << "Error:" << params.error;
  55. if (labels.empty())
  56. {
  57. client.disconnectFromHost();
  58. }
  59. });
  60. QObject::connect(&client, &QDns::Client::QDnsClient::onStateChanged, [&client, &labels](QDns::Client::QDnsClient::OnStateChangedParams params)
  61. {
  62. qDebug() << "State:" << params.state;
  63. if (params.state == QDns::Client::QDnsClient::ClientState::Connected)
  64. {
  65. for (const auto& label : labels)
  66. {
  67. client.query(QDns::Base::QDnsPacket::makeSimpleQuery(label, QDns::Base::RecordType::A));
  68. }
  69. }
  70. });
  71. client.connectToHost();
  72. return app.exec();
  73. }