Subversion Repositories Kolibri OS

Compare Revisions

Regard whitespace Rev 336 → Rev 335

/programs/network/autodhcp/trunk/ETH.INC
File deleted
/programs/network/autodhcp/trunk/debug-fdo.inc
File deleted
/programs/network/autodhcp/trunk/autodhcp.asm
1,17 → 1,16
;
; Automated dhcp client
; v 1.3
;
; with thanks to authors of DHCP client for menuetos: Mike Hibbet
; v 1.1
;
; by HidnPlayr & Derpenguin
; by the hidden player
;
 
 
DEBUG equ 1
TIMEOUT equ 60 ; in seconds
BUFFER equ 1024
__DEBUG__ equ 1
__DEBUG_LEVEL__ equ 1; 1 = all, 2 = errors
 
use32
 
org 0x0
 
db 'MENUET01' ; 8 byte id
22,33 → 21,13
dd I_END ; esp
dd 0x0 , 0x0 ; I_Param , I_Icon
 
;include 'macros.inc'
include 'eth.inc'
include 'debug-fdo.inc'
include 'macros.inc'
 
if DEBUG = 1
include 'debug.inc'
end if
 
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
; CONFIGURATION FOR LINK-LOCAL ;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
; ;
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) ;
; ;
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) ;
; ;
MAX_CONFLICTS equ 10 ; (max conflicts before rate limiting) ;
; ;
RATE_LIMIT_INTERVAL equ 60 ; seconds (delay between successive attempts) ;
; ;
DEFEND_INTERVAL equ 10 ; seconds (min. wait between defensive ARPs) ;
; ;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
 
 
START: ; start of execution
 
mov eax,40 ; Report events
55,69 → 34,61
mov ebx,10000000b ; Only Stack
int 0x40
 
mov eax,52 ; first, enable the stack (packet driver)
mov eax,52 ; first, enable the stack
mov ebx,2
mov ecx,0x00000383
int 0x40
 
DEBUGF 1,"DHCP: Stack Initialized.\n"
if DEBUG = 1
newline
dps "DHCP: Stack Initialized"
newline
end if
 
eth.status eax ; Read the Stack status
test eax,eax ; if eax is zero, no driver was found
jnz @f
DEBUGF 1,"DHCP: No Card detected\n"
jmp close
mov eax, 53 ; then, read in the status
mov ebx, 255
mov ecx, 6
int 0x40
 
@@:
DEBUGF 1,"DHCP: Detected card: %x\n",eax
@@:
eth.check_cable eax
test al,al
jnz @f
DEBUGF 1,"DHCP: Ethernet Cable not connected\n"
cmp eax,0 ; if eax is zero, no driver was found
jne @f
 
mov eax,5
mov ebx,500 ; loop until cable is connected (check every 5 sec)
int 0x40
if DEBUG = 1
dps "DHCP: No Card detected"
newline
end if
 
jmp @r
jmp close
 
@@:
DEBUGF 1,"DHCP: Ethernet Cable status: %d\n",al
if DEBUG = 1
dps "DHCP: Detected card: "
dph eax
newline
end if
 
eth.read_mac MAC
DEBUGF 1,"DHCP: MAC address: %x-%x-%x-%x-%x-%x\n",[MAC]:2,[MAC+1]:2,[MAC+2]:2,[MAC+3]:2,[MAC+4]:2,[MAC+5]:2
; now that the stack is running, lets start the dhcp request
 
; jmp apipa ; comment this out if you want to skip DHCP and continue with link-local
; First, open socket
mov eax, 53
mov ebx, 0
mov ecx, 68 ; local port dhcp client
mov edx, 67 ; remote port - dhcp server
mov esi, -1 ; broadcast
int 0x40
 
;***************************************************************************
;
; DHCP rubish starts here
;
;***************************************************************************
mov [socketNum], eax
 
if DEBUG = 1
dps "DHCP: Socket opened: "
dpd eax
newline
end if
 
 
eth.check_port 68,eax ; Check if port 68 is available
cmp eax,1
je @f
 
DEBUGF 1,"DHCP: Port 68 is already in use.\n"
jmp close
 
@@:
eth.open_udp 68,67,-1,[socketNum] ; open socket (local,remote,ip,socket)
DEBUGF 1,"DHCP: Socket opened: %d\n",eax
; Setup the first msg we will send
mov byte [dhcpMsgType], 0x01 ; DHCP discover
mov dword [dhcpLease], esi ; esi is still -1 (-1 = forever)
 
mov eax,26
mov ebx,9
int 0x40
imul eax,100
mov [currTime],eax
 
;***************************************************************************
; Function
; buildRequest
127,9 → 98,10
;
;***************************************************************************
buildRequest:
xor eax,eax ; Clear dhcpMsg to all zeros
; Clear dhcpMsg to all zeros
xor eax,eax
mov edi,dhcpMsg
mov ecx,BUFFER
mov ecx,512
cld
rep stosb
 
139,76 → 111,111
mov [edx+1], byte 0x01 ; Ethernet
mov [edx+2], byte 0x06 ; Ethernet h/w len
mov [edx+4], dword 0x11223344 ; xid
mov eax,[currTime]
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
 
mov [edx+236], dword 0x63538263 ; magic number
 
mov [edx+240], word 0x0135 ; option DHCP msg type
; option DHCP msg type
mov [edx+240], word 0x0135
mov al, [dhcpMsgType]
mov [edx+240+2], al
 
mov [edx+240+3], word 0x0433 ; option Lease time = infinity
; option Lease time = infinity
mov [edx+240+3], word 0x0433
mov eax, [dhcpLease]
mov [edx+240+5], eax
 
mov [edx+240+9], word 0x0432 ; option requested IP address
mov eax, [dhcpClientIP]
mov [edx+240+11], eax
; ; option requested IP address
mov [edx+240+9], word 0x0432
; mov eax, [dhcpClientIP]
; mov [edx+240+11], eax
 
mov [edx+240+15], word 0x0437 ; option request list
; option request list
mov [edx+240+15], word 0x0437
mov [edx+240+17], dword 0x0f060301
 
cmp [dhcpMsgType], byte 0x01 ; Check which msg we are sending
; Check which msg we are sending
cmp [dhcpMsgType], byte 0x01
jne br001
 
mov [edx+240+21], byte 0xff ; "Discover" options
; "Discover" options
; end of options marker
mov [edx+240+21], byte 0xff
 
mov [dhcpMsgLen], dword 262 ; end of options marker
mov [dhcpMsgLen], dword 262
jmp ctr000
 
br001: ; "Request" options
br001:
; "Request" options
 
mov [edx+240+21], word 0x0436 ; server IP
; server IP
mov [edx+240+21], word 0x0436
mov eax, [dhcpServerIP]
mov [edx+240+23], eax
 
mov [edx+240+27], byte 0xff ; end of options marker
; end of options marker
mov [edx+240+27], byte 0xff
 
mov [dhcpMsgLen], dword 268
 
ctr000:
 
eth.write_udp [socketNum],[dhcpMsgLen],dhcpMsg ; write to socket ( send broadcast request )
; write to socket ( send broadcast request )
mov eax, 53
mov ebx, 4
mov ecx, [socketNum]
mov edx, [dhcpMsgLen]
mov esi, dhcpMsg
int 0x40
 
mov eax, dhcpMsg ; Setup the DHCP buffer to receive response
; Setup the DHCP buffer to receive response
 
mov eax, dhcpMsg
mov [dhcpMsgLen], eax ; Used as a pointer to the data
 
mov eax,23 ; wait here for event (data from remote)
mov ebx,TIMEOUT*10
; now, we wait for data from remote
 
wait_for_data:
mov eax,23 ; wait here for event NOTE a TIME-OUT should be placed here
mov ebx,TIMEOUT*100
int 0x40
 
eth.poll [socketNum]
; Any data in the UDP receive buffer?
mov eax, 53
mov ebx, 2
mov ecx, [socketNum]
int 0x40
 
test eax,eax
jnz ctr002
cmp eax, 0
jne ctr002
 
DEBUGF 2,"DHCP: Timeout!\n"
eth.close_udp [socketNum]
jmp apipa ; no server found, lets try zeroconf
if DEBUG = 1
dps "DHCP: Timeout!"
newline
end if
 
jmp close
 
ctr002: ; we have data - this will be the response
eth.read_packet [socketNum], dhcpMsg, BUFFER
mov [dhcpMsgLen], eax
eth.close_udp [socketNum]
; we have data - this will be the response
ctr002:
 
mov eax, 53
mov ebx, 3
mov ecx, [socketNum]
int 0x40 ; read byte - block (high byte)
 
; Store the data in the response buffer
mov eax, [dhcpMsgLen]
mov [eax], bl
inc dword [dhcpMsgLen]
 
mov eax, 53
mov ebx, 2
mov ecx, [socketNum]
int 0x40 ; any more data?
 
cmp eax, 0
jne ctr002 ; yes, so get it
 
; depending on which msg we sent, handle the response
; accordingly.
; If the response is to a dhcp discover, then:
215,34 → 222,54
; 1) If response is DHCP OFFER then
; 1.1) record server IP, lease time & IP address.
; 1.2) send a request packet
; 2) else exit ( display error )
; 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
; 2) else exit ( display error )
 
 
cmp [dhcpMsgType], byte 0x01 ; did we send a discover?
je discover
cmp [dhcpMsgType], byte 0x03 ; did we send a request?
je request
 
jmp close ; really unknown, what we did
; should never get here - we only send discover or request
jmp close
 
discover:
 
call parseResponse
 
cmp [dhcpMsgType], byte 0x02 ; Was the response an offer?
jne apipa ; NO - so we do zeroconf
; Was the response an offer? It should be
cmp [dhcpMsgType], byte 0x02
jne close ; NO - so quit
 
; send request
mov [dhcpMsgType], byte 0x03 ; DHCP request
jmp buildRequest
 
request:
 
call parseResponse
 
cmp [dhcpMsgType], byte 0x05 ; Was the response an ACK? It should be
jne apipa ; NO - so we do zeroconf
; Was the response an ACK? It should be
cmp [dhcpMsgType], byte 0x05
jne close ; NO - so quit
 
close:
DEBUGF 1,"DHCP: Exiting\n"
 
; close socket
mov eax, 53
mov ebx, 1
mov ecx, [socketNum]
int 0x40
 
if DEBUG = 1
dps "DHCP: Exiting"
newline
end if
 
mov eax,-1 ; at last, exit
int 0x40
 
261,19 → 288,47
;
;***************************************************************************
parseResponse:
DEBUGF 1,"DHCP: Data received, parsing response\n"
 
if DEBUG = 1
dps "DHCP: Data received, parsing response"
newline
end if
 
mov edx, dhcpMsg
 
pusha
eth.set_IP [edx+16]
mov eax,[edx]
mov [dhcpClientIP],eax
DEBUGF 1,"DHCP: Client: %u.%u.%u.%u\n",[edx+16]:1,[edx+17]:1,[edx+18]:1,[edx+19]:1
 
mov eax,52 ; Set Client IP
mov ebx,3
mov ecx, [edx+16]
int 0x40
 
if DEBUG = 1
dps "DHCP: Client: "
 
xor esi,esi
.loop:
 
pusha
movzx eax,byte[edx+esi+16]
call debug_outdec
popa
 
inc esi
cmp esi,4
jne .loop
 
newline
end if
 
popa
 
; Scan options
 
add edx, 240 ; Point to first option
 
pr001:
; Get option id
mov al, [edx]
cmp al, 0xff ; End of options?
je pr_exit
287,6 → 342,7
jmp pr001 ; Get next option
 
pr002:
; All other (accepted) options are 4 bytes in length
inc edx
movzx ecx, byte [edx]
inc edx ; point to data
295,7 → 351,6
jne pr0021
mov eax, [edx] ; All options are 4 bytes, so get it
mov [dhcpServerIP], eax
DEBUGF 1,"DHCP: Server: %u.%u.%u.%u\n",[edx]:1,[edx+1]:1,[edx+2]:1,[edx+3]:1
jmp pr003
 
pr0021:
302,19 → 357,20
cmp al, 51 ; lease
jne pr0022
 
if DEBUG = 1
pusha
DEBUGF 1,"DHCP: lease: "
mov eax,[edx]
bswap eax
mov [dhcpLease],eax
cmp dword[edx],-1 ; i really don't know, how to test it
dps "DHCP: lease: "
 
cmp dword[edx],-1
jne no_lease_forever
DEBUGF 1,"forever\n"
jmp @f
dps "forever"
jmp lease_newline
no_lease_forever:
DEBUGF 1,"%d\n",eax
@@:
dpd [edx]
lease_newline:
newline
popa
end if
 
jmp pr003
 
323,145 → 379,123
jne pr0023
 
pusha
eth.set_SUBNET [edx]
DEBUGF 1,"DHCP: Subnet: %u.%u.%u.%u\n",[edx]:1,[edx+1]:1,[edx+2]:1,[edx+3]:1
popa
mov eax,52
mov ebx,12
mov ecx,[edx]
int 0x40
 
jmp pr003
 
pr0023:
cmp al, 3 ; gateway ip
jne pr0024
if DEBUG = 1
dps "DHCP: Subnet: "
 
xor esi,esi
.loop:
 
pusha
eth.set_GATEWAY [edx]
DEBUGF 1,"DHCP: Gateway: %u.%u.%u.%u\n",[edx]:1,[edx+1]:1,[edx+2]:1,[edx+3]:1
movzx eax,byte[edx+esi]
call debug_outdec
popa
 
inc esi
cmp esi,4
jne .loop
 
pr0024:
cmp al, 6 ; dns ip
jne pr003
newline
end if
 
pusha
eth.set_DNS [edx]
DEBUGF 1,"DHCP: DNS: %u.%u.%u.%u\n",[edx]:1,[edx+1]:1,[edx+2]:1,[edx+3]:1
popa
 
jmp pr003
 
pr003:
add edx, ecx
jmp pr001
pr0023:
cmp al, 6 ; dns ip
jne pr0024
 
pr_exit:
pusha
 
; DEBUGF 1,"DHCP: Sending ARP probe\n"
; eth.ARP_ANNOUNCE [dhcpClientIP] ; send an ARP announc packet
mov eax,52
mov ebx,14
mov ecx,[edx]
int 0x40
 
eth.get_GATEWAY eax ; if gateway was not set, set it to the DHCP SERVER IP
test eax,eax
jnz close
eth.set_GATEWAY [dhcpServerIP]
jmp close
 
apipa:
call random
mov ecx,0xfea9 ; IP 169.254.0.0 link local net, see RFC3927
mov cx,ax
eth.set_IP ecx ; mask is 255.255.0.0
DEBUGF 1,"ZeroConf: Link Local IP assinged: 169.254.%u.%u\n",[generator+2]:1,[generator+3]:1
eth.set_SUBNET 0xffff
eth.set_GATEWAY 0x0
eth.set_DNS 0x0
if DEBUG = 1
dps "DHCP: DNS IP: "
 
mov eax,5
mov ebx,PROBE_WAIT*100
int 0x40
 
xor esi,esi
probe_loop:
call random ; create a pseudo random number in eax (seeded by MAC)
.loop:
 
cmp al,PROBE_MIN*100 ; check if al is bigger then PROBE_MIN
jge @f ; all ok
add al,(PROBE_MAX-PROBE_MIN)*100 ; al is too small
@@:
pusha
movzx eax,byte[edx+esi]
call debug_outdec
popa
 
cmp al,PROBE_MAX*100
jle @f
sub al,(PROBE_MAX-PROBE_MIN)*100
@@:
inc esi
cmp esi,4
jne .loop
 
movzx ebx,al
DEBUGF 1,"ZeroConf: Waiting %u0ms\n",ebx
mov eax,5
int 0x40
newline
end if
 
DEBUGF 1,"ZeroConf: Sending Probe\n"
; eth.ARP_PROBE MAC2
inc esi
popa
 
cmp esi,PROBE_NUM
jl probe_loop
pr0024:
cmp al, 3 ; gateway ip
jne pr003
 
; now we wait further ANNOUNCE_WAIT seconds and send ANNOUNCE_NUM ARP announces. If any other host has assingnd
; IP within this time, we should create another adress, that have to be done later
pusha
 
DEBUGF 1,"ZeroConf: Waiting %us\n",ANNOUNCE_WAIT
mov eax,5
mov ebx,ANNOUNCE_WAIT*100
mov eax,52
mov ebx,11
mov ecx,[edx]
int 0x40
 
 
if DEBUG = 1
dps "DHCP: Gateway:"
 
xor esi,esi
announce_loop:
.loop:
 
DEBUGF 1,"ZeroConf: Sending Announce\n"
; eth.ARP_ANNOUNCE MAC2
pusha
movzx eax,byte[edx+esi]
call debug_outdec
popa
 
inc esi
cmp esi,ANNOUNCE_NUM
je @f
cmp esi,4
jne .loop
 
DEBUGF 1,"ZeroConf: Waiting %us\n",ANNOUNCE_INTERVAL
mov eax,5
mov ebx,ANNOUNCE_INTERVAL*100
int 0x40
newline
end if
 
jmp announce_loop
@@:
jmp close ; we should, instead of closing, detect ARP conflicts and detect if cable keeps connected ;)
popa
 
random:
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
ret
pr003:
add edx, ecx
jmp pr001
 
pr_exit:
 
if DEBUG = 1
dps "DHCP: Done"
newline
end if
 
jmp close
 
 
; DATA AREA
 
include_debug_strings ; ALWAYS present in data section
 
IM_END:
 
dhcpClientIP dd 0
dhcpMsgType db 0
dhcpLease dd 0
dhcpServerIP dd 0
dhcpMsgType: db 0
dhcpLease: dd 0
;dhcpClientIP: dd 0
dhcpServerIP: dd 0
 
dhcpMsgLen dd 0
socketNum dd 0
dhcpMsgLen: dd 0
socketNum: dd 0xFFFF
dhcpMsg: rb 512
 
MAC rb 6
currTime dd 0
renewTime dd 0
generator dd 0
 
dhcpMsg rb BUFFER
I_END:
/programs/network/autodhcp/trunk/debug.inc
0,0 → 1,131
macro debug_print str
{
local ..string, ..label
 
jmp ..label
..string db str,0
..label:
 
pushf
pushad
mov edx,..string
call debug_outstr
popad
popf
}
 
dps fix debug_print
 
macro debug_print_dec arg
{
pushf
pushad
if ~arg eq eax
mov eax,arg
end if
call debug_outdec
popad
popf
}
 
dpd fix debug_print_dec
 
;---------------------------------
debug_outdec: ;(eax - num, edi-str)
push 10 ;2
pop ecx ;1
push -'0' ;2
.l0:
xor edx,edx ;2
div ecx ;2
push edx ;1
test eax,eax ;2
jnz .l0 ;2
.l1:
pop eax ;1
add al,'0' ;2
call debug_outchar ; stosb
jnz .l1 ;2
ret ;1
;---------------------------------
 
debug_outchar: ; al - char
pushf
pushad
mov cl,al
mov eax,63
mov ebx,1
int 0x40
popad
popf
ret
 
debug_outstr:
mov eax,63
mov ebx,1
@@:
mov cl,[edx]
test cl,cl
jz @f
int 40h
inc edx
jmp @b
@@:
ret
 
 
macro newline
{
dps <13,10>
}
 
macro print message
{
dps message
newline
}
 
macro pregs
{
dps "EAX: "
dpd eax
dps " EBX: "
dpd ebx
newline
dps "ECX: "
dpd ecx
dps " EDX: "
dpd edx
newline
}
 
macro debug_print_hex arg
{
pushf
pushad
if ~arg eq eax
mov eax, arg
end if
call debug_outhex
popad
popf
}
dph fix debug_print_hex
 
debug_outhex:
; eax - number
mov edx, 8
.new_char:
rol eax, 4
movzx ecx, al
and cl, 0x0f
mov cl, [__hexdigits + ecx]
pushad
mcall 63, 1
popad
dec edx
jnz .new_char
ret
 
__hexdigits:
db '0123456789ABCDEF'