Subversion Repositories Kolibri OS

Rev

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

Rev Author Line No. Line
2853 hidnplayr 1
format binary as ""
2
 
1282 hidnplayr 3
use32
4
; standard header
2539 hidnplayr 5
        db      'MENUET01'      ; signature
6
        dd      1               ; header version
7
        dd      start           ; entry point
8
        dd      i_end           ; initialized size
9
        dd      mem             ; required memory
10
        dd      mem             ; stack pointer
11
        dd      0               ; parameters
12
        dd      0               ; path
1282 hidnplayr 13
 
2539 hidnplayr 14
__DEBUG__ equ 0
15
__DEBUG_LEVEL__ equ 1
1282 hidnplayr 16
 
2539 hidnplayr 17
 
18
BUFFERSIZE      equ 4096
1282 hidnplayr 19
; useful includes
20
include '../macros.inc'
21
purge mov,add,sub
22
include '../proc32.inc'
1541 hidnplayr 23
include '../dll.inc'
2539 hidnplayr 24
include '../debug-fdo.inc'
1282 hidnplayr 25
 
26
include '../network.inc'
27
 
28
; entry point
29
start:
30
; load libraries
2539 hidnplayr 31
        stdcall dll.Load, @IMPORT
32
        test    eax, eax
33
        jnz     exit
1282 hidnplayr 34
; initialize console
2539 hidnplayr 35
        push    1
36
        call    [con_start]
37
        push    title
38
        push    25
39
        push    80
40
        push    25
41
        push    80
42
        call    [con_init]
1282 hidnplayr 43
; main loop
2539 hidnplayr 44
        push    str1
45
        call    [con_write_asciiz]
1282 hidnplayr 46
main:
47
; write prompt
2539 hidnplayr 48
        push    str2
49
        call    [con_write_asciiz]
1282 hidnplayr 50
; read string
2539 hidnplayr 51
        mov     esi, s
52
        push    256
53
        push    esi
54
        call    [con_gets]
1282 hidnplayr 55
; check for exit
2539 hidnplayr 56
        test    eax, eax
57
        jz      done
58
        cmp     byte [esi], 10
59
        jz      done
1282 hidnplayr 60
; delete terminating '\n'
2539 hidnplayr 61
        push    esi
1282 hidnplayr 62
@@:
2539 hidnplayr 63
        lodsb
64
        test    al, al
65
        jnz     @b
66
        mov     byte [esi-2], al
67
        pop     esi
1282 hidnplayr 68
; resolve name
2539 hidnplayr 69
        push    esp     ; reserve stack place
70
        push    esp     ; fourth parameter
71
        push    0       ; third parameter
72
        push    0       ; second parameter
73
        push    esi     ; first parameter
74
        call    [getaddrinfo]
75
        pop     esi
1282 hidnplayr 76
; test for error
2539 hidnplayr 77
        test    eax, eax
78
        jnz     fail
1282 hidnplayr 79
 
80
; write results
2539 hidnplayr 81
        push    str3
82
        call    [con_write_asciiz]
1282 hidnplayr 83
;        mov     edi, esi
84
 
85
; convert IP address to decimal notation
2539 hidnplayr 86
        mov     eax, [esi+addrinfo.ai_addr]
87
        mov     eax, [eax+sockaddr_in.sin_addr]
88
        mov     [sockaddr1.ip], eax
89
        push    eax
90
        call    [inet_ntoa]
1282 hidnplayr 91
; write result
2539 hidnplayr 92
        push    eax
93
        call    [con_write_asciiz]
1282 hidnplayr 94
; free allocated memory
2539 hidnplayr 95
        push    esi
96
        call    [freeaddrinfo]
1282 hidnplayr 97
 
2539 hidnplayr 98
        push    str4
99
        call    [con_write_asciiz]
1282 hidnplayr 100
 
2539 hidnplayr 101
        mcall   socket, AF_INET4, SOCK_STREAM, 0
102
        cmp     eax, -1
103
        jz      fail2
104
        mov     [socketnum], eax
1282 hidnplayr 105
 
2539 hidnplayr 106
        mcall   connect, [socketnum], sockaddr1, 18
1282 hidnplayr 107
 
2539 hidnplayr 108
        mcall   40, 1 shl 7 ; + 7
109
        call    [con_cls]
1282 hidnplayr 110
 
2539 hidnplayr 111
        mcall   18, 7
112
        push    eax
113
        mcall   51, 1, thread, mem - 2048
114
        pop     ecx
115
        mcall   18, 3
1282 hidnplayr 116
 
117
mainloop:
2539 hidnplayr 118
    DEBUGF  1, 'TELNET: Waiting for events\n'
119
        mcall   10
120
    DEBUGF  1, 'TELNET: EVENT %x !\n', eax
1282 hidnplayr 121
 
2539 hidnplayr 122
        mcall   recv, [socketnum], buffer_ptr, BUFFERSIZE, 0
123
        cmp     eax, -1
124
        je      mainloop
1282 hidnplayr 125
 
2539 hidnplayr 126
    DEBUGF  1, 'TELNET: got %u bytes of data !\n', eax
1282 hidnplayr 127
 
2539 hidnplayr 128
        mov     esi, buffer_ptr
129
        lea     edi, [esi + eax]
130
        mov     byte [edi], 0
131
 
2314 hidnplayr 132
  .scan_cmd:
2539 hidnplayr 133
        cmp     byte [esi], 0xff        ; Interpret As Command
134
        jne     .no_cmd
135
        ; TODO: parse options, for now, we will reply with 'WONT' to everything
136
        mov     byte [esi + 1], 252     ; WONT
137
        add     esi, 3                  ; a command is always 3 bytes
138
        jmp     .scan_cmd
2314 hidnplayr 139
  .no_cmd:
1282 hidnplayr 140
 
2539 hidnplayr 141
        cmp     esi, buffer_ptr
142
        je      .print_loop
1318 hidnplayr 143
 
2539 hidnplayr 144
    DEBUGF  1, 'TELNET: sending data\n'
2314 hidnplayr 145
 
2539 hidnplayr 146
        push    esi edi
147
        sub     esi, buffer_ptr
148
        mcall   send, [socketnum], buffer_ptr, , 0
149
        pop     edi esi
2314 hidnplayr 150
 
2539 hidnplayr 151
  .print_loop:
152
    DEBUGF  1, 'TELNET: printloop\n'
153
        cmp     esi, edi
154
        jae     mainloop
2314 hidnplayr 155
 
2539 hidnplayr 156
        cmp     byte [esi], 0x1b        ; escape character
157
        jne     .print_byte
158
        inc     esi
2314 hidnplayr 159
 
2539 hidnplayr 160
        cmp     word [esi], 0x485b      ; move cursor to beginning
161
        jne     @f
162
        inc     esi
163
        inc     esi
2314 hidnplayr 164
 
2539 hidnplayr 165
    DEBUGF  1, 'TELNET: resetting cursor \n'
1318 hidnplayr 166
 
2539 hidnplayr 167
        push    0
168
        push    0
169
        call    [con_set_cursor_pos]
170
        jmp     .print_loop
1282 hidnplayr 171
 
2539 hidnplayr 172
  @@:
173
        inc     esi
2541 hidnplayr 174
        inc     esi
2539 hidnplayr 175
        jmp     .print_loop
1282 hidnplayr 176
 
2539 hidnplayr 177
  .print_byte:
178
        push    dword 1
179
        push    esi                     ; next string to print
180
        inc     esi
181
        call    [con_write_string]
182
        jmp     .print_loop
1282 hidnplayr 183
 
184
 
185
fail:
2539 hidnplayr 186
        push    str5
2543 hidnplayr 187
        jmp     main
1282 hidnplayr 188
fail2:
2539 hidnplayr 189
        push    str6
2543 hidnplayr 190
        jmp     main
1282 hidnplayr 191
 
192
done:
2539 hidnplayr 193
        push    1
194
        call    [con_exit]
1282 hidnplayr 195
exit:
2539 hidnplayr 196
        mcall   -1
1282 hidnplayr 197
 
198
 
199
 
200
thread:
2539 hidnplayr 201
        mcall   40, 0
1318 hidnplayr 202
  .loop:
2539 hidnplayr 203
        call    [con_getch2]
204
        mov     byte [send_data], al
205
        mcall   send, [socketnum], send_data, 1
206
        jmp     .loop
1282 hidnplayr 207
 
208
; data
2539 hidnplayr 209
title   db      'Telnet',0
210
str1    db      'Telnet v0.1',10,' for KolibriOS # 1281 or later. ',10,10,'If you dont know where to connect to, try towel.blinkenlights.nl',10,10,0
211
str2    db      '> ',0
212
str3    db      'Connecting to: ',0
213
str4    db      10,0
214
str5    db      'Name resolution failed.',10,10,0
215
str6    db      'Could not open socket',10,10,0
216
str7    db      'Got data!',10,10,0
1282 hidnplayr 217
 
218
sockaddr1:
2539 hidnplayr 219
        dw AF_INET4
220
.port   dw 23
221
.ip     dd 0
222
        rb 10
1282 hidnplayr 223
 
2539 hidnplayr 224
include_debug_strings    ; ALWAYS present in data section
1282 hidnplayr 225
 
226
 
2539 hidnplayr 227
 
1282 hidnplayr 228
; import
229
align 4
230
@IMPORT:
231
 
232
library network, 'network.obj', console, 'console.obj'
2539 hidnplayr 233
import  network,        \
234
        getaddrinfo,    'getaddrinfo',  \
235
        freeaddrinfo,   'freeaddrinfo', \
236
        inet_ntoa,      'inet_ntoa'
237
import  console,        \
238
        con_start,      'START',        \
239
        con_init,       'con_init',     \
240
        con_write_asciiz,       'con_write_asciiz',     \
241
        con_exit,       'con_exit',     \
242
        con_gets,       'con_gets',\
243
        con_cls,        'con_cls',\
244
        con_getch2,     'con_getch2',\
245
        con_set_cursor_pos, 'con_set_cursor_pos',\
246
        con_write_string, 'con_write_string'
247
 
248
 
1282 hidnplayr 249
i_end:
250
 
2539 hidnplayr 251
socketnum       dd ?
252
buffer_ptr      rb BUFFERSIZE+1
253
send_data       rb 100
1282 hidnplayr 254
 
2539 hidnplayr 255
s       rb      256
256
align   4
257
rb      4096    ; stack
1282 hidnplayr 258
mem: