Subversion Repositories Kolibri OS

Rev

Rev 2995 | Go to most recent revision | Blame | Last modification | View Log | Download | RSS feed

  1. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  2. ;;                                                                 ;;
  3. ;; Copyright (C) KolibriOS team 2010-2013. All rights reserved.    ;;
  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                      ;;
  12. ;       (DHCP client for menuetos)                                 ;;
  13. ;;                                                                 ;;
  14. ;;          GNU GENERAL PUBLIC LICENSE                             ;;
  15. ;;             Version 2, June 1991                                ;;
  16. ;;                                                                 ;;
  17. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  18.  
  19. format binary as ""
  20.  
  21. use32
  22.                org    0x0
  23.  
  24.                db     'MENUET01'            ; 8 byte id
  25.                dd     0x01                  ; header version
  26.                dd     START                 ; start of code
  27.                dd     IM_END                ; size of image
  28.                dd     (I_END+0x100)         ; memory for app
  29.                dd     (I_END+0x100)         ; esp
  30.                dd     0x0 , path            ; I_Param , I_Icon
  31.  
  32. ; CONFIGURATION
  33.  
  34. TIMEOUT             equ 60                  ; in seconds
  35. BUFFER              equ 1024                ; in bytes
  36. __DEBUG__           equ 1                   ; enable/disable
  37. __DEBUG_LEVEL__     equ 1                   ; 1 = all, 2 = errors
  38.  
  39. ; CONFIGURATION FOR LINK-LOCAL
  40.  
  41. PROBE_WAIT          equ 1                   ; second  (initial random delay)
  42. PROBE_MIN           equ 1                   ; second  (minimum delay till repeated probe)
  43. PROBE_MAX           equ 2                   ; seconds (maximum delay till repeated probe)
  44. PROBE_NUM           equ 3                   ;         (number of probe packets)
  45.  
  46. ANNOUNCE_NUM        equ 2                   ;         (number of announcement packets)
  47. ANNOUNCE_INTERVAL   equ 2                   ; seconds (time between announcement packets)
  48. ANNOUNCE_WAIT       equ 2                   ; seconds (delay before announcing)
  49.  
  50. MAX_CONFLICTS       equ 10                  ;         (max conflicts before rate limiting)
  51.  
  52. RATE_LIMIT_INTERVAL equ 60                  ; seconds (delay between successive attempts)
  53.  
  54. DEFEND_INTERVAL     equ 10                  ; seconds (min. wait between defensive ARPs)
  55.  
  56.  
  57. include '../proc32.inc'
  58. include '../macros.inc'
  59. include '../debug-fdo.inc'
  60. include '../network.inc'
  61. include 'dhcp.inc'
  62. include '../dll.inc'
  63.  
  64.  
  65. Ip2dword:
  66.     push    edx
  67.  
  68.     ; This code validates if the query is an IP containing 4 numbers and 3 dots
  69.  
  70.     xor     al, al            ; make al (dot count) zero
  71.  
  72.    @@:
  73.     cmp     byte[edx],'0'     ; check if this byte is a number, if not jump to no_IP
  74.     jl      no_IP             ;
  75.     cmp     byte[edx],'9'     ;
  76.     jg      no_IP             ;
  77.  
  78.     inc     edx               ; the byte was a number, so lets check the next byte
  79.  
  80.     cmp     byte[edx],0       ; is this byte zero? (have we reached end of query?)
  81.     jz      @f                ; jump to next @@ then
  82.     cmp     byte[edx],':'
  83.     jz      @f
  84.  
  85.     cmp     byte[edx],'.'     ; is this byte a dot?
  86.     jne     @r                ; if not, jump to previous @@
  87.  
  88.     inc     al                ; the byte was a dot so increment al(dot count)
  89.     inc     edx               ; next byte
  90.     jmp     @r                ; lets check for numbers again (jump to previous @@)
  91.  
  92.    @@:                        ; we reach this when end of query reached
  93.     cmp     al,3              ; check if there where 3 dots
  94.     jnz     no_IP             ; if not, jump to no_IP
  95.  
  96.     ; The following code will convert this IP into a dword and output it in eax
  97.     ; If there is also a port number specified, this will be returned in ebx, otherwise ebx is -1
  98.  
  99.     pop     esi               ; edx (query address) was pushed onto stack and is now popped in esi
  100.  
  101.     xor     edx, edx          ; result
  102.     xor     eax, eax          ; current character
  103.     xor     ebx, ebx          ; current byte
  104.  
  105.   .outer_loop:
  106.     shl     edx, 8
  107.     add     edx, ebx
  108.     xor     ebx, ebx
  109.   .inner_loop:
  110.     lodsb
  111.     test    eax, eax
  112.     jz      .finish
  113.     cmp     al, '.'
  114.     jz      .outer_loop
  115.     sub     eax, '0'
  116.     imul    ebx, 10
  117.     add     ebx, eax
  118.     jmp     .inner_loop
  119.   .finish:
  120.     shl     edx, 8
  121.     add     edx, ebx
  122.  
  123.     bswap   edx               ; we want little endian order
  124.  
  125.     ret
  126.  
  127. no_IP:
  128.     pop     edx
  129.     xor     edx, edx
  130.  
  131.     ret
  132.  
  133.  
  134.  
  135.  
  136.  
  137.  
  138. START:
  139.  
  140.         mcall   40, EVM_STACK   ; network event
  141.  
  142.         DEBUGF  1,">Zero-config service loaded\n"
  143.  
  144.         mcall   76, API_ETH + 4 ; get MAC of ethernet interface 0
  145.         cmp     eax, -1
  146.         je      exit
  147.  
  148.         mov     word[MAC], bx
  149.         mov     dword[MAC+2], eax
  150.  
  151.         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
  152.  
  153.         mov     edi, path       ; Calculate the length of zero-terminated string
  154.         xor     al, al
  155.         mov     ecx, 1024
  156.         repne   scasb
  157.         dec     edi
  158.  
  159.         mov     esi, filename   ; append with .ini
  160.         movsd
  161.         movsb
  162.  
  163.         DEBUGF  1,"->Loading ini %s\n", path
  164.  
  165.         mcall   68, 11
  166.  
  167.         stdcall dll.Load,@IMPORT
  168.         or      eax, eax
  169.         jnz     try_dhcp
  170.  
  171.         invoke  ini.get_str, path, str_ipconfig, str_type, inibuf, 16, 0
  172.  
  173.         cmp     dword[inibuf], 'stat'
  174.         jne     try_dhcp
  175.  
  176.         invoke  ini.get_str, path, str_ipconfig, str_ip, inibuf, 16, 0
  177.         mov     edx, inibuf
  178.         call    Ip2dword
  179.         mcall   76, API_IPv4 + 3, edx
  180.  
  181.         invoke  ini.get_str, path, str_ipconfig, str_gateway, inibuf, 16, 0
  182.         mov     edx, inibuf
  183.         call    Ip2dword
  184.         mcall   76, API_IPv4 + 9, edx
  185.  
  186.         invoke  ini.get_str, path, str_ipconfig, str_dns, inibuf, 16, 0
  187.         mov     edx, inibuf
  188.         call    Ip2dword
  189.         mcall   76, API_IPv4 + 5, edx
  190.  
  191.         invoke  ini.get_str, path, str_ipconfig, str_subnet, inibuf, 16, 0
  192.         mov     edx, inibuf
  193.         call    Ip2dword
  194.         mcall   76, API_IPv4 + 7, edx
  195.  
  196.  
  197.         mcall   -1
  198.  
  199.  
  200. try_dhcp:
  201.  
  202.         DEBUGF  1,"->Trying DHCP\n"
  203.  
  204.         mcall   75, 0, AF_INET4, SOCK_DGRAM, 0          ; open socket (parameters: domain, type, reserved)
  205.         cmp     eax, -1
  206.         je      error
  207.         mov     [socketNum], eax
  208.  
  209.         DEBUGF  1,"->Socket %x opened\n", eax
  210.  
  211.         mcall   75, 2, [socketNum], sockaddr1, 18       ; bind socket to local port 68
  212.         cmp     eax, -1
  213.         je      error
  214.  
  215.         DEBUGF  1,"->Socket Bound to local port 68\n"
  216.  
  217.         mcall   75, 4, [socketNum], sockaddr2, 18       ; connect to 255.255.255.255 on port 67
  218.         cmp     eax, -1
  219.         je      error
  220.  
  221.         DEBUGF  1,"->Connected to 255.255.255.255 on port 67\n"
  222.  
  223.         mov     [dhcpMsgType], 0x01                     ; DHCP discover
  224.         mov     [dhcpLease], esi                        ; esi is still -1 (-1 = forever)
  225.  
  226.         mcall   26, 9                                   ; Get system time
  227.         imul    eax, 100
  228.         mov     [currTime], eax
  229.  
  230. build_request:                                          ; Creates a DHCP request packet.
  231.  
  232.         DEBUGF  1,"->Building request\n"
  233.  
  234.         stdcall mem.Alloc, BUFFER
  235.         mov     [dhcpMsg], eax
  236.         test    eax, eax
  237.         jz      apipa
  238.  
  239.             ;;; todo: skip this bullcrap
  240.  
  241.         mov     edi, eax
  242.         mov     ecx, BUFFER
  243.         xor     eax, eax
  244.         rep     stosb
  245.  
  246.             ;; todo: put this in a buffer instead of writing bytes and words!
  247.  
  248.         mov     edx, [dhcpMsg]
  249.  
  250.         mov     [edx], byte 0x01                ; Boot request
  251.         mov     [edx+1], byte 0x01              ; Ethernet
  252.         mov     [edx+2], byte 0x06              ; Ethernet h/w len
  253.         mov     [edx+4], dword 0x11223344       ; xid                 ;;;;;;;
  254.         mov     eax, [currTime]
  255.         mov     [edx+8], eax                    ; secs, our uptime
  256.         mov     [edx+10], byte 0x80             ; broadcast flag set
  257.         mov     eax, dword [MAC]                ; first 4 bytes of MAC
  258.         mov     [edx+28],dword eax
  259.         mov     ax, word [MAC+4]                ; last 2 bytes of MAC
  260.         mov     [edx+32],word ax
  261.         mov     [edx+236], dword 0x63538263     ; magic cookie
  262.         mov     [edx+240], word 0x0135          ; option DHCP msg type
  263.         mov     al, [dhcpMsgType]
  264.         mov     [edx+240+2], al
  265.         mov     [edx+240+3], word 0x0433        ; option Lease time = infinity
  266.         mov     eax, [dhcpLease]
  267.         mov     [edx+240+5], eax
  268.         mov     [edx+240+9], word 0x0432        ; option requested IP address
  269.         mov     eax, [dhcp.ip]
  270.         mov     [edx+240+11], eax
  271.         mov     [edx+240+15], word 0x0437       ; option request list
  272.         mov     [edx+240+17], dword 0x0f060301
  273.  
  274.         cmp     [dhcpMsgType], byte 0x01        ; Check which msg we are sending
  275.         jne     request_options
  276.  
  277.         mov     [edx+240+21], byte 0xff         ; "Discover" options
  278.  
  279.         mov     [dhcpMsgLen], dword 262         ; end of options marker
  280.         jmp     send_dhcpmsg
  281.  
  282. request_options:
  283.         mov     [edx+240+21], word 0x0436       ; server IP
  284.         mov     eax, [dhcpServerIP]
  285.         mov     [edx+240+23], eax
  286.  
  287.         mov     [edx+240+27], byte 0xff         ; end of options marker
  288.  
  289.         mov     [dhcpMsgLen], dword 268
  290.  
  291. send_dhcpmsg:
  292.         mcall   75, 6, [socketNum], [dhcpMsg], [dhcpMsgLen]     ; write to socket ( send broadcast request )
  293.  
  294.         mov     eax, [dhcpMsg]                          ; Setup the DHCP buffer to receive response
  295.         mov     [dhcpMsgLen], eax                       ; Used as a pointer to the data
  296.  
  297.         mcall   23, TIMEOUT*10                          ; wait for data
  298.  
  299. read_data:                                              ; we have data - this will be the response
  300.         mcall   75, 7, [socketNum], [dhcpMsg], BUFFER   ; read data from socket
  301.  
  302.         DEBUGF  1,"->%d bytes received\n", eax
  303.  
  304.         cmp     eax, -1
  305.         je      error
  306.  
  307.         mov     [dhcpMsgLen], eax
  308.  
  309. ; depending on which msg we sent, handle the response
  310. ; accordingly.
  311. ; If the response is to a dhcp discover, then:
  312. ;  1) If response is DHCP OFFER then
  313. ;  1.1) record server IP, lease time & IP address.
  314. ;  1.2) send a request packet
  315. ; If the response is to a dhcp request, then:
  316. ;  1) If the response is DHCP ACK then
  317. ;  1.1) extract the DNS & subnet fields. Set them in the stack
  318.  
  319.         cmp     [dhcpMsgType], 0x01             ; did we send a discover?
  320.         je      discover
  321.  
  322.         cmp     [dhcpMsgType], 0x03             ; did we send a request?
  323.         je      request
  324.  
  325.         call    dhcp_end                        ; we should never reach here ;)
  326.         jmp     exit
  327.  
  328. discover:
  329.         call    parse_response
  330.  
  331.         cmp     [dhcpMsgType], 0x02             ; Was the response an offer?
  332.         je      send_request
  333.  
  334.         call    dhcp_end
  335.         jmp     link_local
  336.  
  337. send_request:
  338.         mov     [dhcpMsgType], 0x03             ; make it a request
  339.         jmp     build_request
  340.  
  341. request:
  342.         call    parse_response
  343.         call    dhcp_end
  344.  
  345.         cmp     [dhcpMsgType], 0x05             ; Was the response an ACK? It should be
  346.         jne     link_local                      ; NO - so we do link-local
  347.  
  348.         mcall   76, API_IPv4 + 3, [dhcp.ip]             ; ip
  349.         mcall   76, API_IPv4 + 5, [dhcp.dns]            ; dns
  350.         mcall   76, API_IPv4 + 7, [dhcp.subnet]         ; subnet
  351.         mcall   76, API_IPv4 + 9, [dhcp.gateway]        ; gateway
  352.  
  353.         jmp     exit
  354.  
  355. dhcp_end:
  356.         mcall   close, [socketNum]
  357.         stdcall mem.Free, [dhcpMsg]
  358.  
  359.         ret
  360.  
  361. ;***************************************************************************
  362. ;   Function
  363. ;      parseResponse
  364. ;
  365. ;   Description
  366. ;      extracts the fields ( client IP address and options ) from
  367. ;      a DHCP response
  368. ;      The values go into
  369. ;       dhcpMsgType,dhcpLease,dhcpClientIP,dhcpServerIP,
  370. ;       dhcpDNSIP, dhcpSubnet
  371. ;      The message is stored in dhcpMsg
  372. ;
  373. ;***************************************************************************
  374. parse_response:
  375.  
  376.         DEBUGF  1,"Data received, parsing response\n"
  377.         mov     edx, [dhcpMsg]
  378.  
  379.         push    dword [edx+16]
  380.         pop     [dhcp.ip]
  381.         DEBUGF  1,"Client: %u.%u.%u.%u\n", [edx+16]:1, [edx+17]:1, [edx+18]:1, [edx+19]:1
  382.  
  383. ; TODO: check if there really are options
  384.  
  385.         mov     al, 240                         ; Point to first option
  386.         movzx   ecx, al
  387.  
  388.   .next_option:
  389.         add     edx, ecx
  390.  
  391.         mov     al, [edx]                       ; get message identifier
  392.  
  393.         cmp     al, 0xff                        ; End of options?
  394.         je      .done
  395.  
  396.         cmp     al, 0
  397.         je      .pad
  398.  
  399. ; TODO: check if we still are inside the buffer
  400.  
  401.         inc     edx
  402.         movzx   ecx, byte [edx]                 ; get data length
  403.         inc     edx                             ; point to data
  404.  
  405.         cmp     al, dhcp_msg_type               ; Msg type is a single byte option
  406.         je      .msgtype
  407.  
  408.         cmp     al, dhcp_dhcp_server_id
  409.         je      .server
  410.  
  411.         cmp     al, dhcp_address_time
  412.         je      .lease
  413.  
  414.         cmp     al, dhcp_subnet_mask
  415.         je      .subnet
  416.  
  417.         cmp     al, dhcp_router
  418.         je      .router
  419.  
  420.         cmp     al, dhcp_domain_server
  421.         je      .dns
  422.  
  423.         DEBUGF  1,"Unsupported DHCP option: %u\n", al
  424.  
  425.         jmp     .next_option
  426.  
  427.   .pad:
  428.         xor     ecx, ecx
  429.         inc     ecx
  430.         jmp     .next_option
  431.  
  432.   .msgtype:
  433.         mov     al, [edx]
  434.         mov     [dhcpMsgType], al
  435.  
  436.         DEBUGF  1,"DHCP Msg type: %u\n", al
  437.         jmp     .next_option                    ; Get next option
  438.  
  439.   .server:
  440.         mov     eax, [edx]
  441.         mov     [dhcpServerIP], eax
  442.         DEBUGF  1,"Server: %u.%u.%u.%u\n",[edx]:1,[edx+1]:1,[edx+2]:1,[edx+3]:1
  443.         jmp     .next_option
  444.  
  445.   .lease:
  446.         pusha
  447.         mov     eax,[edx]
  448.         bswap   eax
  449.         mov     [dhcpLease],eax
  450.         DEBUGF  1,"lease: %d\n",eax
  451.         popa
  452.         jmp     .next_option
  453.  
  454.   .subnet:
  455.         push    dword [edx]
  456.         pop     [dhcp.subnet]
  457.         DEBUGF  1,"Subnet: %u.%u.%u.%u\n",[edx]:1,[edx+1]:1,[edx+2]:1,[edx+3]:1
  458.         jmp     .next_option
  459.  
  460.   .router:
  461.         push    dword [edx]
  462.         pop     [dhcp.gateway]
  463.         DEBUGF  1,"Gateway: %u.%u.%u.%u\n",[edx]:1,[edx+1]:1,[edx+2]:1,[edx+3]:1
  464.         jmp     .next_option
  465.  
  466.   .dns:
  467.         push    dword [edx]
  468.         pop     [dhcp.dns]
  469.         DEBUGF  1,"DNS: %u.%u.%u.%u\n",[edx]:1,[edx+1]:1,[edx+2]:1,[edx+3]:1
  470.         jmp     .next_option
  471.  
  472.   .done:
  473.         ret
  474.  
  475.  
  476.  
  477. apipa:
  478.         mcall   close, [socketNum]
  479.         stdcall mem.Free, [dhcpMsg]
  480.  
  481.  
  482. link_local:
  483.         call    random
  484.         mov     ecx, 0xfea9                             ; IP 169.254.0.0 link local net, see RFC3927
  485.         mov     cx, ax
  486.         mcall   76, API_IPv4 + 3, ecx                     ; mask is 255.255.0.0
  487.         DEBUGF  1,"Link Local IP assinged: 169.254.%u.%u\n", [generator+2]:1, [generator+3]:1
  488.         mcall   76, API_IPv4 + 5, 0xffff
  489.         mcall   76, API_IPv4 + 9, 0x0
  490.         mcall   76, API_IPv4 + 7, 0x0
  491.  
  492.         mcall   5, PROBE_WAIT*100
  493.  
  494.         xor     esi, esi
  495.    probe_loop:
  496.         call    random                                  ; create a pseudo random number in eax (seeded by MAC)
  497.  
  498.         cmp     al, PROBE_MIN*100                       ; check if al is bigger then PROBE_MIN
  499.         jae     @f                                      ; all ok
  500.         add     al, (PROBE_MAX-PROBE_MIN)*100           ; al is too small
  501.    @@:
  502.  
  503.         cmp     al, PROBE_MAX*100
  504.         jbe     @f
  505.         sub     al, (PROBE_MAX-PROBE_MIN)*100
  506.    @@:
  507.  
  508.         movzx   ebx,al
  509.         DEBUGF  1,"Waiting %u0ms\n",ebx
  510.         mcall   5
  511.  
  512.         DEBUGF  1,"Sending Probe\n"
  513.         mcall   76, API_ARP + 6
  514.         inc     esi
  515.  
  516.         cmp     esi, PROBE_NUM
  517.         jb      probe_loop
  518.  
  519. ; now we wait further ANNOUNCE_WAIT seconds and send ANNOUNCE_NUM ARP announces. If any other host has assingned
  520. ; IP within this time, we should create another adress, that have to be done later
  521.  
  522.         DEBUGF  1,"Waiting %us\n", ANNOUNCE_WAIT
  523.         mcall   5, ANNOUNCE_WAIT*100
  524.         xor   esi, esi
  525.    announce_loop:
  526.  
  527.         DEBUGF  1,"Sending Announce\n"
  528.         mcall   76, API_ARP + 6
  529.  
  530.         inc     esi
  531.         cmp     esi,ANNOUNCE_NUM
  532.         je      @f
  533.  
  534.         DEBUGF  1,"Waiting %us\n", ANNOUNCE_INTERVAL
  535.         mcall   5, ANNOUNCE_INTERVAL*100
  536.         jmp     announce_loop
  537.    @@:
  538.  
  539.  
  540. error:
  541.         DEBUGF  1,"Socket error\n"
  542. exit:   ; we should, instead of closing, detect ARP conflicts and detect if cable keeps connected ;)
  543.         mcall   -1
  544.  
  545.  
  546. random:  ; Pseudo random actually
  547.  
  548.         mov     eax, [generator]
  549.         add     eax, -43ab45b5h
  550.         ror     eax, 1
  551.         bswap   eax
  552.         xor     eax, dword[MAC]
  553.         ror     eax, 1
  554.         xor     eax, dword[MAC+2]
  555.         mov     [generator], eax
  556.  
  557.         ret
  558.  
  559. ; DATA AREA
  560.  
  561. align 16
  562. @IMPORT:
  563.  
  564. library \
  565.         libini,'libini.obj'
  566.  
  567. import  libini, \
  568.         ini.get_str,'ini_get_str'
  569.  
  570. include_debug_strings
  571.  
  572. filename        db '.ini', 0
  573. str_ip          db 'ip', 0
  574. str_subnet      db 'subnet', 0
  575. str_gateway     db 'gateway', 0
  576. str_dns         db 'dns', 0
  577. str_ipconfig    db 'ipconfig', 0
  578. str_type        db 'type', 0
  579.  
  580.  
  581. sockaddr1:
  582.  
  583.         dw AF_INET4
  584.         dw 68 shl 8     ; local port
  585.         dd 0            ; local IP
  586.  
  587.         rb 10
  588.  
  589.  
  590. sockaddr2:
  591.  
  592.         dw AF_INET4
  593.         dw 67 shl 8     ; destination port
  594.         dd -1           ; destination IP
  595.  
  596.         rb 10
  597.  
  598.  
  599. IM_END:
  600.  
  601. inibuf          rb 16
  602.  
  603. dhcpMsgType     db  ?
  604. dhcpLease       dd  ?
  605. dhcpServerIP    dd  ?
  606.  
  607. dhcp:
  608. .ip             dd  ?
  609. .subnet         dd  ?
  610. .dns            dd  ?
  611. .gateway        dd  ?
  612.  
  613.  
  614. dhcpMsgLen      dd  ?
  615. socketNum       dd  ?
  616.  
  617. MAC             dp  ?
  618.  
  619. currTime        dd  ?
  620. generator       dd  ?
  621.  
  622. dhcpMsg         dd  ?
  623.  
  624. I_END_2:
  625.  
  626. path            rb  1024+5
  627.  
  628.                 rb  65536
  629.  
  630. I_END: