Subversion Repositories Kolibri OS

Compare Revisions

Regard whitespace Rev 1009 → Rev 1008

/programs/network/autodhcp/trunk/zeroconf.ini
File deleted
\ No newline at end of file
/programs/network/autodhcp/trunk/dll.inc
File deleted
/programs/network/autodhcp/trunk/zeroconf.asm
File deleted
\ No newline at end of file
/programs/network/autodhcp/trunk/ETH.INC
1,7 → 1,7
;
; ETH.INC
;
; made by hidnplayr (hidnplayr@kolibrios.org) for KolibriOS
; made by hidnplayr (hidnplayr@gmail.com) for KolibriOS
;
; The given code before every macro is only a simple example
;
10,7 → 10,7
;
; v1.0: august 2006 original release
; v1.1: december 2006 bugfixes and improvements
; v1.2: february 2007 more bugfixes and improvements
; v1.2: februari 2007 more bugfixes and improvements
 
macro mov arg1,arg2 {
if arg1 eq arg2
339,12 → 339,51
}
 
 
; The function 'resolve' resolves the address in edx and puts the resulting IP in eax.
; When the input is an IP-adress, the function will output this IP in eax.
; If something goes wrong, the result in eax should be 0
;
; example:
;
; resolve query1,IP,PORT
; resolve '192.168.0.1',IP,PORT
; resolve query2,IP,PORT
;
; query1 db 'www.google.com',0
; query2 db '49.78.84.45',0
; IP dd ?
; PORT dd ?
 
Ip2dword:
push edx
macro resolve query,result {
 
if query eqtype 0
mov edx,query
else
local ..string, ..label
jmp ..label
..string db query,0
..label:
mov edx,..string
end if
 
call __resolve
 
mov result,eax
 
}
 
if used __resolve
 
__resolve:
 
if __DEBUG__ eq 1
DEBUGF 1,'DNS: Resolving started\n'
end if
 
; This code validates if the query is an IP containing 4 numbers and 3 dots
 
 
push edx ; push edx (query address) onto stack
xor al, al ; make al (dot count) zero
 
@@:
369,7 → 408,7
 
@@: ; 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
jnz no_IP ; if not, jump to no_IP (this is where the DNS will take over)
 
; 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
399,15 → 438,189
add edx, ebx
 
bswap edx ; we want little endian order
mov eax, edx
 
ret
 
 
no_IP:
 
pop edx
xor edx, edx
 
; The query is not an IP address, we will send the query to a DNS server and hope for answer ;)
if __DEBUG__ eq 1
DEBUGF 1,'DNS: The query is no ip, Building request string from:%u\n',edx
end if
 
; Build the request string
mov eax, 0x00010100
mov [dnsMsg], eax
mov eax, 0x00000100
mov [dnsMsg+4], eax
mov eax, 0x00000000
mov [dnsMsg+8], eax
 
; domain name goes in at dnsMsg+12
mov esi, dnsMsg + 12 ; location of label length
mov edi, dnsMsg + 13 ; label start
mov ecx, 12 ; total string length so far
 
td002:
mov [esi], byte 0
inc ecx
 
td0021:
mov al, [edx]
 
cmp al, 0
je td001 ; we have finished the string translation
 
cmp al, '.'
je td004 ; we have finished the label
 
inc byte [esi]
inc ecx
mov [edi], al
inc edi
inc edx
jmp td0021
 
td004:
mov esi, edi
inc edi
inc edx
jmp td002
 
; write label len + label text
td001:
mov [edi], byte 0
inc ecx
inc edi
mov [edi], dword 0x01000100
add ecx, 4
 
mov [dnsMsgLen], ecx ; We'll need the length of the message when we send it
; Now, lets send this and wait for an answer
 
eth.search_port 1024,edx ; Find a free port starting from 1025 and store in edx
eth.get_DNS esi ; Read DNS IP from stack into esi
eth.open_udp edx,53,esi,[socketNum] ; First, open socket
if __DEBUG__ eq 1
DEBUGF 1,'DNS: Socket opened: %u (port %u)\n',[socketNum],ecx
end if
eth.write_udp [socketNum],[dnsMsgLen],dnsMsg ; Write to socket ( request DNS lookup )
if __DEBUG__ eq 1
DEBUGF 1,'DNS: Data written, length:%u offset:%u\n',[dnsMsgLen],dnsMsg
DEBUGF 1,'DNS: Waiting for data: (timeout is %us)\n',TIMEOUT
end if
eth.wait_for_data [socketNum],TIMEOUT,abort ; Now, we wait for data from remote
eth.read_data dword[socketNum],dnsMsg,dword[dnsMsgLen],dnsMsg+BUFFER ; Read the data into the buffer
if __DEBUG__ eq 1
DEBUGF 1,'Data received, offset:%u buffer size:%u length:%u\n',dnsMsg,BUFFER,esi-dnsMsg
end if
eth.close_udp [socketNum] ; We're done, close the socket
if __DEBUG__ eq 1
DEBUGF 1,'Closed Socket\n'
end if
 
; Now parse the message to get the host IP. Man, this is complicated. It's described in RFC 1035
; 1) Validate that we have an answer with > 0 responses
; 2) Find the answer record with TYPE 0001 ( host IP )
; 3) Finally, copy the IP address to the display
; Note: The response is in dnsMsg, the end of the buffer is pointed to by [dnsMsgLen]
 
mov esi, dnsMsg
 
mov al, [esi+2] ; Is this a response to my question?
and al, 0x80
cmp al, 0x80
jne abort
if __DEBUG__ eq 1
DEBUGF 1,'DNS: It was a response to my question\n'
end if
 
mov al, [esi+3] ; Were there any errors?
and al, 0x0F
cmp al, 0x00
jne abort
 
if __DEBUG__ eq 1
DEBUGF 1,'DNS: There were no errors\n'
end if
 
mov ax, [esi+6] ; Is there ( at least 1 ) answer?
cmp ax, 0x00
je abort
 
; Header validated. Scan through and get my answer
add esi, 12 ; Skip to the question field
call skipName ; Skip through the question field
add esi, 4 ; skip past the questions qtype, qclass
 
ctr002z:
; Now at the answer. There may be several answers, find the right one ( TYPE = 0x0001 )
call skipName
mov ax, [esi]
cmp ax, 0x0100 ; Is this the IP address answer?
jne ctr002c
add esi, 10 ; Yes! Point eax to the first byte of the IP address
mov eax,[esi]
 
ret
 
 
ctr002c: ; Skip through the answer, move to the next
add esi, 8
movzx eax, byte [esi+1]
mov ah, [esi]
add esi, eax
add esi, 2
 
cmp esi, [dnsMsgLen] ; Have we reached the end of the msg? This is an error condition, should not happen
jl ctr002z ; Check next answer
 
abort:
if __DEBUG__ eq 1
DEBUGF 1,'DNS: Something went wrong, aborting\n'
end if
xor eax,eax
 
ret
 
 
skipName:
; Increment esi to the first byte past the name field
; Names may use compressed labels. Normally do.
; RFC 1035 page 30 gives details
mov al, [esi]
cmp al, 0
je sn_exit
and al, 0xc0
cmp al, 0xc0
je sn001
 
movzx eax, byte [esi]
inc eax
add esi, eax
jmp skipName
 
sn001:
add esi, 2 ; A pointer is always at the end
ret
 
sn_exit:
inc esi
ret
 
dnsMsgLen: dd 0
socketNum: dd 0xFFFF
 
if ~defined dnsMsg
dnsMsg: rb BUFFER
end if
 
end if
 
 
 
 
/programs/network/autodhcp/trunk/autodhcp.asm
0,0 → 1,411
; Automated dhcp client
; v 1.3
;
; with thanks to authors of DHCP client for menuetos: Mike Hibbet
;
; by HidnPlayr & Derpenguin
 
use32
org 0x0
 
db 'MENUET01' ; 8 byte id
dd 0x01 ; header version
dd START ; start of code
dd IM_END ; size of image
dd I_END ; memory for app
dd I_END ; esp
dd 0x0 , 0x0 ; I_Param , I_Icon
 
; CONFIGURATION
 
 
TIMEOUT equ 60 ; in seconds
BUFFER equ 1024 ; in bytes
__DEBUG__ equ 1 ; enable/disable
__DEBUG_LEVEL__ equ 1 ; 1 = all, 2 = errors
 
; 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)
 
include '..\..\..\macros.inc'
include 'eth.inc'
include 'debug-fdo.inc'
include 'dhcp.inc'
include 'events.inc'
include 'common.inc'
 
 
START: ; start of execution
 
set_event_mask event_network
eth.set_network_drv 0x00000383
 
DEBUGF 1,"Stack Initialized.\n"
 
eth.status eax ; Read the Stack status
test eax,eax ; if eax is zero, no driver was found
jnz @f
DEBUGF 1,"No Card detected\n"
jmp close
 
@@:
DEBUGF 1,"Detected card: %x\n",eax
@@:
eth.check_cable eax
test al,al
jnz @f
DEBUGF 1,"Ethernet Cable not connected\n"
wait 500 ; loop until cable is connected (check every 5 sec)
jmp @r
 
@@:
DEBUGF 1,"Ethernet Cable status: %d\n",al
 
eth.read_mac MAC
DEBUGF 1,"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
 
eth.check_port 68,eax ; Check if port 68 is available
cmp eax,1
je @f
 
DEBUGF 1,"Port 68 is already in use!\n"
jmp close
 
@@:
eth.open_udp 68,67,-1,[socketNum] ; open socket (local,remote,ip,socket)
DEBUGF 1,"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)
 
get_time_counter eax
imul eax,100
mov [currTime],eax
 
buildRequest: ; Creates a DHCP request packet.
xor eax,eax ; Clear dhcpMsg to all zeros
mov edi,dhcpMsg
mov ecx,BUFFER
cld
rep stosb
 
mov edx, dhcpMsg
 
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
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
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
mov eax, [dhcpClientIP]
mov [edx+240+11], eax
mov [edx+240+15], word 0x0437 ; option request list
mov [edx+240+17], dword 0x0f060301
 
cmp [dhcpMsgType], byte 0x01 ; Check which msg we are sending
jne request_options
 
mov [edx+240+21], byte 0xff ; "Discover" options
 
mov [dhcpMsgLen], dword 262 ; end of options marker
jmp send_request
 
request_options:
mov [edx+240+21], word 0x0436 ; server IP
mov eax, [dhcpServerIP]
mov [edx+240+23], eax
 
mov [edx+240+27], byte 0xff ; end of options marker
 
mov [dhcpMsgLen], dword 268
 
send_request:
eth.write_udp [socketNum],[dhcpMsgLen],dhcpMsg ; write to socket ( send broadcast request )
 
mov eax, dhcpMsg ; Setup the DHCP buffer to receive response
mov [dhcpMsgLen], eax ; Used as a pointer to the data
 
mov eax,23 ; wait here for event (data from remote)
mov ebx,TIMEOUT*10
mcall
 
eth.poll [socketNum]
 
test eax,eax
jnz read_data
 
DEBUGF 2,"Timeout!\n"
eth.close_udp [socketNum]
jmp apipa ; no server found, lets try zeroconf
 
 
read_data: ; we have data - this will be the response
eth.read_packet [socketNum], dhcpMsg, BUFFER
mov [dhcpMsgLen], eax
eth.close_udp [socketNum]
 
; 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], 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
 
discover:
call parseResponse
 
cmp [dhcpMsgType], byte 0x02 ; Was the response an offer?
jne apipa ; NO - so we do zeroconf
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
 
jmp close
 
;***************************************************************************
; 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
;
;***************************************************************************
parseResponse:
DEBUGF 1,"Data received, parsing response\n"
mov edx, dhcpMsg
 
pusha
eth.set_IP [edx+16]
mov eax,[edx]
mov [dhcpClientIP],eax
DEBUGF 1,"Client: %u.%u.%u.%u\n",[edx+16]:1,[edx+17]:1,[edx+18]:1,[edx+19]:1
popa
 
add edx, 240 ; Point to first option
xor ecx, ecx
 
next_option:
add edx, ecx
pr001:
mov al, [edx]
cmp al, 0xff ; End of options?
je pr_exit
 
cmp al, dhcp_msg_type ; Msg type is a single byte option
jne @f
 
mov al, [edx+2]
mov [dhcpMsgType], al
add edx, 3
jmp pr001 ; Get next option
 
@@:
inc edx
movzx ecx, byte [edx]
inc edx ; point to data
 
cmp al, dhcp_dhcp_server_id ; server ip
jne @f
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
 
@@:
cmp al, dhcp_address_time
jne @f
 
pusha
mov eax,[edx]
bswap eax
mov [dhcpLease],eax
DEBUGF 1,"lease: %d\n",eax
popa
 
jmp next_option
 
@@:
cmp al, dhcp_subnet_mask
jne @f
 
pusha
eth.set_SUBNET [edx]
DEBUGF 1,"Subnet: %u.%u.%u.%u\n",[edx]:1,[edx+1]:1,[edx+2]:1,[edx+3]:1
popa
 
jmp next_option
 
@@:
cmp al, dhcp_router
jne @f
 
pusha
eth.set_GATEWAY [edx]
DEBUGF 1,"Gateway: %u.%u.%u.%u\n",[edx]:1,[edx+1]:1,[edx+2]:1,[edx+3]:1
popa
 
jmp next_option
 
 
@@:
cmp al, dhcp_domain_server
jne next_option
 
pusha
eth.set_DNS [edx]
DEBUGF 1,"DNS: %u.%u.%u.%u\n",[edx]:1,[edx+1]:1,[edx+2]:1,[edx+3]:1
popa
 
jmp next_option
 
pr_exit:
 
; DEBUGF 1,"Sending ARP announce\n"
; eth.ARP_ANNOUNCE [dhcpClientIP] ; send an ARP announce packet
 
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,"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
 
wait PROBE_WAIT*100
 
xor esi,esi
probe_loop:
call random ; create a pseudo random number in eax (seeded by MAC)
 
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
@@:
 
cmp al,PROBE_MAX*100
jle @f
sub al,(PROBE_MAX-PROBE_MIN)*100
@@:
 
movzx ebx,al
DEBUGF 1,"Waiting %u0ms\n",ebx
wait ebx
 
DEBUGF 1,"Sending Probe\n"
; eth.ARP_PROBE MAC
inc esi
 
cmp esi,PROBE_NUM
jl probe_loop
 
; 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
 
DEBUGF 1,"Waiting %us\n",ANNOUNCE_WAIT
wait ANNOUNCE_WAIT*100
xor esi,esi
announce_loop:
 
DEBUGF 1,"Sending Announce\n"
; eth.ARP_ANNOUNCE MAC
 
inc esi
cmp esi,ANNOUNCE_NUM
je @f
 
DEBUGF 1,"Waiting %us\n",ANNOUNCE_INTERVAL
wait ANNOUNCE_INTERVAL*100
jmp announce_loop
@@:
; we should, instead of closing, detect ARP conflicts and detect if cable keeps connected ;)
 
close:
DEBUGF 1,"Exiting\n"
exit ; at last, exit
 
 
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
 
; DATA AREA
 
include_debug_strings
 
IM_END:
 
dhcpClientIP dd 0
dhcpMsgType db 0
dhcpLease dd 0
dhcpServerIP dd 0
 
dhcpMsgLen dd 0
socketNum dd 0
 
MAC rb 6
currTime dd 0
renewTime dd 0
generator dd 0
 
dhcpMsg rb BUFFER
I_END:
/programs/network/autodhcp/trunk/common.inc
0,0 → 1,17
macro wait time {
mov ebx,time
mov eax,5
mcall
}
 
macro get_time_counter result {
mov eax,26
mov ebx,9
mcall
mov result,eax
}
 
macro exit {
or eax,-1
mcall
}
/programs/network/autodhcp/trunk/events.inc
0,0 → 1,49
 
macro set_event_mask mask {
mov ebx, mask
mov eax, 40
mcall
}
 
macro wait_for_event {
mov eax, 10
mcall
}
 
macro check_for_event {
mov eax, 11
mcall
}
 
macro wait_for_event_timeout timeout {
mov ebx,timeout
mov eax, 23
mcall
}
 
 
event_redraw equ 1 shl 0
event_keyboard equ 1 shl 1
event_button equ 1 shl 2
event_background equ 1 shl 4
event_mouse equ 1 shl 5
event_ipc equ 1 shl 6
event_network equ 1 shl 7
event_debug equ 1 shl 8
event_irq0 equ 1 shl 15
event_irq1 equ 1 shl 16
event_irq2 equ 1 shl 17
event_irq3 equ 1 shl 18
event_irq4 equ 1 shl 19
event_irq5 equ 1 shl 20
event_irq6 equ 1 shl 21
event_irq7 equ 1 shl 22
event_irq8 equ 1 shl 23
event_irq9 equ 1 shl 24
event_irq10 equ 1 shl 25
event_irq11 equ 1 shl 26
event_irq12 equ 1 shl 27
event_irq13 equ 1 shl 28
event_irq14 equ 1 shl 29
event_irq15 equ 1 shl 30