Subversion Repositories Kolibri OS

Rev

Rev 7638 | Blame | Compare with Previous | Last modification | View Log | Download | RSS feed

  1. ;
  2. ;  SNTP client for KolibriOS
  3. ;
  4. ;  (C) 2019 Coldy      
  5. ;  Thank's you for use this code and software based on it!
  6. ;  I will glad if it's will be helpful.
  7. ;
  8. ;  Distributed under terms of GPL
  9. ;
  10.  
  11. format binary as ""
  12.  
  13. use32
  14.         org     0x0
  15.  
  16.         db      'MENUET01'       ; signature
  17.         dd      1                        ; header version
  18.         dd      START                ; entry point
  19.         dd      I_END                ; initialized size
  20.         dd      MEM              ; required memory
  21.         dd      STACKTOP           ; stack pointer
  22.         dd      params             ; parameters
  23.         dd      0                        ; path
  24.  
  25. __DEBUG__       = 1
  26. __DEBUG_LEVEL__ = 2
  27.  
  28. include '../../proc32.inc'
  29. include '../../macros.inc'
  30. include '../../dll.inc'
  31. include '../../struct.inc'
  32. include '../../network.inc'
  33. include '../../debug-fdo.inc'
  34. include 'time.inc'
  35.  
  36.  
  37. START:
  38.  
  39. ; init heap
  40.         mcall   68, 11
  41.         test    eax, eax
  42.   ; fatal error (not enough memory)
  43.         jz      exit_now
  44.  
  45. ; load libraries
  46.         stdcall dll.Load, @IMPORT
  47.         test    eax, eax
  48.   ; fatal error(imports not loaded)
  49.         jnz     exit_now
  50.  
  51. ; initialize console
  52. ;       push    1
  53. ;       call    [con_start]
  54.         push    str_title
  55.         push    250
  56.         push    80
  57.         push    25
  58.         push    80
  59.         call    [con_init]
  60.   ;test eax, eax
  61.   ; fatal error(console error)
  62.         ;jnz    exit
  63.  
  64.   ; setup params
  65.   call parse_params
  66.   ;clear eax
  67.   call tz_validate
  68.   ; is TZ correct?
  69.   cmp ebx,0
  70.   je @f
  71.   mov   eax, ebx
  72.   mov   ebx, str_err11
  73.   jmp .tz_error
  74. @@:
  75.   ; is command line correct?
  76.   cmp eax, 10
  77.   jne @f
  78.   mov   ebx, str_err10
  79.   jmp .error
  80. @@:
  81.   ; empty command line (need help)?
  82.   cmp eax, 0
  83.   je @f
  84.   cinvoke       con_printf, str_help
  85.   jmp exit
  86.  
  87. @@:
  88.   ; Prepare to do query time
  89.   ; Convert host name to IP address
  90.   invoke inet_addr, params
  91.   ; Host name by IP address was provided?
  92.   cmp     eax, -1
  93.   jne      .resolved  
  94.  
  95.   push    esp     ; reserve stack place
  96.    
  97.   invoke getaddrinfo, params, 0, 0, esp
  98.  
  99.   ; Get ptr to result addrinfo struct
  100.   pop     esi
  101.  
  102.   ; Test for error  
  103.   test    eax, eax
  104.   jz    @f
  105.   mov   eax, 30
  106.   mov   ebx, str_err3
  107.   ;ret ; Error: Name not resolved!
  108.   jmp .error
  109.  
  110.   @@:
  111.   mov     eax, [esi+addrinfo.ai_addr]
  112.   mov     eax, [eax+sockaddr_in.sin_addr]
  113.  
  114.   push eax ; Store IP to stack
  115.  
  116.   invoke  inet_ntoa, eax
  117.   ; Store string of IP
  118.   mov edx, eax
  119.   pop eax ; Load IP from stack
  120.   jmp @f
  121.  
  122. .resolved:
  123.   clear edx ; mark host is IP format
  124. @@:
  125.   mov     [sockaddr1.ip], eax
  126.  
  127.   clear ebx
  128.   mov bx, [port]
  129.  
  130.   ;cinvoke      con_printf, str_query, params, eax, ebx
  131.   cinvoke       con_printf, str_query, params
  132.   cmp edx,0
  133.   je @f ; Skip IP display
  134.   cinvoke       con_printf, str_ip, edx
  135. @@:
  136.   cinvoke       con_printf, str_port, ebx
  137.  
  138.   ; free allocated memory
  139.   invoke  freeaddrinfo, esi
  140.  
  141.   ; Now we ready to query server  
  142.   call sntp_query_time
  143.   cmp eax,1
  144.   jne @f
  145.   mov eax, 59
  146.   jmp .warning
  147. @@:
  148.   cmp eax,2
  149.   jne @f
  150.   mov eax, 61
  151. .warning:
  152.   cinvoke       con_printf, str_warn, eax
  153.   jmp .display
  154. @@:
  155.   cmp eax, 41
  156.   jge .error
  157.  
  158. .display:
  159.   ; Tell user results
  160.  
  161.   ;mov esi, datetime ; ?
  162.  
  163.   ; Display server date and time  
  164.   xor eax, eax
  165.   mov al, [esi + DateTime.sec]
  166.   push eax
  167.   mov al, [esi + DateTime.min]
  168.   push eax
  169.   mov al, [esi + DateTime.hour]
  170.   push eax
  171.   mov ax, [esi + DateTime.year]
  172.   push eax
  173.  
  174.   xor eax, eax
  175.   mov al, [esi + DateTime.month]
  176.   push eax
  177.   mov al, [esi + DateTime.day]
  178.   push eax
  179.  
  180.   push str_dt
  181.  
  182.   call [con_printf]  
  183.   add  esp, 7*4
  184.  
  185.   ; Display timezone
  186.  
  187.   cmp [tz_h],0
  188.   jne @f
  189.   cmp [tz_m],0
  190.   jne @f
  191.   mov eax, str_tz
  192.   add eax, 9  
  193.   cinvoke con_printf, eax ; \n\0
  194.   jmp .no_bias
  195.  
  196. @@:
  197.   clear eax, ebx
  198.   mov  ecx, str_tz
  199.   mov al, [tz_h]
  200.   test al, al
  201.   jns @f
  202.   mov byte[ecx+1],'-'    ; Change sign
  203.   sub bl, al
  204.   mov al, bl
  205.  
  206. @@:
  207.   cmp [tz_m],0
  208.   jne @f
  209.   mov word[ecx+4],10 ; \n\0
  210.    
  211. @@:  
  212.   mov bl, [tz_m]
  213.    
  214.   cinvoke con_printf, str_tz, eax, ebx
  215.  
  216. .no_bias:
  217.   cmp [sync],0
  218.   je  exit
  219.   cmp [sync], SYNC_S
  220.   jne @f
  221.   mov eax, str_s
  222.   jmp .sync_ok
  223. @@:
  224.   cmp [sync], SYNC_ST
  225.   jne @f
  226.   mov eax, str_st
  227.   jmp .sync_ok
  228.   @@:
  229.   cmp [sync], SYNC_SS
  230.   jne exit ; Fixed (24.04.2019): incorrect display with -ss
  231.   mov eax, str_ss
  232.  
  233. .sync_ok:
  234.   cinvoke       con_printf, str_sync, eax
  235.  
  236.   jmp exit
  237.  
  238. .error:
  239.   mov esi, params
  240.  
  241. .tz_error:
  242.   ; Note: esi assign by parse_params
  243.  
  244. ;  cmp [notify],0
  245. ;  jne  @f
  246.   cinvoke con_printf, str_err, eax, esi, ebx
  247.   ;jmp exit
  248. ;@@:
  249.  
  250.   ; Do call @NOTIFY
  251.  
  252.  
  253.   ; Finally... exit!
  254. exit:
  255.         push    0
  256.         call    [con_exit]
  257.  
  258. exit_now:
  259.         mcall   -1
  260.  
  261. ; End of program
  262.  
  263. ;notify:
  264.  
  265. ; Time zone check helper
  266. tz_validate:
  267. ;jmp .exit
  268.   cmp [tz_h],-12
  269.   jl .fail
  270.   cmp [tz_h],14
  271.   jg .fail
  272.  
  273.   cmp [tz_m],0
  274.   je  .exit
  275.   cmp [tz_m],30
  276.   jne .tz_45m
  277.   cmp [tz_h],-9
  278.   je .exit
  279.   cmp [tz_h],-3
  280.   je .exit
  281.   cmp [tz_h],3
  282.   jl .fail  
  283.   je .exit
  284.   cmp [tz_h],6
  285.   jg @f  
  286.   jle .exit
  287. @@:
  288.   cmp [tz_h],9
  289.   je .exit
  290.   cmp [tz_h],10
  291.   jne .fail
  292.   je  .exit
  293.      
  294. .tz_45m:
  295.   cmp [tz_m],45
  296.   jne .fail
  297.   cmp [tz_h],5
  298.   je .exit
  299.   cmp [tz_h],8
  300.   je .exit
  301.   cmp [tz_h],12
  302.   jne .fail
  303. .exit:
  304.   clear ebx
  305.   ret
  306.    
  307. .fail:
  308.   mov ebx,11  
  309.   ret
  310.  
  311.  
  312. ; Sycronization constants  
  313. SYNC_S    = 1
  314. SYNC_SS   = 2
  315. SYNC_ST   = 3
  316.  
  317. parse_params:
  318.   mov     esi, params
  319.   mov   ebx, esi
  320.   clear ecx ; 26.04.2018 Fixed
  321. .f00:
  322.         lodsb
  323.   cmp    al, 0
  324.         jne     .f01
  325.  
  326.   dec esi
  327.   cmp esi, ebx
  328.   jne @f
  329.   ;no params
  330.   mov eax, -1
  331.   ret
  332.    
  333. .exit:
  334.   cmp ecx,0 ; 26.04.2018 Fixed
  335.   je @f
  336.   ; mark end of TZ
  337.   mov     byte [ecx+1],  0
  338.   ; now esi = start of TZ
  339.   mov esi,ebp
  340.  
  341. @@:  
  342.   mov eax, 0
  343.   ret
  344.  
  345. .f01:
  346.   cmp     al, ' '
  347.         jne     .f00
  348.  
  349.   ; Save end of host position
  350.   mov edi, esi
  351.   dec edi
  352.   ;mov    byte [esi-1],  0
  353.         jmp     .param
  354.  
  355.  .param_loop:
  356.         lodsb
  357.         cmp              al, 0
  358.   je @f
  359.   cmp    al, ' '
  360.         jne    .invalid
  361.         jmp   .param
  362. @@:
  363.   mov     byte [edi],  0
  364.   jmp .exit ;ret
  365.  
  366.  .param:
  367.   lodsb
  368.         cmp     al, '-'
  369.         jne     .invalid
  370.  
  371.         lodsb
  372.  
  373. ;  cmp    al, 'n'
  374. ;  jne   @f
  375. ;  mov   [notify],1
  376. ;  jmp    .param_loop
  377. ;@@:  
  378. ; cmp     al, 'p'
  379. ;       je              .p
  380.         cmp     al, 't'
  381.         jne             @f
  382.         lodsb
  383.         cmp     al, 'z'
  384.         jne     .invalid
  385.         je      .tz
  386. @@:
  387.         cmp     al, 's'
  388.   je      .sync
  389.        
  390. ;.p:
  391.   ; port setup
  392. ;       lodsb
  393. ;       cmp     al, ' '
  394. ;       jne     .invalid
  395. ;       call    c2n
  396. ;       test    ebx, ebx
  397. ; jz      .invalid
  398. ; mov     [port], bx
  399. ;       jmp .param_loop
  400. jmp .invalid
  401.  
  402. .tz:  
  403.   ; tz setup
  404.         lodsb
  405.         cmp     al, ' '
  406.         jne     .invalid
  407.  
  408.   ; save start of TZ
  409.   ;push esi
  410.   mov ebp, esi
  411.   call    c2n
  412.   ;cmp    ebx, ebx  ; 0 is possible
  413.   ;jz     .invalid
  414.   mov     [tz_h], bl
  415.         cmp     al, ':'
  416.         je                .tz_m
  417.         ;dec              esi
  418.         jmp    @f;.param_loop
  419.  
  420. .tz_m:
  421.         call    c2n
  422.   ;test   ebx, ebx  ; 0 is possible
  423.   ;jz     .invalid
  424.   mov     [tz_m], bl
  425. @@:
  426.   ; save end of TZ
  427.   ;push esi
  428.   mov ecx, esi
  429.   jmp .param_loop
  430.  
  431.   .sync:
  432.   ; sync setup
  433.   lodsb
  434.   cmp     al, 's'
  435.         jne             .st
  436.   mov     [sync], SYNC_SS
  437.         jmp             .param_loop
  438.   .st:
  439.   cmp     al, 't'
  440.         jne             .s
  441.   mov     [sync], SYNC_ST
  442.         jmp             .param_loop
  443. .s:
  444.   mov     [sync], SYNC_S
  445.   dec   esi
  446.         jmp             .param_loop
  447.  
  448. .invalid:
  449.         mov eax, 10
  450.         ret
  451.  
  452.  
  453. ; Helper to convert char to number
  454. ; Input:
  455. ; esi  - ptr to char of digit
  456. ; Output:
  457. ; ebx - number
  458. ; Use registers (not restore):
  459. ; eax, edx
  460. ;  
  461. c2n:
  462.   xor     eax, eax
  463.   xor     ebx, ebx
  464.         xor     edx, edx
  465.   .loop:
  466.   lodsb
  467.   test    al, al
  468.   jz      .done
  469.   cmp     al, ' '
  470.   je      .done
  471.  
  472.         cmp     al, ':'
  473.         ;jne            .f0
  474.   je .done1
  475.  
  476.         ;.f0:
  477.         cmp             al, '+'
  478.         je              .f00
  479.         cmp             al, '-'
  480.         jne             .f01
  481.         mov             dl,1
  482.   .f00:
  483.         lodsb
  484.   .f01:
  485.  
  486.   sub     al, '0'
  487.   jb      .fail
  488.   cmp     al, 9
  489.   ja      .fail
  490.   lea     ebx, [ebx*4+ebx]
  491.   lea     ebx, [ebx*2+eax]
  492.   jmp     .loop
  493.   .fail:
  494.   xor     ebx, ebx
  495.   .done:
  496.   dec     esi
  497.  .done1:
  498.         cmp             dl, 1
  499.         jne             .ret
  500.         neg             ebx
  501. .ret:
  502.   ret
  503.  
  504.  
  505. ; Sync worker
  506. ; Input:
  507. ; setuped sockaddr1
  508. ; Output:
  509. ; eax - error_code
  510. ; ebx - error_string
  511. ; esi  - ptr to DateTime
  512. ; Use registers (not restore):
  513. ; eax, edx, ecx, edx, esi,...
  514. ;  
  515. ;sntp_sync_time:
  516.   ;mov     edx, eax  
  517. sntp_query_time:
  518.  
  519.   ; if -ss & 59:59 => waiting for new hour
  520.   cmp [sync], SYNC_SS
  521.   jne @f
  522. .new_hour?:  
  523.   ; Query system time
  524.   mcall   3
  525.   cmp ah, 59h ; 59 min. ?
  526.   jne @f
  527.   shr eax, 16
  528.   cmp al, 59h ; 59 sec. ?
  529.   jne @f
  530.   ;DEBUGF  1, "SNTP: Waiting for new hour.\n"
  531.   ; Wait 100 msec.
  532.   mcall 5,10
  533.   jmp .new_hour?
  534. @@:  
  535.   ; Create socket
  536.   mcall   socket, AF_INET4, SOCK_DGRAM, IPPROTO_IP
  537.   cmp     eax, -1
  538.   jne   @f
  539.   mov   eax, 41
  540.   mov   ebx, str_err4
  541.   ret ; Connection error (1)
  542.      
  543. @@:
  544.   ;mov    [socketnum], eax
  545.   ;mcall   connect, [socketnum], sockaddr1, 18
  546.   mov   ebp, eax  ; Store socket
  547.  
  548.   mcall   connect, ebp, sockaddr1, 18
  549.   cmp     eax, -1
  550.   jne   @f
  551.   mov   edx, 42
  552.   mov   edi, str_err4
  553.   jmp .error ; Connection error (2)
  554.   ;DEBUGF  1, "Socket connected.\n"
  555.  
  556. @@:  
  557.   mcall   send, ebp, sntp_packet, SIZEOF_SNTP_PACKET, 0
  558.   cmp     eax, -1
  559.   jne   @f
  560.   mov   edx, 43
  561.   mov   edi, str_err4
  562.   jmp .error ; Connection error (3)
  563.   ;DEBUGF  1, "send done.\n"
  564.  
  565. @@:
  566.   ; Wait 300 msec.
  567.   mcall   5, 30
  568. ;do_recv:
  569.   mcall   recv, ebp, sntp_packet, SIZEOF_SNTP_PACKET, MSG_DONTWAIT
  570.  
  571.   cmp     eax, -1
  572.   jne   @f
  573.   mov   edx, 50
  574.   mov   edi, str_err5  
  575.   jmp .error ; no response
  576.   @@:
  577.   test    eax, eax
  578.   jnz     @f
  579.   mov   edx, 44
  580.   mov   edi, str_err4
  581.   jmp .error ; ; Connection error (4)
  582.  
  583.   ;DEBUGF  1, "recv done.\n"
  584.  
  585. @@:
  586.   ; Kiss of death?
  587.   cmp  [sntp_packet.Stratum], 0
  588.   jne   @f
  589.   mov   edx, 60
  590.   mov   edi, str_err6
  591.   jmp .error
  592.  
  593. @@:
  594.   ;  cmp   eax, SIZEOF_SNTP_PACKET
  595. ;  jne   do_recv
  596.  
  597.   ;push   sntp_packet.ReferenceID
  598.   ;call   [con_write_asciiz]
  599.   ;invoke       con_write_asciiz, str_refid
  600.  
  601.   ; TODO: calñ roudtrip
  602.   mov     eax, [sntp_packet.TransmitTime]
  603.   bswap   eax
  604.  
  605.   ; Bias between epoch
  606.   sub     eax, 0x83AA7E80  
  607.  
  608.   ;push eax
  609.  
  610.   ;cinvoke  con_printf, str_t, eax
  611.   ;cinvoke  con_printf, str_tt, [sntp_packet.TransmitTime], [sntp_packet.TransmitTime + 32]
  612.  
  613.   ;pop eax
  614.  
  615. ;.test:
  616.   mov ebx, datetime
  617. ;  mov eax, 7fffffffh ; max timestamp
  618.   call timestamp2DateTime
  619.  
  620.    
  621. ;  mov esi, datetime
  622.  
  623.   ; Calc time zone (hour only)
  624. ;  mov eax, [SystemTime]
  625. ;  clear ecx
  626. ;  mov   cl, al
  627. ;  clear eax
  628. ;  mov al, [esi + DateTime.hour]
  629. ;  push ecx
  630. ;  b2bcd
  631. ;  pop ecx
  632. ;  sub cl, al
  633.   ;sub bh, [esi + DateTime.min]  
  634.  
  635.   ; correct minutes
  636.   clear eax, ebx, ecx    
  637.   mov al, [tz_m]
  638.   test al, al      ; tz_m = 0 ?
  639.   jz .tz_h
  640.   mov bl, [tz_h]  ; tz_m < 0 ? This not work with tz_h = 0!
  641.   test bl,bl
  642.   jns @f
  643.   sub [esi + DateTime.min], al
  644.   cmp [esi + DateTime.min], 0
  645.   jge @f
  646.   mov al, 60
  647.   mov cl, -1
  648.  
  649.   add [esi + DateTime.min], al;bh
  650.   ;mov [TimeZone], cx
  651.   ;@@:
  652. @@:
  653. .tz_h:  
  654.   ; correct hour    
  655. ;  cmp [sync], SYNC_SS
  656.   ; if -ss ignore timezone for hour
  657. ;  je .tz_done
  658.   clear eax
  659.   mov al, [tz_h]
  660.   add al, cl  
  661.   add [esi + DateTime.hour], al ;3 ; MSK = GMT +3
  662.  
  663.   ; Correct day & hour if prev/new day
  664.   ; hour < 0 ?
  665.   mov   al,[esi + DateTime.hour]
  666.   test  al, al
  667.   jns @f
  668.   add [esi + DateTime.hour],24
  669.   dec [esi + DateTime.day]
  670.   jmp .tz_done    
  671.  
  672. @@:        
  673.   ; hour >= 24 ?
  674.   cmp [esi + DateTime.hour], 24
  675.   jl .tz_done
  676.   inc [esi + DateTime.day]
  677.   mov bl, [esi + DateTime.hour]
  678.   sub ebx, 24
  679.   mov [esi + DateTime.hour],bl
  680.    
  681. .tz_done:
  682.  
  683.   ; {{
  684.   ; Removed block 1.1
  685.   ;}}
  686.  
  687.   ;clear eax, ebx, ecx
  688.   ;mov   al, '+'
  689.   ;mov ax, [TimeZone]
  690.   ;mov bl, al
  691.   ;mov cl, ah
  692.  
  693.   ; {{
  694.   ; Removed block 1.2
  695.   ;}}
  696.  
  697.   ; FIXED:  do sync before display!!!
  698.   ;         It's need to do sync fast ASAP
  699.   ;         Take out any printf from sntp_query_time!
  700.   ; sync > 0 ?
  701.   cmp [sync], 0
  702.   je .nosync
  703.  
  704.   ;{{
  705.   ; Removed block 2
  706.   ; FIXME: Go it from sntp_query_time!
  707.   ;}}
  708.  
  709.  
  710.  
  711.         ; Convert time to BCD  
  712.   clear eax, edx
  713.   mov al, [esi + DateTime.sec]
  714.   b2bcd
  715.   mov ecx, eax
  716.   shl ecx, 16
  717.   mov al, [esi + DateTime.min]
  718.   b2bcd  
  719.   mov ch, al
  720.  
  721.   cmp [sync], SYNC_SS
  722.   jne @f
  723.   ; if -ss ignore timezone for hour
  724.   ; Query system time
  725.   mcall   3  
  726.   jmp .ss_done
  727. @@:  
  728.   mov al, [esi + DateTime.hour]
  729.   b2bcd
  730. .ss_done:
  731.   mov cl, al  
  732.  
  733.   ; Display BCD time
  734.   ;cinvoke con_printf, str_t, ecx
  735.  
  736. ;  mov ecx, [ebx + DateTime.date]
  737. ;  push ecx
  738. ;  push str_d
  739. ;  call [con_printf]
  740. ;  add  esp, 2*4
  741.  
  742.  
  743.   ; Set time
  744.   mov eax, 22
  745.   mov ebx, 0
  746.   int 0x40
  747.  
  748.   ; ? error
  749.  
  750.   cmp [sync], SYNC_S
  751.   jne .nosync
  752.   ; Convert date to BCD
  753.   mov esi, datetime
  754.   clear eax, edx
  755.   mov al, [esi + DateTime.day]
  756.   b2bcd
  757.   mov ecx, eax
  758.   shl ecx, 16
  759.   mov al, [esi + DateTime.month]
  760.   b2bcd
  761.   mov ch, al
  762.   mov ax, [esi + DateTime.year]
  763.   sub ax, 2000
  764.   b2bcd
  765.   mov cl, al
  766.  
  767.   ; Display BCD date
  768.   ;cinvoke con_printf, str_d, ecx
  769.  
  770.   ; Set date
  771.   mov eax, 22
  772.   mov ebx, 1
  773. ;  mov ecx, [edx + DateTime.date]
  774.   int 0x40
  775.  
  776.   ; ? error
  777.  
  778. .nosync:
  779.  
  780.   ; Check LI field
  781. ;  mov al, byte [sntp_packet]
  782. ;  bt al,0
  783. ;  jnc @f
  784. ;  mov edx,1
  785. ;@@:
  786. ;  bt ax, 1
  787. ;  jnc @f
  788. ;  mov edx,2
  789. ;  jmp .error
  790.  
  791. @@:
  792.   clear edx, edi ; no error
  793.  
  794. .error:
  795.  
  796.   ; Close socket
  797.   mcall close, ebp    
  798.  
  799.   ; Ignore error from close,
  800.   ; but it write result to eax & ebx
  801.   ; so we need wtire result here
  802.   mov eax,edx
  803.   mov ebx,edi
  804.  
  805.    
  806.   ret
  807.  
  808. ;fail:
  809. ;  cinvoke con_printf, str_err, eax, ebx
  810. ;  ret
  811.  
  812. ; data
  813. str_title       db 'SNTP client',0
  814. str_help  db 'sntp host [-tz [-[+]]hh[:ss]] [-s]|[-st]|[-ss]',10
  815. ;str_help  db 'sntp host [-c][[-p] [-tz [-[+]]hh[:ss]] [-s]|[-st]|[-ss]]',10
  816. ;'-ñ    Load config from Time.ini',10
  817. ;'-p    Set port, default is 123',10
  818.           db 'host  Name or IP address of NTP/SNTP server',10
  819.           db 'Options:',10
  820.           db '-tz       Set time zone, default is GMT',10
  821.              ;'-qm      Try query time from master SNTP server (if stratum > 1), defautl is disabled',10
  822.           db 10
  823.           db 'Syncronization, default is disabled',10  
  824.           db '-s        System date and time',10
  825.           db '-st       System time (hours, mitutes and seconds) only',10
  826.           db '-ss       Save current hour (syncronize minutes and seconds only)',10    
  827.           db 10
  828. ;-dr    Display SNTP server reserence ID (if stratum = 0)
  829. ;-dt    Display accurate time from SNTP request, default is enabled
  830. ;-da    Display all SNTP server information
  831.           db 'Examples:',10
  832.           db 'sntp pool.ntp.org -tz 1 -s',10
  833.           db 'sntp 88.147.254.227 -tz 1 -ss',10,0
  834. ;str_badoption   db 'Warning: unknown option %s, ignored',10, 0  
  835. ;str_query db 'Query - %s [%s] :%i',10,0
  836. str_query db 'Query: %s',0
  837. str_ip    db ' [%s]',0
  838. str_port  db ' :%i',10,0
  839. ;str_tt   db 'Transmit time - 0x%X.%X',10,0
  840. ;str_t    db 'Timestamp - %i',10,0
  841. str_dt    db 'Date & time: %i.%02i.%02i %i:%02i:%02i GMT',0 ; ' UTC %%i:%02i'
  842. str_tz    db ' +%i:%02i',10,0  ;Time zone: GMT +%i:%02i
  843. ;str_d    db 'BCD date - 0x%08X',10,0
  844. ;str_t    db 'BCD time - 0x%08X',10,0
  845. str_err  db 'Error: #%i, %s => %s',0
  846. str_err10  db  'Bad command line, type ',39,'sntp',39,' for help.',10,0
  847. str_err11 db  'Incorrect time zone! Visit https://www.timeanddate.com/time/map for details.',10,0
  848. ;str_err2  db 'Can',39,'t run @NOTIFY!',10,0
  849. str_err3  db 'Host name not resolved!',0      ; @notify  => ntp.example.com \nError 30: Host name not resolved!
  850. str_err4  db 'Connection failed!',0           ; @notify  => ntp.example.com:100 \nError 4x: Connection failed!
  851. str_err5  db 'Host not responce!',0           ; @notify  => ntp.example.com:100 \nError 50: Host not responce!
  852. str_err6  db 'Received Kiss-o',39,'-Death (KoD) packet, repeat later or try another server!',0
  853.  
  854. ;str_err7 db 'Server clock not syncronizing!',0 Not needed, see RFC 4330 about LI field
  855. str_err7 db 'Clock syncronizing failed!',10,0
  856. ;str_err62 db 'Date syncronizing failed',0
  857. str_warn db 'Warning: Last minute will have %i seconds!',10,0              
  858. str_s     db 'Date & time',0
  859. str_st    db 'Time',0
  860. str_ss    db 'Mitutes & seconds',0
  861. str_sync  db '%s syncronized',10,0
  862. datetime  DateTime ?
  863. ;SystemTime     dd ?
  864. ;TimeZone        dw 180 ; GMT + 3
  865. ;Flags           db 0
  866.  
  867. port      dw 123
  868. tz_h      db 0
  869. tz_m      db 0
  870. sync      db 0
  871. ;notify  db 0
  872.  
  873. sockaddr1:
  874.         dw AF_INET4
  875. .port   dw 0x7b00     ; 123 in network order (big endian)
  876. .ip     dd 0
  877.         rb 10
  878.  
  879. SIZEOF_SNTP_PACKET  = 48        
  880. sntp_packet             db 0x23 ; Li = 0 Vn = 4 Mode = 3 (client)      
  881.   .Stratum              db 0
  882.         .Pool                     db 0
  883.         .Precision            db 0
  884.         .RootDelay            dd 0
  885.         .RootDispersion   dd 0
  886.   .ReferenceID      dd 0
  887.         .ReferenceTime    dq 0
  888.         .OriginateTime    dq 0
  889.         .ReceiveTime        dq 0
  890.   .TransmitTime     dd 0
  891.  
  892. ; import
  893. align 4
  894. @IMPORT:
  895.  
  896. library network, 'network.obj', console, 'console.obj'
  897. import network, \
  898.   inet_addr, 'inet_addr',  \
  899.   getaddrinfo,  'getaddrinfo',  \
  900.         freeaddrinfo,   'freeaddrinfo', \
  901.         inet_ntoa,      'inet_ntoa'
  902.  
  903. ;       con_start,      'START',        \
  904. import  console,        \
  905.         con_init,       'con_init',     \
  906.         con_write_asciiz,       'con_write_asciiz',     \
  907.         con_printf,       'con_printf',     \
  908.         con_exit,       'con_exit',     \
  909.         con_gets,       'con_gets',\
  910.         con_cls,        'con_cls',\
  911.         con_getch2,     'con_getch2',\
  912.         con_set_cursor_pos, 'con_set_cursor_pos',\
  913.         con_get_flags,  'con_get_flags'
  914.   ;, \
  915.   ;con_set_flags, 'con_set_flags'
  916.  
  917. ;socketnum      dd ?
  918.  
  919. I_END:
  920.                 rb 4096
  921.     align 16
  922. ;buffer_ptr:     rb BUFFERSIZE 
  923. STACKTOP:
  924.  
  925. MEM:
  926. params  rb 1024
  927. ;buffer_ptr:     rb BUFFERSIZE
  928.      
  929.        
  930. IM_END: