Subversion Repositories Kolibri OS

Rev

Rev 3361 | 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.         mcall   40, EVM_STACK2
  140.  
  141.         DEBUGF  1,">Zero-config service loaded\n"
  142.  
  143.   .wait:
  144.         mcall   76, API_ETH + 4 ; get MAC of ethernet interface 0
  145.         cmp     eax, -1
  146.         jne     .start
  147.  
  148.         mcall   10
  149.         jmp     .wait
  150.  
  151.   .start:
  152.         mcall   40, EVM_STACK
  153.         mov     word[MAC], bx
  154.         mov     dword[MAC+2], eax
  155.  
  156.         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
  157.  
  158.         mov     edi, path       ; Calculate the length of zero-terminated string
  159.         xor     al, al
  160.         mov     ecx, 1024
  161.         repne   scasb
  162.         dec     edi
  163.  
  164.         mov     esi, filename   ; append with .ini
  165.         movsd
  166.         movsb
  167.  
  168.         DEBUGF  1,"->Loading ini %s\n", path
  169.  
  170.         mcall   68, 11
  171.  
  172.         stdcall dll.Load,@IMPORT
  173.         or      eax, eax
  174.         jnz     try_dhcp
  175.  
  176.         invoke  ini.get_str, path, str_ipconfig, str_type, inibuf, 16, 0
  177.  
  178.         cmp     dword[inibuf], 'stat'
  179.         jne     try_dhcp
  180.  
  181.         invoke  ini.get_str, path, str_ipconfig, str_ip, inibuf, 16, 0
  182.         mov     edx, inibuf
  183.         call    Ip2dword
  184.         mcall   76, API_IPv4 + 3, edx
  185.  
  186.         invoke  ini.get_str, path, str_ipconfig, str_gateway, inibuf, 16, 0
  187.         mov     edx, inibuf
  188.         call    Ip2dword
  189.         mcall   76, API_IPv4 + 9, edx
  190.  
  191.         invoke  ini.get_str, path, str_ipconfig, str_dns, inibuf, 16, 0
  192.         mov     edx, inibuf
  193.         call    Ip2dword
  194.         mcall   76, API_IPv4 + 5, edx
  195.  
  196.         invoke  ini.get_str, path, str_ipconfig, str_subnet, inibuf, 16, 0
  197.         mov     edx, inibuf
  198.         call    Ip2dword
  199.         mcall   76, API_IPv4 + 7, edx
  200.  
  201.  
  202.         mcall   -1
  203.  
  204.  
  205. try_dhcp:
  206.  
  207.         DEBUGF  1,"->Trying DHCP\n"
  208.  
  209.         mcall   75, 0, AF_INET4, SOCK_DGRAM, 0          ; open socket (parameters: domain, type, reserved)
  210.         cmp     eax, -1
  211.         je      error
  212.         mov     [socketNum], eax
  213.  
  214.         DEBUGF  1,"->Socket %x opened\n", eax
  215.  
  216.         mcall   75, 2, [socketNum], sockaddr1, 18       ; bind socket to local port 68
  217.         cmp     eax, -1
  218.         je      error
  219.  
  220.         DEBUGF  1,"->Socket Bound to local port 68\n"
  221.  
  222.         mcall   75, 4, [socketNum], sockaddr2, 18       ; connect to 255.255.255.255 on port 67
  223.         cmp     eax, -1
  224.         je      error
  225.  
  226.         DEBUGF  1,"->Connected to 255.255.255.255 on port 67\n"
  227.  
  228.         mov     [dhcpMsgType], 0x01                     ; DHCP discover
  229.         mov     [dhcpLease], esi                        ; esi is still -1 (-1 = forever)
  230.  
  231.         mcall   26, 9                                   ; Get system time
  232.         imul    eax, 100
  233.         mov     [currTime], eax
  234.  
  235. build_request:                                          ; Creates a DHCP request packet.
  236.  
  237.         DEBUGF  1,"->Building request\n"
  238.  
  239.         stdcall mem.Alloc, BUFFER
  240.         mov     [dhcpMsg], eax
  241.         test    eax, eax
  242.         jz      dhcp_error
  243.  
  244.             ;;; todo: skip this bullcrap
  245.  
  246.         mov     edi, eax
  247.         mov     ecx, BUFFER
  248.         xor     eax, eax
  249.         rep     stosb
  250.  
  251.             ;; todo: put this in a buffer instead of writing bytes and words!
  252.  
  253.         mov     edx, [dhcpMsg]
  254.  
  255.         ; Boot protocol legacy
  256.         mov     [edx], byte 0x01                ; Boot request
  257.         mov     [edx+1], byte 0x01              ; Ethernet
  258.         mov     [edx+2], byte 0x06              ; Ethernet h/w len
  259.         mov     [edx+4], dword 0x11223344       ; xid                 ;;;;;;;
  260.         mov     eax, [currTime]
  261.         mov     [edx+8], eax                    ; secs, our uptime
  262.         mov     [edx+10], byte 0x80             ; broadcast flag set
  263.         mov     eax, dword [MAC]                ; first 4 bytes of MAC
  264.         mov     [edx+28],dword eax
  265.         mov     ax, word [MAC+4]                ; last 2 bytes of MAC
  266.         mov     [edx+32],word ax
  267.  
  268.         ; DHCP extension
  269.         mov     [edx+236], dword 0x63538263     ; magic cookie
  270.         mov     [edx+240], word 0x0135          ; option DHCP msg type
  271.         mov     al, [dhcpMsgType]
  272.         mov     [edx+240+2], al
  273.         mov     [edx+240+3], word 0x0433        ; option Lease time = infinity
  274.         mov     eax, [dhcpLease]
  275.         mov     [edx+240+5], eax
  276.         mov     [edx+240+9], word 0x0432        ; option requested IP address
  277.         mov     eax, [dhcp.ip]
  278.         mov     [edx+240+11], eax
  279.         mov     [edx+240+15], word 0x0437       ; option request list
  280.         mov     [edx+240+17], dword 0x0f060301
  281.  
  282.         cmp     [dhcpMsgType], byte 0x01        ; Check which msg we are sending
  283.         jne     request_options
  284.  
  285.         mov     [edx+240+21], byte 0xff         ; "Discover" options
  286.  
  287.         mov     [dhcpMsgLen], dword 262         ; end of options marker
  288.         jmp     send_dhcpmsg
  289.  
  290. request_options:
  291.         mov     [edx+240+21], word 0x0436       ; server IP
  292.         mov     eax, [dhcpServerIP]
  293.         mov     [edx+240+23], eax
  294.  
  295.         mov     [edx+240+27], byte 0xff         ; end of options marker
  296.  
  297.         mov     [dhcpMsgLen], dword 268
  298.  
  299. send_dhcpmsg:
  300.         mcall   75, 6, [socketNum], [dhcpMsg], [dhcpMsgLen]     ; write to socket ( send broadcast request )
  301.  
  302.         mov     eax, [dhcpMsg]                          ; Setup the DHCP buffer to receive response
  303.         mov     [dhcpMsgLen], eax                       ; Used as a pointer to the data
  304.  
  305.         mcall   23, TIMEOUT*10                          ; wait for data
  306.  
  307. read_data:                                              ; we have data - this will be the response
  308.         mcall   75, 7, [socketNum], [dhcpMsg], BUFFER   ; read data from socket
  309.  
  310.         DEBUGF  1,"->%d bytes received\n", eax
  311.  
  312.         cmp     eax, -1
  313.         je      dhcp_error
  314.  
  315.         mov     [dhcpMsgLen], eax
  316.  
  317. ; depending on which msg we sent, handle the response
  318. ; accordingly.
  319. ; If the response is to a dhcp discover, then:
  320. ;  1) If response is DHCP OFFER then
  321. ;  1.1) record server IP, lease time & IP address.
  322. ;  1.2) send a request packet
  323. ; If the response is to a dhcp request, then:
  324. ;  1) If the response is DHCP ACK then
  325. ;  1.1) extract the DNS & subnet fields. Set them in the stack
  326.  
  327.         cmp     [dhcpMsgType], 0x01             ; did we send a discover?
  328.         je      discover
  329.  
  330.         cmp     [dhcpMsgType], 0x03             ; did we send a request?
  331.         je      request
  332.  
  333.         call    dhcp_end                        ; we should never reach here ;)
  334.         jmp     exit
  335.  
  336. discover:
  337.         call    parse_response
  338.  
  339.         cmp     [dhcpMsgType], 0x02             ; Was the response an offer?
  340.         je      send_request
  341.  
  342.         call    dhcp_end
  343.         jmp     link_local
  344.  
  345. send_request:
  346.         mov     [dhcpMsgType], 0x03             ; make it a request
  347.         jmp     build_request
  348.  
  349. request:
  350.         call    parse_response
  351.  
  352.         cmp     [dhcpMsgType], 0x05             ; Was the response an ACK? It should be
  353.         jne     read_data                       ; NO - read next packets
  354.  
  355.         call    dhcp_end
  356.  
  357.         mcall   76, API_IPv4 + 3, [dhcp.ip]             ; ip
  358.         mcall   76, API_IPv4 + 5, [dhcp.dns]            ; dns
  359.         mcall   76, API_IPv4 + 7, [dhcp.subnet]         ; subnet
  360.         mcall   76, API_IPv4 + 9, [dhcp.gateway]        ; gateway
  361.  
  362.         jmp     exit
  363.  
  364. dhcp_end:
  365.         mcall   close, [socketNum]
  366.         stdcall mem.Free, [dhcpMsg]
  367.  
  368.         ret
  369.  
  370. ;***************************************************************************
  371. ;   Function
  372. ;      parseResponse
  373. ;
  374. ;   Description
  375. ;      extracts the fields ( client IP address and options ) from
  376. ;      a DHCP response
  377. ;      The values go into
  378. ;       dhcpMsgType,dhcpLease,dhcpClientIP,dhcpServerIP,
  379. ;       dhcpDNSIP, dhcpSubnet
  380. ;      The message is stored in dhcpMsg
  381. ;
  382. ;***************************************************************************
  383. parse_response:
  384.  
  385.         DEBUGF  1,"Data received, parsing response\n"
  386.         mov     edx, [dhcpMsg]
  387.  
  388.         push    dword [edx+16]
  389.         pop     [dhcp.ip]
  390.         DEBUGF  1,"Client: %u.%u.%u.%u\n", [edx+16]:1, [edx+17]:1, [edx+18]:1, [edx+19]:1
  391.  
  392. ; TODO: check if there really are options
  393.  
  394.         mov     al, 240                         ; Point to first option
  395.         movzx   ecx, al
  396.  
  397.   .next_option:
  398.         add     edx, ecx
  399.  
  400.         mov     al, [edx]                       ; get message identifier
  401.  
  402.         cmp     al, 0xff                        ; End of options?
  403.         je      .done
  404.  
  405.         cmp     al, 0
  406.         je      .pad
  407.  
  408. ; TODO: check if we still are inside the buffer
  409.  
  410.         inc     edx
  411.         movzx   ecx, byte [edx]                 ; get data length
  412.         inc     edx                             ; point to data
  413.  
  414.         cmp     al, dhcp_msg_type               ; Msg type is a single byte option
  415.         je      .msgtype
  416.  
  417.         cmp     al, dhcp_dhcp_server_id
  418.         je      .server
  419.  
  420.         cmp     al, dhcp_address_time
  421.         je      .lease
  422.  
  423.         cmp     al, dhcp_subnet_mask
  424.         je      .subnet
  425.  
  426.         cmp     al, dhcp_router
  427.         je      .router
  428.  
  429.         cmp     al, dhcp_domain_server
  430.         je      .dns
  431.  
  432.         DEBUGF  1,"Unsupported DHCP option: %u\n", al
  433.  
  434.         jmp     .next_option
  435.  
  436.   .pad:
  437.         xor     ecx, ecx
  438.         inc     ecx
  439.         jmp     .next_option
  440.  
  441.   .msgtype:
  442.         mov     al, [edx]
  443.         mov     [dhcpMsgType], al
  444.  
  445.         DEBUGF  1,"DHCP Msg type: %u\n", al
  446.         jmp     .next_option                    ; Get next option
  447.  
  448.   .server:
  449.         mov     eax, [edx]
  450.         mov     [dhcpServerIP], eax
  451.         DEBUGF  1,"Server: %u.%u.%u.%u\n",[edx]:1,[edx+1]:1,[edx+2]:1,[edx+3]:1
  452.         jmp     .next_option
  453.  
  454.   .lease:
  455.         pusha
  456.         mov     eax,[edx]
  457.         bswap   eax
  458.         mov     [dhcpLease],eax
  459.         DEBUGF  1,"lease: %d\n",eax
  460.         popa
  461.         jmp     .next_option
  462.  
  463.   .subnet:
  464.         push    dword [edx]
  465.         pop     [dhcp.subnet]
  466.         DEBUGF  1,"Subnet: %u.%u.%u.%u\n",[edx]:1,[edx+1]:1,[edx+2]:1,[edx+3]:1
  467.         jmp     .next_option
  468.  
  469.   .router:
  470.         push    dword [edx]
  471.         pop     [dhcp.gateway]
  472.         DEBUGF  1,"Gateway: %u.%u.%u.%u\n",[edx]:1,[edx+1]:1,[edx+2]:1,[edx+3]:1
  473.         jmp     .next_option
  474.  
  475.   .dns:
  476.         push    dword [edx]
  477.         pop     [dhcp.dns]
  478.         DEBUGF  1,"DNS: %u.%u.%u.%u\n",[edx]:1,[edx+1]:1,[edx+2]:1,[edx+3]:1
  479.         jmp     .next_option
  480.  
  481.   .done:
  482.         ret
  483.  
  484.  
  485.  
  486. dhcp_error:
  487.         call    dhcp_end
  488.  
  489. link_local:
  490.         call    random
  491.         mov     cx, ax
  492.         shl     ecx, 16
  493.         mov     cx, 0xfea9                              ; IP 169.254.0.0 link local net, see RFC3927
  494.         mcall   76, API_IPv4 + 3, ecx                   ; mask is 255.255.0.0
  495.         DEBUGF  1,"Link Local IP assinged: 169.254.%u.%u\n", [generator+0]:1, [generator+1]:1
  496.         mcall   76, API_IPv4 + 7, 0xffff
  497.         mcall   76, API_IPv4 + 9, 0x0
  498.         mcall   76, API_IPv4 + 5, 0x0
  499.  
  500.         mcall   5, PROBE_WAIT*100
  501.  
  502.         xor     esi, esi
  503.    probe_loop:
  504.         call    random                                  ; create a pseudo random number in eax (seeded by MAC)
  505.  
  506.         cmp     al, PROBE_MIN*100                       ; check if al is bigger then PROBE_MIN
  507.         jae     @f                                      ; all ok
  508.         add     al, (PROBE_MAX-PROBE_MIN)*100           ; al is too small
  509.    @@:
  510.  
  511.         cmp     al, PROBE_MAX*100
  512.         jbe     @f
  513.         sub     al, (PROBE_MAX-PROBE_MIN)*100
  514.    @@:
  515.  
  516.         movzx   ebx,al
  517.         DEBUGF  1,"Waiting %u0ms\n",ebx
  518.         mcall   5
  519.  
  520.         DEBUGF  1,"Sending Probe\n"
  521.         mcall   76, API_ARP + 6
  522.         inc     esi
  523.  
  524.         cmp     esi, PROBE_NUM
  525.         jb      probe_loop
  526.  
  527. ; now we wait further ANNOUNCE_WAIT seconds and send ANNOUNCE_NUM ARP announces. If any other host has assingned
  528. ; IP within this time, we should create another adress, that have to be done later
  529.  
  530.         DEBUGF  1,"Waiting %us\n", ANNOUNCE_WAIT
  531.         mcall   5, ANNOUNCE_WAIT*100
  532.         xor   esi, esi
  533.    announce_loop:
  534.  
  535.         DEBUGF  1,"Sending Announce\n"
  536.         mcall   76, API_ARP + 6
  537.  
  538.         inc     esi
  539.         cmp     esi,ANNOUNCE_NUM
  540.         je      @f
  541.  
  542.         DEBUGF  1,"Waiting %us\n", ANNOUNCE_INTERVAL
  543.         mcall   5, ANNOUNCE_INTERVAL*100
  544.         jmp     announce_loop
  545.    @@:
  546.  
  547.  
  548. error:
  549.         DEBUGF  1,"Socket error\n"
  550. exit:   ; we should, instead of closing, detect ARP conflicts and detect if cable keeps connected ;)
  551.         mcall   -1
  552.  
  553.  
  554. random:  ; Pseudo random actually
  555.  
  556.         mov     eax, [generator]
  557.         add     eax, -43ab45b5h
  558.         ror     eax, 1
  559.         bswap   eax
  560.         xor     eax, dword[MAC]
  561.         ror     eax, 1
  562.         xor     eax, dword[MAC+2]
  563.         mov     [generator], eax
  564.  
  565.         ret
  566.  
  567. ; DATA AREA
  568.  
  569. align 16
  570. @IMPORT:
  571.  
  572. library \
  573.         libini,'libini.obj'
  574.  
  575. import  libini, \
  576.         ini.get_str,'ini_get_str'
  577.  
  578. include_debug_strings
  579.  
  580. filename        db '.ini', 0
  581. str_ip          db 'ip', 0
  582. str_subnet      db 'subnet', 0
  583. str_gateway     db 'gateway', 0
  584. str_dns         db 'dns', 0
  585. str_ipconfig    db 'ipconfig', 0
  586. str_type        db 'type', 0
  587.  
  588.  
  589. sockaddr1:
  590.  
  591.         dw AF_INET4
  592.         dw 68 shl 8     ; local port
  593.         dd 0            ; local IP
  594.  
  595.         rb 10
  596.  
  597.  
  598. sockaddr2:
  599.  
  600.         dw AF_INET4
  601.         dw 67 shl 8     ; destination port
  602.         dd -1           ; destination IP
  603.  
  604.         rb 10
  605.  
  606.  
  607. IM_END:
  608.  
  609. inibuf          rb 16
  610.  
  611. dhcpMsgType     db  ?
  612. dhcpLease       dd  ?
  613. dhcpServerIP    dd  ?
  614.  
  615. dhcp:
  616. .ip             dd  ?
  617. .subnet         dd  ?
  618. .dns            dd  ?
  619. .gateway        dd  ?
  620.  
  621.  
  622. dhcpMsgLen      dd  ?
  623. socketNum       dd  ?
  624.  
  625. MAC             dp  ?
  626.  
  627. currTime        dd  ?
  628. generator       dd  ?
  629.  
  630. dhcpMsg         dd  ?
  631.  
  632. I_END_2:
  633.  
  634. path            rb  1024+5
  635.  
  636.                 rb  65536
  637.  
  638. I_END: