2013-05-28 19:34:26 +02:00
|
|
|
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
|
|
|
;; ;;
|
2015-10-25 13:04:33 +01:00
|
|
|
;; Copyright (C) KolibriOS team 2010-2015. All rights reserved. ;;
|
2013-05-28 19:34:26 +02:00
|
|
|
;; Distributed under terms of the GNU General Public License ;;
|
|
|
|
;; ;;
|
|
|
|
;; telnet.asm - Telnet client for KolibriOS ;;
|
|
|
|
;; ;;
|
|
|
|
;; Written by hidnplayr@kolibrios.org ;;
|
|
|
|
;; ;;
|
|
|
|
;; GNU GENERAL PUBLIC LICENSE ;;
|
|
|
|
;; Version 2, June 1991 ;;
|
|
|
|
;; ;;
|
|
|
|
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
|
|
|
|
|
|
|
format binary as ""
|
|
|
|
|
2013-06-07 19:26:03 +02:00
|
|
|
BUFFERSIZE = 4096
|
2013-05-28 19:34:26 +02:00
|
|
|
|
|
|
|
use32
|
|
|
|
; standard header
|
|
|
|
db 'MENUET01' ; signature
|
|
|
|
dd 1 ; header version
|
|
|
|
dd start ; entry point
|
|
|
|
dd i_end ; initialized size
|
2013-05-30 05:56:07 +02:00
|
|
|
dd mem+4096 ; required memory
|
|
|
|
dd mem+4096 ; stack pointer
|
|
|
|
dd hostname ; parameters
|
2013-05-28 19:34:26 +02:00
|
|
|
dd 0 ; path
|
|
|
|
|
2013-06-07 19:26:03 +02:00
|
|
|
include '../../macros.inc'
|
2013-05-28 19:34:26 +02:00
|
|
|
purge mov,add,sub
|
2013-06-07 19:26:03 +02:00
|
|
|
include '../../proc32.inc'
|
|
|
|
include '../../dll.inc'
|
|
|
|
include '../../network.inc'
|
2013-05-28 19:34:26 +02:00
|
|
|
|
|
|
|
; entry point
|
|
|
|
start:
|
|
|
|
; load libraries
|
|
|
|
stdcall dll.Load, @IMPORT
|
|
|
|
test eax, eax
|
|
|
|
jnz exit
|
|
|
|
; initialize console
|
2015-10-25 13:04:33 +01:00
|
|
|
invoke con_start, 1
|
|
|
|
invoke con_init, 80, 25, 80, 25, title
|
2013-05-28 19:34:26 +02:00
|
|
|
|
|
|
|
; Check for parameters
|
2015-10-25 13:04:33 +01:00
|
|
|
cmp byte[hostname], 0
|
2013-05-28 19:34:26 +02:00
|
|
|
jne resolve
|
|
|
|
|
|
|
|
main:
|
2015-10-25 13:04:33 +01:00
|
|
|
invoke con_cls
|
2013-05-28 19:34:26 +02:00
|
|
|
; Welcome user
|
2015-10-25 13:04:33 +01:00
|
|
|
invoke con_write_asciiz, str1
|
2013-05-28 19:34:26 +02:00
|
|
|
|
2013-05-30 05:56:07 +02:00
|
|
|
prompt:
|
2013-05-28 19:34:26 +02:00
|
|
|
; write prompt
|
2015-10-25 13:04:33 +01:00
|
|
|
invoke con_write_asciiz, str2
|
|
|
|
; read string (wait for input)
|
2013-05-30 05:56:07 +02:00
|
|
|
mov esi, hostname
|
2015-10-25 13:04:33 +01:00
|
|
|
invoke con_gets, esi, 256
|
2013-05-28 19:34:26 +02:00
|
|
|
; check for exit
|
|
|
|
test eax, eax
|
|
|
|
jz done
|
2015-10-25 13:04:33 +01:00
|
|
|
cmp byte[esi], 10
|
2013-05-28 19:34:26 +02:00
|
|
|
jz done
|
|
|
|
|
|
|
|
resolve:
|
2015-10-25 13:04:33 +01:00
|
|
|
mov [sockaddr1.port], 23 shl 8 ; Port is in network byte order
|
2013-05-28 19:34:26 +02:00
|
|
|
|
2015-10-25 13:04:33 +01:00
|
|
|
; delete terminating newline from URL and parse port, if any.
|
2013-05-30 05:56:07 +02:00
|
|
|
mov esi, hostname
|
2013-05-28 19:34:26 +02:00
|
|
|
@@:
|
|
|
|
lodsb
|
2013-05-30 05:56:07 +02:00
|
|
|
cmp al, ':'
|
|
|
|
je .do_port
|
2013-05-28 19:34:26 +02:00
|
|
|
cmp al, 0x20
|
|
|
|
ja @r
|
2015-10-25 13:04:33 +01:00
|
|
|
mov byte[esi-1], 0
|
2013-05-30 05:56:07 +02:00
|
|
|
jmp .done
|
2013-05-28 19:34:26 +02:00
|
|
|
|
2013-05-30 05:56:07 +02:00
|
|
|
.do_port:
|
|
|
|
xor eax, eax
|
|
|
|
xor ebx, ebx
|
2015-10-25 13:04:33 +01:00
|
|
|
mov byte[esi-1], 0
|
2013-05-30 05:56:07 +02:00
|
|
|
.portloop:
|
|
|
|
lodsb
|
2015-10-25 13:04:33 +01:00
|
|
|
cmp al, ' '
|
2013-05-30 05:56:07 +02:00
|
|
|
jbe .port_done
|
|
|
|
sub al, '0'
|
|
|
|
jb hostname_error
|
|
|
|
cmp al, 9
|
|
|
|
ja hostname_error
|
2015-10-25 13:04:33 +01:00
|
|
|
lea ebx, [ebx*4+ebx]
|
2013-05-30 05:56:07 +02:00
|
|
|
shl ebx, 1
|
|
|
|
add ebx, eax
|
|
|
|
jmp .portloop
|
|
|
|
|
|
|
|
.port_done:
|
|
|
|
xchg bl, bh
|
|
|
|
mov [sockaddr1.port], bx
|
|
|
|
|
|
|
|
.done:
|
2013-05-28 19:34:26 +02:00
|
|
|
|
|
|
|
; resolve name
|
|
|
|
push esp ; reserve stack place
|
2015-10-25 13:04:33 +01:00
|
|
|
invoke getaddrinfo, hostname, 0, 0, esp
|
2013-05-28 19:34:26 +02:00
|
|
|
pop esi
|
|
|
|
; test for error
|
|
|
|
test eax, eax
|
2013-06-19 16:14:38 +02:00
|
|
|
jnz dns_error
|
2013-05-28 19:34:26 +02:00
|
|
|
|
2015-10-25 13:04:33 +01:00
|
|
|
invoke con_cls
|
|
|
|
invoke con_write_asciiz, str3
|
|
|
|
invoke con_write_asciiz, hostname
|
2013-05-30 05:56:07 +02:00
|
|
|
|
2013-05-28 19:34:26 +02:00
|
|
|
; write results
|
2015-10-25 13:04:33 +01:00
|
|
|
invoke con_write_asciiz, str8
|
2013-05-28 19:34:26 +02:00
|
|
|
|
|
|
|
; convert IP address to decimal notation
|
|
|
|
mov eax, [esi+addrinfo.ai_addr]
|
|
|
|
mov eax, [eax+sockaddr_in.sin_addr]
|
|
|
|
mov [sockaddr1.ip], eax
|
2015-10-25 13:04:33 +01:00
|
|
|
invoke inet_ntoa, eax
|
2013-05-28 19:34:26 +02:00
|
|
|
; write result
|
2015-10-25 13:04:33 +01:00
|
|
|
invoke con_write_asciiz, eax
|
2013-05-28 19:34:26 +02:00
|
|
|
; free allocated memory
|
2015-10-25 13:04:33 +01:00
|
|
|
invoke freeaddrinfo, esi
|
2013-05-28 19:34:26 +02:00
|
|
|
|
2015-10-25 13:04:33 +01:00
|
|
|
invoke con_write_asciiz, str9
|
2013-05-28 19:34:26 +02:00
|
|
|
|
|
|
|
mcall socket, AF_INET4, SOCK_STREAM, 0
|
|
|
|
cmp eax, -1
|
2013-06-19 16:14:38 +02:00
|
|
|
jz socket_err
|
2013-05-28 19:34:26 +02:00
|
|
|
mov [socketnum], eax
|
|
|
|
|
|
|
|
mcall connect, [socketnum], sockaddr1, 18
|
2013-10-13 23:35:17 +02:00
|
|
|
test eax, eax
|
|
|
|
jnz socket_err
|
2013-05-28 19:34:26 +02:00
|
|
|
|
2015-10-25 13:04:33 +01:00
|
|
|
mcall 40, EVM_STACK
|
|
|
|
invoke con_cls
|
2013-05-28 19:34:26 +02:00
|
|
|
|
|
|
|
mcall 18, 7
|
|
|
|
push eax
|
|
|
|
mcall 51, 1, thread, mem - 2048
|
|
|
|
pop ecx
|
|
|
|
mcall 18, 3
|
|
|
|
|
|
|
|
mainloop:
|
2015-10-25 13:04:33 +01:00
|
|
|
invoke con_get_flags
|
2013-05-28 19:34:26 +02:00
|
|
|
test eax, 0x200 ; con window closed?
|
|
|
|
jnz exit
|
|
|
|
|
|
|
|
mcall recv, [socketnum], buffer_ptr, BUFFERSIZE, 0
|
|
|
|
cmp eax, -1
|
2013-06-25 22:02:56 +02:00
|
|
|
je closed
|
2013-06-25 18:31:10 +02:00
|
|
|
|
2013-05-28 19:34:26 +02:00
|
|
|
mov esi, buffer_ptr
|
2015-10-25 13:04:33 +01:00
|
|
|
lea edi, [esi+eax]
|
|
|
|
mov byte[edi], 0
|
2013-05-28 19:34:26 +02:00
|
|
|
.scan_cmd:
|
2015-10-25 13:04:33 +01:00
|
|
|
cmp byte[esi], 0xff ; Interpret As Command
|
2013-05-28 19:34:26 +02:00
|
|
|
jne .no_cmd
|
2015-10-25 13:04:33 +01:00
|
|
|
; TODO: parse options
|
|
|
|
; for now, we will reply with 'WONT' to everything
|
|
|
|
mov byte[esi+1], 252 ; WONT
|
2013-05-28 19:34:26 +02:00
|
|
|
add esi, 3 ; a command is always 3 bytes
|
|
|
|
jmp .scan_cmd
|
|
|
|
.no_cmd:
|
|
|
|
|
|
|
|
cmp esi, buffer_ptr
|
2013-06-19 16:14:38 +02:00
|
|
|
je .print
|
2013-05-28 19:34:26 +02:00
|
|
|
|
|
|
|
push esi edi
|
|
|
|
sub esi, buffer_ptr
|
|
|
|
mcall send, [socketnum], buffer_ptr, , 0
|
|
|
|
pop edi esi
|
|
|
|
|
2013-06-19 16:14:38 +02:00
|
|
|
.print:
|
2013-05-28 19:34:26 +02:00
|
|
|
cmp esi, edi
|
2013-06-25 22:02:56 +02:00
|
|
|
jae mainloop
|
2013-05-28 19:34:26 +02:00
|
|
|
|
2015-10-25 13:04:33 +01:00
|
|
|
invoke con_write_asciiz, esi
|
2013-05-28 19:34:26 +02:00
|
|
|
|
2013-06-19 16:14:38 +02:00
|
|
|
.loop:
|
|
|
|
lodsb
|
|
|
|
test al, al
|
|
|
|
jz .print
|
|
|
|
jmp .loop
|
2013-05-28 19:34:26 +02:00
|
|
|
|
|
|
|
|
2013-06-19 16:14:38 +02:00
|
|
|
socket_err:
|
2015-10-25 13:04:33 +01:00
|
|
|
invoke con_write_asciiz, str6
|
2013-05-30 05:56:07 +02:00
|
|
|
jmp prompt
|
2013-05-28 19:34:26 +02:00
|
|
|
|
2013-06-19 16:14:38 +02:00
|
|
|
dns_error:
|
2015-10-25 13:04:33 +01:00
|
|
|
invoke con_write_asciiz, str5
|
2013-05-30 05:56:07 +02:00
|
|
|
jmp prompt
|
|
|
|
|
|
|
|
hostname_error:
|
2015-10-25 13:04:33 +01:00
|
|
|
invoke con_write_asciiz, str11
|
2013-05-30 05:56:07 +02:00
|
|
|
jmp prompt
|
2013-05-28 19:34:26 +02:00
|
|
|
|
2013-06-25 18:31:10 +02:00
|
|
|
closed:
|
2015-10-25 13:04:33 +01:00
|
|
|
invoke con_write_asciiz, str12
|
2013-06-25 18:31:10 +02:00
|
|
|
jmp prompt
|
|
|
|
|
2013-05-28 19:34:26 +02:00
|
|
|
done:
|
2015-10-25 13:04:33 +01:00
|
|
|
invoke con_exit, 1
|
2013-05-28 19:34:26 +02:00
|
|
|
exit:
|
|
|
|
|
|
|
|
mcall close, [socketnum]
|
|
|
|
mcall -1
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
thread:
|
|
|
|
mcall 40, 0
|
|
|
|
.loop:
|
2015-10-25 13:04:33 +01:00
|
|
|
invoke con_getch2
|
2013-10-03 20:46:31 +02:00
|
|
|
mov [send_data], ax
|
|
|
|
xor esi, esi
|
|
|
|
inc esi
|
|
|
|
test al, al
|
|
|
|
jnz @f
|
|
|
|
inc esi
|
|
|
|
@@:
|
|
|
|
mcall send, [socketnum], send_data
|
2013-05-28 19:34:26 +02:00
|
|
|
|
2015-10-25 13:04:33 +01:00
|
|
|
invoke con_get_flags
|
2013-05-28 19:34:26 +02:00
|
|
|
test eax, 0x200 ; con window closed?
|
|
|
|
jz .loop
|
|
|
|
mcall -1
|
|
|
|
|
|
|
|
; data
|
|
|
|
title db 'Telnet',0
|
2013-05-30 05:56:07 +02:00
|
|
|
str1 db 'Telnet for KolibriOS',10,10,\
|
2013-10-03 20:46:31 +02:00
|
|
|
'Please enter URL of telnet server (host:port)',10,10,\
|
|
|
|
'fun stuff:',10,\
|
|
|
|
'telehack.com - arpanet simulator',10,\
|
|
|
|
'towel.blinkenlights.nl - ASCII Star Wars',10,\
|
2014-04-03 23:41:37 +02:00
|
|
|
'nyancat.dakko.us - Nyan cat',10,10,0
|
2013-05-28 19:34:26 +02:00
|
|
|
str2 db '> ',0
|
2013-05-30 05:56:07 +02:00
|
|
|
str3 db 'Connecting to ',0
|
2013-05-28 19:34:26 +02:00
|
|
|
str4 db 10,0
|
|
|
|
str8 db ' (',0
|
|
|
|
str9 db ')',10,0
|
2013-05-30 05:56:07 +02:00
|
|
|
|
|
|
|
str5 db 'Name resolution failed.',10,10,0
|
|
|
|
str6 db 'Could not open socket.',10,10,0
|
|
|
|
str11 db 'Invalid hostname.',10,10,0
|
2013-06-25 18:31:10 +02:00
|
|
|
str12 db 10,'Remote host closed the connection.',10,10,0
|
2013-05-28 19:34:26 +02:00
|
|
|
|
|
|
|
sockaddr1:
|
|
|
|
dw AF_INET4
|
2013-05-30 05:56:07 +02:00
|
|
|
.port dw 0
|
2013-05-28 19:34:26 +02:00
|
|
|
.ip dd 0
|
|
|
|
rb 10
|
|
|
|
|
|
|
|
align 4
|
|
|
|
@IMPORT:
|
|
|
|
|
|
|
|
library network, 'network.obj', console, 'console.obj'
|
|
|
|
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',\
|
|
|
|
con_get_flags, 'con_get_flags'
|
|
|
|
|
|
|
|
|
|
|
|
i_end:
|
|
|
|
|
|
|
|
socketnum dd ?
|
|
|
|
buffer_ptr rb BUFFERSIZE+1
|
2013-10-03 20:46:31 +02:00
|
|
|
send_data dw ?
|
2013-05-30 05:56:07 +02:00
|
|
|
|
|
|
|
hostname rb 1024
|
2013-05-28 19:34:26 +02:00
|
|
|
|
|
|
|
mem:
|