Subversion Repositories Kolibri OS

Rev

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

Rev Author Line No. Line
2872 hidnplayr 1
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
2
;;                                                                 ;;
3
;; Copyright (C) KolibriOS team 2010-2012. All rights reserved.    ;;
4
;; Distributed under terms of the GNU General Public License       ;;
5
;;                                                                 ;;
6
;;  ping.asm - ICMP echo client for KolibriOS                      ;;
7
;;                                                                 ;;
8
;;  Written by hidnplayr@kolibrios.org                             ;;
9
;;                                                                 ;;
10
;;          GNU GENERAL PUBLIC LICENSE                             ;;
11
;;             Version 2, June 1991                                ;;
12
;;                                                                 ;;
13
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
2853 hidnplayr 14
 
2872 hidnplayr 15
 
2853 hidnplayr 16
format binary as ""
17
 
1541 hidnplayr 18
use32
2853 hidnplayr 19
        org     0x0
2872 hidnplayr 20
 
2853 hidnplayr 21
        db      'MENUET01'      ; signature
22
        dd      1               ; header version
23
        dd      start           ; entry point
24
        dd      I_END           ; initialized size
25
        dd      mem             ; required memory
26
        dd      mem             ; stack pointer
2872 hidnplayr 27
        dd      s               ; parameters
2853 hidnplayr 28
        dd      0               ; path
1541 hidnplayr 29
 
30
 
2853 hidnplayr 31
BUFFERSIZE      equ 1500
1541 hidnplayr 32
; useful includes
33
include '../macros.inc'
34
purge mov,add,sub
35
include '../proc32.inc'
36
include '../dll.inc'
37
include '../network.inc'
38
 
2872 hidnplayr 39
include 'icmp.inc'
1541 hidnplayr 40
 
41
 
42
start:
43
; load libraries
2853 hidnplayr 44
        stdcall dll.Load, @IMPORT
45
        test    eax, eax
46
        jnz     exit
1541 hidnplayr 47
; initialize console
2853 hidnplayr 48
        push    1
49
        call    [con_start]
50
        push    title
51
        push    25
52
        push    80
53
        push    25
54
        push    80
55
        call    [con_init]
1541 hidnplayr 56
; main loop
2872 hidnplayr 57
        cmp     byte[s], 0
58
        jne     resolve
1541 hidnplayr 59
main:
60
; write prompt
2853 hidnplayr 61
        push    str2
62
        call    [con_write_asciiz]
1541 hidnplayr 63
; read string
2853 hidnplayr 64
        mov     esi, s
65
        push    256
66
        push    esi
67
        call    [con_gets]
1541 hidnplayr 68
; check for exit
2853 hidnplayr 69
        test    eax, eax
70
        jz      done
71
        cmp     byte [esi], 10
72
        jz      done
1541 hidnplayr 73
; delete terminating '\n'
2853 hidnplayr 74
        push    esi
1541 hidnplayr 75
@@:
2853 hidnplayr 76
        lodsb
77
        test    al, al
78
        jnz     @b
79
        mov     byte [esi-2], al
80
        pop     esi
2872 hidnplayr 81
 
82
resolve:
1541 hidnplayr 83
; resolve name
2853 hidnplayr 84
        push    esp     ; reserve stack place
85
        push    esp     ; fourth parameter
86
        push    0       ; third parameter
87
        push    0       ; second parameter
2872 hidnplayr 88
        push    s       ; first parameter
2853 hidnplayr 89
        call    [getaddrinfo]
90
        pop     esi
1541 hidnplayr 91
; test for error
2853 hidnplayr 92
        test    eax, eax
93
        jnz     fail
1541 hidnplayr 94
 
95
; convert IP address to decimal notation
2853 hidnplayr 96
        mov     eax, [esi+addrinfo.ai_addr]
97
        mov     eax, [eax+sockaddr_in.sin_addr]
98
        mov     [sockaddr1.ip], eax
99
        push    eax
100
        call    [inet_ntoa]
1541 hidnplayr 101
; write result
2853 hidnplayr 102
        mov     [ip_ptr], eax
1541 hidnplayr 103
 
2853 hidnplayr 104
        push    eax
1541 hidnplayr 105
 
106
; free allocated memory
2853 hidnplayr 107
        push    esi
108
        call    [freeaddrinfo]
1541 hidnplayr 109
 
2853 hidnplayr 110
        push    str4
111
        call    [con_write_asciiz]
1541 hidnplayr 112
 
2853 hidnplayr 113
        mcall   socket, AF_INET4, SOCK_RAW, IPPROTO_ICMP
114
        cmp     eax, -1
115
        jz      fail2
116
        mov     [socketnum], eax
1541 hidnplayr 117
 
2853 hidnplayr 118
        mcall   connect, [socketnum], sockaddr1, 18
1541 hidnplayr 119
 
2853 hidnplayr 120
        mcall   40, 1 shl 7 ; + 7
1541 hidnplayr 121
;        call    [con_cls]
122
 
2853 hidnplayr 123
        mov     [count], 4
1541 hidnplayr 124
 
125
mainloop:
2853 hidnplayr 126
        push    str3
127
        call    [con_write_asciiz]
128
        push    [ip_ptr]
129
        call    [con_write_asciiz]
1541 hidnplayr 130
 
2853 hidnplayr 131
        mcall   26,9
132
        mov     [time_reference], eax
133
        mcall   send, [socketnum], icmp_packet, icmp_packet.length, 0
1541 hidnplayr 134
 
2853 hidnplayr 135
        mcall   23, 300 ; 3 seconds time-out
136
        mcall   26,9
137
        neg     [time_reference]
138
        add     [time_reference], eax
1541 hidnplayr 139
 
2853 hidnplayr 140
        mcall   recv, [socketnum], buffer_ptr, BUFFERSIZE, 0
141
        cmp     eax, -1
142
        je      .no_response
1541 hidnplayr 143
 
144
; validate the packet
2853 hidnplayr 145
        lea     esi, [buffer_ptr + ICMP_Packet.Data]
146
        mov     edi, icmp_packet.data
147
        mov     ecx, 32/4
148
        repe    cmpsd
149
        jne     .miscomp
1541 hidnplayr 150
 
2853 hidnplayr 151
        push    [time_reference]
152
        push    str7
153
        call    [con_printf]
1541 hidnplayr 154
 
2853 hidnplayr 155
        jmp     continue
1541 hidnplayr 156
 
157
  .miscomp:
2853 hidnplayr 158
        sub     edi, icmp_packet.data
159
        push    edi
160
        push    str9
161
        call    [con_printf]
162
        jmp     continue
1541 hidnplayr 163
 
164
  .no_response:
2853 hidnplayr 165
        push    str8
166
        call    [con_write_asciiz]
1541 hidnplayr 167
 
168
   continue:
2853 hidnplayr 169
        dec     [count]
170
        jz      done
171
        mcall   5, 100  ; wait a second
172
        inc     [icmp_packet.id]
173
        jmp     mainloop
1541 hidnplayr 174
 
175
 
176
 
177
done:
2853 hidnplayr 178
        push    str10
179
        call    [con_write_asciiz]
180
        call    [con_getch2]
181
        push    1
182
        call    [con_exit]
1541 hidnplayr 183
exit:
2853 hidnplayr 184
        mcall   -1
1541 hidnplayr 185
 
186
fail:
2853 hidnplayr 187
        push    str5
188
        call    [con_write_asciiz]
189
        jmp     done
1541 hidnplayr 190
fail2:
2853 hidnplayr 191
        push    str6
192
        call    [con_write_asciiz]
193
        jmp     done
1541 hidnplayr 194
 
195
 
196
; data
2872 hidnplayr 197
title   db      'ICMP - echo client',0
2853 hidnplayr 198
str2    db      '> ',0
2872 hidnplayr 199
str3    db      'Ping to ',0
2853 hidnplayr 200
str4    db      10,0
2872 hidnplayr 201
str5    db      'Name resolution failed.',10,0
202
str6    db      'Could not open socket',10,0
2853 hidnplayr 203
str7    db      ' time= %u0ms',10,0
204
str8    db      ' timeout!',10,0
205
str9    db      ' miscompare at offset %u',10,0
2872 hidnplayr 206
str10   db      10,'Press any key to exit',0
1541 hidnplayr 207
 
208
sockaddr1:
2853 hidnplayr 209
        dw AF_INET4
210
.port   dw 0
211
.ip     dd 0
212
        rb 10
1541 hidnplayr 213
 
2853 hidnplayr 214
time_reference  dd ?
215
ip_ptr          dd ?
216
count           dd ?
1541 hidnplayr 217
 
218
 
219
; import
220
align 4
221
@IMPORT:
222
 
223
library network, 'network.obj', console, 'console.obj'
2853 hidnplayr 224
import  network,        \
225
        getaddrinfo,    'getaddrinfo',  \
226
        freeaddrinfo,   'freeaddrinfo', \
227
        inet_ntoa,      'inet_ntoa'
1541 hidnplayr 228
 
2853 hidnplayr 229
import  console,        \
230
        con_start,      'START',        \
231
        con_init,       'con_init',     \
232
        con_write_asciiz,       'con_write_asciiz',     \
233
        con_printf,       'con_printf',     \
234
        con_exit,       'con_exit',     \
235
        con_gets,       'con_gets',\
236
        con_cls,        'con_cls',\
237
        con_getch2,     'con_getch2',\
238
        con_set_cursor_pos, 'con_set_cursor_pos'
1541 hidnplayr 239
 
2853 hidnplayr 240
socketnum       dd ?
1541 hidnplayr 241
 
2853 hidnplayr 242
icmp_packet:    db 8            ; type
243
                db 0            ; code
244
                dw 0            ;
245
 .id            dw 0x0000       ; identifier
246
 .seq           dw 0x0001       ; sequence number
247
 .data          db 'abcdefghijklmnopqrstuvwxyz012345678'
1541 hidnplayr 248
 .length = $ - icmp_packet
249
 
250
I_END:
251
 
2853 hidnplayr 252
buffer_ptr      rb BUFFERSIZE
1541 hidnplayr 253
 
2872 hidnplayr 254
s               rb 1024
255
                rb 4096    ; stack
1541 hidnplayr 256
mem: