Rev 3618 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
3545 | hidnplayr | 1 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; |
2 | ;; ;; |
||
3618 | hidnplayr | 3 | ;; Copyright (C) KolibriOS team 2010-2013. All rights reserved. ;; |
3545 | hidnplayr | 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 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; |
||
14 | |||
4012 | hidnplayr | 15 | ; TODO: more precise timer, ttl, user selectable size/number of packets |
16 | |||
3545 | hidnplayr | 17 | format binary as "" |
18 | |||
3618 | hidnplayr | 19 | BUFFERSIZE = 1500 |
4012 | hidnplayr | 20 | IDENTIFIER = 0x1337 |
3618 | hidnplayr | 21 | |
3545 | hidnplayr | 22 | use32 |
23 | org 0x0 |
||
24 | |||
25 | db 'MENUET01' ; signature |
||
26 | dd 1 ; header version |
||
27 | dd start ; entry point |
||
28 | dd I_END ; initialized size |
||
29 | dd mem ; required memory |
||
30 | dd mem ; stack pointer |
||
31 | dd s ; parameters |
||
32 | dd 0 ; path |
||
33 | |||
34 | |||
35 | ; useful includes |
||
3618 | hidnplayr | 36 | include '../../macros.inc' |
3545 | hidnplayr | 37 | purge mov,add,sub |
3618 | hidnplayr | 38 | include '../../proc32.inc' |
39 | include '../../dll.inc' |
||
40 | include '../../network.inc' |
||
3545 | hidnplayr | 41 | |
42 | include 'icmp.inc' |
||
43 | |||
44 | |||
45 | start: |
||
46 | ; load libraries |
||
47 | stdcall dll.Load, @IMPORT |
||
48 | test eax, eax |
||
49 | jnz exit |
||
50 | ; initialize console |
||
51 | push 1 |
||
52 | call [con_start] |
||
53 | push title |
||
54 | push 25 |
||
55 | push 80 |
||
56 | push 25 |
||
57 | push 80 |
||
58 | call [con_init] |
||
59 | ; main loop |
||
60 | cmp byte[s], 0 |
||
61 | jne resolve |
||
62 | main: |
||
63 | ; write prompt |
||
64 | push str2 |
||
65 | call [con_write_asciiz] |
||
66 | ; read string |
||
67 | mov esi, s |
||
68 | push 256 |
||
69 | push esi |
||
70 | call [con_gets] |
||
71 | ; check for exit |
||
72 | test eax, eax |
||
73 | jz done |
||
74 | cmp byte [esi], 10 |
||
75 | jz done |
||
76 | ; delete terminating '\n' |
||
77 | push esi |
||
78 | @@: |
||
79 | lodsb |
||
80 | test al, al |
||
81 | jnz @b |
||
82 | mov byte [esi-2], al |
||
83 | pop esi |
||
84 | |||
85 | resolve: |
||
86 | ; resolve name |
||
87 | push esp ; reserve stack place |
||
88 | push esp ; fourth parameter |
||
89 | push 0 ; third parameter |
||
90 | push 0 ; second parameter |
||
91 | push s ; first parameter |
||
92 | call [getaddrinfo] |
||
93 | pop esi |
||
94 | ; test for error |
||
95 | test eax, eax |
||
96 | jnz fail |
||
97 | |||
98 | ; convert IP address to decimal notation |
||
99 | mov eax, [esi+addrinfo.ai_addr] |
||
100 | mov eax, [eax+sockaddr_in.sin_addr] |
||
101 | mov [sockaddr1.ip], eax |
||
102 | push eax |
||
103 | call [inet_ntoa] |
||
104 | ; write result |
||
105 | mov [ip_ptr], eax |
||
106 | |||
107 | push eax |
||
108 | |||
109 | ; free allocated memory |
||
110 | push esi |
||
111 | call [freeaddrinfo] |
||
112 | |||
113 | push str4 |
||
114 | call [con_write_asciiz] |
||
115 | |||
116 | mcall socket, AF_INET4, SOCK_RAW, IPPROTO_ICMP |
||
117 | cmp eax, -1 |
||
118 | jz fail2 |
||
119 | mov [socketnum], eax |
||
120 | |||
121 | mcall connect, [socketnum], sockaddr1, 18 |
||
122 | |||
123 | mcall 40, 1 shl 7 ; + 7 |
||
124 | ; call [con_cls] |
||
125 | |||
126 | mov [count], 4 |
||
127 | |||
128 | push str3 |
||
129 | call [con_write_asciiz] |
||
4012 | hidnplayr | 130 | |
3545 | hidnplayr | 131 | push [ip_ptr] |
132 | call [con_write_asciiz] |
||
133 | |||
4012 | hidnplayr | 134 | push (icmp_packet.length - ICMP_Packet.Data) |
135 | push str3b |
||
136 | call [con_printf] |
||
137 | |||
138 | mainloop: |
||
139 | inc [stats.tx] |
||
3545 | hidnplayr | 140 | mcall 26,9 |
141 | mov [time_reference], eax |
||
142 | mcall send, [socketnum], icmp_packet, icmp_packet.length, 0 |
||
143 | |||
144 | mcall 23, 300 ; 3 seconds time-out |
||
145 | mcall 26,9 |
||
146 | neg [time_reference] |
||
147 | add [time_reference], eax |
||
148 | |||
4012 | hidnplayr | 149 | mcall recv, [socketnum], buffer_ptr, BUFFERSIZE, MSG_DONTWAIT |
3545 | hidnplayr | 150 | cmp eax, -1 |
151 | je .no_response |
||
152 | |||
4012 | hidnplayr | 153 | sub eax, ICMP_Packet.Data |
154 | jb .no_response ; FIXME: use other error message? |
||
155 | mov [recvd], eax |
||
156 | |||
157 | cmp word[buffer_ptr + ICMP_Packet.Identifier], IDENTIFIER |
||
158 | jne .no_response ; FIXME: use other error message? |
||
159 | |||
160 | ; OK, we have a response, update stats and let the user know |
||
161 | inc [stats.rx] |
||
162 | mov eax, [time_reference] |
||
163 | add [stats.time], eax |
||
164 | |||
165 | push str11 ; TODO: print IP address of packet sender |
||
166 | call [con_write_asciiz] |
||
167 | |||
3545 | hidnplayr | 168 | ; validate the packet |
169 | lea esi, [buffer_ptr + ICMP_Packet.Data] |
||
4012 | hidnplayr | 170 | mov ecx, [recvd] |
3545 | hidnplayr | 171 | mov edi, icmp_packet.data |
4012 | hidnplayr | 172 | repe cmpsb |
3545 | hidnplayr | 173 | jne .miscomp |
174 | |||
4012 | hidnplayr | 175 | ; All OK, print to the user! |
3545 | hidnplayr | 176 | push [time_reference] |
4012 | hidnplayr | 177 | movzx eax, word[buffer_ptr + ICMP_Packet.SequenceNumber] |
178 | push eax |
||
179 | push [recvd] |
||
180 | |||
3545 | hidnplayr | 181 | push str7 |
182 | call [con_printf] |
||
183 | |||
184 | jmp continue |
||
185 | |||
4012 | hidnplayr | 186 | ; Error in packet, print it to user |
3545 | hidnplayr | 187 | .miscomp: |
188 | sub edi, icmp_packet.data |
||
189 | push edi |
||
190 | push str9 |
||
191 | call [con_printf] |
||
192 | jmp continue |
||
193 | |||
4012 | hidnplayr | 194 | ; Timeout! |
3545 | hidnplayr | 195 | .no_response: |
196 | push str8 |
||
197 | call [con_write_asciiz] |
||
198 | |||
4012 | hidnplayr | 199 | ; Send more ICMP packets ? |
3545 | hidnplayr | 200 | continue: |
201 | dec [count] |
||
202 | jz done |
||
4012 | hidnplayr | 203 | |
3545 | hidnplayr | 204 | mcall 5, 100 ; wait a second |
4012 | hidnplayr | 205 | |
206 | inc [icmp_packet.seq] |
||
3545 | hidnplayr | 207 | jmp mainloop |
208 | |||
4012 | hidnplayr | 209 | ; Done.. |
210 | done: |
||
211 | xor edx, edx |
||
212 | mov eax, [stats.time] |
||
213 | div [stats.rx] |
||
214 | push eax |
||
215 | push [stats.rx] |
||
216 | push [stats.tx] |
||
217 | push str12 |
||
218 | call [con_printf] |
||
3545 | hidnplayr | 219 | |
220 | push str10 |
||
221 | call [con_write_asciiz] |
||
222 | call [con_getch2] |
||
223 | push 1 |
||
224 | call [con_exit] |
||
4012 | hidnplayr | 225 | |
226 | ; Finally.. exit! |
||
3545 | hidnplayr | 227 | exit: |
228 | mcall -1 |
||
229 | |||
4012 | hidnplayr | 230 | ; DNS error |
3545 | hidnplayr | 231 | fail: |
232 | push str5 |
||
233 | call [con_write_asciiz] |
||
234 | jmp done |
||
4012 | hidnplayr | 235 | |
236 | ; Socket error |
||
3545 | hidnplayr | 237 | fail2: |
238 | push str6 |
||
239 | call [con_write_asciiz] |
||
240 | jmp done |
||
241 | |||
242 | |||
243 | ; data |
||
244 | title db 'ICMP - echo client',0 |
||
245 | str2 db '> ',0 |
||
4012 | hidnplayr | 246 | str3 db 'Pinging to ',0 |
247 | str3b db ' with %u data bytes',10,0 |
||
248 | |||
3545 | hidnplayr | 249 | str4 db 10,0 |
250 | str5 db 'Name resolution failed.',10,0 |
||
251 | str6 db 'Could not open socket',10,0 |
||
4012 | hidnplayr | 252 | |
253 | str11 db 'Answer: ',0 |
||
254 | str7 db 'bytes=%u seq=%u time=%u0 ms',10,0 |
||
255 | str8 db 'timeout!',10,0 |
||
256 | str9 db 'miscompare at offset %u',10,0 |
||
3545 | hidnplayr | 257 | str10 db 10,'Press any key to exit',0 |
258 | |||
4012 | hidnplayr | 259 | str12 db 10,'Ping stats:',10,'%u packets sent, %u packets received',10,'average response time=%u0 ms',10,0 |
260 | |||
3545 | hidnplayr | 261 | sockaddr1: |
262 | dw AF_INET4 |
||
263 | .port dw 0 |
||
264 | .ip dd 0 |
||
265 | rb 10 |
||
266 | |||
267 | time_reference dd ? |
||
268 | ip_ptr dd ? |
||
269 | count dd ? |
||
4012 | hidnplayr | 270 | recvd dd ? ; received number of bytes in last packet |
3545 | hidnplayr | 271 | |
4012 | hidnplayr | 272 | stats: |
273 | .tx dd 0 |
||
274 | .rx dd 0 |
||
275 | .time dd 0 |
||
3545 | hidnplayr | 276 | |
277 | ; import |
||
278 | align 4 |
||
279 | @IMPORT: |
||
280 | |||
281 | library network, 'network.obj', console, 'console.obj' |
||
282 | import network, \ |
||
283 | getaddrinfo, 'getaddrinfo', \ |
||
284 | freeaddrinfo, 'freeaddrinfo', \ |
||
285 | inet_ntoa, 'inet_ntoa' |
||
286 | |||
287 | import console, \ |
||
288 | con_start, 'START', \ |
||
289 | con_init, 'con_init', \ |
||
290 | con_write_asciiz, 'con_write_asciiz', \ |
||
291 | con_printf, 'con_printf', \ |
||
292 | con_exit, 'con_exit', \ |
||
293 | con_gets, 'con_gets',\ |
||
294 | con_cls, 'con_cls',\ |
||
295 | con_getch2, 'con_getch2',\ |
||
296 | con_set_cursor_pos, 'con_set_cursor_pos' |
||
297 | |||
298 | socketnum dd ? |
||
299 | |||
300 | icmp_packet: db 8 ; type |
||
301 | db 0 ; code |
||
302 | dw 0 ; |
||
4012 | hidnplayr | 303 | .id dw IDENTIFIER ; identifier |
304 | .seq dw 0x0000 ; sequence number |
||
305 | .data db 'abcdefghijklmnopqrstuvwxyz012345' |
||
3545 | hidnplayr | 306 | .length = $ - icmp_packet |
307 | |||
308 | I_END: |
||
309 | |||
310 | buffer_ptr rb BUFFERSIZE |
||
311 | |||
312 | s rb 1024 |
||
313 | rb 4096 ; stack |
||
314 | mem: |