Subversion Repositories Kolibri OS

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
1010 hidnplayr 1
;
2
; ETH.INC
3
;
4
; made by hidnplayr (hidnplayr@kolibrios.org) for KolibriOS
5
;
6
; The given code before every macro is only a simple example
7
;
8
;
9
; HISTORY
10
;
11
; v1.0: august 2006  original release
12
; v1.1: december 2006 bugfixes and improvements
13
; v1.2: february 2007 more bugfixes and improvements
14
 
15
macro mov arg1,arg2 {
16
    if arg1 eq arg2
17
    else
18
    mov arg1,arg2
19
    end if
20
}
21
 
22
TCB_LISTEN = 1
23
TCB_SYN_SENT = 2
24
TCB_SYN_RECEIVED = 3
25
TCB_ESTABLISHED = 4
26
TCB_FIN_WAIT_1 = 5
27
TCB_FIN_WAIT_2 = 6
28
TCB_CLOSE_WAIT = 7
29
TCB_CLOSING = 8
30
TCB_LAST_ASK = 9
31
TCB_TIME_WAIT = 10
32
TCB_CLOSED = 11
33
 
34
PASSIVE = 0
35
ACTIVE = 1
36
 
37
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
38
 
39
macro eth.get_IP IP {
40
    mov  ebx,1
41
    mov  eax,52
42
    mcall
43
 
44
    mov  IP ,eax
45
}
46
 
47
macro eth.get_GATEWAY GATEWAY {
48
    mov  ebx,9
49
    mov  eax,52
50
    mcall
51
    mov GATEWAY ,eax
52
}
53
 
54
macro eth.get_SUBNET SUBNET {
55
    mov  ebx,10
56
    mov  eax,52
57
    mcall
58
    mov SUBNET ,eax
59
}
60
 
61
macro eth.get_DNS DNS {
62
    mov  ebx,13
63
    mov  eax,52
64
    mcall
65
    mov  DNS ,eax
66
}
67
 
68
macro eth.set_IP IP {
69
    mov  ecx,IP
70
    mov  ebx,3
71
    mov  eax,52
72
    mcall
73
}
74
 
75
macro eth.set_GATEWAY GATEWAY {
76
    mov  ecx,GATEWAY
77
    mov  ebx,11
78
    mov  eax,52
79
    mcall
80
}
81
 
82
macro eth.set_SUBNET SUBNET {
83
    mov  ecx,SUBNET
84
    mov  ebx,12
85
    mov  eax,52
86
    mcall
87
}
88
 
89
macro eth.set_DNS DNS {
90
    mov  ecx,DNS
91
    mov  ebx,14
92
    mov  eax,52
93
    mcall
94
}
95
 
96
macro eth.set_network_drv conf {
97
    mov  ecx,conf
98
    mov  ebx,2
99
    mov  eax,52
100
    mcall
101
}
102
 
103
macro eth.open_udp local,remote,ip,socket {
104
    mov  ecx, local
105
    mov  edx, remote
106
    mov  esi, ip
107
    mov  ebx, 0
108
    mov  eax, 53
109
    mcall
110
 
111
    mov  socket,eax
112
}
113
 
114
macro eth.close_udp socket {
115
    mov  ecx, socket
116
    mov  ebx, 1
117
    mov  eax, 53
118
    mcall
119
}
120
 
121
macro eth.poll socket {
122
    mov  ecx, socket
123
    mov  ebx, 2
124
    mov  eax, 53
125
    mcall
126
}
127
 
128
macro eth.read_byte socket, result {
129
    mov  ecx, socket
130
    mov  ebx, 3
131
    mov  eax, 53
132
    mcall
133
 
134
    mov  result,bl
135
}
136
 
137
macro eth.read_packet socket, result, buffersize {
138
    mov  esi, buffersize
139
    mov  edx, result
140
    mov  ecx, socket
141
    mov  ebx, 11
142
    mov  eax, 53
143
    mcall
144
}
145
 
146
macro eth.write_udp socket,length,msg,verify {
147
    mov  ecx, socket
148
    mov  edx, length
149
    mov  esi, msg
150
    mov  ebx, 4
151
    mov  eax, 53
152
    mcall
153
 
154
    if verify eq 1
155
    call verifysend
156
    end if
157
 
158
}
159
 
160
verifysend:
161
    test eax,eax
162
    jnz  @f
163
    ret
164
@@:
165
    pusha
166
    mov  eax,5
167
    mov  ebx,100
168
    mcall
169
    popa
170
    mcall
171
ret
172
 
173
macro eth.open_tcp local,remote,ip,passive,socket {
174
 
175
    mov  ecx, local
176
    mov  edx, remote
177
    mov  esi, ip
178
    mov  edi, passive	   ; 0 = PASSIVE open
179
    mov  ebx, 5
180
    mov  eax, 53
181
    mcall
182
 
183
    mov  socket,eax
184
}
185
 
186
macro eth.socket_status socket,result {
187
    mov  ecx, socket
188
    mov  ebx, 6
189
    mov  eax, 53
190
    mcall
191
 
192
    mov  result,eax
193
}
194
 
195
macro eth.write_tcp socket,length,msg,verify {
196
    mov  ecx, socket
197
    mov  edx, length
198
    mov  esi, msg
199
    mov  ebx, 7
200
    mov  eax, 53
201
    mcall
202
 
203
    if verify eq 1
204
    call verifysend
205
    end if
206
}
207
 
208
macro eth.read_mac mac {
209
    mov  eax, 52
210
    mov  ebx, 15
211
    xor  ecx, ecx
212
    pusha
213
    mcall
214
    mov  dword[mac],eax
215
    popa
216
    add  cl, 4
217
    mcall
218
    mov  word[mac+4],ax
219
 
220
}
221
 
222
macro eth.close_tcp socket {
223
    mov  ecx, socket
224
    mov  ebx, 8
225
    mov  eax, 53
226
    mcall
227
}
228
 
229
macro eth.check_port port,result {
230
    mov  ecx, port
231
    mov  ebx, 9
232
    mov  eax, 53
233
    mcall
234
 
235
    mov  result,eax
236
}
237
 
238
macro eth.check_cable result {
239
    mov  ebx, 10
240
    mov  eax, 53
241
    mcall
242
 
243
    mov  result,eax
244
}
245
 
246
macro eth.status status {
247
    mov  ebx, 255
248
    mov  ecx, 6
249
    mov  eax, 53
250
    mcall
251
 
252
    mov  status,eax
253
}
254
 
255
macro eth.search_port port,result {
256
    mov  edx,port
257
   @@:
258
    inc  edx
259
    eth.check_port edx,eax
260
    cmp  eax,0
261
    je	 @r
262
    mov  result,edx
263
}
264
 
265
macro eth.ARP_PROBE address{
266
 
267
    mov  edx,address
268
    mov  eax,52
269
    mov  ebx,16
270
    xor  ecx,ecx
271
    mcall
272
 
273
}
274
 
275
 
276
macro eth.ARP_ANNOUNCE address{
277
 
278
    mov  edx,address
279
    mov  eax,52
280
    mov  ebx,16
281
    xor  ecx,ecx
282
    inc  ecx
283
    mcall
284
 
285
}
286
 
287
macro eth.read_data socket,dest,endptr,bufferl {
288
local .getdata,.loop,.end
289
    mov     eax, dest
290
    mov     endptr, eax
291
 
292
.getdata:
293
    cmp     endptr, bufferl
294
    jg	    .end
295
 
296
    eth.read_packet socket, endptr, 0
297
    add     endptr,eax
298
 
299
    test    eax, eax
300
    jnz     .getdata
301
 
302
    xor     edx, edx
303
.loop:
304
    eth.poll socket
305
 
306
    test    eax, eax
307
    jnz     .getdata
308
 
309
    mov     eax,5
310
    mov     ebx,1
311
    mcall
312
 
313
    inc     edx
314
    cmp     edx,30
315
    jl	    .loop
316
 
317
.end:
318
}
319
 
320
macro eth.wait_for_data socket,TIMEOUT,abort {
321
    mov   edx,TIMEOUT
322
 
323
   @@:
324
    eth.poll socket
325
 
326
    cmp   eax,0
327
    jne   @f
328
 
329
    dec   edx
330
    jz	  abort
331
 
332
    mov   eax,5 			      ; wait here for event
333
    mov   ebx,10
334
    mcall
335
 
336
    jmp   @r
337
   @@:
338
 
339
}
340
 
341
 
342
 
343
Ip2dword:
344
    push    edx
345
 
346
    ; This code validates if the query is an IP containing 4 numbers and 3 dots
347
 
348
    xor     al, al	      ; make al (dot count) zero
349
 
350
   @@:
351
    cmp     byte[edx],'0'     ; check if this byte is a number, if not jump to no_IP
352
    jl	    no_IP	      ;
353
    cmp     byte[edx],'9'     ;
354
    jg	    no_IP	      ;
355
 
356
    inc     edx 	      ; the byte was a number, so lets check the next byte
357
 
358
    cmp     byte[edx],0       ; is this byte zero? (have we reached end of query?)
359
    jz	    @f		      ; jump to next @@ then
360
    cmp     byte[edx],':'
361
    jz	    @f
362
 
363
    cmp     byte[edx],'.'     ; is this byte a dot?
364
    jne     @r		      ; if not, jump to previous @@
365
 
366
    inc     al		      ; the byte was a dot so increment al(dot count)
367
    inc     edx 	      ; next byte
368
    jmp     @r		      ; lets check for numbers again (jump to previous @@)
369
 
370
   @@:			      ; we reach this when end of query reached
371
    cmp     al,3	      ; check if there where 3 dots
372
    jnz     no_IP	      ; if not, jump to no_IP
373
 
374
    ; The following code will convert this IP into a dword and output it in eax
375
    ; If there is also a port number specified, this will be returned in ebx, otherwise ebx is -1
376
 
377
    pop     esi 	      ; edx (query address) was pushed onto stack and is now popped in esi
378
 
379
    xor     edx, edx	      ; result
380
    xor     eax, eax	      ; current character
381
    xor     ebx, ebx	      ; current byte
382
 
383
.outer_loop:
384
    shl     edx, 8
385
    add     edx, ebx
386
    xor     ebx, ebx
387
.inner_loop:
388
    lodsb
389
    test    eax, eax
390
    jz	    .finish
391
    cmp     al, '.'
392
    jz	    .outer_loop
393
    sub     eax, '0'
394
    imul    ebx, 10
395
    add     ebx, eax
396
    jmp     .inner_loop
397
.finish:
398
    shl     edx, 8
399
    add     edx, ebx
400
 
401
    bswap   edx 	      ; we want little endian order
402
 
403
    ret
404
 
405
no_IP:
406
    pop     edx
407
    xor     edx, edx
408
 
409
    ret
410