flat assembler
Message board for the users of flat assembler.

Index > Windows > Problem with winsock2

Thread Post new topic Reply to topic

Joined: 19 Feb 2004
Posts: 209
Almost done porting my httpd from linux to windows (well, the code is already ported, just got to get the damn thing to work Wink).
Problem im having, im using winsock, with the unix-style function (socket,accept), not the WSA* ones. But when i run the program, it crashes.
So, i put it into a debugger, and run it. And it ends up that when it calls socket, where it jumps to, doesnt seem to be real code, looks more like data than anything else. And if i read the data there, it actually shows: KERNEL32.DLL, ws2_32.dll, and then some more data, then the functions i use in my program.. So does this mean my program is actually jumping to my .idata section rather than the actual function?

PS: Ive tried using both wsock32.dll and ws2_32.dll (the latter is the one they tell you to use on msdn, the other i found lying around in my system folder so tried it out in case it was something wrong with the dll).

Oh, and the code, in case you can see anything i did wrong:

format PE console

include 'F:\windows\system32\include\win32a.inc'

macro tcpsend msg,msglen
pushd 0 msglen msg [peer]
call send

section '.code' code readable executable

pushd 2 1 2
call socket
mov [sockfd], eax

pushd 16 bindparams [peer]
call bind

pushd 10 [sockfd]
call listen

pushd sixteen peeraddr [sockfd]
call accept
mov dword [peer], eax

pushd 0 bufferlen buffer [peer]
call recv

mov ebx, buffer
cmp dword [ebx], 'GET '
jne endpeer

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

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
push ecx
jmp @f

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

pushd 0 buffer
call GetFileSize
cmp eax, 0
je @f
tcpsend h404,h404.len
jmp endpeer
mov [fdlen], eax

pushd 0 0 0 0 1 0 buffer
call CreateFile
mov [fd], eax

mov eax, [fdlen]
mov ebx, 10
mov edi, inuse
call IntToStr
sub edi, inuse
mov dword [inuse+edi-1], 0x0a0d0a0d
add edi, h200.len+3
mov [transbuffers+4], edi

pushd 0 transbuffers 0 0 0 [fd] [peer]
call TransmitFile

pushd [fd]
call CloseHandle

pushd [peer]
call CloseHandle
jmp acceptie

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

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

section '.data' data readable writeable

h404 db 'HTTP/1.1 404 Not Found',13,10,13,10,'<HTML><HEAD><TITLE>404 Not Found</TITLE></HEAD><BODY><H1>File Not Found</H1></BODY></HTML>',10,13
.len = $ - h404
h200 db 'HTTP/1.1 200 OK',13,10,'Content-Length: '
.len = $ - h200
inuse db 'Port already in use, exiting..',10
.len = $ - inuse

bindparams dw 2, 80
          dd 0, 0, 0

transbuffers dd h200,0,0,0

peeraddr rb 16
sixteen dd 16
sockfd rd 1
peer rd 1
fd rd 1
fdlen rd 1
buffer rb 512
bufferlen = $ - buffer

section '.idata' import data readable writeable

library kernel32,'KERNEL32.DLL',\

import mswsock,\

include 'F:\windows\system32\include\apia\kernel32.inc'
include 'F:\windows\system32\include\apia\wsock32.inc'    

[edit]Forgot the section '.code', added that in, didnt help though[/edit]

Post 05 Nov 2005, 10:50
View user's profile Send private message Reply with quote

Joined: 16 Jun 2003
Posts: 1137
Location: Russian Federation
did you call WSAStartup?
Post 05 Nov 2005, 18:18
View user's profile Send private message Visit poster's website AIM Address Yahoo Messenger MSN Messenger ICQ Number Reply with quote

Joined: 19 Feb 2004
Posts: 209
That would probably help, thanks comrade, im still used to the unix way Embarassed

Hmm, i thought it would, but it didnt. Now when i call WSAStartup, it jumps to the end of my file just like socket did. Seems to be jumping to a small amount of garbage which is lodged between the imports for kernel32.dll, and the imports for ws2_32.dll.
code for where it jumps to is:
JG SHORT httpd.004030C3
JNO SHORT httpd.00403175    

It takes that second jump and ends up somewhere in the padding of the file, so just loads of 0's, which causes it to segfault.

Any other ideas?

I'll try removing those includes and just writing the imports by hand, and see if it could be a problem with the includes, which i doubt, but you never know.
Post 05 Nov 2005, 19:27
View user's profile Send private message Reply with quote

Joined: 19 Feb 2004
Posts: 209
My bad,
forgot that windows functions you should call like call [functionname],
last time i coded for windows i just used the invoke macro, so forgot about that.

Until the next screwup,
gunblade Rolling Eyes
Post 05 Nov 2005, 19:47
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.

Powered by rwasa.