forked from beretta42/zombie
-
Notifications
You must be signed in to change notification settings - Fork 0
/
resolv.s
213 lines (201 loc) · 3.42 KB
/
resolv.s
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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
;;;
;;;
;;; This is a simple internet name resolver
;;;
;;;
include "zombie.def"
export ans
DNSTO equ 3*CPS ; 3 second timeout
.area .data
retry rmb 1 ; send this many queries, max
ans rmb 4 ; resolved address, if any
flag rmb 1 ;
qptr rmb 2 ; ptr to query text name
.area .code
;;; convert string to ip address
;;; takes: X - str ptr
;;; return: C set on error, ip in ans
;;; fixme: this doesn't check for out of range octets
atoip ldb #3 ; how many octets are we allowed?
pshs b
leay ans,pcr
b@ clra
a@ ldb ,x+
beq out@
cmpb #'.
beq store@
subb #'0
bmi err@
cmpb #9
bhi err@
pshs b
ldb #10
mul
tfr b,a
adda ,s+
bra a@
store@ tst ,s ; too many dots? then error
beq err@
sta ,y+
bra b@
out@ sta ,y+
clra
puls b,pc
err@ coma
puls b,pc
;;; resolves name
;;; takes X = name
;;; returns X = ptr to ip
;;; returns C set on error
export resolve
resolve
stx qptr,pcr
;; first, try to directly convert to ip
bsr atoip
bcc ok@
;; not an IP so do a DNS lookup
ldb #3
stb retry,pcr
clr flag,pcr
;; make and bind socket - DNS
ldb #C_UDP ; socket is a UDP
lbsr socket
bcs bad@ ; handle running out of sockets
ldx conn,pcr ; conn is newly opened sockets
ldd dns,pcr ; set IP to system dns server
std C_DIP,x ; our DNS server from dns
ldd dns+2,pcr
std C_DIP+2,x
ldd #53 ; port 53 - dns
std C_DPORT,x
ldd #DNSTO ; timeout socket at 2 secs
std C_TIME,x
leay call,pcr
sty C_CALL,x
bsr query ; send initial query
a@ tst flag,pcr
beq a@
out@ lbsr close
ldb #1
leax ans,pcr
cmpb flag,pcr
rts
bad@ coma
rts
ok@ clra
rts
;; send query packet to server
query
lbsr getbuff
bcs err@
pshs x
leax 47,x ; leave room for lower layer
pshs x
ldd mac+2,pcr ; use our mac as a ID field
std ,x++
ldd #$0100 ; recursive search
std ,x++ ; and a bunch of other stuff
ldd #1
std ,x++ ; one question
clrb
std ,x++ ; no answers
std ,x++ ; no name servers
std ,x++ ; no auth
; append name
bsr appname
ldd #1 ; qtype A - host
std ,x++
std ,x++ ; qclass IN - internet
; finish and send
tfr x,d ; calc length
subd ,s
puls x ; get pdu back
lbsr send
puls x
lbra freebuff
err@ rts
appname
ldy qptr,pcr
a@ leau ,x+
clrb
b@ lda ,y+
beq out@
cmpa #'.
beq next@
sta ,x+
incb
bra b@
next@ stb ,u+
bra a@
out@ stb ,u+
clr ,x+
rts
call cmpb #C_CALLTO
beq to@
;; filter out bad answers
ldx pdu,pcr
ldd ,x ; is our ID?
cmpd mac+2,pcr
bne out@
ldd 2,x ; is a reponse?
bita #$80
beq out@
bitb #$f ; error code?
bne err@
ldd 6,x ; answers?
beq out@
;; ok this is our answer packet
;; skip overs questions
leau 12,x
a@ bsr skip
leau 4,u ; skip over type/class
ldd 4,x ; decrement question number
subd #1
std 4,x
bne a@
;; scan our answers for matching type
b@ bsr skip ; skip name
ldd ,u ; check type (A)
cmpd #1
bne next@
ldd 2,u ; check class (IN)
cmpd #1
bne next@
bra ans@
;; it doesn't match goto next answer
next@ leau 8,u ; skip TTL
ldd ,u++ ; get answer length
leau d,u ; skip it
ldd 6,x ; decrement answer number
subd #1
std 6,x
bne b@ ; get another
bra err@ ; all out of answers!
;; U points to first answer, get it.
ans@ leau 10,u
ldd ,u++
std ans,pcr
ldd ,u
std ans+2,pcr
inc flag,pcr
out@ lbra ip_drop
to@ dec retry,pcr
beq err@
ldy conn,pcr
ldd #DNSTO
std C_TIME,y
lbra query
err@ inc flag,pcr
inc flag,pcr
bra out@
;; skips over name
skip
ldb ,u
andb #$c0
bne ref@
ldb ,u+
beq out@
leau b,u
bra skip
ref@ leau 2,u
out@ rts