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