Fixed Telnet for net branch.

Not the most elegant sollution but at least it works now!

git-svn-id: svn://kolibrios.org@2539 a494cfbc-eb01-0410-851d-a64ba20cac60
This commit is contained in:
hidnplayr 2012-04-02 12:06:31 +00:00
parent 38b226fb8d
commit f28853c51d

View File

@ -1,218 +1,225 @@
use32 use32
; standard header ; standard header
db 'MENUET01' ; signature db 'MENUET01' ; signature
dd 1 ; header version dd 1 ; header version
dd start ; entry point dd start ; entry point
dd i_end ; initialized size dd i_end ; initialized size
dd mem ; required memory dd mem ; required memory
dd mem ; stack pointer dd mem ; stack pointer
dd 0 ; parameters dd 0 ; parameters
dd 0 ; path dd 0 ; path
__DEBUG__ equ 0
__DEBUG_LEVEL__ equ 1
BUFFERSIZE equ 4096 BUFFERSIZE equ 4096
; useful includes ; useful includes
include '../macros.inc' include '../macros.inc'
purge mov,add,sub purge mov,add,sub
include '../proc32.inc' include '../proc32.inc'
include '../dll.inc' include '../dll.inc'
include '../debug-fdo.inc'
include '../network.inc' include '../network.inc'
; entry point ; entry point
start: start:
; load libraries ; load libraries
stdcall dll.Load, @IMPORT stdcall dll.Load, @IMPORT
test eax, eax test eax, eax
jnz exit jnz exit
; initialize console ; initialize console
push 1 push 1
call [con_start] call [con_start]
push title push title
push 25 push 25
push 80 push 80
push 25 push 25
push 80 push 80
call [con_init] call [con_init]
; main loop ; main loop
push str1 push str1
call [con_write_asciiz] call [con_write_asciiz]
main: main:
; write prompt ; write prompt
push str2 push str2
call [con_write_asciiz] call [con_write_asciiz]
; read string ; read string
mov esi, s mov esi, s
push 256 push 256
push esi push esi
call [con_gets] call [con_gets]
; check for exit ; check for exit
test eax, eax test eax, eax
jz done jz done
cmp byte [esi], 10 cmp byte [esi], 10
jz done jz done
; delete terminating '\n' ; delete terminating '\n'
push esi push esi
@@: @@:
lodsb lodsb
test al, al test al, al
jnz @b jnz @b
mov byte [esi-2], al mov byte [esi-2], al
pop esi pop esi
; resolve name ; resolve name
push esp ; reserve stack place push esp ; reserve stack place
push esp ; fourth parameter push esp ; fourth parameter
push 0 ; third parameter push 0 ; third parameter
push 0 ; second parameter push 0 ; second parameter
push esi ; first parameter push esi ; first parameter
call [getaddrinfo] call [getaddrinfo]
pop esi pop esi
; test for error ; test for error
test eax, eax test eax, eax
jnz fail jnz fail
; write results ; write results
push str3 push str3
call [con_write_asciiz] call [con_write_asciiz]
; mov edi, esi ; mov edi, esi
; convert IP address to decimal notation ; convert IP address to decimal notation
mov eax, [esi+addrinfo.ai_addr] mov eax, [esi+addrinfo.ai_addr]
mov eax, [eax+sockaddr_in.sin_addr] mov eax, [eax+sockaddr_in.sin_addr]
mov [sockaddr1.ip], eax mov [sockaddr1.ip], eax
push eax push eax
call [inet_ntoa] call [inet_ntoa]
; write result ; write result
push eax push eax
call [con_write_asciiz] call [con_write_asciiz]
; free allocated memory ; free allocated memory
push esi push esi
call [freeaddrinfo] call [freeaddrinfo]
push str4 push str4
call [con_write_asciiz] call [con_write_asciiz]
mcall socket, AF_INET4, SOCK_STREAM, 0 mcall socket, AF_INET4, SOCK_STREAM, 0
cmp eax, -1 cmp eax, -1
jz fail2 jz fail2
mov [socketnum], eax mov [socketnum], eax
mcall connect, [socketnum], sockaddr1, 18 mcall connect, [socketnum], sockaddr1, 18
mcall 40, 1 shl 7 ; + 7 mcall 40, 1 shl 7 ; + 7
call [con_cls] call [con_cls]
mcall 18, 7 mcall 18, 7
push eax push eax
mcall 51, 1, thread, mem - 2048 mcall 51, 1, thread, mem - 2048
pop ecx pop ecx
mcall 18, 3 mcall 18, 3
mainloop: mainloop:
mcall 10 DEBUGF 1, 'TELNET: Waiting for events\n'
mcall 10
DEBUGF 1, 'TELNET: EVENT %x !\n', eax
mcall recv, [socketnum], buffer_ptr, BUFFERSIZE, 0 mcall recv, [socketnum], buffer_ptr, BUFFERSIZE, 0
cmp eax, -1 cmp eax, -1
je mainloop je mainloop
mov esi, buffer_ptr DEBUGF 1, 'TELNET: got %u bytes of data !\n', eax
lea edi, [esi + eax]
mov byte [edi], 0 mov esi, buffer_ptr
lea edi, [esi + eax]
mov byte [edi], 0
.scan_cmd: .scan_cmd:
cmp byte [esi], 0xff ; Interpret As Command cmp byte [esi], 0xff ; Interpret As Command
jne .no_cmd jne .no_cmd
; TODO: parse options, for now, we will reply with 'WONT' to everything ; TODO: parse options, for now, we will reply with 'WONT' to everything
mov byte [esi + 1], 252 ; WONT mov byte [esi + 1], 252 ; WONT
add esi, 3 ; a command is always 3 bytes add esi, 3 ; a command is always 3 bytes
jmp .scan_cmd jmp .scan_cmd
.no_cmd: .no_cmd:
push esi cmp esi, buffer_ptr
je .print_loop
cmp esi, buffer_ptr DEBUGF 1, 'TELNET: sending data\n'
je .print_it
push edi push esi edi
mov edx, buffer_ptr sub esi, buffer_ptr
sub esi, buffer_ptr mcall send, [socketnum], buffer_ptr, , 0
xor edi, edi pop edi esi
mcall send, [socketnum]
pop edi
.print_it: .print_loop:
mov esi, [esp] DEBUGF 1, 'TELNET: printloop\n'
cmp esi, edi
jae mainloop
@@: cmp byte [esi], 0x1b ; escape character
cmp esi, edi jne .print_byte
jge .last_print mov byte [esi], 0
inc esi
cmp byte [esi], 0x1b ; escape character cmp word [esi], 0x485b ; move cursor to beginning
inc esi jne @f
jne @f inc esi
cmp word [esi], 0x485b ; move cursor to beginning inc esi
jne @f
mov byte [esi - 1], 0 DEBUGF 1, 'TELNET: resetting cursor \n'
call [con_write_asciiz]
push 0
push 0
call [con_set_cursor_pos]
inc esi push 0
inc esi push 0
push esi call [con_set_cursor_pos]
jmp @r jmp .print_loop
@@:
inc esi
jmp .print_loop
.print_byte:
push dword 1
push esi ; next string to print
inc esi
call [con_write_string]
jmp .print_loop
.last_print:
call [con_write_asciiz]
jmp mainloop
; write newline and continue main loop
push str4
@@:
call [con_write_asciiz]
jmp main
fail: fail:
push str5 push str5
jmp @b jmp @b
fail2: fail2:
push str6 push str6
jmp @b jmp @b
done: done:
push 1 push 1
call [con_exit] call [con_exit]
exit: exit:
mcall -1 mcall -1
thread: thread:
mcall 40, 0 mcall 40, 0
.loop: .loop:
call [con_getch2] call [con_getch2]
mov byte [send_data], al mov byte [send_data], al
mcall send, [socketnum], send_data, 1 mcall send, [socketnum], send_data, 1
jmp .loop jmp .loop
; data ; data
title db 'Telnet',0 title db 'Telnet',0
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 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
str2 db '> ',0 str2 db '> ',0
str3 db 'Connecting to: ',0 str3 db 'Connecting to: ',0
str4 db 10,0 str4 db 10,0
str5 db 'Name resolution failed.',10,10,0 str5 db 'Name resolution failed.',10,10,0
str6 db 'Could not open socket',10,10,0 str6 db 'Could not open socket',10,10,0
str7 db 'Got data!',10,10,0 str7 db 'Got data!',10,10,0
sockaddr1: sockaddr1:
dw AF_INET4 dw AF_INET4
.port dw 23 .port dw 23
.ip dd 0 .ip dd 0
rb 10 rb 10
include_debug_strings ; ALWAYS present in data section
@ -221,26 +228,29 @@ align 4
@IMPORT: @IMPORT:
library network, 'network.obj', console, 'console.obj' library network, 'network.obj', console, 'console.obj'
import network, \ import network, \
getaddrinfo, 'getaddrinfo', \ getaddrinfo, 'getaddrinfo', \
freeaddrinfo, 'freeaddrinfo', \ freeaddrinfo, 'freeaddrinfo', \
inet_ntoa, 'inet_ntoa' inet_ntoa, 'inet_ntoa'
import console, \ import console, \
con_start, 'START', \ con_start, 'START', \
con_init, 'con_init', \ con_init, 'con_init', \
con_write_asciiz, 'con_write_asciiz', \ con_write_asciiz, 'con_write_asciiz', \
con_exit, 'con_exit', \ con_exit, 'con_exit', \
con_gets, 'con_gets',\ con_gets, 'con_gets',\
con_cls, 'con_cls',\ con_cls, 'con_cls',\
con_getch2, 'con_getch2',\ con_getch2, 'con_getch2',\
con_set_cursor_pos, 'con_set_cursor_pos' con_set_cursor_pos, 'con_set_cursor_pos',\
con_write_string, 'con_write_string'
i_end: i_end:
socketnum dd ? socketnum dd ?
buffer_ptr rb BUFFERSIZE+1 buffer_ptr rb BUFFERSIZE+1
send_data rb 100 send_data rb 100
s rb 256 s rb 256
align 4 align 4
rb 4096 ; stack rb 4096 ; stack
mem: mem: