Subversion Repositories Kolibri OS

Rev

Rev 2995 | Go to most recent revision | Show entire file | Regard whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 2995 Rev 3200
Line -... Line 1...
-
 
1
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
-
 
2
;;                                                                 ;;
-
 
3
;; Copyright (C) KolibriOS team 2010-2013. All rights reserved.    ;;
-
 
4
;; Distributed under terms of the GNU General Public License       ;;
-
 
5
;;                                                                 ;;
1
; Zero-config
6
;;  zeroconfig.asm - Zeroconfig service for KolibriOS              ;;
2
; v 1.4
-
 
3
;
-
 
-
 
7
;;                                                                 ;;
-
 
8
;;  Written by hidnplayr@kolibrios.org                             ;;
-
 
9
;;    Some code contributed by Derpenguin                          ;;
-
 
10
;;                                                                 ;;
4
; DHCP code is based on that by Mike Hibbet (DHCP client for menuetos)
11
;;  DHCP code is based on that by Mike Hibbet                      ;;
5
;
-
 
6
; Written by HidnPlayr & Derpenguin
12
;       (DHCP client for menuetos)                                 ;;
-
 
13
;;                                                                 ;;
-
 
14
;;          GNU GENERAL PUBLIC LICENSE                             ;;
-
 
15
;;             Version 2, June 1991                                ;;
-
 
16
;;                                                                 ;;
-
 
17
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
Line 7... Line 18...
7
 
18
 
Line 8... Line 19...
8
format binary as ""
19
format binary as ""
9
 
20
 
Line 18... Line 29...
18
               dd     (I_END+0x100)         ; esp
29
               dd     (I_END+0x100)         ; esp
19
               dd     0x0 , path            ; I_Param , I_Icon
30
               dd     0x0 , path            ; I_Param , I_Icon
Line 20... Line 31...
20
 
31
 
Line 21... Line -...
21
; CONFIGURATION
-
 
22
 
32
; CONFIGURATION
23
 
33
 
24
TIMEOUT             equ 60                  ; in seconds
34
TIMEOUT             equ 60                  ; in seconds
25
BUFFER              equ 1024                ; in bytes
35
BUFFER              equ 1024                ; in bytes
Line 123... Line -...
123
 
-
 
124
 
133
 
125
 
-
 
Line 126... Line 134...
126
 
134
 
Line 127... Line 135...
127
 
135
 
Line -... Line 136...
-
 
136
 
128
START:                                      ; start of execution
137
 
129
 
138
START:
Line 130... Line 139...
130
        mcall   40, 1 shl 7 ; network event
139
 
131
 
140
        mcall   40, EVM_STACK   ; network event
Line 132... Line 141...
132
        DEBUGF  1,">Zero-config service:\n"
141
 
Line 133... Line -...
133
 
-
 
134
        mcall   76, API_ETH + 4
142
        DEBUGF  1,">Zero-config service loaded\n"
135
 
143
 
136
        cmp     eax, -1
144
        mcall   76, API_ETH + 4 ; get MAC of ethernet interface 0
137
        je      exit
145
        cmp     eax, -1
138
 
146
        je      exit
Line 139... Line 147...
139
        mov     word[MAC], bx
147
 
140
        mov     dword[MAC+2], eax
148
        mov     word[MAC], bx
141
 
149
        mov     dword[MAC+2], eax
Line 142... Line 150...
142
        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
150
 
Line 143... Line 151...
143
 
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
Line 144... Line 152...
144
        cld
152
 
145
        mov     edi, path      ; Calculate the length of zero-terminated string
153
        mov     edi, path       ; Calculate the length of zero-terminated string
146
        xor     al , al
154
        xor     al, al
147
        mov     ecx, 1024
-
 
Line 148... Line 155...
148
        repnz   scas byte[es:edi]
155
        mov     ecx, 1024
Line 149... Line 156...
149
        dec     edi
156
        repne   scasb
150
 
-
 
151
        mov     esi, filename
-
 
152
        movsd
157
        dec     edi
Line 153... Line 158...
153
        movsb
158
 
154
 
159
        mov     esi, filename   ; append with .ini
155
        DEBUGF  1,"->path to ini: %s\n", path
160
        movsd
156
 
161
        movsb
Line 190... Line 195...
190
 
195
 
Line 191... Line 196...
191
 
196
 
Line 192... Line 197...
192
        mcall  -1
197
        mcall   -1
Line 193... Line 198...
193
 
198
 
194
 
199
 
195
skip_ini:
200
try_dhcp:
196
 
201
 
Line 197... Line 202...
197
        DEBUGF  1,"->Skip ini\n"
202
        DEBUGF  1,"->Trying DHCP\n"
Line 198... Line 203...
198
 
203
 
199
        mcall 75, 0, AF_INET4, SOCK_DGRAM, 0      ; open socket (parameters: domain, type, reserved)
204
        mcall   75, 0, AF_INET4, SOCK_DGRAM, 0          ; open socket (parameters: domain, type, reserved)
200
        cmp   eax, -1
205
        cmp     eax, -1
Line 213... Line 218...
213
        cmp   eax, -1
218
        cmp     eax, -1
214
        je    error
219
        je      error
Line 215... Line 220...
215
 
220
 
Line 216... Line 221...
216
        DEBUGF  1,"->Connected to 255.255.255.255 on port 67\n"
221
        DEBUGF  1,"->Connected to 255.255.255.255 on port 67\n"
217
 
222
 
Line 218... Line 223...
218
        mov     byte [dhcpMsgType], 0x01        ; DHCP discover
223
        mov     [dhcpMsgType], 0x01                     ; DHCP discover
219
        mov     dword [dhcpLease], esi          ; esi is still -1 (-1 = forever)
224
        mov     [dhcpLease], esi                        ; esi is still -1 (-1 = forever)
220
 
225
 
Line 221... Line 226...
221
        mcall   26, 9
226
        mcall   26, 9                                   ; Get system time
Line 222... Line 227...
222
        imul    eax,100
227
        imul    eax, 100
Line 223... Line 228...
223
        mov     [currTime],eax
228
        mov     [currTime], eax
224
 
229
 
Line 234... Line 239...
234
            ;;; todo: skip this bullcrap
239
            ;;; todo: skip this bullcrap
Line 235... Line 240...
235
 
240
 
236
        mov     edi, eax
241
        mov     edi, eax
237
        mov     ecx, BUFFER
242
        mov     ecx, BUFFER
238
        xor     eax, eax
-
 
239
        cld
243
        xor     eax, eax
Line 240... Line 244...
240
        rep     stosb
244
        rep     stosb
Line 241... Line 245...
241
 
245
 
Line 271... Line 275...
271
        jne     request_options
275
        jne     request_options
Line 272... Line 276...
272
 
276
 
Line 273... Line 277...
273
        mov     [edx+240+21], byte 0xff         ; "Discover" options
277
        mov     [edx+240+21], byte 0xff         ; "Discover" options
274
 
278
 
Line 275... Line 279...
275
        mov     [dhcpMsgLen], dword 262         ; end of options marker
279
        mov     [dhcpMsgLen], dword 262         ; end of options marker
276
        jmp     send_request
280
        jmp     send_dhcpmsg
277
 
281
 
278
request_options:
282
request_options:
Line 279... Line 283...
279
        mov     [edx+240+21], word 0x0436       ; server IP
283
        mov     [edx+240+21], word 0x0436       ; server IP
Line 280... Line 284...
280
        mov     eax, [dhcpServerIP]
284
        mov     eax, [dhcpServerIP]
Line 281... Line 285...
281
        mov     [edx+240+23], eax
285
        mov     [edx+240+23], eax
282
 
286
 
Line 283... Line 287...
283
        mov     [edx+240+27], byte 0xff         ; end of options marker
287
        mov     [edx+240+27], byte 0xff         ; end of options marker
284
 
288
 
Line 310... Line 314...
310
    ;  1.2) send a request packet
314
;  1.2) send a request packet
311
    ; If the response is to a dhcp request, then:
315
; If the response is to a dhcp request, then:
312
    ;  1) If the response is DHCP ACK then
316
;  1) If the response is DHCP ACK then
313
    ;  1.1) extract the DNS & subnet fields. Set them in the stack
317
;  1.1) extract the DNS & subnet fields. Set them in the stack
Line 314... Line 318...
314
 
318
 
315
    cmp     [dhcpMsgType], byte 0x01        ; did we send a discover?
319
        cmp     [dhcpMsgType], 0x01             ; did we send a discover?
-
 
320
        je      discover
316
    je      discover
321
 
317
    cmp     [dhcpMsgType], byte 0x03        ; did we send a request?
322
        cmp     [dhcpMsgType], 0x03             ; did we send a request?
Line 318... Line 323...
318
    je      request
323
        je      request
-
 
324
 
Line 319... Line 325...
319
 
325
        call    dhcp_end                        ; we should never reach here ;)
320
        jmp     exit                           ; really unknown, what we did
326
        jmp     exit
-
 
327
 
-
 
328
discover:
-
 
329
        call    parse_response
Line 321... Line 330...
321
 
330
 
322
discover:
331
        cmp     [dhcpMsgType], 0x02             ; Was the response an offer?
-
 
332
        je      send_request
-
 
333
 
323
        call    parseResponse
334
        call    dhcp_end
324
 
335
        jmp     link_local
Line 325... Line 336...
325
        cmp     [dhcpMsgType], byte 0x02        ; Was the response an offer?
336
 
326
        jne     apipa                           ; NO - so we do zeroconf
337
send_request:
-
 
338
        mov     [dhcpMsgType], 0x03             ; make it a request
Line 327... Line 339...
327
        mov     [dhcpMsgType], byte 0x03        ; DHCP request
339
        jmp     build_request
328
        jmp     buildRequest
340
 
Line 329... Line 341...
329
 
341
request:
330
request:
342
        call    parse_response
331
        call    parseResponse
343
        call    dhcp_end
332
 
344
 
Line 333... Line 345...
333
        cmp     [dhcpMsgType], byte 0x05        ; Was the response an ACK? It should be
345
        cmp     [dhcpMsgType], 0x05             ; Was the response an ACK? It should be
Line -... Line 346...
-
 
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
334
        jne     apipa                           ; NO - so we do zeroconf
352
 
335
 
353
        jmp     exit
336
        mcall 76, API_IPv4 + 3, [dhcp.ip]       ; ip
354
 
337
        mcall 76, API_IPv4 + 5, [dhcp.dns]      ; dns
355
dhcp_end:
338
        mcall 76, API_IPv4 + 7, [dhcp.subnet]   ; subnet
356
        mcall   close, [socketNum]
Line 351... Line 369...
351
;       dhcpMsgType,dhcpLease,dhcpClientIP,dhcpServerIP,
369
;       dhcpMsgType,dhcpLease,dhcpClientIP,dhcpServerIP,
352
;       dhcpDNSIP, dhcpSubnet
370
;       dhcpDNSIP, dhcpSubnet
353
;      The message is stored in dhcpMsg
371
;      The message is stored in dhcpMsg
354
;
372
;
355
;***************************************************************************
373
;***************************************************************************
356
parseResponse:
374
parse_response:
-
 
375
 
357
    DEBUGF  1,"Data received, parsing response\n"
376
        DEBUGF  1,"Data received, parsing response\n"
358
    mov     edx, [dhcpMsg]
377
        mov     edx, [dhcpMsg]
Line 359... Line 378...
359
 
378
 
360
    push    dword [edx+16]
379
        push    dword [edx+16]
361
    pop     [dhcp.ip]
380
        pop     [dhcp.ip]
Line -... Line 381...
-
 
381
        DEBUGF  1,"Client: %u.%u.%u.%u\n", [edx+16]:1, [edx+17]:1, [edx+18]:1, [edx+19]:1
-
 
382
 
362
    DEBUGF  1,"Client: %u.%u.%u.%u\n",[edx+16]:1,[edx+17]:1,[edx+18]:1,[edx+19]:1
383
; TODO: check if there really are options
363
 
384
 
Line 364... Line 385...
364
    add     edx, 240                        ; Point to first option
385
        mov     al, 240                         ; Point to first option
365
    xor     ecx, ecx
386
        movzx   ecx, al
366
 
-
 
367
next_option:
-
 
368
    add     edx, ecx
-
 
369
pr001:
-
 
Line 370... Line 387...
370
    mov     al, [edx]
387
 
371
    cmp     al, 0xff                        ; End of options?
-
 
Line 372... Line 388...
372
    je      pr_exit
388
  .next_option:
373
 
389
        add     edx, ecx
Line 374... Line 390...
374
    cmp     al, dhcp_msg_type               ; Msg type is a single byte option
390
 
-
 
391
        mov     al, [edx]                       ; get message identifier
Line 375... Line -...
375
    jne     @f
-
 
376
 
392
 
Line 377... Line -...
377
    mov     al, [edx+2]
-
 
378
    mov     [dhcpMsgType], al
393
        cmp     al, 0xff                        ; End of options?
379
 
394
        je      .done
380
    DEBUGF  1,"DHCP Msg type: %u\n", al
395
 
Line -... Line 396...
-
 
396
        cmp     al, 0
-
 
397
        je      .pad
-
 
398
 
381
 
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
 
382
    add     edx, 3
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
383
    jmp     pr001                           ; Get next option
431
 
384
 
432
  .msgtype:
385
@@:
433
        mov     al, [edx]
386
    inc     edx
434
        mov     [dhcpMsgType], al
387
    movzx   ecx, byte [edx]
-
 
388
    inc     edx                             ; point to data
-
 
389
 
-
 
390
    cmp     al, dhcp_dhcp_server_id         ; server ip
-
 
Line -... Line 435...
-
 
435
 
391
    jne     @f
436
        DEBUGF  1,"DHCP Msg type: %u\n", al
392
    mov     eax, [edx]
437
        jmp     .next_option                    ; Get next option
393
    mov     [dhcpServerIP], eax
438
 
394
    DEBUGF  1,"Server: %u.%u.%u.%u\n",[edx]:1,[edx+1]:1,[edx+2]:1,[edx+3]:1
439
  .server:
395
    jmp     next_option
440
        mov     eax, [edx]
396
 
441
        mov     [dhcpServerIP], eax
-
 
442
        DEBUGF  1,"Server: %u.%u.%u.%u\n",[edx]:1,[edx+1]:1,[edx+2]:1,[edx+3]:1
Line 397... Line -...
397
@@:
-
 
398
    cmp     al, dhcp_address_time
-
 
399
    jne     @f
-
 
400
 
-
 
401
    pusha
443
        jmp     .next_option
402
    mov     eax,[edx]
-
 
403
    bswap   eax
444
 
404
    mov     [dhcpLease],eax
445
  .lease:
405
    DEBUGF  1,"lease: %d\n",eax
446
        pusha
406
    popa
447
        mov     eax,[edx]
407
 
-
 
408
    jmp     next_option
-
 
409
 
-
 
410
@@:
-
 
Line -... Line 448...
-
 
448
        bswap   eax
411
    cmp     al, dhcp_subnet_mask
449
        mov     [dhcpLease],eax
412
    jne     @f
450
        DEBUGF  1,"lease: %d\n",eax
413
 
451
        popa
414
    push    dword [edx]
452
        jmp     .next_option
415
    pop     [dhcp.subnet]
-
 
416
    DEBUGF  1,"Subnet: %u.%u.%u.%u\n",[edx]:1,[edx+1]:1,[edx+2]:1,[edx+3]:1
-
 
417
    jmp     next_option
-
 
418
 
-
 
419
@@:
-
 
Line -... Line 453...
-
 
453
 
420
    cmp     al, dhcp_router
454
  .subnet:
421
    jne     @f
455
        push    dword [edx]
422
 
456
        pop     [dhcp.subnet]
423
    push    dword [edx]
457
        DEBUGF  1,"Subnet: %u.%u.%u.%u\n",[edx]:1,[edx+1]:1,[edx+2]:1,[edx+3]:1
424
    pop     [dhcp.gateway]
-
 
425
    DEBUGF  1,"Gateway: %u.%u.%u.%u\n",[edx]:1,[edx+1]:1,[edx+2]:1,[edx+3]:1
-
 
Line -... Line 458...
-
 
458
        jmp     .next_option
426
    jmp     next_option
459
 
Line 427... Line -...
427
 
-
 
428
 
-
 
Line 429... Line 460...
429
@@:
460
  .router:
430
    cmp     al, dhcp_domain_server
461
        push    dword [edx]
431
    jne     next_option
462
        pop     [dhcp.gateway]
Line -... Line 463...
-
 
463
        DEBUGF  1,"Gateway: %u.%u.%u.%u\n",[edx]:1,[edx+1]:1,[edx+2]:1,[edx+3]:1
432
 
464
        jmp     .next_option
433
    push    dword [edx]
465
 
434
    pop     [dhcp.dns]
466
  .dns:
435
    DEBUGF  1,"DNS: %u.%u.%u.%u\n",[edx]:1,[edx+1]:1,[edx+2]:1,[edx+3]:1
467
        push    dword [edx]
436
    jmp     next_option
468
        pop     [dhcp.dns]
Line 462... Line 494...
462
    xor esi,esi
494
        xor     esi, esi
463
   probe_loop:
495
   probe_loop:
464
    call  random                            ; create a pseudo random number in eax (seeded by MAC)
496
        call    random                                  ; create a pseudo random number in eax (seeded by MAC)
Line 465... Line 497...
465
 
497
 
466
    cmp   al,PROBE_MIN*100                  ; check if al is bigger then PROBE_MIN
498
        cmp     al, PROBE_MIN*100                       ; check if al is bigger then PROBE_MIN
467
    jge   @f                                ; all ok
499
        jae     @f                                      ; all ok
468
    add   al,(PROBE_MAX-PROBE_MIN)*100      ; al is too small
500
        add     al, (PROBE_MAX-PROBE_MIN)*100           ; al is too small
Line 469... Line 501...
469
   @@:
501
   @@:
470
 
502
 
471
    cmp   al,PROBE_MAX*100
503
        cmp     al, PROBE_MAX*100
472
    jle   @f
504
        jbe     @f
Line 473... Line 505...
473
    sub   al,(PROBE_MAX-PROBE_MIN)*100
505
        sub     al, (PROBE_MAX-PROBE_MIN)*100
474
   @@:
506
   @@:
475
 
507
 
Line 476... Line 508...
476
    movzx ebx,al
508
        movzx   ebx,al
477
    DEBUGF  1,"Waiting %u0ms\n",ebx
509
        DEBUGF  1,"Waiting %u0ms\n",ebx
478
    mcall 5
510
        mcall   5
Line 479... Line 511...
479
 
511
 
480
    DEBUGF  1,"Sending Probe\n"
512
        DEBUGF  1,"Sending Probe\n"
Line 481... Line 513...
481
;    eth.ARP_PROBE MAC
513
        mcall   76, API_ARP + 6
482
    inc   esi
514
        inc     esi
Line 483... Line 515...
483
 
515
 
484
    cmp   esi,PROBE_NUM
516
        cmp     esi, PROBE_NUM
485
    jl    probe_loop
517
        jb      probe_loop
486
 
518
 
Line 487... Line 519...
487
; now we wait further ANNOUNCE_WAIT seconds and send ANNOUNCE_NUM ARP announces. If any other host has assingned
519
; now we wait further ANNOUNCE_WAIT seconds and send ANNOUNCE_NUM ARP announces. If any other host has assingned
488
; IP within this time, we should create another adress, that have to be done later
520
; IP within this time, we should create another adress, that have to be done later
Line 489... Line 521...
489
 
521
 
490
    DEBUGF  1,"Waiting %us\n",ANNOUNCE_WAIT
522
        DEBUGF  1,"Waiting %us\n", ANNOUNCE_WAIT
491
    mcall 5, ANNOUNCE_WAIT*100
523
        mcall   5, ANNOUNCE_WAIT*100
Line 492... Line 524...
492
    xor   esi,esi
524
        xor   esi, esi
493
   announce_loop:
525
   announce_loop:
494
 
526
 
495
    DEBUGF  1,"Sending Announce\n"
527
        DEBUGF  1,"Sending Announce\n"
496
;    eth.ARP_ANNOUNCE MAC
-
 
-
 
528
        mcall   76, API_ARP + 6
Line 497... Line 529...
497
 
529
 
498
    inc   esi
530
        inc     esi
-
 
531
        cmp     esi,ANNOUNCE_NUM
499
    cmp   esi,ANNOUNCE_NUM
532
        je      @f
Line 500... Line 533...
500
    je    @f
533
 
Line 582... Line 615...
582
socketNum       dd  ?
615
socketNum       dd  ?
Line 583... Line 616...
583
 
616
 
Line 584... Line 617...
584
MAC             dp  ?
617
MAC             dp  ?
585
 
-
 
586
currTime        dd  ?
618
 
Line 587... Line 619...
587
renewTime       dd  ?
619
currTime        dd  ?
Line 588... Line 620...
588
generator       dd  ?
620
generator       dd  ?