-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHostnameResolver.cs
172 lines (152 loc) · 5.67 KB
/
HostnameResolver.cs
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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
using PcapDotNet.Core;
using PcapDotNet.Packets;
using PcapDotNet.Packets.Dns;
using PcapDotNet.Packets.Ethernet;
using PcapDotNet.Packets.IpV4;
using PcapDotNet.Packets.Transport;
namespace http_client
{
public class HostnameResolver
{
public enum ResolverStates
{
IP_ALREADY,
RESOLVED,
ERROR
}
private MandatoryAddresses _addresses;
private LivePacketDevice _interface;
public HostnameResolver(MandatoryAddresses addresses, LivePacketDevice @interface)
{
_addresses = addresses;
_interface = @interface;
}
public HostnameResolver(MandatoryAddresses addresses, LivePacketDevice @interface, string addr)
{
_addresses = addresses;
_interface = @interface;
_addresses.DestIP = new IpV4Address(addr);
}
public IpV4Address GetIpFromInput(string input)
{
ResolverStates tmpState;
return GetIpFromInput(input, out tmpState);
}
public IpV4Address GetIpFromInput(string input, out ResolverStates state)
{
IpV4Address DestIP;
IpV4Address.TryParse(input, out DestIP);
if (DestIP == IpV4Address.Zero)
{
try
{
DestIP = this.Resolve(input);
state = ResolverStates.RESOLVED;
}
catch (Exception)
{
DestIP = IpV4Address.Zero;
state = ResolverStates.ERROR;
}
}
else
{
state = ResolverStates.IP_ALREADY;
}
return DestIP;
}
public IpV4Address Resolve(string hostname)
{
Packet tmpPacket;
string dnsFilter = "udp and src port 53 and src host " + _addresses.DestIP.ToString() + " and dst host "+_addresses.SourceIP.ToString();
using (PacketCommunicator communicator = _interface.Open(65536, PacketDeviceOpenAttributes.Promiscuous, 100))
{
communicator.SetFilter(dnsFilter);
communicator.SendPacket(BuildDnsPacket(new DnsDomainName(hostname)));
while (true)
{
if (communicator.ReceivePacket(out tmpPacket) == PacketCommunicatorReceiveResult.Ok)
{
var answer = tmpPacket.Ethernet.IpV4.Udp.Dns.Answers[0];
string domainName = answer.DomainName.ToString();
if (domainName == hostname + '.')
{
break;
}
}
}
DnsDatagram dns = tmpPacket.Ethernet.IpV4.Udp.Dns;
foreach (var value in dns.Answers)
{
try
{
return (value.Data as DnsResourceDataIpV4)!.Data;
}
catch (Exception)
{
// IpV6
}
}
return IpV4Address.Zero;
}
}
private Packet BuildDnsPacket(DnsDomainName queryDomainName)
{
EthernetLayer ethernetLayer =
new EthernetLayer
{
Source = _addresses.SourceMAC,
Destination = _addresses.DestMAC,
EtherType = EthernetType.None, // Will be filled automatically.
};
IpV4Layer ipV4Layer =
new IpV4Layer
{
Source = _addresses.SourceIP,
CurrentDestination = _addresses.DestIP,
Fragmentation = IpV4Fragmentation.None,
HeaderChecksum = null, // Will be filled automatically.
Identification = 1235,
Options = IpV4Options.None,
Protocol = null, // Will be filled automatically.
Ttl = 128,
TypeOfService = 0,
};
UdpLayer udpLayer =
new UdpLayer
{
SourcePort = 4050,
DestinationPort = 53,
Checksum = null, // Will be filled automatically.
CalculateChecksumValue = true,
};
DnsLayer dnsLayer =
new DnsLayer
{
Id = 100,
IsResponse = false,
OpCode = DnsOpCode.Query,
IsAuthoritativeAnswer = false,
IsTruncated = false,
IsRecursionDesired = true,
IsRecursionAvailable = false,
FutureUse = false,
IsAuthenticData = false,
IsCheckingDisabled = false,
ResponseCode = DnsResponseCode.NoError,
Queries = new[]
{
new DnsQueryResourceRecord(queryDomainName,
DnsType.A,
DnsClass.Internet),
},
Answers = null,
Authorities = null,
Additionals = null,
DomainNameCompressionMode = DnsDomainNameCompressionMode.All,
};
PacketBuilder builder = new PacketBuilder(ethernetLayer, ipV4Layer, udpLayer, dnsLayer);
return builder.Build(DateTime.Now);
}
}
}