2011-02-03 15:37:36 +01:00
|
|
|
use32
|
|
|
|
; standard header
|
2012-04-03 20:35:40 +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
|
2011-02-03 15:37:36 +01:00
|
|
|
|
|
|
|
|
2012-04-03 20:35:40 +02:00
|
|
|
BUFFERSIZE equ 1500
|
2011-02-03 15:37:36 +01:00
|
|
|
; useful includes
|
|
|
|
include '../macros.inc'
|
|
|
|
purge mov,add,sub
|
|
|
|
include '../proc32.inc'
|
|
|
|
include '../dll.inc'
|
|
|
|
|
|
|
|
include '../network.inc'
|
|
|
|
|
|
|
|
; entry point
|
|
|
|
start:
|
|
|
|
; load libraries
|
2012-04-03 20:35:40 +02:00
|
|
|
stdcall dll.Load, @IMPORT
|
|
|
|
test eax, eax
|
|
|
|
jnz exit
|
2011-02-03 15:37:36 +01:00
|
|
|
|
|
|
|
; initialize console
|
2012-04-03 20:35:40 +02:00
|
|
|
push 1
|
|
|
|
call [con_start]
|
|
|
|
push title
|
|
|
|
push 25
|
|
|
|
push 80
|
|
|
|
push 25
|
|
|
|
push 80
|
|
|
|
call [con_init]
|
2011-02-03 15:37:36 +01:00
|
|
|
|
2012-04-03 20:35:40 +02:00
|
|
|
mcall 40, 1 shl 7 ; we only want network events
|
2011-02-03 15:37:36 +01:00
|
|
|
|
2012-04-03 20:35:40 +02:00
|
|
|
push str1
|
|
|
|
call [con_write_asciiz]
|
2011-02-03 15:37:36 +01:00
|
|
|
|
2012-04-03 20:35:40 +02:00
|
|
|
mcall socket, AF_INET4, SOCK_STREAM, 0
|
|
|
|
cmp eax, -1
|
|
|
|
je sock_err
|
2011-02-03 15:37:36 +01:00
|
|
|
|
2012-04-03 20:35:40 +02:00
|
|
|
mov [socketnum], eax
|
2011-02-03 15:37:36 +01:00
|
|
|
|
|
|
|
;; mcall setsockopt, [socketnum], SOL_SOCKET, SO_REUSEADDR, &yes,
|
|
|
|
;; cmp eax, -1
|
|
|
|
;; je opt_err
|
|
|
|
|
2012-04-03 20:35:40 +02:00
|
|
|
mcall bind, [socketnum], sockaddr1, sockaddr1.length
|
|
|
|
cmp eax, -1
|
|
|
|
je bind_err
|
2011-02-03 15:37:36 +01:00
|
|
|
|
2012-04-03 20:35:40 +02:00
|
|
|
mcall listen, [socketnum], 10 ; Backlog = 10
|
|
|
|
cmp eax, -1
|
|
|
|
je listen_err
|
2011-02-03 15:37:36 +01:00
|
|
|
|
2012-04-03 20:35:40 +02:00
|
|
|
push str2
|
|
|
|
call [con_write_asciiz]
|
2011-02-03 15:37:36 +01:00
|
|
|
|
2012-04-03 20:35:40 +02:00
|
|
|
mcall 10
|
2011-02-03 15:37:36 +01:00
|
|
|
|
2012-04-03 20:35:40 +02:00
|
|
|
mcall accept, [socketnum], sockaddr1, sockaddr1.length
|
|
|
|
cmp eax, -1
|
|
|
|
je acpt_err
|
2011-02-03 15:37:36 +01:00
|
|
|
|
2012-04-03 20:35:40 +02:00
|
|
|
mov [socketnum2], eax
|
2011-02-03 15:37:36 +01:00
|
|
|
|
|
|
|
;; mcall close, [socketnum]
|
|
|
|
|
2012-04-03 20:35:40 +02:00
|
|
|
mcall send, [socketnum2], hello, hello.length
|
2011-02-03 15:37:36 +01:00
|
|
|
|
|
|
|
.loop:
|
2012-04-03 20:35:40 +02:00
|
|
|
mcall 10
|
2011-02-03 15:37:36 +01:00
|
|
|
|
2012-04-03 20:35:40 +02:00
|
|
|
mcall recv, [socketnum2], buffer, buffer.length
|
|
|
|
cmp eax, -1
|
|
|
|
je .loop
|
2011-02-03 15:37:36 +01:00
|
|
|
|
2012-04-03 20:35:40 +02:00
|
|
|
mov byte [buffer + eax], 0
|
2011-02-03 15:37:36 +01:00
|
|
|
|
2012-04-03 20:35:40 +02:00
|
|
|
push buffer
|
|
|
|
call [con_write_asciiz]
|
|
|
|
|
|
|
|
jmp .loop
|
2011-02-03 15:37:36 +01:00
|
|
|
|
|
|
|
acpt_err:
|
2012-04-03 20:35:40 +02:00
|
|
|
push str8
|
|
|
|
call [con_write_asciiz]
|
|
|
|
jmp done
|
2011-02-03 15:37:36 +01:00
|
|
|
|
|
|
|
listen_err:
|
2012-04-03 20:35:40 +02:00
|
|
|
push str3
|
|
|
|
call [con_write_asciiz]
|
|
|
|
jmp done
|
2011-02-03 15:37:36 +01:00
|
|
|
|
|
|
|
bind_err:
|
2012-04-03 20:35:40 +02:00
|
|
|
push str4
|
|
|
|
call [con_write_asciiz]
|
|
|
|
jmp done
|
2011-02-03 15:37:36 +01:00
|
|
|
|
|
|
|
sock_err:
|
2012-04-03 20:35:40 +02:00
|
|
|
push str6
|
|
|
|
call [con_write_asciiz]
|
|
|
|
jmp done
|
2011-02-03 15:37:36 +01:00
|
|
|
|
|
|
|
done:
|
2012-04-03 20:35:40 +02:00
|
|
|
call [con_getch2]
|
|
|
|
push 1
|
|
|
|
call [con_exit]
|
2011-02-03 15:37:36 +01:00
|
|
|
exit:
|
2012-04-03 20:35:40 +02:00
|
|
|
mcall -1
|
2011-02-03 15:37:36 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
; data
|
2012-04-03 20:35:40 +02:00
|
|
|
title db 'TCP stream server - test',0
|
|
|
|
str1 db 'Opening socket',10, 0
|
|
|
|
str2 db 'Listening for incoming connections...',10,0
|
|
|
|
str3 db 'Listen error',10,10,0
|
|
|
|
str4 db 'Bind error',10,10,0
|
|
|
|
str5 db 'Setsockopt error.',10,10,0
|
|
|
|
str6 db 'Could not open socket',10,10,0
|
|
|
|
str7 db 'Got data!',10,10,0
|
|
|
|
str8 db 'Error accepting connection',10,10,0
|
|
|
|
|
|
|
|
hello db 'Hello world!',0
|
2011-02-03 15:37:36 +01:00
|
|
|
.length = $ - hello
|
|
|
|
|
|
|
|
sockaddr1:
|
2012-04-03 20:35:40 +02:00
|
|
|
dw AF_INET4
|
|
|
|
.port dw 23
|
|
|
|
.ip dd 0
|
|
|
|
rb 10
|
2011-02-03 15:37:36 +01:00
|
|
|
.length = $ - sockaddr1
|
|
|
|
|
|
|
|
; import
|
|
|
|
align 4
|
|
|
|
@IMPORT:
|
|
|
|
|
|
|
|
library console, 'console.obj'
|
|
|
|
|
2012-04-03 20:35:40 +02:00
|
|
|
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_printf, 'con_printf',\
|
|
|
|
con_getch2, 'con_getch2',\
|
|
|
|
con_set_cursor_pos, 'con_set_cursor_pos'
|
2011-02-03 15:37:36 +01:00
|
|
|
i_end:
|
|
|
|
|
2012-04-03 20:35:40 +02:00
|
|
|
socketnum dd ?
|
|
|
|
socketnum2 dd ?
|
|
|
|
buffer rb BUFFERSIZE
|
2011-02-03 15:37:36 +01:00
|
|
|
.length = BUFFERSIZE
|
|
|
|
|
2012-04-03 20:35:40 +02:00
|
|
|
align 4
|
|
|
|
rb 4096 ; stack
|
2011-02-03 15:37:36 +01:00
|
|
|
mem:
|