2013-01-27 21:03:18 +01:00
|
|
|
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
|
|
|
;; ;;
|
|
|
|
;; Copyright (C) KolibriOS team 2010-2013. All rights reserved. ;;
|
|
|
|
;; Distributed under terms of the GNU General Public License ;;
|
|
|
|
;; ;;
|
|
|
|
;; zeroconfig.asm - Zeroconfig service for KolibriOS ;;
|
|
|
|
;; ;;
|
|
|
|
;; Written by hidnplayr@kolibrios.org ;;
|
|
|
|
;; Some code contributed by Derpenguin ;;
|
|
|
|
;; ;;
|
|
|
|
;; DHCP code is based on that by Mike Hibbet ;;
|
|
|
|
; (DHCP client for menuetos) ;;
|
|
|
|
;; ;;
|
|
|
|
;; GNU GENERAL PUBLIC LICENSE ;;
|
|
|
|
;; Version 2, June 1991 ;;
|
|
|
|
;; ;;
|
|
|
|
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
2009-09-18 14:19:46 +02:00
|
|
|
|
2012-07-11 13:59:14 +02:00
|
|
|
format binary as ""
|
|
|
|
|
2009-09-18 14:19:46 +02:00
|
|
|
use32
|
2012-07-11 13:59:14 +02:00
|
|
|
org 0x0
|
2009-09-18 14:19:46 +02:00
|
|
|
|
2012-07-11 13:59:14 +02:00
|
|
|
db 'MENUET01' ; 8 byte id
|
|
|
|
dd 0x01 ; header version
|
|
|
|
dd START ; start of code
|
|
|
|
dd IM_END ; size of image
|
|
|
|
dd (I_END+0x100) ; memory for app
|
|
|
|
dd (I_END+0x100) ; esp
|
|
|
|
dd 0x0 , path ; I_Param , I_Icon
|
2009-09-18 14:19:46 +02:00
|
|
|
|
|
|
|
; CONFIGURATION
|
|
|
|
|
2012-07-11 13:59:14 +02:00
|
|
|
TIMEOUT equ 60 ; in seconds
|
|
|
|
BUFFER equ 1024 ; in bytes
|
|
|
|
__DEBUG__ equ 1 ; enable/disable
|
|
|
|
__DEBUG_LEVEL__ equ 1 ; 1 = all, 2 = errors
|
2009-09-18 14:19:46 +02:00
|
|
|
|
|
|
|
; CONFIGURATION FOR LINK-LOCAL
|
|
|
|
|
2012-07-11 13:59:14 +02:00
|
|
|
PROBE_WAIT equ 1 ; second (initial random delay)
|
|
|
|
PROBE_MIN equ 1 ; second (minimum delay till repeated probe)
|
|
|
|
PROBE_MAX equ 2 ; seconds (maximum delay till repeated probe)
|
|
|
|
PROBE_NUM equ 3 ; (number of probe packets)
|
2009-09-18 14:19:46 +02:00
|
|
|
|
2012-07-11 13:59:14 +02:00
|
|
|
ANNOUNCE_NUM equ 2 ; (number of announcement packets)
|
|
|
|
ANNOUNCE_INTERVAL equ 2 ; seconds (time between announcement packets)
|
|
|
|
ANNOUNCE_WAIT equ 2 ; seconds (delay before announcing)
|
2009-09-18 14:19:46 +02:00
|
|
|
|
2012-07-11 13:59:14 +02:00
|
|
|
MAX_CONFLICTS equ 10 ; (max conflicts before rate limiting)
|
2009-09-18 14:19:46 +02:00
|
|
|
|
2012-07-11 13:59:14 +02:00
|
|
|
RATE_LIMIT_INTERVAL equ 60 ; seconds (delay between successive attempts)
|
2009-09-18 14:19:46 +02:00
|
|
|
|
2012-07-11 13:59:14 +02:00
|
|
|
DEFEND_INTERVAL equ 10 ; seconds (min. wait between defensive ARPs)
|
2009-09-18 14:19:46 +02:00
|
|
|
|
|
|
|
|
2009-09-18 16:31:34 +02:00
|
|
|
include '../proc32.inc'
|
|
|
|
include '../macros.inc'
|
|
|
|
include '../debug-fdo.inc'
|
2010-07-31 13:17:06 +02:00
|
|
|
include '../network.inc'
|
2009-09-18 14:19:46 +02:00
|
|
|
include 'dhcp.inc'
|
2012-07-11 13:59:14 +02:00
|
|
|
include '../dll.inc'
|
2009-09-18 14:19:46 +02:00
|
|
|
|
|
|
|
|
|
|
|
Ip2dword:
|
|
|
|
push edx
|
|
|
|
|
|
|
|
; This code validates if the query is an IP containing 4 numbers and 3 dots
|
|
|
|
|
2012-07-11 13:59:14 +02:00
|
|
|
xor al, al ; make al (dot count) zero
|
2009-09-18 14:19:46 +02:00
|
|
|
|
|
|
|
@@:
|
|
|
|
cmp byte[edx],'0' ; check if this byte is a number, if not jump to no_IP
|
2012-07-11 13:59:14 +02:00
|
|
|
jl no_IP ;
|
2009-09-18 14:19:46 +02:00
|
|
|
cmp byte[edx],'9' ;
|
2012-07-11 13:59:14 +02:00
|
|
|
jg no_IP ;
|
2009-09-18 14:19:46 +02:00
|
|
|
|
2012-07-11 13:59:14 +02:00
|
|
|
inc edx ; the byte was a number, so lets check the next byte
|
2009-09-18 14:19:46 +02:00
|
|
|
|
|
|
|
cmp byte[edx],0 ; is this byte zero? (have we reached end of query?)
|
2012-07-11 13:59:14 +02:00
|
|
|
jz @f ; jump to next @@ then
|
2009-09-18 14:19:46 +02:00
|
|
|
cmp byte[edx],':'
|
2012-07-11 13:59:14 +02:00
|
|
|
jz @f
|
2009-09-18 14:19:46 +02:00
|
|
|
|
|
|
|
cmp byte[edx],'.' ; is this byte a dot?
|
2012-07-11 13:59:14 +02:00
|
|
|
jne @r ; if not, jump to previous @@
|
2009-09-18 14:19:46 +02:00
|
|
|
|
2012-07-11 13:59:14 +02:00
|
|
|
inc al ; the byte was a dot so increment al(dot count)
|
|
|
|
inc edx ; next byte
|
|
|
|
jmp @r ; lets check for numbers again (jump to previous @@)
|
2009-09-18 14:19:46 +02:00
|
|
|
|
2012-07-11 13:59:14 +02:00
|
|
|
@@: ; we reach this when end of query reached
|
|
|
|
cmp al,3 ; check if there where 3 dots
|
|
|
|
jnz no_IP ; if not, jump to no_IP
|
2009-09-18 14:19:46 +02:00
|
|
|
|
|
|
|
; The following code will convert this IP into a dword and output it in eax
|
|
|
|
; If there is also a port number specified, this will be returned in ebx, otherwise ebx is -1
|
|
|
|
|
2012-07-11 13:59:14 +02:00
|
|
|
pop esi ; edx (query address) was pushed onto stack and is now popped in esi
|
2009-09-18 14:19:46 +02:00
|
|
|
|
2012-07-11 13:59:14 +02:00
|
|
|
xor edx, edx ; result
|
|
|
|
xor eax, eax ; current character
|
|
|
|
xor ebx, ebx ; current byte
|
2009-09-18 14:19:46 +02:00
|
|
|
|
2013-01-27 21:03:18 +01:00
|
|
|
.outer_loop:
|
2009-09-18 14:19:46 +02:00
|
|
|
shl edx, 8
|
|
|
|
add edx, ebx
|
|
|
|
xor ebx, ebx
|
2013-01-27 21:03:18 +01:00
|
|
|
.inner_loop:
|
2009-09-18 14:19:46 +02:00
|
|
|
lodsb
|
|
|
|
test eax, eax
|
2012-07-11 13:59:14 +02:00
|
|
|
jz .finish
|
2009-09-18 14:19:46 +02:00
|
|
|
cmp al, '.'
|
2012-07-11 13:59:14 +02:00
|
|
|
jz .outer_loop
|
2009-09-18 14:19:46 +02:00
|
|
|
sub eax, '0'
|
|
|
|
imul ebx, 10
|
|
|
|
add ebx, eax
|
|
|
|
jmp .inner_loop
|
2013-01-27 21:03:18 +01:00
|
|
|
.finish:
|
2009-09-18 14:19:46 +02:00
|
|
|
shl edx, 8
|
|
|
|
add edx, ebx
|
|
|
|
|
2012-07-11 13:59:14 +02:00
|
|
|
bswap edx ; we want little endian order
|
2009-09-18 14:19:46 +02:00
|
|
|
|
|
|
|
ret
|
|
|
|
|
|
|
|
no_IP:
|
|
|
|
pop edx
|
|
|
|
xor edx, edx
|
|
|
|
|
|
|
|
ret
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2013-01-27 21:03:18 +01:00
|
|
|
START:
|
2013-03-06 12:15:28 +01:00
|
|
|
mcall 40, EVM_STACK2
|
2009-09-18 14:19:46 +02:00
|
|
|
|
2013-01-27 21:03:18 +01:00
|
|
|
DEBUGF 1,">Zero-config service loaded\n"
|
2009-09-18 14:19:46 +02:00
|
|
|
|
2013-03-06 12:15:28 +01:00
|
|
|
.wait:
|
2013-01-27 21:03:18 +01:00
|
|
|
mcall 76, API_ETH + 4 ; get MAC of ethernet interface 0
|
2012-07-11 13:59:14 +02:00
|
|
|
cmp eax, -1
|
2013-03-06 12:15:28 +01:00
|
|
|
jne .start
|
|
|
|
|
|
|
|
mcall 10
|
|
|
|
jmp .wait
|
2009-09-18 14:19:46 +02:00
|
|
|
|
2013-03-06 12:15:28 +01:00
|
|
|
.start:
|
|
|
|
mcall 40, EVM_STACK
|
2012-07-11 13:59:14 +02:00
|
|
|
mov word[MAC], bx
|
|
|
|
mov dword[MAC+2], eax
|
2009-09-18 14:19:46 +02:00
|
|
|
|
2012-07-11 13:59:14 +02:00
|
|
|
DEBUGF 1,"->MAC: %x-%x-%x-%x-%x-%x\n",[MAC]:2,[MAC+1]:2,[MAC+2]:2,[MAC+3]:2,[MAC+4]:2,[MAC+5]:2
|
2009-09-18 14:19:46 +02:00
|
|
|
|
2013-01-27 21:03:18 +01:00
|
|
|
mov edi, path ; Calculate the length of zero-terminated string
|
|
|
|
xor al, al
|
2012-07-11 13:59:14 +02:00
|
|
|
mov ecx, 1024
|
2013-01-27 21:03:18 +01:00
|
|
|
repne scasb
|
2012-07-11 13:59:14 +02:00
|
|
|
dec edi
|
2009-09-24 20:32:03 +02:00
|
|
|
|
2013-01-27 21:03:18 +01:00
|
|
|
mov esi, filename ; append with .ini
|
2012-07-11 13:59:14 +02:00
|
|
|
movsd
|
|
|
|
movsb
|
2009-09-24 20:32:03 +02:00
|
|
|
|
2013-01-27 21:03:18 +01:00
|
|
|
DEBUGF 1,"->Loading ini %s\n", path
|
2009-09-18 14:19:46 +02:00
|
|
|
|
2013-01-27 21:03:18 +01:00
|
|
|
mcall 68, 11
|
2009-09-18 14:19:46 +02:00
|
|
|
|
2012-07-11 13:59:14 +02:00
|
|
|
stdcall dll.Load,@IMPORT
|
2013-01-27 21:03:18 +01:00
|
|
|
or eax, eax
|
|
|
|
jnz try_dhcp
|
2009-09-18 14:19:46 +02:00
|
|
|
|
2013-01-27 21:03:18 +01:00
|
|
|
invoke ini.get_str, path, str_ipconfig, str_type, inibuf, 16, 0
|
2009-09-18 14:19:46 +02:00
|
|
|
|
2013-01-27 21:03:18 +01:00
|
|
|
cmp dword[inibuf], 'stat'
|
|
|
|
jne try_dhcp
|
2009-09-18 14:19:46 +02:00
|
|
|
|
2013-01-27 21:03:18 +01:00
|
|
|
invoke ini.get_str, path, str_ipconfig, str_ip, inibuf, 16, 0
|
|
|
|
mov edx, inibuf
|
|
|
|
call Ip2dword
|
|
|
|
mcall 76, API_IPv4 + 3, edx
|
2009-09-18 14:19:46 +02:00
|
|
|
|
2013-01-27 21:03:18 +01:00
|
|
|
invoke ini.get_str, path, str_ipconfig, str_gateway, inibuf, 16, 0
|
|
|
|
mov edx, inibuf
|
|
|
|
call Ip2dword
|
|
|
|
mcall 76, API_IPv4 + 9, edx
|
2009-09-18 14:19:46 +02:00
|
|
|
|
2013-01-27 21:03:18 +01:00
|
|
|
invoke ini.get_str, path, str_ipconfig, str_dns, inibuf, 16, 0
|
|
|
|
mov edx, inibuf
|
|
|
|
call Ip2dword
|
|
|
|
mcall 76, API_IPv4 + 5, edx
|
2009-09-18 14:19:46 +02:00
|
|
|
|
2013-01-27 21:03:18 +01:00
|
|
|
invoke ini.get_str, path, str_ipconfig, str_subnet, inibuf, 16, 0
|
|
|
|
mov edx, inibuf
|
|
|
|
call Ip2dword
|
|
|
|
mcall 76, API_IPv4 + 7, edx
|
2009-09-18 14:19:46 +02:00
|
|
|
|
|
|
|
|
2013-01-27 21:03:18 +01:00
|
|
|
mcall -1
|
2009-09-18 14:19:46 +02:00
|
|
|
|
|
|
|
|
2013-01-27 21:03:18 +01:00
|
|
|
try_dhcp:
|
2009-09-18 14:19:46 +02:00
|
|
|
|
2013-01-27 21:03:18 +01:00
|
|
|
DEBUGF 1,"->Trying DHCP\n"
|
2009-09-18 14:19:46 +02:00
|
|
|
|
2013-01-27 21:03:18 +01:00
|
|
|
mcall 75, 0, AF_INET4, SOCK_DGRAM, 0 ; open socket (parameters: domain, type, reserved)
|
|
|
|
cmp eax, -1
|
|
|
|
je error
|
|
|
|
mov [socketNum], eax
|
2009-09-18 14:19:46 +02:00
|
|
|
|
2013-01-27 21:03:18 +01:00
|
|
|
DEBUGF 1,"->Socket %x opened\n", eax
|
2009-09-18 14:19:46 +02:00
|
|
|
|
2013-01-27 21:03:18 +01:00
|
|
|
mcall 75, 2, [socketNum], sockaddr1, 18 ; bind socket to local port 68
|
|
|
|
cmp eax, -1
|
|
|
|
je error
|
2009-09-18 14:19:46 +02:00
|
|
|
|
2012-07-11 13:59:14 +02:00
|
|
|
DEBUGF 1,"->Socket Bound to local port 68\n"
|
2009-09-18 14:19:46 +02:00
|
|
|
|
2013-01-27 21:03:18 +01:00
|
|
|
mcall 75, 4, [socketNum], sockaddr2, 18 ; connect to 255.255.255.255 on port 67
|
|
|
|
cmp eax, -1
|
|
|
|
je error
|
2009-09-18 14:19:46 +02:00
|
|
|
|
2012-07-11 13:59:14 +02:00
|
|
|
DEBUGF 1,"->Connected to 255.255.255.255 on port 67\n"
|
2009-09-18 14:19:46 +02:00
|
|
|
|
2013-01-27 21:03:18 +01:00
|
|
|
mov [dhcpMsgType], 0x01 ; DHCP discover
|
|
|
|
mov [dhcpLease], esi ; esi is still -1 (-1 = forever)
|
2009-09-18 14:19:46 +02:00
|
|
|
|
2013-01-27 21:03:18 +01:00
|
|
|
mcall 26, 9 ; Get system time
|
|
|
|
imul eax, 100
|
|
|
|
mov [currTime], eax
|
2009-09-18 14:19:46 +02:00
|
|
|
|
2013-01-27 21:03:18 +01:00
|
|
|
build_request: ; Creates a DHCP request packet.
|
2009-09-18 14:19:46 +02:00
|
|
|
|
2012-07-11 13:59:14 +02:00
|
|
|
DEBUGF 1,"->Building request\n"
|
2010-07-12 01:13:12 +02:00
|
|
|
|
2012-07-11 13:59:14 +02:00
|
|
|
stdcall mem.Alloc, BUFFER
|
|
|
|
mov [dhcpMsg], eax
|
2013-01-27 21:03:18 +01:00
|
|
|
test eax, eax
|
2013-03-11 18:17:07 +01:00
|
|
|
jz dhcp_error
|
2010-07-12 01:13:12 +02:00
|
|
|
|
2012-07-11 13:59:14 +02:00
|
|
|
;;; todo: skip this bullcrap
|
2010-07-12 01:13:12 +02:00
|
|
|
|
2012-07-11 13:59:14 +02:00
|
|
|
mov edi, eax
|
|
|
|
mov ecx, BUFFER
|
|
|
|
xor eax, eax
|
|
|
|
rep stosb
|
2010-07-12 01:13:12 +02:00
|
|
|
|
2012-07-11 13:59:14 +02:00
|
|
|
;; todo: put this in a buffer instead of writing bytes and words!
|
2010-07-12 01:13:12 +02:00
|
|
|
|
2013-01-27 21:03:18 +01:00
|
|
|
mov edx, [dhcpMsg]
|
2010-07-12 01:13:12 +02:00
|
|
|
|
2013-03-11 18:17:07 +01:00
|
|
|
; Boot protocol legacy
|
2012-07-11 13:59:14 +02:00
|
|
|
mov [edx], byte 0x01 ; Boot request
|
|
|
|
mov [edx+1], byte 0x01 ; Ethernet
|
|
|
|
mov [edx+2], byte 0x06 ; Ethernet h/w len
|
|
|
|
mov [edx+4], dword 0x11223344 ; xid ;;;;;;;
|
2013-01-27 21:03:18 +01:00
|
|
|
mov eax, [currTime]
|
2012-07-11 13:59:14 +02:00
|
|
|
mov [edx+8], eax ; secs, our uptime
|
|
|
|
mov [edx+10], byte 0x80 ; broadcast flag set
|
|
|
|
mov eax, dword [MAC] ; first 4 bytes of MAC
|
|
|
|
mov [edx+28],dword eax
|
|
|
|
mov ax, word [MAC+4] ; last 2 bytes of MAC
|
|
|
|
mov [edx+32],word ax
|
2013-03-11 18:17:07 +01:00
|
|
|
|
|
|
|
; DHCP extension
|
2012-07-11 13:59:14 +02:00
|
|
|
mov [edx+236], dword 0x63538263 ; magic cookie
|
|
|
|
mov [edx+240], word 0x0135 ; option DHCP msg type
|
|
|
|
mov al, [dhcpMsgType]
|
|
|
|
mov [edx+240+2], al
|
|
|
|
mov [edx+240+3], word 0x0433 ; option Lease time = infinity
|
|
|
|
mov eax, [dhcpLease]
|
|
|
|
mov [edx+240+5], eax
|
|
|
|
mov [edx+240+9], word 0x0432 ; option requested IP address
|
2012-07-12 15:57:34 +02:00
|
|
|
mov eax, [dhcp.ip]
|
2012-07-11 13:59:14 +02:00
|
|
|
mov [edx+240+11], eax
|
|
|
|
mov [edx+240+15], word 0x0437 ; option request list
|
|
|
|
mov [edx+240+17], dword 0x0f060301
|
2010-07-12 01:13:12 +02:00
|
|
|
|
2012-07-11 13:59:14 +02:00
|
|
|
cmp [dhcpMsgType], byte 0x01 ; Check which msg we are sending
|
|
|
|
jne request_options
|
2010-07-12 01:13:12 +02:00
|
|
|
|
2012-07-11 13:59:14 +02:00
|
|
|
mov [edx+240+21], byte 0xff ; "Discover" options
|
2010-07-12 01:13:12 +02:00
|
|
|
|
2012-07-11 13:59:14 +02:00
|
|
|
mov [dhcpMsgLen], dword 262 ; end of options marker
|
2013-01-27 21:03:18 +01:00
|
|
|
jmp send_dhcpmsg
|
2009-09-18 14:19:46 +02:00
|
|
|
|
|
|
|
request_options:
|
2012-07-11 13:59:14 +02:00
|
|
|
mov [edx+240+21], word 0x0436 ; server IP
|
|
|
|
mov eax, [dhcpServerIP]
|
|
|
|
mov [edx+240+23], eax
|
2009-09-18 14:19:46 +02:00
|
|
|
|
2012-07-11 13:59:14 +02:00
|
|
|
mov [edx+240+27], byte 0xff ; end of options marker
|
2009-09-18 14:19:46 +02:00
|
|
|
|
2012-07-11 13:59:14 +02:00
|
|
|
mov [dhcpMsgLen], dword 268
|
2009-09-18 14:19:46 +02:00
|
|
|
|
2013-01-27 21:03:18 +01:00
|
|
|
send_dhcpmsg:
|
2012-07-11 13:59:14 +02:00
|
|
|
mcall 75, 6, [socketNum], [dhcpMsg], [dhcpMsgLen] ; write to socket ( send broadcast request )
|
2009-09-18 14:19:46 +02:00
|
|
|
|
2012-07-11 13:59:14 +02:00
|
|
|
mov eax, [dhcpMsg] ; Setup the DHCP buffer to receive response
|
|
|
|
mov [dhcpMsgLen], eax ; Used as a pointer to the data
|
2009-09-18 14:19:46 +02:00
|
|
|
|
2012-07-11 13:59:14 +02:00
|
|
|
mcall 23, TIMEOUT*10 ; wait for data
|
2009-09-18 14:19:46 +02:00
|
|
|
|
2012-07-11 13:59:14 +02:00
|
|
|
read_data: ; we have data - this will be the response
|
|
|
|
mcall 75, 7, [socketNum], [dhcpMsg], BUFFER ; read data from socket
|
2009-09-18 14:19:46 +02:00
|
|
|
|
2012-07-11 13:59:14 +02:00
|
|
|
DEBUGF 1,"->%d bytes received\n", eax
|
2009-09-18 14:19:46 +02:00
|
|
|
|
2012-07-11 13:59:14 +02:00
|
|
|
cmp eax, -1
|
2013-03-11 18:17:07 +01:00
|
|
|
je dhcp_error
|
2009-09-18 14:19:46 +02:00
|
|
|
|
2012-07-11 13:59:14 +02:00
|
|
|
mov [dhcpMsgLen], eax
|
2009-09-18 14:19:46 +02:00
|
|
|
|
2013-01-27 21:03:18 +01:00
|
|
|
; depending on which msg we sent, handle the response
|
|
|
|
; accordingly.
|
|
|
|
; If the response is to a dhcp discover, then:
|
|
|
|
; 1) If response is DHCP OFFER then
|
|
|
|
; 1.1) record server IP, lease time & IP address.
|
|
|
|
; 1.2) send a request packet
|
|
|
|
; If the response is to a dhcp request, then:
|
|
|
|
; 1) If the response is DHCP ACK then
|
|
|
|
; 1.1) extract the DNS & subnet fields. Set them in the stack
|
|
|
|
|
|
|
|
cmp [dhcpMsgType], 0x01 ; did we send a discover?
|
|
|
|
je discover
|
2009-09-18 14:19:46 +02:00
|
|
|
|
2013-01-27 21:03:18 +01:00
|
|
|
cmp [dhcpMsgType], 0x03 ; did we send a request?
|
|
|
|
je request
|
2009-09-18 14:19:46 +02:00
|
|
|
|
2013-01-27 21:03:18 +01:00
|
|
|
call dhcp_end ; we should never reach here ;)
|
|
|
|
jmp exit
|
2009-09-18 14:19:46 +02:00
|
|
|
|
|
|
|
discover:
|
2013-01-27 21:03:18 +01:00
|
|
|
call parse_response
|
|
|
|
|
|
|
|
cmp [dhcpMsgType], 0x02 ; Was the response an offer?
|
|
|
|
je send_request
|
2009-09-18 14:19:46 +02:00
|
|
|
|
2013-01-27 21:03:18 +01:00
|
|
|
call dhcp_end
|
|
|
|
jmp link_local
|
|
|
|
|
|
|
|
send_request:
|
|
|
|
mov [dhcpMsgType], 0x03 ; make it a request
|
|
|
|
jmp build_request
|
2009-09-18 14:19:46 +02:00
|
|
|
|
|
|
|
request:
|
2013-01-27 21:03:18 +01:00
|
|
|
call parse_response
|
2009-09-18 14:19:46 +02:00
|
|
|
|
2013-01-27 21:03:18 +01:00
|
|
|
cmp [dhcpMsgType], 0x05 ; Was the response an ACK? It should be
|
2013-03-11 18:17:07 +01:00
|
|
|
jne read_data ; NO - read next packets
|
|
|
|
|
|
|
|
call dhcp_end
|
2009-09-18 14:19:46 +02:00
|
|
|
|
2013-01-27 21:03:18 +01:00
|
|
|
mcall 76, API_IPv4 + 3, [dhcp.ip] ; ip
|
|
|
|
mcall 76, API_IPv4 + 5, [dhcp.dns] ; dns
|
|
|
|
mcall 76, API_IPv4 + 7, [dhcp.subnet] ; subnet
|
|
|
|
mcall 76, API_IPv4 + 9, [dhcp.gateway] ; gateway
|
2012-07-12 15:57:34 +02:00
|
|
|
|
2012-07-11 13:59:14 +02:00
|
|
|
jmp exit
|
2009-09-18 14:19:46 +02:00
|
|
|
|
2013-01-27 21:03:18 +01:00
|
|
|
dhcp_end:
|
|
|
|
mcall close, [socketNum]
|
|
|
|
stdcall mem.Free, [dhcpMsg]
|
|
|
|
|
|
|
|
ret
|
|
|
|
|
2009-09-18 14:19:46 +02:00
|
|
|
;***************************************************************************
|
|
|
|
; Function
|
|
|
|
; parseResponse
|
|
|
|
;
|
|
|
|
; Description
|
|
|
|
; extracts the fields ( client IP address and options ) from
|
|
|
|
; a DHCP response
|
|
|
|
; The values go into
|
|
|
|
; dhcpMsgType,dhcpLease,dhcpClientIP,dhcpServerIP,
|
|
|
|
; dhcpDNSIP, dhcpSubnet
|
|
|
|
; The message is stored in dhcpMsg
|
|
|
|
;
|
|
|
|
;***************************************************************************
|
2013-01-27 21:03:18 +01:00
|
|
|
parse_response:
|
|
|
|
|
|
|
|
DEBUGF 1,"Data received, parsing response\n"
|
|
|
|
mov edx, [dhcpMsg]
|
2009-09-18 14:19:46 +02:00
|
|
|
|
2013-01-27 21:03:18 +01:00
|
|
|
push dword [edx+16]
|
|
|
|
pop [dhcp.ip]
|
|
|
|
DEBUGF 1,"Client: %u.%u.%u.%u\n", [edx+16]:1, [edx+17]:1, [edx+18]:1, [edx+19]:1
|
2009-09-18 14:19:46 +02:00
|
|
|
|
2013-01-27 21:03:18 +01:00
|
|
|
; TODO: check if there really are options
|
2009-09-18 14:19:46 +02:00
|
|
|
|
2013-01-27 21:03:18 +01:00
|
|
|
mov al, 240 ; Point to first option
|
|
|
|
movzx ecx, al
|
2009-09-18 14:19:46 +02:00
|
|
|
|
2013-01-27 21:03:18 +01:00
|
|
|
.next_option:
|
|
|
|
add edx, ecx
|
2009-09-18 14:19:46 +02:00
|
|
|
|
2013-01-27 21:03:18 +01:00
|
|
|
mov al, [edx] ; get message identifier
|
2012-07-12 15:57:34 +02:00
|
|
|
|
2013-01-27 21:03:18 +01:00
|
|
|
cmp al, 0xff ; End of options?
|
|
|
|
je .done
|
2012-07-12 15:57:34 +02:00
|
|
|
|
2013-01-27 21:03:18 +01:00
|
|
|
cmp al, 0
|
|
|
|
je .pad
|
2009-09-18 14:19:46 +02:00
|
|
|
|
2013-01-27 21:03:18 +01:00
|
|
|
; TODO: check if we still are inside the buffer
|
2009-09-18 14:19:46 +02:00
|
|
|
|
2013-01-27 21:03:18 +01:00
|
|
|
inc edx
|
|
|
|
movzx ecx, byte [edx] ; get data length
|
|
|
|
inc edx ; point to data
|
2009-09-18 14:19:46 +02:00
|
|
|
|
2013-01-27 21:03:18 +01:00
|
|
|
cmp al, dhcp_msg_type ; Msg type is a single byte option
|
|
|
|
je .msgtype
|
2009-09-18 14:19:46 +02:00
|
|
|
|
2013-01-27 21:03:18 +01:00
|
|
|
cmp al, dhcp_dhcp_server_id
|
|
|
|
je .server
|
2009-09-18 14:19:46 +02:00
|
|
|
|
2013-01-27 21:03:18 +01:00
|
|
|
cmp al, dhcp_address_time
|
|
|
|
je .lease
|
2009-09-18 14:19:46 +02:00
|
|
|
|
2013-01-27 21:03:18 +01:00
|
|
|
cmp al, dhcp_subnet_mask
|
|
|
|
je .subnet
|
2009-09-18 14:19:46 +02:00
|
|
|
|
2013-01-27 21:03:18 +01:00
|
|
|
cmp al, dhcp_router
|
|
|
|
je .router
|
2009-09-18 14:19:46 +02:00
|
|
|
|
2013-01-27 21:03:18 +01:00
|
|
|
cmp al, dhcp_domain_server
|
|
|
|
je .dns
|
2009-09-18 14:19:46 +02:00
|
|
|
|
2013-01-27 21:03:18 +01:00
|
|
|
DEBUGF 1,"Unsupported DHCP option: %u\n", al
|
2009-09-18 14:19:46 +02:00
|
|
|
|
2013-01-27 21:03:18 +01:00
|
|
|
jmp .next_option
|
2009-09-18 14:19:46 +02:00
|
|
|
|
2013-01-27 21:03:18 +01:00
|
|
|
.pad:
|
|
|
|
xor ecx, ecx
|
|
|
|
inc ecx
|
|
|
|
jmp .next_option
|
2009-09-18 14:19:46 +02:00
|
|
|
|
2013-01-27 21:03:18 +01:00
|
|
|
.msgtype:
|
|
|
|
mov al, [edx]
|
|
|
|
mov [dhcpMsgType], al
|
2009-09-18 14:19:46 +02:00
|
|
|
|
2013-01-27 21:03:18 +01:00
|
|
|
DEBUGF 1,"DHCP Msg type: %u\n", al
|
|
|
|
jmp .next_option ; Get next option
|
2009-09-18 14:19:46 +02:00
|
|
|
|
2013-01-27 21:03:18 +01:00
|
|
|
.server:
|
|
|
|
mov eax, [edx]
|
|
|
|
mov [dhcpServerIP], eax
|
|
|
|
DEBUGF 1,"Server: %u.%u.%u.%u\n",[edx]:1,[edx+1]:1,[edx+2]:1,[edx+3]:1
|
|
|
|
jmp .next_option
|
|
|
|
|
|
|
|
.lease:
|
|
|
|
pusha
|
|
|
|
mov eax,[edx]
|
|
|
|
bswap eax
|
|
|
|
mov [dhcpLease],eax
|
|
|
|
DEBUGF 1,"lease: %d\n",eax
|
|
|
|
popa
|
|
|
|
jmp .next_option
|
|
|
|
|
|
|
|
.subnet:
|
|
|
|
push dword [edx]
|
|
|
|
pop [dhcp.subnet]
|
|
|
|
DEBUGF 1,"Subnet: %u.%u.%u.%u\n",[edx]:1,[edx+1]:1,[edx+2]:1,[edx+3]:1
|
|
|
|
jmp .next_option
|
|
|
|
|
|
|
|
.router:
|
|
|
|
push dword [edx]
|
|
|
|
pop [dhcp.gateway]
|
|
|
|
DEBUGF 1,"Gateway: %u.%u.%u.%u\n",[edx]:1,[edx+1]:1,[edx+2]:1,[edx+3]:1
|
|
|
|
jmp .next_option
|
|
|
|
|
|
|
|
.dns:
|
|
|
|
push dword [edx]
|
|
|
|
pop [dhcp.dns]
|
|
|
|
DEBUGF 1,"DNS: %u.%u.%u.%u\n",[edx]:1,[edx+1]:1,[edx+2]:1,[edx+3]:1
|
|
|
|
jmp .next_option
|
|
|
|
|
|
|
|
.done:
|
2012-07-11 13:59:14 +02:00
|
|
|
ret
|
|
|
|
|
2009-09-18 14:19:46 +02:00
|
|
|
|
|
|
|
|
2013-03-11 18:17:07 +01:00
|
|
|
dhcp_error:
|
|
|
|
call dhcp_end
|
2013-01-27 21:03:18 +01:00
|
|
|
|
2009-09-18 14:19:46 +02:00
|
|
|
link_local:
|
2013-01-27 21:03:18 +01:00
|
|
|
call random
|
|
|
|
mov cx, ax
|
2013-03-11 18:17:07 +01:00
|
|
|
shl ecx, 16
|
|
|
|
mov cx, 0xfea9 ; IP 169.254.0.0 link local net, see RFC3927
|
|
|
|
mcall 76, API_IPv4 + 3, ecx ; mask is 255.255.0.0
|
2013-03-12 14:23:08 +01:00
|
|
|
DEBUGF 1,"Link Local IP assinged: 169.254.%u.%u\n", [generator+0]:1, [generator+1]:1
|
2013-03-11 18:17:07 +01:00
|
|
|
mcall 76, API_IPv4 + 7, 0xffff
|
2013-01-27 21:03:18 +01:00
|
|
|
mcall 76, API_IPv4 + 9, 0x0
|
2013-03-11 18:17:07 +01:00
|
|
|
mcall 76, API_IPv4 + 5, 0x0
|
2013-01-27 21:03:18 +01:00
|
|
|
|
|
|
|
mcall 5, PROBE_WAIT*100
|
|
|
|
|
|
|
|
xor esi, esi
|
2009-09-18 14:19:46 +02:00
|
|
|
probe_loop:
|
2013-01-27 21:03:18 +01:00
|
|
|
call random ; create a pseudo random number in eax (seeded by MAC)
|
2009-09-18 14:19:46 +02:00
|
|
|
|
2013-01-27 21:03:18 +01:00
|
|
|
cmp al, PROBE_MIN*100 ; check if al is bigger then PROBE_MIN
|
|
|
|
jae @f ; all ok
|
|
|
|
add al, (PROBE_MAX-PROBE_MIN)*100 ; al is too small
|
2009-09-18 14:19:46 +02:00
|
|
|
@@:
|
|
|
|
|
2013-01-27 21:03:18 +01:00
|
|
|
cmp al, PROBE_MAX*100
|
|
|
|
jbe @f
|
|
|
|
sub al, (PROBE_MAX-PROBE_MIN)*100
|
2009-09-18 14:19:46 +02:00
|
|
|
@@:
|
|
|
|
|
2013-01-27 21:03:18 +01:00
|
|
|
movzx ebx,al
|
|
|
|
DEBUGF 1,"Waiting %u0ms\n",ebx
|
|
|
|
mcall 5
|
2009-09-18 14:19:46 +02:00
|
|
|
|
2013-01-27 21:03:18 +01:00
|
|
|
DEBUGF 1,"Sending Probe\n"
|
|
|
|
mcall 76, API_ARP + 6
|
|
|
|
inc esi
|
2009-09-18 14:19:46 +02:00
|
|
|
|
2013-01-27 21:03:18 +01:00
|
|
|
cmp esi, PROBE_NUM
|
|
|
|
jb probe_loop
|
2009-09-18 14:19:46 +02:00
|
|
|
|
|
|
|
; now we wait further ANNOUNCE_WAIT seconds and send ANNOUNCE_NUM ARP announces. If any other host has assingned
|
|
|
|
; IP within this time, we should create another adress, that have to be done later
|
|
|
|
|
2013-01-27 21:03:18 +01:00
|
|
|
DEBUGF 1,"Waiting %us\n", ANNOUNCE_WAIT
|
|
|
|
mcall 5, ANNOUNCE_WAIT*100
|
|
|
|
xor esi, esi
|
2009-09-18 14:19:46 +02:00
|
|
|
announce_loop:
|
|
|
|
|
2013-01-27 21:03:18 +01:00
|
|
|
DEBUGF 1,"Sending Announce\n"
|
|
|
|
mcall 76, API_ARP + 6
|
2009-09-18 14:19:46 +02:00
|
|
|
|
2013-01-27 21:03:18 +01:00
|
|
|
inc esi
|
|
|
|
cmp esi,ANNOUNCE_NUM
|
|
|
|
je @f
|
2009-09-18 14:19:46 +02:00
|
|
|
|
2013-01-27 21:03:18 +01:00
|
|
|
DEBUGF 1,"Waiting %us\n", ANNOUNCE_INTERVAL
|
|
|
|
mcall 5, ANNOUNCE_INTERVAL*100
|
|
|
|
jmp announce_loop
|
2009-09-18 14:19:46 +02:00
|
|
|
@@:
|
2013-01-27 21:03:18 +01:00
|
|
|
|
2009-09-18 14:19:46 +02:00
|
|
|
|
|
|
|
error:
|
2013-01-27 21:03:18 +01:00
|
|
|
DEBUGF 1,"Socket error\n"
|
|
|
|
exit: ; we should, instead of closing, detect ARP conflicts and detect if cable keeps connected ;)
|
|
|
|
mcall -1
|
2009-09-18 14:19:46 +02:00
|
|
|
|
|
|
|
|
|
|
|
random: ; Pseudo random actually
|
|
|
|
|
2013-01-27 21:03:18 +01:00
|
|
|
mov eax, [generator]
|
|
|
|
add eax, -43ab45b5h
|
|
|
|
ror eax, 1
|
|
|
|
bswap eax
|
|
|
|
xor eax, dword[MAC]
|
|
|
|
ror eax, 1
|
|
|
|
xor eax, dword[MAC+2]
|
|
|
|
mov [generator], eax
|
2009-09-18 14:19:46 +02:00
|
|
|
|
2013-01-27 21:03:18 +01:00
|
|
|
ret
|
2009-09-18 14:19:46 +02:00
|
|
|
|
|
|
|
; DATA AREA
|
|
|
|
|
|
|
|
align 16
|
|
|
|
@IMPORT:
|
|
|
|
|
|
|
|
library \
|
2012-07-11 13:59:14 +02:00
|
|
|
libini,'libini.obj'
|
2009-09-18 14:19:46 +02:00
|
|
|
|
2012-07-11 13:59:14 +02:00
|
|
|
import libini, \
|
|
|
|
ini.get_str,'ini_get_str'
|
2009-09-18 14:19:46 +02:00
|
|
|
|
|
|
|
include_debug_strings
|
|
|
|
|
2013-01-27 21:03:18 +01:00
|
|
|
filename db '.ini', 0
|
|
|
|
str_ip db 'ip', 0
|
|
|
|
str_subnet db 'subnet', 0
|
|
|
|
str_gateway db 'gateway', 0
|
|
|
|
str_dns db 'dns', 0
|
|
|
|
str_ipconfig db 'ipconfig', 0
|
|
|
|
str_type db 'type', 0
|
2009-09-18 14:19:46 +02:00
|
|
|
|
|
|
|
|
|
|
|
sockaddr1:
|
|
|
|
|
2012-07-11 13:59:14 +02:00
|
|
|
dw AF_INET4
|
2012-11-01 13:33:15 +01:00
|
|
|
dw 68 shl 8 ; local port
|
2012-07-11 13:59:14 +02:00
|
|
|
dd 0 ; local IP
|
2009-09-18 14:19:46 +02:00
|
|
|
|
2012-07-11 13:59:14 +02:00
|
|
|
rb 10
|
2009-09-18 14:19:46 +02:00
|
|
|
|
|
|
|
|
|
|
|
sockaddr2:
|
|
|
|
|
2012-07-11 13:59:14 +02:00
|
|
|
dw AF_INET4
|
2012-11-01 13:33:15 +01:00
|
|
|
dw 67 shl 8 ; destination port
|
2012-07-11 13:59:14 +02:00
|
|
|
dd -1 ; destination IP
|
2009-09-18 14:19:46 +02:00
|
|
|
|
2012-07-11 13:59:14 +02:00
|
|
|
rb 10
|
2009-09-18 14:19:46 +02:00
|
|
|
|
|
|
|
|
|
|
|
IM_END:
|
|
|
|
|
2012-07-11 13:59:14 +02:00
|
|
|
inibuf rb 16
|
2009-09-18 14:19:46 +02:00
|
|
|
|
2012-07-11 13:59:14 +02:00
|
|
|
dhcpMsgType db ?
|
|
|
|
dhcpLease dd ?
|
|
|
|
dhcpServerIP dd ?
|
2009-09-18 14:19:46 +02:00
|
|
|
|
2012-07-12 15:57:34 +02:00
|
|
|
dhcp:
|
|
|
|
.ip dd ?
|
|
|
|
.subnet dd ?
|
|
|
|
.dns dd ?
|
|
|
|
.gateway dd ?
|
|
|
|
|
|
|
|
|
2012-07-11 13:59:14 +02:00
|
|
|
dhcpMsgLen dd ?
|
|
|
|
socketNum dd ?
|
2009-09-18 14:19:46 +02:00
|
|
|
|
2012-07-11 13:59:14 +02:00
|
|
|
MAC dp ?
|
2009-09-18 14:19:46 +02:00
|
|
|
|
2012-07-11 13:59:14 +02:00
|
|
|
currTime dd ?
|
|
|
|
generator dd ?
|
2009-09-18 14:19:46 +02:00
|
|
|
|
2012-07-11 13:59:14 +02:00
|
|
|
dhcpMsg dd ?
|
2009-09-18 14:19:46 +02:00
|
|
|
|
|
|
|
I_END_2:
|
|
|
|
|
2012-07-11 13:59:14 +02:00
|
|
|
path rb 1024+5
|
2009-09-18 14:19:46 +02:00
|
|
|
|
2012-07-11 13:59:14 +02:00
|
|
|
rb 65536
|
2010-07-31 13:17:06 +02:00
|
|
|
|
2009-09-18 14:19:46 +02:00
|
|
|
I_END:
|