The MASM Forum Archive 2004 to 2012

General Forums => The Campus => Topic started by: Farabi on November 20, 2007, 02:36:19 AM

Title: Winsock question
Post by: Farabi on November 20, 2007, 02:36:19 AM
I have read madwizard tutorial and I know a bit thing about winsock.
Here is my code.


include ws2_32.inc

includelib ws2_32.lib

WM_FSOCKET equ WM_USER + 0fh



.data
wsadata WSADATA <>
sin sockaddr_in <>
.data?
sock dd ?
error_code dd ?
.code

fInitInternet proc hWnd:dword
; Mad wizard tutorial

invoke WSAStartup,101h,addr wsadata
.if eax!=0
xor eax,eax
dec eax
ret
.endif

invoke socket,AF_INET,SOCK_STREAM,0     ; Create a stream socket for internet use
.if eax!=INVALID_SOCKET
    mov sock,eax
invoke WSAAsyncSelect,sock,hWnd,WM_FSOCKET,FD_CONNECT+FD_READ+FD_CLOSE+FD_WRITE+FD_ACCEPT
.else
invoke MessageBox,hWnd,CADD("Internet connection initialization error"),CADD("Unknown cause"),MB_OK
.endif


ret
fInitInternet endp

fCloseInternet proc

invoke closesocket,sock
invoke WSACleanup

ret
fCloseInternet endp

fConnect proc uses esi edi lpszHostName:dword,nPortNumber:dword

mov sin.sin_family, AF_INET
invoke htons, nPortNumber
mov sin.sin_port,ax
invoke gethostbyname, lpszHostName
.if eax==0
invoke PERR
ret
.endif
mov eax,[eax+12]

mov eax,[eax]                      ; copy the pointer to the actual IP address into eax
mov eax,[eax]                      ; copy IP address into eax
mov sin.sin_addr,eax

invoke connect,sock,addr sin,sizeof sin
.if eax==SOCKET_ERROR
invoke WSAGetLastError         
.endif


ret
fConnect endp


The problem is, I cannot use send API. Everytime I use API I got this error message: "A request to send or receive data was disallowed because the  socket is not connected". I have use fConnect to connect the sockert and there is no error message. Anyone can explain what is my mistake and maybe guide me?
Title: Re: Winsock question
Post by: Tedd on November 20, 2007, 10:59:40 AM
When "connect" returns it does not mean the connection was successful (or failed) - the socket is not connected yet.
When the connection has completed, you will receive FD_CONNECT in your WM_FSOCKET message - it will say whether the connection was successful.
Read and understand ALL of the comments and explanation for "WSAAsyncSelect"
(Or, don't use it, and your socket will be blocking - so it will wait for connect/read/send to finish before returning.)
Title: Re: Winsock question
Post by: Mark Jones on November 20, 2007, 05:12:25 PM
Welcome back Farabi. :U
Title: Re: Winsock question
Post by: Farabi on November 21, 2007, 04:18:18 AM
Mark:
Thanks.

all:
I think I need a example source code of a working connection. anyone can help?
Title: Re: Winsock question
Post by: jj2007 on November 21, 2007, 02:44:52 PM
I stumbled over something odd a day ago: I installed a software package that tried to download an extra file, which triggered a message box by the firewall. Fine so far. What is odd is that on the same virgin pc I had installed my own baby (link in the signature), which also downloads files using urlmon.dll when pressing Control F5. No firewall message box!
Of course I am damn proud that I could cheat the firewall so easily, but still I'd like to know how I did it. I am sure MSIE uses urlmon; is it possible that an app "inherits" access rights from MSIE simply because it uses the same dll? If that is true, I would call it a security risk (or is it "by design"?). I know this is off topic but readers of "winsock question" might be in a position to solve the mystery...

Title: Re: Winsock question
Post by: Farabi on November 22, 2007, 03:50:00 AM
Is the waiting accept from my code is right?


mov ecx,SOCKET_ERROR
@@:
push ecx
invoke accept,sock,NULL,NULL
pushad
; invoke PERR
popad
pop ecx
cmp eax,ecx
jz @b


Here is the complete code.

fConnect proc uses esi edi lpszHostName:dword,nPortNumber:dword
LOCAL buff[128]:byte

mov sin.sin_family, AF_INET
invoke htons, nPortNumber
mov sin.sin_port,ax
invoke gethostbyname, lpszHostName
.if eax==0
invoke PERR
ret
.endif
mov eax,[eax+12]

mov eax,[eax]                      ; copy the pointer to the actual IP address into eax
mov eax,[eax]                      ; copy IP address into eax
xor eax,eax
mov sin.sin_addr,eax

invoke bind,sock,addr sin, sizeof sockaddr_in
.if eax!=0
invoke PERR
ret
.endif

invoke listen,sock,1
.if eax!=0
invoke PERR
ret
.endif

mov ecx,SOCKET_ERROR
@@:
push ecx
invoke accept,sock,NULL,NULL
pushad
; invoke PERR
popad
pop ecx
cmp eax,ecx
jz @b


; invoke connect,sock,addr sin,sizeof sin
; .if eax==SOCKET_ERROR
; invoke WSAGetLastError         
; .endif


ret
fConnect endp


Here is how I call the function


invoke fConnect,CADD("www.awardspace.com"),80


please help.
Title: Re: Winsock question
Post by: hoverlees on November 22, 2007, 04:35:36 AM
Hi Farabi,
see client.asm!

[attachment deleted by admin]
Title: Re: Winsock question
Post by: Farabi on November 28, 2007, 12:17:29 AM
Quote from: hoverlees on November 22, 2007, 04:35:36 AM
Hi Farabi,
see client.asm!

This is cool  :U
Title: Re: Winsock question
Post by: Farabi on November 28, 2007, 02:48:56 AM
Hai hoverlees your client.exe is working well, but Im still cannot get it working when I implement it to my code. I sent my full source code to your email, I hope you can read it.
My email account is realvampire2006@yahoo.co.id please check it.
Title: Re: Winsock question
Post by: Farabi on November 30, 2007, 12:57:21 AM
hi,
I try to get the main page on internet using this command "GET / HTTP/1.0" and its work, but when I send it for the second time I got a message "an established connection was aborted by the software in your host machine". why is that happen? I think last night it didnot appear.