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
|
# The original resolver fails if IPv6 is being used. This patch fixes that by using a pipe to call
# dig. Only an A record is returned.
--- resolver.bi 2009-11-18 14:41:16.000000000 -0500
+++ resolver.bi.b 2017-04-09 16:17:31.782515682 -0400
@@ -7,69 +7,19 @@ REM one of the following error codes:
REM !1 - Failed to resolve host
REM
-
-PRIVATE FUNCTION IntegerToIp(RawIpAddress AS ULONGINT) AS STRING
- DIM AS ULONGINT Digit1, Digit2, Digit3, Digit4
- DIM AS STRING Result
- Digit1 = (RawIpAddress AND &b00000000000000000000000011111111)
- Digit2 = (RawIpAddress AND &b00000000000000001111111100000000) shr 8
- Digit3 = (RawIpAddress AND &b00000000111111110000000000000000) shr 16
- Digit4 = (RawIpAddress AND &b11111111000000000000000000000000) shr 24
- Result = "" & Digit1 & "." & Digit2 & "." & Digit3 & "." & Digit4
- RETURN Result
-END FUNCTION
-
-
FUNCTION Resolver(SomeDomain AS STRING) AS STRING
- REM Check the internal DNS cache...
- STATIC DnsCacheTable(1 TO 2, 1 TO 20) AS STRING ' 1=Domains 2=IPs
- STATIC DnsCacheTableTTL(1 TO 20) AS DOUBLE
- DIM AS STRING Result
- DIM AS BYTE x, y
- DIM AS INTEGER MaxTTL = 600
- DIM CurTTL AS INTEGER
- DIM AS STRING CurIP, CurDomainName
- DIM AS ULONGINT RawIpAddress
- IF LEN(SomeDomain) > 0 THEN
- FOR x = 1 TO 20
- IF LCASE(DnsCacheTable(1, x)) = LCASE(SomeDomain) AND ABS(TIMER - DnsCacheTableTTL(x)) < MaxTTL THEN
- CurDomainName = DnsCacheTable(1, x)
- CurIP = DnsCacheTable(2, x)
- CurTTL = DnsCacheTableTTL(x)
- Result = CurIP
- FOR y = x TO 19
- DnsCacheTable(1, y) = DnsCacheTable(1, y + 1)
- DnsCacheTable(2, y) = DnsCacheTable(2, y + 1)
- DnsCacheTableTTL(y) = DnsCacheTableTTL(y + 1)
- NEXT y
- FOR y = 20 TO 2 STEP -1
- DnsCacheTable(1, y) = DnsCacheTable(1, y - 1)
- DnsCacheTable(2, y) = DnsCacheTable(2, y - 1)
- DnsCacheTableTTL(y) = DnsCacheTableTTL(y - 1)
- NEXT y
- DnsCacheTable(1, 1) = CurDomainName
- DnsCacheTable(2, 1) = CurIP
- DnsCacheTableTTL(1) = CurTTL
- END IF
- NEXT x
- END IF
+ DIM AS INTEGER f = freefile( )
+ DIM AS STRING Answer
- IF LEN(Result) = 0 THEN ' Entry wasn't present in cache
- RawIpAddress = Resolve(SomeDomain)
- IF RawIpAddress = 4294967295 OR RawIpAddress = 0 THEN
- Result = "!1"
- ELSE
- CurIP = IntegerToIp(RawIpAddress)
- Result = CurIP
- FOR y = 20 TO 2 STEP -1
- DnsCacheTable(1, y) = DnsCacheTable(1, y - 1)
- DnsCacheTable(2, y) = DnsCacheTable(2, y - 1)
- DnsCacheTableTTL(y) = DnsCacheTableTTL(y - 1)
- NEXT y
- DnsCacheTable(1, 1) = LCASE(SomeDomain)
- DnsCacheTable(2, 1) = CurIP
- DnsCacheTableTTL(1) = TIMER
+ OPEN PIPE "dig A +short " & SomeDomain FOR INPUT AS #f
+ DIM AS STRING Rec
+ LINE INPUT #f, Rec
+ IF Rec = "" THEN
+ Answer = "!1"
+ ELSE
+ Answer = Rec
END IF
- END IF
- RETURN Result
+ CLOSE #f
+
+ RETURN Answer
END FUNCTION
|