Subversion Repositories Kolibri OS

Rev

Rev 3618 | Rev 3636 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

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