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