199,31 → 199,33 |
|
DEBUGF 1,"Connected to 255.255.255.255 on port 67\n" |
|
mov [dhcpMsgType], 0x01 ; DHCP discover |
mov [dhcpMsgType_tx], 0x01 ; DHCP discover |
mov [dhcpLease], esi ; esi is still -1 (-1 = forever) |
|
call random |
mov [dhcpXID], eax |
|
build_request: ; Creates a DHCP request packet. |
|
DEBUGF 1,"Building request\n" |
|
mcall 26, 9 ; Get system time |
imul eax, 100 |
mov [currTime], eax |
|
build_request: ; Creates a DHCP request packet. |
|
mov [tries], DHCP_TRIES |
|
DEBUGF 1,"Building request\n" |
|
stdcall mem.Alloc, BUFFER |
test eax, eax |
jz dhcp_fail2 |
mov [dhcpMsg], eax |
|
; Fill buffer with zeros |
mov edi, eax |
mov ecx, BUFFER |
xor eax, eax |
rep stosb |
|
;; todo: put this in one buffer we can copy, instead of writing bytes and words! |
|
mov edx, [dhcpMsg] |
|
; Boot protocol legacy |
230,7 → 232,8 |
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 ;;;;;;; FIXME |
mov eax, [dhcpXID] |
mov [edx+4], eax ; xid |
mov eax, [currTime] |
mov [edx+8], eax ; secs, our uptime |
mov [edx+10], byte 0x80 ; broadcast flag set |
242,7 → 245,7 |
; DHCP extension |
mov [edx+236], dword 0x63538263 ; magic cookie |
mov [edx+240], word 0x0135 ; option DHCP msg type |
mov al, [dhcpMsgType] |
mov al, [dhcpMsgType_tx] |
mov [edx+240+2], al |
mov [edx+240+3], word 0x0433 ; option Lease time = infinity |
mov eax, [dhcpLease] |
253,8 → 256,8 |
mov [edx+240+15], word 0x0437 ; option request list |
mov [edx+240+17], dword 0x0f060301 |
|
cmp [dhcpMsgType], 0x01 ; Check which msg we are sending |
jne request_options |
cmp [dhcpMsgType_tx], 0x01 ; Check which msg we are sending |
jne .options |
|
mov [edx+240+21], byte 0xff ; end of options marker |
|
261,7 → 264,7 |
mov [dhcpMsgLen], 262 ; length |
jmp send_dhcpmsg |
|
request_options: |
.options: |
mov [edx+240+21], word 0x0436 ; server IP |
mov eax, [dhcpServerIP] |
mov [edx+240+23], eax |
273,16 → 276,18 |
send_dhcpmsg: |
DEBUGF 1,"Sending DHCP discover/request\n" |
mcall 75, 6, [socketNum], [dhcpMsg], [dhcpMsgLen] ; write to socket (send broadcast request) |
|
; Wait for data |
mcall 26, 9 |
add eax, TIMEOUT*100 |
mov [timeout], eax |
.wait: |
mcall 23, TIMEOUT ; wait for data (with timeout) |
mcall 23, TIMEOUT |
|
read_data: ; we have data - this will be the response |
mcall 75, 7, [socketNum], [dhcpMsg], BUFFER, MSG_DONTWAIT ; read data from socket |
cmp eax, -1 |
jne @f |
jne .got_data |
|
mcall 26, 9 |
cmp eax, [timeout] |
291,9 → 296,10 |
DEBUGF 2,"No answer from DHCP server\n" |
dec [tries] |
jnz send_dhcpmsg ; try again |
stdcall mem.Free, [dhcpMsg] |
jmp dhcp_fail |
|
@@: |
.got_data: |
DEBUGF 1,"%d bytes received\n", eax |
mov [dhcpMsgLen], eax |
|
307,29 → 313,28 |
; 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 |
cmp [dhcpMsgType_tx], 0x01 ; did we send a discover? |
je discover_sent |
|
cmp [dhcpMsgType], 0x03 ; did we send a request? |
je request |
cmp [dhcpMsgType_tx], 0x03 ; did we send a request? |
je request_sent |
|
; we should never reach here ;) |
stdcall mem.Free, [dhcpMsg] |
jmp fail |
|
discover: |
discover_sent: |
call parse_response |
cmp [dhcpMsgType_rx], 0x02 ; Was the response an offer? |
jne read_data |
|
cmp [dhcpMsgType2], 0x02 ; Was the response an offer? |
jne dhcp_fail |
|
DEBUGF 1, "Got offer, making request\n" |
mov [dhcpMsgType], 0x03 ; make it a request |
mov [dhcpMsgType_tx], 0x03 ; make it a request |
jmp build_request |
|
request: |
request_sent: |
call parse_response |
|
cmp [dhcpMsgType2], 0x05 ; Was the response an ACK? It should be |
cmp [dhcpMsgType_rx], 0x05 ; Was the response an ACK? It should be |
jne read_data ; NO - read next packets |
|
DEBUGF 2, "IP assigned by DHCP server successfully\n" |
336,8 → 341,9 |
|
mov [notify_struct.msg], str_connected |
mcall 70, notify_struct |
call dhcp_fail |
|
mcall close, [socketNum] |
|
mov ebx, API_IPv4 + 3 |
mov bh, [device] |
mcall 76, , [dhcp.ip] ; ip |
366,10 → 372,15 |
;*************************************************************************** |
parse_response: |
|
DEBUGF 1,"Data received, parsing response\n" |
DEBUGF 1,"Parsing response\n" |
mov edx, [dhcpMsg] |
mov [dhcpMsgType2], 0 |
mov [dhcpMsgType_rx], 0 |
|
; Verify if session ID matches |
mov eax, [dhcpXID] |
cmp dword[edx+4], eax |
jne .done |
|
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 |
425,7 → 436,7 |
|
.msgtype: |
mov al, [edx] |
mov [dhcpMsgType2], al |
mov [dhcpMsgType_rx], al |
|
DEBUGF 1,"DHCP Msg type: %u\n", al |
jmp .next_option ; Get next option |
464,16 → 475,18 |
jmp .next_option |
|
.done: |
stdcall mem.Free, [dhcpMsg] |
ret |
|
|
dhcp_fail: |
|
mcall close, [socketNum] |
stdcall mem.Free, [dhcpMsg] |
|
dhcp_fail2: |
DEBUGF 1,"DHCP failed\n" |
|
|
link_local: |
call random |
mov cx, ax |
680,6 → 693,7 |
db '/sys/@notify', 0 |
|
str_connected db '"You are now connected to the network." -N', 0 |
|
path db '/sys/settings/network.ini',0 |
|
IM_END: |
688,8 → 702,9 |
inibuf rb 16 |
tries db ? |
|
dhcpMsgType db ? ; sent |
dhcpMsgType2 db ? ; received |
dhcpMsgType_tx db ? ; sent |
dhcpMsgType_rx db ? ; received |
dhcpXID dd ? |
dhcpLease dd ? |
dhcpServerIP dd ? |
|