Rev 4804 | Rev 5391 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
3545 | hidnplayr | 1 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; |
2 | ;; ;; |
||
4804 | hidnplayr | 3 | ;; Copyright (C) KolibriOS team 2010-2014. All rights reserved. ;; |
3545 | hidnplayr | 4 | ;; Distributed under terms of the GNU General Public License ;; |
5 | ;; ;; |
||
6 | ;; zeroconfig.asm - Zeroconfig service for KolibriOS ;; |
||
7 | ;; ;; |
||
8 | ;; Written by hidnplayr@kolibrios.org ;; |
||
9 | ;; Some code contributed by Derpenguin ;; |
||
10 | ;; ;; |
||
11 | ;; DHCP code is based on that by Mike Hibbet ;; |
||
3618 | hidnplayr | 12 | ;; (DHCP client for menuetos) ;; |
3545 | hidnplayr | 13 | ;; ;; |
14 | ;; GNU GENERAL PUBLIC LICENSE ;; |
||
15 | ;; Version 2, June 1991 ;; |
||
16 | ;; ;; |
||
17 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; |
||
18 | |||
19 | format binary as "" |
||
20 | |||
21 | ; CONFIGURATION |
||
22 | |||
3632 | hidnplayr | 23 | TIMEOUT = 3 ; in seconds |
3545 | hidnplayr | 24 | BUFFER = 1024 ; in bytes |
3632 | hidnplayr | 25 | DHCP_TRIES = 3 ; number of times to try contacting DHCP server |
3545 | hidnplayr | 26 | __DEBUG__ = 1 ; enable/disable |
3682 | hidnplayr | 27 | __DEBUG_LEVEL__ = 2 ; 1 = all, 2 = errors |
3545 | hidnplayr | 28 | |
29 | ; CONFIGURATION FOR LINK-LOCAL |
||
30 | |||
31 | PROBE_WAIT = 1 ; second (initial random delay) |
||
32 | PROBE_MIN = 1 ; second (minimum delay till repeated probe) |
||
33 | PROBE_MAX = 2 ; seconds (maximum delay till repeated probe) |
||
34 | PROBE_NUM = 3 ; (number of probe packets) |
||
35 | |||
36 | ANNOUNCE_NUM = 2 ; (number of announcement packets) |
||
37 | ANNOUNCE_INTERVAL = 2 ; seconds (time between announcement packets) |
||
38 | ANNOUNCE_WAIT = 2 ; seconds (delay before announcing) |
||
39 | |||
40 | MAX_CONFLICTS = 10 ; (max conflicts before rate limiting) |
||
41 | |||
42 | RATE_LIMIT_INTERVAL = 60 ; seconds (delay between successive attempts) |
||
43 | |||
44 | DEFEND_INTERVAL = 10 ; seconds (min. wait between defensive ARPs) |
||
45 | |||
3618 | hidnplayr | 46 | use32 |
47 | org 0x0 |
||
3545 | hidnplayr | 48 | |
3618 | hidnplayr | 49 | db 'MENUET01' ; 8 byte id |
50 | dd 0x01 ; header version |
||
51 | dd START ; start of code |
||
52 | dd IM_END ; size of image |
||
53 | dd (I_END+0x100) ; memory for app |
||
54 | dd (I_END+0x100) ; esp |
||
55 | dd 0, 0 ; I_Param, I_Path |
||
56 | |||
57 | |||
58 | include '../../proc32.inc' |
||
59 | include '../../macros.inc' |
||
60 | include '../../debug-fdo.inc' |
||
61 | include '../../network.inc' |
||
3545 | hidnplayr | 62 | include 'dhcp.inc' |
3618 | hidnplayr | 63 | include '../../dll.inc' |
3545 | hidnplayr | 64 | |
65 | |||
66 | Ip2dword: |
||
67 | push edx |
||
68 | |||
69 | ; This code validates if the query is an IP containing 4 numbers and 3 dots |
||
70 | |||
71 | xor al, al ; make al (dot count) zero |
||
72 | |||
73 | @@: |
||
74 | cmp byte[edx],'0' ; check if this byte is a number, if not jump to no_IP |
||
75 | jl no_IP ; |
||
76 | cmp byte[edx],'9' ; |
||
77 | jg no_IP ; |
||
78 | |||
79 | inc edx ; the byte was a number, so lets check the next byte |
||
80 | |||
81 | cmp byte[edx],0 ; is this byte zero? (have we reached end of query?) |
||
82 | jz @f ; jump to next @@ then |
||
83 | cmp byte[edx],':' |
||
84 | jz @f |
||
85 | |||
86 | cmp byte[edx],'.' ; is this byte a dot? |
||
87 | jne @r ; if not, jump to previous @@ |
||
88 | |||
89 | inc al ; the byte was a dot so increment al(dot count) |
||
90 | inc edx ; next byte |
||
91 | jmp @r ; lets check for numbers again (jump to previous @@) |
||
92 | |||
93 | @@: ; we reach this when end of query reached |
||
94 | cmp al,3 ; check if there where 3 dots |
||
95 | jnz no_IP ; if not, jump to no_IP |
||
96 | |||
97 | ; The following code will convert this IP into a dword and output it in eax |
||
98 | ; If there is also a port number specified, this will be returned in ebx, otherwise ebx is -1 |
||
99 | |||
100 | pop esi ; edx (query address) was pushed onto stack and is now popped in esi |
||
101 | |||
102 | xor edx, edx ; result |
||
103 | xor eax, eax ; current character |
||
104 | xor ebx, ebx ; current byte |
||
105 | |||
106 | .outer_loop: |
||
107 | shl edx, 8 |
||
108 | add edx, ebx |
||
109 | xor ebx, ebx |
||
110 | .inner_loop: |
||
111 | lodsb |
||
112 | test eax, eax |
||
113 | jz .finish |
||
114 | cmp al, '.' |
||
115 | jz .outer_loop |
||
116 | sub eax, '0' |
||
117 | imul ebx, 10 |
||
118 | add ebx, eax |
||
119 | jmp .inner_loop |
||
120 | .finish: |
||
121 | shl edx, 8 |
||
122 | add edx, ebx |
||
123 | |||
124 | bswap edx ; we want little endian order |
||
125 | |||
126 | ret |
||
127 | |||
128 | no_IP: |
||
129 | pop edx |
||
130 | xor edx, edx |
||
131 | |||
132 | ret |
||
133 | |||
134 | |||
135 | |||
136 | |||
137 | |||
138 | |||
139 | START: |
||
140 | mcall 40, EVM_STACK2 |
||
141 | |||
4804 | hidnplayr | 142 | DEBUGF 2,"Zero-config service loaded\n" |
3545 | hidnplayr | 143 | |
144 | .wait: |
||
3601 | hidnplayr | 145 | mov ebx, API_ETH + 0 |
146 | mov bh, [device] |
||
147 | mcall 76 ; get MAC of ethernet interface 1 |
||
3545 | hidnplayr | 148 | cmp eax, -1 |
149 | jne .start |
||
150 | |||
151 | mcall 10 |
||
152 | jmp .wait |
||
153 | |||
154 | .start: |
||
155 | mov word[MAC], bx |
||
156 | mov dword[MAC+2], eax |
||
4804 | hidnplayr | 157 | DEBUGF 1,"MAC: %x-%x-%x-%x-%x-%x\n", [MAC+0]:2, [MAC+1]:2, [MAC+2]:2, [MAC+3]:2, [MAC+4]:2, [MAC+5]:2 |
3545 | hidnplayr | 158 | |
159 | mcall 40, EVM_STACK |
||
160 | |||
161 | mcall 68, 11 |
||
162 | |||
163 | stdcall dll.Load,@IMPORT |
||
164 | or eax, eax |
||
165 | jnz try_dhcp |
||
166 | |||
167 | invoke ini.get_str, path, str_ipconfig, str_type, inibuf, 16, 0 |
||
168 | |||
169 | cmp dword[inibuf], 'stat' |
||
170 | jne try_dhcp |
||
171 | |||
172 | invoke ini.get_str, path, str_ipconfig, str_ip, inibuf, 16, 0 |
||
173 | mov edx, inibuf |
||
174 | call Ip2dword |
||
3844 | hidnplayr | 175 | mov ecx, edx |
176 | mov ebx, API_IPv4 + 3 ; set IP |
||
177 | mov bh, [device] |
||
178 | mcall 76 |
||
3545 | hidnplayr | 179 | |
180 | invoke ini.get_str, path, str_ipconfig, str_gateway, inibuf, 16, 0 |
||
181 | mov edx, inibuf |
||
182 | call Ip2dword |
||
3844 | hidnplayr | 183 | mov ecx, edx |
184 | mov ebx, API_IPv4 + 9 ; set gateway |
||
185 | mov bh, [device] |
||
186 | mcall 76 |
||
3545 | hidnplayr | 187 | |
188 | invoke ini.get_str, path, str_ipconfig, str_dns, inibuf, 16, 0 |
||
189 | mov edx, inibuf |
||
190 | call Ip2dword |
||
3844 | hidnplayr | 191 | mov ecx, edx |
192 | mov ebx, API_IPv4 + 5 ; set DNS |
||
193 | mov bh, [device] |
||
194 | mcall 76 |
||
3545 | hidnplayr | 195 | |
196 | invoke ini.get_str, path, str_ipconfig, str_subnet, inibuf, 16, 0 |
||
197 | mov edx, inibuf |
||
198 | call Ip2dword |
||
3844 | hidnplayr | 199 | mov ecx, edx |
200 | mov ebx, API_IPv4 + 7 ; set subnet |
||
201 | mov bh, [device] |
||
202 | mcall 76 |
||
3545 | hidnplayr | 203 | |
204 | |||
205 | mcall -1 |
||
206 | |||
207 | |||
208 | try_dhcp: |
||
209 | |||
4804 | hidnplayr | 210 | DEBUGF 2,"Trying to contact DHCP server\n" |
3545 | hidnplayr | 211 | |
212 | mcall 75, 0, AF_INET4, SOCK_DGRAM, 0 ; open socket (parameters: domain, type, reserved) |
||
213 | cmp eax, -1 |
||
214 | je error |
||
215 | mov [socketNum], eax |
||
216 | |||
4804 | hidnplayr | 217 | DEBUGF 1,"Socket %x opened\n", eax |
3545 | hidnplayr | 218 | |
219 | mcall 75, 2, [socketNum], sockaddr1, 18 ; bind socket to local port 68 |
||
220 | cmp eax, -1 |
||
221 | je error |
||
222 | |||
4804 | hidnplayr | 223 | DEBUGF 1,"Socket Bound to local port 68\n" |
3545 | hidnplayr | 224 | |
225 | mcall 75, 4, [socketNum], sockaddr2, 18 ; connect to 255.255.255.255 on port 67 |
||
226 | cmp eax, -1 |
||
227 | je error |
||
228 | |||
4804 | hidnplayr | 229 | DEBUGF 1,"Connected to 255.255.255.255 on port 67\n" |
3545 | hidnplayr | 230 | |
231 | mov [dhcpMsgType], 0x01 ; DHCP discover |
||
232 | mov [dhcpLease], esi ; esi is still -1 (-1 = forever) |
||
233 | |||
234 | mcall 26, 9 ; Get system time |
||
235 | imul eax, 100 |
||
236 | mov [currTime], eax |
||
237 | |||
238 | build_request: ; Creates a DHCP request packet. |
||
239 | |||
3636 | hidnplayr | 240 | mov [tries], DHCP_TRIES |
241 | |||
4804 | hidnplayr | 242 | DEBUGF 1,"Building request\n" |
3545 | hidnplayr | 243 | |
244 | stdcall mem.Alloc, BUFFER |
||
245 | mov [dhcpMsg], eax |
||
246 | test eax, eax |
||
247 | jz dhcp_error |
||
248 | |||
249 | ;;; todo: skip this bullcrap |
||
250 | |||
251 | mov edi, eax |
||
252 | mov ecx, BUFFER |
||
253 | xor eax, eax |
||
254 | rep stosb |
||
255 | |||
256 | ;; todo: put this in a buffer instead of writing bytes and words! |
||
257 | |||
258 | mov edx, [dhcpMsg] |
||
259 | |||
260 | ; Boot protocol legacy |
||
261 | mov [edx], byte 0x01 ; Boot request |
||
262 | mov [edx+1], byte 0x01 ; Ethernet |
||
263 | mov [edx+2], byte 0x06 ; Ethernet h/w len |
||
264 | mov [edx+4], dword 0x11223344 ; xid ;;;;;;; |
||
265 | mov eax, [currTime] |
||
266 | mov [edx+8], eax ; secs, our uptime |
||
267 | mov [edx+10], byte 0x80 ; broadcast flag set |
||
268 | mov eax, dword [MAC] ; first 4 bytes of MAC |
||
269 | mov [edx+28],dword eax |
||
270 | mov ax, word [MAC+4] ; last 2 bytes of MAC |
||
271 | mov [edx+32],word ax |
||
272 | |||
273 | ; DHCP extension |
||
274 | mov [edx+236], dword 0x63538263 ; magic cookie |
||
275 | mov [edx+240], word 0x0135 ; option DHCP msg type |
||
276 | mov al, [dhcpMsgType] |
||
277 | mov [edx+240+2], al |
||
278 | mov [edx+240+3], word 0x0433 ; option Lease time = infinity |
||
279 | mov eax, [dhcpLease] |
||
280 | mov [edx+240+5], eax |
||
281 | mov [edx+240+9], word 0x0432 ; option requested IP address |
||
282 | mov eax, [dhcp.ip] |
||
283 | mov [edx+240+11], eax |
||
284 | mov [edx+240+15], word 0x0437 ; option request list |
||
285 | mov [edx+240+17], dword 0x0f060301 |
||
286 | |||
3735 | hidnplayr | 287 | cmp [dhcpMsgType], 0x01 ; Check which msg we are sending |
3545 | hidnplayr | 288 | jne request_options |
289 | |||
3636 | hidnplayr | 290 | mov [edx+240+21], byte 0xff ; end of options marker |
3545 | hidnplayr | 291 | |
3735 | hidnplayr | 292 | mov [dhcpMsgLen], 262 ; length |
3545 | hidnplayr | 293 | jmp send_dhcpmsg |
294 | |||
295 | request_options: |
||
296 | mov [edx+240+21], word 0x0436 ; server IP |
||
297 | mov eax, [dhcpServerIP] |
||
298 | mov [edx+240+23], eax |
||
299 | |||
300 | mov [edx+240+27], byte 0xff ; end of options marker |
||
301 | |||
3735 | hidnplayr | 302 | mov [dhcpMsgLen], 268 ; length |
3545 | hidnplayr | 303 | |
304 | send_dhcpmsg: |
||
3636 | hidnplayr | 305 | DEBUGF 1,"Sending DHCP discover/request\n" |
3545 | hidnplayr | 306 | mcall 75, 6, [socketNum], [dhcpMsg], [dhcpMsgLen] ; write to socket ( send broadcast request ) |
4805 | hidnplayr | 307 | mcall 26, 9 |
308 | add eax, TIMEOUT*100 |
||
309 | mov [timeout], eax |
||
3735 | hidnplayr | 310 | .wait: |
4805 | hidnplayr | 311 | mcall 23, TIMEOUT ; wait for data |
3545 | hidnplayr | 312 | |
4805 | hidnplayr | 313 | read_data: ; we have data - this will be the response |
3704 | hidnplayr | 314 | mcall 75, 7, [socketNum], [dhcpMsg], BUFFER, MSG_DONTWAIT ; read data from socket |
3632 | hidnplayr | 315 | cmp eax, -1 |
316 | jne @f |
||
4805 | hidnplayr | 317 | |
318 | mcall 26, 9 |
||
319 | cmp eax, [timeout] |
||
320 | jb send_dhcpmsg.wait |
||
321 | |||
4804 | hidnplayr | 322 | DEBUGF 2,"No answer from DHCP server\n" |
3632 | hidnplayr | 323 | dec [tries] |
324 | jnz send_dhcpmsg ; try again |
||
325 | jmp dhcp_error ; fail |
||
3545 | hidnplayr | 326 | |
3632 | hidnplayr | 327 | @@: |
4804 | hidnplayr | 328 | DEBUGF 1,"%d bytes received\n", eax |
3545 | hidnplayr | 329 | mov [dhcpMsgLen], eax |
330 | |||
331 | ; depending on which msg we sent, handle the response |
||
332 | ; accordingly. |
||
333 | ; If the response is to a dhcp discover, then: |
||
334 | ; 1) If response is DHCP OFFER then |
||
335 | ; 1.1) record server IP, lease time & IP address. |
||
336 | ; 1.2) send a request packet |
||
337 | ; If the response is to a dhcp request, then: |
||
338 | ; 1) If the response is DHCP ACK then |
||
339 | ; 1.1) extract the DNS & subnet fields. Set them in the stack |
||
340 | |||
341 | cmp [dhcpMsgType], 0x01 ; did we send a discover? |
||
342 | je discover |
||
343 | |||
344 | cmp [dhcpMsgType], 0x03 ; did we send a request? |
||
345 | je request |
||
346 | |||
347 | call dhcp_end ; we should never reach here ;) |
||
348 | jmp exit |
||
349 | |||
350 | discover: |
||
351 | call parse_response |
||
352 | |||
3735 | hidnplayr | 353 | cmp [dhcpMsgType2], 0x02 ; Was the response an offer? |
3545 | hidnplayr | 354 | je send_request |
355 | |||
356 | call dhcp_end |
||
357 | jmp link_local |
||
358 | |||
359 | send_request: |
||
3636 | hidnplayr | 360 | DEBUGF 1, "Got offer, making request\n" |
3545 | hidnplayr | 361 | mov [dhcpMsgType], 0x03 ; make it a request |
362 | jmp build_request |
||
363 | |||
364 | request: |
||
365 | call parse_response |
||
366 | |||
3735 | hidnplayr | 367 | cmp [dhcpMsgType2], 0x05 ; Was the response an ACK? It should be |
3545 | hidnplayr | 368 | jne read_data ; NO - read next packets |
369 | |||
3682 | hidnplayr | 370 | DEBUGF 2, "Setting IP using DHCP\n" |
3636 | hidnplayr | 371 | |
4013 | hidnplayr | 372 | mov [notify_struct.msg], str_connected |
373 | mcall 70, notify_struct |
||
3545 | hidnplayr | 374 | call dhcp_end |
375 | |||
3601 | hidnplayr | 376 | mov ebx, API_IPv4 + 3 |
377 | mov bh, [device] |
||
378 | mcall 76, , [dhcp.ip] ; ip |
||
379 | mov bl, 5 |
||
380 | mcall 76, , [dhcp.dns] ; dns |
||
381 | mov bl, 7 |
||
382 | mcall 76, , [dhcp.subnet] ; subnet |
||
383 | mov bl, 9 |
||
384 | mcall 76, , [dhcp.gateway] ; gateway |
||
3545 | hidnplayr | 385 | |
386 | jmp exit |
||
387 | |||
388 | dhcp_end: |
||
389 | mcall close, [socketNum] |
||
390 | stdcall mem.Free, [dhcpMsg] |
||
391 | |||
392 | ret |
||
393 | |||
394 | ;*************************************************************************** |
||
395 | ; Function |
||
396 | ; parseResponse |
||
397 | ; |
||
398 | ; Description |
||
399 | ; extracts the fields ( client IP address and options ) from |
||
400 | ; a DHCP response |
||
401 | ; The values go into |
||
402 | ; dhcpMsgType,dhcpLease,dhcpClientIP,dhcpServerIP, |
||
403 | ; dhcpDNSIP, dhcpSubnet |
||
404 | ; The message is stored in dhcpMsg |
||
405 | ; |
||
406 | ;*************************************************************************** |
||
407 | parse_response: |
||
408 | |||
409 | DEBUGF 1,"Data received, parsing response\n" |
||
410 | mov edx, [dhcpMsg] |
||
3735 | hidnplayr | 411 | mov [dhcpMsgType2], 0 |
3545 | hidnplayr | 412 | |
413 | push dword [edx+16] |
||
414 | pop [dhcp.ip] |
||
415 | DEBUGF 1,"Client: %u.%u.%u.%u\n", [edx+16]:1, [edx+17]:1, [edx+18]:1, [edx+19]:1 |
||
416 | |||
417 | ; TODO: check if there really are options |
||
418 | |||
419 | mov al, 240 ; Point to first option |
||
420 | movzx ecx, al |
||
421 | |||
422 | .next_option: |
||
423 | add edx, ecx |
||
424 | |||
425 | mov al, [edx] ; get message identifier |
||
426 | |||
427 | cmp al, 0xff ; End of options? |
||
428 | je .done |
||
429 | |||
430 | cmp al, 0 |
||
431 | je .pad |
||
432 | |||
433 | ; TODO: check if we still are inside the buffer |
||
434 | |||
435 | inc edx |
||
436 | movzx ecx, byte [edx] ; get data length |
||
437 | inc edx ; point to data |
||
438 | |||
439 | cmp al, dhcp_msg_type ; Msg type is a single byte option |
||
440 | je .msgtype |
||
441 | |||
442 | cmp al, dhcp_dhcp_server_id |
||
443 | je .server |
||
444 | |||
445 | cmp al, dhcp_address_time |
||
446 | je .lease |
||
447 | |||
448 | cmp al, dhcp_subnet_mask |
||
449 | je .subnet |
||
450 | |||
451 | cmp al, dhcp_router |
||
452 | je .router |
||
453 | |||
454 | cmp al, dhcp_domain_server |
||
455 | je .dns |
||
456 | |||
457 | DEBUGF 1,"Unsupported DHCP option: %u\n", al |
||
458 | |||
459 | jmp .next_option |
||
460 | |||
461 | .pad: |
||
462 | xor ecx, ecx |
||
463 | inc ecx |
||
464 | jmp .next_option |
||
465 | |||
466 | .msgtype: |
||
467 | mov al, [edx] |
||
3735 | hidnplayr | 468 | mov [dhcpMsgType2], al |
3545 | hidnplayr | 469 | |
470 | DEBUGF 1,"DHCP Msg type: %u\n", al |
||
471 | jmp .next_option ; Get next option |
||
472 | |||
473 | .server: |
||
474 | mov eax, [edx] |
||
475 | mov [dhcpServerIP], eax |
||
476 | DEBUGF 1,"Server: %u.%u.%u.%u\n",[edx]:1,[edx+1]:1,[edx+2]:1,[edx+3]:1 |
||
477 | jmp .next_option |
||
478 | |||
479 | .lease: |
||
480 | pusha |
||
481 | mov eax,[edx] |
||
482 | bswap eax |
||
483 | mov [dhcpLease],eax |
||
484 | DEBUGF 1,"lease: %d\n",eax |
||
485 | popa |
||
486 | jmp .next_option |
||
487 | |||
488 | .subnet: |
||
489 | push dword [edx] |
||
490 | pop [dhcp.subnet] |
||
491 | DEBUGF 1,"Subnet: %u.%u.%u.%u\n",[edx]:1,[edx+1]:1,[edx+2]:1,[edx+3]:1 |
||
492 | jmp .next_option |
||
493 | |||
494 | .router: |
||
495 | push dword [edx] |
||
496 | pop [dhcp.gateway] |
||
497 | DEBUGF 1,"Gateway: %u.%u.%u.%u\n",[edx]:1,[edx+1]:1,[edx+2]:1,[edx+3]:1 |
||
498 | jmp .next_option |
||
499 | |||
500 | .dns: |
||
501 | push dword [edx] |
||
502 | pop [dhcp.dns] |
||
503 | DEBUGF 1,"DNS: %u.%u.%u.%u\n",[edx]:1,[edx+1]:1,[edx+2]:1,[edx+3]:1 |
||
504 | jmp .next_option |
||
505 | |||
506 | .done: |
||
507 | ret |
||
508 | |||
509 | |||
510 | |||
511 | dhcp_error: |
||
512 | call dhcp_end |
||
513 | |||
514 | link_local: |
||
515 | call random |
||
516 | mov cx, ax |
||
517 | shl ecx, 16 |
||
518 | mov cx, 0xfea9 ; IP 169.254.0.0 link local net, see RFC3927 |
||
3601 | hidnplayr | 519 | mov ebx, API_IPv4 + 3 |
520 | mov bh, [device] |
||
521 | mcall 76, , ecx ; mask is 255.255.0.0 |
||
3682 | hidnplayr | 522 | DEBUGF 2,"Link Local IP assigned: 169.254.%u.%u\n", [generator+0]:1, [generator+1]:1 |
3601 | hidnplayr | 523 | mov bl, 7 |
524 | mcall 76, , 0xffff |
||
525 | mov bl, 9 |
||
526 | mcall 76, , 0x0 |
||
527 | mov bl, 5 |
||
528 | mcall 76, , 0x0 |
||
3545 | hidnplayr | 529 | |
530 | mcall 5, PROBE_WAIT*100 |
||
531 | |||
532 | xor esi, esi |
||
533 | probe_loop: |
||
534 | call random ; create a pseudo random number in eax (seeded by MAC) |
||
535 | |||
536 | cmp al, PROBE_MIN*100 ; check if al is bigger then PROBE_MIN |
||
537 | jae @f ; all ok |
||
538 | add al, (PROBE_MAX-PROBE_MIN)*100 ; al is too small |
||
539 | @@: |
||
540 | |||
541 | cmp al, PROBE_MAX*100 |
||
542 | jbe @f |
||
543 | sub al, (PROBE_MAX-PROBE_MIN)*100 |
||
544 | @@: |
||
545 | |||
546 | movzx ebx,al |
||
547 | DEBUGF 1,"Waiting %u0ms\n",ebx |
||
548 | mcall 5 |
||
549 | |||
550 | DEBUGF 1,"Sending Probe\n" |
||
3601 | hidnplayr | 551 | mov ebx, API_ARP + 6 |
552 | mov bh, [device] |
||
553 | mcall 76 |
||
3545 | hidnplayr | 554 | inc esi |
555 | |||
556 | cmp esi, PROBE_NUM |
||
557 | jb probe_loop |
||
558 | |||
559 | ; now we wait further ANNOUNCE_WAIT seconds and send ANNOUNCE_NUM ARP announces. If any other host has assingned |
||
560 | ; IP within this time, we should create another adress, that have to be done later |
||
561 | |||
562 | DEBUGF 1,"Waiting %us\n", ANNOUNCE_WAIT |
||
563 | mcall 5, ANNOUNCE_WAIT*100 |
||
564 | xor esi, esi |
||
565 | announce_loop: |
||
566 | |||
567 | DEBUGF 1,"Sending Announce\n" |
||
3601 | hidnplayr | 568 | mov ebx, API_ARP + 6 |
569 | mov bh, [device] |
||
570 | mcall 76 |
||
3545 | hidnplayr | 571 | |
572 | inc esi |
||
573 | cmp esi,ANNOUNCE_NUM |
||
574 | je @f |
||
575 | |||
576 | DEBUGF 1,"Waiting %us\n", ANNOUNCE_INTERVAL |
||
577 | mcall 5, ANNOUNCE_INTERVAL*100 |
||
578 | jmp announce_loop |
||
579 | @@: |
||
4805 | hidnplayr | 580 | jmp exit |
3545 | hidnplayr | 581 | |
582 | |||
583 | error: |
||
3682 | hidnplayr | 584 | DEBUGF 2,"Socket error\n" |
3545 | hidnplayr | 585 | exit: ; we should, instead of closing, detect ARP conflicts and detect if cable keeps connected ;) |
3735 | hidnplayr | 586 | DEBUGF 2,"Exiting\n" |
3545 | hidnplayr | 587 | mcall -1 |
588 | |||
589 | |||
590 | random: ; Pseudo random actually |
||
591 | |||
592 | mov eax, [generator] |
||
593 | add eax, -43ab45b5h |
||
594 | ror eax, 1 |
||
595 | bswap eax |
||
596 | xor eax, dword[MAC] |
||
597 | ror eax, 1 |
||
598 | xor eax, dword[MAC+2] |
||
599 | mov [generator], eax |
||
600 | |||
601 | ret |
||
602 | |||
603 | ; DATA AREA |
||
604 | |||
605 | align 16 |
||
606 | @IMPORT: |
||
607 | |||
608 | library \ |
||
609 | libini,'libini.obj' |
||
610 | |||
611 | import libini, \ |
||
612 | ini.get_str,'ini_get_str' |
||
613 | |||
614 | include_debug_strings |
||
615 | |||
616 | str_ip db 'ip', 0 |
||
617 | str_subnet db 'subnet', 0 |
||
618 | str_gateway db 'gateway', 0 |
||
619 | str_dns db 'dns', 0 |
||
620 | str_ipconfig db 'ipconfig', 0 |
||
621 | str_type db 'type', 0 |
||
622 | |||
623 | |||
624 | sockaddr1: |
||
625 | |||
626 | dw AF_INET4 |
||
627 | dw 68 shl 8 ; local port |
||
628 | dd 0 ; local IP |
||
629 | |||
630 | rb 10 |
||
631 | |||
632 | |||
633 | sockaddr2: |
||
634 | |||
635 | dw AF_INET4 |
||
636 | dw 67 shl 8 ; destination port |
||
637 | dd -1 ; destination IP |
||
638 | |||
639 | rb 10 |
||
640 | |||
4013 | hidnplayr | 641 | notify_struct: |
642 | dd 7 ; run application |
||
643 | dd 0 |
||
644 | .msg dd 0 |
||
645 | dd 0 |
||
646 | dd 0 |
||
647 | db '/sys/@notify', 0 |
||
3545 | hidnplayr | 648 | |
4790 | leency | 649 | str_connected db '"You are now connected to the network." -N', 0 |
4101 | mario79 | 650 | path db '/sys/settings/network.ini',0 |
4013 | hidnplayr | 651 | |
3545 | hidnplayr | 652 | IM_END: |
653 | |||
3601 | hidnplayr | 654 | device db 1 |
3545 | hidnplayr | 655 | inibuf rb 16 |
3632 | hidnplayr | 656 | tries db ? |
3545 | hidnplayr | 657 | |
3735 | hidnplayr | 658 | dhcpMsgType db ? ; sent |
659 | dhcpMsgType2 db ? ; received |
||
3632 | hidnplayr | 660 | dhcpLease dd ? |
661 | dhcpServerIP dd ? |
||
3545 | hidnplayr | 662 | |
663 | dhcp: |
||
3632 | hidnplayr | 664 | .ip dd ? |
665 | .subnet dd ? |
||
666 | .dns dd ? |
||
667 | .gateway dd ? |
||
3545 | hidnplayr | 668 | |
669 | |||
3632 | hidnplayr | 670 | dhcpMsgLen dd ? |
671 | socketNum dd ? |
||
3545 | hidnplayr | 672 | |
3632 | hidnplayr | 673 | MAC dp ? |
3545 | hidnplayr | 674 | |
3632 | hidnplayr | 675 | currTime dd ? |
676 | generator dd ? |
||
3545 | hidnplayr | 677 | |
3632 | hidnplayr | 678 | dhcpMsg dd ? |
3545 | hidnplayr | 679 | |
4805 | hidnplayr | 680 | timeout dd ? |
681 | |||
3545 | hidnplayr | 682 | I_END: |