-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathDNSQuery.cpp
More file actions
77 lines (55 loc) · 2.07 KB
/
Copy pathDNSQuery.cpp
File metadata and controls
77 lines (55 loc) · 2.07 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
#include <iostream>
#include <SFNUL.hpp>
int main() {
// Create our UDP socket.
auto socket = sfn::UdpSocket::Create();
// Our soon-to-be DNS request.
std::string request;
// Transaction ID
request += { 0x13, 0x37 };
// Standard recursive query flags
request += { 0x01, 0x00 };
// Questions
request += { 0x00, 0x01 };
// RRs
request += { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
// Name part 1
request += static_cast<char>( 4 ); // Length
request += "ietf";
// Name part 2
request += static_cast<char>( 3 ); // Length
request += "org";
// Name terminator
request += static_cast<char>( 0x00 );
// Type: A Class: IN
request += { 0x00, 0x01, 0x00, 0x01 };
// Google DNS server endpoint.
sfn::Endpoint google_dns{ sfn::IpAddress{ "8.8.8.8" }, 53 };
// Bind the socket to a local endpoint so we are able to receive data.
socket->Bind( sfn::Endpoint{ sfn::IpAddress{ "0.0.0.0" }, 1337 } );
// Send the DNS request to the Google DNS server endpoint.
socket->SendTo( request.c_str(), request.size(), google_dns );
// Start a network processing thread.
sfn::Start();
while( true ) {
std::array<char, 1024> reply;
// Dequeue any data we receive from the Google DNS server.
std::size_t reply_size = socket->ReceiveFrom( reply.data(), reply.size(), google_dns );
// Display the IP address so everybody believes this works.
if( reply_size >= 4 ) {
std::cout << "Address: " << static_cast<unsigned short>( reply[reply_size - 4] & 0xff ) << "."
<< static_cast<unsigned short>( reply[reply_size - 3] & 0xff ) << "."
<< static_cast<unsigned short>( reply[reply_size - 2] & 0xff ) << "."
<< static_cast<unsigned short>( reply[reply_size - 1] & 0xff ) << "\n";
break;
}
}
// Close the socket.
socket->Close();
// Stop all network processing threads.
sfn::Stop();
return 0;
}