Subversion Repositories Kolibri OS

Rev

Rev 4743 | Show entire file | Regard whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 4743 Rev 5858
Line 1... Line 1...
1
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
2
;;                                                                 ;;
2
;;                                                                 ;;
3
;; Copyright (C) KolibriOS team 2010-2013. All rights reserved.    ;;
3
;; Copyright (C) KolibriOS team 2010-2015. All rights reserved.    ;;
4
;; Distributed under terms of the GNU General Public License       ;;
4
;; Distributed under terms of the GNU General Public License       ;;
5
;;                                                                 ;;
5
;;                                                                 ;;
6
;;  telnet.asm - Telnet client for KolibriOS                       ;;
6
;;  telnet.asm - Telnet client for KolibriOS                       ;;
7
;;                                                                 ;;
7
;;                                                                 ;;
8
;;  Written by hidnplayr@kolibrios.org                             ;;
8
;;  Written by hidnplayr@kolibrios.org                             ;;
Line 12... Line 12...
12
;;                                                                 ;;
12
;;                                                                 ;;
13
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
13
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
Line 14... Line 14...
14
 
14
 
Line 15... Line -...
15
format binary as ""
-
 
16
 
-
 
17
__DEBUG__       = 0
15
format binary as ""
Line 18... Line 16...
18
__DEBUG_LEVEL__ = 1
16
 
19
BUFFERSIZE      = 4096
17
BUFFERSIZE      = 4096
20
 
18
 
Line 31... Line 29...
31
 
29
 
32
include '../../macros.inc'
30
include '../../macros.inc'
33
purge mov,add,sub
31
purge mov,add,sub
34
include '../../proc32.inc'
32
include '../../proc32.inc'
35
include '../../dll.inc'
-
 
36
include '../../debug-fdo.inc'
33
include '../../dll.inc'
Line 37... Line 34...
37
include '../../network.inc'
34
include '../../network.inc'
38
 
35
 
39
; entry point
36
; entry point
40
start:
37
start:
41
; load libraries
38
; load libraries
42
        stdcall dll.Load, @IMPORT
39
        stdcall dll.Load, @IMPORT
43
        test    eax, eax
40
        test    eax, eax
44
        jnz     exit
-
 
45
; initialize console
41
        jnz     exit
46
        push    1
-
 
47
        call    [con_start]
-
 
48
        push    title
-
 
49
        push    25
-
 
50
        push    80
-
 
51
        push    25
42
; initialize console
Line 52... Line 43...
52
        push    80
43
        invoke  con_start, 1
53
        call    [con_init]
44
        invoke  con_init, 80, 25, 80, 25, title
54
 
45
 
Line 55... Line 46...
55
; Check for parameters
46
; Check for parameters
56
        cmp     byte [hostname], 0
47
        cmp     byte[hostname], 0
57
        jne     resolve
48
        jne     resolve
58
 
-
 
59
main:
49
 
Line 60... Line 50...
60
        call    [con_cls]
50
main:
61
; Welcome user
51
        invoke  con_cls
62
        push    str1
-
 
63
        call    [con_write_asciiz]
52
; Welcome user
64
 
53
        invoke  con_write_asciiz, str1
65
prompt:
54
 
66
; write prompt
-
 
67
        push    str2
-
 
68
        call    [con_write_asciiz]
55
prompt:
69
; read string
56
; write prompt
70
        mov     esi, hostname
57
        invoke  con_write_asciiz, str2
71
        push    256
58
; read string (wait for input)
72
        push    esi
59
        mov     esi, hostname
73
        call    [con_gets]
60
        invoke  con_gets, esi, 256
Line 74... Line 61...
74
; check for exit
61
; check for exit
-
 
62
        test    eax, eax
Line 75... Line -...
75
        test    eax, eax
-
 
76
        jz      done
-
 
77
        cmp     byte [esi], 10
63
        jz      done
78
        jz      done
64
        cmp     byte[esi], 10
79
 
65
        jz      done
80
resolve:
66
 
81
 
67
resolve:
82
        mov     [sockaddr1.port], 23 shl 8
68
        mov     [sockaddr1.port], 23 shl 8      ; Port is in network byte order
Line 96... Line 82...
96
        xor     eax, eax
82
        xor     eax, eax
97
        xor     ebx, ebx
83
        xor     ebx, ebx
98
        mov     byte [esi-1], 0
84
        mov     byte[esi-1], 0
99
  .portloop:
85
  .portloop:
100
        lodsb
86
        lodsb
101
        cmp     al, 0x20
87
        cmp     al, ' '
102
        jbe     .port_done
88
        jbe     .port_done
103
        sub     al, '0'
89
        sub     al, '0'
104
        jb      hostname_error
90
        jb      hostname_error
105
        cmp     al, 9
91
        cmp     al, 9
106
        ja      hostname_error
92
        ja      hostname_error
Line 115... Line 101...
115
 
101
 
Line 116... Line 102...
116
  .done:
102
  .done:
117
 
103
 
118
; resolve name
-
 
119
        push    esp     ; reserve stack place
-
 
120
        push    esp     ; ptr to result
-
 
121
        push    0       ; addrinfo hints
104
; resolve name
122
        push    0       ; servname
-
 
123
        push    hostname; hostname
105
        push    esp     ; reserve stack place
124
        call    [getaddrinfo]
106
        invoke  getaddrinfo, hostname, 0, 0, esp
125
        pop     esi
107
        pop     esi
126
; test for error
108
; test for error
Line 127... Line 109...
127
        test    eax, eax
109
        test    eax, eax
128
        jnz     dns_error
-
 
129
 
110
        jnz     dns_error
130
        call    [con_cls]
-
 
131
        push    str3
111
 
Line 132... Line 112...
132
        call    [con_write_asciiz]
112
        invoke  con_cls
133
        push    hostname
-
 
134
        call    [con_write_asciiz]
113
        invoke  con_write_asciiz, str3
135
 
-
 
Line 136... Line 114...
136
; write results
114
        invoke  con_write_asciiz, hostname
137
        push    str8
115
 
138
        call    [con_write_asciiz]
116
; write results
139
;        mov     edi, esi
117
        invoke  con_write_asciiz, str8
140
 
-
 
141
; convert IP address to decimal notation
118
 
142
        mov     eax, [esi+addrinfo.ai_addr]
119
; convert IP address to decimal notation
143
        mov     eax, [eax+sockaddr_in.sin_addr]
-
 
144
        mov     [sockaddr1.ip], eax
120
        mov     eax, [esi+addrinfo.ai_addr]
145
        push    eax
121
        mov     eax, [eax+sockaddr_in.sin_addr]
146
        call    [inet_ntoa]
-
 
147
; write result
122
        mov     [sockaddr1.ip], eax
Line 148... Line -...
148
        push    eax
-
 
149
        call    [con_write_asciiz]
123
        invoke  inet_ntoa, eax
Line 150... Line 124...
150
; free allocated memory
124
; write result
151
        push    esi
125
        invoke  con_write_asciiz, eax
152
        call    [freeaddrinfo]
126
; free allocated memory
153
 
127
        invoke  freeaddrinfo, esi
Line 154... Line 128...
154
        push    str9
128
 
155
        call    [con_write_asciiz]
129
        invoke  con_write_asciiz, str9
156
 
130
 
Line 157... Line 131...
157
        mcall   socket, AF_INET4, SOCK_STREAM, 0
131
        mcall   socket, AF_INET4, SOCK_STREAM, 0
158
        cmp     eax, -1
132
        cmp     eax, -1
Line 159... Line 133...
159
        jz      socket_err
133
        jz      socket_err
160
        mov     [socketnum], eax
134
        mov     [socketnum], eax
161
 
135
 
162
        mcall   connect, [socketnum], sockaddr1, 18
136
        mcall   connect, [socketnum], sockaddr1, 18
163
        test    eax, eax
137
        test    eax, eax
Line 164... Line 138...
164
        jnz     socket_err
138
        jnz     socket_err
165
 
139
 
166
        mcall   40, 1 shl 7 ; + 7
140
        mcall   40, EVM_STACK
167
        call    [con_cls]
141
        invoke  con_cls
Line 168... Line 142...
168
 
142
 
169
        mcall   18, 7
143
        mcall   18, 7
170
        push    eax
144
        push    eax
Line 171... Line -...
171
        mcall   51, 1, thread, mem - 2048
-
 
172
        pop     ecx
-
 
173
        mcall   18, 3
-
 
174
 
145
        mcall   51, 1, thread, mem - 2048
175
mainloop:
146
        pop     ecx
176
        call    [con_get_flags]
147
        mcall   18, 3
177
        test    eax, 0x200                      ; con window closed?
-
 
178
        jnz     exit
148
 
179
 
149
mainloop:
180
        mcall   recv, [socketnum], buffer_ptr, BUFFERSIZE, 0
150
        invoke  con_get_flags
-
 
151
        test    eax, 0x200                      ; con window closed?
181
        cmp     eax, -1
152
        jnz     exit
182
        je      closed
153
 
183
 
154
        mcall   recv, [socketnum], buffer_ptr, BUFFERSIZE, 0
184
 
155
        cmp     eax, -1
185
    DEBUGF  1, 'TELNET: got %u bytes of data !\n', eax
156
        je      closed
Line 186... Line 157...
186
 
157
 
187
        mov     esi, buffer_ptr
158
        mov     esi, buffer_ptr
Line 188... Line -...
188
        lea     edi, [esi + eax]
-
 
189
        mov     byte [edi], 0
-
 
190
 
159
        lea     edi, [esi+eax]
191
  .scan_cmd:
160
        mov     byte[edi], 0
192
        cmp     byte [esi], 0xff        ; Interpret As Command
161
  .scan_cmd:
193
        jne     .no_cmd
162
        cmp     byte[esi], 0xff         ; Interpret As Command
Line 194... Line 163...
194
        ; TODO: parse options, for now, we will reply with 'WONT' to everything
163
        jne     .no_cmd
195
        mov     byte [esi + 1], 252     ; WONT
164
; TODO: parse options
196
        add     esi, 3                  ; a command is always 3 bytes
165
; for now, we will reply with 'WONT' to everything
Line 197... Line -...
197
        jmp     .scan_cmd
-
 
198
  .no_cmd:
166
        mov     byte[esi+1], 252        ; WONT
Line 199... Line 167...
199
 
167
        add     esi, 3                  ; a command is always 3 bytes
200
        cmp     esi, buffer_ptr
168
        jmp     .scan_cmd
201
        je      .print
169
  .no_cmd:
202
 
170
 
203
    DEBUGF  1, 'TELNET: sending data\n'
171
        cmp     esi, buffer_ptr
Line 204... Line 172...
204
 
172
        je      .print
205
        push    esi edi
-
 
206
        sub     esi, buffer_ptr
-
 
207
        mcall   send, [socketnum], buffer_ptr, , 0
173
 
208
        pop     edi esi
-
 
209
 
174
        push    esi edi
Line 210... Line 175...
210
  .print:
175
        sub     esi, buffer_ptr
211
        cmp     esi, edi
-
 
212
        jae     mainloop
-
 
213
 
176
        mcall   send, [socketnum], buffer_ptr, , 0
214
        push    esi
-
 
215
        call    [con_write_asciiz]
177
        pop     edi esi
Line 216... Line 178...
216
 
178
 
217
  .loop:
-
 
218
        lodsb
179
  .print:
219
        test    al, al
180
        cmp     esi, edi
Line 220... Line 181...
220
        jz      .print
181
        jae     mainloop
221
        jmp     .loop
-
 
222
 
182
 
223
 
183
        invoke  con_write_asciiz, esi
Line 224... Line 184...
224
socket_err:
184
 
225
        DEBUGF  1, "TELNET: socket error %d", ebx
-
 
226
        push    str6
185
  .loop:
227
        call    [con_write_asciiz]
186
        lodsb
Line 228... Line 187...
228
 
187
        test    al, al
229
        jmp     prompt
188
        jz      .print
Line 230... Line 189...
230
 
189
        jmp     .loop
231
dns_error:
190
 
232
        DEBUGF  1, "TELNET: DNS error %d", eax
191
 
233
        push    str5
192
socket_err:
234
        call    [con_write_asciiz]
193
        invoke  con_write_asciiz, str6
235
 
194
        jmp     prompt
236
        jmp     prompt
195
 
237
 
196
dns_error:
238
hostname_error:
197
        invoke  con_write_asciiz, str5
239
        push    str11
198
        jmp     prompt
240
        call    [con_write_asciiz]
199
 
241
        jmp     prompt
200
hostname_error:
Line 242... Line 201...
242
 
201
        invoke  con_write_asciiz, str11
243
closed:
202
        jmp     prompt
244
        push    str12
203
 
245
        call    [con_write_asciiz]
204
closed:
Line 246... Line 205...
246
        jmp     prompt
205
        invoke  con_write_asciiz, str12
Line 296... Line 255...
296
        dw AF_INET4
255
        dw AF_INET4
297
.port   dw 0
256
.port   dw 0
298
.ip     dd 0
257
.ip     dd 0
299
        rb 10
258
        rb 10
Line 300... Line -...
300
 
-
 
301
include_debug_strings    ; ALWAYS present in data section
-
 
302
 
-
 
303
 
-
 
304
 
-
 
305
; import
259
 
306
align 4
260
align 4
Line 307... Line 261...
307
@IMPORT:
261
@IMPORT:
308
 
262