Skip to content

Commit e7234b4

Browse files
author
Artiom N.
committed
dn_comp/dn_expand example add
1 parent 1882291 commit e7234b4

File tree

3 files changed

+47
-0
lines changed

3 files changed

+47
-0
lines changed

src/book01/ch02/cpp/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,5 @@ add_subdirectory(ip_convert)
55

66
if (NOT WIN32)
77
add_subdirectory(net_db)
8+
add_subdirectory(dn_comp_expand)
89
endif()
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
cmake_minimum_required(VERSION 3.10)
2+
3+
project("${CHAPTER_NAME}dn-comp-expand" C CXX)
4+
5+
add_executable("${PROJECT_NAME}" main.cpp)
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
extern "C"
2+
{
3+
#include <arpa/nameser.h>
4+
#include <netinet/in.h>
5+
#include <resolv.h>
6+
}
7+
8+
#include <array>
9+
#include <iostream>
10+
11+
12+
int main()
13+
{
14+
std::array<unsigned char, 256> buf;
15+
unsigned char *dnptrs = nullptr;
16+
unsigned char *lastdnptr = nullptr;
17+
18+
if (auto result = dn_comp("www.google.com", buf.data(), buf.size(), &dnptrs, &lastdnptr); result != -1)
19+
{
20+
std::cout << "Len = " << result << "\n"
21+
<< std::string(buf.begin(), buf.begin() + result) << "\n"
22+
<< "DP = " << (dnptrs ? reinterpret_cast<const char *>(dnptrs) : "nullptr") << "\n"
23+
<< "LDP = " << (lastdnptr ? reinterpret_cast<const char *>(lastdnptr) : "nullptr") << std::endl;
24+
25+
std::array<char, 256> exp_buf;
26+
27+
if ((result = dn_expand(buf.data(), buf.data() + result, buf.data(), exp_buf.data(), exp_buf.size())) != -1)
28+
{
29+
std::cout << "Len = " << result << "\n"
30+
<< std::string(exp_buf.begin(), exp_buf.begin() + result) << "\n"
31+
<< std::endl;
32+
return EXIT_SUCCESS;
33+
}
34+
35+
perror("dn_expand");
36+
return EXIT_FAILURE;
37+
}
38+
perror("dn_comp");
39+
40+
return EXIT_FAILURE;
41+
}

0 commit comments

Comments
 (0)