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