Rev 1282 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
1282 | hidnplayr | 1 | use32 |
2 | ; standard header |
||
3 | db 'MENUET01' ; signature |
||
4 | dd 1 ; header version |
||
5 | dd start ; entry point |
||
6 | dd i_end ; initialized size |
||
7 | dd mem ; required memory |
||
8 | dd mem ; stack pointer |
||
9 | dd 0 ; parameters |
||
10 | dd 0 ; path |
||
11 | |||
12 | |||
13 | BUFFERSIZE equ 1500 |
||
14 | ; useful includes |
||
15 | include '../macros.inc' |
||
16 | purge mov,add,sub |
||
17 | include '../proc32.inc' |
||
18 | include 'dll.inc' |
||
19 | |||
20 | include '../network.inc' |
||
21 | |||
22 | ; entry point |
||
23 | start: |
||
24 | ; load libraries |
||
25 | stdcall dll.Load, @IMPORT |
||
26 | test eax, eax |
||
27 | jnz exit |
||
28 | ; initialize console |
||
29 | push 1 |
||
30 | call [con_start] |
||
31 | push title |
||
1318 | hidnplayr | 32 | push 25 |
33 | push 80 |
||
34 | push 25 |
||
35 | push 80 |
||
1282 | hidnplayr | 36 | call [con_init] |
37 | ; main loop |
||
38 | push str1 |
||
39 | call [con_write_asciiz] |
||
40 | main: |
||
41 | ; write prompt |
||
42 | push str2 |
||
43 | call [con_write_asciiz] |
||
44 | ; read string |
||
45 | mov esi, s |
||
46 | push 256 |
||
47 | push esi |
||
48 | call [con_gets] |
||
49 | ; check for exit |
||
50 | test eax, eax |
||
51 | jz done |
||
52 | cmp byte [esi], 10 |
||
53 | jz done |
||
54 | ; delete terminating '\n' |
||
55 | push esi |
||
56 | @@: |
||
57 | lodsb |
||
58 | test al, al |
||
59 | jnz @b |
||
60 | mov byte [esi-2], al |
||
61 | pop esi |
||
62 | ; resolve name |
||
63 | push esp ; reserve stack place |
||
64 | push esp ; fourth parameter |
||
65 | push 0 ; third parameter |
||
66 | push 0 ; second parameter |
||
67 | push esi ; first parameter |
||
68 | call [getaddrinfo] |
||
69 | pop esi |
||
70 | ; test for error |
||
71 | test eax, eax |
||
72 | jnz fail |
||
73 | |||
74 | ; write results |
||
75 | push str3 |
||
76 | call [con_write_asciiz] |
||
77 | ; mov edi, esi |
||
78 | |||
79 | ; convert IP address to decimal notation |
||
80 | mov eax, [esi+addrinfo.ai_addr] |
||
81 | mov eax, [eax+sockaddr_in.sin_addr] |
||
82 | mov [sockaddr1.ip], eax |
||
83 | push eax |
||
84 | call [inet_ntoa] |
||
85 | ; write result |
||
86 | push eax |
||
87 | call [con_write_asciiz] |
||
88 | ; free allocated memory |
||
89 | push esi |
||
90 | call [freeaddrinfo] |
||
91 | |||
92 | push str4 |
||
93 | call [con_write_asciiz] |
||
94 | |||
95 | mcall socket, AF_INET4, IPPROTO_TCP, 0 |
||
96 | cmp eax, -1 |
||
97 | jz fail2 |
||
98 | mov [socketnum], eax |
||
99 | |||
100 | mcall connect, [socketnum], sockaddr1, 18 |
||
101 | |||
102 | mcall 40, 1 shl 7 ; + 7 |
||
103 | call [con_cls] |
||
104 | |||
1318 | hidnplayr | 105 | mcall 18, 7 |
106 | push eax |
||
1282 | hidnplayr | 107 | mcall 51, 1, thread, mem - 2048 |
1318 | hidnplayr | 108 | pop ecx |
109 | mcall 18, 3 |
||
1282 | hidnplayr | 110 | |
111 | mainloop: |
||
112 | mcall 10 |
||
113 | |||
114 | mcall recv, [socketnum], buffer_ptr, BUFFERSIZE, 0 |
||
115 | cmp eax, -1 |
||
116 | je mainloop |
||
117 | |||
118 | mov esi, buffer_ptr |
||
119 | mov byte [esi + eax], 0 |
||
120 | |||
121 | @@: |
||
1318 | hidnplayr | 122 | cmp byte [esi], 0xff ; Interpret As Command |
1282 | hidnplayr | 123 | jne @f |
124 | ; TODO: parse options, for now, we will reply with 'WONT' to everything |
||
125 | mov byte [esi + 1], 252 ; WONT |
||
126 | add esi, 3 ; a command is always 3 bytes |
||
127 | jmp @r |
||
128 | |||
1318 | hidnplayr | 129 | |
1282 | hidnplayr | 130 | @@: |
1318 | hidnplayr | 131 | cmp byte [esi], 0x1b ; escape character |
132 | jne @f |
||
133 | cmp word [esi+1], 0x485b ; move cursor to beginning |
||
134 | jne @f |
||
135 | push 0 |
||
136 | push 0 |
||
137 | call [con_set_cursor_pos] |
||
138 | add esi, 3 |
||
139 | |||
140 | |||
141 | @@: |
||
1282 | hidnplayr | 142 | push esi |
143 | |||
144 | cmp esi, buffer_ptr |
||
145 | je .nocommands |
||
146 | |||
147 | mov edx, buffer_ptr |
||
148 | sub esi, buffer_ptr |
||
149 | xor edi, edi |
||
150 | mcall send, [socketnum] |
||
151 | |||
152 | .nocommands: |
||
153 | call [con_write_asciiz] |
||
154 | jmp mainloop |
||
155 | |||
156 | |||
157 | ; write newline and continue main loop |
||
158 | push str4 |
||
159 | @@: |
||
160 | call [con_write_asciiz] |
||
161 | jmp main |
||
162 | fail: |
||
163 | push str5 |
||
164 | jmp @b |
||
165 | fail2: |
||
166 | push str6 |
||
167 | jmp @b |
||
168 | |||
169 | done: |
||
170 | push 1 |
||
171 | call [con_exit] |
||
172 | exit: |
||
173 | mcall -1 |
||
174 | |||
175 | |||
176 | |||
177 | thread: |
||
178 | mcall 40, 0 |
||
1318 | hidnplayr | 179 | .loop: |
1282 | hidnplayr | 180 | call [con_getch2] |
181 | mov byte [send_data], al |
||
182 | mcall send, [socketnum], send_data, 1 |
||
1318 | hidnplayr | 183 | jmp .loop |
1282 | hidnplayr | 184 | |
185 | ; data |
||
186 | title db 'Telnet',0 |
||
1318 | hidnplayr | 187 | 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 |
1282 | hidnplayr | 188 | str2 db '> ',0 |
189 | str3 db 'Connecting to: ',0 |
||
190 | str4 db 10,0 |
||
191 | str5 db 'Name resolution failed.',10,10,0 |
||
192 | str6 db 'Could not open socket',10,10,0 |
||
193 | str7 db 'Got data!',10,10,0 |
||
194 | |||
195 | sockaddr1: |
||
196 | dw AF_INET4 |
||
197 | .port dw 23 shl 8 |
||
198 | .ip dd 0 |
||
199 | rb 10 |
||
200 | |||
201 | |||
202 | |||
203 | ; import |
||
204 | align 4 |
||
205 | @IMPORT: |
||
206 | |||
207 | library network, 'network.obj', console, 'console.obj' |
||
208 | import network, \ |
||
209 | getaddrinfo, 'getaddrinfo', \ |
||
210 | freeaddrinfo, 'freeaddrinfo', \ |
||
211 | inet_ntoa, 'inet_ntoa' |
||
212 | import console, \ |
||
213 | con_start, 'START', \ |
||
214 | con_init, 'con_init', \ |
||
215 | con_write_asciiz, 'con_write_asciiz', \ |
||
216 | con_exit, 'con_exit', \ |
||
217 | con_gets, 'con_gets',\ |
||
218 | con_cls, 'con_cls',\ |
||
1318 | hidnplayr | 219 | con_getch2, 'con_getch2',\ |
220 | con_set_cursor_pos, 'con_set_cursor_pos' |
||
1282 | hidnplayr | 221 | i_end: |
222 | |||
223 | socketnum dd ? |
||
224 | buffer_ptr rb BUFFERSIZE |
||
225 | send_data rb 100 |
||
226 | |||
227 | s rb 256 |
||
228 | align 4 |
||
229 | rb 4096 ; stack |
||
230 | mem: |