flat assembler
Message board for the users of flat assembler.

Index > Windows > ftp and http server???

Author
Thread Post new topic Reply to topic
edfed



Joined: 20 Feb 2006
Posts: 4242
Location: 2018
edfed
hello!

hwo to make a ftp or http server using windows API?

what are the needed functions?

is it usefull to make it?
Post 16 May 2008, 14:53
View user's profile Send private message Visit poster's website Reply with quote
asmrox



Joined: 19 Jan 2008
Posts: 160
asmrox
ws2_32.dll

msdn

wsastartup
socket
closesocket
listen
bind
accept
recv
send
setsockopt - dont forget!


sockaddr_in
400 bytes buffer
timeval - for SO_RCVTIMEO/SO_SNDTIMEO
Post 17 May 2008, 02:59
View user's profile Send private message Reply with quote
OzzY



Joined: 19 Sep 2003
Posts: 1029
Location: Everywhere
OzzY
And the protocol knowledge. Wink

I made this question long time ago regarding HTTP: http://board.flatassembler.net/topic.php?t=7502
Post 19 May 2008, 22:51
View user's profile Send private message Reply with quote
LocoDelAssembly
Your code has a bug


Joined: 06 May 2005
Posts: 4633
Location: Argentina
LocoDelAssembly
For HTTP grab a copy of RFC 2616 and perhaps 2396 also (the latter is of secondary importance).

BTW, is Menuet web browser open-source? You could take some hints from there if it is.
Post 20 May 2008, 01:41
View user's profile Send private message Reply with quote
gunblade



Joined: 19 Feb 2004
Posts: 209
gunblade
As they say.. "heres one i made earlier":

Code:
format PE console
use32
entry start

PORT = 80

include '%fasminc%\win32a.inc'

macro print msg,msglen
{ common
    invoke WriteConsole,[stdout],msg,msglen,NULL,NULL
}

macro tcpsend msg,msglen
{ common
    invoke send,[peer],msg,msglen,NULL
}

section '.code' code readable executable

start:

invoke GetStdHandle,STD_OUTPUT_HANDLE
mov [stdout],eax
invoke WSAStartup,0101h,wsadata

mov [port], PORT

invoke socket,AF_INET,SOCK_STREAM,NULL
mov [sockfd], eax

mov eax, [port]
and eax, 0xffff
ror ax, 8
mov word [bindparms+2], ax
invoke bind,[sockfd],bindparms,16

invoke listen,[sockfd],50

acceptit:
invoke accept,[sockfd],peeraddr,sixteen
mov dword [peer], eax

invoke recv,[peer],buffer,bufferlen,0

mov ebx, buffer
cmp dword [ebx], 'GET '
je .1

jmp endpeer

.1:
xor ecx, ecx
inc ebx
cmp byte [ebx], '/'
je @f
jmp .1
.2:
@@:

inc ebx
cmp byte [ebx], ' '
je @f
cmp byte [ebx], '%'
je .hex
mov al, [ebx]
mov [buffer+ecx], al
inc ecx
jmp @b
@@:
mov byte [buffer+ecx], 0
cmp ecx, 0
jne @f
mov dword [buffer], 'inde'
mov dword [buffer+4], 'x.ht'
mov  word [buffer+8], 'ml'
mov  byte [buffer+10], 0
jmp @f

.hex:
inc ebx
mov dx, [ebx]
mov word [fd], dx
mov word [fd+2], 0
mov edi, fd
push ebx
mov ebx, 16
call StrToInt
pop ebx
mov [buffer+ecx], al
inc ebx
inc ecx
jmp .2

@@:
invoke CreateFile,buffer,GENERIC_READ,0,NULL,OPEN_EXISTING,FILE_ATTRIBUTE_ARCHIVE,NULL
cmp eax, 0ffffffffh
jne @f
tcpsend h404,h404.len
jmp endpeer
@@:
mov [fd], eax

invoke TransmitFile,[peer],[fd],0,0,NULL,transmitbuffer,NULL

invoke CloseHandle,[fd]

endpeer:

invoke CloseHandle,[peer]

jmp acceptit


exit:
call WSACleanup
invoke ExitProcess,0


IntToStr:
; eax = number, ebx = base, edi = buffer
push    ecx edx
xor ecx,ecx
  .new:
xor edx,edx
div ebx
push    edx
inc ecx
test    eax,eax
jnz .new
 .loop:
pop eax
add al,30h
cmp al,'9'
jng .ok
add al,7   
 .ok:
stosb
loop    .loop
mov al,0 
stosb
pop edx ecx
ret

StrToInt:
; edi = buffer, ebx = base
push    edx edi
xor eax,eax
xor edx,edx
 .loop:
mov dl,byte [edi]
test    dl,dl
jz  .end
imul    eax,ebx
sub dl,'0'
cmp dl,9
jle .ok
sub dl,7
  .ok:
add eax,edx
inc edi
jmp .loop
  .end:
pop edi edx
ret

section '.data' data readable writeable

h404 db 'HTTP/1.1 404 Not Found',13,10,13,10,'<HTML><BODY>404 Not Found</BODY></HTML>',10,13
.len = $ - h404
h200 db 'HTTP/1.1 200 OK',13,10,13,10
.len = $ - h200

transmitbuffer dd h200,h200.len,NULL,NULL

bindparms dw 2, 0
          dd 0, 0, 0

wsadata WSADATA
stdout rd 1
peeraddr rb 16
port rd 1
strlen rd 1
multiplier rd 1
integer rd 1
sixteen dd 16
ten dd 10
sockfd rd 1
peer rd 1
fd rd 1
fdlen rd 1
buffer rb 512
bufferlen = $ - buffer

section '.idata' import data readable writeable

library kernel,'KERNEL32.DLL',\
        winsock,'WSOCK32.DLL',\
        mswsock,'MSWSOCK.DLL'

import  kernel,\
        GetStdHandle,'GetStdHandle',\
        ExitProcess,'ExitProcess',\
        WriteConsole,'WriteConsoleA',\
        CreateFile,'CreateFileA',\
        CloseHandle,'CloseHandle',\
        GetCommandLine,'GetCommandLineA'

import  winsock,\
        WSAStartup,'WSAStartup',\
        WSACleanup,'WSACleanup',\
        WSAAsyncSelect,'WSAAsyncSelect',\
        gethostbyname,'gethostbyname',\
        socket,'socket',\
        bind,'bind',\
        listen,'listen',\
        accept,'accept',\
        connect,'connect',\
        recv,'recv',\
        send,'send',\
        closesocket,'closesocket'

import  mswsock,\
        TransmitFile,'TransmitFile'
    


I know i should use comments more, but you can check the invoke calls for what the main calls are.

I looked into using raw send, but TrasmitFile is actually better, because it does not require you to manually "map" chunks of the file before sending it, its all done in the API somewhere, and its done very well.

Oh yeah, this one doesnt send the file size, usually you would add a "File-Length:" to the 200 header, to say how big the file is, ive done this in my linux httpd, which i can post too if you want, but i never bothered to update the windows one, since i tend to rarely use windows.

Oh, and I wouldn't use this directly as a public server in any way, its lacking even the most basic security features (people can do http://host/../../../../somefile), again, my linux one has those features, but again, not updated the windows one (plus not sure if i can, i use chroot in linux to "lock" the user into the folder that its executed in)
Post 21 May 2008, 10:00
View user's profile Send private message Reply with quote
OzzY



Joined: 19 Sep 2003
Posts: 1029
Location: Everywhere
OzzY
gunblade, just curious, in how much time did you write this code?

The "non-commented" ASM code, that still works, gives the idea that this person wrote it very fast and has strong ASM knowledge.
That's cool if it was really quick to code! Very Happy
Post 22 May 2008, 03:28
View user's profile Send private message Reply with quote
gunblade



Joined: 19 Feb 2004
Posts: 209
gunblade
Heh, it did not take me long, but it is a very simple implementation, most of the execution time is spent in the API somewhere doing recv/send, the rest of the code is text-parsing (converting %XX to the character it stands for (which i just realised is actually kinda dangerous)).

The lack of comments is just because I dont bother commenting unless something really needs to be explained (or if i'm writing code to show/explain something to someone else), and all this code is quite simple in nature.

Anyway, for the original poster, if you are looking into making a ftpd, then the outlines would be the same (socket, recv, etc), but then you'd need to do authentication and parsing of commands coming from the client.

Edit: Also realised this code doesnt even do multithreading, which the linux version does. Will probably have to update it after my exams are over then I'll upload both versions (windows and linux) to the projects section, if someone is interested in them.
Post 22 May 2008, 13:25
View user's profile Send private message Reply with quote
Display posts from previous:
Post new topic Reply to topic

Jump to:  


< Last Thread | Next Thread >
Forum Rules:
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Copyright © 1999-2020, Tomasz Grysztar. Also on GitHub, YouTube, Twitter.

Website powered by rwasa.