6e6acff0fdf1865903370d1df911fe0461f2fb53
[mgsmtp.git] / DNSResolve.pas
1 {
2 Copyright (C) 2010 MegaBrutal
3
4 This unit is free software: you can redistribute it and/or modify
5 it under the terms of the GNU Lesser General Public License as published by
6 the Free Software Foundation, either version 3 of the License, or
7 (at your option) any later version.
8
9 This unit is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU Lesser General Public License for more details.
13
14 You should have received a copy of the GNU Lesser General Public License
15 along with this program. If not, see <http://www.gnu.org/licenses/>.
16 }
17
18
19 {$MODE FPC}
20
21 unit DNSResolve;
22
23 interface
24 uses WinSock, Sockets;
25
26
27 function ResolveHost(HostName: ansistring): in_addr;
28 function ResolveIP(IP: in_addr): ansistring;
29
30
31 implementation
32
33
34 function ResolveHost(HostName: ansistring): in_addr;
35 var
36 HostEnt: PHostEnt;
37 begin
38 HostEnt:= gethostbyname(PChar(HostName));
39 if HostEnt <> nil then
40 ResolveHost.s_addr:= PLongWord(HostEnt^.h_addr_list[0])^
41 else
42 ResolveHost.s_addr:= 0;
43 end;
44
45 function ResolveIP(IP: in_addr): ansistring;
46 var
47 HostEnt: PHostEnt;
48 begin
49 HostEnt:= gethostbyaddr(@IP, 4, AF_INET);
50 if HostEnt <> nil then
51 ResolveIP:= HostEnt^.h_name
52 else
53 ResolveIP:= NetAddrToStr(IP);
54 end;
55
56
57
58 end.