-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
116 lines (98 loc) · 2.81 KB
/
main.cpp
File metadata and controls
116 lines (98 loc) · 2.81 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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
#include <resolv.h>
#include <arpa/nameser.h>
#include <netinet/in.h>
#include <iostream>
#include <string>
#include <limits>
#include <arpa/inet.h>
std::string get_best_mx_record(const std::string &domain)
{
u_char response[NS_PACKETSZ];
int len = res_query(domain.c_str(), ns_c_in, ns_t_mx, response, sizeof(response));
if (len < 0)
{
perror("res_query failed");
return "";
}
ns_msg handle;
if (ns_initparse(response, len, &handle) < 0)
{
perror("ns_initparse failed");
return "";
}
int count = ns_msg_count(handle, ns_s_an);
int best_pref = std::numeric_limits<int>::max();
std::string best_mx;
for (int i = 0; i < count; ++i)
{
ns_rr rr;
if (ns_parserr(&handle, ns_s_an, i, &rr) < 0)
{
perror("ns_parserr failed");
continue;
}
const u_char *rdata = ns_rr_rdata(rr);
if (ns_rr_rdlen(rr) < 3)
continue; // MX 레코드는 최소 3바이트 이상이어야 함
int preference = (rdata[0] << 8) | rdata[1]; // MX preference 값
char exchange[NS_MAXDNAME];
if (dn_expand(response, response + len, rdata + 2, exchange, sizeof(exchange)) < 0)
{
perror("dn_expand failed");
continue;
}
if (preference < best_pref)
{
best_pref = preference;
best_mx = exchange;
}
}
std::cout << best_mx << std::endl;
len = res_query(best_mx.c_str(), ns_c_in, ns_t_a, response, sizeof(response));
if (len < 0)
{
perror("res_query failed");
return "";
}
if (ns_initparse(response, len, &handle) < 0)
{
perror("ns_initparse failed");
return "";
}
count = ns_msg_count(handle, ns_s_an);
std::cout<< count << std::endl;
for (int i = 0; i < count; ++i)
{
ns_rr rr;
if (ns_parserr(&handle, ns_s_an, i, &rr) < 0)
{
perror("ns_parserr failed");
continue;
}
const u_char *rdata = ns_rr_rdata(rr);
char ip[INET6_ADDRSTRLEN];
if (ns_rr_type(rr) == ns_t_a && ns_rr_rdlen(rr) == 4)
{
// A record (IPv4)
inet_ntop(AF_INET, rdata, ip, sizeof(ip));
return ip;
}
else if (ns_rr_type(rr) == ns_t_aaaa && ns_rr_rdlen(rr) == 16)
{
// AAAA record (IPv6)
inet_ntop(AF_INET6, rdata, ip, sizeof(ip));
return ip;
}
}
return "";
}
int main()
{
std::string domain = "naver.com";
std::string best_mx = get_best_mx_record(domain);
if (!best_mx.empty())
std::cout << "Best MX for " << domain << ": " << best_mx << std::endl;
else
std::cout << "Failed to get MX record for " << domain << std::endl;
return 0;
}